-
Notifications
You must be signed in to change notification settings - Fork 19
/
path_service_principal.go
364 lines (303 loc) · 11 KB
/
path_service_principal.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package azuresecrets
import (
"context"
"errors"
"fmt"
"time"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/helper/locksutil"
"github.com/hashicorp/vault/sdk/logical"
)
const (
SecretTypeSP = "service_principal"
SecretTypeStaticSP = "static_service_principal"
)
// SPs will be created with a far-future expiration in Azure
// unless `explicit_max_ttl` is set in the role
var spExpiration = 10 * 365 * 24 * time.Hour
func secretServicePrincipal(b *azureSecretBackend) *framework.Secret {
return &framework.Secret{
Type: SecretTypeSP,
Renew: b.spRenew,
Revoke: b.spRevoke,
}
}
func secretStaticServicePrincipal(b *azureSecretBackend) *framework.Secret {
return &framework.Secret{
Type: SecretTypeStaticSP,
Renew: b.spRenew,
Revoke: b.staticSPRevoke,
}
}
func pathServicePrincipal(b *azureSecretBackend) *framework.Path {
return &framework.Path{
Pattern: fmt.Sprintf("creds/%s", framework.GenericNameRegex("role")),
DisplayAttrs: &framework.DisplayAttributes{
OperationPrefix: operationPrefixAzure,
OperationVerb: "request",
OperationSuffix: "service-principal-credentials",
},
Fields: map[string]*framework.FieldSchema{
"role": {
Type: framework.TypeLowerCaseString,
Description: "Name of the Vault role",
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.ReadOperation: &framework.PathOperation{
Callback: b.pathSPRead,
ForwardPerformanceSecondary: true,
ForwardPerformanceStandby: true,
},
},
HelpSynopsis: pathServicePrincipalHelpSyn,
HelpDescription: pathServicePrincipalHelpDesc,
}
}
// pathSPRead generates Azure credentials based on the role credential type.
func (b *azureSecretBackend) pathSPRead(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
client, err := b.getClient(ctx, req.Storage)
if err != nil {
return nil, err
}
roleName := d.Get("role").(string)
role, err := getRole(ctx, roleName, req.Storage)
if err != nil {
return nil, err
}
if role == nil {
return logical.ErrorResponse(fmt.Sprintf("role '%s' does not exist", roleName)), nil
}
var resp *logical.Response
if role.ApplicationObjectID != "" {
resp, err = b.createStaticSPSecret(ctx, client, roleName, role)
} else {
resp, err = b.createSPSecret(ctx, req.Storage, client, roleName, role)
}
if err != nil {
return nil, err
}
resp.Secret.TTL = role.TTL
resp.Secret.MaxTTL = role.MaxTTL
if role.ExplicitMaxTTL != 0 && (role.ExplicitMaxTTL < role.MaxTTL || role.MaxTTL == 0) {
resp.Secret.MaxTTL = role.ExplicitMaxTTL
}
return resp, nil
}
// createSPSecret generates a new App/Service Principal.
func (b *azureSecretBackend) createSPSecret(ctx context.Context, s logical.Storage, c *client, roleName string, role *roleEntry) (*logical.Response, error) {
// Create the App, which is the top level object to be tracked in the secret
// and deleted upon revocation. If any subsequent step fails, the App will be
// deleted as part of WAL rollback.
app, err := c.createApp(ctx, role.SignInAudience, role.Tags)
if err != nil {
return nil, err
}
appID := app.AppID
appObjID := app.AppObjectID
// Write a WAL entry in case the SP create process doesn't complete
walID, err := framework.PutWAL(ctx, s, walAppKey, &walApp{
AppID: appID,
AppObjID: appObjID,
Expiration: time.Now().Add(maxWALAge),
})
if err != nil {
return nil, fmt.Errorf("error writing WAL: %w", err)
}
// Determine SP duration
spDuration := spExpiration
if role.ExplicitMaxTTL != 0 {
spDuration = role.ExplicitMaxTTL
}
// Create a service principal associated with the new App
spID, password, endDate, err := c.createSP(ctx, app, spDuration)
if err != nil {
return nil, err
}
assignmentIDs, err := c.generateUUIDs(len(role.AzureRoles))
if err != nil {
return nil, fmt.Errorf("error generating assignment IDs; err=%w", err)
}
// Write a second WAL entry in case the Role assignments don't complete
rWALID, err := framework.PutWAL(ctx, s, walAppRoleAssignment, &walAppRoleAssign{
SpID: spID,
AssignmentIDs: assignmentIDs,
AzureRoles: role.AzureRoles,
Expiration: time.Now().Add(maxWALAge),
})
if err != nil {
return nil, fmt.Errorf("error writing WAL: %w", err)
}
// Assign Azure roles to the new SP
raIDs, err := c.assignRoles(ctx, spID, role.AzureRoles, assignmentIDs)
if err != nil {
return nil, err
}
// Assign Azure group memberships to the new SP
if err := c.addGroupMemberships(ctx, spID, role.AzureGroups); err != nil {
return nil, err
}
// SP is fully created so delete the WALs
if err := framework.DeleteWAL(ctx, s, walID); err != nil {
return nil, fmt.Errorf("error deleting WAL: %w", err)
}
if err := framework.DeleteWAL(ctx, s, rWALID); err != nil {
return nil, fmt.Errorf("error deleting role assignment WAL: %w", err)
}
data := map[string]interface{}{
"client_id": appID,
"client_secret": password,
}
internalData := map[string]interface{}{
"app_object_id": appObjID,
"sp_object_id": spID,
"role_assignment_ids": raIDs,
"group_membership_ids": groupObjectIDs(role.AzureGroups),
"role": roleName,
"permanently_delete": role.PermanentlyDelete,
"key_end_date": endDate.Format(time.RFC3339Nano),
}
return b.Secret(SecretTypeSP).Response(data, internalData), nil
}
// createStaticSPSecret adds a new password to the App associated with the role.
func (b *azureSecretBackend) createStaticSPSecret(ctx context.Context, c *client, roleName string, role *roleEntry) (*logical.Response, error) {
lock := locksutil.LockForKey(b.appLocks, role.ApplicationObjectID)
lock.Lock()
defer lock.Unlock()
// Determine SP duration
spDuration := spExpiration
if role.ExplicitMaxTTL != 0 {
spDuration = role.ExplicitMaxTTL
}
keyID, password, endDate, err := c.addAppPassword(ctx, role.ApplicationObjectID, spDuration)
if err != nil {
return nil, err
}
data := map[string]interface{}{
"client_id": role.ApplicationID,
"client_secret": password,
}
internalData := map[string]interface{}{
"app_object_id": role.ApplicationObjectID,
"key_id": keyID,
"key_end_date": endDate.Format(time.RFC3339Nano),
"role": roleName,
}
return b.Secret(SecretTypeStaticSP).Response(data, internalData), nil
}
func (b *azureSecretBackend) spRenew(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
roleRaw, ok := req.Secret.InternalData["role"]
if !ok {
return nil, errors.New("internal data 'role' not found")
}
role, err := getRole(ctx, roleRaw.(string), req.Storage)
if err != nil {
return nil, err
}
if role == nil {
return nil, nil
}
// Determine remaining lifetime of SP secret in Azure
keyEndDateRaw, ok := req.Secret.InternalData["key_end_date"]
if !ok {
return nil, errors.New("internal data 'key_end_date' not found")
}
keyEndDate, err := time.Parse(time.RFC3339Nano, keyEndDateRaw.(string))
if err != nil {
return nil, errors.New("cannot parse 'key_end_date' to timestamp")
}
keyLifetime := time.Until(keyEndDate)
resp := &logical.Response{Secret: req.Secret}
resp.Secret.TTL = min(role.TTL, keyLifetime)
resp.Secret.MaxTTL = min(role.MaxTTL, keyLifetime)
resp.Secret.Renewable = role.TTL < keyLifetime // Lease cannot be renewed beyond service-side endDate
return resp, nil
}
func (b *azureSecretBackend) spRevoke(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
resp := new(logical.Response)
appObjectIDRaw, ok := req.Secret.InternalData["app_object_id"]
if !ok {
return nil, errors.New("internal data 'app_object_id' not found")
}
appObjectID := appObjectIDRaw.(string)
// Get the service principal object ID. Only set if using dynamic service
// principals.
var spObjectID string
if spObjectIDRaw, ok := req.Secret.InternalData["sp_object_id"]; ok {
spObjectID = spObjectIDRaw.(string)
}
// Get the permanently delete setting. Only set if using dynamic service
// principals.
var permanentlyDelete bool
if permanentlyDeleteRaw, ok := req.Secret.InternalData["permanently_delete"]; ok {
permanentlyDelete = permanentlyDeleteRaw.(bool)
}
var raIDs []string
if req.Secret.InternalData["role_assignment_ids"] != nil {
for _, v := range req.Secret.InternalData["role_assignment_ids"].([]interface{}) {
raIDs = append(raIDs, v.(string))
}
}
var gmIDs []string
if req.Secret.InternalData["group_membership_ids"] != nil {
for _, v := range req.Secret.InternalData["group_membership_ids"].([]interface{}) {
gmIDs = append(gmIDs, v.(string))
}
}
if len(gmIDs) != 0 && spObjectID == "" {
return nil, errors.New("internal data 'sp_object_id' not found")
}
c, err := b.getClient(ctx, req.Storage)
if err != nil {
return nil, fmt.Errorf("error during revoke: %w", err)
}
// unassigning roles is effectively a garbage collection operation. Errors will be noted but won't fail the
// revocation process. Deleting the app, however, *is* required to consider the secret revoked.
if err := c.unassignRoles(ctx, raIDs); err != nil {
resp.AddWarning(err.Error())
}
// removing group membership is effectively a garbage collection
// operation. Errors will be noted but won't fail the revocation process.
// Deleting the app, however, *is* required to consider the secret revoked.
if err := c.removeGroupMemberships(ctx, spObjectID, gmIDs); err != nil {
resp.AddWarning(err.Error())
}
// removing the service principal is effectively a garbage collection
// operation. Errors will be noted but won't fail the revocation process.
// Deleting the app, however, *is* required to consider the secret revoked.
if err := c.deleteServicePrincipal(ctx, spObjectID, permanentlyDelete); err != nil {
resp.AddWarning(err.Error())
}
err = c.deleteApp(ctx, appObjectID, permanentlyDelete)
return resp, err
}
func (b *azureSecretBackend) staticSPRevoke(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
appObjectIDRaw, ok := req.Secret.InternalData["app_object_id"]
if !ok {
return nil, errors.New("internal data 'app_object_id' not found")
}
appObjectID := appObjectIDRaw.(string)
c, err := b.getClient(ctx, req.Storage)
if err != nil {
return nil, fmt.Errorf("error during revoke: %w", err)
}
keyIDRaw, ok := req.Secret.InternalData["key_id"]
if !ok {
return nil, errors.New("internal data 'key_id' not found")
}
lock := locksutil.LockForKey(b.appLocks, appObjectID)
lock.Lock()
defer lock.Unlock()
return nil, c.deleteAppPassword(ctx, appObjectID, keyIDRaw.(string))
}
const pathServicePrincipalHelpSyn = `
Request Service Principal credentials for a given Vault role.
`
const pathServicePrincipalHelpDesc = `
This path creates or updates dynamic Service Principal credentials.
The associated role can be configured to create a new App/Service Principal,
or add a new password to an existing App. The Service Principal or password
will be automatically deleted when the lease has expired.
`