do not import HTTPKerberosAuth

This commit is contained in:
Yuming Zhu 2017-12-21 15:04:25 +08:00
parent af1a85841b
commit c241d28c25
3 changed files with 20 additions and 12 deletions

View file

@ -50,7 +50,7 @@ from koji.util import md5_constructor
SSL_Error = None
try:
from OpenSSL.SSL import Error as SSL_Error
except Exception: #pragma: no cover
except Exception: # pragma: no cover
# the hub imports koji, and sometimes this import fails there
# see: https://cryptography.io/en/latest/faq/#starting-cryptography-using-mod-wsgi-produces-an-internalerror-during-a-call-in-register-osrandom-engine
# unfortunately the workaround at the above link does not always work, so
@ -65,10 +65,8 @@ import re
import requests
try:
import requests_kerberos
from requests_kerberos import HTTPKerberosAuth
except ImportError: #pragma: no cover
except ImportError: # pragma: no cover
requests_kerberos = None
HTTPKerberosAuth = None
import rpm
import shutil
import signal
@ -2208,7 +2206,7 @@ class ClientSession(object):
return host
def gssapi_login(self, principal=None, keytab=None, ccache=None, proxyuser=None):
if not HTTPKerberosAuth or not requests_kerberos:
if not requests_kerberos:
raise PythonImportError(
"Please install python-requests-kerberos to use GSSAPI."
)
@ -2243,7 +2241,7 @@ class ClientSession(object):
raise PythonImportError(e_str)
else:
kwargs['principal'] = principal
self.opts['auth'] = HTTPKerberosAuth(**kwargs)
self.opts['auth'] = requests_kerberos.HTTPKerberosAuth(**kwargs)
try:
# Depending on the server configuration, we might not be able to
# connect without client certificate, which means that the conn

View file

@ -17,7 +17,7 @@ class TestGSSAPI(unittest.TestCase):
maxDiff = None
@mock.patch('koji.HTTPKerberosAuth', new=None)
@mock.patch('koji.requests_kerberos', new=None)
def test_gssapi_disabled(self):
with self.assertRaises(ImportError):
self.session.gssapi_login()
@ -29,8 +29,8 @@ class TestGSSAPI(unittest.TestCase):
retry=False)
self.assertEqual(old_environ, dict(**os.environ))
@mock.patch('koji.requests_kerberos.__version__', new='0.9.0')
@mock.patch('koji.HTTPKerberosAuth')
@mock.patch('requests_kerberos.__version__', new='0.9.0')
@mock.patch('requests_kerberos.HTTPKerberosAuth')
def test_gssapi_login_keytab(self, HTTPKerberosAuth_mock):
principal = 'user@EXAMPLE.COM'
keytab = '/path/to/keytab'
@ -41,7 +41,7 @@ class TestGSSAPI(unittest.TestCase):
retry=False)
self.assertEqual(old_environ, dict(**os.environ))
@mock.patch('koji.requests_kerberos.__version__', new='0.7.0')
@mock.patch('requests_kerberos.__version__', new='0.7.0')
def test_gssapi_login_keytab_unsupported_requests_kerberos_version(self):
principal = 'user@EXAMPLE.COM'
keytab = '/path/to/keytab'

View file

@ -1,4 +1,5 @@
from __future__ import absolute_import
import unittest
# This is python-mock, not the rpm mock tool we know and love
@ -8,12 +9,21 @@ import koji
class KrbVTestCase(unittest.TestCase):
@mock.patch('koji.krbV', new=None)
@mock.patch('koji.HTTPKerberosAuth', new=None)
@mock.patch('koji.requests_kerberos', new=None)
def test_krbv_disabled(self):
"""Test that when krbV and gssapi are absent, we behave rationally"""
self.assertEquals(koji.krbV, None)
session = koji.ClientSession('whatever')
with self.assertRaises(ImportError):
session.krb_login()
@mock.patch('koji.krbV', autospec=True)
@mock.patch('requests_kerberos.__version__', new='0.7.0')
def test_krbv_old_requests_kerberos(self, krbV_mock):
"""Test that when krbV and gssapi are absent, we behave rationally"""
self.assertIsNotNone(koji.krbV)
session = koji.ClientSession('whatever')
with self.assertRaises(koji.AuthError) as cm:
session.krb_login(principal='any@somewhere.com')
self.assertEqual(cm.exception.args[0], 'cannot specify a principal without a keytab')