mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-10-24 06:50:40 +02:00
* s3: end a catalog-vended session with the access key it was vended for A table credential the Iceberg catalog vends is a self-contained STS session: it stays valid until it expires (an hour by default) even after the access key that loaded the table is deleted or deactivated, or its identity is removed or disabled. An operator revoking a leaked key cannot cut off the table credentials that key already obtained. Sessions are stateless JWTs, so there is no revocation list to consult. This change follows the same pattern as role sessions, which are already refused once their role's ID no longer matches (checkSessionRoleBinding): a vended session carries the access key it was vended for (src_ak, recorded by the previous change), and validateSTSSessionToken checks it against the identity table held in memory. A session of provider iceberg-catalog that carries a source key is refused (InvalidAccessKeyId, as for an unknown key) once its identity is gone or disabled, or the key is no longer one of that identity's active, unexpired credentials. Scope: - Only catalog-vended sessions are checked; web-identity, AssumeRole and every other STS session are validated as before. - The vendor binds a session to the caller's key only when the caller is a stored identity holding that key. A caller that signed with another STS session's key, an OIDC user, or a request that recorded no key gets an unbound session, as today; binding it would refuse every request made with it. A session minted before this change carries no key and is unbound. - The check is one map lookup under the read lock and a scan of one identity's keys (about 11 ns, 0 allocations, with 1,000 identities). Tests (s3api_iceberg_vended_session_test.go) mint real sessions through the catalog vendor and validate them the way a data-path request is validated: deactivating, expiring or deleting key A ends A's session while the session vended through key B on the same identity keeps working; disabling or deleting the identity ends it; callers that are not a stored identity holding the key get unbound sessions that keep working; other STS sessions are not checked. The first two tests and the third fail without this change. * s3: keep the vended-session key binding when the key dies before vend vendedSessionKeyLive gated the binding, so a key revoked between the request's authentication and the vend skipped it and minted an unbound session that stayed usable until it expired. Every catalog auth path pairs the identity name in context with the key that authenticated, so a stored-identity principal always means the request signed with that identity's own key. Bind whenever the principal is a stored identity: a session bound to a now-dead key is refused on its first request, which is what the binding is for. * s3: gate the vended-session key binding on the authenticated caller type lookupByIdentityName(principal) named only that some identity exists, not that the request signed with a stored key: an STS session or OIDC principal that collides with an identity name got a session bound to its temporary key, which vendedSessionKeyLive then refused on every request. Verifiers now record the verified key as a stored credential, and the vendor binds only when that marker is present — keeping the binding even when the key was revoked between authentication and the vend. * s3: enforce the vended key binding under a dedicated claim Sessions minted before the binding existed carry the caller's key in the audit-only SourceAccessKeyId — including temporary STS keys that can never resolve in the identity table. Enforcing on that field would end live table credentials at upgrade; the binding now travels under BoundAccessKeyId, leaving legacy sessions unbound as before. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * s3: bind vended sessions to a credential fingerprint The key id alone cannot identify a credential: an operator can delete a key and re-create it under the same id with a different secret, reviving sessions minted for the original. Verifiers now record a fingerprint of the secret at authentication; the session dies unless it still matches. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * s3: keep recording the caller's key on every catalog session SourceAccessKeyId was unconditional audit metadata before the binding existed; gating it on the stored-credential marker dropped the record for callers that authenticated another way. With the binding under its own claim there is no ambiguity left, so the field records the caller's key again regardless of how it authenticated. * s3: key the vended-session credential fingerprint with the signing key The bound-credential fingerprint travels inside the vended session token. As a plain SHA-256 of the secret it gave any token holder an offline guessing oracle for weak secrets, and CodeQL flags it twice. Store the verified secret in the request marker instead and HMAC it with the STS signing key at vend and at validation. The claim can no longer be mined, and the per-request auth path stops hashing secrets. --------- Co-authored-by: Javier de la Torre <jatorre@lakehousebox.com> Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com> Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
242 lines
8.7 KiB
Go
242 lines
8.7 KiB
Go
package s3api
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/iam/sts"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
|
)
|
|
|
|
// icebergVendedCredentialsProvider names the origin recorded on sessions the
|
|
// catalog mints, so audit trails can tell them apart from ordinary logins.
|
|
const icebergVendedCredentialsProvider = "iceberg-catalog"
|
|
|
|
// SetIcebergCredentialRole configures the role the Iceberg catalog assumes when
|
|
// a client asks for vended credentials. Vending stays off until it is set.
|
|
func (s3a *S3ApiServer) SetIcebergCredentialRole(roleArn string, durationSeconds int64) {
|
|
s3a.icebergCredentialRole = strings.TrimSpace(roleArn)
|
|
s3a.icebergCredentialDuration = durationSeconds
|
|
}
|
|
|
|
// VendedCredentials are short-lived S3 credentials scoped to one table.
|
|
type VendedCredentials struct {
|
|
AccessKeyID string
|
|
SecretAccessKey string
|
|
SessionToken string
|
|
Expiration time.Time
|
|
}
|
|
|
|
// VendTableCredentials mints credentials that can read exactly one table's
|
|
// prefix, and write it too unless readOnly, for a caller the Iceberg catalog
|
|
// has already authenticated and authorized. Returns nil when no role is
|
|
// configured, which leaves the catalog telling the client to keep using its
|
|
// own credentials.
|
|
//
|
|
// The access key the caller authenticated with (from ctx) is recorded in the
|
|
// session: a short hash of it ends the session name, so the audit log's
|
|
// requester_arn tells apart the holders of several keys on one identity. When
|
|
// the caller is a stored identity holding that key, the key id is also carried
|
|
// in the session token, binding the session to it: the session ends when the
|
|
// key is deleted or deactivated, or its identity removed or disabled
|
|
// (validateSTSSessionToken).
|
|
func (s3a *S3ApiServer) VendTableCredentials(ctx context.Context, principal, bucket, prefix string, readOnly bool) (*VendedCredentials, error) {
|
|
if s3a.icebergCredentialRole == "" {
|
|
return nil, nil
|
|
}
|
|
if principal == "" {
|
|
return nil, fmt.Errorf("no principal to vend credentials for")
|
|
}
|
|
if bucket == "" {
|
|
return nil, fmt.Errorf("no bucket to scope credentials to")
|
|
}
|
|
|
|
stsService := s3a.stsService()
|
|
if stsService == nil {
|
|
return nil, fmt.Errorf("STS is not configured")
|
|
}
|
|
|
|
policy, err := tablePrefixSessionPolicy(bucket, prefix, readOnly)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
accessKey := s3_constants.AccessKeyIdFromContext(ctx)
|
|
request := &sts.AssumeRoleForPrincipalRequest{
|
|
RoleArn: s3a.icebergCredentialRole,
|
|
Principal: principal,
|
|
RoleSessionName: icebergSessionName(principal, bucket, prefix, accessKey),
|
|
ProviderName: icebergVendedCredentialsProvider,
|
|
Policy: &policy,
|
|
SourceAccessKeyId: accessKey,
|
|
}
|
|
// Bind the session to the caller's key when the caller authenticated with a
|
|
// stored identity's key — a marker its verifier records, so a caller that
|
|
// signed with an STS session key or OIDC token stays unbound even when its
|
|
// principal names a stored identity, whose session a binding would refuse
|
|
// on every request. The marker survives the key being revoked between
|
|
// authentication and this vend: the session then dies on its first request
|
|
// instead of living unbound until it expires. The binding travels under
|
|
// its own claim — SourceAccessKeyId is audit-only for compatibility with
|
|
// tokens minted before the binding existed.
|
|
if storedKey, storedSecret := s3_constants.StoredCredentialFromContext(ctx); storedKey != "" {
|
|
request.BoundAccessKeyId = storedKey
|
|
request.BoundAccessKeyFingerprint = s3_constants.StoredCredentialFingerprint(s3a.iam.stsSigningKey(), storedKey, storedSecret)
|
|
}
|
|
if s3a.icebergCredentialDuration > 0 {
|
|
duration := s3a.icebergCredentialDuration
|
|
request.DurationSeconds = &duration
|
|
}
|
|
|
|
resp, err := stsService.AssumeRoleForPrincipal(ctx, request)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if resp == nil || resp.Credentials == nil {
|
|
return nil, fmt.Errorf("STS returned no credentials")
|
|
}
|
|
|
|
return &VendedCredentials{
|
|
AccessKeyID: resp.Credentials.AccessKeyId,
|
|
SecretAccessKey: resp.Credentials.SecretAccessKey,
|
|
SessionToken: resp.Credentials.SessionToken,
|
|
Expiration: resp.Credentials.Expiration,
|
|
}, nil
|
|
}
|
|
|
|
func (s3a *S3ApiServer) stsService() *sts.STSService {
|
|
if s3a.iam == nil {
|
|
return nil
|
|
}
|
|
return s3a.iam.stsService()
|
|
}
|
|
|
|
func (iam *IdentityAccessManagement) stsService() *sts.STSService {
|
|
if iam.iamIntegration == nil {
|
|
return nil
|
|
}
|
|
provider, ok := iam.iamIntegration.(IAMManagerProvider)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
manager := provider.GetIAMManager()
|
|
if manager == nil {
|
|
return nil
|
|
}
|
|
return manager.GetSTSService()
|
|
}
|
|
|
|
func (iam *IdentityAccessManagement) stsSigningKey() []byte {
|
|
if s := iam.stsService(); s != nil && s.Config != nil {
|
|
return s.Config.SigningKey
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// tablePrefixSessionPolicy narrows a session to one table's files plus the
|
|
// bucket listing that clients need to resolve them. A read-only session drops
|
|
// the write verbs: the catalog vends no more authority than the caller holds.
|
|
func tablePrefixSessionPolicy(bucket, prefix string, readOnly bool) (string, error) {
|
|
prefix = strings.Trim(prefix, "/")
|
|
// A resource pattern is matched with wildcards, so a location carrying one
|
|
// would widen the session to sibling tables. Refuse rather than escape it:
|
|
// nothing SeaweedFS generates contains these.
|
|
if strings.ContainsAny(bucket, "*?") || strings.ContainsAny(prefix, "*?") {
|
|
return "", fmt.Errorf("refusing to scope credentials to a location with wildcards: s3://%s/%s", bucket, prefix)
|
|
}
|
|
// Without a prefix there is nothing to scope to, and the session would carry
|
|
// read and write over every table in the bucket. A table registered at the
|
|
// bucket root is the only way to get here.
|
|
if prefix == "" {
|
|
return "", fmt.Errorf("refusing to scope credentials to a whole bucket: s3://%s", bucket)
|
|
}
|
|
|
|
objectActions := []string{"s3:GetObject", "s3:PutObject", "s3:DeleteObject", "s3:AbortMultipartUpload", "s3:ListMultipartUploadParts"}
|
|
if readOnly {
|
|
objectActions = []string{"s3:GetObject"}
|
|
}
|
|
|
|
policy := map[string]interface{}{
|
|
"Version": "2012-10-17",
|
|
"Statement": []map[string]interface{}{
|
|
{
|
|
"Effect": "Allow",
|
|
"Action": objectActions,
|
|
"Resource": []string{fmt.Sprintf("arn:aws:s3:::%s/%s/*", bucket, prefix)},
|
|
},
|
|
{
|
|
// Listing is granted on the bucket, so without the condition the
|
|
// session could enumerate every other table's object names.
|
|
"Effect": "Allow",
|
|
"Action": []string{"s3:ListBucket"},
|
|
"Resource": []string{fmt.Sprintf("arn:aws:s3:::%s", bucket)},
|
|
"Condition": map[string]interface{}{
|
|
"StringLike": map[string]interface{}{
|
|
"s3:prefix": []string{prefix, prefix + "/*"},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
// GetBucketLocation carries no prefix to condition on, so it sits
|
|
// in its own statement rather than being denied by one.
|
|
// ListBucketMultipartUploads is deliberately absent: Iceberg
|
|
// writers complete and abort by upload id, and granting it either
|
|
// leaks in-flight keys bucket-wide or breaks on the same missing
|
|
// prefix.
|
|
"Effect": "Allow",
|
|
"Action": []string{"s3:GetBucketLocation"},
|
|
"Resource": []string{fmt.Sprintf("arn:aws:s3:::%s", bucket)},
|
|
},
|
|
},
|
|
}
|
|
|
|
encoded, err := json.Marshal(policy)
|
|
if err != nil {
|
|
return "", fmt.Errorf("build session policy: %w", err)
|
|
}
|
|
return string(encoded), nil
|
|
}
|
|
|
|
// icebergSessionKeyTagLength is the hex length of the access key hash that ends
|
|
// a vended session's name: 48 bits, so two keys of one identity collide with
|
|
// odds of about 2^-48.
|
|
const icebergSessionKeyTagLength = 12
|
|
|
|
// icebergSessionKeyTag is "-k" and the first icebergSessionKeyTagLength hex
|
|
// digits of the SHA-256 of the access key id, or "" without a key.
|
|
func icebergSessionKeyTag(accessKey string) string {
|
|
if accessKey == "" {
|
|
return ""
|
|
}
|
|
sum := sha256.Sum256([]byte(accessKey))
|
|
return "-k" + hex.EncodeToString(sum[:])[:icebergSessionKeyTagLength]
|
|
}
|
|
|
|
// icebergSessionName builds a session name that identifies the caller, the
|
|
// table and the caller's access key in audit logs, within the 64-character AWS
|
|
// limit. The key tag is never cut: the table part gives way to it.
|
|
func icebergSessionName(principal, bucket, prefix, accessKey string) string {
|
|
name := fmt.Sprintf("iceberg-%s-%s", principal, strings.ReplaceAll(strings.Trim(prefix, "/"), "/", "-"))
|
|
if strings.Trim(prefix, "/") == "" {
|
|
name = fmt.Sprintf("iceberg-%s-%s", principal, bucket)
|
|
}
|
|
name = strings.Map(func(r rune) rune {
|
|
switch {
|
|
case r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z', r >= '0' && r <= '9', r == '-', r == '_':
|
|
return r
|
|
default:
|
|
return '-'
|
|
}
|
|
}, name)
|
|
tag := icebergSessionKeyTag(accessKey)
|
|
if limit := 64 - len(tag); len(name) > limit {
|
|
name = name[:limit]
|
|
}
|
|
return name + tag
|
|
}
|