[
https://issues.apache.org/jira/browse/KNOX-3568?focusedWorklogId=1054287&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-1054287
]
ASF GitHub Bot logged work on KNOX-3568:
----------------------------------------
Author: ASF GitHub Bot
Created on: 12/Nov/26 13:59
Start Date: 12/Nov/26 13:59
Worklog Time Spent: 10m
Work Description: smolnar82 opened a new pull request, #1511:
URL: https://github.com/apache/knox/pull/1511
[KNOX-3568](https://issues.apache.org/jira/browse/KNOX-3568) - Audit the
outcome of every delegated (act-bearing) request checked by
K8sDestinationAudienceValidator
## What changes were proposed in this pull request?
Requests presenting a delegated token (one carrying an RFC 8693 `act` claim)
now leave an audit trail at the destination, whether the token is accepted or
rejected. Tokens without an `act` claim produce no new records.
- **Audience-check record:** `K8sDestinationAudienceValidator` writes an
`audience-validation` record (a new `Action`) with the outcome of the check:
`event_type=delegated_audience_check claims_verified=false token_id=... sub=...
act_chain=... dest_service=... [dest_spiffe_id=...] path=...`, plus
`reason=audience_invalid detail=...` on failure. The destination header values
are recorded raw, so even a value that fails to parse shows up.
- **Rejection record:** whenever `AbstractJWTFilter` rejects a delegated
token, it writes an `authentication` failure record with
`event_type=delegated_request_rejected` and a machine-parseable reason:
`audience_invalid`, `token_expired`, `signature_invalid`, `untrusted_issuer`,
`not_before`, `token_disabled`, `idle_timeout_exceeded` or
`token_state_unknown`.
- **Success record:** the existing authentication success record for a
delegated token (`act_chain=...`) now also carries the destination fields
(`dest_service`, `dest_spiffe_id`, `path`).
- **Safety:**
- The claims are read before the signature is verified, so the records say
`claims_verified=false`.
- Every value is length-capped and stripped of line-breaking characters,
and quoted if it contains whitespace or `"`.
- The token is identified only by its displayable id, and no token
material is written.
- The record's resource name drops the query string, which may carry
credentials.
- The audience-failure reason logged by `AbstractJWTFilter` is now
sanitized the same way.
The new logic lives in `DelegatedRequestAuditing`. A new `rejectToken`
helper in `AbstractJWTFilter` writes the rejection record and then calls the
existing `handleValidationError`.
## How was this patch tested?
- New unit tests:
- `DelegatedRequestAuditingTest`: field rendering, sanitization, quoting
and query stripping.
- `K8sDestinationAudienceValidatorAuditTest`: audience-check records for
accepted and rejected tokens, nested act chains, raw header values, and no
record for tokens without `act`.
- `DelegatedRequestAuditTest`: the full `JWTFederationFilter` trail, i.e.
the audience check followed by the success record or the rejection record with
its reason.
- `mvn verify` on `gateway-util-common` and `gateway-provider-security-jwt`
passes (728 tests in the JWT provider module).
- Docker Compose integration suite run locally: 151 passed.
## Integration Tests
`knoxauthtoken.xml` now runs the k8s destination audience validator
(`x-dest-server-name` header, `validate.audiences.without.act.claim=false`).
Tokens without an `act` claim are unaffected, so the existing KNOX-3478
token-forwarding tests on that topology still pass. The new
`delegated_audit_helpers.py` reads `gateway-audit.log` from the mounted log
directory and finds a request's records by request id, using a random query
marker.
- `test_delegation.py`:
- An accepted delegated token leaves an audience-validation success record
and an authentication success record with the act chain and destination.
- A token presented for another destination gets a 400 and leaves an
audience-validation failure record and a `delegated_request_rejected` record,
both with `reason=audience_invalid`.
- A token exchanged twice is audited with its whole act chain, most recent
actor first (`sam<-recursiveUser`).
- No audit line contains the token's signature.
- `test_token_exchange.py`: a same-subject exchanged token (no `act` claim)
leaves no audience-validation record and a plain authentication success record.
Issue Time Tracking
-------------------
Worklog Id: (was: 1054287)
Remaining Estimate: 0h
Time Spent: 10m
> Audit the outcome of every delegated (act-bearing) request checked by
> K8sDestinationAudienceValidator
> -----------------------------------------------------------------------------------------------------
>
> Key: KNOX-3568
> URL: https://issues.apache.org/jira/browse/KNOX-3568
> Project: Apache Knox
> Issue Type: Improvement
> Components: JWT, KnoxIDF
> Reporter: Sandor Molnar
> Assignee: Sandor Molnar
> Priority: Critical
> Fix For: 3.1.0
>
> Time Spent: 10m
> Remaining Estimate: 0h
>
> With RFC 8693 on-behalf-of token exchange, a delegated token names the user
> in {{sub}} and the acting workload in the {{act}} claim. Knox records the
> delegation when it mints the token ({{TokenResource}},
> {{addingActorClaimToToken}}). KNOX-3477 also adds {{act_chain=}} to the
> {{authentication|success}} record in {{gateway-audit.log}} once a delegated
> token is accepted.
> What's not recorded is which destination the delegated token was presented
> to, or any delegated request that was rejected. When
> {{K8sDestinationAudienceValidator}} (KNOX-3491/KNOX-3509) rejects a token,
> the only trace is a {{gateway.log}} WARN with no actor and no destination. If
> a workload misuses or replays a delegated token, nothing ties the attempt to
> that workload. Because the backend services only see the end user, Knox is
> the one place that can record it.
> h3. Proposal
> For tokens carrying an {{act}} claim, write two kinds of records to
> {{gateway-audit.log}}:
> # a *validator record*, written by {{K8sDestinationAudienceValidator}},
> stating what the audience check decided;
> # a *final record*, written by {{AbstractJWTFilter.validateToken}}, stating
> what Knox decided for the request.
> Both records for a request share the root/parent/request correlation ids that
> {{AuditLayout}} already writes on every line, so they can be joined.
> h4. Validator record
> Written whenever the topology's request audience validator is
> {{request.audience.k8s.destination.validation}}, the validator runs, and the
> token has an {{act}} claim.
> * Uses a new action, e.g. {{Action.AUDIENCE_VALIDATION}}, so it can be
> filtered separately from {{authentication}} records.
> * The outcome is the audience check's result: {{success}}, or {{failure}}
> with the reason.
> * Carries {{claims_verified=false}}. The audience check runs before signature
> verification, so the claims in this record are not verified yet.
> * Fields:
> ** {{sub}}: the user. It goes in the message, because the audit context's
> {{username}} column is only populated later, in
> {{continueWithEstablishedSecurityContext}}.
> ** {{act_chain}}: the actor chain from outermost (most recent) to innermost.
> Use the same rendering as KNOX-3477 ({{SubjectUtils.renderActorChain}}).
> ** {{dest_service}}: the destination as read from the configured
> {{request.audience.k8s.server.name.header.name}} (and/or the namespace from
> the destination SPIFFE-id header, if that's configured).
> ** {{path}}: the request path (the configured path header if set, otherwise
> the request URI). No query string.
> ** {{reason}}: on failure only.
> h4. Final record
> Written by the filter once the outcome is known, for every delegated request
> it rejects or accepts.
> * *Accepted:* extend the existing KNOX-3477
> {{authentication|success|act_chain=...}} record with {{dest_service}} and
> {{path}} rather than writing a second success line. The validator exposes the
> destination and path it parsed through a request attribute, so the filter
> doesn't parse them again.
> * *Rejected:* a new {{authentication|failure}} record (none exists today)
> with {{sub}}, {{act_chain}}, {{dest_service}}, {{path}} and the reason. This
> covers every rejection: expiry, the audience check, not-before, a disabled
> token, the idle timeout, and the signature.
> When the audience check passes but a later check fails (e.g. a forged token
> rejected on its signature), the validator record shows {{success}} with
> {{claims_verified=false}}, and the final record shows the actual rejection.
> h3. Constraints
> * Tokens without {{act}} produce no new record. Existing behavior for those
> tokens is unchanged.
> * No token material is logged: no raw JWT, no signature. The token id
> ({{displayableTokenId}}) is allowed.
> * Every value that comes from a header or claim ({{sub}}, {{act}} entries,
> header values, path, and the rejection reason) is sanitized before logging,
> in both records. CR, LF and other control characters are escaped or removed,
> and long values are truncated, so a value can't inject or fake log lines.
> * The same sanitization is applied to the existing
> {{failedToValidateAudience}} reasons, which currently copy raw header values
> into {{gateway.log}}.
> h3. Acceptance criteria
> * Every delegated request writes exactly one final record, accepted or
> rejected.
> * Every delegated request that reaches {{K8sDestinationAudienceValidator}}
> also writes exactly one validator record, marked {{claims_verified=false}}.
> * Both records for a request can be correlated by request id.
> * Non-delegated tokens produce no new record of either kind.
> * No raw JWT or signature appears in any record.
> * Header and claim values are sanitized against CR/LF and control-character
> injection.
> * Unit tests cover:
> ** an accepted request (validator record {{success}}, final record extends
> the KNOX-3477 success record);
> ** a rejection for an {{aud}}/destination mismatch (validator record
> {{failure}}, final record {{failure}});
> ** an audience check that passes followed by a signature failure (validator
> record {{success}}, final record {{failure}});
> ** an expired delegated token (final record only);
> ** a nested {{act}} chain with 2 or more levels, checking the order;
> ** a token without {{act}} (no new records);
> ** an injection attempt (CR/LF in the destination header and in {{act.sub}}).
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)