forked from segmentio/aws-okta
/
login.go
174 lines (145 loc) · 4.08 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
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"time"
"github.com/99designs/keyring"
analytics "github.com/segmentio/analytics-go"
"github.com/segmentio/aws-okta/lib"
"github.com/skratchdot/open-golang/open"
"github.com/spf13/cobra"
)
// loginCmd represents the login command
var loginCmd = &cobra.Command{
Use: "login <profile>",
Short: "login will authenticate you through okta and allow you to access your AWS environment through a browser",
RunE: loginRun,
PreRun: loginPre,
}
// Stdout is the bool for -stdout
var Stdout bool
func init() {
RootCmd.AddCommand(loginCmd)
loginCmd.Flags().BoolVarP(&Stdout, "stdout", "", false, "Print login URL to stdout instead of opening in default browser")
loginCmd.Flags().DurationVarP(&sessionTTL, "session-ttl", "t", time.Hour, "Expiration time for okta role session")
loginCmd.Flags().DurationVarP(&assumeRoleTTL, "assume-role-ttl", "a", time.Hour, "Expiration time for assumed role")
}
func loginPre(cmd *cobra.Command, args []string) {
if err := loadDurationFlagFromEnv(cmd, "session-ttl", "AWS_SESSION_TTL", &sessionTTL); err != nil {
fmt.Fprintln(os.Stderr, "warning: failed to parse duration from AWS_SESSION_TTL")
}
if err := loadDurationFlagFromEnv(cmd, "assume-role-ttl", "AWS_ASSUME_ROLE_TTL", &assumeRoleTTL); err != nil {
fmt.Fprintln(os.Stderr, "warning: failed to parse duration from AWS_ASSUME_ROLE_TTL")
}
}
func loginRun(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return ErrTooFewArguments
}
if len(args) > 1 {
return ErrTooManyArguments
}
profile := args[0]
config, err := lib.NewConfigFromEnv()
if err != nil {
return err
}
profiles, err := config.Parse()
if err != nil {
return err
}
if _, ok := profiles[profile]; !ok {
return fmt.Errorf("Profile '%s' not found in your aws config", profile)
}
opts := lib.ProviderOptions{
Profiles: profiles,
SessionDuration: sessionTTL,
AssumeRoleDuration: assumeRoleTTL,
}
var allowedBackends []keyring.BackendType
if backend != "" {
allowedBackends = append(allowedBackends, keyring.BackendType(backend))
}
kr, err := lib.OpenKeyring(allowedBackends)
if err != nil {
return err
}
if analyticsEnabled && analyticsClient != nil {
analyticsClient.Enqueue(analytics.Track{
UserId: username,
Event: "Ran Command",
Properties: analytics.NewProperties().
Set("backend", backend).
Set("aws-okta-version", version).
Set("profile", profile).
Set("command", "login"),
})
}
p, err := lib.NewProvider(kr, profile, opts)
if err != nil {
return err
}
creds, err := p.Retrieve()
if err != nil {
return err
}
jsonBytes, err := json.Marshal(map[string]string{
"sessionId": creds.AccessKeyID,
"sessionKey": creds.SecretAccessKey,
"sessionToken": creds.SessionToken,
})
if err != nil {
return err
}
req, err := http.NewRequest("GET", "https://signin.aws.amazon.com/federation", nil)
if err != nil {
return err
}
q := req.URL.Query()
q.Add("Action", "getSigninToken")
q.Add("Session", string(jsonBytes))
req.URL.RawQuery = q.Encode()
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Call to getSigninToken failed with %v", resp.Status)
}
var respParsed map[string]string
if err = json.Unmarshal([]byte(body), &respParsed); err != nil {
return err
}
signinToken, ok := respParsed["SigninToken"]
if !ok {
return err
}
destination := "https://console.aws.amazon.com/"
prof := profiles[profile]
if region, ok := prof["region"]; ok {
destination = fmt.Sprintf(
"https://%s.console.aws.amazon.com/console/home?region=%s",
region, region,
)
}
loginURL := fmt.Sprintf(
"https://signin.aws.amazon.com/federation?Action=login&Issuer=aws-okta&Destination=%s&SigninToken=%s",
url.QueryEscape(destination),
url.QueryEscape(signinToken),
)
if Stdout {
fmt.Println(loginURL)
} else if err = open.Run(loginURL); err != nil {
return err
}
return nil
}