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

storage: add profile flag #453

Merged
merged 17 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
storage: add profile flag
Fixes #353
  • Loading branch information
kucukaslan committed Jun 29, 2022
commit 44a84ff7faa58aa23f69e8aa6b2f91d6f443e43c
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ s5cmd --use-list-objects-v1 ls s3://bucket/

`s5cmd` uses official AWS SDK to access S3. SDK requires credentials to sign
requests to AWS. Credentials can be provided in a variety of ways:

- Command line options `--profile` to use a [named profile](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html) and `--no-sign-request` to send requests anonymously
- Environment variables
- AWS credentials file, including profile selection via `AWS_PROFILE` environment
variable
Expand Down
5 changes: 5 additions & 0 deletions command/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ var app = &cli.App{
Name: "request-payer",
Usage: "who pays for request (access requester pays buckets)",
},
&cli.StringFlag{
Name: "profile",
Usage: "use the specified `ProfileName` from the credential file",
igungor marked this conversation as resolved.
Show resolved Hide resolved
},
},
Before: func(c *cli.Context) error {
retryCount := c.Int("retry-count")
Expand Down Expand Up @@ -162,6 +166,7 @@ func NewStorageOpts(c *cli.Context) storage.Options {
NoVerifySSL: c.Bool("no-verify-ssl"),
RequestPayer: c.String("request-payer"),
UseListObjectsV1: c.Bool("use-list-objects-v1"),
Profile: c.String("profile"),
}
}

Expand Down
2 changes: 2 additions & 0 deletions storage/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,8 @@ func (sc *SessionCache) newSession(ctx context.Context, opts Options) (*session.
if opts.NoSignRequest {
// do not sign requests when making service API calls
awsCfg.Credentials = credentials.AnonymousCredentials
} else if opts.Profile != "" { // if they provided a profile
seruman marked this conversation as resolved.
Show resolved Hide resolved
awsCfg = awsCfg.WithCredentials(credentials.NewSharedCredentials("", opts.Profile))
}

endpointURL, err := parseEndpoint(opts.Endpoint)
Expand Down
19 changes: 19 additions & 0 deletions storage/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,25 @@ func TestNewSessionWithNoSignRequest(t *testing.T) {
}
}

func TestNewSessionWithProfile(t *testing.T) {
globalSessionCache.clear()
profileName := "default"
igungor marked this conversation as resolved.
Show resolved Hide resolved
sess, err := globalSessionCache.newSession(context.Background(), Options{
Profile: profileName,
})
if err != nil {
t.Fatal(err)
}

got, _ := sess.Config.Credentials.Get()
expected, _ := credentials.NewSharedCredentials("", profileName).Get()

if expected != got {
t.Fatalf("expected credentials does not match the credential we got! (To avoid unintended credential leaks, we don't print them here.)")
//t.Fatalf("expected %v, got %v", expected, got)
}
}

func TestS3ListURL(t *testing.T) {
url, err := url.New("s3://bucket/key")
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func NewRemoteClient(ctx context.Context, url *url.URL, opts Options) (*S3, erro
NoSignRequest: opts.NoSignRequest,
UseListObjectsV1: opts.UseListObjectsV1,
RequestPayer: opts.RequestPayer,
Profile: opts.Profile,
bucket: url.Bucket,
region: opts.region,
}
Expand All @@ -77,6 +78,7 @@ type Options struct {
NoSignRequest bool
UseListObjectsV1 bool
RequestPayer string
Profile string
bucket string
region string
}
Expand Down