From 73a38b14337d92c6784136736b70e6ec274adb25 Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Wed, 12 Aug 2020 00:02:26 -0400 Subject: [PATCH] Remove some unneeded resource types and fix the resource typing for default role in global scope (#256) --- internal/auth/auth_test.go | 7 -- internal/db/migrations/postgres.gen.go | 2 +- internal/db/migrations/postgres/06_iam.up.sql | 2 +- internal/iam/account.go | 2 +- internal/iam/group.go | 2 +- internal/iam/role.go | 2 +- internal/iam/role_grant.go | 4 - internal/iam/role_grant_test.go | 8 -- internal/iam/scope_test.go | 2 +- internal/iam/user.go | 2 +- internal/iam/user_test.go | 12 --- internal/perms/grants.go | 7 +- internal/perms/grants_test.go | 7 -- .../handlers/scopes/scope_service.go | 5 +- internal/types/resource/resource.go | 82 +++++++------------ internal/types/resource/resource_test.go | 40 ++------- 16 files changed, 47 insertions(+), 139 deletions(-) diff --git a/internal/auth/auth_test.go b/internal/auth/auth_test.go index bfe53b8be..ee40edbc1 100644 --- a/internal/auth/auth_test.go +++ b/internal/auth/auth_test.go @@ -175,13 +175,6 @@ func TestHandler_AuthDecoration(t *testing.T) { resource: resource.Scope, id: "o_1234", }, - { - name: "org scope, get on collection is list", - path: "/v1/scopes/o_abc123/projects", - action: action.List, - scope: "o_abc123", - resource: resource.Project, - }, { name: "global action, valid", path: "/v1/scopes/global/:read", diff --git a/internal/db/migrations/postgres.gen.go b/internal/db/migrations/postgres.gen.go index 9397c2123..45555beba 100644 --- a/internal/db/migrations/postgres.gen.go +++ b/internal/db/migrations/postgres.gen.go @@ -957,7 +957,7 @@ insert into iam_role (public_id, name, description, scope_id) values('r_default', 'default', 'default role', 'global'); insert into iam_role_grant (role_id, canonical_grant, raw_grant) - values('r_default', 'type=org;actions=list', 'type=org;actions=list'); + values('r_default', 'type=scope;actions=list', 'type=scope;actions=list'); create table iam_group ( public_id wt_public_id not null primary key, diff --git a/internal/db/migrations/postgres/06_iam.up.sql b/internal/db/migrations/postgres/06_iam.up.sql index bebbe7c27..bc386d58c 100644 --- a/internal/db/migrations/postgres/06_iam.up.sql +++ b/internal/db/migrations/postgres/06_iam.up.sql @@ -451,7 +451,7 @@ insert into iam_role (public_id, name, description, scope_id) values('r_default', 'default', 'default role', 'global'); insert into iam_role_grant (role_id, canonical_grant, raw_grant) - values('r_default', 'type=org;actions=list', 'type=org;actions=list'); + values('r_default', 'type=scope;actions=list', 'type=scope;actions=list'); create table iam_group ( public_id wt_public_id not null primary key, diff --git a/internal/iam/account.go b/internal/iam/account.go index 4f70ac124..d59a0ff13 100644 --- a/internal/iam/account.go +++ b/internal/iam/account.go @@ -52,7 +52,7 @@ func (a *authAccount) VetForWrite(ctx context.Context, r db.Reader, opType db.Op } func (a *authAccount) validScopeTypes() []scope.Type { - return []scope.Type{scope.Org} + return []scope.Type{scope.Global, scope.Org} } // GetScope returns the scope for the auth account. diff --git a/internal/iam/group.go b/internal/iam/group.go index 9983ffd4e..44e7ca989 100644 --- a/internal/iam/group.go +++ b/internal/iam/group.go @@ -71,7 +71,7 @@ func (g *Group) VetForWrite(ctx context.Context, r db.Reader, opType db.OpType, } func (u *Group) validScopeTypes() []scope.Type { - return []scope.Type{scope.Org, scope.Project} + return []scope.Type{scope.Global, scope.Org, scope.Project} } // GetScope returns the scope for the Group. diff --git a/internal/iam/role.go b/internal/iam/role.go index 122a545c8..56b0a30bf 100644 --- a/internal/iam/role.go +++ b/internal/iam/role.go @@ -72,7 +72,7 @@ func (role *Role) VetForWrite(ctx context.Context, r db.Reader, opType db.OpType } func (u *Role) validScopeTypes() []scope.Type { - return []scope.Type{scope.Org, scope.Project} + return []scope.Type{scope.Global, scope.Org, scope.Project} } // Getscope returns the scope for the Role. diff --git a/internal/iam/role_grant.go b/internal/iam/role_grant.go index 63ac178fa..8a8de543c 100644 --- a/internal/iam/role_grant.go +++ b/internal/iam/role_grant.go @@ -7,7 +7,6 @@ import ( "github.com/hashicorp/boundary/internal/db" "github.com/hashicorp/boundary/internal/iam/store" "github.com/hashicorp/boundary/internal/perms" - "github.com/hashicorp/boundary/internal/types/resource" "google.golang.org/protobuf/proto" ) @@ -87,9 +86,6 @@ func (g *RoleGrant) VetForWrite(ctx context.Context, r db.Reader, opType db.OpTy return nil } -// ResourceType returns the type of the RoleGrant -func (*RoleGrant) ResourceType() resource.Type { return resource.RoleGrant } - // TableName returns the tablename to override the default gorm table name func (g *RoleGrant) TableName() string { if g.tableName != "" { diff --git a/internal/iam/role_grant_test.go b/internal/iam/role_grant_test.go index a5fbd812f..67c5c1aab 100644 --- a/internal/iam/role_grant_test.go +++ b/internal/iam/role_grant_test.go @@ -8,7 +8,6 @@ import ( "github.com/hashicorp/boundary/internal/db" "github.com/hashicorp/boundary/internal/iam/store" - "github.com/hashicorp/boundary/internal/types/resource" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "google.golang.org/protobuf/proto" @@ -216,13 +215,6 @@ func TestRoleGrant_Delete(t *testing.T) { } } -func TestRoleGrant_ResourceType(t *testing.T) { - assert := assert.New(t) - r := &RoleGrant{} - ty := r.ResourceType() - assert.Equal(ty, resource.RoleGrant) -} - func TestRoleGrant_Clone(t *testing.T) { t.Parallel() conn, _ := db.TestSetup(t, "postgres") diff --git a/internal/iam/scope_test.go b/internal/iam/scope_test.go index 12bee7293..58b2c9c1e 100644 --- a/internal/iam/scope_test.go +++ b/internal/iam/scope_test.go @@ -165,7 +165,7 @@ func TestScope_ResourceType(t *testing.T) { o, err := NewOrg() require.NoError(t, err) assert.Equal(t, o.ResourceType(), resource.Scope) - assert.Equal(t, o.GetParentId(), resource.Global.String()) + assert.Equal(t, o.GetParentId(), scope.Global.String()) } func TestScope_Clone(t *testing.T) { diff --git a/internal/iam/user.go b/internal/iam/user.go index 40b9797e7..3402be748 100644 --- a/internal/iam/user.go +++ b/internal/iam/user.go @@ -72,7 +72,7 @@ func (u *User) VetForWrite(ctx context.Context, r db.Reader, opType db.OpType, o } func (u *User) validScopeTypes() []scope.Type { - return []scope.Type{scope.Org} + return []scope.Type{scope.Global, scope.Org} } // GetScope returns the scope for the User diff --git a/internal/iam/user_test.go b/internal/iam/user_test.go index 0d72cc1e9..fa9c91115 100644 --- a/internal/iam/user_test.go +++ b/internal/iam/user_test.go @@ -124,18 +124,6 @@ func Test_UserCreate(t *testing.T) { require.Error(err) assert.Equal("create: vet for write failed: scope is not found", err.Error()) }) - t.Run("bad-scope", func(t *testing.T) { - assert, require := assert.New(t), require.New(t) - w := db.New(conn) - user, err := NewUser("global") - require.NoError(err) - id, err := newUserId() - require.NoError(err) - user.PublicId = id - err = w.Create(context.Background(), user) - require.Error(err) - assert.Equal("create: vet for write failed: global not a valid scope type for this resource", err.Error()) - }) } func Test_UserUpdate(t *testing.T) { diff --git a/internal/perms/grants.go b/internal/perms/grants.go index c05163cfb..4cb93393e 100644 --- a/internal/perms/grants.go +++ b/internal/perms/grants.go @@ -298,14 +298,15 @@ func (g Grant) validateType() error { switch g.typ { case resource.Unknown, resource.All, - resource.Role, - resource.Group, + resource.Scope, resource.User, + resource.Group, + resource.Role, resource.AuthMethod, + resource.Account, resource.HostCatalog, resource.HostSet, resource.Host, - resource.Org, resource.Target: return nil } diff --git a/internal/perms/grants_test.go b/internal/perms/grants_test.go index 098d0d0e3..0c9092280 100644 --- a/internal/perms/grants_test.go +++ b/internal/perms/grants_test.go @@ -104,13 +104,6 @@ func Test_ValidateType(t *testing.T) { { name: "no specifier", }, - { - name: "unknown specifier", - input: Grant{ - typ: resource.RoleGrant, - }, - errResult: `unknown type specifier "role-grant"`, - }, { name: "valid specifier", input: Grant{ diff --git a/internal/servers/controller/handlers/scopes/scope_service.go b/internal/servers/controller/handlers/scopes/scope_service.go index cc2bd383a..ce5751f7b 100644 --- a/internal/servers/controller/handlers/scopes/scope_service.go +++ b/internal/servers/controller/handlers/scopes/scope_service.go @@ -51,10 +51,7 @@ var _ pbs.ScopeServiceServer = Service{} // ListScopes implements the interface pbs.ScopeServiceServer. func (s Service) ListScopes(ctx context.Context, req *pbs.ListScopesRequest) (*pbs.ListScopesResponse, error) { if req.GetScopeId() == "" { - return nil, handlers.InvalidArgumentErrorf( - "Argument errors found in the request.", - map[string]string{"scope_id": "Missing value for scope_id"}, - ) + req.ScopeId = scope.Global.String() } authResults := auth.Verify(ctx, auth.WithScopeId(req.GetScopeId())) if !authResults.Valid { diff --git a/internal/types/resource/resource.go b/internal/types/resource/resource.go index 928ce8408..8cdc6d122 100644 --- a/internal/types/resource/resource.go +++ b/internal/types/resource/resource.go @@ -4,75 +4,51 @@ package resource type Type int const ( - Unknown Type = 0 - Scope Type = 1 - User Type = 2 - Group Type = 3 - Role Type = 4 - Org Type = 5 - GroupMember Type = 6 - GroupUserMember Type = 7 - AssignedRole Type = 8 - AssignedUserRole Type = 9 - AssignedGroupRole Type = 10 - RoleGrant Type = 11 - AuthMethod Type = 12 - Project Type = 13 - All Type = 14 - HostCatalog Type = 15 - HostSet Type = 16 - Host Type = 17 - Target Type = 18 - Global Type = 19 - AuthToken Type = 20 + Unknown Type = 0 + All Type = 1 + Scope Type = 2 + User Type = 3 + Group Type = 4 + Role Type = 5 + AuthMethod Type = 6 + Account Type = 7 + AuthToken Type = 8 + HostCatalog Type = 9 + HostSet Type = 10 + Host Type = 11 + Target Type = 12 ) func (r Type) String() string { return [...]string{ "unknown", + "*", "scope", "user", "group", "role", - "org", - "group-member", - "group-user-member", - "assigned-role", - "assigned-user-role", - "assigned-group-role", - "role-grant", "auth-method", - "project", - "*", + "account", + "auth-token", "host-catalog", "host-set", "host", "target", - "global", - "auth-token", }[r] } var Map = map[string]Type{ - Scope.String(): Scope, - User.String(): User, - Group.String(): Group, - Role.String(): Role, - Org.String(): Org, - GroupMember.String(): GroupMember, - GroupUserMember.String(): GroupUserMember, - AssignedRole.String(): AssignedRole, - AssignedUserRole.String(): AssignedUserRole, - AssignedGroupRole.String(): AssignedGroupRole, - RoleGrant.String(): RoleGrant, - AuthMethod.String(): AuthMethod, - Project.String(): Project, - All.String(): All, - HostCatalog.String(): HostCatalog, - HostSet.String(): HostSet, - Host.String(): Host, - Target.String(): Target, - Global.String(): Global, - Group.String(): Group, - AuthToken.String(): AuthToken, + Unknown.String(): Unknown, + All.String(): All, + Scope.String(): Scope, + User.String(): User, + Group.String(): Group, + Role.String(): Role, + AuthMethod.String(): AuthMethod, + Account.String(): Account, + AuthToken.String(): AuthToken, + HostCatalog.String(): HostCatalog, + HostSet.String(): HostSet, + Host.String(): Host, + Target.String(): Target, } diff --git a/internal/types/resource/resource_test.go b/internal/types/resource/resource_test.go index b820532c6..b386f57cc 100644 --- a/internal/types/resource/resource_test.go +++ b/internal/types/resource/resource_test.go @@ -32,41 +32,17 @@ func Test_Resource(t *testing.T) { typeString: "role", want: Role, }, - { - typeString: "org", - want: Org, - }, - { - typeString: "group-member", - want: GroupMember, - }, - { - typeString: "group-user-member", - want: GroupUserMember, - }, - { - typeString: "assigned-role", - want: AssignedRole, - }, - { - typeString: "assigned-user-role", - want: AssignedUserRole, - }, - { - typeString: "assigned-group-role", - want: AssignedGroupRole, - }, - { - typeString: "role-grant", - want: RoleGrant, - }, { typeString: "auth-method", want: AuthMethod, }, { - typeString: "project", - want: Project, + typeString: "account", + want: Account, + }, + { + typeString: "auth-token", + want: AuthToken, }, { typeString: "*", @@ -88,10 +64,6 @@ func Test_Resource(t *testing.T) { typeString: "target", want: Target, }, - { - typeString: "global", - want: Global, - }, } for _, tt := range tests { t.Run(tt.typeString, func(t *testing.T) {