-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
230 lines (180 loc) · 5.48 KB
/
main.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"bufio"
"fmt"
"log"
"os"
"regexp"
"strconv"
"strings"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/defaults"
"gopkg.in/ini.v1"
)
// LoadIni will load a filename as an INI file, as well as
// a slice of the config sections
func LoadIni(filename string) (*ini.File, []*ini.Section, error) {
iniFile, err := ini.Load(filename)
if err != nil {
return nil, nil, err
}
sections := iniFile.Sections()
return iniFile, sections, nil
}
// SetCreds will populate the aws credentials file(passed as INI)
// with a new 'default' section and updated STS credentials
func SetCreds(filename string, creds *sts.Credentials, f *ini.File) {
section, err := f.NewSection("default")
if err != nil {
fmt.Println(err)
}
// Populate the section with the temp creds
fmt.Println("Setting temporary credentials in your file")
section.NewKey("aws_access_key_id", *creds.AccessKeyId)
section.NewKey("aws_secret_access_key", *creds.SecretAccessKey)
section.NewKey("aws_session_token", *creds.SessionToken)
f.SaveTo(filename)
}
// Authenticate with the AWS CLI
func Authenticate(profile, mfa, token, credfile, conffile string, s *sts.STS, conf, cred *ini.File) {
activeProf := conf.Section(profile)
// This handles authentication when a credential section is role-based instead of user-based
if activeProf.HasKey("role_arn") {
roleArn, _ := activeProf.GetKey("role_arn")
res, err := s.GetSessionToken(&sts.GetSessionTokenInput{
TokenCode: aws.String(token),
SerialNumber: aws.String(mfa),
})
if err != nil {
log.Fatalln(err)
}
id := *res.Credentials.AccessKeyId
secret := *res.Credentials.SecretAccessKey
stoken := *res.Credentials.SessionToken
sessConf := &aws.Config{
Credentials: credentials.NewStaticCredentials(id, secret, stoken),
}
sess, err := session.NewSession(sessConf)
if err != nil {
fmt.Println(err)
}
svc := sts.New(sess)
input := &sts.AssumeRoleInput{
RoleArn: aws.String(roleArn.String()),
DurationSeconds: aws.Int64(3600),
RoleSessionName: aws.String(profile + "-mfa-session"),
}
result, err := svc.AssumeRole(input)
if err != nil {
fmt.Println(err)
}
SetCreds(credfile, result.Credentials, cred)
fmt.Println("Complete!")
} else {
result, err := s.GetSessionToken(&sts.GetSessionTokenInput{
TokenCode: aws.String(token),
SerialNumber: aws.String(mfa),
})
if err != nil {
log.Fatalln(err)
}
SetCreds(credfile, result.Credentials, cred)
fmt.Println("Complete!")
}
}
func main() {
// Load the config and credential filenames
credfile := defaults.SharedCredentialsFilename()
configfile := defaults.SharedConfigFilename()
// Clear any existing env vars to avoid issues
awsvars := []string{"AWS_ACCESS_KEY_ID", "AWS_SECRET_KEY_ID", "AWS_SESSION_TOKEN"}
fmt.Println("Resetting AWS Environment variables...")
fmt.Println("===================================")
for _, v := range awsvars {
err := os.Setenv(v, "")
if err != nil {
fmt.Println(err)
}
}
// Load the credentials and config files as ini files
awscreds, credprofiles, err := LoadIni(credfile)
awsconfig, _, err := LoadIni(configfile)
var profiles []string
for _, s := range credprofiles {
if !(strings.Contains(strings.ToLower(s.Name()), "default")) {
profiles = append(profiles, s.Name())
}
}
fmt.Println("Choose which AWS profile to authenticate with:")
for i, p := range profiles {
fmt.Printf("%d: %s\n", i+1, p)
}
chooseProfile := func() string {
var profile string
for {
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
text = strings.Replace(text, "\n", "", -1)
i, err := strconv.Atoi(text)
if err != nil {
fmt.Println(err)
continue
}
if i >= 1 && i <= len(profiles) {
profile = profiles[i-1]
break
}
fmt.Println("Please choose one of the available profiles")
continue
}
return profile
}
profile := chooseProfile()
// Read in hard-coded config/credentials(needed for STS handoff)
conf := &aws.Config{
Credentials: credentials.NewSharedCredentials(credfile, profile),
}
// Create new session
sess, err := session.NewSession(conf)
// Create session with STS service for getting the initial caller identity
// and the eventual auth token
_sts := sts.New(sess)
// Get ARN of profile user
arn, err := _sts.GetCallerIdentity(&sts.GetCallerIdentityInput{})
if err != nil {
fmt.Println(err)
}
// Create session with IAM service to get MFA device for chosen profile
_iam := iam.New(sess)
mfas, err := _iam.ListVirtualMFADevices(&iam.ListVirtualMFADevicesInput{
AssignmentStatus: aws.String("Assigned"),
})
var mfa string
for _, device := range mfas.VirtualMFADevices {
if *device.User.Arn == *arn.Arn {
mfa = *device.SerialNumber
}
}
// Get MFA token from user
token, err := stscreds.StdinTokenProvider()
if err != nil {
fmt.Println(err)
}
// Use regex to validate a 6 digit number and catch
// malformed input without wasting an API call
tokenmatch, _ := regexp.MatchString(`^\d{6}$`, token)
for !tokenmatch {
fmt.Println("Please enter a valid 6 digit token")
token, err = stscreds.StdinTokenProvider()
if err != nil {
fmt.Println(err)
}
}
// Perform authentication
Authenticate(profile, mfa, token, credfile, configfile, _sts, awsconfig, awscreds)
}