Include Principals and Grants on Roles When Updating the Role. (#203)

This commit is contained in:
Todd Knight authored and GitHub committed 2020-07-17 09:31:10 -07:00
1 parent 99653727bb
commit 5bb13e71fe
6 files changed
+182 -38

No files matched your search

+39 -11
View File
@@ -45,15 +45,15 @@ func (r *Repository) CreateRole(ctx context.Context, role *Role, opt ...Option)
// included in fieldMask. Name, Description, and GrantScopeId are the only
// updatable fields, If no updatable fields are included in the fieldMaskPaths,
// then an error is returned.
func (r *Repository) UpdateRole(ctx context.Context, role *Role, fieldMaskPaths []string, opt ...Option) (*Role, int, error) {
func (r *Repository) UpdateRole(ctx context.Context, role *Role, fieldMaskPaths []string, opt ...Option) (*Role, []PrincipalRole, []*RoleGrant, int, error) {
if role == nil {
return nil, db.NoRowsAffected, fmt.Errorf("update role: missing role %w", db.ErrNilParameter)
return nil, nil, nil, db.NoRowsAffected, fmt.Errorf("update role: missing role %w", db.ErrNilParameter)
}
if role.Role == nil {
return nil, db.NoRowsAffected, fmt.Errorf("update role: missing role store %w", db.ErrNilParameter)
return nil, nil, nil, db.NoRowsAffected, fmt.Errorf("update role: missing role store %w", db.ErrNilParameter)
}
if role.PublicId == "" {
return nil, db.NoRowsAffected, fmt.Errorf("update role: missing role public id %w", db.ErrInvalidParameter)
return nil, nil, nil, db.NoRowsAffected, fmt.Errorf("update role: missing role public id %w", db.ErrInvalidParameter)
}
for _, f := range fieldMaskPaths {
switch {
@@ -61,7 +61,7 @@ func (r *Repository) UpdateRole(ctx context.Context, role *Role, fieldMaskPaths
case strings.EqualFold("description", f):
case strings.EqualFold("grantscopeid", f):
default:
return nil, db.NoRowsAffected, fmt.Errorf("update role: field: %s: %w", f, db.ErrInvalidFieldMask)
return nil, nil, nil, db.NoRowsAffected, fmt.Errorf("update role: field: %s: %w", f, db.ErrInvalidFieldMask)
}
}
var dbMask, nullFields []string
@@ -74,17 +74,45 @@ func (r *Repository) UpdateRole(ctx context.Context, role *Role, fieldMaskPaths
fieldMaskPaths,
)
if len(dbMask) == 0 && len(nullFields) == 0 {
return nil, db.NoRowsAffected, fmt.Errorf("update role: %w", db.ErrEmptyFieldMask)
return nil, nil, nil, db.NoRowsAffected, fmt.Errorf("update role: %w", db.ErrEmptyFieldMask)
}
c := role.Clone().(*Role)
resource, rowsUpdated, err := r.update(ctx, c, dbMask, nullFields)
var resource Resource
var rowsUpdated int
var pr []PrincipalRole
var rg []*RoleGrant
_, err := r.writer.DoTx(
ctx,
db.StdRetryCnt,
db.ExpBackoff{},
func(read db.Reader, w db.Writer) error {
var err error
c := role.Clone().(*Role)
resource, rowsUpdated, err = r.update(ctx, c, dbMask, nullFields)
if err != nil {
return err
}
repo, err := NewRepository(read, w, r.wrapper)
if err != nil {
return fmt.Errorf("update role: failed creating inner repo: %w for %s", err, role.PublicId)
}
pr, err = repo.ListPrincipalRoles(ctx, role.PublicId)
if err != nil {
return fmt.Errorf("update role: listing principal roles: %w for %s", err, role.PublicId)
}
rg, err = repo.ListRoleGrants(ctx, role.PublicId)
if err != nil {
return fmt.Errorf("update role: listing principal roles: %w for %s", err, role.PublicId)
}
return nil
},
)
if err != nil {
if db.IsUniqueError(err) {
return nil, db.NoRowsAffected, fmt.Errorf("update role: role %s already exists in org %s: %w", role.Name, role.ScopeId, db.ErrNotUnique)
return nil, nil, nil, db.NoRowsAffected, fmt.Errorf("update role: role %s already exists in org %s: %w", role.Name, role.ScopeId, db.ErrNotUnique)
}
return nil, db.NoRowsAffected, fmt.Errorf("update role: %w for %s", err, c.PublicId)
return nil, nil, nil, db.NoRowsAffected, fmt.Errorf("update role: %w for %s", err, role.PublicId)
}
return resource.(*Role), rowsUpdated, err
return resource.(*Role), pr, rg, rowsUpdated, err
}
// LookupRole will look up a role in the repository. If the role is not
+33 -8
View File
@@ -3,12 +3,14 @@ package iam
import (
"context"
"errors"
"fmt"
"testing"
"time"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/watchtower/internal/db"
dbassert "github.com/hashicorp/watchtower/internal/db/assert"
"github.com/hashicorp/watchtower/internal/iam/store"
"github.com/hashicorp/watchtower/internal/oplog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -181,6 +183,8 @@ func TestRepository_UpdateRole(t *testing.T) {
require.NoError(t, err)
org, proj := TestScopes(t, conn)
u := TestUser(t, conn, org.GetPublicId())
pubId := func(s string) *string { return &s }
type args struct {
@@ -194,7 +198,7 @@ func TestRepository_UpdateRole(t *testing.T) {
tests := []struct {
name string
newScopeId string
newGrpOpts []Option
newRoleOpts []Option
args args
wantRowsUpdate int
wantErr bool
@@ -221,7 +225,7 @@ func TestRepository_UpdateRole(t *testing.T) {
ScopeId: org.PublicId,
},
newScopeId: org.PublicId,
newGrpOpts: []Option{WithName("valid-no-op" + id)},
newRoleOpts: []Option{WithName("valid-no-op" + id)},
wantErr: false,
wantRowsUpdate: 1,
},
@@ -247,7 +251,7 @@ func TestRepository_UpdateRole(t *testing.T) {
ScopeId: org.PublicId,
},
newScopeId: org.PublicId,
newGrpOpts: []Option{WithName("null-name" + id)},
newRoleOpts: []Option{WithName("null-name" + id)},
wantErr: false,
wantRowsUpdate: 1,
},
@@ -259,7 +263,7 @@ func TestRepository_UpdateRole(t *testing.T) {
ScopeId: org.PublicId,
},
newScopeId: org.PublicId,
newGrpOpts: []Option{WithDescription("null-description" + id)},
newRoleOpts: []Option{WithDescription("null-description" + id)},
wantErr: false,
wantRowsUpdate: 1,
},
@@ -359,7 +363,7 @@ func TestRepository_UpdateRole(t *testing.T) {
ScopeId: proj.PublicId,
},
newScopeId: proj.PublicId,
newGrpOpts: []Option{WithName("dup-name-in-diff-scope-pre-update" + id)},
newRoleOpts: []Option{WithName("dup-name-in-diff-scope-pre-update" + id)},
wantErr: false,
wantRowsUpdate: 1,
wantDup: true,
@@ -383,12 +387,31 @@ func TestRepository_UpdateRole(t *testing.T) {
require, assert := require.New(t), assert.New(t)
if tt.wantDup {
r := TestRole(t, conn, org.PublicId)
_ = TestUserRole(t, conn, r.GetPublicId(), u.GetPublicId())
_ = TestRoleGrant(t, conn, r.GetPublicId(), "id=*;actions=*")
r.Name = tt.args.name
_, _, err := repo.UpdateRole(context.Background(), r, tt.args.fieldMaskPaths, tt.args.opt...)
_, _, _, _, err := repo.UpdateRole(context.Background(), r, tt.args.fieldMaskPaths, tt.args.opt...)
assert.NoError(err)
}
r := TestRole(t, conn, tt.newScopeId, tt.newGrpOpts...)
r := TestRole(t, conn, tt.newScopeId, tt.newRoleOpts...)
ur := TestUserRole(t, conn, r.GetPublicId(), u.GetPublicId())
princRole := PrincipalRole{PrincipalRoleView: &store.PrincipalRoleView{
Type: UserRoleType.String(),
CreateTime: ur.CreateTime,
RoleId: ur.RoleId,
PrincipalId: ur.PrincipalId,
PrincipalScopeId: org.GetPublicId(),
ScopedPrincipalId: ur.PrincipalId,
RoleScopeId: org.GetPublicId(),
}}
if tt.newScopeId != org.GetPublicId() {
// If the project is in a different scope from the created user we need to update the
// scope specific fields.
princRole.RoleScopeId = tt.newScopeId
princRole.ScopedPrincipalId = fmt.Sprintf("%s:%s", org.PublicId, ur.PrincipalId)
}
rGrant := TestRoleGrant(t, conn, r.GetPublicId(), "id=*;actions=*")
updateRole := allocRole()
updateRole.PublicId = r.PublicId
@@ -399,7 +422,7 @@ func TestRepository_UpdateRole(t *testing.T) {
updateRole.Name = tt.args.name
updateRole.Description = tt.args.description
roleAfterUpdate, updatedRows, err := repo.UpdateRole(context.Background(), &updateRole, tt.args.fieldMaskPaths, tt.args.opt...)
roleAfterUpdate, principals, grants, updatedRows, err := repo.UpdateRole(context.Background(), &updateRole, tt.args.fieldMaskPaths, tt.args.opt...)
if tt.wantErr {
assert.Error(err)
if tt.wantIsError != nil {
@@ -416,6 +439,8 @@ func TestRepository_UpdateRole(t *testing.T) {
require.NoError(err)
require.NotNil(roleAfterUpdate)
assert.Equal(tt.wantRowsUpdate, updatedRows)
assert.Equal([]PrincipalRole{princRole}, principals)
assert.Equal([]*RoleGrant{rGrant}, grants)
switch tt.name {
case "valid-no-op":
assert.Equal(r.UpdateTime, roleAfterUpdate.UpdateTime)