-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsession.go
211 lines (179 loc) · 4.93 KB
/
session.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
package alks
import (
"encoding/json"
"fmt"
"log"
"strings"
"time"
)
// SessionRequest is used to represent a new STS session request.
type SessionRequest struct {
SessionDuration int `json:"sessionTime"`
}
// SessionResponse is used to represent a new STS session.
type SessionResponse struct {
BaseResponse
AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"`
SessionToken string `json:"sessionToken"`
SessionDuration int `json:"sessionDuration"`
Expires time.Time `json:"expires"`
}
// SkypieaAccount is used to represent Skypiea data
type SkypieaAccount struct {
Account string `json:"Account"`
Alias string `json:"alias"`
Label string `json:"label"`
}
// AccountRole is used to represent an ALKS account and role combination
type AccountRole struct {
Account string `json:"account"`
Role string `json:"role"`
IamActive bool `json:"iamKeyActive"`
SkypieaAccount SkypieaAccount `json:"skypieaAccount"`
}
// AccountsResponseInt is used internally to represent a collection of ALKS accounts
type AccountsResponseInt struct {
BaseResponse
Accounts map[string][]AccountRole `json:"accountListRole"`
}
// AccountsResponse is used to represent a collection of ALKS accounts
type AccountsResponse struct {
Accounts []AccountRole `json:"accountListRole"`
}
// GetAccounts return a list of AccountRoles for an AWS account
func (c *Client) GetAccounts() (*AccountsResponse, *AlksError) {
log.Printf("[INFO] Requesting available accounts from ALKS")
b, err := json.Marshal(c.Credentials)
if err != nil {
return nil, &AlksError{
StatusCode: 0,
RequestId: "",
Err: fmt.Errorf("Error encoding account request JSON: %s", err),
}
}
req, err := c.NewRequest(b, "POST", "/getAccounts/")
if err != nil {
return nil, &AlksError{
StatusCode: 0,
RequestId: "",
Err: err,
}
}
resp, err := c.http.Do(req)
if err != nil {
return nil, &AlksError{
StatusCode: 0,
RequestId: "",
Err: err,
}
}
_accts := new(AccountsResponseInt)
err = decodeBody(resp, &_accts)
reqID := GetRequestID(resp)
if err != nil {
return nil, &AlksError{
StatusCode: resp.StatusCode,
RequestId: reqID,
Err: fmt.Errorf("Error parsing get accounts response: %s", err),
}
}
if _accts.RequestFailed() {
return nil, &AlksError{
StatusCode: resp.StatusCode,
RequestId: _accts.BaseResponse.RequestID,
Err: fmt.Errorf("Error getting accounts : %s", strings.Join(_accts.GetErrors(), ", ")),
}
}
accts := new(AccountsResponse)
for k, v := range _accts.Accounts {
v[0].Account = k
accts.Accounts = append(accts.Accounts, v[0])
}
return accts, nil
}
// CreateSession will create a new STS session on AWS. If no error is
// returned then you will receive a SessionResponse object representing
// your STS session.
func (c *Client) CreateSession(sessionDuration int, useIAM bool) (*SessionResponse, *AlksError) {
log.Printf("[INFO] Creating %v hr session", sessionDuration)
var found = false
durations, err := c.Durations()
if err != nil {
return nil, &AlksError{
StatusCode: 0,
RequestId: "",
Err: fmt.Errorf("Error fetching allowable durations from ALKS: %s", err),
}
}
for _, v := range durations {
if sessionDuration == v {
found = true
}
}
if !found {
return nil, &AlksError{
StatusCode: 0,
RequestId: "",
Err: fmt.Errorf("Unsupported session duration"),
}
}
session := SessionRequest{sessionDuration}
b, err := json.Marshal(struct {
SessionRequest
AccountDetails
}{session, c.AccountDetails})
if err != nil {
return nil, &AlksError{
StatusCode: 0,
RequestId: "",
Err: fmt.Errorf("Error encoding session create JSON: %s", err),
}
}
var endpoint = "/getKeys/"
if useIAM {
endpoint = "/getIAMKeys/"
}
req, err := c.NewRequest(b, "POST", endpoint)
if err != nil {
return nil, &AlksError{
StatusCode: 0,
RequestId: "",
Err: err,
}
}
resp, httpErr := c.http.Do(req)
if httpErr != nil {
return nil, &AlksError{
StatusCode: resp.StatusCode,
RequestId: "",
Err: err,
}
}
sr := new(SessionResponse)
err = decodeBody(resp, &sr)
if err != nil {
if reqID := GetRequestID(resp); reqID != "" {
return nil, &AlksError{
StatusCode: resp.StatusCode,
RequestId: reqID,
Err: fmt.Errorf("Error parsing session create response: %s", err),
}
}
return nil, &AlksError{
StatusCode: resp.StatusCode,
RequestId: "",
Err: fmt.Errorf("Error parsing session create response: %s", err),
}
}
if sr.RequestFailed() {
return nil, &AlksError{
StatusCode: resp.StatusCode,
RequestId: sr.BaseResponse.RequestID,
Err: fmt.Errorf("Error creating session: %s", strings.Join(sr.GetErrors(), ", ")),
}
}
sr.Expires = time.Now().Local().Add(time.Hour * time.Duration(sessionDuration))
sr.SessionDuration = sessionDuration
return sr, nil
}