Files
seaweedfs/weed/iam/sts/source_access_key_test.go
T
Chris LuandClaude Opus 5.5 2acd49ba27 s3api: end a vended table session no later than the caller's credential (#13161)
* s3api: record the verified credential's expiry in the request context

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3api: cap a vended table session at the caller's credential expiry

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* sts: let a principal session request cap the session's expiry

AssumeRoleForPrincipal takes an absolute NotAfter. issueSession caps both
the credentials and the signed token at it, so a cap computed before the
trust-policy lookup is not stretched by the time that lookup takes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3api: hand the caller's credential expiry to STS as an absolute cap

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 10:15:36 -08:00

67 lines
2.7 KiB
Go

package sts
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func newSourceKeyTestSTS(t *testing.T) *STSService {
t.Helper()
s := NewSTSService()
require.NoError(t, s.Initialize(&STSConfig{
TokenDuration: FlexibleDuration{Duration: time.Hour},
MaxSessionLength: FlexibleDuration{Duration: 12 * time.Hour},
Issuer: "test-issuer",
SigningKey: []byte("test-signing-key-at-least-32-bytes-long-for-security"),
}))
return s
}
// A session minted for a caller's access key carries that key back out of its
// token; a session minted without one carries none.
func TestSessionCarriesItsSourceAccessKey(t *testing.T) {
s := newSourceKeyTestSTS(t)
resp, err := s.issueSession("arn:aws:iam::role/IcebergTableAccess", "", "iceberg-alice-ns-t-kabc", "", nil, time.Time{},
"iceberg-catalog", "alice", func(c *STSSessionClaims) { c.WithSourceAccessKeyId("AKIASOURCE") })
require.NoError(t, err)
info, err := s.ValidateSessionToken(context.Background(), resp.Credentials.SessionToken)
require.NoError(t, err)
assert.Equal(t, "AKIASOURCE", info.SourceAccessKeyId)
assert.Equal(t, "alice", info.ExternalUserId)
assert.Equal(t, "iceberg-catalog", info.IdentityProvider)
resp, err = s.issueSession("arn:aws:iam::role/IcebergTableAccess", "", "iceberg-alice-ns-t", "", nil, time.Time{}, "iceberg-catalog", "alice")
require.NoError(t, err)
info, err = s.ValidateSessionToken(context.Background(), resp.Credentials.SessionToken)
require.NoError(t, err)
assert.Equal(t, "", info.SourceAccessKeyId)
}
// A session never expires after notAfter: both the credentials and the token
// end there, however much later the requested duration would have ended it.
func TestSessionEndsNoLaterThanNotAfter(t *testing.T) {
s := newSourceKeyTestSTS(t)
notAfter := time.Now().Add(20 * time.Minute).Truncate(time.Second)
resp, err := s.issueSession("arn:aws:iam::role/IcebergTableAccess", "", "iceberg-alice-ns-t", "", nil, notAfter,
"iceberg-catalog", "alice")
require.NoError(t, err)
assert.Equal(t, notAfter, resp.Credentials.Expiration)
info, err := s.ValidateSessionToken(context.Background(), resp.Credentials.SessionToken)
require.NoError(t, err)
assert.False(t, info.ExpiresAt.After(notAfter), "token expires at %s, after %s", info.ExpiresAt, notAfter)
later := time.Now().Add(48 * time.Hour)
resp, err = s.issueSession("arn:aws:iam::role/IcebergTableAccess", "", "iceberg-alice-ns-t", "", nil, later,
"iceberg-catalog", "alice")
require.NoError(t, err)
assert.True(t, resp.Credentials.Expiration.Before(later))
_, err = s.issueSession("arn:aws:iam::role/IcebergTableAccess", "", "iceberg-alice-ns-t", "", nil, time.Now().Add(-time.Second),
"iceberg-catalog", "alice")
assert.Error(t, err)
}