JSONDecodeError when OIDCRedirectURI is the same as the Keystone OIDC auth endpoint

Bug #2075349 reported by Jadon Naas
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
OpenStack Identity (keystone)
Fix Released
Undecided
Jadon Naas
OpenStack Keystone OIDC Integration Charm
In Progress
Undecided
Jadon Naas
puppet-keystone
Fix Released
Medium
Takashi Kajinami

Bug Description

This bug is about test failures for jammy-caracal, jammy-bobcat, and jammy-antelope in cherry-pick commits from this change:

https://review.opendev.org/c/openstack/charm-keystone-openidc/+/922049

That change fixed some bugs in the Keystone OpenIDC charm and added some additional configuration options to help with proxies.

The tests all fail with a JSONDecodeError during the Zaza tests for the Keystone OpenIDC charm. Here is an example of the error:

Expecting value: line 1 column 1 (char 0)
Traceback (most recent call last):
  File "/home/jadon/py3-venv/lib/python3.10/site-packages/requests/models.py", line 974, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3.10/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.10/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.10/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/jadon/py3-venv/lib/python3.10/site-packages/cliff/app.py", line 414, in run_subcommand
    self.prepare_to_run_command(cmd)
  File "/home/jadon/py3-venv/lib/python3.10/site-packages/osc_lib/shell.py", line 516, in prepare_to_run_command
    self.client_manager.auth_ref
  File "/home/jadon/py3-venv/lib/python3.10/site-packages/osc_lib/clientmanager.py", line 208, in auth_ref
    self._auth_ref = self.auth.get_auth_ref(self.session)
  File "/home/jadon/py3-venv/lib/python3.10/site-packages/keystoneauth1/identity/v3/federation.py", line 62, in get_auth_ref
    auth_ref = self.get_unscoped_auth_ref(session)
  File "/home/jadon/py3-venv/lib/python3.10/site-packages/keystoneauth1/identity/v3/oidc.py", line 293, in get_unscoped_auth_ref
    return access.create(resp=response)
  File "/home/jadon/py3-venv/lib/python3.10/site-packages/keystoneauth1/access/access.py", line 36, in create
    body = resp.json()
  File "/home/jadon/py3-venv/lib/python3.10/site-packages/requests/models.py", line 978, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
clean_up ListServer: Expecting value: line 1 column 1 (char 0)
END return value: 1

According to debug output, the failure happens during the OIDC authentication flow. Testing using the OpenStack CLI shows the failure happen right after this request:

REQ: curl -g -i --insecure -X POST https://10.70.143.111:5000/v3/OS-FEDERATION/identity_providers/keycloak/protocols/openid/auth -H "Authorization: {SHA256}45dbb29ea555e0bd24995cbb1481c8ac66c2d03383bc0c335be977d0daaf6959" -H "User-Agent: openstacksdk/3.3.0 keystoneauth1/5.7.0 python-requests/2.32.3 CPython/3.10.12"
Starting new HTTPS connection (1): 10.70.143.111:5000
RESP: [200] Connection: Keep-Alive Content-Length: 0 Date: Tue, 30 Jul 2024 19:28:17 GMT Keep-Alive: timeout=75, max=1000 Server: Apache/2.4.52 (Ubuntu)
RESP BODY: Omitted, Content-Type is set to None. Only text/plain, application/json responses have their bodies logged.

This request is unusual in that the request is a POST request with no request body, and the response is an empty response. The empty response causes the JSONDecodeError because the keystoneauth package expects a JSON document to return from the request for a Keystone token. The empty response causes the JSONDecodeError because an empty string is not a valid document.

This strange behavior happens due to a misconfiguration in the mod_auth_openidc Apache configuration. I looked up how Kolla-Ansible configures OpenIDC in Keystone, and I noticed that they used a different value for the OIDCRedirectURI in the mod_auth_openidc Apache configuration than the Keystone OpenIDC charm. The value of OIDCRedirectURI is supposed to be a fake URI that does not map to any real URI in the protected service. The fake URI should be protected by mod_auth_openidc in Apache's configuration. When someone accesses the path in OIDCRedirectURI mod_auth_openidc handles the request and returns an empty response.

In our configuration, the OIDCRedirectURI is the same URI as the Keystone federation protocol authentication endpoint. Keystone generates the federation protocol authentication endpoint dynamically based on the identity provider and federation protocol configured in Keystone. Here is an example of a federation protocol endpoint for a Keystone identity provider named "keycloak" using a Keystone federation protocol called "openid":

/v3/OS-FEDERATION/identity_providers/keycloak/protocols/openid/auth

When Keystone is attempting to access the Keystone federation protocol authentication endpoint, mod_auth_openidc is intercepting the request and returning an empty response. This is blocking Keystone from properly handling the OIDC authentication flow.

If you change OIDCRedirectURI to something other than your federation protocol authentication endpoint, Keystone can complete the OIDC authentication flow successfully.

This contradicts the configuration outlined in the official Keystone federation configuration documentation at https://docs.openstack.org/keystone/latest/admin/federation/configure_federation.html#configure-mod-auth-openidc.

Revision history for this message
Jadon Naas (jadonn) wrote :

Here is a corrected Apache configuration for mod_auth_openidc that should illustrate the problem and the solution.

Revision history for this message
Jadon Naas (jadonn) wrote :

Here is an example openrc file for authenticating using OIDC to access OpenStack. The credentials in the file were for a test environment that has been destroyed.

description: updated
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to charm-keystone-openidc (master)
Changed in charm-keystone-openidc:
status: Triaged → In Progress
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to keystone (master)

Fix proposed to branch: master
Review: https://review.opendev.org/c/openstack/keystone/+/925553

Changed in keystone:
status: New → In Progress
Revision history for this message
Jadon Naas (jadonn) wrote (last edit ):

I added a task to this bug for the Keystone project since following the documentation in the configuration guide was what got me tripped up. I also submitted a small change to Keystone to update the documentation for the OIDC configuration. I would love to get feedback from Keystone project folks on this configuration if that's possible. I am happy to provide any information they might need about my config and how I tested/replicated this problem.

Changed in keystone:
assignee: nobody → Jadon Naas (jadonn)
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to charm-keystone-openidc (stable/2024.1)
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to charm-keystone-openidc (stable/2024.1)

Reviewed: https://review.opendev.org/c/openstack/charm-keystone-openidc/+/925918
Committed: https://opendev.org/openstack/charm-keystone-openidc/commit/69c86c320b4e62845cca3cbaded4768493cd5dd8
Submitter: "Zuul (22348)"
Branch: stable/2024.1

commit 69c86c320b4e62845cca3cbaded4768493cd5dd8
Author: Jadon Naas <email address hidden>
Date: Wed Jul 31 12:03:17 2024 -0400

    Fix JSONDecodeError from misconfigured OIDCRedirectURI

    This change makes the OIDCRedirectURI into a fake URI. If
    OIDCRedirectURI matches a real Keystone federation auth
    endpoint, the Keystone federation auth journey breaks.

    Closes-Bug: 2075349
    Change-Id: I39ad349db5175ca8c0e1669f784c70042e18a3f6

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Change abandoned on charm-keystone-openidc (master)

Change abandoned by "Jadon Naas <email address hidden>" on branch: master
Review: https://review.opendev.org/c/openstack/charm-keystone-openidc/+/925408
Reason: This change was successfully merged to stable/2024.1 instead of master due to issues with the Noble Caracal bundle. Felipe recommended abandoning this change and doing a fresh cherry-pick from 2024.1 to master in the merged branch. I'm abandoning this change so I can do that.

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to charm-keystone-openidc (master)
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to keystone (master)

Reviewed: https://review.opendev.org/c/openstack/keystone/+/925553
Committed: https://opendev.org/openstack/keystone/commit/7ac0c3cd33214ff3c926e2b5316b637892d701fb
Submitter: "Zuul (22348)"
Branch: master

commit 7ac0c3cd33214ff3c926e2b5316b637892d701fb
Author: Jadon Naas <email address hidden>
Date: Thu Aug 1 21:10:43 2024 -0400

    Update OIDC Apache config to avoid masking Keystone API endpoint

    The current configuration for the OIDCRedirectURI results in
    mod_auth_openidc masking the Keystone federation authentication
    endpoint, which results in incorrect responses to requests for
    Keystone tokens. This change updates the documentation to
    recommend using a vanity URL that does not match a Keystone
    API endpoint.

    Closes-Bug: 2075349
    Change-Id: I1dfba5c71da68522fdb6059f0dc03cddc74cb07d

Changed in keystone:
status: In Progress → Fix Released
Changed in puppet-keystone:
assignee: nobody → Takashi Kajinami (kajinamit)
importance: Undecided → Medium
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to puppet-keystone (master)
Changed in puppet-keystone:
status: New → In Progress
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Related fix proposed to keystone (master)

Related fix proposed to branch: master
Review: https://review.opendev.org/c/openstack/keystone/+/928756

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to puppet-keystone (master)

Reviewed: https://review.opendev.org/c/openstack/puppet-keystone/+/928755
Committed: https://opendev.org/openstack/puppet-keystone/commit/fdf2a2b31a6de76973a35a2494455ef176eee936
Submitter: "Zuul (22348)"
Branch: master

commit fdf2a2b31a6de76973a35a2494455ef176eee936
Author: Takashi Kajinami <email address hidden>
Date: Tue Sep 10 13:39:46 2024 +0900

    Fix default OIDCRedirectURI hiding keystone federation auth endpoint

    This updates the default OIDCRedirectURI according to the change made
    in the example file in keystone repo[1].

    [1] https://review.opendev.org/925553

    Closes-Bug: #2075349
    Change-Id: Ia0f3cbb842a4c01e6a3ca44ca66dc9a8a731720c

Changed in puppet-keystone:
status: In Progress → Fix Released
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to puppet-keystone (stable/2024.1)

Fix proposed to branch: stable/2024.1
Review: https://review.opendev.org/c/openstack/puppet-keystone/+/929005

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix included in openstack/keystone 26.0.0.0rc1

This issue was fixed in the openstack/keystone 26.0.0.0rc1 release candidate.

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to puppet-keystone (stable/2024.1)

Reviewed: https://review.opendev.org/c/openstack/puppet-keystone/+/929005
Committed: https://opendev.org/openstack/puppet-keystone/commit/723cc8694dd87d1ed4d06db35346cdc49d9cfacc
Submitter: "Zuul (22348)"
Branch: stable/2024.1

commit 723cc8694dd87d1ed4d06db35346cdc49d9cfacc
Author: Takashi Kajinami <email address hidden>
Date: Tue Sep 10 13:39:46 2024 +0900

    Fix default OIDCRedirectURI hiding keystone federation auth endpoint

    This updates the default OIDCRedirectURI according to the change made
    in the example file in keystone repo[1].

    [1] https://review.opendev.org/925553

    Conflicts:
            spec/classes/keystone_federation_openidc_spec.rb

    Closes-Bug: #2075349
    Change-Id: Ia0f3cbb842a4c01e6a3ca44ca66dc9a8a731720c
    (cherry picked from commit fdf2a2b31a6de76973a35a2494455ef176eee936)

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Related fix merged to keystone (master)

Reviewed: https://review.opendev.org/c/openstack/keystone/+/928756
Committed: https://opendev.org/openstack/keystone/commit/c450599cbd45210844e1356987c4f27e84e7161d
Submitter: "Zuul (22348)"
Branch: master

commit c450599cbd45210844e1356987c4f27e84e7161d
Author: Takashi Kajinami <email address hidden>
Date: Tue Sep 10 13:45:51 2024 +0900

    Update mod_openidc config for devstack

    Use a vanity URL for redirect uri so that it does not conflict with
    existing keystone endpoint. The documentation was updated recently[1]
    but the actual configuration used in devstack setup was still kept old
    at that time.

    [1] 7ac0c3cd33214ff3c926e2b5316b637892d701fb

    Related-Bug: #2075349
    Change-Id: I8d06f3c388260f356c7a1da0212bb3b399f3a848

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Related fix proposed to keystone (stable/2024.2)

Related fix proposed to branch: stable/2024.2
Review: https://review.opendev.org/c/openstack/keystone/+/930155

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix included in openstack/puppet-keystone 25.0.0

This issue was fixed in the openstack/puppet-keystone 25.0.0 release.

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Related fix merged to keystone (stable/2024.2)

Reviewed: https://review.opendev.org/c/openstack/keystone/+/930155
Committed: https://opendev.org/openstack/keystone/commit/47891f4ae8fd7876e5a7657f58c32c371feeddc3
Submitter: "Zuul (22348)"
Branch: stable/2024.2

commit 47891f4ae8fd7876e5a7657f58c32c371feeddc3
Author: Takashi Kajinami <email address hidden>
Date: Tue Sep 10 13:45:51 2024 +0900

    Update mod_openidc config for devstack

    Use a vanity URL for redirect uri so that it does not conflict with
    existing keystone endpoint. The documentation was updated recently[1]
    but the actual configuration used in devstack setup was still kept old
    at that time.

    [1] 7ac0c3cd33214ff3c926e2b5316b637892d701fb

    Related-Bug: #2075349
    Change-Id: I8d06f3c388260f356c7a1da0212bb3b399f3a848
    (cherry picked from commit c450599cbd45210844e1356987c4f27e84e7161d)

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.