Merge pull request #408 from cihlamar/AMW-625

Add use_netrc option to prevent .netrc overriding bearer token
This commit is contained in:
Ranabir Chakraborty
2026-09-09 12:33:09 +05:30
committed by GitHub
5 changed files with 127 additions and 0 deletions
+49
View File
@@ -0,0 +1,49 @@
---
driver:
name: docker
platforms:
- name: instance
image: registry.access.redhat.com/ubi9/ubi-init:latest
pre_build_image: true
privileged: true
command: "/usr/sbin/init"
exposed_ports:
- "8080/tcp"
- "8443/tcp"
- "8009/tcp"
- "9000/tcp"
provisioner:
name: ansible
config_options:
defaults:
interpreter_python: auto_silent
roles_path: ../../roles
ssh_connection:
pipelining: false
playbooks:
prepare: ../default/prepare.yml
converge: ../default/converge.yml
verify: verify.yml
inventory:
group_vars:
all:
keycloak_install_requires_become: true
host_vars:
localhost:
ansible_python_interpreter: "{{ ansible_playbook_python }}"
env:
ANSIBLE_FORCE_COLOR: "true"
PROXY: "${PROXY}"
NO_PROXY: "${NO_PROXY}"
verifier:
name: ansible
scenario:
test_sequence:
- cleanup
- destroy
- create
- prepare
- converge
- verify
- cleanup
- destroy
+1
View File
@@ -0,0 +1 @@
../../roles
+64
View File
@@ -0,0 +1,64 @@
---
- name: Verify use_netrc option prevents .netrc from overriding bearer token (issue 403)
hosts: all
vars:
auth_keycloak_url: http://instance:8080
auth_realm: master
auth_username: remembertochangeme
auth_password: remembertochangeme
target_realm: TestRealm
module_defaults:
group/middleware_automation.keycloak.keycloak:
auth_keycloak_url: "{{ auth_keycloak_url }}"
auth_realm: "{{ auth_realm }}"
auth_username: "{{ auth_username }}"
auth_password: "{{ auth_password }}"
validate_certs: false
tasks:
- name: Populate service facts
ansible.builtin.service_facts:
- name: Check if keycloak service started
ansible.builtin.assert:
that:
- ansible_facts.services["keycloak.service"]["state"] == "running"
- ansible_facts.services["keycloak.service"]["status"] == "enabled"
fail_msg: Keycloak service is not running
- name: Verify netrc handling
block:
- name: Create .netrc with bogus credentials matching the Keycloak host
ansible.builtin.copy:
dest: ~/.netrc
mode: "0600"
content: |
machine instance
login netrc_user
password netrc_password
- name: API call with default use_netrc (false) ignores .netrc
middleware_automation.keycloak.keycloak_realm_keys_metadata_info:
realm: "{{ target_realm }}"
register: netrc_default_result
- name: Assert API call succeeds with default use_netrc despite .netrc
ansible.builtin.assert:
that:
- netrc_default_result is not failed
- name: API call with use_netrc true picks up .netrc credentials
middleware_automation.keycloak.keycloak_realm_keys_metadata_info:
realm: "{{ target_realm }}"
use_netrc: true
register: netrc_true_result
ignore_errors: true
- name: Assert API call fails when use_netrc is true and .netrc has credentials
ansible.builtin.assert:
that:
- netrc_true_result is failed
always:
- name: Remove .netrc test file
ansible.builtin.file:
path: ~/.netrc
state: absent
+8
View File
@@ -78,4 +78,12 @@ options:
- Configures the HTTP User-Agent header.
type: str
default: Ansible
use_netrc:
description:
- Whether to use credentials from C(~/.netrc) file.
- By default, C(~/.netrc) is not used because these modules authenticate via
explicit OIDC credentials/token. A matching C(~/.netrc) entry would silently
override the Bearer token with Basic authentication, causing 401 errors.
type: bool
default: false
'''
@@ -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: