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

Loading credentials from env_file parameter #107

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/franela/goblin v0.0.0-20170111051028-2fa789fd0c6b
github.com/go-ini/ini v1.21.1
github.com/jmespath/go-jmespath v0.0.0-20160803190731-bd40a432e4c7
github.com/joho/godotenv v0.0.0-20150907010228-4ed13390c0ac
github.com/joho/godotenv v1.3.0
ndajr marked this conversation as resolved.
Show resolved Hide resolved
github.com/urfave/cli v0.0.0-20161006035353-55f715e28c46
golang.org/x/sys v0.0.0-20161006025142-8d1157a43547
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/jmespath/go-jmespath v0.0.0-20160803190731-bd40a432e4c7 h1:SMvOWPJCES
github.com/jmespath/go-jmespath v0.0.0-20160803190731-bd40a432e4c7/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/joho/godotenv v0.0.0-20150907010228-4ed13390c0ac h1:wF2VgtpbaLqhBHV9FxVWzgzgv8VcCjZ66Bl/+F6cpT0=
github.com/joho/godotenv v0.0.0-20150907010228-4ed13390c0ac/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/urfave/cli v0.0.0-20161006035353-55f715e28c46 h1:EztUvugq7AA7F3lYLmtFQyvKdcY5pisPt10DqPjRCL8=
github.com/urfave/cli v0.0.0-20161006035353-55f715e28c46/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
golang.org/x/sys v0.0.0-20161006025142-8d1157a43547/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ func main() {
EnvVar: "PLUGIN_CA_CERT",
},
cli.StringFlag{
Name: "env-file",
Usage: "source env file",
Name: "env_file",
ndajr marked this conversation as resolved.
Show resolved Hide resolved
Usage: "pass filename to source it and load variables into current shell",
EnvVar: "PLUGIN_ENV_FILE",
},
cli.StringFlag{
Name: "init_options",
Expand Down Expand Up @@ -125,8 +126,8 @@ func run(c *cli.Context) error {
"Revision": revision,
}).Info("Drone Terraform Plugin Version")

if c.String("env-file") != "" {
_ = godotenv.Load(c.String("env-file"))
if c.String("env_file") != "" {
_ = godotenv.Load(c.String("env_file"))
}

var vars map[string]string
Expand Down
12 changes: 11 additions & 1 deletion plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (p Plugin) Exec() error {
}
}

if p.Config.RoleARN != "" {
if p.Config.RoleARN != "" && !credsSet() {
assumeRole(p.Config.RoleARN)
}

Expand Down Expand Up @@ -169,6 +169,16 @@ func CopyTfEnv() {
}
}

func credsSet() bool {
ndajr marked this conversation as resolved.
Show resolved Hide resolved
awsTokens := []string{"AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN"}
ndajr marked this conversation as resolved.
Show resolved Hide resolved
for _, token := range awsTokens {
if os.Getenv(token) == "" {
return false
}
}
return true
}

func assumeRole(roleArn string) {
ndajr marked this conversation as resolved.
Show resolved Hide resolved
client := sts.New(session.New())
duration := time.Hour * 1
Expand Down
38 changes: 38 additions & 0 deletions plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,44 @@ func TestPlugin(t *testing.T) {
})
})

g.Describe("credsSet", func() {
tests := []struct {
name string
args map[string]string
want bool
}{
{
"Should return true when all credentials were set",
map[string]string{"AWS_ACCESS_KEY_ID": "x", "AWS_SECRET_ACCESS_KEY": "x", "AWS_SESSION_TOKEN": "x"},
true,
},
{
"Should return false when access key id is missing",
map[string]string{"AWS_SECRET_ACCESS_KEY": "x", "AWS_SESSION_TOKEN": "x"},
false,
},
{
"Should return false when secret access key is missing",
map[string]string{"AWS_ACCESS_KEY_ID": "x", "AWS_SESSION_TOKEN": "x"},
false,
},
{
"Should return false when session token is missing",
map[string]string{"AWS_ACCESS_KEY_ID": "x", "AWS_SECRET_ACCESS_KEY": "x"},
false,
},
}

for _, tt := range tests {
g.It(tt.name, func() {
for k, v := range tt.args {
os.Setenv(k, v)
}
g.Assert(credsSet()).Equal(tt.want)
})
}
})

g.Describe("tfApply", func() {
g.It("Should return correct apply commands given the arguments", func() {
type args struct {
Expand Down