Skip to content

Commit cbc170b

Browse files
feat: [CI-14214]: Add PLUGIN_USER_ROLE_EXTERNAL_ID to pass external ID for the secondary role when required (#122)
* feat: [CI-14214]: Add PLUGIN_USER_ROLE_EXTERNAL_ID to CACHE * feat: [CI-14214]: Add PLUGIN_USER_ROLE_EXTERNAL_ID to CACHE trigger * feat: [CI-14214]: Add PLUGIN_USER_ROLE_EXTERNAL_ID to CACHE trigger #2 * feat: [CI-14214]: Add PLUGIN_USER_ROLE_EXTERNAL_ID to CACHE main.go * feat: [CI-14214]: Add PLUGIN_USER_ROLE_EXTERNAL_ID to CACHE main.go * feat: [CI-14214]: Add PLUGIN_USER_ROLE_EXTERNAL_ID to CACHE s3.go * feat: [CI-14214]: Add PLUGIN_USER_ROLE_EXTERNAL_ID to CACHE s3.go * feat: [CI-14214]: Add PLUGIN_USER_ROLE_EXTERNAL_ID to CACHE s3.go logging * feat: [CI-14214]: Add PLUGIN_USER_ROLE_EXTERNAL_ID to CACHE s3.go client setup * feat: [CI-14214]: s3.go client setup * feat: [CI-14214]: Updating tests. * feat: [CI-14214]: Updating tests. * feat: [CI-14214]: Updating tests. * feat: [CI-14214]: Updating userrolearn in test file. * feat: [CI-14214]: Updating userrolearn in test file. * feat: [CI-14214]: Updating userrolearn in test file. * feat: [CI-14214]: Updating userrolearn in test file. * feat: [CI-14214]: Updating userrolearn in test file. #5 * feat: [CI-14214]: Refactoring to make tests work. * feat: [CI-14214]: Refactoring to make tests work. * feat: [CI-14214]: Refactoring to make tests work. - 3 * feat: [CI-14214]: Refactoring to make tests work. - 4 * feat: [CI-14214]: Reverting all code to previous state to check if tests were working correctly. * feat: [CI-14214]: Reverting all code to previous state to check if tests were working correctly. - 2 * feat: [CI-14214]: Refactoring to make tests work. - 5 * feat: [CI-14214]: Refactoring to make tests work. - 6 * feat: [CI-14214]: Refactoring to make tests work. - 7 * feat: [CI-14214]: Refactoring to make tests work. - 8 * feat: [CI-14214]: Refactoring to make tests work. - 9 * feat: [CI-14214]: Refactoring to make tests work. - 10 * Refactoring to make tests work. - 11 * feat: [CI-14214] - Refactoring S3 backend code.. - 12 * feat: [CI-14214] - Refactoring S3 backend codeto make tests work. - 13 * feat: [CI-14214] - Refactoring S3 backend codeto make tests work. - 14 * feat: [CI-14214] - Refactoring S3 backend codeto make tests work. - 15 * feat: [CI-14214] - Refactoring S3 backend codeto make tests work. - 16 * feat: [CI-14214] - Refactoring S3 backend codeto make tests work. - 17 * feat: [CI-14214] - Refactoring S3 backend codeto make tests work. - 18 * feat: [CI-14214] - Refactoring S3 backend codeto make tests work. - 19 * feat: [CI-14214] - Refactoring S3 backend codeto make tests work. - 20 * feat: [CI-14214] - Refactoring S3 backend codeto make tests work. - 21 * feat: [CI-14214] - Refactoring S3 backend codeto make tests work. - 21 * feat: [CI-14214] - Refactoring S3 backend code to make tests work. - 22 * feat: [CI-14214] - Refactoring S3 backend code to make tests work. - 23 * feat: [CI-14214] - Refactoring S3 backend code to make tests work. - 24 * feat: [CI-14214] - Refactoring S3_test backend code to make tests work. - 25 * feat: [CI-14214] - Refactoring S3_test. - 26 * feat: [CI-14214] - Refactoring S3_test. - 27 * feat: [CI-14214] - Refactoring S3_test. - 28 * feat: [CI-14214] - Refactoring S3_test. - 29 * feat: [CI-14214] - Refactoring S3_test. - 30 * feat: [CI-14214] - Refactoring S3_test. - 31 * feat: [CI-14214] - Refactoring S3_test. - 32 * feat: [CI-14214] - Refactoring S3_test. - 33 * feat: [CI-14214] - Refactoring S3_test. - 34 * feat: [CI-14214] - Refactoring S3_test. - 35 * feat: [CI-14214] - Refactoring S3_test. - 36 * feat: [CI-14214] - Refactoring S3_test. - 37 * feat: [CI-14214] - Refactoring S3_test. - 38 * feat: [CI-14214] - Refactoring S3_test. - 39 * feat: [CI-14214] - Refactoring S3_test. - 40 * feat: [CI-14214] - Refactoring S3_test. - 41 * feat: [CI-14214] - Refactoring S3cache. - 42 * feat: [CI-14214] - Refactoring S3cache. - 43 * feat: [CI-14214] - Refactoring S3cache. - 44 * feat: [CI-14214] - Refactoring S3cache. - 45 * feat: [CI-14214] - Refactoring S3cache. - 46 * feat: [CI-14214] - Refactoring S3cache. - 47 * feat: [CI-14214] - Refactoring S3cache. - 48 * feat: [CI-14214] - Refactoring S3cache. - 49 * feat: [CI-14214] - Refactoring S3cache. - 50 * feat: [CI-14214] - Final Refactoring. - 1 * feat: [CI-14214] - Final Refactoring. - 2 * feat: [CI-14214] - Final Refactoring. - 3 * Update storage/backend/s3/s3.go Removing loggers. Co-authored-by: OP (oppenheimer) <[email protected]> * Update storage/backend/s3/s3.go Removing loggers. Co-authored-by: OP (oppenheimer) <[email protected]> * Update storage/backend/s3/s3.go Removing loggers. Co-authored-by: OP (oppenheimer) <[email protected]> * Update storage/backend/s3/s3.go Changing warning message. Co-authored-by: OP (oppenheimer) <[email protected]> * Updating README * Remove excessive logging. * Update README.md * Remove redundant files. --------- Co-authored-by: OP (oppenheimer) <[email protected]>
1 parent 88a2782 commit cbc170b

File tree

8 files changed

+164
-42
lines changed

8 files changed

+164
-42
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,5 @@ release/
3737
# Azurite temp files
3838
__*__
3939
__*__.json
40+
41+
update_script.sh

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,28 @@ Targets:
255255
help Shows this help message
256256
```
257257

258+
## Configuration Variables for Secondary Role Assumption with External ID
259+
260+
The following environment variables enable the plugin to assume a secondary IAM role using IRSA, with an External ID if required by the role’s trust policy.
261+
262+
### Variables
263+
264+
#### `PLUGIN_USER_ROLE_ARN`
265+
266+
- **Type**: String
267+
- **Required**: No
268+
- **Description**: Specifies the secondary IAM role to be assumed by the plugin, allowing it to inherit permissions associated with this role and access specific AWS resources.
269+
270+
#### `PLUGIN_USER_ROLE_EXTERNAL_ID`
271+
272+
- **Type**: String
273+
- **Required**: No
274+
- **Description**: Provide the External ID necessary for the role assumption process if the secondary role’s trust policy mandates it. This is often required for added security, ensuring that only authorized entities assume the role.
275+
276+
### Usage Notes
277+
278+
- If the role secondary role (`PLUGIN_USER_ROLE_ARN`) requires an External ID then pass it through `PLUGIN_USER_ROLE_EXTERNAL_ID`.
279+
258280
## Releases
259281

260282
Release management handled by the CI pipeline. When you create a tag on `master` branch, CI handles the rest.
@@ -299,3 +321,4 @@ One bigger area of future investment is to add a couple of [new storage backends
299321
## License and Copyright
300322

301323
This project is licensed under the [Apache License 2.0](LICENSE).
324+
.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/google/go-cmp v0.5.9
1111
github.com/klauspost/compress v1.16.3
1212
github.com/pkg/sftp v1.13.5
13+
github.com/sirupsen/logrus v1.9.3
1314
github.com/urfave/cli/v2 v2.25.0
1415
golang.org/x/crypto v0.14.0
1516
golang.org/x/oauth2 v0.6.0

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
114114
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
115115
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
116116
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
117+
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
118+
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
117119
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
118120
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
119121
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
@@ -166,6 +168,7 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
166168
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
167169
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
168170
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
171+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
169172
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
170173
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
171174
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=

main.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,10 +420,15 @@ func main() {
420420
EnvVars: []string{"PLUGIN_USER_ROLE_ARN", "AWS_USER_ROLE_ARN"},
421421
},
422422
&cli.StringFlag{
423-
Name: "external-id",
424-
Usage: "external ID to use when assuming role",
423+
Name: "external-id",
424+
Usage: "external ID to use when assuming role",
425425
EnvVars: []string{"PLUGIN_EXTERNAL_ID"},
426426
},
427+
&cli.StringFlag{
428+
Name: "user-role-external-id",
429+
Usage: "external ID to use when assuming secondary role",
430+
EnvVars: []string{"PLUGIN_USER_ROLE_EXTERNAL_ID"},
431+
},
427432

428433
// GCS specific Configs flags
429434

@@ -561,7 +566,7 @@ func run(c *cli.Context) error {
561566
logLevel = internal.LogLevelDebug
562567
}
563568

564-
logger := internal.NewLogger(logLevel, c.String("log.format"), "drone-cache")
569+
logger := internal.NewLogger(logLevel, c.String("log.format"), "drone-cache-logger")
565570
level.Debug(logger).Log("version", version, "commit", commit, "date", date)
566571

567572
plg := plugin.New(log.With(logger, "component", "plugin"))
@@ -637,6 +642,7 @@ func run(c *cli.Context) error {
637642
UserRoleArn: c.String("user-role-arn"),
638643
OIDCTokenID: c.String("oidc-token-id"),
639644
ExternalID: c.String("external-id"),
645+
UserRoleExternalID: c.String("user-role-external-id"),
640646
},
641647
Azure: azure.Config{
642648
AccountName: c.String("azure.account-name"),

storage/backend/s3/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package s3
22

3+
import "github.com/aws/aws-sdk-go/aws/credentials"
4+
35
// Config is a structure to store S3 backend configuration.
46
type Config struct {
57
// Indicates the files ACL, which should be one,
@@ -21,6 +23,9 @@ type Config struct {
2123
UserRoleArn string
2224
OIDCTokenID string
2325
ExternalID string
26+
UserRoleExternalID string
27+
28+
Credentials *credentials.Credentials
2429

2530
// us-east-1
2631
// us-west-1

storage/backend/s3/s3.go

Lines changed: 62 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ import (
1616
"github.com/aws/aws-sdk-go/service/s3/s3manager"
1717
"github.com/aws/aws-sdk-go/service/sts"
1818
"github.com/go-kit/kit/log"
19-
"github.com/go-kit/kit/log/level"
19+
"github.com/sirupsen/logrus"
2020

2121
"github.com/meltwater/drone-cache/internal"
2222
"github.com/meltwater/drone-cache/storage/common"
2323
)
2424

25-
// Backend implements storage.Backend for AWs S3.
25+
// Backend implements storage.Backend for AWS S3.
2626
type Backend struct {
2727
logger log.Logger
2828

@@ -32,52 +32,76 @@ type Backend struct {
3232
client *s3.S3
3333
}
3434

35-
// New creates an S3 backend.
35+
// New creates a new S3 backend with lazy-loaded credentials.
3636
func New(l log.Logger, c Config, debug bool) (*Backend, error) {
37+
3738
conf := &aws.Config{
3839
Region: aws.String(c.Region),
3940
Endpoint: &c.Endpoint,
4041
DisableSSL: aws.Bool(strings.HasPrefix(c.Endpoint, "http://")),
4142
S3ForcePathStyle: aws.Bool(c.PathStyle),
4243
}
4344

44-
if c.Key != "" && c.Secret != "" { // nolint:gocritic
45+
// Create the initial session
46+
sess, err := session.NewSession(conf)
47+
if err != nil {
48+
logrus.WithError(err).Error("Could not instantiate AWS session")
49+
return nil, fmt.Errorf("AWS session creation failed: %w", err)
50+
}
51+
// Inject credentials after session creation
52+
if c.Key != "" && c.Secret != "" {
53+
logrus.Info("Using static credentials (Key/Secret provided)")
4554
conf.Credentials = credentials.NewStaticCredentials(c.Key, c.Secret, "")
4655
} else if c.AssumeRoleARN != "" {
56+
// Use OIDC Token or assume role
4757
if c.OIDCTokenID != "" {
48-
// Assume role with OIDC
58+
logrus.Info("Attempting to assume role with OIDC")
4959
creds, err := assumeRoleWithWebIdentity(c.AssumeRoleARN, c.AssumeRoleSessionName, c.OIDCTokenID)
5060
if err != nil {
51-
level.Error(l).Log("msg", "failed to assume role with OIDC", "error", err)
61+
logrus.WithError(err).Error("Failed to assume role with OIDC")
5262
return nil, err
5363
}
5464
conf.Credentials = creds
65+
logrus.Info("Successfully assumed role with OIDC")
5566
} else {
5667
conf.Credentials = assumeRole(c.AssumeRoleARN, c.AssumeRoleSessionName, c.ExternalID)
5768
}
5869
} else {
59-
level.Warn(l).Log("msg", "aws key and/or Secret not provided (falling back to anonymous credentials)")
60-
}
61-
62-
sess, err := session.NewSession(conf)
63-
if err != nil {
64-
level.Warn(l).Log("msg", "could not instantiate session", "error", err)
65-
return nil, err
70+
logrus.Warn("No AWS credentials provided or role assumed; using default machine credentials for AWS requests")
6671
}
6772

6873
var client *s3.S3
69-
// If user role ARN is set then assume role here
74+
75+
// If UserRoleArn is set, create a new session and assume the role
7076
if len(c.UserRoleArn) > 0 {
71-
confRoleArn := aws.Config{
72-
Region: aws.String(c.Region),
73-
Credentials: stscreds.NewCredentials(sess, c.UserRoleArn),
74-
}
77+
logrus.WithFields(logrus.Fields{
78+
"UserRoleArn": c.UserRoleArn,
79+
"UserRoleExternalID": c.UserRoleExternalID,
80+
}).Info("Setting up credentials with UserRoleArn")
81+
82+
creds := stscreds.NewCredentials(sess, c.UserRoleArn, func(provider *stscreds.AssumeRoleProvider) {
83+
if c.UserRoleExternalID != "" {
84+
logrus.WithField("ExternalID", c.UserRoleExternalID).Info("Setting up creds with UserRoleExternalID")
85+
provider.ExternalID = aws.String(c.UserRoleExternalID)
86+
}
87+
})
88+
89+
// Update the config with the assumed role credentials, reuse the session
90+
conf.Credentials = creds
91+
client = s3.New(sess, conf)
92+
logrus.Info("Created S3 client with assumed user role")
93+
// }
7594

76-
client = s3.New(sess, &confRoleArn)
7795
} else {
78-
client = s3.New(sess)
96+
// Use the original session for the S3 client if no UserRoleArn is set
97+
client = s3.New(sess, conf)
98+
logrus.Info("Created S3 client with default session")
7999
}
80100

101+
logrus.WithFields(logrus.Fields{
102+
"Client": client,
103+
}).Info("New Client set here.")
104+
81105
backend := &Backend{
82106
logger: l,
83107
bucket: c.Bucket,
@@ -89,6 +113,7 @@ func New(l log.Logger, c Config, debug bool) (*Backend, error) {
89113
backend.acl = c.ACL
90114
}
91115

116+
92117
return backend, nil
93118
}
94119

@@ -193,22 +218,30 @@ func (b *Backend) List(ctx context.Context, p string) ([]common.FileEntry, error
193218
return entries, err
194219
}
195220

196-
func assumeRole(roleArn, roleSessionName string, externalID string) *credentials.Credentials {
197-
sess, _ := session.NewSession()
198-
client := sts.New(sess) // nolint:staticcheck
199-
duration := time.Hour * 1
200-
stsProvider := &stscreds.AssumeRoleProvider{
201-
Client: client,
202-
Duration: duration,
221+
// AssumeRole logic
222+
func assumeRole(roleArn, roleSessionName, externalID string) *credentials.Credentials {
223+
224+
sess, err := session.NewSession()
225+
if err != nil {
226+
logrus.WithError(err).Error("Failed to create session for role assumption")
227+
return nil
228+
}
229+
230+
stsClient := sts.New(sess)
231+
roleProvider := &stscreds.AssumeRoleProvider{
232+
Client: stsClient,
203233
RoleARN: roleArn,
204234
RoleSessionName: roleSessionName,
235+
Duration: time.Hour, // 1-hour session
205236
}
206237

207238
if externalID != "" {
208-
stsProvider.ExternalID = &externalID
239+
roleProvider.ExternalID = aws.String(externalID)
209240
}
241+
242+
creds := credentials.NewCredentials(roleProvider)
210243

211-
return credentials.NewCredentials(stsProvider)
244+
return creds
212245
}
213246

214247
func assumeRoleWithWebIdentity(roleArn, roleSessionName, webIdentityToken string) (*credentials.Credentials, error) {

storage/backend/s3/s3_test.go

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ import (
1313

1414
"github.com/aws/aws-sdk-go/aws"
1515
"github.com/aws/aws-sdk-go/aws/credentials"
16+
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
1617
"github.com/aws/aws-sdk-go/aws/session"
1718
"github.com/aws/aws-sdk-go/service/s3"
1819
"github.com/go-kit/kit/log"
20+
"github.com/sirupsen/logrus"
1921

2022
"github.com/meltwater/drone-cache/test"
2123
)
@@ -55,22 +57,52 @@ func TestRoundTrip(t *testing.T) {
5557
roundTrip(t, backend)
5658
}
5759

58-
func TestRoundTripWithAssumeRole(t *testing.T) {
60+
func TestRoundTripWithAssumeRoleAndExternalID(t *testing.T) {
5961
t.Parallel()
6062

63+
// Log the credentials being used for the test (without exposing secrets)
64+
logrus.WithFields(logrus.Fields{
65+
"RoleARN": "arn:aws:iam::account-id:role/TestRole",
66+
}).Info("Setting up AssumeRole test")
67+
68+
// Setting up the base session with static credentials
69+
baseSess, err := session.NewSession(&aws.Config{
70+
Region: aws.String(defaultRegion),
71+
Endpoint: aws.String(endpoint),
72+
DisableSSL: aws.Bool(true),
73+
Credentials: credentials.NewStaticCredentials(accessKey, secretAccessKey, ""), // Use static credentials for the base session
74+
})
75+
if err != nil {
76+
t.Fatalf("failed to create base session: %v", err)
77+
}
78+
79+
// Use stscreds.NewCredentials for assuming the role
80+
creds := stscreds.NewCredentials(baseSess, "arn:aws:iam::account-id:role/TestRole", func(p *stscreds.AssumeRoleProvider) {
81+
p.ExternalID = aws.String("example-external-id") // Optionally pass ExternalID
82+
logrus.WithField("externalID", "example-external-id").Info("Using external ID for assume role")
83+
})
84+
85+
// Setup backend using the assumed role credentials
6186
backend, cleanUp := setup(t, Config{
6287
ACL: acl,
6388
Bucket: "s3-round-trip-with-role",
6489
Endpoint: endpoint,
6590
StsEndpoint: endpoint,
66-
Key: userAccessKey,
67-
PathStyle: true, // Should be true for minio and false for AWS.
91+
PathStyle: true,
92+
Key: accessKey,
93+
Secret: secretAccessKey,
6894
Region: defaultRegion,
69-
Secret: userSecretAccessKey,
7095
AssumeRoleARN: "arn:aws:iam::account-id:role/TestRole",
7196
AssumeRoleSessionName: "drone-cache",
97+
ExternalID: "example-external-id",
98+
UserRoleExternalID: "example-external-id",
99+
Credentials: creds, // Pass the assumed role credentials here
72100
})
101+
102+
// Cleanup after the test
73103
t.Cleanup(cleanUp)
104+
105+
// Perform the round-trip test
74106
roundTrip(t, backend)
75107
}
76108

@@ -124,15 +156,32 @@ func setup(t *testing.T, config Config) (*Backend, func()) {
124156
}
125157

126158
func newClient(config Config) *s3.S3 {
159+
var creds *credentials.Credentials
160+
if config.Key != "" && config.Secret != "" {
161+
creds = credentials.NewStaticCredentials(config.Key, config.Secret, "")
162+
} else {
163+
creds = credentials.NewEnvCredentials()
164+
logrus.Info("Using environment-based credentials for S3 client")
165+
}
166+
127167
conf := &aws.Config{
128-
Region: aws.String(defaultRegion),
129-
Endpoint: aws.String(endpoint),
130-
DisableSSL: aws.Bool(strings.HasPrefix(endpoint, "http://")),
131-
S3ForcePathStyle: aws.Bool(true),
132-
Credentials: credentials.NewStaticCredentials(config.Key, config.Secret, ""),
168+
Region: aws.String(defaultRegion),
169+
Endpoint: aws.String(endpoint),
170+
DisableSSL: aws.Bool(strings.HasPrefix(endpoint, "http://")),
171+
S3ForcePathStyle: aws.Bool(true),
172+
Credentials: creds,
173+
CredentialsChainVerboseErrors: aws.Bool(true),
133174
}
134175

135-
return s3.New(session.Must(session.NewSessionWithOptions(session.Options{})), conf)
176+
logrus.WithFields(logrus.Fields{
177+
"Region": defaultRegion,
178+
"Endpoint": endpoint,
179+
"AccessKey": config.Key,
180+
}).Info("Creating new S3 client")
181+
182+
return s3.New(session.Must(session.NewSessionWithOptions(session.Options{
183+
SharedConfigState: session.SharedConfigEnable,
184+
})), conf)
136185
}
137186

138187
func getEnv(key, defaultVal string) string {

0 commit comments

Comments
 (0)