smolnar82 opened a new pull request, #1532:
URL: https://github.com/apache/knox/pull/1532

   [KNOX-3601](https://issues.apache.org/jira/browse/KNOX-3601) - Reject 
CLIENTID client secrets presented as access tokens
   
   ## What changes were proposed in this pull request?
   
   A client secret issued by the CLIENTID service is a long-lived grant 
credential: it should be exchanged for a short-lived access token through the 
OAuth 2.0 `client_credentials` grant. Until now `JWTFederationFilter` also 
accepted it directly as an access token, as `Authorization: Bearer 
<client_secret>` or as HTTP Basic with the `Passcode` username, because client 
secrets and API keys share the same passcode encoding.
   
   With this change:
   - `JWTFederationFilter` rejects a CLIENTID client secret presented directly 
as an access token with `401 Unauthorized` and the message "A client secret is 
not an access token; exchange it for one using the client_credentials grant".
   - The check runs right after the passcode is decoded and before 
`validateToken`, so a secret that is going to be rejected is never verified.
   - Client secrets are told apart from API keys by 
`TokenMetadata.isClientId()`; API keys keep working as Bearer / Basic 
`Passcode` tokens.
   - Client secrets are still accepted in the `client_credentials` grant, both 
in the form body (`client_id` / `client_secret`) and as HTTP Basic 
`client_id:client_secret`. `WireToken` now records whether the token came from 
a `client_credentials` grant (`WireToken.clientSecret(...)`), and the grant 
parsing methods return `WireToken` directly.
   - A new `JWTProvider` param, 
`knox.token.allow.client.secret.as.access.token` (default `false`), restores 
the old behavior on a topology that still needs it.
   - The rejection is logged at WARN with the token id display text only 
(`Rejected client secret of {0} used as an access token`); the param name is 
deliberately not in the log, it is documented instead.
   - The check needs the token state service, so it only applies when 
`knox.token.exp.server-managed` is on; without it a client secret cannot be 
identified as such and nothing changes.
   - Docs: new "Using the client secret" section in 
`config_client_credentials.md` with the grant examples, the 401 behavior and 
the new param.
   
   Known limitation (not addressed here): `getParameter()` merges the query 
string with the form body, so HTTP Basic `client_id:client_secret` with 
`?grant_type=client_credentials` in the URL counts as a `client_credentials` 
grant on any path. That form has worked since KNOX-3260 / KNOX-3312 and shipped 
in 3.0.0, so closing it would be a separate, compatibility-relevant decision. 
The impact is low: whoever holds the secret can always exchange it for an 
access token.
   
   ## How was this patch tested?
   
   - Unit tests:
     - `JWTFederationFilterTest`: a client secret is rejected as Bearer and as 
Basic `Passcode` (and is never validated), is still rejected as Bearer when 
`grant_type=client_credentials` is present, and is accepted in both forms when 
`knox.token.allow.client.secret.as.access.token=true`. The existing passcode 
tests cover API keys and non-server-managed topologies.
     - `OAuthFlowsFederationFilterTest`: a client secret is accepted in the 
`client_credentials` grant, in the form body and as HTTP Basic.
     - `HadoopAuthFilterTest`: the strict filter config mock expects the new 
param.
   - `mvn verify` on `gateway-provider-security-jwt` and 
`gateway-provider-security-hadoopauth`.
   - Manual tests against a local gateway with CLIENTID and APIKEY credentials:
     - `client_credentials` grant at `oauth/v1/token`, body and HTTP Basic: 200
     - client secret as Bearer and as Basic `Passcode`: 401, with the WARN log 
line
     - API key as Bearer: 200
     - client secret as Bearer on a topology with 
`knox.token.allow.client.secret.as.access.token=true`: 200
   
   ## Integration Tests
   
   Extended `test_clientid_credentials.py` and added the 
`knoxtoken-allow-client-secret` topology (`knoxtoken` plus 
`knox.token.allow.client.secret.as.access.token=true`):
   - `test_supplied_client_id_authenticates` used to present the client secret 
as Basic `Passcode` to `knoxtoken`, which is now rejected. It now exchanges the 
secret through the `client_credentials` grant, checks that the access token's 
`sub` is the custom client id, and authenticates with that access token.
   - `test_client_secret_exchanged_through_client_credentials_grant`: the grant 
accepts the secret in the body and as HTTP Basic.
   - `test_client_secret_rejected_as_access_token`: Bearer and Basic `Passcode` 
get 401 with the error message on `knoxtoken`.
   - `test_client_secret_accepted_as_access_token_when_allowed`: both forms are 
accepted on `knoxtoken-allow-client-secret`.
   - `test_api_key_accepted_as_access_token`: an API key (minted at 
`tokenlimitbytype` for a fresh `doAs` user) is still accepted in both forms.
   
   ```
   tests-1  | Your code has been rated at 10.00/10
   tests-1  | 
   tests-1  | Waiting for knox...
   tests-1  | ============================= test session starts 
==============================
   tests-1  | platform linux -- Python 3.10.20, pytest-9.0.3, pluggy-1.7.0
   tests-1  | rootdir: /tests
   tests-1  | plugins: platformdirs-4.12.4
   tests-1  | collected 166 items
   tests-1  | 
   tests-1  | test_clientid_credentials.py ...........                          
       [  6%]
   tests-1  | test_delegation.py ............                                   
       [ 13%]
   tests-1  | test_gateway_identity_cert.py ......                              
       [ 17%]
   tests-1  | test_health.py .....                                              
       [ 20%]
   tests-1  | test_k8s_delegation.py ........                                   
       [ 25%]
   tests-1  | test_k8s_jwks_ca.py ...........                                   
       [ 31%]
   tests-1  | test_k8s_serviceaccount_validation.py ......                      
       [ 35%]
   tests-1  | test_knox_admin_path_traversal.py ...                             
       [ 37%]
   tests-1  | test_knox_auth_service_and_ldap.py ...                            
       [ 39%]
   tests-1  | test_knox_configs.py .                                            
       [ 39%]
   tests-1  | test_knox_ldap_backend_password_alias_retry.py ..                 
       [ 40%]
   tests-1  | test_knox_ldap_cache.py ...                                       
       [ 42%]
   tests-1  | test_knox_ldap_injection.py .......                               
       [ 46%]
   tests-1  | test_knox_ldap_proxy_search.py ...............                    
       [ 56%]
   tests-1  | test_knoxauth_preauth_and_paths.py ......                         
       [ 59%]
   tests-1  | test_knoxauth_token_forwarding.py ...........                     
       [ 66%]
   tests-1  | test_knoxidf.py .......                                           
       [ 70%]
   tests-1  | test_knoxsso_redirect.py .                                        
       [ 71%]
   tests-1  | test_knoxtoken_jwt.py ....................                        
       [ 83%]
   tests-1  | test_remote_auth.py ...                                           
       [ 84%]
   tests-1  | test_remoteauth_extauthz_additional_path.py ....                  
       [ 87%]
   tests-1  | test_token_exchange.py .................                          
       [ 97%]
   tests-1  | test_token_limit_by_type.py ....                                  
       [100%]
   tests-1  | 
   tests-1  | =============================== warnings summary 
===============================
   ...
   tests-1  | -- Docs: 
https://docs.pytest.org/en/stable/how-to/capture-warnings.html
   tests-1  | ----------------- generated xml file: /tests/test-results.xml 
------------------
   tests-1  | ================= 166 passed, 144 warnings in 60.42s (0:01:00) 
=================
   tests-1 exited with code 0
   Aborting on container exit...
   Container compose-tests-1 Stopping 
   Container compose-tests-1 Stopped Config   w Enable Watch   d Detach
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to