From 011c61b2547e9db988262f9ef12c64e981c42705 Mon Sep 17 00:00:00 2001 From: Daniel Valdivia <18384552+dvaldivia@users.noreply.github.com> Date: Sun, 5 Nov 2023 20:19:23 -0800 Subject: [PATCH] Add support for STS credentials (#4744) --- cmd/client-s3.go | 53 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/cmd/client-s3.go b/cmd/client-s3.go index 57e4d272da..8e79349c0b 100644 --- a/cmd/client-s3.go +++ b/cmd/client-s3.go @@ -38,6 +38,8 @@ import ( "sync" "time" + "github.com/minio/pkg/v2/env" + "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/credentials" "github.com/minio/minio-go/v7/pkg/encrypt" @@ -158,12 +160,6 @@ func newFactory() func(config *Config) (Client, *probe.Error) { var api *minio.Client var found bool if api, found = clientCache[confSum]; !found { - // if Signature version '4' use NewV4 directly. - creds := credentials.NewStaticV4(config.AccessKey, config.SecretKey, config.SessionToken) - // if Signature version '2' use NewV2 directly. - if strings.ToUpper(config.Signature) == "S3V2" { - creds = credentials.NewStaticV2(config.AccessKey, config.SecretKey, "") - } var transport http.RoundTripper @@ -223,6 +219,51 @@ func newFactory() func(config *Config) (Client, *probe.Error) { } } + var credsChain []credentials.Provider + + // if an STS endpoint is set, we will add that to the chain + if stsEndpoint := env.Get("MC_STS_ENDPOINT", ""); stsEndpoint != "" { + // set AWS_WEB_IDENTITY_TOKEN_FILE is MC_WEB_IDENTITY_TOKEN_FILE is set + if val := env.Get("MC_WEB_IDENTITY_TOKEN_FILE", ""); val != "" { + os.Setenv("AWS_WEB_IDENTITY_TOKEN_FILE", val) + } + + stsEndpointURL, err := url.Parse(stsEndpoint) + if err != nil { + return nil, probe.NewError(fmt.Errorf("Error parsing sts endpoint: %v", err)) + } + credsSts := &credentials.IAM{ + Client: &http.Client{ + Transport: transport, + }, + Endpoint: stsEndpointURL.String(), + } + credsChain = append(credsChain, credsSts) + } + + // V4 Credentials + credsV4 := &credentials.Static{ + Value: credentials.Value{ + AccessKeyID: config.AccessKey, + SecretAccessKey: config.SecretKey, + SessionToken: config.SessionToken, + SignerType: credentials.SignatureV4, + }, + } + credsChain = append(credsChain, credsV4) + // V2 Credentials + credsV2 := &credentials.Static{ + Value: credentials.Value{ + AccessKeyID: config.AccessKey, + SecretAccessKey: config.SecretKey, + SessionToken: "", + SignerType: credentials.SignatureV2, + }, + } + credsChain = append(credsChain, credsV2) + + creds := credentials.NewChainCredentials(credsChain) + // Not found. Instantiate a new MinIO var e error