Skip to content

Commit fe9d75b

Browse files
authored
Merge pull request #129 from drone-plugins/ci-9188
adds externalID mapping for assume role
2 parents 41e5a19 + e3d8407 commit fe9d75b

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ func main() {
132132
Name: "env-file",
133133
Usage: "source env file",
134134
},
135+
cli.StringFlag{
136+
Name: "external-id",
137+
Usage: "external ID to use when assuming role",
138+
EnvVar: "PLUGIN_EXTERNAL_ID",
139+
},
135140
}
136141

137142
if err := app.Run(os.Args); err != nil {
@@ -165,6 +170,7 @@ func run(c *cli.Context) error {
165170
StorageClass: c.String("storage-class"),
166171
PathStyle: c.Bool("path-style"),
167172
DryRun: c.Bool("dry-run"),
173+
ExternalID: c.String("external-id"),
168174
}
169175

170176
return plugin.Exec()

plugin.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ type Plugin struct {
9090
PathStyle bool
9191
// Dry run without uploading/
9292
DryRun bool
93+
94+
// set externalID for assume role
95+
ExternalID string
9396
}
9497

9598
// Exec runs the plugin
@@ -108,7 +111,7 @@ func (p *Plugin) Exec() error {
108111
if p.Key != "" && p.Secret != "" {
109112
conf.Credentials = credentials.NewStaticCredentials(p.Key, p.Secret, "")
110113
} else if p.AssumeRole != "" {
111-
conf.Credentials = assumeRole(p.AssumeRole, p.AssumeRoleSessionName)
114+
conf.Credentials = assumeRole(p.AssumeRole, p.AssumeRoleSessionName, p.ExternalID)
112115
} else {
113116
log.Warn("AWS Key and/or Secret not provided (falling back to ec2 instance profile)")
114117
}
@@ -287,7 +290,7 @@ func matchExtension(match string, stringMap map[string]string) string {
287290
return ""
288291
}
289292

290-
func assumeRole(roleArn, roleSessionName string) *credentials.Credentials {
293+
func assumeRole(roleArn, roleSessionName, externalID string) *credentials.Credentials {
291294
sess, _ := session.NewSession()
292295
client := sts.New(sess)
293296
duration := time.Hour * 1
@@ -298,6 +301,10 @@ func assumeRole(roleArn, roleSessionName string) *credentials.Credentials {
298301
RoleSessionName: roleSessionName,
299302
}
300303

304+
if externalID != "" {
305+
stsProvider.ExternalID = &externalID
306+
}
307+
301308
return credentials.NewCredentials(stsProvider)
302309
}
303310

@@ -318,17 +325,17 @@ func isDir(source string, matches []string) bool {
318325
if err != nil {
319326
return true // should never happen
320327
}
321-
if (stat.IsDir()) {
328+
if stat.IsDir() {
322329
count := 0
323330
for _, match := range matches {
324331
if strings.HasPrefix(match, source) {
325-
count++;
332+
count++
326333
}
327334
}
328335
if count <= 1 {
329336
log.Warnf("Skipping '%s' since it is a directory. Please use correct glob expression if this is unexpected.", source)
330337
}
331-
return true;
338+
return true
332339
}
333340
return false
334341
}

0 commit comments

Comments
 (0)