[ 
https://issues.apache.org/jira/browse/KNOX-3582?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Sandor Molnar updated KNOX-3582:
--------------------------------
    Description: 
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.


  was:
h3. Problem

The KnoxIDF {{TokenResource}} gets its token state setup from 
{{{}PasscodeTokenResourceBase#setupTokenStateService{}}}, which always wires in 
the token state service:
{code:java}
wrapperContext.setInitParameter(TokenStateService.CONFIG_SERVER_MANAGED, 
"true");
...
tokenStateService = services.getService(ServiceType.TOKEN_STATE_SERVICE);
{code}
The {{setInitParameter}} call only sets a default. 
{{ServletContextWrapper#getInitParameter}} checks the {{{}knoxidf.{}}}-prefixed 
extension params first, 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}} runs from {{wrapContextForDefaultParams}} 
at the start of {{{}TokenResource#init{}}}, before that check. By then 
{{tokenStateService}} is already assigned, and nothing clears it. Saving the 
token and setting the {{managed}} claim only check {{{}tokenStateService != 
null{}}}:
 * {{TokenResource#persistTokenDetails}}
 * the KnoxIDF refresh token in {{issueRefreshToken}}
 * {{setManaged(tokenStateService != null)}}

So every token KnoxIDF issues is saved and marked managed, whatever the 
topology says. There is no way to turn this off.
h3. Impact

For grants whose tokens are never revoked or looked up, saving them is pure 
overhead. One example is RFC 8693 token exchange, 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

Scope the change to the KnoxIDF {{{}TokenResource{}}}, for example by 
overriding {{{}setupTokenStateService{}}}. The passcode-based resources 
({{{}APIKeyResource{}}}, {{{}ClientCredentialsResource{}}}, 
{{{}OAuthResource{}}}) and the KnoxIDF {{AuthorizeResource}} need token state 
and stay as they are.
 * *Not declared:* default to {{true}} (today's behaviour).
 * *Declared {{{}true{}}}:* the same as today.
 * *Declared {{{}false{}}}:*
 ** Don't assign {{{}tokenStateService{}}}. Tokens are issued without the 
{{managed}} claim and are not saved. 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, because {{TokenMAC}} needs it to 
check {{{}client_secret{}}}.
 ** Grants that need stored state can't work. These are {{authorization_code}} 
(the code lookup) and {{refresh_token}} (and issuing refresh tokens for 
{{{}offline_access{}}}). Reject them with a clear OAuth error such as 
{{unsupported_grant_type}} instead of failing with an NPE, and log a warning at 
startup.

h3. Testing
 * Unit tests for the KnoxIDF {{{}TokenResource{}}}:
 ** Not declared, or {{{}true{}}}: the token is saved and has the {{managed}} 
claim.
 ** {{{}false{}}}: the token is not saved and has no {{managed}} claim.
 ** {{{}false{}}}: the {{authorization_code}} and {{refresh_token}} grants are 
rejected, and no refresh token is issued.
 * The tests for the passcode-based resources pass unchanged.


> 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
>
>
> 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