/
main.go
292 lines (261 loc) · 9.87 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// main.go
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"io/ioutil"
"strings"
"strconv"
"github.com/vaughan0/go-ini"
"github.com/fatih/color"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/sts"
)
// Global constants
const (
ProgName = "awslogin"
ProgVer = "1.5.2"
)
// Global variables
var (
gra = color.New(color.FgWhite).SprintFunc()
whi = color.New(color.FgWhite, color.Bold).SprintFunc()
red = color.New(color.FgRed, color.Bold).SprintFunc()
skeletonConf = "[default]\n" +
"aws_access_key_id = AKERNEIDUFENICUQ3NDO\n" +
"aws_secret_access_key = ilsjkasdUEwlwDUgvD1b7234Fn/lepi0ACmk8upFy\n\n" +
"[stag]\n" +
"profile_name = stag\n" +
"account_number = 466692114123\n" +
"user_role = PowerUser\n\n" +
"[prod]\n" +
"profile_name = prod\n" +
"account_number = 544492114123\n" +
"user_role = PowerUser\n"
progConfDir = filepath.Join(os.Getenv("HOME"), "." + ProgName)
awsConfFile = filepath.Join(os.Getenv("HOME"), ".aws/config")
awsCredsFile = filepath.Join(os.Getenv("HOME"), ".aws/credentials")
)
func main() {
// Allow only 1 or 2 arguments
args := os.Args[1:]
if len(args) == 1 {
if args[0] == "-c" {
createSkeletonConfig()
} else if args[0] == "-l" {
listAccounts()
} else if args[0] == "-h" {
printHelp()
} else {
printUsage()
}
} else if len(args) != 2 {
printUsage()
}
profile := args[0] // Target account profile in config
token := args[1] // 6-digit token from user's MFA device
_, err := strconv.Atoi(token)
if err != nil || len(token) != 6 {
Die(1, "Token " + red(token) + " is invalid")
}
// Validate credentials file settings, ensuring target profile is valid
cfg := validateConfig(profile)
logonToAWS(cfg, profile, token)
}
func printUsage() {
fmt.Println("AWS CLI MFA Logon Utility", ProgVer)
fmt.Println(ProgName, "PROFILE TOKEN Logon to account PROFILE using 6-digit TOKEN")
fmt.Println(" -l List all account profiles in", gra(awsCredsFile))
fmt.Println(" -c Create skeleton", gra(awsCredsFile), "file")
fmt.Println(" -h Show additional help information")
Die(0,"")
}
func printHelp() {
fmt.Println("This utility simplifies secured AWS CLI MFA authentication to any account profile")
fmt.Println("defined in your", gra(awsCredsFile), "file. It expects that file to be")
fmt.Println("formatted in the following sample manner:\n")
fmt.Println(skeletonConf)
fmt.Println("So ...")
fmt.Println("1. The", gra("default"), "profile is for the AWS account, where your username is defined")
fmt.Println("2. All other profiles are treated as", gra("federated"), "AWS accounts you may have access to")
fmt.Println("3. You", gra("must"), "defined a valid key pair for your", gra("default"), "profile")
fmt.Println("4. Each profile must have a unique", gra("profile_name"), "so this utility can identify it")
fmt.Println("5. Each federated profile must have a valid", gra("account_number"), "and", gra("user_role"))
fmt.Println("6. The -c switch can create a fresh skeleton", gra(awsCredsFile), "file")
Die(0,"")
}
// Exit program with a final message
func Die(code int, message string) {
if message != "" { fmt.Println(message) }
os.Exit(code)
}
// Create default skeleton credentials file
func createSkeletonConfig() {
if _, err := os.Stat(awsCredsFile); os.IsNotExist(err) {
err = ioutil.WriteFile(awsCredsFile, []byte(skeletonConf), 0600)
if err != nil {
Die(1, err.Error())
}
} else {
fmt.Printf("There's already a %s file.\n", gra(awsCredsFile))
}
Die(0,"")
}
// List all accounts in current credentials file
func listAccounts() {
cfg, err := ini.LoadFile(awsCredsFile)
if err != nil {
Die(1, err.Error())
}
for section, _ := range cfg {
fmt.Printf("[%s]\n", whi(section))
for k, v := range cfg[section] {
fmt.Printf("%-21s = %s\n", k, v)
}
fmt.Println("")
}
Die(0,"")
}
// Check configuration file for anomalies and return the full config object
func validateConfig(profile string) (cfg ini.File) {
// Read the credentials INI file
cfg, err := ini.LoadFile(awsCredsFile)
if err != nil {
Die(1, err.Error())
}
// Ensure there is at least one default section, with the required 2 entries
deflt, prof := 0, 0
for section, entries := range cfg {
if section == "default" {
if entries["aws_access_key_id"] != "" &&
entries["aws_secret_access_key"] != "" {
deflt++
}
}
if section == profile {
prof++
}
}
if deflt == 0 {
Die(1, "File " + gra(awsCredsFile) + " is missing " + red("default") +
" profile or both credential entries.")
}
// Ensure user-specified profile is in the config file and is valid
if prof == 0 {
Die(1, "Profile name " + red(profile) + " is not defined in " + gra(awsCredsFile))
}
if cfg[profile]["profile_name"] == "" ||
cfg[profile]["account_number"] == "" ||
cfg[profile]["user_role"] == "" {
Die(1, "Profile name " + red(profile) + " is missing needed entries")
}
return cfg
}
// Logon to AWS account designated by given profile, with given token
func logonToAWS(cfg ini.File, profile, token string) {
// In order to make any initial AWS API call we have validate the default profile credentials
os.Setenv("AWS_REGION", GetAWSRegion())
os.Setenv("AWS_ACCESS_KEY_ID", cfg["default"]["aws_access_key_id"])
os.Setenv("AWS_SECRET_ACCESS_KEY", cfg["default"]["aws_secret_access_key"])
// Get main account username. Also an implicit validation of the keys in default profile
sess := session.Must(session.NewSession())
svc := iam.New(sess, aws.NewConfig())
resp, err := svc.GetUser(nil)
if err != nil {
Die(1, err.Error())
}
usrarn := *resp.User.Arn
username := *resp.User.UserName
if usrarn == "" || username == "" {
Die(1, "Error with returned AWS user object.")
}
// Derive main account ID and MFA device ARN
s := strings.Split(usrarn, ":")
mainAccountId := s[4]
mfaDeviceArn := "arn:aws:iam::" + mainAccountId + ":mfa/" + username
// Do either main or federated account login, based on profile
accessKey, secretKey, sessToken := "", "", ""
svc2 := sts.New(sess)
if profile == cfg["default"]["profile_name"] {
params := &sts.GetSessionTokenInput{
DurationSeconds: aws.Int64(86400), // One day for default account
SerialNumber: aws.String(mfaDeviceArn),
TokenCode: aws.String(token),
}
resp, err := svc2.GetSessionToken(params)
if err != nil {
Die(1, err.Error())
}
accessKey = *resp.Credentials.AccessKeyId
secretKey = *resp.Credentials.SecretAccessKey
sessToken = *resp.Credentials.SessionToken
} else {
// Derive the Role ARN
targetAccountId := cfg[profile]["account_number"]
userRole := cfg[profile]["user_role"]
roleArn := "arn:aws:iam::" + targetAccountId + ":role/" + userRole
params := &sts.AssumeRoleInput{
DurationSeconds: aws.Int64(3600), // One hour for target account
SerialNumber: aws.String(mfaDeviceArn),
TokenCode: aws.String(token),
RoleSessionName: aws.String(username),
RoleArn: aws.String(roleArn),
}
resp, err := svc2.AssumeRole(params)
if err != nil {
Die(1, err.Error())
}
accessKey = *resp.Credentials.AccessKeyId
secretKey = *resp.Credentials.SecretAccessKey
sessToken = *resp.Credentials.SessionToken
}
// Actual logon is to simply update these 3 environment variables with newly
// acquired credentials then jumping into a subshell
if resp != nil {
os.Setenv("AWS_ACCESS_KEY_ID", accessKey)
os.Setenv("AWS_SECRET_ACCESS_KEY", secretKey)
os.Setenv("AWS_SESSION_TOKEN", sessToken)
fmt.Println(ProgName, ": Logged in to AWS within a new subshell. Type exit when done.")
execCommand(os.Getenv("SHELL"))
Die(0, ProgName + ": Back to your original shell session.")
}
}
// Run any OS command
func execCommand(program string, args ...string) {
cmd := exec.Command(program, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
Die(1, err.Error())
}
}
func GetAWSRegion() (awsRegion string) {
// Start by checking the environment variables (order is important)
if os.Getenv("AWS_REGION") != "" {
awsRegion = os.Getenv("AWS_REGION")
} else if os.Getenv("AMAZON_REGION") != "" {
awsRegion = os.Getenv("AMAZON_REGION")
} else if os.Getenv("AWS_DEFAULT_REGION") != "" {
awsRegion = os.Getenv("AWS_DEFAULT_REGION")
} else {
// End by checking the AWS config file
if _, err := os.Stat(awsConfFile); os.IsNotExist(err) {
Die(1, "AWS region variable is not defined, and " + awsConfFile + " file does not exist.")
}
cfgfile, err := ini.LoadFile(awsConfFile)
if err != nil {
Die(1, err.Error())
}
awsRegion, _ = cfgfile.Get("default", "region")
}
if awsRegion == "" {
Die(1, "Error. AWS region variable is not defined anywhere.")
}
return awsRegion
}