This repository was archived by the owner on Apr 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsecret_programmatic_api_keys.go
More file actions
304 lines (253 loc) · 8.57 KB
/
secret_programmatic_api_keys.go
File metadata and controls
304 lines (253 loc) · 8.57 KB
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
package mongodbatlas
import (
"context"
"errors"
"fmt"
"net/http"
"time"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
"github.com/mitchellh/mapstructure"
"github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
)
func (b *Backend) programmaticAPIKeys() *framework.Secret {
return &framework.Secret{
Type: programmaticAPIKey,
Fields: map[string]*framework.FieldSchema{
"public_key": {
Type: framework.TypeString,
Description: "Programmatic API Key Public Key",
},
"private_key": {
Type: framework.TypeString,
Description: "Programmatic API Key Private Key",
},
},
Renew: b.programmaticAPIKeysRenew,
Revoke: b.programmaticAPIKeyRevoke,
}
}
func (b *Backend) programmaticAPIKeyCreate(ctx context.Context, s logical.Storage, displayName string, cred *atlasCredentialEntry) (*logical.Response, error) {
apiKeyDescription, err := genUsername(displayName)
if err != nil {
return nil, errwrap.Wrapf("error generating username: {{err}}", err)
}
client, err := b.clientMongo(ctx, s)
if err != nil {
return logical.ErrorResponse(err.Error()), nil
}
walID, err := framework.PutWAL(ctx, s, programmaticAPIKey, &walEntry{
UserName: apiKeyDescription,
})
if err != nil {
return nil, errwrap.Wrapf("error writing WAL entry: {{err}}", err)
}
var key *mongodbatlas.APIKey
switch {
case isOrgKey(cred.OrganizationID, cred.ProjectID):
key, err = createOrgKey(ctx, client, apiKeyDescription, cred)
case isProjectKey(cred.OrganizationID, cred.ProjectID):
key, err = createProjectAPIKey(ctx, client, apiKeyDescription, cred)
case isAssignedToProject(cred.OrganizationID, cred.ProjectID):
key, err = createAndAssignKey(ctx, client, apiKeyDescription, cred)
}
if err != nil {
if walErr := framework.DeleteWAL(ctx, s, walID); walErr != nil {
dbUserErr := errwrap.Wrapf("error creating programmaticAPIKey: {{err}}", err)
return nil, errwrap.Wrap(errwrap.Wrapf("failed to delete WAL entry: {{err}}", walErr), dbUserErr)
}
return logical.ErrorResponse("Error creating programmatic api key: %s", err), err
}
if key == nil {
return nil, errors.New("error creating credential")
}
if err := framework.DeleteWAL(ctx, s, walID); err != nil {
return nil, errwrap.Wrapf("failed to commit WAL entry: {{err}}", err)
}
resp := b.Secret(programmaticAPIKey).Response(map[string]interface{}{
"public_key": key.PublicKey,
"private_key": key.PrivateKey,
"description": apiKeyDescription,
}, map[string]interface{}{
"programmatic_api_key_id": key.ID,
"project_id": cred.ProjectID,
"organization_id": cred.OrganizationID,
})
defaultLease, maxLease := b.getDefaultAndMaxLease()
// If defined, credential TTL overrides default lease configuration
if cred.TTL > 0 {
defaultLease = cred.TTL
}
if cred.MaxTTL > 0 {
maxLease = cred.MaxTTL
}
resp.Secret.TTL = defaultLease
resp.Secret.MaxTTL = maxLease
return resp, nil
}
func createOrgKey(ctx context.Context, client *mongodbatlas.Client, apiKeyDescription string, credentialEntry *atlasCredentialEntry) (*mongodbatlas.APIKey, error) {
key, _, err := client.APIKeys.Create(ctx, credentialEntry.OrganizationID,
&mongodbatlas.APIKeyInput{
Desc: apiKeyDescription,
Roles: credentialEntry.Roles,
})
if err != nil {
return nil, err
}
if err := addWhitelistEntry(ctx, client, credentialEntry.OrganizationID, key.ID, credentialEntry); err != nil {
return nil, err
}
return key, nil
}
func createProjectAPIKey(ctx context.Context, client *mongodbatlas.Client, apiKeyDescription string, credentialEntry *atlasCredentialEntry) (*mongodbatlas.APIKey, error) {
key, _, err := client.ProjectAPIKeys.Create(ctx, credentialEntry.ProjectID,
&mongodbatlas.APIKeyInput{
Desc: apiKeyDescription,
Roles: credentialEntry.Roles,
})
return key, err
}
func createAndAssignKey(ctx context.Context, client *mongodbatlas.Client, apiKeyDescription string, credentialEntry *atlasCredentialEntry) (*mongodbatlas.APIKey, error) {
key, err := createOrgKey(ctx, client, apiKeyDescription, credentialEntry)
if err != nil {
return nil, err
}
if _, err := client.ProjectAPIKeys.Assign(ctx, credentialEntry.ProjectID, key.ID, &mongodbatlas.AssignAPIKey{
Roles: credentialEntry.ProjectRoles,
}); err != nil {
return nil, err
}
return key, nil
}
func addWhitelistEntry(ctx context.Context, client *mongodbatlas.Client, orgID string, keyID string, cred *atlasCredentialEntry) error {
var entries []*mongodbatlas.WhitelistAPIKeysReq
for _, cidrBlock := range cred.CIDRBlocks {
cidr := &mongodbatlas.WhitelistAPIKeysReq{
CidrBlock: cidrBlock,
}
entries = append(entries, cidr)
}
for _, ipAddress := range cred.IPAddresses {
ip := &mongodbatlas.WhitelistAPIKeysReq{
IPAddress: ipAddress,
}
entries = append(entries, ip)
}
if entries != nil {
_, _, err := client.WhitelistAPIKeys.Create(ctx, orgID, keyID, entries)
return err
}
return nil
}
func (b *Backend) programmaticAPIKeyRevoke(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
programmaticAPIKeyIDRaw, ok := req.Secret.InternalData["programmatic_api_key_id"]
if !ok {
return nil, fmt.Errorf("secret is missing programmatic api key id internal data")
}
programmaticAPIKeyID, ok := programmaticAPIKeyIDRaw.(string)
if !ok {
return nil, fmt.Errorf("secret is missing programmatic api key id internal data")
}
organizationID := ""
organizationIDRaw, ok := req.Secret.InternalData["organization_id"]
if ok {
organizationID, ok = organizationIDRaw.(string)
if !ok {
return nil, fmt.Errorf("secret is missing organization id internal data")
}
}
projectID := ""
projectIDRaw, ok := req.Secret.InternalData["project_id"]
if ok {
projectID, ok = projectIDRaw.(string)
if !ok {
return nil, fmt.Errorf("secret is missing project_id internal data")
}
}
var data = map[string]interface{}{
"organization_id": organizationID,
"programmatic_api_key_id": programmaticAPIKeyID,
"project_id": projectID,
}
// Use the user rollback mechanism to delete this database_user
if err := b.pathProgrammaticAPIKeyRollback(ctx, req, programmaticAPIKey, data); err != nil {
return nil, err
}
return nil, nil
}
func (b *Backend) pathProgrammaticAPIKeyRollback(ctx context.Context, req *logical.Request, _kind string, data interface{}) error {
var entry walEntry
if err := mapstructure.Decode(data, &entry); err != nil {
return err
}
// Get the client
client, err := b.clientMongo(ctx, req.Storage)
if err != nil {
return nil
}
switch {
case isOrgKey(entry.OrganizationID, entry.ProjectID):
// check if the user exists or not
_, res, err := client.APIKeys.Get(ctx, entry.OrganizationID, entry.ProgrammaticAPIKeyID)
// if the user is gone, move along
if err != nil {
if res != nil && res.StatusCode == http.StatusNotFound {
return nil
}
return err
}
// now, delete the api key
res, err = client.APIKeys.Delete(ctx, entry.OrganizationID, entry.ProgrammaticAPIKeyID)
if err != nil {
if res != nil && res.StatusCode == http.StatusNotFound {
return nil
}
return err
}
case isProjectKey(entry.OrganizationID, entry.ProjectID):
// now, delete the user
res, err := client.ProjectAPIKeys.Unassign(ctx, entry.ProjectID, entry.ProgrammaticAPIKeyID)
if err != nil {
if res != nil && res.StatusCode == http.StatusNotFound {
return nil
}
return err
}
case isAssignedToProject(entry.OrganizationID, entry.ProjectID):
// check if the user exists or not
_, res, err := client.APIKeys.Get(ctx, entry.OrganizationID, entry.ProgrammaticAPIKeyID)
// if the user is gone, move along
if err != nil {
if res != nil && res.StatusCode == http.StatusNotFound {
return nil
}
return err
}
// now, delete the api key
res, err = client.APIKeys.Delete(ctx, entry.OrganizationID, entry.ProgrammaticAPIKeyID)
if err != nil {
if res != nil && res.StatusCode == http.StatusNotFound {
return nil
}
return err
}
}
return nil
}
func (b *Backend) programmaticAPIKeysRenew(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
// Get the lease (if any)
defaultLease, maxLease := b.getDefaultAndMaxLease()
resp := &logical.Response{Secret: req.Secret}
resp.Secret.TTL = defaultLease
resp.Secret.MaxTTL = maxLease
return resp, nil
}
func (b *Backend) getDefaultAndMaxLease() (time.Duration, time.Duration) {
maxLease := b.system.MaxLeaseTTL()
defaultLease := b.system.DefaultLeaseTTL()
if defaultLease > maxLease {
maxLease = defaultLease
}
return defaultLease, maxLease
}