-
Notifications
You must be signed in to change notification settings - Fork 0
/
CachingSessionTokenProvider.go
135 lines (115 loc) · 3.74 KB
/
CachingSessionTokenProvider.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/defaults"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/mbndr/logo"
"os/user"
"path/filepath"
"time"
)
type CachingSessionTokenProvider struct {
CredentialsCacherProvider
Profile string
Duration time.Duration
MfaSerial string
Logger *logo.Logger
}
func (p *CachingSessionTokenProvider) CacheFile() string {
p.Logger.Debug("In CacheFile()")
cacheDir := filepath.Dir(defaults.SharedCredentialsFilename())
cacheFile := filepath.Join(cacheDir, fmt.Sprintf(".aws_session_token_%s", p.Profile))
p.Logger.Debugf("CREDENTIAL CACHE FILE: %s", cacheFile)
return cacheFile
}
// Satisfies credentials.Provider
func (p *CachingSessionTokenProvider) Retrieve() (credentials.Value, error) {
p.Logger.Debug("In Retrieve()")
p.CredentialsCacherProvider.CacheFilename = p.CacheFile()
creds, _ := p.CredentialsCacherProvider.Retrieve() // TODO let errors fall through?
if p.IsExpired() {
p.Logger.Debug("Detected expired credentials")
dur_secs := int64(p.Duration.Seconds())
input := sts.GetSessionTokenInput{DurationSeconds: &dur_secs}
if len(p.MfaSerial) > 0 {
p.Logger.Debug("MfaSerial passed, prompting for code")
// Prompt for MFA code
var mfa_code string
fmt.Print("Enter MFA Code: ")
fmt.Scanln(&mfa_code)
input.SerialNumber = &p.MfaSerial
input.TokenCode = &mfa_code
}
sess := session.Must(session.NewSessionWithOptions(
session.Options{
SharedConfigState: session.SharedConfigEnable,
Profile: p.Profile,
}))
s := sts.New(sess)
res, err := s.GetSessionToken(&input)
if err != nil {
return credentials.Value{}, err
}
c := CacheableCredentials{
AccessKeyId: *res.Credentials.AccessKeyId,
SecretAccessKey: *res.Credentials.SecretAccessKey,
SessionToken: *res.Credentials.SessionToken,
Expiration: (*res.Credentials.Expiration).Unix(),
}
p.Logger.Debug("Storing new credentials in cache")
p.CredentialsCacherProvider.Store(&c)
creds, _ = p.CredentialsCacherProvider.Retrieve()
}
p.Logger.Debugf("SESSION TOKEN CREDENTIALS: %+v", creds)
return creds, nil
}
// Satisfies credentials.Provider
func (p *CachingSessionTokenProvider) IsExpired() bool {
p.Logger.Debug("In IsExpired()")
expired := true
expired = p.CredentialsCacherProvider.IsExpired()
p.Logger.Debugf("EXPIRED: %t", expired)
return expired
}
func (p *CachingSessionTokenProvider) AssumeRole(profile_cfg *AWSProfile) (credentials.Value, error) {
p.Logger.Debug("In AssumeRole()")
username := "__"
user, err := user.Current()
if err == nil {
username = user.Username
}
sesName := aws.String(fmt.Sprintf("AWS-RUNAS-%s-%d", username, time.Now().Unix()))
p.Logger.Debugf("Setting AssumeRole session name to: %s", *sesName)
input := sts.AssumeRoleInput{
DurationSeconds: aws.Int64(int64(3600)),
RoleArn: aws.String(profile_cfg.RoleArn),
RoleSessionName: sesName,
}
creds, err := p.Retrieve()
if err != nil {
return credentials.Value{}, err
}
sesOpts := session.Options{
Profile: profile_cfg.SourceProfile,
SharedConfigState: session.SharedConfigEnable,
Config: aws.Config{Credentials: credentials.NewStaticCredentialsFromCreds(creds)},
}
s := session.Must(session.NewSessionWithOptions(sesOpts))
sts := sts.New(s)
res, err := sts.AssumeRole(&input)
if err != nil {
return credentials.Value{}, err
}
c := res.Credentials
v := credentials.Value{
AccessKeyID: *c.AccessKeyId,
SecretAccessKey: *c.SecretAccessKey,
SessionToken: *c.SessionToken,
ProviderName: "CachingSessionTokenProvider",
}
p.Logger.Debugf("ASSUME ROLE CREDENTIALS: %+v", v)
return v, nil
}