From a7fb3025c367e7b40545e319e58e6497a3963c22 Mon Sep 17 00:00:00 2001 From: Chenxiong Qi Date: Nov 01 2016 08:58:46 +0000 Subject: Replace krbV with python-gssapi Fix #133 Signed-off-by: Chenxiong Qi --- diff --git a/pyrpkg/__init__.py b/pyrpkg/__init__.py index 79419e6..2204f7a 100644 --- a/pyrpkg/__init__.py +++ b/pyrpkg/__init__.py @@ -27,6 +27,8 @@ import tempfile from ConfigParser import ConfigParser +import gssapi + from osbs.api import OSBS from osbs.conf import Configuration from six.moves import configparser @@ -45,12 +47,6 @@ else: # We need a subprocess that has check_call from kitchen.pycompat27 import subprocess -# Try to import krb, it's OK if it fails -try: - import krbV -except ImportError: - pass - class NullHandler(logging.Handler): """Null logger to avoid spurious messages, add a handler in app code""" @@ -845,16 +841,23 @@ class Commands(object): # Define some helper functions, they start with _ def _has_krb_creds(self): - # This function is lifted from /usr/bin/koji - if 'krbV' not in sys.modules: + """Test if there is usable initialized Kerberos credential + + :return: True if credential is initialized and not expired. Otherwise, False is returned. + :rtype: bool + """ + try: + cred = gssapi.creds.Credentials(usage='initiate') + except gssapi.exceptions.GSSError: + # GSSError is raised if credential cache is not initialized or has bad format. return False try: - ctx = krbV.default_context() - ccache = ctx.default_ccache() - princ = ccache.principal() # noqa - return True - except krbV.Krb5Error: + # We don't care about the value of attribute lifetime. Reading + # lifetime is only for testing if credential is usable. + cred.lifetime + except gssapi.exceptions.ExpiredCredentialsError: return False + return True def _run_command(self, cmd, shell=False, env=None, pipe=[], cwd=None): """Run the given command.