forked from Versent/saml2aws
/
login.go
274 lines (213 loc) · 8 KB
/
login.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
package commands
import (
"encoding/base64"
"fmt"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/versent/saml2aws"
"github.com/versent/saml2aws/helper/credentials"
"github.com/versent/saml2aws/pkg/awsconfig"
"github.com/versent/saml2aws/pkg/cfg"
"github.com/versent/saml2aws/pkg/creds"
"github.com/versent/saml2aws/pkg/flags"
)
// Login login to ADFS
func Login(loginFlags *flags.LoginExecFlags) error {
logger := logrus.WithField("command", "login")
account, err := buildIdpAccount(loginFlags)
if err != nil {
return errors.Wrap(err, "error building login details")
}
sharedCreds := awsconfig.NewSharedCredentials(account.Profile)
logger.Debug("check if Creds Exist")
// this checks if the credentials file has been created yet
exist, err := sharedCreds.CredsExists()
if err != nil {
return errors.Wrap(err, "error loading credentials")
}
if !exist {
fmt.Println("unable to load credentials, login required to create them")
return nil
}
if !sharedCreds.Expired() && !loginFlags.Force {
fmt.Println("credentials are not expired skipping")
return nil
}
loginDetails, err := resolveLoginDetails(account, loginFlags)
if err != nil {
fmt.Printf("%+v\n", err)
os.Exit(1)
}
err = loginDetails.Validate()
if err != nil {
return errors.Wrap(err, "error validating login details")
}
logger.WithField("idpAccount", account).Debug("building provider")
provider, err := saml2aws.NewSAMLClient(account)
if err != nil {
return errors.Wrap(err, "error building IdP client")
}
fmt.Printf("Authenticating as %s ...\n", loginDetails.Username)
samlAssertion, err := provider.Authenticate(loginDetails)
if err != nil {
return errors.Wrap(err, "error authenticating to IdP")
}
if samlAssertion == "" {
fmt.Println("Response did not contain a valid SAML assertion")
fmt.Println("Please check your username and password is correct")
os.Exit(1)
}
err = credentials.SaveCredentials(loginDetails.URL, loginDetails.Username, loginDetails.Password)
if err != nil {
return errors.Wrap(err, "error storing password in keychain")
}
role, err := selectAwsRole(samlAssertion, account)
if err != nil {
return errors.Wrap(err, "Failed to assume role, please check whether you are permitted to assume the given role for the AWS service")
}
fmt.Println("Selected role:", role.RoleARN)
awsCreds, err := loginToStsUsingRole(account, role, samlAssertion)
if err != nil {
return errors.Wrap(err, "error logging into aws role using saml assertion")
}
return saveCredentials(awsCreds, sharedCreds)
}
func buildIdpAccount(loginFlags *flags.LoginExecFlags) (*cfg.IDPAccount, error) {
cfgm, err := cfg.NewConfigManager(cfg.DefaultConfigPath)
if err != nil {
return nil, errors.Wrap(err, "failed to load configuration")
}
account, err := cfgm.LoadIDPAccount(loginFlags.CommonFlags.IdpAccount)
if err != nil {
return nil, errors.Wrap(err, "failed to load idp account")
}
// update username and hostname if supplied
flags.ApplyFlagOverrides(loginFlags.CommonFlags, account)
err = account.Validate()
if err != nil {
return nil, errors.Wrap(err, "failed to validate account")
}
return account, nil
}
func resolveLoginDetails(account *cfg.IDPAccount, loginFlags *flags.LoginExecFlags) (*creds.LoginDetails, error) {
// fmt.Printf("loginFlags %+v\n", loginFlags)
loginDetails := &creds.LoginDetails{URL: account.URL, Username: account.Username, MFAToken: loginFlags.CommonFlags.MFAToken, DuoMFAOption: loginFlags.DuoMFAOption}
fmt.Printf("Using IDP Account %s to access %s %s\n", loginFlags.CommonFlags.IdpAccount, account.Provider, account.URL)
err := credentials.LookupCredentials(loginDetails, account.Provider)
if err != nil {
if !credentials.IsErrCredentialsNotFound(err) {
return nil, errors.Wrap(err, "error loading saved password")
}
}
// fmt.Printf("%s %s\n", savedUsername, savedPassword)
// if you supply a username in a flag it takes precedence
if loginFlags.CommonFlags.Username != "" {
loginDetails.Username = loginFlags.CommonFlags.Username
}
// if you supply a password in a flag it takes precedence
if loginFlags.CommonFlags.Password != "" {
loginDetails.Password = loginFlags.CommonFlags.Password
}
// fmt.Printf("loginDetails %+v\n", loginDetails)
// if skip prompt was passed just pass back the flag values
if loginFlags.CommonFlags.SkipPrompt {
return loginDetails, nil
}
err = saml2aws.PromptForLoginDetails(loginDetails, account.Provider)
if err != nil {
return nil, errors.Wrap(err, "Error occurred accepting input")
}
return loginDetails, nil
}
func selectAwsRole(samlAssertion string, account *cfg.IDPAccount) (*saml2aws.AWSRole, error) {
data, err := base64.StdEncoding.DecodeString(samlAssertion)
if err != nil {
return nil, errors.Wrap(err, "error decoding saml assertion")
}
roles, err := saml2aws.ExtractAwsRoles(data)
if err != nil {
return nil, errors.Wrap(err, "error parsing aws roles")
}
if len(roles) == 0 {
fmt.Println("No roles to assume")
fmt.Println("Please check you are permitted to assume roles for the AWS service")
os.Exit(1)
}
awsRoles, err := saml2aws.ParseAWSRoles(roles)
if err != nil {
return nil, errors.Wrap(err, "error parsing aws roles")
}
return resolveRole(awsRoles, samlAssertion, account)
}
func resolveRole(awsRoles []*saml2aws.AWSRole, samlAssertion string, account *cfg.IDPAccount) (*saml2aws.AWSRole, error) {
var role = new(saml2aws.AWSRole)
if len(awsRoles) == 1 {
if account.RoleARN != "" {
return saml2aws.LocateRole(awsRoles, account.RoleARN)
}
return awsRoles[0], nil
} else if len(awsRoles) == 0 {
return nil, errors.New("no roles available")
}
awsAccounts, err := saml2aws.ParseAWSAccounts(samlAssertion)
if err != nil {
return nil, errors.Wrap(err, "error parsing aws role accounts")
}
if len(awsAccounts) == 0 {
return nil, errors.New("no accounts available")
}
saml2aws.AssignPrincipals(awsRoles, awsAccounts)
if account.RoleARN != "" {
return saml2aws.LocateRole(awsRoles, account.RoleARN)
}
for {
role, err = saml2aws.PromptForAWSRoleSelection(awsAccounts)
if err == nil {
break
}
fmt.Println("error selecting role, try again")
}
return role, nil
}
func loginToStsUsingRole(account *cfg.IDPAccount, role *saml2aws.AWSRole, samlAssertion string) (*awsconfig.AWSCredentials, error) {
sess, err := session.NewSession()
if err != nil {
return nil, errors.Wrap(err, "failed to create session")
}
svc := sts.New(sess)
params := &sts.AssumeRoleWithSAMLInput{
PrincipalArn: aws.String(role.PrincipalARN), // Required
RoleArn: aws.String(role.RoleARN), // Required
SAMLAssertion: aws.String(samlAssertion), // Required
DurationSeconds: aws.Int64(int64(account.SessionDuration)),
}
fmt.Println("Requesting AWS credentials using SAML assertion")
resp, err := svc.AssumeRoleWithSAML(params)
if err != nil {
return nil, errors.Wrap(err, "error retrieving STS credentials using SAML")
}
return &awsconfig.AWSCredentials{
AWSAccessKey: aws.StringValue(resp.Credentials.AccessKeyId),
AWSSecretKey: aws.StringValue(resp.Credentials.SecretAccessKey),
AWSSessionToken: aws.StringValue(resp.Credentials.SessionToken),
AWSSecurityToken: aws.StringValue(resp.Credentials.SessionToken),
PrincipalARN: aws.StringValue(resp.AssumedRoleUser.Arn),
Expires: resp.Credentials.Expiration.Local(),
}, nil
}
func saveCredentials(awsCreds *awsconfig.AWSCredentials, sharedCreds *awsconfig.CredentialsProvider) error {
err := sharedCreds.Save(awsCreds)
if err != nil {
return errors.Wrap(err, "error saving credentials")
}
fmt.Println("Logged in as:", awsCreds.PrincipalARN)
fmt.Println("")
fmt.Println("Your new access key pair has been stored in the AWS configuration")
fmt.Printf("Note that it will expire at %v\n", awsCreds.Expires)
fmt.Println("To use this credential, call the AWS CLI with the --profile option (e.g. aws --profile", sharedCreds.Profile, "ec2 describe-instances).")
return nil
}