Skip to content

Commit

Permalink
Make requests_kerberos optional if endpoint is not secured
Browse files Browse the repository at this point in the history
If the url to fetch using uri_request is not secured (it does not return
401/403) make requests_kerberos optional.
  • Loading branch information
pablintino committed Jan 14, 2025
1 parent 3cd906d commit c264d70
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions plugins/modules/url_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@


try:
from requests import get
from requests import get, head

python_requests_installed = True
except ImportError:
Expand All @@ -102,6 +102,17 @@
python_requests_kerberos_installed = False


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")


def main():
module_args = {
"url": {"type": "str", "required": True},
Expand All @@ -120,13 +131,15 @@ def main():
if not python_requests_installed:
module.fail_json(msg="requests required for this module.")

if not python_requests_kerberos_installed:
module.fail_json(msg="requests_kerberos required for this module.")

url = module.params["url"]
verify_ssl = module.params["verify_ssl"]

auth = HTTPKerberosAuth(mutual_authentication=OPTIONAL)
_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)

Expand Down

0 comments on commit c264d70

Please sign in to comment.