[ 
https://issues.apache.org/jira/browse/KNOX-3582?focusedWorklogId=1055397&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-1055397
 ]

ASF GitHub Bot logged work on KNOX-3582:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 17/Nov/26 18:16
            Start Date: 17/Nov/26 18:16
    Worklog Time Spent: 10m 
      Work Description: smolnar82 opened a new pull request, #1521:
URL: https://github.com/apache/knox/pull/1521

   [KNOX-3582](https://issues.apache.org/jira/browse/KNOX-3582) - Honor 
knox.token.exp.server-managed in PasscodeTokenResourceBase
   
   ## What changes were proposed in this pull request?
   
   `PasscodeTokenResourceBase#setupTokenStateService` always assigned the token 
state service. As a result, an explicit `knox.token.exp.server-managed=false` 
was ignored for the `CLIENTID`, `APIKEY` and KnoxIDF resources, and every token 
they issued was stored and marked managed. It also left the resource 
inconsistent: the parent `TokenResource#init` skipped building the token MAC 
because of the `false` setting, while `tokenStateService` was still set, so 
`persistTokenDetails` failed with an NPE.
   
   Changes:
   - `setupTokenStateService` keeps `true` as the default for topologies that 
don't declare the setting, using the wrapper's lowest-precedence default param. 
It then reads the effective value with `isServerManagedTokenStateEnabled` (now 
`protected`) and assigns `tokenStateService` only when it is enabled. An 
explicit value, plain or extension-prefixed (for example 
`knoxidf.knox.token.exp.server-managed`), is honored. A blank value falls back 
to the gateway-level setting, as it does for `KNOXTOKEN`.
   - When it is disabled, an INFO message is logged for the topology.
   - The token hash key alias is still generated in both cases, because the 
KnoxIDF token endpoint always builds a `TokenMAC` from it.
   
   With `false`, the token endpoint issues unmanaged tokens. They are not 
stored, get `managed.token=false`, and cannot be revoked. This is meant for 
token-exchange-only topologies, where storing every short-lived token costs DB 
writes on the request path. Grants and endpoints that need stored state are not 
supported with this setting: the KnoxIDF `authorization_code` and 
`refresh_token` grants, authorize, registration, and API key and client 
credentials passcodes. The docs say so and recommend pairing the setting with 
`jwt.allowed.grant.types` (KNOX-3578).
   
   Docs:
   - `config_knox_token.md`: `CLIENTID`, `APIKEY` and KnoxIDF default the 
setting to `true`, and an explicit value is honored.
   - `knoxidf/configuration.md`: the new 
`knoxidf.knox.token.exp.server-managed` parameter and an "Unmanaged tokens" 
section.
   
   ## How was this patch tested?
   
   - New `PasscodeTokenResourceBaseServerManagedTest` (6 tests), covering the 
base resource, `OAuthResource`, `APIKeyResource` and 
`ClientCredentialsResource`:
     - not declared (even with the gateway default `false`) or `true`: token 
state is used;
     - `false`: no token state, and the hash key alias is still generated;
     - blank: falls back to the gateway default;
     - an extension-prefixed value wins over a plain one, and works on its own.
   - `mvn -pl gateway-service-knoxtoken,gateway-service-knoxidf install` with 
checkstyle: knoxtoken 152 tests and KnoxIDF 274 tests, all passing.
   
   ## Integration Tests
   
   No integration tests added. The existing compose topologies don't set the 
parameter, so they keep today's default behaviour (`true`). The unit tests 
above cover the new `false` path.
   




Issue Time Tracking
-------------------

            Worklog Id:     (was: 1055397)
    Remaining Estimate: 0h
            Time Spent: 10m

> Passcode token based resources: honor knox.token.exp.server-managed instead 
> of always enabling token state
> ----------------------------------------------------------------------------------------------------------
>
>                 Key: KNOX-3582
>                 URL: https://issues.apache.org/jira/browse/KNOX-3582
>             Project: Apache Knox
>          Issue Type: Bug
>          Components: Server
>    Affects Versions: 3.0.0
>            Reporter: Sandor Molnar
>            Assignee: Sandor Molnar
>            Priority: Major
>             Fix For: 3.1.0
>
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
> h3. Problem
> {{PasscodeTokenResourceBase#setupTokenStateService}} always wires in the 
> token state service:
> {code:java}
> wrapperContext.setInitParameter(TokenStateService.CONFIG_SERVER_MANAGED, 
> "true");
> ...
> tokenStateService = services.getService(ServiceType.TOKEN_STATE_SERVICE);
> {code}
> Its subclasses all use this setup: {{APIKeyResource}}, 
> {{ClientCredentialsResource}}, {{OAuthResource}} and the KnoxIDF 
> {{TokenResource}}.
> The {{setInitParameter}} call only sets a default. 
> {{ServletContextWrapper#getInitParameter}} checks the extension-prefixed 
> params first ({{knoxidf.}}, {{apikey.}}, {{clientid.}}), then the topology's 
> params, then the defaults. So if a topology sets 
> {{knox.token.exp.server-managed=false}}, 
> {{TokenResource#isServerManagedTokenStateEnabled}} returns {{false}}.
> However, {{setupTokenStateService}} assigns {{tokenStateService}} without 
> looking at that setting, and nothing clears it later. Saving the token and 
> setting the {{managed}} claim only check {{tokenStateService != null}}. So 
> every token these resources issue is saved and marked managed, whatever the 
> topology says. There is no way to turn this off.
> It is also inconsistent: with an explicit {{false}}, the parent 
> {{TokenResource#init}} skips building the token MAC, but 
> {{tokenStateService}} is still set. {{persistTokenDetails}} then fails with 
> an NPE on the null {{tokenMAC}}.
> h3. Impact
> For grants whose tokens are never revoked or looked up, saving them is pure 
> overhead. One example is RFC 8693 token exchange on the KnoxIDF token 
> endpoint, where data services get a short-lived token for a workload. With 
> {{JDBCTokenStateService}}, each issued token means:
> * synchronous DB writes on the request path ({{addToken}} plus 
> {{addMetadata}})
> * one more row in the token tables until it is cleaned up
> h3. Proposal
> Make {{PasscodeTokenResourceBase#setupTokenStateService}} honor the setting:
> * Keep {{true}} as the default by calling {{setInitParameter}} first. Then 
> read the effective value with {{isServerManagedTokenStateEnabled}} (made 
> {{protected}}). A blank value falls back to the gateway-level 
> {{gateway.knox.token.exp.server-managed}}, as it does for KNOXTOKEN.
> * *Not declared, or {{true}}:* the same as today.
> * *Declared {{false}}* (plain or extension-prefixed, for example 
> {{knoxidf.knox.token.exp.server-managed}}): don't assign 
> {{tokenStateService}}, and log that token state is disabled for the topology. 
> Tokens are not saved and get {{managed.token=false}}. They are checked by 
> signature and expiry only, so they can't be revoked; the operator opts into 
> that trade-off.
> * Still generate the token hash key alias in both cases, because the KnoxIDF 
> {{TokenResource}} always builds a {{TokenMAC}} from it.
> There is no special handling for features that need stored state. With 
> {{false}}, these won't work:
> * the KnoxIDF {{authorization_code}} and {{refresh_token}} grants, and the 
> authorize and registration endpoints;
> * API key and client credentials passcodes.
> The setting is meant for token-exchange-only topologies, combined with 
> {{jwt.allowed.grant.types}} (KNOX-3578) so other grants are rejected with 
> {{400 unsupported_grant_type}}. The docs say this.
> h3. Testing
> * Unit tests for {{PasscodeTokenResourceBase}} and its subclasses 
> ({{OAuthResource}}, {{APIKeyResource}}, {{ClientCredentialsResource}}):
> ** Not declared (even with the gateway default {{false}}), or {{true}}: token 
> state is used.
> ** {{false}}: no token state, and the hash key alias is still generated.
> ** Blank: falls back to the gateway default.
> ** An extension-prefixed value wins over a plain one, and works on its own.
> * Existing knoxtoken and KnoxIDF tests pass unchanged.
> h3. Docs
> * {{config_knox_token.md}}: CLIENTID, APIKEY and KnoxIDF default the setting 
> to {{true}}, and an explicit value is honored.
> * {{knoxidf/configuration.md}}: the {{knoxidf.knox.token.exp.server-managed}} 
> parameter and an "Unmanaged tokens" section.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to