-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathaccess_key.go
349 lines (282 loc) · 11 KB
/
access_key.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package cloudaccess
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"time"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/edgegriderr"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/session"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
type (
// GetAccessKeyStatusResponse contains response from GetAccessKeyStatus
GetAccessKeyStatusResponse struct {
AccessKey *KeyLink `json:"accessKey"`
AccessKeyVersion *KeyVersion `json:"accessKeyVersion"`
ProcessingStatus ProcessingType `json:"processingStatus"`
Request *RequestInformation `json:"request"`
RequestDate time.Time `json:"requestDate"`
RequestID int64 `json:"requestId"`
RequestedBy string `json:"requestedBy"`
}
// GetAccessKeyStatusRequest holds parameters for GetAccessKeyStatus
GetAccessKeyStatusRequest struct {
RequestID int64
}
// CreateAccessKeyRequest holds request body for CreateAccessKey
CreateAccessKeyRequest struct {
AccessKeyName string `json:"accessKeyName"`
AuthenticationMethod string `json:"authenticationMethod"`
ContractID string `json:"contractId"`
Credentials Credentials `json:"credentials"`
GroupID int64 `json:"groupId"`
NetworkConfiguration SecureNetwork `json:"networkConfiguration"`
}
// Credentials holds information used to sign API requests
Credentials struct {
CloudAccessKeyID string `json:"cloudAccessKeyId"`
CloudSecretAccessKey string `json:"cloudSecretAccessKey"`
}
// CreateAccessKeyResponse contains response from CreateAccessKey
CreateAccessKeyResponse struct {
RequestID int64 `json:"requestId,omitempty"`
RetryAfter int64 `json:"retryAfter,omitempty"`
Location string
}
// AccessKeyRequest holds parameters for GetAccessKey, UpdateAccessKey, DeleteAccessKey
AccessKeyRequest struct {
AccessKeyUID int64
}
// AccessKeyResponse contains response from ListAccessKeys
AccessKeyResponse struct {
AccessKeyUID int64 `json:"accessKeyUid"`
AccessKeyName string `json:"accessKeyName"`
AuthenticationMethod string `json:"authenticationMethod"`
NetworkConfiguration *SecureNetwork `json:"networkConfiguration"`
LatestVersion int64 `json:"latestVersion"`
Groups []Group `json:"groups"`
CreatedBy string `json:"createdBy"`
CreatedTime time.Time `json:"createdTime"`
}
// GetAccessKeyResponse contains response from GetAccessKey
GetAccessKeyResponse AccessKeyResponse
// Group is an object to which the access key is assigned
Group struct {
ContractIDs []string `json:"contractIds"`
GroupID int64 `json:"groupId"`
GroupName *string `json:"groupName"`
}
// ListAccessKeysRequest holds parameters for GetAccessKeys
ListAccessKeysRequest struct {
VersionGUID string
}
// ListAccessKeysResponse contains array of GetAccessKeyResponse
ListAccessKeysResponse struct {
AccessKeys []AccessKeyResponse `json:"accessKeys"`
}
// UpdateAccessKeyRequest holds request body for UpdateAccessKey
UpdateAccessKeyRequest struct {
AccessKeyName string `json:"accessKeyName,omitempty"`
}
// UpdateAccessKeyResponse contains response from UpdateAccessKey
UpdateAccessKeyResponse struct {
AccessKeyUID int64 `json:"accessKeyUid"`
AccessKeyName string `json:"accessKeyName"`
}
)
// Validate validates GetAccessKeyStatusRequest
func (r GetAccessKeyStatusRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"RequestID": validation.Validate(r.RequestID, validation.Required),
})
}
// Validate validates CreateAccessKeyRequest
func (r CreateAccessKeyRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"AccessKeyName": validation.Validate(r.AccessKeyName, validation.Required),
"AuthenticationMethod": validation.Validate(r.AuthenticationMethod, validation.Required),
"ContractID": validation.Validate(r.ContractID, validation.Required),
"CloudAccessKeyID": validation.Validate(r.Credentials.CloudAccessKeyID, validation.Required),
"CloudSecretAccessKey": validation.Validate(r.Credentials.CloudSecretAccessKey, validation.Required),
"GroupID": validation.Validate(r.GroupID, validation.Required),
"SecurityNetwork": validation.Validate(r.NetworkConfiguration.SecurityNetwork, validation.Required),
})
}
// Validate validates AccessKeyRequest
func (r AccessKeyRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"AccessKeyUID": validation.Validate(r.AccessKeyUID, validation.Required),
})
}
// Validate validates UpdateAccessKeyRequest
func (r UpdateAccessKeyRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"AccessKeyName": validation.Validate(r.AccessKeyName, validation.Required, validation.Length(1, 50)),
})
}
var (
// ErrGetAccessKeyStatus is returned when GetAccessKeyStatus fails
ErrGetAccessKeyStatus = errors.New("get the status of an access key")
// ErrCreateAccessKey is returned when CreateAccessKey fails
ErrCreateAccessKey = errors.New("create an access key")
// ErrGetAccessKey is returned when GetAccessKey fails
ErrGetAccessKey = errors.New("get an access key")
// ErrUpdateAccessKey is returned when UpdateAccessKey fails
ErrUpdateAccessKey = errors.New("update an access key")
// ErrDeleteAccessKey is returned when DeleteAccessKey fails
ErrDeleteAccessKey = errors.New("delete an access key")
)
func (c *cloudaccess) GetAccessKeyStatus(ctx context.Context, params GetAccessKeyStatusRequest) (*GetAccessKeyStatusResponse, error) {
logger := c.Log(ctx)
logger.Debug("GetAccessKeyStatus")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrGetAccessKeyStatus, ErrStructValidation, err)
}
getURL := fmt.Sprintf("/cam/v1/access-key-create-requests/%d", params.RequestID)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
if err != nil {
return nil, fmt.Errorf("failed to create GetAccessKeyStatus request: %w", err)
}
var result GetAccessKeyStatusResponse
resp, err := c.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("GetAccessKeyStatus request failed: %w", err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, c.Error(resp)
}
return &result, nil
}
func (c *cloudaccess) CreateAccessKey(ctx context.Context, accessKey CreateAccessKeyRequest) (*CreateAccessKeyResponse, error) {
logger := c.Log(ctx)
logger.Debug("CreateAccessKey")
if err := accessKey.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrCreateAccessKey, ErrStructValidation, err)
}
uri, err := url.Parse("/cam/v1/access-keys")
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("failed to create CreateAccessKey request: %w", err)
}
var result CreateAccessKeyResponse
resp, err := c.Exec(req, &result, accessKey)
if err != nil {
return nil, fmt.Errorf("CreateAccessKey request failed: %w", err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusAccepted {
return nil, c.Error(resp)
}
result.Location = resp.Header.Get("Location")
return &result, nil
}
func (c *cloudaccess) GetAccessKey(ctx context.Context, params AccessKeyRequest) (*GetAccessKeyResponse, error) {
logger := c.Log(ctx)
logger.Debug("GetAccessKey")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrGetAccessKey, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/cam/v1/access-keys/%d", params.AccessKeyUID))
if err != nil {
return nil, fmt.Errorf("failed to parse url: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("failed to create GetAccessKey request: %w", err)
}
var result GetAccessKeyResponse
resp, err := c.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("GetAccessKey request failed: %w", err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, c.Error(resp)
}
return &result, nil
}
func (c *cloudaccess) ListAccessKeys(ctx context.Context, params ListAccessKeysRequest) (*ListAccessKeysResponse, error) {
logger := c.Log(ctx)
logger.Debug("ListAccessKeys")
uri, err := url.Parse("/cam/v1/access-keys")
if err != nil {
return nil, fmt.Errorf("failed to parse url: %w", err)
}
q := uri.Query()
if params.VersionGUID != "" {
q.Add("versionGuid", params.VersionGUID)
}
uri.RawQuery = q.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("failed to create ListAccessKeys request: %w", err)
}
var result ListAccessKeysResponse
resp, err := c.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("ListAccessKeys request failed: %w", err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, c.Error(resp)
}
return &result, nil
}
func (c *cloudaccess) DeleteAccessKey(ctx context.Context, params AccessKeyRequest) error {
logger := c.Log(ctx)
logger.Debug("DeleteAccessKey")
if err := params.Validate(); err != nil {
return fmt.Errorf("%s: %w: %s", ErrDeleteAccessKey, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/cam/v1/access-keys/%d", params.AccessKeyUID))
if err != nil {
return fmt.Errorf("failed to parse url: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, uri.String(), nil)
if err != nil {
return fmt.Errorf("failed to create DeleteAccessKey request: %s", err.Error())
}
resp, err := c.Exec(req, nil)
if err != nil {
return fmt.Errorf("DeleteAccessKey request failed: %s", err.Error())
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusNoContent {
return c.Error(resp)
}
return nil
}
func (c *cloudaccess) UpdateAccessKey(ctx context.Context, accessKey UpdateAccessKeyRequest, params AccessKeyRequest) (*UpdateAccessKeyResponse, error) {
logger := c.Log(ctx)
logger.Debug("UpdateAccessKey")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrUpdateAccessKey, ErrStructValidation, err)
}
if err := accessKey.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrUpdateAccessKey, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/cam/v1/access-keys/%d", params.AccessKeyUID))
if err != nil {
return nil, fmt.Errorf("failed to parse url: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("failed to create UpdateAccessKey request: %w", err)
}
var result UpdateAccessKeyResponse
resp, err := c.Exec(req, &result, accessKey)
if err != nil {
return nil, fmt.Errorf("UpdateAccessKey request failed: %w", err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, c.Error(resp)
}
return &result, nil
}