Skip to content

Commit 3a0b175

Browse files
committed
implement Kerberos auth
this tries to use requests-gssapi and falls back to requests-kerberos
1 parent fbe1e2a commit 3a0b175

File tree

6 files changed

+31
-1
lines changed

6 files changed

+31
-1
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
run: sudo mkdir -p /etc/rhsm/ca/ && sudo curl -o /etc/rhsm/ca/redhat-uep.pem https://ftp.redhat.com/redhat/convert2rhel/redhat-uep.pem
4343
- name: Install system dependencies
4444
# libyaml-dev for PyYAML, rpm for rpm-py-installer, python3-rpm for rpm-shim
45-
run: sudo apt-get install -y libyaml-dev rpm python3-rpm
45+
run: sudo apt-get install -y libyaml-dev rpm python3-rpm libkrb5-dev build-essential --no-install-recommends
4646
- name: Fix up Python RPM binding filenames so that other Pythons find it
4747
run: |
4848
for file in /usr/lib/python3/dist-packages/rpm/_rpm*.cpython-*.so; do

apypie/api.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
from urllib.parse import urljoin # type: ignore
1313
import requests
1414

15+
try:
16+
from requests_gssapi import HTTPKerberosAuth # type: ignore
17+
except ImportError:
18+
try:
19+
from requests_kerberos import HTTPKerberosAuth # type: ignore
20+
except ImportError:
21+
HTTPKerberosAuth = None
22+
1523
from apypie.resource import Resource
1624
from apypie.exceptions import DocLoadingError
1725

@@ -40,6 +48,7 @@ class Api(object):
4048
:param password: password to access the API
4149
:param client_cert: client cert to access the API
4250
:param client_key: client key to access the API
51+
:param kerberos: use Kerberos/GSSAPI for authentication with the API. Requires either `requests-gssapi` or `requests-kerberos`.
4352
:param api_version: version of the API. Defaults to `1`
4453
:param language: prefered locale for the API description
4554
:param apidoc_cache_base_dir: base directory for building apidoc_cache_dir. Defaults to `~/.cache/apipie_bindings`.
@@ -85,6 +94,12 @@ def __init__(self, **kwargs):
8594
if kwargs.get('client_cert') and kwargs.get('client_key'):
8695
self._session.cert = (kwargs['client_cert'], kwargs['client_key'])
8796

97+
if kwargs.get('kerberos'):
98+
if HTTPKerberosAuth is not None:
99+
self._session.auth = HTTPKerberosAuth()
100+
else:
101+
raise ValueError('Kerberos authentication requested, but neither requests-gssapi nor requests-kerberos found.')
102+
88103
self._apidoc = None
89104

90105
@property

apypie/foreman.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ def __init__(self, **kwargs):
6363
self.task_poll = 4
6464
kwargs['api_version'] = 2
6565
super().__init__(**kwargs)
66+
if kwargs.get('kerberos'):
67+
self.call('users', 'extlogin')
6668

6769
def _resource(self, resource: str) -> 'Resource':
6870
if resource not in self.resources:

requirements-test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ pytest-xdist
88
pytest-pylint
99
pytest-mock
1010
requests_mock
11+
requests-gssapi
1112
setuptools

setup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@
3939
install_requires=[
4040
'requests>=2.4.2',
4141
],
42+
extras_require={
43+
'kerberos': ['requests-gssapi'],
44+
},
4245
)

tests/test_foreman.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ def test_init(foremanapi):
3636
assert foremanapi.apidoc
3737

3838

39+
def test_kerberos(fixture_dir, requests_mock, tmpdir):
40+
with fixture_dir.join('foreman.json').open() as read_file:
41+
data = json.load(read_file)
42+
requests_mock.get('https://api.example.com/apidoc/v2.json', json=data)
43+
requests_mock.get('https://api.example.com/api/users/extlogin', status_code=204)
44+
ForemanApi(uri='https://api.example.com', apidoc_cache_dir=tmpdir.strpath, kerberos=True)
45+
assert requests_mock.last_request.url == 'https://api.example.com/api/users/extlogin'
46+
47+
3948
def test_resources(foremanapi):
4049
assert 'domains' in foremanapi.resources
4150

0 commit comments

Comments
 (0)