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

s3: Force setting region always in AWS session #333

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
16 changes: 11 additions & 5 deletions get_s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (g *S3Getter) getObject(ctx context.Context, client *s3.S3, dst, bucket, ke
return copyReader(dst, resp.Body, 0666, g.client.umask())
}

func (g *S3Getter) getAWSConfig(region string, url *url.URL, creds *credentials.Credentials) *aws.Config {
func (g *S3Getter) getAWSConfig(url *url.URL, creds *credentials.Credentials) *aws.Config {
conf := &aws.Config{}
metadataURLOverride := os.Getenv("AWS_METADATA_URL")
if creds == nil && metadataURLOverride != "" {
Expand All @@ -201,9 +201,6 @@ func (g *S3Getter) getAWSConfig(region string, url *url.URL, creds *credentials.
}

conf.Credentials = creds
if region != "" {
conf.Region = aws.String(region)
}

return conf.WithCredentialsChainVerboseErrors(true)
}
Expand Down Expand Up @@ -302,9 +299,18 @@ func (g *S3Getter) newS3Client(
return nil, err
}
} else {
config := g.getAWSConfig(region, url, creds)
config := g.getAWSConfig(url, creds)
sess = session.New(config)
}

// When using an AWS_PROFILE, the region is coming from your ~/.aws/config
// This may not exist in your profile, and it also may differ from the bucket
// location, so we need to override and explicitly bind what region we're
// intending based on the bucket location otherwise it's possible to get
// a MissingRegion error.
if region != "" {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to make this change since I didn't look that deep, but I think we can avoid this check entirely since afaict, this value will always be something. Any detection of region that gets passed in here comes through at least with a fallback of us-east-1 when there's no region defined in the url itself. But I kept it since it existed in getAWSConfig before.

sess.Config.Region = aws.String(region)
}

return s3.New(sess), nil
}