Sandor Molnar created KNOX-3606:
-----------------------------------

             Summary: KnoxIDF: in-flight authorization state is node-local, 
breaking the authorization-code flow in HA deployments
                 Key: KNOX-3606
                 URL: https://issues.apache.org/jira/browse/KNOX-3606
             Project: Apache Knox
          Issue Type: Improvement
          Components: KnoxIDF
            Reporter: Sandor Molnar
            Assignee: Sandor Molnar


h2. Problem

KnoxIDF keeps the state of an in-flight authorization request in JVM-local 
Caffeine caches ({{{}KnoxIDFArtifactStore{}}} subclasses, static singletons):
 * {{{}AuthorizeRequestMetadataStore{}}}: the pending authorize request 
(client_id, subject, redirect_uri, scopes, state, nonce, PKCE challenge)
 * {{{}FederatedOpConfigurationStore{}}}: the federated OPs offered for a login 
session
 * {{{}FederatedNonceStore{}}}: the nonce sent to the federated OP

These entries are written on one request and read back on a later browser 
request:
 * *Consent flow:* {{AuthorizeResource#authorize}} stores the request under a 
random consent state and redirects to {{{}/authConsent{}}}. {{POST 
.../authorize/consentAccepted}} looks it up again.
 * *Federated login flow:* {{SSOCookieFederationFilter#constructLoginURL}} 
stores the request and the OP configs. {{WebSSOResource}} reads the OP configs 
and stores the nonce. After the external OP redirects back, 
{{AuthorizeResource#authCallback}} ({{{}/authorize/callback{}}}) reads all 
three.

With more than one Knox instance behind a load balancer, the follow-up request 
can reach an instance that never saw the first one. The lookup misses and the 
user gets {{invalid_request}} ("Invalid state" / "Unknown or expired state"). 
Without session affinity, the failure rate is roughly (N-1)/N for N instances. 
The flow fails closed, so this is an availability problem, not a security one.

Persisted KnoxIDF state is not affected. Consent records and authorization 
codes are stored through {{{}TokenStateService{}}}, and with an external 
database {{JDBCTokenStateService}} reads them directly from the DB (KNOX-2658).

The HA section of the KnoxIDF operations guide says sticky sessions are 
"recommended". In practice they are *required* for any interactive 
authorization-code flow.

Related: {{AuthorizeRequestMetadataStore.getInstance(ttl)}} (and the other 
store singletons) ignore {{ttl}} after the first call. 
{{SSOCookieFederationFilter}} passes {{120000}} and {{AuthorizeResource}} 
passes the topology token TTL, so the effective TTL depends on which one 
initializes first.
h2. Proposed solution

Move in-flight authorization state to the shared gateway database when one is 
configured, so every instance can complete any flow and LB affinity is no 
longer needed.
 * Turn {{KnoxIDFArtifactStore}} into a pluggable abstraction with two 
implementations:
 ** in-memory (current Caffeine behavior), the default when no external 
database is configured (single instance);
 ** JDBC-backed, selected when {{gateway.database.*}} points at an external 
database. This follows the existing JDBC selection logic used for token state 
and federated identities.
 * New table (e.g. {{{}KNOXIDF_AUTHZ_FLOW_STATE{}}}: {{{}ID{}}}, {{{}KIND{}}}, 
{{{}PAYLOAD{}}}, {{{}EXPIRES_AT{}}}) with DDL for every supported database, 
following the existing {{createKnoxIDF*.sql}} / {{*Oracle.sql}} pattern. 
{{PAYLOAD}} is a serialized form of the artifact. Store only what the flow 
needs; for example, store federated OP names and resolve their configuration 
from the topology instead of persisting secrets.
 * *Atomic single-use consume.* The state must be consumable exactly once 
across the cluster, using a {{DELETE ... WHERE ID = ? AND EXPIRES_AT > ?}} 
whose affected-row count decides the winner (or an equivalent transactional 
read-and-delete). This keeps today's single-use / anti-replay guarantees for 
the consent state and the federated callback state, including the 
subject-mismatch path.
 * *Expiry:* reads ignore expired rows, and a periodic sweep deletes them.
 * Fix the singleton TTL handling so each store's TTL is explicit and 
deterministic.
 * Docs: update the HA section of {{{}knox-site/docs/knoxidf/operations.md{}}}. 
Without an external DB, affinity stays required. With one, it is no longer 
needed.

Considered and not preferred: putting the authorize request into a signed and 
encrypted (JWE) {{state}} value. It needs no storage, but it still needs a 
shared replay record for strict single use, it puts the request into URLs and 
into the external OP's logs, and it grows URL length.
h2. Workaround

Configure cookie-based session affinity on the load balancer for the KnoxIDF 
authorize path (e.g. {{{}/gateway/<topology>/knoxidf/api/v1/authorize{}}}; for 
the federated flow, also the WebSSO login path). Source-IP affinity is not 
reliable behind proxies. For the federated flow, the affinity cookie must not 
be {{{}SameSite=Strict{}}}, because the OP callback is a cross-site top-level 
redirect. A flow that is in progress during an instance failover still fails, 
and the user has to restart it.
h2. Acceptance criteria
 * With an external DB and 2+ instances behind a round-robin LB with no 
affinity, both the consent flow and the federated login flow succeed every time.
 * Each consent / federated-callback state can be consumed exactly once 
cluster-wide, and a concurrent replay against another instance is rejected.
 * Expired state is rejected and removed by the sweep.
 * Single-instance deployments with no external DB behave as today.
 * Unit tests cover both store implementations, and an integration test runs 
the flow across two instances.
 * The operations guide HA section is updated.



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

Reply via email to