-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathiam_helper.go
464 lines (388 loc) · 14.2 KB
/
iam_helper.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package ibmcloudsecrets
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/vault/sdk/logical"
)
// A struct to contain information from IBM Cloud tokens that we want to include in Vault token metadata
type tokenInfo struct {
Subject string
Expiry time.Time
}
// IAM API paths
const (
accessGroupMembers = "/v2/groups/%s/members"
serviceIDs = "/v1/serviceids"
serviceIDDetails = serviceIDs + "/%s"
getAccessGroup = "/v2/groups/%s"
v1APIKeys = "/v1/apikeys"
v1APIKeysID = v1APIKeys + "/%s"
v1APIKeyDetails = "/v1/apikeys/details"
identityToken = "/identity/token"
// IAM OIDC provider paths
authorizationEndpoint = "/identity/authorize"
jwksURI = "/identity/keys"
)
type accountIDDeserializer struct {
Account string `json:"account_id"`
}
type serviceIDv1Response struct {
ID string `json:"id"`
IAMID string `json:"iam_id"`
Account string `json:"account_id"`
}
type AddGroupMembersRequest struct {
Members []map[string]string `json:"members"`
}
type AddGroupMembersResponse struct {
Members []AddGroupMembersResponseMembers `json:"members"`
}
type AddGroupMembersResponseMembers struct {
IAMID string `json:"iam_id"`
Status int `json:"status_code"`
}
type APIKeyV1Response struct {
APIKey string `json:"apikey"`
ID string `json:"id"`
}
type APIKeyDetailsResponse struct {
ID string `json:"id"`
IAMID string `json:"iam_id"`
AccountID string `json:"account_id"`
}
type iamHelper interface {
ObtainToken(apiKey string) (string, error)
VerifyToken(ctx context.Context, token string) (*tokenInfo, *logical.Response)
VerifyAccessGroupExists(iamToken, accessGroup, accountID string) *logical.Response
CheckServiceIDAccount(iamToken, identifier, accountID string) (*serviceIDv1Response, *logical.Response)
CreateServiceID(iamToken, accountID, roleName string) (iamID, identifier string, err error)
DeleteServiceID(iamToken, identifier string) error
AddServiceIDToAccessGroup(iamToken, iamID, group string) error
CreateAPIKey(iamToken, IAMid, accountID, name, description string) (*APIKeyV1Response, error)
DeleteAPIKey(iamToken, apiKeyID string) error
GetAPIKeyDetails(iamToken, apiKeyValue string) (*APIKeyDetailsResponse, error)
Init(iamEndpoint string)
Cleanup()
}
type ibmCloudHelper struct {
providerLock sync.RWMutex
provider *oidc.Provider
providerCtx context.Context
providerCtxCancel context.CancelFunc
httpClient *http.Client
iamEndpoint string
}
func (h *ibmCloudHelper) Init(iamEndpoint string) {
h.providerCtx, h.providerCtxCancel = context.WithCancel(context.Background())
h.httpClient = cleanhttp.DefaultPooledClient()
h.iamEndpoint = iamEndpoint
}
func (h *ibmCloudHelper) Cleanup() {
h.providerLock.Lock()
if h.providerCtxCancel != nil {
h.providerCtxCancel()
}
h.providerLock.Unlock()
}
func (h *ibmCloudHelper) getProvider() *oidc.Provider {
h.providerLock.RLock()
unlockFunc := h.providerLock.RUnlock
defer func() { unlockFunc() }()
if h.provider != nil {
return h.provider
}
h.providerLock.RUnlock()
h.providerLock.Lock()
unlockFunc = h.providerLock.Unlock
if h.provider != nil {
return h.provider
}
providerCtx := h.providerCtx
providerConfig := oidc.ProviderConfig{
IssuerURL: openIDIssuer,
AuthURL: h.getURL(authorizationEndpoint),
TokenURL: h.getURL(identityToken),
JWKSURL: h.getURL(jwksURI),
}
provider := providerConfig.NewProvider(providerCtx)
h.provider = provider
return provider
}
/*
*
Obtain an IAM token by way of an API Key
*/
func (h *ibmCloudHelper) ObtainToken(apiKey string) (string, error) {
data := url.Values{}
data.Set("grant_type", "urn:ibm:params:oauth:grant-type:apikey")
data.Set("apikey", apiKey)
data.Set("response_type", "cloud_iam")
req, err := http.NewRequest(http.MethodPost, h.getURL(identityToken), strings.NewReader(data.Encode()))
if err != nil {
return "", errwrap.Wrapf("Error creating obtain token request: {{err}}", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept", "application/json")
resp, err := h.httpClient.Do(req)
if err != nil {
return "", errwrap.Wrapf("Error obtaining token: {{err}}", err)
}
defer closeResponse(resp)
var result map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return "", errwrap.Wrapf("Error decoding the obtained token: {{err}}", err)
} else if _, ok := result["errorMessage"]; ok {
return "", fmt.Errorf("error message obtaining token: %s", result["errorMessage"])
}
return result["access_token"].(string), nil
}
/*
*
Verifies an IBM Cloud IAM token. If successful, it will return a tokenInfo
with relevant items contained in the token.
*/
func (h *ibmCloudHelper) VerifyToken(ctx context.Context, token string) (*tokenInfo, *logical.Response) {
// verify the token
provider := h.getProvider()
oidcConfig := &oidc.Config{
SkipClientIDCheck: true,
}
verifier := provider.Verifier(oidcConfig)
idToken, err := verifier.Verify(ctx, token)
if err != nil {
return nil, logical.ErrorResponse("an error occurred verifying the token %s", err)
}
return &tokenInfo{
Subject: idToken.Subject,
Expiry: idToken.Expiry,
}, nil
}
func (h *ibmCloudHelper) VerifyAccessGroupExists(iamToken, accessGroup, accountID string) *logical.Response {
r, err := http.NewRequest(http.MethodGet, h.getURL(getAccessGroup, accessGroup), nil)
if err != nil {
return logical.ErrorResponse("failed creating http request: %s", err)
}
r.Header.Set("Authorization", "Bearer "+iamToken)
r.Header.Set("Content-Type", "application/json")
r.Header.Set("Accept", "application/json")
body, httpStatus, err := httpRequest(h.httpClient, r)
if err != nil {
return logical.ErrorResponse("error with http request: %s", err)
}
if httpStatus == 404 {
return logical.ErrorResponse("the access group %s does not exist", accessGroup)
}
if httpStatus != 200 {
return logical.ErrorResponse("unexpected http status code from access group API: %v with response %v", httpStatus, string(body))
}
groupInfo := new(accountIDDeserializer)
if err := json.Unmarshal(body, &groupInfo); err != nil {
return logical.ErrorResponse("error reading access group API response: %s", err)
}
if accountID != groupInfo.Account {
return logical.ErrorResponse("the access group %s, was created in account %s which does not match the configured account %s", accessGroup, groupInfo.Account, accountID)
}
return nil
}
func (h *ibmCloudHelper) CreateServiceID(iamToken, accountID, roleName string) (iamID, identifier string, err error) {
requestBody, err := json.Marshal(map[string]string{
"name": fmt.Sprintf("vault-generated-%s", roleName),
"account_id": accountID,
"description": fmt.Sprintf("Generated by Vault's secret engine for IBM Cloud credentials using Vault role %s.", roleName),
})
if err != nil {
return "", "", errwrap.Wrapf("failed marshalling the request for creating a service ID: {{err}}", err)
}
r, err := http.NewRequest(http.MethodPost, h.getURL(serviceIDs), bytes.NewBuffer(requestBody))
if err != nil {
return "", "", errwrap.Wrapf("failed creating http request: {{err}}", err)
}
r.Header.Set("Authorization", "Bearer "+iamToken)
r.Header.Set("Content-Type", "application/json")
r.Header.Set("Accept", "application/json")
body, httpStatus, err := httpRequest(h.httpClient, r)
if err != nil {
return "", "", err
}
if httpStatus != 201 {
return "", "", fmt.Errorf("unexpected http status code: %v with response %v", httpStatus, string(body))
}
idInfo := new(serviceIDv1Response)
if err := json.Unmarshal(body, &idInfo); err != nil {
return "", "", err
}
return idInfo.IAMID, idInfo.ID, nil
}
// Checks the existence of a service ID and verifies that it is created in the passed in account.
// Returns the serviceIDv1Response struct with service ID information if successful, else returns an error logical.Response
func (h *ibmCloudHelper) CheckServiceIDAccount(iamToken, identifier, accountID string) (*serviceIDv1Response, *logical.Response) {
r, err := http.NewRequest(http.MethodGet, h.getURL(serviceIDDetails, identifier), nil)
if err != nil {
return nil, logical.ErrorResponse("failed creating http request: %s", err)
}
r.Header.Set("Authorization", "Bearer "+iamToken)
r.Header.Set("Content-Type", "application/json")
r.Header.Set("Accept", "application/json")
body, httpStatus, err := httpRequest(h.httpClient, r)
if err != nil {
return nil, logical.ErrorResponse("error with http request: %s", err)
}
if httpStatus == 404 {
return nil, logical.ErrorResponse("service ID %s does not exist", identifier)
}
if httpStatus != 200 {
return nil, logical.ErrorResponse("unexpected http status code: %v with response %v", httpStatus, string(body))
}
idInfo := new(serviceIDv1Response)
if err := json.Unmarshal(body, &idInfo); err != nil {
return nil, logical.ErrorResponse("error reading API response: %s", err)
}
if accountID != idInfo.Account {
return nil, logical.ErrorResponse("service ID account %s does not match the configured account %s", idInfo.Account, accountID)
}
return idInfo, nil
}
func (h *ibmCloudHelper) AddServiceIDToAccessGroup(iamToken string, iamID string, group string) error {
reqBody := new(AddGroupMembersRequest)
reqBody.Members = []map[string]string{{"iam_id": iamID, "type": "service"}}
requestBody, err := json.Marshal(reqBody)
if err != nil {
return errwrap.Wrapf("failed marshalling the request for adding a serviceID to access group: {{err}}", err)
}
r, err := http.NewRequest(http.MethodPut, h.getURL(accessGroupMembers, group), bytes.NewBuffer(requestBody))
if err != nil {
return errwrap.Wrapf("failed creating http request: {{err}}", err)
}
r.Header.Set("Authorization", iamToken)
r.Header.Set("Content-Type", "application/json")
r.Header.Set("Accept", "application/json")
body, httpStatus, err := httpRequest(h.httpClient, r)
if err != nil {
return err
}
if httpStatus != 207 {
return fmt.Errorf("unexpected http status code: %v with response %v", httpStatus, string(body))
}
resp := new(AddGroupMembersResponse)
if err := json.Unmarshal(body, &resp); err != nil {
return err
}
if len(resp.Members) == 0 {
return fmt.Errorf("no member sections found in response %v", string(body))
}
if resp.Members[0].Status != 200 {
return fmt.Errorf("error adding member to the access group. http status code: %v with response %v", resp.Members[0].Status, string(body))
}
return nil
}
func (h *ibmCloudHelper) CreateAPIKey(iamToken, IAMid, accountID, name, description string) (*APIKeyV1Response, error) {
requestBody, err := json.Marshal(map[string]interface{}{
"name": name,
"iam_id": IAMid,
"account_id": accountID,
"description": description,
"store_value": false,
})
if err != nil {
return nil, errwrap.Wrapf("failed marshalling the request for creating a service ID: {{err}}", err)
}
r, err := http.NewRequest(http.MethodPost, h.getURL(v1APIKeys), bytes.NewBuffer(requestBody))
if err != nil {
return nil, errwrap.Wrapf("failed creating http request: {{err}}", err)
}
r.Header.Set("Authorization", "Bearer "+iamToken)
r.Header.Set("Content-Type", "application/json")
r.Header.Set("Accept", "application/json")
body, httpStatus, err := httpRequest(h.httpClient, r)
if err != nil {
return nil, err
}
if httpStatus != 201 {
return nil, fmt.Errorf("unexpected http status code: %v with response %v", httpStatus, string(body))
}
keyInfo := new(APIKeyV1Response)
if err := json.Unmarshal(body, &keyInfo); err != nil {
return nil, err
}
if len(keyInfo.APIKey) == 0 {
return nil, fmt.Errorf("an empty API key was returned with code %v and response %v", httpStatus, string(body))
}
if len(keyInfo.ID) == 0 {
return nil, fmt.Errorf("API key with an empty ID was returned with code %v and response %v", httpStatus, string(body))
}
return keyInfo, nil
}
func (h *ibmCloudHelper) DeleteAPIKey(iamToken, apiKeyID string) error {
r, err := http.NewRequest(http.MethodDelete, h.getURL(v1APIKeysID, apiKeyID), nil)
if err != nil {
return errwrap.Wrapf("failed creating http request: {{err}}", err)
}
r.Header.Set("Authorization", "Bearer "+iamToken)
r.Header.Set("Content-Type", "application/json")
r.Header.Set("Accept", "application/json")
body, httpStatus, err := httpRequest(h.httpClient, r)
if err != nil {
return err
}
if httpStatus != 204 {
return fmt.Errorf("unexpected http status code: %v with response %v", httpStatus, string(body))
}
return nil
}
func (h *ibmCloudHelper) GetAPIKeyDetails(iamToken, apiKeyValue string) (*APIKeyDetailsResponse, error) {
r, err := http.NewRequest(http.MethodGet, h.getURL(v1APIKeyDetails), nil)
if err != nil {
return nil, errwrap.Wrapf("failed creating http request: {{err}}", err)
}
r.Header.Set("Authorization", "Bearer "+iamToken)
r.Header.Set("IAM-Apikey", apiKeyValue)
r.Header.Set("Content-Type", "application/json")
r.Header.Set("Accept", "application/json")
body, httpStatus, err := httpRequest(h.httpClient, r)
if err != nil {
return nil, err
}
keyDetails := new(APIKeyDetailsResponse)
if err := json.Unmarshal(body, &keyDetails); err != nil {
return nil, err
}
if httpStatus != 200 {
return nil, fmt.Errorf("unexpected http status code: %v with response %v", httpStatus, string(body))
}
return keyDetails, nil
}
func (h *ibmCloudHelper) DeleteServiceID(iamToken, identifier string) error {
r, err := http.NewRequest(http.MethodDelete, h.getURL(serviceIDDetails, identifier), nil)
if err != nil {
return errwrap.Wrapf("failed creating http request: {{err}}", err)
}
r.Header.Set("Authorization", "Bearer "+iamToken)
r.Header.Set("Content-Type", "application/json")
r.Header.Set("Accept", "application/json")
body, httpStatus, err := httpRequest(h.httpClient, r)
if err != nil {
return err
}
if httpStatus != 204 {
return fmt.Errorf("unexpected http status code: %v with response %v", httpStatus, string(body))
}
return nil
}
func (h *ibmCloudHelper) getURL(path string, pathReplacements ...string) string {
pathSubs := make([]interface{}, len(pathReplacements))
for i, v := range pathReplacements {
pathSubs[i] = v
}
return fmt.Sprintf("%s%s", h.iamEndpoint, fmt.Sprintf(path, pathSubs...))
}