Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support session tokens in S3 client #596

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type S3ClientOpts struct {
Transport http.RoundTripper
AccessKey string
SecretKey string
SessionToken string
Trace bool
RoleARN string
RoleSessionName string
Expand Down Expand Up @@ -136,8 +137,13 @@ func GetAssumeRoleCredentials(opts S3ClientOpts) (*credentials.Credentials, erro

func GetCredentials(opts S3ClientOpts) (*credentials.Credentials, error) {
if opts.AccessKey != "" && opts.SecretKey != "" {
log.WithField("endpoint", opts.Endpoint).Info("Creating minio client using static credentials")
return credentials.NewStaticV4(opts.AccessKey, opts.SecretKey, ""), nil
if opts.SessionToken != "" {
log.WithField("endpoint", opts.Endpoint).Info("Creating minio client using ephemeral credentials")
return credentials.NewStaticV4(opts.AccessKey, opts.SecretKey, opts.SessionToken), nil
} else {
log.WithField("endpoint", opts.Endpoint).Info("Creating minio client using static credentials")
return credentials.NewStaticV4(opts.AccessKey, opts.SecretKey, ""), nil
}
} else if opts.RoleARN != "" {
log.WithField("roleArn", opts.RoleARN).Info("Creating minio client using assumed-role credentials")
return GetAssumeRoleCredentials(opts)
Expand Down
25 changes: 23 additions & 2 deletions s3/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/assert"
)

// TestNewS3Client tests the s3 construtor
// TestNewS3Client tests the s3 constructor
func TestNewS3Client(t *testing.T) {
opts := S3ClientOpts{
Endpoint: "foo.com",
Expand All @@ -17,6 +17,7 @@ func TestNewS3Client(t *testing.T) {
Transport: http.DefaultTransport,
AccessKey: "key",
SecretKey: "secret",
SessionToken: "",
Trace: true,
RoleARN: "",
RoleSessionName: "",
Expand All @@ -31,14 +32,34 @@ func TestNewS3Client(t *testing.T) {
assert.Equal(t, opts.Secure, s3cli.Secure)
assert.Equal(t, opts.Transport, s3cli.Transport)
assert.Equal(t, opts.AccessKey, s3cli.AccessKey)
assert.Equal(t, opts.SessionToken, s3cli.SessionToken)
assert.Equal(t, opts.Trace, s3cli.Trace)
assert.Equal(t, opts.EncryptOpts, s3cli.EncryptOpts)
assert.Equal(t, opts.AddressingStyle, s3cli.AddressingStyle)
// s3cli.minioClient.
// s3client.minioClient
}

// TestNewS3Client tests the s3 construtor
// TestNewS3Client tests the S3 constructor using ephemeral credentials
func TestNewS3ClientEphemeral(t *testing.T) {
opts := S3ClientOpts{
Endpoint: "foo.com",
Region: "us-south-3",
AccessKey: "key",
SecretKey: "secret",
SessionToken: "sessionToken",
}
s3If, err := NewS3Client(context.Background(), opts)
assert.NoError(t, err)
s3cli := s3If.(*s3client)
assert.Equal(t, opts.Endpoint, s3cli.Endpoint)
assert.Equal(t, opts.Region, s3cli.Region)
assert.Equal(t, opts.AccessKey, s3cli.AccessKey)
assert.Equal(t, opts.SecretKey, s3cli.SecretKey)
assert.Equal(t, opts.SessionToken, s3cli.SessionToken)
}

// TestNewS3Client tests the s3 constructor
func TestNewS3ClientWithDiff(t *testing.T) {
t.Run("IAMRole", func(t *testing.T) {
opts := S3ClientOpts{
Expand Down
Loading