Skip to content

Commit

Permalink
Merge pull request #117 from ndegory/51-bucket-location
Browse files Browse the repository at this point in the history
Environment variable for S3 bucket location
  • Loading branch information
hypnoglow authored Oct 24, 2020
2 parents d65cc1d + 359ff3b commit 508fa41
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ See [these integration tests](https://github.com/hypnoglow/helm-s3/blob/master/h

To enable S3 SSE export environment variable `AWS_S3_SSE` and set it to desired type for example `AES256`.

## S3 bucket location

The plugin will look for the bucket in the region inferred by the environment. If the bucket is in another region, the commands will fail.

This can be controlled by exporting one of HELM\_S3\_REGION, AWS\_REGION or AWS\_DEFAULT\_REGION, in order of precedence.

## Documentation

Additional documentation is available in the [docs](docs) directory. This currently includes:
Expand Down
11 changes: 11 additions & 0 deletions internal/awsutil/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const (

// awsDisableSSL can be set to true to disable SSL for AWS S3 server.
awsDisableSSL = "AWS_DISABLE_SSL"

// awsBucketLocation can be set to an AWS region to force the session region
// if AWS_DEFAULT_REGION and AWS_REGION cannot be trusted
awsBucketLocation = "HELM_S3_REGION"
)

// SessionOption is an option for session.
Expand Down Expand Up @@ -43,6 +47,13 @@ func Session(opts ...SessionOption) (*session.Session, error) {
AssumeRoleTokenProvider: StderrTokenProvider,
}

bucketRegion := os.Getenv(awsBucketLocation)
// if not set, we don't update the config,
// so that the AWS SDK can still rely on either AWS_REGION or AWS_DEFAULT_REGION
if bucketRegion != "" {
so.Config.Region = aws.String(bucketRegion)
}

for _, opt := range opts {
opt(&so)
}
Expand Down
5 changes: 5 additions & 0 deletions internal/awsutil/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
func TestSessionWithCustomEndpoint(t *testing.T) {
os.Setenv("AWS_ENDPOINT", "foobar:1234")
os.Setenv("AWS_DISABLE_SSL", "true")
os.Setenv("HELM_S3_REGION", "us-west-2")

s, err := Session()
if err != nil {
Expand All @@ -22,6 +23,10 @@ func TestSessionWithCustomEndpoint(t *testing.T) {
t.Fatalf("Expected to disable SSL")
}

if *s.Config.Region != "us-west-2" {
t.Fatalf("Expected to set us-west-2 region")
}
os.Unsetenv("AWS_ENDPOINT")
os.Unsetenv("AWS_DISABLE_SSL")
os.Unsetenv("HELM_S3_REGION")
}

0 comments on commit 508fa41

Please sign in to comment.