Skip to content

Commit 9528036

Browse files
authored
Merge pull request moby#3065 from crazy-max/s3-auth
cache(s3): handle session token for temporary credentials
2 parents 23837e5 + cc2553b commit 9528036

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,10 @@ The simplest way is to use an IAM Instance profile.
475475
Others options are:
476476

477477
* Any system using environment variables / config files supported by the [AWS Go SDK](https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html). The configuration must be available for the buildkit daemon, not for the client.
478-
* Access key ID and Secret Access Key, using the `access_key_id` and `secret_access_key` attributes.
478+
* Using the following attributes:
479+
* `access_key_id`: Access Key ID
480+
* `secret_access_key`: Secret Access Key
481+
* `session_token`: Session Token
479482

480483
`--export-cache` options:
481484
* `type=s3`

cache/remotecache/s3/s3.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const (
4040
attrEndpointURL = "endpoint_url"
4141
attrAccessKeyID = "access_key_id"
4242
attrSecretAccessKey = "secret_access_key"
43+
attrSessionToken = "session_token"
4344
attrUsePathStyle = "use_path_style"
4445
)
4546

@@ -54,6 +55,7 @@ type Config struct {
5455
EndpointURL string
5556
AccessKeyID string
5657
SecretAccessKey string
58+
SessionToken string
5759
UsePathStyle bool
5860
}
5961

@@ -108,6 +110,7 @@ func getConfig(attrs map[string]string) (Config, error) {
108110
endpointURL := attrs[attrEndpointURL]
109111
accessKeyID := attrs[attrAccessKeyID]
110112
secretAccessKey := attrs[attrSecretAccessKey]
113+
sessionToken := attrs[attrSessionToken]
111114

112115
usePathStyle := false
113116
usePathStyleStr, ok := attrs[attrUsePathStyle]
@@ -129,6 +132,7 @@ func getConfig(attrs map[string]string) (Config, error) {
129132
EndpointURL: endpointURL,
130133
AccessKeyID: accessKeyID,
131134
SecretAccessKey: secretAccessKey,
135+
SessionToken: sessionToken,
132136
UsePathStyle: usePathStyle,
133137
}, nil
134138
}
@@ -200,14 +204,13 @@ func (e *exporter) Finalize(ctx context.Context) (map[string]string, error) {
200204
}
201205
} else {
202206
layerDone := progress.OneOff(ctx, fmt.Sprintf("writing layer %s", l.Blob))
203-
bytes, err := content.ReadBlob(ctx, dgstPair.Provider, dgstPair.Descriptor)
207+
dt, err := content.ReadBlob(ctx, dgstPair.Provider, dgstPair.Descriptor)
204208
if err != nil {
205209
return nil, layerDone(err)
206210
}
207-
if err := e.s3Client.saveMutable(ctx, key, bytes); err != nil {
211+
if err := e.s3Client.saveMutable(ctx, key, dt); err != nil {
208212
return nil, layerDone(errors.Wrap(err, "error writing layer blob"))
209213
}
210-
211214
layerDone(nil)
212215
}
213216

@@ -352,7 +355,7 @@ func newS3Client(ctx context.Context, config Config) (*s3Client, error) {
352355
}
353356
client := s3.NewFromConfig(cfg, func(options *s3.Options) {
354357
if config.AccessKeyID != "" && config.SecretAccessKey != "" {
355-
options.Credentials = credentials.NewStaticCredentialsProvider(config.AccessKeyID, config.SecretAccessKey, "")
358+
options.Credentials = credentials.NewStaticCredentialsProvider(config.AccessKeyID, config.SecretAccessKey, config.SessionToken)
356359
}
357360
if config.EndpointURL != "" {
358361
options.UsePathStyle = config.UsePathStyle
@@ -435,15 +438,15 @@ func (s3Client *s3Client) exists(ctx context.Context, key string) (*time.Time, e
435438

436439
func (s3Client *s3Client) touch(ctx context.Context, key string) error {
437440
copySource := fmt.Sprintf("%s/%s", s3Client.bucket, key)
438-
copy := &s3.CopyObjectInput{
441+
cp := &s3.CopyObjectInput{
439442
Bucket: &s3Client.bucket,
440443
CopySource: &copySource,
441444
Key: &key,
442445
Metadata: map[string]string{"updated_at": time.Now().String()},
443446
MetadataDirective: "REPLACE",
444447
}
445448

446-
_, err := s3Client.CopyObject(ctx, copy)
449+
_, err := s3Client.CopyObject(ctx, cp)
447450

448451
return err
449452
}
@@ -464,6 +467,6 @@ func (s3Client *s3Client) blobKey(dgst digest.Digest) string {
464467
}
465468

466469
func isNotFound(err error) bool {
467-
var error smithy.APIError
468-
return errors.As(err, &error) && (error.ErrorCode() == "NoSuchKey" || error.ErrorCode() == "NotFound")
470+
var errapi smithy.APIError
471+
return errors.As(err, &errapi) && (errapi.ErrorCode() == "NoSuchKey" || errapi.ErrorCode() == "NotFound")
469472
}

0 commit comments

Comments
 (0)