-
Notifications
You must be signed in to change notification settings - Fork 3
/
aws.go
140 lines (111 loc) · 3.28 KB
/
aws.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
"github.com/aws/aws-sdk-go/aws"
awscreds "github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/pkg/errors"
)
type result struct {
Version int
sts.Credentials
}
func (this result) Expiry() *time.Time {
return this.Expiration
}
func fetchAWSCredentials(arn, token, sessionName string) (*result, error) {
input := sts.AssumeRoleWithWebIdentityInput{
RoleArn: &arn,
RoleSessionName: &sessionName,
WebIdentityToken: &token,
}
sess, err := session.NewSession()
if err != nil {
return nil, errors.Wrap(err, "error creating aws session")
}
svc := sts.New(sess)
output, err := svc.AssumeRoleWithWebIdentity(&input)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("error assuming role %q with web identity", arn))
}
result := result{
Version: 1,
Credentials: *output.Credentials,
}
return &result, nil
}
func assumeRole(arn, sessionName string, credentials *result) (*result, error) {
input := sts.AssumeRoleInput{
RoleArn: &arn,
RoleSessionName: &sessionName,
}
sess, err := session.NewSession(
aws.NewConfig().WithCredentials(
awscreds.NewStaticCredentials(
*credentials.Credentials.AccessKeyId,
*credentials.Credentials.SecretAccessKey,
*credentials.Credentials.SessionToken,
)))
if err != nil {
return nil, errors.Wrap(err, "error creating aws session")
}
svc := sts.New(sess)
output, err := svc.AssumeRole(&input)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("error assuming role %q", arn))
}
result := result{
Version: 1,
Credentials: *output.Credentials,
}
return &result, nil
}
type signinSession struct {
SessionId string `json:"sessionId"`
SessionKey string `json:"sessionKey"`
SessionToken string `json:"sessionToken"`
}
type signinToken struct {
SigninToken string
}
// https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_enable-console-custom-url.html
func fetchSigninToken(result *result) error {
sessionData := signinSession{
SessionId: *result.Credentials.AccessKeyId,
SessionKey: *result.Credentials.SecretAccessKey,
SessionToken: *result.Credentials.SessionToken,
}
b, err := json.Marshal(sessionData)
if err != nil {
return errors.Wrap(err, "failed to encode signin json data")
}
tokenURL := fmt.Sprintf("https://signin.aws.amazon.com/federation?Action=getSigninToken&Session=%s", url.QueryEscape(string(b)))
resp, err := http.DefaultClient.Get(tokenURL)
if err != nil {
return errors.Wrap(err, "failed to fetch signin token for credentials")
} else if resp.StatusCode != 200 {
return fmt.Errorf("getSigninToken returned %d instead of 200", resp.StatusCode)
}
buffer := new(bytes.Buffer)
buffer.ReadFrom(resp.Body)
token := signinToken{}
err = json.Unmarshal(buffer.Bytes(), &token)
if err != nil {
return errors.Wrap(err, "failed to decode getSigninToken response")
}
destination := "https://console.aws.amazon.com/"
loginUrl := fmt.Sprintf("https://signin.aws.amazon.com/federation?Action=login&Destination=%s&SigninToken=%s",
url.QueryEscape(destination),
token.SigninToken,
)
if err := openInBrowser(loginUrl); err != nil {
return err
}
return nil
}