Skip to content

Commit

Permalink
Merge pull request #129 from drone-plugins/ci-9188
Browse files Browse the repository at this point in the history
adds externalID mapping for assume role
  • Loading branch information
eoinmcafee00 authored Aug 30, 2023
2 parents 41e5a19 + e3d8407 commit fe9d75b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ func main() {
Name: "env-file",
Usage: "source env file",
},
cli.StringFlag{
Name: "external-id",
Usage: "external ID to use when assuming role",
EnvVar: "PLUGIN_EXTERNAL_ID",
},
}

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

return plugin.Exec()
Expand Down
17 changes: 12 additions & 5 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ type Plugin struct {
PathStyle bool
// Dry run without uploading/
DryRun bool

// set externalID for assume role
ExternalID string
}

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

func assumeRole(roleArn, roleSessionName string) *credentials.Credentials {
func assumeRole(roleArn, roleSessionName, externalID string) *credentials.Credentials {
sess, _ := session.NewSession()
client := sts.New(sess)
duration := time.Hour * 1
Expand All @@ -298,6 +301,10 @@ func assumeRole(roleArn, roleSessionName string) *credentials.Credentials {
RoleSessionName: roleSessionName,
}

if externalID != "" {
stsProvider.ExternalID = &externalID
}

return credentials.NewCredentials(stsProvider)
}

Expand All @@ -318,17 +325,17 @@ func isDir(source string, matches []string) bool {
if err != nil {
return true // should never happen
}
if (stat.IsDir()) {
if stat.IsDir() {
count := 0
for _, match := range matches {
if strings.HasPrefix(match, source) {
count++;
count++
}
}
if count <= 1 {
log.Warnf("Skipping '%s' since it is a directory. Please use correct glob expression if this is unexpected.", source)
}
return true;
return true
}
return false
}

0 comments on commit fe9d75b

Please sign in to comment.