AMW-625 add use_netrc option to prevent .netrc overriding bearer token auth

When ~/.netrc contains an entry matching the Keycloak host, open_url()
silently replaces the OIDC Bearer header with Basic auth, causing 401
errors. Add use_netrc option (default false) to all keycloak modules
and pass it through to both open_url() call sites.

Includes molecule test scenario (netrc) that verifies both behaviors:
- default use_netrc=false ignores .netrc (fix works)
- use_netrc=true picks up .netrc and gets 401 (proves the bug)

Fixes: https://github.com/ansible-middleware/keycloak/issues/403
This commit is contained in:
Martin Cihlar
2026-09-07 09:09:39 +02:00
parent 8d5526aeb6
commit d6ceb63914
5 changed files with 127 additions and 0 deletions
@@ -177,6 +177,7 @@ def keycloak_argument_spec() -> dict[str, t.Any]:
token=dict(type="str", no_log=True),
refresh_token=dict(type="str", no_log=True),
http_agent=dict(type="str", default="Ansible"),
use_netrc=dict(type="bool", default=False),
)
@@ -216,6 +217,7 @@ def _token_request(module_params: dict[str, t.Any], payload: dict[str, t.Any]) -
http_agent = module_params.get("http_agent")
validate_certs = module_params.get("validate_certs")
connection_timeout = module_params.get("connection_timeout")
use_netrc = module_params.get("use_netrc", False)
try:
r = json.loads(
@@ -226,6 +228,7 @@ def _token_request(module_params: dict[str, t.Any], payload: dict[str, t.Any]) -
http_agent=http_agent,
timeout=connection_timeout,
data=urlencode(payload),
use_netrc=use_netrc,
).read()
)
@@ -401,6 +404,7 @@ class KeycloakAPI:
self.connection_timeout = self.module.params.get("connection_timeout")
self.restheaders = connection_header
self.http_agent = self.module.params.get("http_agent")
self.use_netrc = self.module.params.get("use_netrc", False)
def _request(
self, url: str, method: str, data: str | bytes | None = None, *, extra_headers: dict[str, str] | None = None
@@ -429,6 +433,7 @@ class KeycloakAPI:
headers=headers,
timeout=self.connection_timeout,
validate_certs=self.validate_certs,
use_netrc=self.use_netrc,
)
except HTTPError as e:
if e.code != HTTPStatus.UNAUTHORIZED: