Files
seaweedfs/weed/remote_storage/s3/workload_credentials.go
T
800d80ed51 remote/s3: WEED_REMOTE_S3_CREDENTIALS=workload, the workload's identity only (#12939)
* remote/s3: WEED_REMOTE_S3_CREDENTIALS=workload, the workload's identity only

A remote of type s3 with no access key and no secret takes its credentials
from the SDK default chain, which looks at AWS_ACCESS_KEY_ID and
AWS_SECRET_ACCESS_KEY first. A SeaweedFS process often holds keys of its own
there: `weed mini` and `weed server -s3` read their initial S3 identity from
those variables. On EC2, a remote mounted on an Amazon S3 bucket with the
instance's role (no keys) therefore signs with the server's local identity,
and every call answers InvalidAccessKeyId, while a filer.remote.sync of the
same remote, started without those variables, works with the role.

WEED_REMOTE_S3_CREDENTIALS=workload makes such a remote take the AWS
workload's identity alone: web identity when AWS_WEB_IDENTITY_TOKEN_FILE and
AWS_ROLE_ARN name one (EKS IRSA), then the ECS/EKS container credentials
endpoint or the EC2 instance metadata service, on an unguarded HTTP client as
the default-chain path already does. Environment keys and shared credentials
files are never read. Unset, nothing changes. Keys in the configuration win;
half a key pair and an unknown value are refused; other S3-compatible types
are not affected. The volume server's client cache key includes the variables
that choose the source.

* remote/s3: a configured web identity has no instance-role fallback

With AWS_WEB_IDENTITY_TOKEN_FILE and AWS_ROLE_ARN both set, a failed token
read or role exchange fell through the chain to the container endpoint or
IMDS, so requests were signed with the node's role.

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

* remote/s3: the workload credential session reads no shared files

SharedConfigDisable still loads the shared credentials file, so a selected
AWS_PROFILE whose role_arn lives there failed every workload client build.

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

* remote/s3: exchange the web identity in the workload's region

The credential session took the bucket's region, so with regional STS a pod
whose only reachable STS endpoint is its own region could not sign for a
bucket in another one. AWS_REGION/AWS_DEFAULT_REGION win; the bucket's
region remains the fallback.

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

* volume: a container authorization token change rebuilds the guarded client

The container credential provider captures AWS_CONTAINER_AUTHORIZATION_TOKEN
and its _FILE path when it is built.

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

* remote/s3: the workload's web identity uses the regional STS endpoint

SDK v1 still defaults to the legacy global endpoint for most regions, so a
pod that reaches only its regional STS endpoint got no credentials. An
explicit AWS_STS_REGIONAL_ENDPOINTS keeps its say.

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

* volume: a workload region or STS endpoint mode change rebuilds the guarded client

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

* remote/s3: a workload-mode storage session reads no shared files either

A selected profile the SDK rejects still failed the storage session even
though it carries the workload's credentials.

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

* volume: hash AWS_ROLE_SESSION_NAME into the guarded credentials version

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

* volume: an IMDS endpoint or mode change rebuilds the guarded client

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

* volume: a keyed s3 remote's guarded version follows the credentials mode too

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

* volume: hash every AWS_* variable into a default-chain remote's guarded version

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

---------

Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 15:28:02 -08:00

94 lines
4.2 KiB
Go

package s3
import (
"fmt"
"net/http"
"os"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/defaults"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/seaweedfs/seaweedfs/weed/util"
)
// WEED_REMOTE_S3_CREDENTIALS chooses where a remote of type "s3" with no
// access key and no secret in its configuration takes its credentials from:
//
// - unset (the default): as before, the SDK default chain when the
// environment names a credential source, else unsigned requests;
// - "workload": the identity of the AWS workload only (web identity, the
// ECS/EKS container credentials endpoint, the EC2 instance role), never
// AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY or a shared credentials file.
// Every keyless "s3" remote of the process is signed with it, a public
// bucket's too: set it only where the process runs as an AWS workload.
//
// The other S3-compatible types (wasabi, b2, filebase, storj, ...) are not
// affected. A remote with only one of the two keys is refused in workload
// mode: falling back to the default chain would sign with the environment's
// keys, which is what the setting rules out.
//
// "workload" exists because a SeaweedFS process often holds keys of its own in
// those variables: `weed mini` and `weed server -s3` read their initial S3
// identity from AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY. On EC2, a remote
// mounted on an Amazon S3 bucket with the instance's role (no keys) then signed
// with that local identity and every call answered InvalidAccessKeyId, while a
// filer.remote.sync of the same remote, started without those variables,
// worked.
const workloadCredentialsEnv = "WEED_REMOTE_S3_CREDENTIALS"
// workloadCredentialsMode reports whether workload mode is on; an unknown
// value is an error, so a misspelling never falls back to the environment's
// keys without a word.
func workloadCredentialsMode() (bool, error) {
switch v := strings.TrimSpace(os.Getenv(workloadCredentialsEnv)); v {
case "":
return false, nil
case "workload":
return true, nil
default:
return false, fmt.Errorf("%s=%q: the only value is \"workload\" (or unset)", workloadCredentialsEnv, v)
}
}
// workloadCredentials is the workload's identity alone: web identity when the
// environment names a token file and a role (EKS IRSA), else the container
// credentials endpoint or the EC2 instance metadata service. STS is asked at
// the regional endpoint (unless AWS_STS_REGIONAL_ENDPOINTS says otherwise) of
// the workload's own region, the bucket's only when the environment names
// none. The session it builds on carries anonymous credentials so that it
// resolves none of its own (a token file without a role would otherwise fail
// every build), and it fetches on an unguarded HTTP client: the guarded one is
// for the storage endpoint and refuses the link-local credential endpoints.
func workloadCredentials(region string) (*credentials.Credentials, error) {
cfg := aws.Config{Region: aws.String(util.Nvl(os.Getenv("AWS_REGION"), os.Getenv("AWS_DEFAULT_REGION"), region)), HTTPClient: &http.Client{},
Credentials: credentials.AnonymousCredentials}
if os.Getenv("AWS_STS_REGIONAL_ENDPOINTS") == "" {
cfg.STSRegionalEndpoint = endpoints.RegionalSTSEndpoint
}
sess, err := session.NewSessionWithOptions(session.Options{
Config: cfg,
SharedConfigState: session.SharedConfigDisable,
SharedConfigFiles: []string{},
})
if err != nil {
return nil, err
}
if tokenFile, role := os.Getenv("AWS_WEB_IDENTITY_TOKEN_FILE"), os.Getenv("AWS_ROLE_ARN"); tokenFile != "" && role != "" {
name := os.Getenv("AWS_ROLE_SESSION_NAME")
if name == "" {
name = "seaweedfs-remote"
}
// no fallback: a failed exchange must not sign with the node's role
return credentials.NewCredentials(stscreds.NewWebIdentityRoleProviderWithOptions(
sts.New(sess), role, name, stscreds.FetchTokenPath(tokenFile),
func(p *stscreds.WebIdentityRoleProvider) { p.ExpiryWindow = 5 * time.Minute })), nil
}
return credentials.NewCredentials(defaults.RemoteCredProvider(*sess.Config, sess.Handlers)), nil
}