/
auth.go
191 lines (168 loc) · 6.68 KB
/
auth.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
// Copyright © 2017 Jason Myers <jason@mailthemyers.com>
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package cmd
import (
"bufio"
"fmt"
"os"
"strings"
"time"
"github.com/jasonamyers/portray/util"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var accountId string
var userName string
var tokenCode string
var profile string
var noMfa bool
// authCmd represents the auth command
var authCmd = &cobra.Command{
Use: "auth",
Short: "establishes an MFA session via STS",
Long: `The auth command helps you authenticate via MFA.`,
Run: func(cmd *cobra.Command, args []string) {
noMfa = viper.GetBool("NoMfa")
// User specified profile
if profile != "" {
// validate it against Portray config
if viper.IsSet("AuthProfiles." + profile) {
profileKey := "AuthProfiles." + profile + "."
fmt.Printf("Found profile %s in config\n", profile)
// get account id from profile
if accountId == "" {
if !viper.IsSet(profileKey + "AccountId") {
fmt.Printf("Error! Unable to find AccountId for the %s profile. Is it configured in the AuthProfiles section?\n", profile)
os.Exit(1)
}
accountId = viper.GetString(profileKey + "AccountId")
viper.Set("AccountId", accountId)
} else {
fmt.Println("Error! Can't specify alternate account for a configured profile")
os.Exit(1)
}
// get user name from profile
if userName == "" {
if !viper.IsSet(profileKey + "UserName") {
fmt.Printf("Error! Unable to find UserName for the %s profile. Is it configured in the AuthProfiles section?\n", profile)
os.Exit(1)
}
userName = viper.GetString(profileKey + "UserName")
viper.Set("UserName", userName)
} else {
fmt.Println("Error! Can't specify alternate username for a configured profile")
os.Exit(1)
}
// passed validations, tell dah user
fmt.Printf("Using %s profile with AccountId %s and UserName %s\n",
profile,
accountId,
userName)
} else {
fmt.Printf("Invalid profile %s! Is it configured in the AuthProfiles section?\n", profile)
os.Exit(1)
}
} else { // user has not specified a profile
// user has not specified account
// try to find default from config
if accountId == "" {
defaultAccountId := viper.GetString("AuthProfiles.default.AccountId")
defaultUserName := viper.GetString("AuthProfiles.default.UserName")
defaultProfileName := viper.GetString("AuthProfiles.default.Name")
// populate account id
if defaultAccountId != "" {
accountId = defaultAccountId
viper.Set("AccountId", defaultAccountId)
} else {
fmt.Println("Error! Unable to find AccoundId for the default profile. Is it configured in the AuthProfiles section?")
}
// populate user name
if defaultUserName != "" {
userName = defaultUserName
viper.Set("UserName", defaultUserName)
} else {
fmt.Println("Error! Unable to find UserName for the default profile. Is it configured in the AuthProfiles section?")
}
// populate profile name
if defaultProfileName != "" {
profile = defaultProfileName
viper.Set("Profile", defaultProfileName)
} else {
fmt.Println("Default profile name not specified in config. Using default AWS profile \"default\"")
profile = "default"
viper.Set("Profile", profile)
}
fmt.Printf("Using default profile %s with AccountId %s and UserName %s\n", profile, accountId, userName)
} else {
// if user has specified account, they have to specify username
// as well.
if userName == "" {
fmt.Println("Error! Must specify --username/-u if manually setting AccountId via --account/-a")
os.Exit(1)
} else {
viper.Set("UserName", userName)
}
}
}
home, err := homedir.Dir()
util.CheckError(err)
fileName := home + "/.aws/portray-session-" + profile + ".json"
awsCreds := util.GetCredsFromFile(fileName)
// If there's no valid session cache, generate a new session. Prompt
// for MFA token if it's not passed, unless the --no-mfa flag is set.
if awsCreds.SessionToken == "" || !util.ValidateSession(awsCreds) {
if tokenCode == "" {
if noMfa {
fmt.Println("Skipping MFA token prompting")
} else {
// Prompt for MFA token
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter token: ")
token, _ := reader.ReadString('\n')
tokenCode = strings.TrimSpace(token)
}
}
awsCreds = util.GetNewSession(profile, accountId, userName, tokenCode)
util.WriteSessionFile(awsCreds, fileName)
} else {
// Found a cached sessions that's still valid
fmt.Println("Using cached session credentials")
// Check how much time is left on the session so we can tell the user
sessionExpiration := time.Unix(awsCreds.Expiration, 0)
currentTime := time.Now()
sessionTimeLeft := sessionExpiration.Sub(currentTime)
fmt.Printf("Session valid for %+v\n", util.Round(sessionTimeLeft, time.Second))
}
util.SessionToEnvVars(awsCreds, accountId, "", profile)
util.StartShell(accountId)
},
}
func init() {
rootCmd.AddCommand(authCmd)
authCmd.Flags().StringVarP(&accountId, "account", "a", "", "the AWS account number")
authCmd.Flags().StringVarP(&userName, "username", "u", "", "the AWS user name")
authCmd.Flags().StringVarP(&tokenCode, "token", "t", "", "an MFA token")
authCmd.Flags().StringVarP(&profile, "profile", "p", "", "a name for your profile")
authCmd.Flags().BoolP("no-mfa", "n", false, "disable MFA")
viper.BindPFlag("AccountId", authCmd.Flags().Lookup("account"))
viper.BindPFlag("UserName", authCmd.Flags().Lookup("username"))
viper.BindPFlag("Profile", authCmd.Flags().Lookup("profile"))
viper.BindPFlag("NoMfa", authCmd.Flags().Lookup("no-mfa"))
}