Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[url_request] Use gssapi lib by default #2654

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions plugins/modules/url_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,27 @@
except ImportError:
python_requests_kerberos_installed = False

try:
from requests_gssapi import HTTPSPNEGOAuth, OPTIONAL

python_requests_gssapi_installed = True
except ImportError:
python_requests_gssapi_installed = False


def _get_auth_module(module, url, verify_ssl):
# If the auth modules are loaded there's no need to check if the url is secured beforehand
if python_requests_gssapi_installed:
return HTTPSPNEGOAuth(mutual_authentication=OPTIONAL)
elif python_requests_kerberos_installed:
return HTTPKerberosAuth(mutual_authentication=OPTIONAL)

def _validate_auth_module(module, url, verify_ssl):
if python_requests_kerberos_installed:
# The module are loaded if requires, no need to validate if it's necessary or not
return
response = head(url=url, verify=verify_ssl, allow_redirects=True, timeout=30)
# If the response in a 401 or 403 we need to authenticate
if response.status_code in [401, 403]:
# Kerberos module not present, fail
module.fail_json(
msg="requests_kerberos required for this module to authenticate against the given url"
msg="Neither requests_gssapi or requests_kerberos are installed and the url requires authentication"
)


Expand All @@ -136,14 +146,13 @@ def main():
url = module.params["url"]
verify_ssl = module.params["verify_ssl"]

_validate_auth_module(module, url, verify_ssl)
if python_requests_kerberos_installed:
auth = HTTPKerberosAuth(mutual_authentication=OPTIONAL)
else:
auth = None

try:
response = get(url=url, auth=auth, verify=verify_ssl, allow_redirects=True)
response = get(
url=url,
auth=_get_auth_module(module, url, verify_ssl),
verify=verify_ssl,
allow_redirects=True,
)

result["status_code"] = response.status_code
result["headers"] = dict(response.headers)
Expand Down
Loading