Files
seaweedfs/weed/iam/sts/oidc_provider_lookup_test.go
T
Chris Lu 6dd6ad512c iam/oidc: drop single-use web-identity tokens and the jti replay cache (#12065)
* iam/oidc: drop single-use web-identity tokens and the jti replay cache

Reverts the single-use semantics added in #11936 (and the #12017
refactor): AssumeRoleWithWebIdentity consumed each web-identity
token's jti into a bounded replay cache, so a second exchange of the
same token was rejected until exp + skew.

AWS STS accepts a web-identity token repeatedly until it expires,
and Kubernetes workload identity depends on that: the kubelet keeps
a projected ServiceAccount token until ~80% of its lifetime, so pod
restarts, co-located containers sharing the token file, repeated CLI
invocations, and pre-rotation SDK refreshes all re-present the same
jti and were refused on 4.50.

The replay machinery also carried a real maintenance cost — a
bounded concurrent cache, fingerprint-keyed entries for jti-less
tokens, adopt/merge handoffs so provider rebuilds could not reopen
consumed tokens, and provider-reuse bookkeeping in the refresh path
that existed only to preserve that state. Removing it restores AWS
semantics and deletes the whole apparatus.

Refs #12063. Alternative to the per-provider opt-out in #12064.

* iam: reuse unchanged OIDC providers on refresh

refreshOIDCProvidersFrom rebuilt every managed provider on any store
change, so an unrelated write discarded each provider's JWKS cache and
an unreachable issuer could then fail exchanges the cached keys would
have allowed. Keep a per-record provider cache keyed on the record
content fingerprint and only rebuild when the record changes.

* iam/oidc: require exp in ValidateToken

Dropping the single-use check also removed the implicit bound on
expiry-less tokens: a signed web-identity token without exp could
mint STS sessions indefinitely. Require the exp claim, matching AWS
AssumeRoleWithWebIdentity which rejects tokens without one.

* iam/oidc: bound idle connections on the provider transport

A provider replaced on refresh drops its http.Transport without a
close; give it an IdleConnTimeout so pooled connections and their
goroutines cannot linger.

* s3api: cover web-identity token reuse over HTTP

Sign one token and exchange that exact string twice, directly covering
the Kubernetes projected-token reuse this change restores.

* test(iam): refresh reuses an unchanged record's provider

Covers the provider-reuse cache: an unrelated record's refresh keeps a
provider's JWKS state, a changed record rebuilds it.
2026-10-25 12:04:59 -07:00

106 lines
3.8 KiB
Go

package sts
import (
"context"
"testing"
"github.com/seaweedfs/seaweedfs/weed/iam/providers"
)
// stubIdentityProvider is the minimal IdentityProvider needed to drive
// lookupOIDCProviderForAccount; the lookup never calls Authenticate.
type stubIdentityProvider struct{ name string }
func (s *stubIdentityProvider) Name() string { return s.name }
func (s *stubIdentityProvider) Initialize(interface{}) error { return nil }
func (s *stubIdentityProvider) Authenticate(context.Context, string) (*providers.ExternalIdentity, error) {
return nil, nil
}
func (s *stubIdentityProvider) GetUserInfo(context.Context, string) (*providers.ExternalIdentity, error) {
return nil, nil
}
func (s *stubIdentityProvider) ValidateToken(context.Context, string) (*providers.TokenClaims, error) {
return nil, nil
}
func TestLookupOIDCProviderForAccountPrefersAccountMatch(t *testing.T) {
const issuer = "https://example.com"
accountA := &stubIdentityProvider{name: "A"}
accountB := &stubIdentityProvider{name: "B"}
s := &STSService{issuerToProvider: map[string]providers.IdentityProvider{}}
s.SetIAMManagedOIDCProviders(map[string][]ScopedOIDCProvider{
issuer: {
{AccountID: "111111111111", Provider: accountA},
{AccountID: "222222222222", Provider: accountB},
},
})
got, ok := s.lookupOIDCProviderForAccount(issuer, "222222222222")
if !ok || got != accountB {
t.Fatalf("expected accountB provider for matching account, got %v ok=%v", got, ok)
}
got, ok = s.lookupOIDCProviderForAccount(issuer, "111111111111")
if !ok || got != accountA {
t.Fatalf("expected accountA provider for matching account, got %v ok=%v", got, ok)
}
}
func TestLookupOIDCProviderForAccountFallsBackToGlobal(t *testing.T) {
const issuer = "https://example.com"
global := &stubIdentityProvider{name: "global"}
accountA := &stubIdentityProvider{name: "A"}
s := &STSService{issuerToProvider: map[string]providers.IdentityProvider{}}
s.SetIAMManagedOIDCProviders(map[string][]ScopedOIDCProvider{
issuer: {
{AccountID: "", Provider: global},
{AccountID: "111111111111", Provider: accountA},
},
})
// Account not represented in records → global match.
got, ok := s.lookupOIDCProviderForAccount(issuer, "999999999999")
if !ok || got != global {
t.Fatalf("expected global provider as fallback, got %v ok=%v", got, ok)
}
// Empty account hint → never picks an account-scoped record arbitrarily;
// only the global record is eligible.
got, ok = s.lookupOIDCProviderForAccount(issuer, "")
if !ok || got != global {
t.Fatalf("expected global provider when account unknown, got %v ok=%v", got, ok)
}
}
func TestLookupOIDCProviderForAccountSkipsAccountSpecificWhenAccountUnknown(t *testing.T) {
const issuer = "https://example.com"
accountA := &stubIdentityProvider{name: "A"}
s := &STSService{issuerToProvider: map[string]providers.IdentityProvider{}}
s.SetIAMManagedOIDCProviders(map[string][]ScopedOIDCProvider{
issuer: {
{AccountID: "111111111111", Provider: accountA},
},
})
// No account hint AND no global record → must fall through (return false),
// not silently pick the account-A entry. Picking an arbitrary entry is the
// pre-fix bug that lets a token be validated by the wrong tenant's record.
if _, ok := s.lookupOIDCProviderForAccount(issuer, ""); ok {
t.Fatalf("expected no match when account unknown and only account-scoped records exist")
}
}
func TestLookupOIDCProviderForAccountFallsBackToStatic(t *testing.T) {
const issuer = "https://example.com"
static := &stubIdentityProvider{name: "static"}
s := &STSService{issuerToProvider: map[string]providers.IdentityProvider{issuer: static}}
s.SetIAMManagedOIDCProviders(nil)
got, ok := s.lookupOIDCProviderForAccount(issuer, "111111111111")
if !ok || got != static {
t.Fatalf("expected static-config provider as last resort, got %v ok=%v", got, ok)
}
}