Skip to content

Commit b992157

Browse files
stanhuGitLab
authored andcommitted
Merge branch 'sh-pass-aws-session-token' into 'main'
Pass S3 session token for access key credentials See merge request https://gitlab.com/gitlab-org/gitlab-runner/-/merge_requests/6376 Merged-by: Stan Hu <stanhu@gmail.com> Approved-by: Axel von Bertoldi <avonbertoldi@gitlab.com>
2 parents a236a71 + 0377682 commit b992157

2 files changed

Lines changed: 42 additions & 13 deletions

File tree

cache/s3v2/s3.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ func newRawS3Client(s3Config *common.CacheS3Config) (*aws.Config, *s3.Client, er
242242
break
243243
case common.S3AuthTypeAccessKey:
244244
options = append(options,
245-
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(s3Config.AccessKey, s3Config.SecretKey, "")),
245+
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(s3Config.AccessKey, s3Config.SecretKey, s3Config.SessionToken)),
246246
)
247247
}
248248

cache/s3v2/s3_test.go

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,25 +73,43 @@ func TestNewS3ClientOptions(t *testing.T) {
7373
disableDualStack := false
7474

7575
tests := map[string]struct {
76-
s3Config common.CacheS3Config
77-
expectedRegion string
78-
expectedScheme string
79-
usePathStyle bool
80-
expectedAccelerate bool
81-
expectedDualStack bool
82-
expectedEndpoint string
76+
s3Config common.CacheS3Config
77+
expectedStaticCreds bool
78+
expectedRegion string
79+
expectedScheme string
80+
usePathStyle bool
81+
expectedAccelerate bool
82+
expectedDualStack bool
83+
expectedEndpoint string
8384
}{
8485
"s3-standard": {
8586
s3Config: common.CacheS3Config{
8687
AccessKey: "test-access-key",
8788
SecretKey: "test-secret-key",
89+
ServerAddress: "s3.amazonaws.com",
8890
BucketName: "test-bucket",
8991
BucketLocation: "us-west-2",
9092
},
91-
expectedRegion: "us-west-2",
92-
expectedScheme: "https",
93-
expectedEndpoint: "",
94-
expectedDualStack: true,
93+
expectedStaticCreds: true,
94+
expectedRegion: "us-west-2",
95+
expectedScheme: "https",
96+
expectedEndpoint: "",
97+
expectedDualStack: true,
98+
},
99+
"s3-standard-with-session-token": {
100+
s3Config: common.CacheS3Config{
101+
AccessKey: "test-access-key",
102+
SecretKey: "test-secret-key",
103+
SessionToken: "test-session-token",
104+
ServerAddress: "s3.amazonaws.com",
105+
BucketName: "test-bucket",
106+
BucketLocation: "us-west-2",
107+
},
108+
expectedStaticCreds: true,
109+
expectedRegion: "us-west-2",
110+
expectedScheme: "https",
111+
expectedEndpoint: "",
112+
expectedDualStack: true,
95113
},
96114
"s3-standard-dual-stack": {
97115
s3Config: common.CacheS3Config{
@@ -212,8 +230,19 @@ func TestNewS3ClientOptions(t *testing.T) {
212230
client, err := newS3Client(&tt.s3Config)
213231
require.NoError(t, err)
214232

215-
clientOptions := client.(*s3Client).client.Options()
233+
s3Client := client.(*s3Client).client
234+
235+
if tt.expectedStaticCreds {
236+
credsProvider := s3Client.Options().Credentials
237+
238+
creds, err := credsProvider.Retrieve(t.Context())
239+
require.NoError(t, err)
240+
require.Equal(t, tt.s3Config.AccessKey, creds.AccessKeyID)
241+
require.Equal(t, tt.s3Config.SecretKey, creds.SecretAccessKey)
242+
require.Equal(t, tt.s3Config.SessionToken, creds.SessionToken)
243+
}
216244

245+
clientOptions := s3Client.Options()
217246
require.Equal(t, tt.expectedRegion, clientOptions.Region)
218247
require.Equal(t, tt.s3Config.Accelerate, clientOptions.UseAccelerate)
219248
require.Equal(t, tt.expectedDualStack, clientOptions.UseDualstack) // nolint:staticcheck

0 commit comments

Comments
 (0)