From fe6aeae7f4b5c367da1a30cd6e0db63603e67ad9 Mon Sep 17 00:00:00 2001 From: Chenxiong Qi Date: Nov 14 2016 02:23:33 +0000 Subject: Backwards compatible with krbV Fix #139 Currently, python-gssapi is not available in EPEL, especially el6. This patch is for the old version of OS to use krbV. Signed-off-by: Chenxiong Qi --- diff --git a/pyrpkg/__init__.py b/pyrpkg/__init__.py index 28a85a9..94374de 100644 --- a/pyrpkg/__init__.py +++ b/pyrpkg/__init__.py @@ -25,8 +25,6 @@ import six import sys import tempfile -import gssapi - from osbs.api import OSBS from osbs.conf import Configuration from six.moves import configparser @@ -39,6 +37,20 @@ from pyrpkg.lookaside import CGILookasideCache from pyrpkg.sources import SourcesFile from pyrpkg.utils import cached_property, log_result +try: + # Use gssapi to detect the Kerberos credential by default, even krbV is + # still available too. + import gssapi +except ImportError: + gssapi = None + +try: + # This is for backwards compatibility in old version OS where gssapi is not + # available. + import krbV +except ImportError: + krbV = None + if sys.version_info[0:2] >= (2, 5): import subprocess else: @@ -839,7 +851,7 @@ class Commands(object): return None # Define some helper functions, they start with _ - def _has_krb_creds(self): + def _has_krb_creds_by_gssapi(self): """Test if there is usable initialized Kerberos credential :return: True if credential is initialized and not expired. Otherwise, False is returned. @@ -858,6 +870,31 @@ class Commands(object): return False return True + def _has_krb_creds_by_krbv(self): + """Test if there is usable initialized Kerberos credential + + :return: True if credential is initialized and not expired. Otherwise, False is returned. + :rtype: bool + """ + try: + ctx = krbV.default_context() + ccache = ctx.default_ccache() + princ = ccache.principal() # noqa + return True + except krbV.Krb5Error: + return False + + def _has_krb_creds_default(self): + """Kerberos authentication is disabled if neither gssapi nor krbV is available""" + return False + + if gssapi: + _has_krb_creds = _has_krb_creds_by_gssapi + elif krbV: + _has_krb_creds = _has_krb_creds_by_krbv + else: + _has_krb_creds = _has_krb_creds_default + def _run_command(self, cmd, shell=False, env=None, pipe=[], cwd=None): """Run the given command.