Skip to content

Commit af61fd6

Browse files
authored
Merge pull request #66 from robertstettner/assume-role
2 parents f6fdfaf + 15078de commit af61fd6

File tree

3 files changed

+56
-21
lines changed

3 files changed

+56
-21
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module github.com/drone-plugins/drone-s3
22

3+
go 1.15
4+
35
require (
46
github.com/aws/aws-sdk-go v1.16.17
57
github.com/joho/godotenv v1.3.0

main.go

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ func main() {
3636
Usage: "aws secret key",
3737
EnvVar: "PLUGIN_SECRET_KEY,AWS_SECRET_ACCESS_KEY",
3838
},
39+
cli.StringFlag{
40+
Name: "assume-role",
41+
Usage: "aws iam role to assume",
42+
EnvVar: "PLUGIN_ASSUME_ROLE,ASSUME_ROLE",
43+
},
44+
cli.StringFlag{
45+
Name: "assume-role-session-name",
46+
Usage: "aws iam role session name to assume",
47+
Value: "drone-s3",
48+
EnvVar: "PLUGIN_ASSUME_ROLE_SESSION_NAME,ASSUME_ROLE_SESSION_NAME",
49+
},
3950
cli.StringFlag{
4051
Name: "bucket",
4152
Usage: "aws bucket",
@@ -129,23 +140,25 @@ func run(c *cli.Context) error {
129140
}
130141

131142
plugin := Plugin{
132-
Endpoint: c.String("endpoint"),
133-
Key: c.String("access-key"),
134-
Secret: c.String("secret-key"),
135-
Bucket: c.String("bucket"),
136-
Region: c.String("region"),
137-
Access: c.String("acl"),
138-
Source: c.String("source"),
139-
Target: c.String("target"),
140-
StripPrefix: c.String("strip-prefix"),
141-
Exclude: c.StringSlice("exclude"),
142-
Encryption: c.String("encryption"),
143-
ContentType: c.Generic("content-type").(*StringMapFlag).Get(),
144-
ContentEncoding: c.Generic("content-encoding").(*StringMapFlag).Get(),
145-
CacheControl: c.Generic("cache-control").(*StringMapFlag).Get(),
146-
StorageClass: c.String("storage-class"),
147-
PathStyle: c.Bool("path-style"),
148-
DryRun: c.Bool("dry-run"),
143+
Endpoint: c.String("endpoint"),
144+
Key: c.String("access-key"),
145+
Secret: c.String("secret-key"),
146+
AssumeRole: c.String("assume-role"),
147+
AssumeRoleSessionName: c.String("assume-role-session-name"),
148+
Bucket: c.String("bucket"),
149+
Region: c.String("region"),
150+
Access: c.String("acl"),
151+
Source: c.String("source"),
152+
Target: c.String("target"),
153+
StripPrefix: c.String("strip-prefix"),
154+
Exclude: c.StringSlice("exclude"),
155+
Encryption: c.String("encryption"),
156+
ContentType: c.Generic("content-type").(*StringMapFlag).Get(),
157+
ContentEncoding: c.Generic("content-encoding").(*StringMapFlag).Get(),
158+
CacheControl: c.Generic("cache-control").(*StringMapFlag).Get(),
159+
StorageClass: c.String("storage-class"),
160+
PathStyle: c.Bool("path-style"),
161+
DryRun: c.Bool("dry-run"),
149162
}
150163

151164
return plugin.Exec()

plugin.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,26 @@ import (
66
"path/filepath"
77
"regexp"
88
"strings"
9+
"time"
910

1011
"github.com/aws/aws-sdk-go/aws"
1112
"github.com/aws/aws-sdk-go/aws/credentials"
13+
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
1214
"github.com/aws/aws-sdk-go/aws/session"
1315
"github.com/aws/aws-sdk-go/service/s3"
16+
"github.com/aws/aws-sdk-go/service/sts"
1417
"github.com/mattn/go-zglob"
1518
log "github.com/sirupsen/logrus"
1619
)
1720

1821
// Plugin defines the S3 plugin parameters.
1922
type Plugin struct {
20-
Endpoint string
21-
Key string
22-
Secret string
23-
Bucket string
23+
Endpoint string
24+
Key string
25+
Secret string
26+
AssumeRole string
27+
AssumeRoleSessionName string
28+
Bucket string
2429

2530
// if not "", enable server-side encryption
2631
// valid values are:
@@ -103,6 +108,8 @@ func (p *Plugin) Exec() error {
103108

104109
if p.Key != "" && p.Secret != "" {
105110
conf.Credentials = credentials.NewStaticCredentials(p.Key, p.Secret, "")
111+
} else if p.AssumeRole != "" {
112+
conf.Credentials = assumeRole(p.AssumeRole, p.AssumeRoleSessionName)
106113
} else {
107114
log.Warn("AWS Key and/or Secret not provided (falling back to ec2 instance profile)")
108115
}
@@ -272,3 +279,16 @@ func matchExtension(match string, stringMap map[string]string) string {
272279

273280
return ""
274281
}
282+
283+
func assumeRole(roleArn, roleSessionName string) *credentials.Credentials {
284+
client := sts.New(session.New())
285+
duration := time.Hour * 1
286+
stsProvider := &stscreds.AssumeRoleProvider{
287+
Client: client,
288+
Duration: duration,
289+
RoleARN: roleArn,
290+
RoleSessionName: roleSessionName,
291+
}
292+
293+
return credentials.NewCredentials(stsProvider)
294+
}

0 commit comments

Comments
 (0)