-
Notifications
You must be signed in to change notification settings - Fork 291
/
Copy pathrepository_scope.go
502 lines (458 loc) · 19 KB
/
repository_scope.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
package iam
import (
"context"
"crypto/rand"
"fmt"
"strings"
"github.com/hashicorp/boundary/globals"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/errors"
"github.com/hashicorp/boundary/internal/kms"
"github.com/hashicorp/boundary/internal/oplog"
"github.com/hashicorp/boundary/internal/types/resource"
"github.com/hashicorp/boundary/internal/types/scope"
"github.com/hashicorp/go-dbw"
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
)
// CreateScope will create a scope in the repository and return the written
// scope. Supported options include: WithPublicId and WithRandomReader.
func (r *Repository) CreateScope(ctx context.Context, s *Scope, userId string, opt ...Option) (*Scope, error) {
const op = "iam.(Repository).CreateScope"
if s == nil {
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing scope")
}
if s.Scope == nil {
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing scope store")
}
if s.PublicId != "" {
return nil, errors.New(ctx, errors.InvalidParameter, op, "public id not empty")
}
var parentOplogWrapper wrapping.Wrapper
var err error
switch s.Type {
case scope.Unknown.String():
return nil, errors.New(ctx, errors.InvalidParameter, op, "unknown type")
case scope.Global.String():
return nil, errors.New(ctx, errors.InvalidParameter, op, "invalid type")
default:
switch s.ParentId {
case "":
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing parent id")
case scope.Global.String():
parentOplogWrapper, err = r.kms.GetWrapper(ctx, scope.Global.String(), kms.KeyPurposeOplog)
default:
parentOplogWrapper, err = r.kms.GetWrapper(ctx, s.ParentId, kms.KeyPurposeOplog)
}
}
if err != nil {
return nil, errors.New(ctx, errors.InvalidParameter, op, "unable to get oplog wrapper")
}
opts := getOpts(opt...)
var scopePublicId string
var scopeMetadata oplog.Metadata
var scopeRaw any
{
scopeType := scope.Map[s.Type]
if opts.withPublicId != "" {
if !strings.HasPrefix(opts.withPublicId, scopeType.Prefix()+"_") {
return nil, errors.New(ctx, errors.InvalidParameter, op, fmt.Sprintf("passed-in public ID %q has wrong prefix for type %q which uses prefix %q", opts.withPublicId, scopeType.String(), scopeType.Prefix()))
}
scopePublicId = opts.withPublicId
} else {
scopePublicId, err = newScopeId(scopeType)
if err != nil {
return nil, errors.Wrap(ctx, err, op)
}
}
sc := s.Clone().(*Scope)
sc.PublicId = scopePublicId
scopeRaw = sc
scopeMetadata, err = r.stdMetadata(ctx, sc)
if err != nil {
return nil, errors.Wrap(ctx, err, op)
}
scopeMetadata["op-type"] = []string{oplog.OpType_OP_TYPE_CREATE.String()}
}
var adminRolePublicId string
var adminRoleMetadata oplog.Metadata
var adminRole *Role
var adminRoleRaw any
switch {
case userId == "",
userId == globals.AnonymousUserId,
userId == globals.AnyAuthenticatedUserId,
userId == globals.RecoveryUserId,
opts.withSkipAdminRoleCreation:
// TODO: Cause a log entry. The repo doesn't have a logger right now,
// and ideally we will be using context to pass around log info scoped
// to this request for grouped display in the server log. The only
// reason this should ever happen anyways is via the administrative
// recovery workflow so it's already a special case.
// Also, stop linter from complaining
_ = adminRole
default:
adminRole, err = NewRole(scopePublicId)
if err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("error instantiating new admin role"))
}
adminRolePublicId, err = newRoleId()
if err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("error generating public id for new admin role"))
}
adminRole.PublicId = adminRolePublicId
adminRole.Name = "Administration"
adminRole.Description = fmt.Sprintf("Role created for administration of scope %s by user %s at its creation time", scopePublicId, userId)
adminRoleRaw = adminRole
adminRoleMetadata = oplog.Metadata{
"resource-public-id": []string{adminRolePublicId},
"scope-id": []string{scopePublicId},
"scope-type": []string{s.Type},
"resource-type": []string{resource.Role.String()},
"op-type": []string{oplog.OpType_OP_TYPE_CREATE.String()},
}
}
var defaultRolePublicId string
var defaultRoleMetadata oplog.Metadata
var defaultRole *Role
var defaultRoleRaw any
if !opts.withSkipDefaultRoleCreation {
defaultRole, err = NewRole(scopePublicId)
if err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("error instantiating new default role"))
}
defaultRolePublicId, err = newRoleId()
if err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("error generating public id for new default role"))
}
defaultRole.PublicId = defaultRolePublicId
switch s.Type {
case scope.Project.String():
defaultRole.Name = "Default Grants"
defaultRole.Description = fmt.Sprintf("Role created to provide default grants to users of scope %s at its creation time", scopePublicId)
default:
defaultRole.Name = "Login and Default Grants"
defaultRole.Description = fmt.Sprintf("Role created for login capability, account self-management, and other default grants for users of scope %s at its creation time", scopePublicId)
}
defaultRoleRaw = defaultRole
defaultRoleMetadata = oplog.Metadata{
"resource-public-id": []string{defaultRolePublicId},
"scope-id": []string{scopePublicId},
"scope-type": []string{s.Type},
"resource-type": []string{resource.Role.String()},
"op-type": []string{oplog.OpType_OP_TYPE_CREATE.String()},
}
}
reader := opts.withRandomReader
if reader == nil {
reader = rand.Reader
}
_, err = r.writer.DoTx(
ctx,
db.StdRetryCnt,
db.ExpBackoff{},
func(dbr db.Reader, w db.Writer) error {
if err := w.Create(
ctx,
scopeRaw,
db.WithOplog(parentOplogWrapper, scopeMetadata),
); err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("error creating scope"))
}
s := scopeRaw.(*Scope)
txnKms, err := kms.NewUsingReaderWriter(ctx, dbr, w)
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("error creating transaction's kms"))
}
if err := txnKms.AddExternalWrappers(ctx, kms.WithRootWrapper(r.kms.GetExternalWrappers(ctx).Root())); err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("error adding external root wrapper to transaction's kms"))
}
if err := txnKms.CreateKeys(ctx, s.PublicId, kms.WithRandomReader(reader), kms.WithReaderWriter(dbr, w)); err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("error creating scope keys"))
}
childOplogWrapper, err := txnKms.GetWrapper(ctx, s.PublicId, kms.KeyPurposeOplog)
if err != nil {
return errors.New(ctx, errors.InvalidParameter, op, "unable to get oplog wrapper")
}
// We create a new role, then set grants and principals on it. This
// turns into a bunch of stuff sadly because the role is the
// aggregate.
if adminRoleRaw != nil {
if err := w.Create(
ctx,
adminRoleRaw,
db.WithOplog(childOplogWrapper, adminRoleMetadata),
); err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("error creating role"))
}
adminRole = adminRoleRaw.(*Role)
msgs := make([]*oplog.Message, 0, 3)
roleTicket, err := w.GetTicket(ctx, adminRole)
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to get ticket"))
}
// We need to update the role version as that's the aggregate
var roleOplogMsg oplog.Message
rowsUpdated, err := w.Update(ctx, adminRole, []string{"Version"}, nil, db.NewOplogMsg(&roleOplogMsg), db.WithVersion(&adminRole.Version))
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to update role version for adding grant"))
}
if rowsUpdated != 1 {
return errors.New(ctx, errors.MultipleRecords, op, fmt.Sprintf("updated role but %d rows updated", rowsUpdated))
}
msgs = append(msgs, &roleOplogMsg)
roleGrant, err := NewRoleGrant(adminRolePublicId, "id=*;type=*;actions=*")
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to create in memory role grant"))
}
roleGrantOplogMsgs := make([]*oplog.Message, 0, 1)
if err := w.CreateItems(ctx, []any{roleGrant}, db.NewOplogMsgs(&roleGrantOplogMsgs)); err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to add grants"))
}
msgs = append(msgs, roleGrantOplogMsgs...)
rolePrincipal, err := NewUserRole(adminRolePublicId, userId)
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to create in memory role user"))
}
roleUserOplogMsgs := make([]*oplog.Message, 0, 1)
if err := w.CreateItems(ctx, []any{rolePrincipal}, db.NewOplogMsgs(&roleUserOplogMsgs)); err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to add grants"))
}
msgs = append(msgs, roleUserOplogMsgs...)
metadata := oplog.Metadata{
"op-type": []string{oplog.OpType_OP_TYPE_CREATE.String()},
"scope-id": []string{s.PublicId},
"scope-type": []string{s.Type},
"resource-public-id": []string{adminRole.PublicId},
}
if err := w.WriteOplogEntryWith(ctx, childOplogWrapper, roleTicket, metadata, msgs); err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to write oplog"))
}
}
// We create a new role, then set grants and principals on it. This
// turns into a bunch of stuff sadly because the role is the
// aggregate.
if defaultRoleRaw != nil {
if err := w.Create(
ctx,
defaultRoleRaw,
db.WithOplog(childOplogWrapper, defaultRoleMetadata),
); err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("error creating role"))
}
defaultRole = defaultRoleRaw.(*Role)
msgs := make([]*oplog.Message, 0, 6)
roleTicket, err := w.GetTicket(ctx, defaultRole)
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to get ticket"))
}
// We need to update the role version as that's the aggregate
var roleOplogMsg oplog.Message
rowsUpdated, err := w.Update(ctx, defaultRole, []string{"Version"}, nil, db.NewOplogMsg(&roleOplogMsg), db.WithVersion(&defaultRole.Version))
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to update role version for adding grant"))
}
if rowsUpdated != 1 {
return errors.New(ctx, errors.MultipleRecords, op, fmt.Sprintf("updated role but %d rows updated", rowsUpdated))
}
msgs = append(msgs, &roleOplogMsg)
// Grants
{
grants := []any{}
switch s.Type {
case scope.Project.String():
roleGrant, err := NewRoleGrant(defaultRolePublicId, "id=*;type=session;actions=list,read:self,cancel:self")
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to create in memory role grant"))
}
grants = append(grants, roleGrant)
roleGrant, err = NewRoleGrant(defaultRolePublicId, "type=target;actions=list")
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to create in memory role grant"))
}
grants = append(grants, roleGrant)
default:
roleGrant, err := NewRoleGrant(defaultRolePublicId, "id=*;type=scope;actions=list,no-op")
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to create in memory role grant"))
}
grants = append(grants, roleGrant)
roleGrant, err = NewRoleGrant(defaultRolePublicId, "id=*;type=auth-method;actions=authenticate,list")
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to create in memory role grant"))
}
grants = append(grants, roleGrant)
roleGrant, err = NewRoleGrant(defaultRolePublicId, "id={{.Account.Id}};actions=read,change-password")
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to create in memory role grant"))
}
grants = append(grants, roleGrant)
roleGrant, err = NewRoleGrant(defaultRolePublicId, "id=*;type=auth-token;actions=list,read:self,delete:self")
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to create in memory role grant"))
}
grants = append(grants, roleGrant)
}
roleGrantOplogMsgs := make([]*oplog.Message, 0, 3)
if err := w.CreateItems(ctx, grants, db.NewOplogMsgs(&roleGrantOplogMsgs)); err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to add grants"))
}
msgs = append(msgs, roleGrantOplogMsgs...)
}
// Principals
{
principals := []any{}
userId := globals.AnonymousUserId
if s.Type == scope.Project.String() {
userId = globals.AnyAuthenticatedUserId
}
rolePrincipal, err := NewUserRole(defaultRolePublicId, userId)
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to create in memory role user"))
}
principals = append(principals, rolePrincipal)
roleUserOplogMsgs := make([]*oplog.Message, 0, 2)
if err := w.CreateItems(ctx, principals, db.NewOplogMsgs(&roleUserOplogMsgs)); err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to add grants"))
}
msgs = append(msgs, roleUserOplogMsgs...)
}
metadata := oplog.Metadata{
"op-type": []string{oplog.OpType_OP_TYPE_CREATE.String()},
"scope-id": []string{s.PublicId},
"scope-type": []string{s.Type},
"resource-public-id": []string{defaultRole.PublicId},
}
if err := w.WriteOplogEntryWith(ctx, childOplogWrapper, roleTicket, metadata, msgs); err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to write oplog"))
}
}
return nil
},
)
if err != nil {
if errors.IsUniqueError(err) {
return nil, errors.New(ctx, errors.NotUnique, op, fmt.Sprintf("scope %s/%s already exists", scopePublicId, s.Name))
}
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("for %s", scopePublicId)))
}
return scopeRaw.(*Scope), nil
}
// UpdateScope will update a scope in the repository and return the written
// scope. fieldMaskPaths provides field_mask.proto paths for fields that should
// be updated. Fields will be set to NULL if the field is a zero value and
// included in fieldMask. Name and Description are the only updatable fields,
// and everything else is ignored. If no updatable fields are included in the
// fieldMaskPaths, then an error is returned.
func (r *Repository) UpdateScope(ctx context.Context, scope *Scope, version uint32, fieldMaskPaths []string, _ ...Option) (*Scope, int, error) {
const op = "iam.(Repository).UpdateScope"
if scope == nil {
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "missing scope")
}
if scope.PublicId == "" {
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "missing public id")
}
if contains(fieldMaskPaths, "ParentId") {
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidFieldMask, op, "you cannot change a scope's parent")
}
var dbMask, nullFields []string
dbMask, nullFields = dbw.BuildUpdatePaths(
map[string]any{
"name": scope.Name,
"description": scope.Description,
"PrimaryAuthMethodId": scope.PrimaryAuthMethodId, // gorm: it's important that the field start with a capital letter.
},
fieldMaskPaths,
nil,
)
// nada to update, so reload scope from db and return it
if len(dbMask) == 0 && len(nullFields) == 0 {
return nil, db.NoRowsAffected, errors.E(ctx, errors.WithCode(errors.EmptyFieldMask), errors.WithOp(op))
}
resource, rowsUpdated, err := r.update(ctx, scope, version, dbMask, nullFields)
if err != nil {
if errors.IsUniqueError(err) {
return nil, db.NoRowsAffected, errors.New(ctx, errors.NotUnique, op, fmt.Sprintf("%s name %s already exists", scope.PublicId, scope.Name))
}
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("for public id %s", scope.PublicId)))
}
return resource.(*Scope), rowsUpdated, nil
}
// LookupScope will look up a scope in the repository. If the scope is not
// found, it will return nil, nil.
func (r *Repository) LookupScope(ctx context.Context, withPublicId string, _ ...Option) (*Scope, error) {
const op = "iam.(Repository).LookupScope"
if withPublicId == "" {
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing public id")
}
scope := AllocScope()
scope.PublicId = withPublicId
if err := r.reader.LookupByPublicId(ctx, &scope); err != nil {
if errors.IsNotFoundError(err) {
return nil, nil
}
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("failed for %s", withPublicId)))
}
return &scope, nil
}
// DeleteScope will delete a scope from the repository
func (r *Repository) DeleteScope(ctx context.Context, withPublicId string, _ ...Option) (int, error) {
const op = "iam.(Repository).DeleteScope"
if withPublicId == "" {
return db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "missing public id")
}
if withPublicId == scope.Global.String() {
return db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "invalid to delete global scope")
}
scope := AllocScope()
scope.PublicId = withPublicId
rowsDeleted, err := r.delete(ctx, &scope)
if err != nil {
if errors.Is(err, ErrMetadataScopeNotFound) {
return 0, nil
}
return db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("failed for %s", withPublicId)))
}
return rowsDeleted, nil
}
// ListScopes with the parent IDs, supports the WithLimit option.
func (r *Repository) ListScopes(ctx context.Context, withParentIds []string, opt ...Option) ([]*Scope, error) {
const op = "iam.(Repository).ListScopes"
if len(withParentIds) == 0 {
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing parent id")
}
var items []*Scope
err := r.list(ctx, &items, "parent_id in (?)", []any{withParentIds}, opt...)
if err != nil {
return nil, errors.Wrap(ctx, err, op)
}
return items, nil
}
// ListScopesRecursively allows for recursive listing of scopes based on a root scope
// ID. It returns the root scope ID as a part of the set.
func (r *Repository) ListScopesRecursively(ctx context.Context, rootScopeId string, opt ...Option) ([]*Scope, error) {
const op = "iam.(Repository).ListRecursively"
var scopes []*Scope
var where string
var args []any
switch {
case rootScopeId == scope.Global.String():
// Nothing -- we want all scopes
case strings.HasPrefix(rootScopeId, "o_"):
// The org itself and any projects that have it as parent
where = "public_id = ? or parent_id = ?"
args = append(args, rootScopeId, rootScopeId)
case strings.HasPrefix(rootScopeId, "p_"):
// No scopes can (currently) live under projects, so just the project
// itself
where = "public_id = ?"
args = append(args, rootScopeId)
default:
// We have no idea what scope type this is so bail
return nil, errors.New(ctx, errors.InvalidPublicId, op+":TypeSwitch", "invalid scope ID")
}
err := r.list(ctx, &scopes, where, args, opt...)
if err != nil {
return nil, errors.Wrap(ctx, err, op+":ListQuery")
}
return scopes, nil
}