forked from remind101/assume-role
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.go
35 lines (29 loc) · 1013 Bytes
/
exec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main
import (
"os"
"os/exec"
"syscall"
"github.com/aws/aws-sdk-go/aws/credentials"
log "github.com/sirupsen/logrus"
)
func execWithCredentials(role string, argv []string, creds *credentials.Value) error {
argv0, err := exec.LookPath(argv[0])
if err != nil {
return err
}
os.Setenv("AWS_ACCESS_KEY_ID", creds.AccessKeyID)
os.Setenv("AWS_SECRET_ACCESS_KEY", creds.SecretAccessKey)
os.Setenv("AWS_SESSION_TOKEN", creds.SessionToken)
os.Setenv("AWS_SECURITY_TOKEN", creds.SessionToken)
os.Setenv("ASSUMED_ROLE", role)
log.WithFields(log.Fields{
"command": argv0,
"AWS_ACCESS_KEY_ID": os.Getenv("AWS_ACCESS_KEY_ID"),
"AWS_SECRET_ACCESS_KEY": os.Getenv("AWS_SECRET_ACCESS_KEY"),
"AWS_SESSION_TOKEN": os.Getenv("AWS_SESSION_TOKEN"),
"AWS_SECURITY_TOKEN": os.Getenv("AWS_SECURITY_TOKEN"),
"ASSUMED_ROLE": os.Getenv("ASSUMED_ROLE"),
}).Debug("Executing command with credentials...")
env := os.Environ()
return syscall.Exec(argv0, argv, env)
}