Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!--
Same as knoxtoken.xml, but opted in to knox.token.allow.client.secret.as.access.token, so a CLIENTID
client secret presented directly as an access token is accepted (the behavior before KNOX-3601).
knoxtoken.xml keeps the default and rejects it.
-->
<topology>
<gateway>
<provider>
<role>federation</role>
<name>JWTProvider</name>
<enabled>true</enabled>
<param>
<name>knox.token.use.cookie</name>
<value>false</value>
</param>
<param>
<name>jwt.expected.sigalg</name>
<value>RS256</value>
</param>
<!-- Client secrets are only recognized as such when token state is server-managed. -->
<param>
<name>knox.token.exp.server-managed</name>
<value>true</value>
</param>
<param>
<name>knox.token.allow.client.secret.as.access.token</name>
<value>true</value>
</param>
</provider>
</gateway>
<service>
<role>KNOX-AUTH-SERVICE</role>
<param>
<name>preauth.auth.header.actor.id.name</name>
<value>x-knox-actor-username</value>
</param>
<param>
<name>preauth.auth.header.actor.groups.prefix</name>
<value>x-knox-actor-groups</value>
</param>
<param>
<name>preauth.group.filter.pattern</name>
<value>[^\s]+</value>
</param>
</service>
</topology>
168 changes: 150 additions & 18 deletions .github/workflows/tests/test_clientid_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,19 @@
ignored and a UUID is generated (proves the default is non-disruptive).

The gateway runs on H2 (persistent, primary-key enforced), so the duplicate ->
409 path is genuinely exercised. The end-to-end case then presents the minted
``client_secret`` as a Passcode to the JWTProvider-federated ``knoxtoken``
topology to prove a custom id authenticates like any other.
409 path is genuinely exercised. The end-to-end case then exchanges the minted
``client_secret`` for an access token through the ``client_credentials`` grant and
presents that to the JWTProvider-federated ``knoxtoken`` topology, to prove a custom
id authenticates like any other.

KNOX-3601: a client secret is a grant credential, not an access token. The
JWTProvider rejects it when presented directly (``Bearer``, or HTTP Basic with the
``Passcode`` username) unless the topology sets
``knox.token.allow.client.secret.as.access.token``:

* ``knoxtoken`` — default: a client secret used as an access token is rejected
(401), while an API key and a ``client_credentials`` exchange still work.
* ``knoxtoken-allow-client-secret`` — opted in: the client secret is accepted.
"""

import base64
Expand All @@ -41,14 +51,26 @@

from requests.auth import HTTPBasicAuth

from common_utils import gateway_base_url, knox_delete, knox_get, knox_post
from common_utils import (
gateway_base_url,
get_token_claim,
knox_delete,
knox_get,
knox_post,
)

UUID_RE = re.compile(
r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
)

# JWTFederationFilter.CLIENT_SECRET_AS_ACCESS_TOKEN_ERROR
CLIENT_SECRET_AS_ACCESS_TOKEN_ERROR = (
"A client secret is not an access token; "
"exchange it for one using the client_credentials grant"
)

class TestClientIdCredentials(unittest.TestCase):

class TestClientIdCredentials(unittest.TestCase): # pylint: disable=too-many-instance-attributes
"""Register client credentials with and without a caller-supplied clientId."""

def setUp(self):
Expand All @@ -64,6 +86,15 @@ def setUp(self):
)
# JWTProvider-protected auth service used to verify a minted passcode.
self.federated_pre_url = self.base_url + "gateway/knoxtoken/auth/api/v1/pre"
# Same, opted in to accepting a client secret as an access token.
self.allow_client_secret_pre_url = (
self.base_url + "gateway/knoxtoken-allow-client-secret/auth/api/v1/pre"
)
# OAuth token endpoint of the KNOXTOKEN service, behind the same JWTProvider.
self.oauth_token_url = self.base_url + "gateway/knoxtoken/oauth/v1/token"
# API keys are minted on behalf of a fresh user so the per-user limit there is never hit.
self.api_key_url = self.base_url + "gateway/tokenlimitbytype/apikey/api/v1/auth/key"
self.admin_auth = HTTPBasicAuth("admin", "admin-password")
# v2 revoke lives on the KNOXTOKEN service in the knoxldap topology.
self.token_v2_url = self.base_url + "gateway/knoxldap/knoxtoken/api/v2/token"

Expand Down Expand Up @@ -116,27 +147,128 @@ def test_supplied_client_id_is_used(self):
payload = self._register_ok(self.enabled_url, client_id=supplied)
self.assertEqual(payload["client_id"], supplied)

@staticmethod
def _passcode_basic(secret):
"""An Authorization header presenting a passcode as HTTP Basic, username Passcode."""
basic = base64.b64encode(("Passcode:" + secret).encode("utf-8")).decode("ascii")
return {"Authorization": "Basic " + basic}

@staticmethod
def _bearer(token):
return {"Authorization": "Bearer " + token}

def _exchange_client_secret(self, client_id, client_secret, basic_auth):
"""Exchange client credentials for an access token via the client_credentials grant."""
data = {"grant_type": "client_credentials"}
if basic_auth:
return knox_post(
self.oauth_token_url,
data=data,
auth=HTTPBasicAuth(client_id, client_secret),
)
data.update({"client_id": client_id, "client_secret": client_secret})
return knox_post(self.oauth_token_url, data=data)

def _assert_authenticated_as(self, response, actor, what):
self.assertEqual(
response.status_code,
200,
msg=f"{what} was not accepted: {response.status_code} {response.text}",
)
self.assertEqual(response.headers.get("x-knox-actor-username"), actor)

def test_supplied_client_id_authenticates(self):
"""A custom client_id's client_secret authenticates as a Passcode (E2E)."""
"""A custom client_id's exchanged access token authenticates (E2E)."""
supplied = "e2e-app_" + uuid.uuid4().hex
payload = self._register_ok(self.enabled_url, client_id=supplied)

basic = base64.b64encode(
("Passcode:" + payload["client_secret"]).encode("utf-8")
).decode("ascii")
response = knox_get(
self.federated_pre_url,
headers={"Authorization": "Basic " + basic},
exchange = self._exchange_client_secret(
supplied, payload["client_secret"], basic_auth=True
)
self.assertEqual(
response.status_code,
exchange.status_code,
200,
msg=f"client_credentials grant failed: "
f"{exchange.status_code} {exchange.text}",
)
access_token = exchange.json()["access_token"]
# For a CLIENTID (third-party-app) token the authenticated principal is the token_id
# itself, so the custom client_id becomes the subject of the exchanged access token...
self.assertEqual(get_token_claim(access_token, "sub"), supplied)

response = knox_get(self.federated_pre_url, headers=self._bearer(access_token))
# ...and flows through as the actor username.
self._assert_authenticated_as(response, supplied, "exchanged access token")

def test_client_secret_exchanged_through_client_credentials_grant(self):
"""KNOX-3601: the grant still accepts a client secret, in the body or as HTTP Basic."""
payload = self._register_ok(self.enabled_url)
for basic_auth in (False, True):
with self.subTest(basic_auth=basic_auth):
response = self._exchange_client_secret(
payload["client_id"], payload["client_secret"], basic_auth
)
self.assertEqual(
response.status_code,
200,
msg=f"client_credentials grant failed: "
f"{response.status_code} {response.text}",
)
self.assertIn("access_token", response.json())

def test_client_secret_rejected_as_access_token(self):
"""KNOX-3601: a client secret presented directly as an access token is rejected (401)."""
payload = self._register_ok(self.enabled_url)
secret = payload["client_secret"]
for presented_as, headers in (
("Bearer", self._bearer(secret)),
("Basic Passcode", self._passcode_basic(secret)),
):
with self.subTest(presented_as=presented_as):
response = knox_get(self.federated_pre_url, headers=headers)
self.assertEqual(
response.status_code,
401,
msg=f"client secret as {presented_as} access token was not rejected: "
f"{response.status_code} {response.text}",
)
self.assertIn(CLIENT_SECRET_AS_ACCESS_TOKEN_ERROR, response.text)
self.assertIsNone(response.headers.get("x-knox-actor-username"))

def test_client_secret_accepted_as_access_token_when_allowed(self):
"""KNOX-3601: a topology that opts in still accepts a client secret as an access token."""
supplied = "optin-app_" + uuid.uuid4().hex
payload = self._register_ok(self.enabled_url, client_id=supplied)
secret = payload["client_secret"]
for presented_as, headers in (
("Bearer", self._bearer(secret)),
("Basic Passcode", self._passcode_basic(secret)),
):
with self.subTest(presented_as=presented_as):
response = knox_get(self.allow_client_secret_pre_url, headers=headers)
self._assert_authenticated_as(
response, supplied, f"client secret as {presented_as} access token"
)

def test_api_key_accepted_as_access_token(self):
"""KNOX-3601: API keys share the passcode format but are not client secrets."""
user = "apikey-" + uuid.uuid4().hex[:12]
minted = knox_post(self.api_key_url, auth=self.admin_auth, params={"doAs": user})
self.assertEqual(
minted.status_code,
200,
msg=f"custom-id passcode was not accepted: "
f"{response.status_code} {response.text}",
msg=f"API key was not issued: {minted.status_code} {minted.text}",
)
# For a CLIENTID (third-party-app) token the authenticated principal's audit username is
# the token_id itself, so the custom client_id flows through as the actor username.
self.assertEqual(response.headers.get("x-knox-actor-username"), supplied)
api_key = minted.json()["api_key"]
for presented_as, headers in (
("Bearer", self._bearer(api_key)),
("Basic Passcode", self._passcode_basic(api_key)),
):
with self.subTest(presented_as=presented_as):
response = knox_get(self.federated_pre_url, headers=headers)
self._assert_authenticated_as(
response, user, f"API key as {presented_as} access token"
)

def test_omitted_client_id_returns_generated_id(self):
"""With no clientId param, an enabled topology still generates a UUID."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ private HadoopAuthFilter testIfJwtSupported(String supportJwt) throws Exception
expect(filterConfig.getInitParameter(JWTFederationFilter.KNOX_TOKEN_COOKIE_NAME)).andReturn(null).anyTimes();
expect(filterConfig.getInitParameter(RequestAudienceValidatorService.REQUEST_AUDIENCE_VALIDATOR_PARAM)).andReturn(null).anyTimes();
expect(filterConfig.getInitParameter(JWTFederationFilter.ALLOWED_GRANT_TYPES)).andReturn(null).anyTimes();
expect(filterConfig.getInitParameter(JWTFederationFilter.ALLOW_CLIENT_SECRET_AS_ACCESS_TOKEN)).andReturn(null).anyTimes();
}

final ServletContext servletContext = createMock(ServletContext.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public interface JWTMessages {
+ "cookie, enabling {1} permits falling back to the token''s own exp claim." )
void unknownTokenStateRejected(String tokenId, String paramName);

@Message( level = MessageLevel.WARN, text = "Rejected client secret of {0} used as an access token" )
void clientSecretRejectedAsAccessToken(String tokenId);

@Message( level = MessageLevel.WARN, text = "Unable to verify passcode token ({0}) due to missing or incorrect token state service configuration.")
void unableToVerifyPasscodeToken(String tokenId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,16 @@ private TokenMetadata getTokenMetadata(final String tokenId) throws UnknownToken
return getTokenMetadata(tokenId, false);
}

/**
* @return true when the server-managed metadata of the given token marks it as OAuth client
* credentials issued by the CLIENTID service; false when it is any other token (e.g. an API key)
* or there is no server-managed state for it
*/
protected boolean isClientIdToken(final String tokenId) throws UnknownTokenException {
final TokenMetadata metadata = getTokenMetadata(tokenId);
return metadata != null && metadata.isClientId();
}

/**
* @param stateAbsentTolerated true only when the expiration lookup for this same token already
* established that <em>no</em> server-managed record exists and the opt-in excused it. The
Expand Down
Loading
Loading