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

   [KNOX-3578](https://issues.apache.org/jira/browse/KNOX-3578) - Add 
jwt.allowed.grant.types to restrict token endpoint grant types
   
   ## What changes were proposed in this pull request?
   
   A new `JWTProvider` topology param, `jwt.allowed.grant.types`, takes a 
comma-separated list of the `grant_type` values the topology's token endpoint 
accepts. When it is unset or blank, nothing changes.
   
   Today a topology such as an exchange-only KnoxIDF token endpoint accepts 
every grant. `JWTFederationFilter` also does each grant's credential work 
(passcode, `client_assertion`, refresh token or `subject_token` validation, 
possibly a JWKS fetch) before KnoxIDF ever sees the request.
   
   With the param set:
   
   - **JWTProvider**
     - A request with a present, non-allowed `grant_type` is rejected with `400 
{"error":"unsupported_grant_type"}` **before any credential is validated**. 
This applies even when an `Authorization` header is present.
     - The rejection is audited as `event=token_grant grant_type=<grant> 
reason=grant_type_not_allowed`. Unknown grant values are logged as `other`, so 
caller input never reaches the audit log.
     - The parsed list is passed to the service in the `X-Allowed-Grant-Types` 
request attribute. The filter sets it before the unauthenticated-path bypass, 
so anonymous discovery sees it too.
     - A missing grant_type is not rejected by the filter, since it can't tell 
a token request from other traffic on the topology; KnoxIDF's token endpoint 
rejects it instead.
   - **KnoxIDF**
     - The token endpoint (GET and POST) rejects a missing or non-allowed 
`grant_type`. This closes the bearer-token re-mint on restricted topologies.
     - The discovery documents (`openid-configuration` and 
`oauth-authorization-server`) advertise only the listed grants in 
`grant_types_supported`. Without `authorization_code`, 
`authorization_endpoint`, `registration_endpoint` and `userinfo_endpoint` are 
omitted.
     - A KnoxIDF behind another provider sees no attribute and keeps today's 
behaviour.
   - **Shared code:** the shared parser `AllowedGrantTypes` lives in 
`gateway-util-common`.
   - **Docs:** the new page `knox-site/docs/config_jwt_allowed_grant_types.md`, 
listed under Providers in `mkdocs.yml`. It includes a caveat: with the param 
set, the provider reads the form body of every authenticated request, so it is 
meant for token-endpoint topologies, not ones that proxy form posts.
   
   ## How was this patch tested?
   
   - New unit tests:
     - `AllowedGrantTypesTest`.
     - `JWTFederationFilterAllowedGrantTypesTest`. It covers:
       - rejection before credential parsing for `refresh_token`, 
`authorization_code` and `client_credentials` (malformed secret, HTTP Basic, 
`client_assertion`);
       - a bearer token with a disallowed grant;
       - pass-through of a missing grant;
       - the attribute on unauthenticated paths, without reading the body;
       - no behaviour change when the param is unset;
       - the audit record.
     - `TokenResourceAllowedGrantTypesTest`.
   - Extended unit tests: `OAuthServerMetadataTest` and 
`DiscoveryResourceMetadataTest`.
   - `mvn -pl 
gateway-util-common,gateway-provider-security-jwt,gateway-service-knoxidf -am 
verify` is green: 194, 740 and 274 tests with 0 failures, and checkstyle, PMD, 
spotbugs and forbiddenapis pass.
   
   ## Integration Tests
   
   - A new topology, `knoxidf-token-exchange-only.xml`. It is 
`knoxidf-token.xml` restricted to 
`urn:ietf:params:oauth:grant-type:token-exchange`, with anonymous discovery and 
JWKS. It is added to the test image's `Dockerfile`.
   - Five new tests in `test_token_exchange.py`:
     - same-subject exchange still succeeds;
     - `refresh_token`, `client_credentials` and `authorization_code` are 
rejected with `unsupported_grant_type`;
     - the rejection is audited;
     - a bearer request without `grant_type` is rejected;
     - discovery advertises only token exchange.
   - Results: the full Docker Compose suite passed 162 tests, 
`test_token_exchange.py` passed 17/17, and pylint scored 10/10.
   
   ```
   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 162 items
   tests-1  | 
   tests-1  | test_clientid_credentials.py .......                              
       [  4%]
   tests-1  | test_delegation.py ............                                   
       [ 11%]
   tests-1  | test_gateway_identity_cert.py ......                              
       [ 15%]
   tests-1  | test_health.py .....                                              
       [ 18%]
   tests-1  | test_k8s_delegation.py ........                                   
       [ 23%]
   tests-1  | test_k8s_jwks_ca.py ...........                                   
       [ 30%]
   tests-1  | test_k8s_serviceaccount_validation.py ......                      
       [ 33%]
   tests-1  | test_knox_admin_path_traversal.py ...                             
       [ 35%]
   tests-1  | test_knox_auth_service_and_ldap.py ...                            
       [ 37%]
   tests-1  | test_knox_configs.py .                                            
       [ 38%]
   tests-1  | test_knox_ldap_backend_password_alias_retry.py ..                 
       [ 39%]
   tests-1  | test_knox_ldap_cache.py ...                                       
       [ 41%]
   tests-1  | test_knox_ldap_injection.py .......                               
       [ 45%]
   tests-1  | test_knox_ldap_proxy_search.py ...............                    
       [ 54%]
   tests-1  | test_knoxauth_preauth_and_paths.py ......                         
       [ 58%]
   tests-1  | test_knoxauth_token_forwarding.py ...........                     
       [ 65%]
   tests-1  | test_knoxidf.py .......                                           
       [ 69%]
   tests-1  | test_knoxsso_redirect.py .                                        
       [ 70%]
   tests-1  | test_knoxtoken_jwt.py ....................                        
       [ 82%]
   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  | ================= 162 passed, 140 warnings in 75.96s (0:01:15) 
=================
   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