Sandor Molnar created KNOX-3582:
-----------------------------------

             Summary: 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
             Fix For: 3.1.0


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.



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

Reply via email to