Files
seaweedfs/weed/iam/sts/parent_user_test.go
T
Chris Lu a3638e479e fix(s3api/audit): surface OIDC identity claim in audit log for STS sessions (#11269)
* Add ResolveIdentityClaim helper for OIDC audit identity

ComputeParentUser derives a stable per-identity hash from (sub, iss) for
internal keying, but it is opaque and not human-readable. Audit logs for
STS-assumed OIDC sessions currently surface that opaque value (or the
random session id) as the requester, leaving no authoritative trace of the
federated user.

Add ResolveIdentityClaim next to ComputeParentUser to recover a
human-readable, server-asserted identity attribute from the STS request
context populated at federation time. It walks a priority list
(preferred_username, email, name, sub) so a federated session always
audits against a stable OIDC claim rather than a client-supplied role
session name.

For #11264

* Surface authoritative OIDC identity claim in S3 audit log

For STS-assumed sessions minted from an OIDC web identity, the audit log
requester field is the opaque session subject, which cannot be traced back
to the federated user who performed the operation. The OIDC identity claims
(preferred_username, email, sub) are already carried in the session request
context and reach the auth layer as identity.Claims, but they were never
surfaced to the audit log.

Add a requester_identity field to the S3 access audit log, populated from
the authoritative OIDC identity claim resolved via ResolveIdentityClaim.
The claim is propagated through the shared identity holder (the same
mechanism the requester name and principal ARN already use) so it survives
the request-context copy that hides auth-set values from the outer audit
middleware.

The existing requester field is left unchanged for backward compatibility;
requester_identity is empty for non-federated sessions, where requester
already carries the real username.

For #11264

* Gate OIDC audit identity on federation marker and harden resolver

Address review feedback (Devin Review, Greptile) on the initial
implementation:

- Non-federated STS sessions no longer gain a false requester_identity.
  ValidateJWTWithClaims merges the JWT registered sub claim (the opaque
  session id) into RequestContext for sessions without an explicit request
  context, so the previous ResolveIdentityClaim fallback to sub surfaced
  that session id as an authoritative identity. Resolution is now gated on
  SessionInfo.ParentUser, which is set only for OIDC-federated sessions in
  AssumeRoleWithWebIdentity. The claim is resolved from the original
  sessionInfo.RequestContext (not the local claims map, whose sub the bearer
  path overwrites with the session subject) so SigV4 and bearer sessions
  surface the same identity.

- ResolveIdentityClaim now trims whitespace and treats whitespace-only
  claims as absent, so a blank preferred_username no longer masks a usable
  email or sub.

The resolved claim is carried on Identity.IdentityClaim (and IAMIdentity for
the bearer path) rather than re-derived in recordIdentityInContext, making
the federation gate explicit at the auth boundary.

For #11264

* Resolve OIDC identity claim for external bearer tokens

The external OIDC bearer path (a raw OIDC JWT presented directly, not via
STS) populates Claims with preferred_username/email/name/sub from the
validated token but did not set IdentityClaim, so requester_identity stayed
blank for that authentication path. Resolve the claim there too — sub is the
real OIDC subject on this path (not an STS session id), so no federation
gate is needed.

Also drop an ineffectual ctx assignment flagged by ineffassign in the audit
test.

For #11264
2026-09-11 11:29:46 -07:00

133 lines
4.5 KiB
Go

package sts
import (
"strings"
"testing"
"time"
)
func TestComputeParentUserStability(t *testing.T) {
// Same (sub, iss) must produce the same hash, regardless of order or
// whitespace mutations callers should never apply.
a := ComputeParentUser("alice", "https://idp.example/")
b := ComputeParentUser("alice", "https://idp.example/")
if a == "" {
t.Fatal("parent user should not be empty for non-empty sub")
}
if a != b {
t.Fatalf("expected stable hash, got %q vs %q", a, b)
}
}
func TestComputeParentUserDistinguishesIssuer(t *testing.T) {
// The point of incorporating iss is that the same `sub` from two providers
// must not collide. If this assertion ever fails, the hash input is wrong.
a := ComputeParentUser("alice", "https://idp-a.example/")
b := ComputeParentUser("alice", "https://idp-b.example/")
if a == b {
t.Fatalf("hashes for different issuers must differ, both = %q", a)
}
}
func TestComputeParentUserDistinguishesSubject(t *testing.T) {
a := ComputeParentUser("alice", "https://idp.example/")
b := ComputeParentUser("bob", "https://idp.example/")
if a == b {
t.Fatalf("hashes for different subjects must differ, both = %q", a)
}
}
func TestComputeParentUserEmptySub(t *testing.T) {
if got := ComputeParentUser("", "https://idp.example/"); got != "" {
t.Fatalf("empty sub should produce empty parent user, got %q", got)
}
}
func TestComputeParentUserEmptyIss(t *testing.T) {
// Without an issuer, two different IDPs that both name a user "alice"
// would collide on the same parent_user. Refuse rather than hash.
if got := ComputeParentUser("alice", ""); got != "" {
t.Fatalf("empty iss should produce empty parent user, got %q", got)
}
}
func TestComputeParentUserEncoding(t *testing.T) {
got := ComputeParentUser("alice", "https://idp.example/")
// Base64 RawURL has no padding and uses URL-safe alphabet — important
// because parent_user shows up in filer paths and audit log fields.
if strings.ContainsAny(got, "=+/") {
t.Fatalf("parent user should be base64 raw url, got %q", got)
}
}
func TestSessionClaimsRoundTripParentUser(t *testing.T) {
parent := ComputeParentUser("alice", "https://idp.example/")
claims := NewSTSSessionClaims("sid-1", "issuer", time.Now().Add(time.Hour)).
WithRoleInfo("arn:aws:iam::123:role/r", "arn:aws:sts::123:assumed-role/r/s", "arn:aws:sts::123:assumed-role/r/s").
WithParentUser(parent)
info := claims.ToSessionInfo()
if info.ParentUser != parent {
t.Fatalf("ParentUser lost on round-trip: got %q want %q", info.ParentUser, parent)
}
}
func TestResolveIdentityClaimPrefersPreferredUsername(t *testing.T) {
ctx := map[string]interface{}{
"preferred_username": "grant.west",
"email": "grant.west@example.com",
"sub": "alice-sub",
}
if got := ResolveIdentityClaim(ctx); got != "grant.west" {
t.Fatalf("expected preferred_username, got %q", got)
}
}
func TestResolveIdentityClaimFallsBackThroughPriority(t *testing.T) {
if got := ResolveIdentityClaim(map[string]interface{}{"email": "g@example.com", "sub": "s"}); got != "g@example.com" {
t.Fatalf("expected email fallback, got %q", got)
}
if got := ResolveIdentityClaim(map[string]interface{}{"sub": "s"}); got != "s" {
t.Fatalf("expected sub fallback, got %q", got)
}
}
func TestResolveIdentityClaimEmpty(t *testing.T) {
if got := ResolveIdentityClaim(nil); got != "" {
t.Fatalf("expected empty for nil ctx, got %q", got)
}
if got := ResolveIdentityClaim(map[string]interface{}{}); got != "" {
t.Fatalf("expected empty for empty ctx, got %q", got)
}
}
func TestResolveIdentityClaimSkipsNonString(t *testing.T) {
ctx := map[string]interface{}{
"preferred_username": []string{"no"},
"sub": "real-sub",
}
if got := ResolveIdentityClaim(ctx); got != "real-sub" {
t.Fatalf("expected sub after skipping non-string claim, got %q", got)
}
}
func TestResolveIdentityClaimTrimsWhitespace(t *testing.T) {
ctx := map[string]interface{}{
"preferred_username": " grant.west ",
"email": "grant.west@example.com",
}
if got := ResolveIdentityClaim(ctx); got != "grant.west" {
t.Fatalf("expected trimmed preferred_username, got %q", got)
}
}
func TestResolveIdentityClaimSkipsWhitespaceOnly(t *testing.T) {
ctx := map[string]interface{}{
"preferred_username": " ",
"email": "grant.west@example.com",
}
if got := ResolveIdentityClaim(ctx); got != "grant.west@example.com" {
t.Fatalf("expected email after skipping whitespace-only preferred_username, got %q", got)
}
}