Roles and Groups Handlers Support Project Scope (#154)
* Adding project level scopes for groups and roles. * Adding tests to verify the routes are working as expected.
This commit is contained in:
1 parent
3e3144117e
commit
8399764442
13 files changed
+3193
-389
No files matched your search
@@ -2,17 +2,127 @@ package controller
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/grpc-ecosystem/grpc-gateway/runtime"
|
||||
"github.com/hashicorp/watchtower/internal/gen/controller/api/services"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestGrpcGatewayRouting(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
// The unimplemented result indicates the grpc routing is happening successfully otherwise it would return NotFound.
|
||||
routed := http.StatusNotImplemented
|
||||
unrouted := http.StatusNotFound
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
setup func(mux *runtime.ServeMux)
|
||||
url string
|
||||
expectedResult int
|
||||
}{
|
||||
{
|
||||
name: "project",
|
||||
setup: func(mux *runtime.ServeMux) {
|
||||
require.NoError(t, services.RegisterProjectServiceHandlerServer(ctx, mux, &services.UnimplementedProjectServiceServer{}))
|
||||
},
|
||||
url: "v1/orgs/someid/projects",
|
||||
expectedResult: routed,
|
||||
},
|
||||
{
|
||||
name: "users",
|
||||
setup: func(mux *runtime.ServeMux) {
|
||||
require.NoError(t, services.RegisterUserServiceHandlerServer(ctx, mux, &services.UnimplementedUserServiceServer{}))
|
||||
},
|
||||
url: "v1/orgs/someid/users",
|
||||
expectedResult: routed,
|
||||
},
|
||||
{
|
||||
name: "roles",
|
||||
setup: func(mux *runtime.ServeMux) {
|
||||
require.NoError(t, services.RegisterRoleServiceHandlerServer(ctx, mux, &services.UnimplementedRoleServiceServer{}))
|
||||
},
|
||||
url: "v1/orgs/someid/roles",
|
||||
expectedResult: routed,
|
||||
},
|
||||
{
|
||||
name: "project_scoped_roles",
|
||||
setup: func(mux *runtime.ServeMux) {
|
||||
require.NoError(t, services.RegisterRoleServiceHandlerServer(ctx, mux, &services.UnimplementedRoleServiceServer{}))
|
||||
},
|
||||
url: "v1/orgs/someid/projects/_someprojectid/roles",
|
||||
expectedResult: routed,
|
||||
},
|
||||
{
|
||||
name: "groups",
|
||||
setup: func(mux *runtime.ServeMux) {
|
||||
require.NoError(t, services.RegisterGroupServiceHandlerServer(ctx, mux, &services.UnimplementedGroupServiceServer{}))
|
||||
},
|
||||
url: "v1/orgs/someid/groups",
|
||||
expectedResult: routed,
|
||||
},
|
||||
{
|
||||
name: "project_scoped_groups",
|
||||
setup: func(mux *runtime.ServeMux) {
|
||||
require.NoError(t, services.RegisterGroupServiceHandlerServer(ctx, mux, &services.UnimplementedGroupServiceServer{}))
|
||||
},
|
||||
url: "v1/orgs/someid/projects/_someprojectid/groups",
|
||||
expectedResult: routed,
|
||||
},
|
||||
{
|
||||
name: "not routed",
|
||||
setup: func(mux *runtime.ServeMux) {},
|
||||
url: "v1/nothing",
|
||||
expectedResult: unrouted,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
mux := runtime.NewServeMux()
|
||||
tc.setup(mux)
|
||||
|
||||
// List request
|
||||
req := httptest.NewRequest("GET", fmt.Sprintf("http://localhost/%s", tc.url), nil)
|
||||
resp := httptest.NewRecorder()
|
||||
mux.ServeHTTP(resp, req)
|
||||
assert.Equal(t, tc.expectedResult, resp.Result().StatusCode, "Got response %v", resp)
|
||||
|
||||
// Create request
|
||||
req = httptest.NewRequest("POST", fmt.Sprintf("http://localhost/%s", tc.url), nil)
|
||||
resp = httptest.NewRecorder()
|
||||
mux.ServeHTTP(resp, req)
|
||||
assert.Equal(t, tc.expectedResult, resp.Result().StatusCode, "Got response %v", resp)
|
||||
|
||||
// Get request
|
||||
req = httptest.NewRequest("GET", fmt.Sprintf("http://localhost/%s/somemadeupid", tc.url), nil)
|
||||
resp = httptest.NewRecorder()
|
||||
mux.ServeHTTP(resp, req)
|
||||
assert.Equal(t, tc.expectedResult, resp.Result().StatusCode, "Got response %v", resp)
|
||||
|
||||
// Update request
|
||||
req = httptest.NewRequest("PATCH", fmt.Sprintf("http://localhost/%s/somemadeupid", tc.url), nil)
|
||||
resp = httptest.NewRecorder()
|
||||
mux.ServeHTTP(resp, req)
|
||||
assert.Equal(t, tc.expectedResult, resp.Result().StatusCode, "Got response %v", resp)
|
||||
|
||||
// Delete request
|
||||
req = httptest.NewRequest("DELETE", fmt.Sprintf("http://localhost/%s/somemadeupid", tc.url), nil)
|
||||
resp = httptest.NewRecorder()
|
||||
mux.ServeHTTP(resp, req)
|
||||
assert.Equal(t, tc.expectedResult, resp.Result().StatusCode, "Got response %v", resp)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleGrpcGateway(t *testing.T) {
|
||||
c := NewTestController(t, nil)
|
||||
defer c.Shutdown()
|
||||
@@ -43,6 +153,7 @@ func TestHandleGrpcGateway(t *testing.T) {
|
||||
if got, want := resp.StatusCode, tc.code; got != want {
|
||||
t.Errorf("GET on %q got code %d, wanted %d", tc.path, got, want)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,10 @@ import (
|
||||
"google.golang.org/protobuf/types/known/wrapperspb"
|
||||
)
|
||||
|
||||
const orgIdFieldName = "org_id"
|
||||
const (
|
||||
orgIdFieldName = "org_id"
|
||||
projIdFieldName = "project_id"
|
||||
)
|
||||
|
||||
var (
|
||||
reInvalidID = regexp.MustCompile("[^A-Za-z0-9]")
|
||||
@@ -37,7 +40,7 @@ type Service struct {
|
||||
// NewService returns a group service which handles group related requests to watchtower.
|
||||
func NewService(repo func() (*iam.Repository, error)) (Service, error) {
|
||||
if repo == nil {
|
||||
return Service{}, fmt.Errorf("nil iam repostiroy provided")
|
||||
return Service{}, fmt.Errorf("nil iam repository provided")
|
||||
}
|
||||
return Service{repoFn: repo}, nil
|
||||
}
|
||||
@@ -49,7 +52,7 @@ func (s Service) ListGroups(ctx context.Context, req *pbs.ListGroupsRequest) (*p
|
||||
if err := validateListRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
gl, err := s.listFromRepo(ctx, req.GetOrgId())
|
||||
gl, err := s.listFromRepo(ctx, parentScope(req))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -73,11 +76,15 @@ func (s Service) CreateGroup(ctx context.Context, req *pbs.CreateGroupRequest) (
|
||||
if err := validateCreateRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u, err := s.createInRepo(ctx, req.GetOrgId(), req.GetItem())
|
||||
u, err := s.createInRepo(ctx, parentScope(req), req.GetItem())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pbs.CreateGroupResponse{Item: u, Uri: fmt.Sprintf("orgs/%s/groups/%s", req.GetOrgId(), u.GetId())}, nil
|
||||
var projectPart string
|
||||
if req.GetProjectId() != "" {
|
||||
projectPart = fmt.Sprintf("projects/%s/", req.GetProjectId())
|
||||
}
|
||||
return &pbs.CreateGroupResponse{Item: u, Uri: fmt.Sprintf("orgs/%s/%sgroups/%s", req.GetOrgId(), projectPart, u.GetId())}, nil
|
||||
}
|
||||
|
||||
// UpdateGroup implements the interface pbs.GroupServiceServer.
|
||||
@@ -85,7 +92,7 @@ func (s Service) UpdateGroup(ctx context.Context, req *pbs.UpdateGroupRequest) (
|
||||
if err := validateUpdateRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u, err := s.updateInRepo(ctx, req.GetOrgId(), req.GetId(), req.GetUpdateMask().GetPaths(), req.GetItem())
|
||||
u, err := s.updateInRepo(ctx, parentScope(req), req.GetId(), req.GetUpdateMask().GetPaths(), req.GetItem())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -122,7 +129,7 @@ func (s Service) getFromRepo(ctx context.Context, id string) (*pb.Group, error)
|
||||
return toProto(u), nil
|
||||
}
|
||||
|
||||
func (s Service) createInRepo(ctx context.Context, orgId string, item *pb.Group) (*pb.Group, error) {
|
||||
func (s Service) createInRepo(ctx context.Context, scopeId string, item *pb.Group) (*pb.Group, error) {
|
||||
var opts []iam.Option
|
||||
if item.GetName() != nil {
|
||||
opts = append(opts, iam.WithName(item.GetName().GetValue()))
|
||||
@@ -130,7 +137,7 @@ func (s Service) createInRepo(ctx context.Context, orgId string, item *pb.Group)
|
||||
if item.GetDescription() != nil {
|
||||
opts = append(opts, iam.WithDescription(item.GetDescription().GetValue()))
|
||||
}
|
||||
u, err := iam.NewGroup(orgId, opts...)
|
||||
u, err := iam.NewGroup(scopeId, opts...)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "Unable to build group for creation: %v.", err)
|
||||
}
|
||||
@@ -148,7 +155,7 @@ func (s Service) createInRepo(ctx context.Context, orgId string, item *pb.Group)
|
||||
return toProto(out), nil
|
||||
}
|
||||
|
||||
func (s Service) updateInRepo(ctx context.Context, orgId, id string, mask []string, item *pb.Group) (*pb.Group, error) {
|
||||
func (s Service) updateInRepo(ctx context.Context, scopeId, id string, mask []string, item *pb.Group) (*pb.Group, error) {
|
||||
var opts []iam.Option
|
||||
if desc := item.GetDescription(); desc != nil {
|
||||
opts = append(opts, iam.WithDescription(desc.GetValue()))
|
||||
@@ -156,7 +163,7 @@ func (s Service) updateInRepo(ctx context.Context, orgId, id string, mask []stri
|
||||
if name := item.GetName(); name != nil {
|
||||
opts = append(opts, iam.WithName(name.GetValue()))
|
||||
}
|
||||
u, err := iam.NewGroup(orgId, opts...)
|
||||
u, err := iam.NewGroup(scopeId, opts...)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "Unable to build group for update: %v.", err)
|
||||
}
|
||||
@@ -341,6 +348,7 @@ func validId(id, prefix string) bool {
|
||||
|
||||
type ancestorProvider interface {
|
||||
GetOrgId() string
|
||||
GetProjectId() string
|
||||
}
|
||||
|
||||
// validateAncestors verifies that the ancestors of this call are properly set and provided.
|
||||
@@ -351,5 +359,16 @@ func validateAncestors(r ancestorProvider) map[string]string {
|
||||
if !validId(r.GetOrgId(), scope.Organization.Prefix()+"_") {
|
||||
return map[string]string{orgIdFieldName: "Improperly formatted identifier."}
|
||||
}
|
||||
if r.GetProjectId() != "" && !validId(r.GetProjectId(), scope.Project.Prefix()+"_") {
|
||||
return map[string]string{projIdFieldName: "Improperly formatted identifier."}
|
||||
}
|
||||
return map[string]string{}
|
||||
}
|
||||
|
||||
// Given an ancestorProvider, return the resource's immediate parent scope
|
||||
func parentScope(r ancestorProvider) string {
|
||||
if r.GetProjectId() != "" {
|
||||
return r.GetProjectId()
|
||||
}
|
||||
return r.GetOrgId()
|
||||
}
|
||||
@@ -23,7 +23,8 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func createDefaultGroupAndRepo(t *testing.T) (*iam.Group, func() (*iam.Repository, error)) {
|
||||
// Creates an org scoped group and a project scoped group.
|
||||
func createDefaultGroupsAndRepo(t *testing.T) (*iam.Group, *iam.Group, func() (*iam.Repository, error)) {
|
||||
t.Helper()
|
||||
cleanup, conn, _ := db.TestSetup(t, "postgres")
|
||||
t.Cleanup(func() {
|
||||
@@ -40,26 +41,35 @@ func createDefaultGroupAndRepo(t *testing.T) (*iam.Group, func() (*iam.Repositor
|
||||
return iam.NewRepository(rw, rw, wrap)
|
||||
}
|
||||
|
||||
o, _ := iam.TestScopes(t, conn)
|
||||
g := iam.TestGroup(t, conn, o.GetPublicId(), iam.WithDescription("default"), iam.WithName("default"))
|
||||
return g, repoFn
|
||||
o, p := iam.TestScopes(t, conn)
|
||||
og := iam.TestGroup(t, conn, o.GetPublicId(), iam.WithDescription("default"), iam.WithName("default"))
|
||||
pg := iam.TestGroup(t, conn, p.GetPublicId(), iam.WithDescription("default"), iam.WithName("default"))
|
||||
return og, pg, repoFn
|
||||
}
|
||||
|
||||
func TestGet(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
require := require.New(t)
|
||||
g, repo := createDefaultGroupAndRepo(t)
|
||||
og, pg, repo := createDefaultGroupsAndRepo(t)
|
||||
toMerge := &pbs.GetGroupRequest{
|
||||
OrgId: g.GetScopeId(),
|
||||
Id: g.GetPublicId(),
|
||||
OrgId: og.GetScopeId(),
|
||||
Id: og.GetPublicId(),
|
||||
}
|
||||
|
||||
wantU := &pb.Group{
|
||||
Id: g.GetPublicId(),
|
||||
Name: &wrapperspb.StringValue{Value: g.GetName()},
|
||||
Description: &wrapperspb.StringValue{Value: g.GetDescription()},
|
||||
CreatedTime: g.CreateTime.GetTimestamp(),
|
||||
UpdatedTime: g.UpdateTime.GetTimestamp(),
|
||||
wantOrgGroup := &pb.Group{
|
||||
Id: og.GetPublicId(),
|
||||
Name: &wrapperspb.StringValue{Value: og.GetName()},
|
||||
Description: &wrapperspb.StringValue{Value: og.GetDescription()},
|
||||
CreatedTime: og.CreateTime.GetTimestamp(),
|
||||
UpdatedTime: og.UpdateTime.GetTimestamp(),
|
||||
}
|
||||
|
||||
wantProjGroup := &pb.Group{
|
||||
Id: pg.GetPublicId(),
|
||||
Name: &wrapperspb.StringValue{Value: pg.GetName()},
|
||||
Description: &wrapperspb.StringValue{Value: pg.GetDescription()},
|
||||
CreatedTime: pg.CreateTime.GetTimestamp(),
|
||||
UpdatedTime: pg.UpdateTime.GetTimestamp(),
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
@@ -70,8 +80,8 @@ func TestGet(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
name: "Get an Existing Group",
|
||||
req: &pbs.GetGroupRequest{Id: g.GetPublicId()},
|
||||
res: &pbs.GetGroupResponse{Item: wantU},
|
||||
req: &pbs.GetGroupRequest{Id: og.GetPublicId()},
|
||||
res: &pbs.GetGroupResponse{Item: wantOrgGroup},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
@@ -92,6 +102,30 @@ func TestGet(t *testing.T) {
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
},
|
||||
{
|
||||
name: "Project Scoped Get an Existing Group",
|
||||
req: &pbs.GetGroupRequest{Id: pg.GetPublicId(), ProjectId: pg.GetScopeId()},
|
||||
res: &pbs.GetGroupResponse{Item: wantProjGroup},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Project Scoped Get a non existant Group",
|
||||
req: &pbs.GetGroupRequest{Id: iam.GroupPrefix + "_DoesntExis", ProjectId: pg.GetScopeId()},
|
||||
res: nil,
|
||||
errCode: codes.NotFound,
|
||||
},
|
||||
{
|
||||
name: "Project Scoped Wrong id prefix",
|
||||
req: &pbs.GetGroupRequest{Id: "j_1234567890", ProjectId: pg.GetScopeId()},
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
},
|
||||
{
|
||||
name: "Project Scoped space in id",
|
||||
req: &pbs.GetGroupRequest{Id: iam.GroupPrefix + "_1 23456789", ProjectId: pg.GetScopeId()},
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
@@ -124,15 +158,22 @@ func TestList(t *testing.T) {
|
||||
repoFn := func() (*iam.Repository, error) {
|
||||
return iam.NewRepository(rw, rw, wrap)
|
||||
}
|
||||
oNoGroups, _ := iam.TestScopes(t, conn)
|
||||
oWithGroups, _ := iam.TestScopes(t, conn)
|
||||
var wantGroups []*pb.Group
|
||||
oNoGroups, pWithGroups := iam.TestScopes(t, conn)
|
||||
oWithGroups, pNoGroups := iam.TestScopes(t, conn)
|
||||
var wantOrgGroups []*pb.Group
|
||||
var wantProjGroups []*pb.Group
|
||||
for i := 0; i < 10; i++ {
|
||||
g := iam.TestGroup(t, conn, oWithGroups.GetPublicId())
|
||||
wantGroups = append(wantGroups, &pb.Group{
|
||||
Id: g.GetPublicId(),
|
||||
CreatedTime: g.GetCreateTime().GetTimestamp(),
|
||||
UpdatedTime: g.GetUpdateTime().GetTimestamp(),
|
||||
og := iam.TestGroup(t, conn, oWithGroups.GetPublicId())
|
||||
wantOrgGroups = append(wantOrgGroups, &pb.Group{
|
||||
Id: og.GetPublicId(),
|
||||
CreatedTime: og.GetCreateTime().GetTimestamp(),
|
||||
UpdatedTime: og.GetUpdateTime().GetTimestamp(),
|
||||
})
|
||||
pg := iam.TestGroup(t, conn, pWithGroups.GetPublicId())
|
||||
wantProjGroups = append(wantProjGroups, &pb.Group{
|
||||
Id: pg.GetPublicId(),
|
||||
CreatedTime: pg.GetCreateTime().GetTimestamp(),
|
||||
UpdatedTime: pg.GetUpdateTime().GetTimestamp(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -145,7 +186,7 @@ func TestList(t *testing.T) {
|
||||
{
|
||||
name: "List Many Group",
|
||||
req: &pbs.ListGroupsRequest{OrgId: oWithGroups.GetPublicId()},
|
||||
res: &pbs.ListGroupsResponse{Items: wantGroups},
|
||||
res: &pbs.ListGroupsResponse{Items: wantOrgGroups},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
@@ -167,6 +208,31 @@ func TestList(t *testing.T) {
|
||||
res: &pbs.ListGroupsResponse{},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "List Many Project Group",
|
||||
req: &pbs.ListGroupsRequest{OrgId: pWithGroups.GetParentId(), ProjectId: pWithGroups.GetPublicId()},
|
||||
res: &pbs.ListGroupsResponse{Items: wantProjGroups},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "List No Project Groups",
|
||||
req: &pbs.ListGroupsRequest{OrgId: pNoGroups.GetParentId(), ProjectId: pNoGroups.GetPublicId()},
|
||||
res: &pbs.ListGroupsResponse{},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Invalid Project Id",
|
||||
req: &pbs.ListGroupsRequest{OrgId: oWithGroups.GetPublicId(), ProjectId: scope.Project.Prefix() + "_this is invalid"},
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
},
|
||||
// TODO: When an org doesn't exist, we should return a 404 instead of an empty list.
|
||||
{
|
||||
name: "Unfound Project",
|
||||
req: &pbs.ListGroupsRequest{OrgId: oWithGroups.GetPublicId(), ProjectId: scope.Project.Prefix() + "_DoesntExis"},
|
||||
res: &pbs.ListGroupsResponse{},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
@@ -182,7 +248,7 @@ func TestList(t *testing.T) {
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
require := require.New(t)
|
||||
g, repo := createDefaultGroupAndRepo(t)
|
||||
og, pg, repo := createDefaultGroupsAndRepo(t)
|
||||
|
||||
s, err := groups.NewService(repo)
|
||||
require.NoError(err, "Error when getting new group service.")
|
||||
@@ -196,8 +262,8 @@ func TestDelete(t *testing.T) {
|
||||
{
|
||||
name: "Delete an Existing Group",
|
||||
req: &pbs.DeleteGroupRequest{
|
||||
OrgId: g.GetScopeId(),
|
||||
Id: g.GetPublicId(),
|
||||
OrgId: og.GetScopeId(),
|
||||
Id: og.GetPublicId(),
|
||||
},
|
||||
res: &pbs.DeleteGroupResponse{
|
||||
Existed: true,
|
||||
@@ -207,7 +273,7 @@ func TestDelete(t *testing.T) {
|
||||
{
|
||||
name: "Delete bad group id",
|
||||
req: &pbs.DeleteGroupRequest{
|
||||
OrgId: g.GetScopeId(),
|
||||
OrgId: og.GetScopeId(),
|
||||
Id: iam.GroupPrefix + "_doesntexis",
|
||||
},
|
||||
res: &pbs.DeleteGroupResponse{
|
||||
@@ -219,7 +285,7 @@ func TestDelete(t *testing.T) {
|
||||
name: "Delete bad org id",
|
||||
req: &pbs.DeleteGroupRequest{
|
||||
OrgId: "o_doesntexis",
|
||||
Id: g.GetPublicId(),
|
||||
Id: og.GetPublicId(),
|
||||
},
|
||||
res: &pbs.DeleteGroupResponse{
|
||||
Existed: false,
|
||||
@@ -230,7 +296,7 @@ func TestDelete(t *testing.T) {
|
||||
name: "Bad org formatting",
|
||||
req: &pbs.DeleteGroupRequest{
|
||||
OrgId: "bad_format",
|
||||
Id: g.GetPublicId(),
|
||||
Id: og.GetPublicId(),
|
||||
},
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
@@ -238,12 +304,58 @@ func TestDelete(t *testing.T) {
|
||||
{
|
||||
name: "Bad Group Id formatting",
|
||||
req: &pbs.DeleteGroupRequest{
|
||||
OrgId: g.GetScopeId(),
|
||||
OrgId: og.GetScopeId(),
|
||||
Id: "bad_format",
|
||||
},
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
},
|
||||
{
|
||||
name: "Project Scoped Delete an Existing Group",
|
||||
req: &pbs.DeleteGroupRequest{
|
||||
OrgId: og.GetScopeId(),
|
||||
ProjectId: pg.GetScopeId(),
|
||||
Id: pg.GetPublicId(),
|
||||
},
|
||||
res: &pbs.DeleteGroupResponse{
|
||||
Existed: true,
|
||||
},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Project Scoped Delete bad group id",
|
||||
req: &pbs.DeleteGroupRequest{
|
||||
OrgId: og.GetScopeId(),
|
||||
ProjectId: pg.GetScopeId(),
|
||||
Id: iam.GroupPrefix + "_doesntexis",
|
||||
},
|
||||
res: &pbs.DeleteGroupResponse{
|
||||
Existed: false,
|
||||
},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Project Scoped Delete bad project id",
|
||||
req: &pbs.DeleteGroupRequest{
|
||||
OrgId: og.GetScopeId(),
|
||||
ProjectId: scope.Project.Prefix() + "_doesntexis",
|
||||
Id: pg.GetPublicId(),
|
||||
},
|
||||
res: &pbs.DeleteGroupResponse{
|
||||
Existed: false,
|
||||
},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Bad project formatting",
|
||||
req: &pbs.DeleteGroupRequest{
|
||||
OrgId: og.GetScopeId(),
|
||||
ProjectId: "bad_format",
|
||||
Id: pg.GetPublicId(),
|
||||
},
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
@@ -258,13 +370,13 @@ func TestDelete(t *testing.T) {
|
||||
func TestDelete_twice(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
require := require.New(t)
|
||||
g, repo := createDefaultGroupAndRepo(t)
|
||||
og, pg, repo := createDefaultGroupsAndRepo(t)
|
||||
|
||||
s, err := groups.NewService(repo)
|
||||
require.NoError(err, "Error when getting new group service")
|
||||
req := &pbs.DeleteGroupRequest{
|
||||
OrgId: g.GetScopeId(),
|
||||
Id: g.GetPublicId(),
|
||||
OrgId: og.GetScopeId(),
|
||||
Id: og.GetPublicId(),
|
||||
}
|
||||
got, gErr := s.DeleteGroup(context.Background(), req)
|
||||
assert.NoError(gErr, "First attempt")
|
||||
@@ -272,15 +384,27 @@ func TestDelete_twice(t *testing.T) {
|
||||
got, gErr = s.DeleteGroup(context.Background(), req)
|
||||
assert.NoError(gErr, "Second attempt")
|
||||
assert.False(got.GetExisted(), "Expected existed to be false for the second delete.")
|
||||
|
||||
projReq := &pbs.DeleteGroupRequest{
|
||||
OrgId: og.GetScopeId(),
|
||||
ProjectId: pg.GetScopeId(),
|
||||
Id: pg.GetPublicId(),
|
||||
}
|
||||
got, gErr = s.DeleteGroup(context.Background(), projReq)
|
||||
assert.NoError(gErr, "First attempt")
|
||||
assert.True(got.GetExisted(), "Expected existed to be true for the first delete.")
|
||||
got, gErr = s.DeleteGroup(context.Background(), projReq)
|
||||
assert.NoError(gErr, "Second attempt")
|
||||
assert.False(got.GetExisted(), "Expected existed to be false for the second delete.")
|
||||
}
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
require := require.New(t)
|
||||
defaultGroup, repo := createDefaultGroupAndRepo(t)
|
||||
defaultCreated, err := ptypes.Timestamp(defaultGroup.GetCreateTime().GetTimestamp())
|
||||
defaultOGroup, defaultPGroup, repo := createDefaultGroupsAndRepo(t)
|
||||
defaultCreated, err := ptypes.Timestamp(defaultOGroup.GetCreateTime().GetTimestamp())
|
||||
require.NoError(err, "Error converting proto to timestamp.")
|
||||
toMerge := &pbs.CreateGroupRequest{
|
||||
OrgId: defaultGroup.GetScopeId(),
|
||||
OrgId: defaultOGroup.GetScopeId(),
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
@@ -296,7 +420,25 @@ func TestCreate(t *testing.T) {
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
}},
|
||||
res: &pbs.CreateGroupResponse{
|
||||
Uri: fmt.Sprintf("orgs/%s/groups/%s_", defaultGroup.GetScopeId(), iam.GroupPrefix),
|
||||
Uri: fmt.Sprintf("orgs/%s/groups/%s_", defaultOGroup.GetScopeId(), iam.GroupPrefix),
|
||||
Item: &pb.Group{
|
||||
Name: &wrapperspb.StringValue{Value: "name"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Create a valid Project Scoped Group",
|
||||
req: &pbs.CreateGroupRequest{
|
||||
ProjectId: defaultPGroup.GetScopeId(),
|
||||
Item: &pb.Group{
|
||||
Name: &wrapperspb.StringValue{Value: "name"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
},
|
||||
},
|
||||
res: &pbs.CreateGroupResponse{
|
||||
Uri: fmt.Sprintf("orgs/%s/projects/%s/groups/%s_", defaultOGroup.GetScopeId(), defaultPGroup.GetScopeId(), iam.GroupPrefix),
|
||||
Item: &pb.Group{
|
||||
Name: &wrapperspb.StringValue{Value: "name"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
@@ -363,22 +505,24 @@ func TestCreate(t *testing.T) {
|
||||
|
||||
func TestUpdate(t *testing.T) {
|
||||
require := require.New(t)
|
||||
g, repoFn := createDefaultGroupAndRepo(t)
|
||||
og, pg, repoFn := createDefaultGroupsAndRepo(t)
|
||||
tested, err := groups.NewService(repoFn)
|
||||
require.NoError(err, "Error when getting new group service.")
|
||||
|
||||
resetGroup := func() {
|
||||
resetGroups := func() {
|
||||
repo, err := repoFn()
|
||||
require.NoError(err, "Couldn't get a new repo")
|
||||
g, _, err = repo.UpdateGroup(context.Background(), g, []string{"Name", "Description"})
|
||||
og, _, err = repo.UpdateGroup(context.Background(), og, []string{"Name", "Description"})
|
||||
require.NoError(err, "Failed to reset the group")
|
||||
pg, _, err = repo.UpdateGroup(context.Background(), pg, []string{"Name", "Description"})
|
||||
require.NoError(err, "Failed to reset the group")
|
||||
}
|
||||
|
||||
created, err := ptypes.Timestamp(g.GetCreateTime().GetTimestamp())
|
||||
created, err := ptypes.Timestamp(og.GetCreateTime().GetTimestamp())
|
||||
require.NoError(err, "Error converting proto to timestamp")
|
||||
toMerge := &pbs.UpdateGroupRequest{
|
||||
OrgId: g.GetScopeId(),
|
||||
Id: g.GetPublicId(),
|
||||
OrgId: og.GetScopeId(),
|
||||
Id: og.GetPublicId(),
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
@@ -400,10 +544,10 @@ func TestUpdate(t *testing.T) {
|
||||
},
|
||||
res: &pbs.UpdateGroupResponse{
|
||||
Item: &pb.Group{
|
||||
Id: g.GetPublicId(),
|
||||
Id: og.GetPublicId(),
|
||||
Name: &wrapperspb.StringValue{Value: "new"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
CreatedTime: g.GetCreateTime().GetTimestamp(),
|
||||
CreatedTime: og.GetCreateTime().GetTimestamp(),
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
@@ -421,10 +565,56 @@ func TestUpdate(t *testing.T) {
|
||||
},
|
||||
res: &pbs.UpdateGroupResponse{
|
||||
Item: &pb.Group{
|
||||
Id: g.GetPublicId(),
|
||||
Id: og.GetPublicId(),
|
||||
Name: &wrapperspb.StringValue{Value: "new"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
CreatedTime: g.GetCreateTime().GetTimestamp(),
|
||||
CreatedTime: og.GetCreateTime().GetTimestamp(),
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Update an Existing Project Scoped Group",
|
||||
req: &pbs.UpdateGroupRequest{
|
||||
ProjectId: pg.GetScopeId(),
|
||||
Id: pg.GetPublicId(),
|
||||
UpdateMask: &field_mask.FieldMask{
|
||||
Paths: []string{"name", "description"},
|
||||
},
|
||||
Item: &pb.Group{
|
||||
Name: &wrapperspb.StringValue{Value: "new"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
},
|
||||
},
|
||||
res: &pbs.UpdateGroupResponse{
|
||||
Item: &pb.Group{
|
||||
Id: pg.GetPublicId(),
|
||||
Name: &wrapperspb.StringValue{Value: "new"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
CreatedTime: pg.GetCreateTime().GetTimestamp(),
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Multiple Paths in single string",
|
||||
req: &pbs.UpdateGroupRequest{
|
||||
ProjectId: pg.GetScopeId(),
|
||||
Id: pg.GetPublicId(),
|
||||
UpdateMask: &field_mask.FieldMask{
|
||||
Paths: []string{"name,description"},
|
||||
},
|
||||
Item: &pb.Group{
|
||||
Name: &wrapperspb.StringValue{Value: "new"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
},
|
||||
},
|
||||
res: &pbs.UpdateGroupResponse{
|
||||
Item: &pb.Group{
|
||||
Id: pg.GetPublicId(),
|
||||
Name: &wrapperspb.StringValue{Value: "new"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
CreatedTime: pg.GetCreateTime().GetTimestamp(),
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
@@ -473,9 +663,9 @@ func TestUpdate(t *testing.T) {
|
||||
},
|
||||
res: &pbs.UpdateGroupResponse{
|
||||
Item: &pb.Group{
|
||||
Id: g.GetPublicId(),
|
||||
Id: og.GetPublicId(),
|
||||
Description: &wrapperspb.StringValue{Value: "default"},
|
||||
CreatedTime: g.GetCreateTime().GetTimestamp(),
|
||||
CreatedTime: og.GetCreateTime().GetTimestamp(),
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
@@ -493,10 +683,10 @@ func TestUpdate(t *testing.T) {
|
||||
},
|
||||
res: &pbs.UpdateGroupResponse{
|
||||
Item: &pb.Group{
|
||||
Id: g.GetPublicId(),
|
||||
Id: og.GetPublicId(),
|
||||
Name: &wrapperspb.StringValue{Value: "updated"},
|
||||
Description: &wrapperspb.StringValue{Value: "default"},
|
||||
CreatedTime: g.GetCreateTime().GetTimestamp(),
|
||||
CreatedTime: og.GetCreateTime().GetTimestamp(),
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
@@ -514,10 +704,10 @@ func TestUpdate(t *testing.T) {
|
||||
},
|
||||
res: &pbs.UpdateGroupResponse{
|
||||
Item: &pb.Group{
|
||||
Id: g.GetPublicId(),
|
||||
Id: og.GetPublicId(),
|
||||
Name: &wrapperspb.StringValue{Value: "default"},
|
||||
Description: &wrapperspb.StringValue{Value: "notignored"},
|
||||
CreatedTime: g.GetCreateTime().GetTimestamp(),
|
||||
CreatedTime: og.GetCreateTime().GetTimestamp(),
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
@@ -541,7 +731,7 @@ func TestUpdate(t *testing.T) {
|
||||
{
|
||||
name: "Cant change Id",
|
||||
req: &pbs.UpdateGroupRequest{
|
||||
Id: g.GetPublicId(),
|
||||
Id: og.GetPublicId(),
|
||||
UpdateMask: &field_mask.FieldMask{
|
||||
Paths: []string{"id"},
|
||||
},
|
||||
@@ -582,7 +772,7 @@ func TestUpdate(t *testing.T) {
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
defer resetGroup()
|
||||
defer resetGroups()
|
||||
assert := assert.New(t)
|
||||
req := proto.Clone(toMerge).(*pbs.UpdateGroupRequest)
|
||||
proto.Merge(req, tc.req)
|
||||
|
||||
+31
-12
@@ -18,7 +18,10 @@ import (
|
||||
"google.golang.org/protobuf/types/known/wrapperspb"
|
||||
)
|
||||
|
||||
const orgIdFieldName = "org_id"
|
||||
const (
|
||||
orgIdFieldName = "org_id"
|
||||
projIdFieldName = "project_id"
|
||||
)
|
||||
|
||||
var (
|
||||
reInvalidID = regexp.MustCompile("[^A-Za-z0-9]")
|
||||
@@ -37,7 +40,7 @@ type Service struct {
|
||||
// NewService returns a role service which handles role related requests to watchtower.
|
||||
func NewService(repo func() (*iam.Repository, error)) (Service, error) {
|
||||
if repo == nil {
|
||||
return Service{}, fmt.Errorf("nil iam repostiroy provided")
|
||||
return Service{}, fmt.Errorf("nil iam repository provided")
|
||||
}
|
||||
return Service{repoFn: repo}, nil
|
||||
}
|
||||
@@ -49,7 +52,7 @@ func (s Service) ListRoles(ctx context.Context, req *pbs.ListRolesRequest) (*pbs
|
||||
if err := validateListRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
gl, err := s.listFromRepo(ctx, req.GetOrgId())
|
||||
gl, err := s.listFromRepo(ctx, parentScope(req))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -73,11 +76,15 @@ func (s Service) CreateRole(ctx context.Context, req *pbs.CreateRoleRequest) (*p
|
||||
if err := validateCreateRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r, err := s.createInRepo(ctx, req.GetOrgId(), req.GetItem())
|
||||
r, err := s.createInRepo(ctx, parentScope(req), req.GetItem())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pbs.CreateRoleResponse{Item: r, Uri: fmt.Sprintf("orgs/%s/roles/%s", req.GetOrgId(), r.GetId())}, nil
|
||||
var projectPart string
|
||||
if req.GetProjectId() != "" {
|
||||
projectPart = fmt.Sprintf("projects/%s/", req.GetProjectId())
|
||||
}
|
||||
return &pbs.CreateRoleResponse{Item: r, Uri: fmt.Sprintf("orgs/%s/%sroles/%s", req.GetOrgId(), projectPart, r.GetId())}, nil
|
||||
}
|
||||
|
||||
// UpdateRole implements the interface pbs.RoleServiceServer.
|
||||
@@ -85,7 +92,7 @@ func (s Service) UpdateRole(ctx context.Context, req *pbs.UpdateRoleRequest) (*p
|
||||
if err := validateUpdateRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u, err := s.updateInRepo(ctx, req.GetOrgId(), req.GetId(), req.GetUpdateMask().GetPaths(), req.GetItem())
|
||||
u, err := s.updateInRepo(ctx, parentScope(req), req.GetId(), req.GetUpdateMask().GetPaths(), req.GetItem())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -122,7 +129,7 @@ func (s Service) getFromRepo(ctx context.Context, id string) (*pb.Role, error) {
|
||||
return toProto(u), nil
|
||||
}
|
||||
|
||||
func (s Service) createInRepo(ctx context.Context, orgId string, item *pb.Role) (*pb.Role, error) {
|
||||
func (s Service) createInRepo(ctx context.Context, scopeId string, item *pb.Role) (*pb.Role, error) {
|
||||
var opts []iam.Option
|
||||
if item.GetName() != nil {
|
||||
opts = append(opts, iam.WithName(item.GetName().GetValue()))
|
||||
@@ -130,7 +137,7 @@ func (s Service) createInRepo(ctx context.Context, orgId string, item *pb.Role)
|
||||
if item.GetDescription() != nil {
|
||||
opts = append(opts, iam.WithDescription(item.GetDescription().GetValue()))
|
||||
}
|
||||
u, err := iam.NewRole(orgId, opts...)
|
||||
u, err := iam.NewRole(scopeId, opts...)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "Unable to build role for creation: %v.", err)
|
||||
}
|
||||
@@ -148,7 +155,7 @@ func (s Service) createInRepo(ctx context.Context, orgId string, item *pb.Role)
|
||||
return toProto(out), nil
|
||||
}
|
||||
|
||||
func (s Service) updateInRepo(ctx context.Context, orgId, id string, mask []string, item *pb.Role) (*pb.Role, error) {
|
||||
func (s Service) updateInRepo(ctx context.Context, scopeId, id string, mask []string, item *pb.Role) (*pb.Role, error) {
|
||||
var opts []iam.Option
|
||||
if desc := item.GetDescription(); desc != nil {
|
||||
opts = append(opts, iam.WithDescription(desc.GetValue()))
|
||||
@@ -156,7 +163,7 @@ func (s Service) updateInRepo(ctx context.Context, orgId, id string, mask []stri
|
||||
if name := item.GetName(); name != nil {
|
||||
opts = append(opts, iam.WithName(name.GetValue()))
|
||||
}
|
||||
u, err := iam.NewRole(orgId, opts...)
|
||||
u, err := iam.NewRole(scopeId, opts...)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "Unable to build role for update: %v.", err)
|
||||
}
|
||||
@@ -197,12 +204,12 @@ func (s Service) deleteFromRepo(ctx context.Context, id string) (bool, error) {
|
||||
return rows > 0, nil
|
||||
}
|
||||
|
||||
func (s Service) listFromRepo(ctx context.Context, orgId string) ([]*pb.Role, error) {
|
||||
func (s Service) listFromRepo(ctx context.Context, scopeId string) ([]*pb.Role, error) {
|
||||
repo, err := s.repoFn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rl, err := repo.ListRoles(ctx, orgId)
|
||||
rl, err := repo.ListRoles(ctx, scopeId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -341,6 +348,7 @@ func validId(id, prefix string) bool {
|
||||
|
||||
type ancestorProvider interface {
|
||||
GetOrgId() string
|
||||
GetProjectId() string
|
||||
}
|
||||
|
||||
// validateAncestors verifies that the ancestors of this call are properly set and provided.
|
||||
@@ -351,5 +359,16 @@ func validateAncestors(r ancestorProvider) map[string]string {
|
||||
if !validId(r.GetOrgId(), scope.Organization.Prefix()+"_") {
|
||||
return map[string]string{orgIdFieldName: "Improperly formatted identifier."}
|
||||
}
|
||||
if r.GetProjectId() != "" && !validId(r.GetProjectId(), scope.Project.Prefix()+"_") {
|
||||
return map[string]string{projIdFieldName: "Improperly formatted identifier."}
|
||||
}
|
||||
return map[string]string{}
|
||||
}
|
||||
|
||||
// Given an ancestorProvider, return the resource's immediate parent scope
|
||||
func parentScope(r ancestorProvider) string {
|
||||
if r.GetProjectId() != "" {
|
||||
return r.GetProjectId()
|
||||
}
|
||||
return r.GetOrgId()
|
||||
}
|
||||
+246
-56
@@ -23,7 +23,7 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func createDefaultRoleAndRepo(t *testing.T) (*iam.Role, func() (*iam.Repository, error)) {
|
||||
func createDefaultRolesAndRepo(t *testing.T) (*iam.Role, *iam.Role, func() (*iam.Repository, error)) {
|
||||
t.Helper()
|
||||
cleanup, conn, _ := db.TestSetup(t, "postgres")
|
||||
t.Cleanup(func() {
|
||||
@@ -40,26 +40,35 @@ func createDefaultRoleAndRepo(t *testing.T) (*iam.Role, func() (*iam.Repository,
|
||||
return iam.NewRepository(rw, rw, wrap)
|
||||
}
|
||||
|
||||
o, _ := iam.TestScopes(t, conn)
|
||||
u := iam.TestRole(t, conn, o.GetPublicId(), iam.WithDescription("default"), iam.WithName("default"))
|
||||
return u, repoFn
|
||||
o, p := iam.TestScopes(t, conn)
|
||||
or := iam.TestRole(t, conn, o.GetPublicId(), iam.WithDescription("default"), iam.WithName("default"))
|
||||
pr := iam.TestRole(t, conn, p.GetPublicId(), iam.WithDescription("default"), iam.WithName("default"))
|
||||
return or, pr, repoFn
|
||||
}
|
||||
|
||||
func TestGet(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
require := require.New(t)
|
||||
u, repo := createDefaultRoleAndRepo(t)
|
||||
or, pr, repo := createDefaultRolesAndRepo(t)
|
||||
toMerge := &pbs.GetRoleRequest{
|
||||
OrgId: u.GetScopeId(),
|
||||
Id: u.GetPublicId(),
|
||||
OrgId: or.GetScopeId(),
|
||||
Id: or.GetPublicId(),
|
||||
}
|
||||
|
||||
wantU := &pb.Role{
|
||||
Id: u.GetPublicId(),
|
||||
Name: &wrapperspb.StringValue{Value: u.GetName()},
|
||||
Description: &wrapperspb.StringValue{Value: u.GetDescription()},
|
||||
CreatedTime: u.CreateTime.GetTimestamp(),
|
||||
UpdatedTime: u.UpdateTime.GetTimestamp(),
|
||||
wantOrgRole := &pb.Role{
|
||||
Id: or.GetPublicId(),
|
||||
Name: &wrapperspb.StringValue{Value: or.GetName()},
|
||||
Description: &wrapperspb.StringValue{Value: or.GetDescription()},
|
||||
CreatedTime: or.CreateTime.GetTimestamp(),
|
||||
UpdatedTime: or.UpdateTime.GetTimestamp(),
|
||||
}
|
||||
|
||||
wantProjRole := &pb.Role{
|
||||
Id: pr.GetPublicId(),
|
||||
Name: &wrapperspb.StringValue{Value: pr.GetName()},
|
||||
Description: &wrapperspb.StringValue{Value: pr.GetDescription()},
|
||||
CreatedTime: pr.CreateTime.GetTimestamp(),
|
||||
UpdatedTime: pr.UpdateTime.GetTimestamp(),
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
@@ -70,8 +79,8 @@ func TestGet(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
name: "Get an Existing Role",
|
||||
req: &pbs.GetRoleRequest{Id: u.GetPublicId()},
|
||||
res: &pbs.GetRoleResponse{Item: wantU},
|
||||
req: &pbs.GetRoleRequest{Id: or.GetPublicId()},
|
||||
res: &pbs.GetRoleResponse{Item: wantOrgRole},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
@@ -92,6 +101,30 @@ func TestGet(t *testing.T) {
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
},
|
||||
{
|
||||
name: "Project Scoped Get an Existing Role",
|
||||
req: &pbs.GetRoleRequest{Id: pr.GetPublicId(), ProjectId: pr.GetScopeId()},
|
||||
res: &pbs.GetRoleResponse{Item: wantProjRole},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Project Scoped Get a non existant Role",
|
||||
req: &pbs.GetRoleRequest{Id: iam.RolePrefix + "_DoesntExis", ProjectId: pr.GetScopeId()},
|
||||
res: nil,
|
||||
errCode: codes.NotFound,
|
||||
},
|
||||
{
|
||||
name: "Project Scoped Wrong id prefix",
|
||||
req: &pbs.GetRoleRequest{Id: "j_1234567890", ProjectId: pr.GetScopeId()},
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
},
|
||||
{
|
||||
name: "Project Scoped space in id",
|
||||
req: &pbs.GetRoleRequest{Id: iam.RolePrefix + "_1 23456789", ProjectId: pr.GetScopeId()},
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
@@ -124,15 +157,22 @@ func TestList(t *testing.T) {
|
||||
repoFn := func() (*iam.Repository, error) {
|
||||
return iam.NewRepository(rw, rw, wrap)
|
||||
}
|
||||
oNoRoles, _ := iam.TestScopes(t, conn)
|
||||
oWithRoles, _ := iam.TestScopes(t, conn)
|
||||
var wantRoles []*pb.Role
|
||||
oNoRoles, pWithRoles := iam.TestScopes(t, conn)
|
||||
oWithRoles, pNoRoles := iam.TestScopes(t, conn)
|
||||
var wantOrgRoles []*pb.Role
|
||||
var wantProjRoles []*pb.Role
|
||||
for i := 0; i < 10; i++ {
|
||||
g := iam.TestRole(t, conn, oWithRoles.GetPublicId())
|
||||
wantRoles = append(wantRoles, &pb.Role{
|
||||
Id: g.GetPublicId(),
|
||||
CreatedTime: g.GetCreateTime().GetTimestamp(),
|
||||
UpdatedTime: g.GetUpdateTime().GetTimestamp(),
|
||||
or := iam.TestRole(t, conn, oWithRoles.GetPublicId())
|
||||
wantOrgRoles = append(wantOrgRoles, &pb.Role{
|
||||
Id: or.GetPublicId(),
|
||||
CreatedTime: or.GetCreateTime().GetTimestamp(),
|
||||
UpdatedTime: or.GetUpdateTime().GetTimestamp(),
|
||||
})
|
||||
pr := iam.TestRole(t, conn, pWithRoles.GetPublicId())
|
||||
wantProjRoles = append(wantProjRoles, &pb.Role{
|
||||
Id: pr.GetPublicId(),
|
||||
CreatedTime: pr.GetCreateTime().GetTimestamp(),
|
||||
UpdatedTime: pr.GetUpdateTime().GetTimestamp(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -145,7 +185,7 @@ func TestList(t *testing.T) {
|
||||
{
|
||||
name: "List Many Role",
|
||||
req: &pbs.ListRolesRequest{OrgId: oWithRoles.GetPublicId()},
|
||||
res: &pbs.ListRolesResponse{Items: wantRoles},
|
||||
res: &pbs.ListRolesResponse{Items: wantOrgRoles},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
@@ -167,6 +207,31 @@ func TestList(t *testing.T) {
|
||||
res: &pbs.ListRolesResponse{},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "List Many Project Role",
|
||||
req: &pbs.ListRolesRequest{OrgId: pWithRoles.GetParentId(), ProjectId: pWithRoles.GetPublicId()},
|
||||
res: &pbs.ListRolesResponse{Items: wantProjRoles},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "List No Project Roles",
|
||||
req: &pbs.ListRolesRequest{OrgId: pNoRoles.GetParentId(), ProjectId: pNoRoles.GetPublicId()},
|
||||
res: &pbs.ListRolesResponse{},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Invalid Project Id",
|
||||
req: &pbs.ListRolesRequest{OrgId: oWithRoles.GetPublicId(), ProjectId: scope.Project.Prefix() + "_this is invalid"},
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
},
|
||||
// TODO: When an org doesn't exist, we should return a 404 instead of an empty list.
|
||||
{
|
||||
name: "Unfound Project",
|
||||
req: &pbs.ListRolesRequest{OrgId: oWithRoles.GetPublicId(), ProjectId: scope.Project.Prefix() + "_DoesntExis"},
|
||||
res: &pbs.ListRolesResponse{},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
@@ -182,7 +247,7 @@ func TestList(t *testing.T) {
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
require := require.New(t)
|
||||
u, repo := createDefaultRoleAndRepo(t)
|
||||
or, pr, repo := createDefaultRolesAndRepo(t)
|
||||
|
||||
s, err := roles.NewService(repo)
|
||||
require.NoError(err, "Error when getting new role service.")
|
||||
@@ -196,8 +261,8 @@ func TestDelete(t *testing.T) {
|
||||
{
|
||||
name: "Delete an Existing Role",
|
||||
req: &pbs.DeleteRoleRequest{
|
||||
OrgId: u.GetScopeId(),
|
||||
Id: u.GetPublicId(),
|
||||
OrgId: or.GetScopeId(),
|
||||
Id: or.GetPublicId(),
|
||||
},
|
||||
res: &pbs.DeleteRoleResponse{
|
||||
Existed: true,
|
||||
@@ -207,7 +272,7 @@ func TestDelete(t *testing.T) {
|
||||
{
|
||||
name: "Delete bad role id",
|
||||
req: &pbs.DeleteRoleRequest{
|
||||
OrgId: u.GetScopeId(),
|
||||
OrgId: or.GetScopeId(),
|
||||
Id: iam.RolePrefix + "_doesntexis",
|
||||
},
|
||||
res: &pbs.DeleteRoleResponse{
|
||||
@@ -219,7 +284,7 @@ func TestDelete(t *testing.T) {
|
||||
name: "Delete bad org id",
|
||||
req: &pbs.DeleteRoleRequest{
|
||||
OrgId: "o_doesntexis",
|
||||
Id: u.GetPublicId(),
|
||||
Id: or.GetPublicId(),
|
||||
},
|
||||
res: &pbs.DeleteRoleResponse{
|
||||
Existed: false,
|
||||
@@ -230,7 +295,7 @@ func TestDelete(t *testing.T) {
|
||||
name: "Bad org formatting",
|
||||
req: &pbs.DeleteRoleRequest{
|
||||
OrgId: "bad_format",
|
||||
Id: u.GetPublicId(),
|
||||
Id: or.GetPublicId(),
|
||||
},
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
@@ -238,12 +303,58 @@ func TestDelete(t *testing.T) {
|
||||
{
|
||||
name: "Bad Role Id formatting",
|
||||
req: &pbs.DeleteRoleRequest{
|
||||
OrgId: u.GetScopeId(),
|
||||
OrgId: or.GetScopeId(),
|
||||
Id: "bad_format",
|
||||
},
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
},
|
||||
{
|
||||
name: "Project Scoped Delete an Existing Role",
|
||||
req: &pbs.DeleteRoleRequest{
|
||||
OrgId: or.GetScopeId(),
|
||||
ProjectId: pr.GetScopeId(),
|
||||
Id: pr.GetPublicId(),
|
||||
},
|
||||
res: &pbs.DeleteRoleResponse{
|
||||
Existed: true,
|
||||
},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Project Scoped Delete bad Role id",
|
||||
req: &pbs.DeleteRoleRequest{
|
||||
OrgId: or.GetScopeId(),
|
||||
ProjectId: pr.GetScopeId(),
|
||||
Id: iam.RolePrefix + "_doesntexis",
|
||||
},
|
||||
res: &pbs.DeleteRoleResponse{
|
||||
Existed: false,
|
||||
},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Project Scoped Delete bad project id",
|
||||
req: &pbs.DeleteRoleRequest{
|
||||
OrgId: or.GetScopeId(),
|
||||
ProjectId: scope.Project.Prefix() + "_doesntexis",
|
||||
Id: pr.GetPublicId(),
|
||||
},
|
||||
res: &pbs.DeleteRoleResponse{
|
||||
Existed: false,
|
||||
},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Bad project formatting",
|
||||
req: &pbs.DeleteRoleRequest{
|
||||
OrgId: or.GetScopeId(),
|
||||
ProjectId: "bad_format",
|
||||
Id: pr.GetPublicId(),
|
||||
},
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
@@ -258,13 +369,13 @@ func TestDelete(t *testing.T) {
|
||||
func TestDelete_twice(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
require := require.New(t)
|
||||
u, repo := createDefaultRoleAndRepo(t)
|
||||
or, pr, repo := createDefaultRolesAndRepo(t)
|
||||
|
||||
s, err := roles.NewService(repo)
|
||||
require.NoError(err, "Error when getting new role service")
|
||||
req := &pbs.DeleteRoleRequest{
|
||||
OrgId: u.GetScopeId(),
|
||||
Id: u.GetPublicId(),
|
||||
OrgId: or.GetScopeId(),
|
||||
Id: or.GetPublicId(),
|
||||
}
|
||||
got, gErr := s.DeleteRole(context.Background(), req)
|
||||
assert.NoError(gErr, "First attempt")
|
||||
@@ -272,15 +383,28 @@ func TestDelete_twice(t *testing.T) {
|
||||
got, gErr = s.DeleteRole(context.Background(), req)
|
||||
assert.NoError(gErr, "Second attempt")
|
||||
assert.False(got.GetExisted(), "Expected existed to be false for the second delete.")
|
||||
|
||||
projReq := &pbs.DeleteRoleRequest{
|
||||
OrgId: or.GetScopeId(),
|
||||
ProjectId: pr.GetScopeId(),
|
||||
Id: pr.GetPublicId(),
|
||||
}
|
||||
got, gErr = s.DeleteRole(context.Background(), projReq)
|
||||
assert.NoError(gErr, "First attempt")
|
||||
assert.True(got.GetExisted(), "Expected existed to be true for the first delete.")
|
||||
got, gErr = s.DeleteRole(context.Background(), projReq)
|
||||
assert.NoError(gErr, "Second attempt")
|
||||
assert.False(got.GetExisted(), "Expected existed to be false for the second delete.")
|
||||
|
||||
}
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
require := require.New(t)
|
||||
defaultRole, repo := createDefaultRoleAndRepo(t)
|
||||
defaultCreated, err := ptypes.Timestamp(defaultRole.GetCreateTime().GetTimestamp())
|
||||
defaultOrgRole, defaultProjRole, repo := createDefaultRolesAndRepo(t)
|
||||
defaultCreated, err := ptypes.Timestamp(defaultOrgRole.GetCreateTime().GetTimestamp())
|
||||
require.NoError(err, "Error converting proto to timestamp.")
|
||||
toMerge := &pbs.CreateRoleRequest{
|
||||
OrgId: defaultRole.GetScopeId(),
|
||||
OrgId: defaultOrgRole.GetScopeId(),
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
@@ -296,7 +420,25 @@ func TestCreate(t *testing.T) {
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
}},
|
||||
res: &pbs.CreateRoleResponse{
|
||||
Uri: fmt.Sprintf("orgs/%s/roles/%s_", defaultRole.GetScopeId(), iam.RolePrefix),
|
||||
Uri: fmt.Sprintf("orgs/%s/roles/%s_", defaultOrgRole.GetScopeId(), iam.RolePrefix),
|
||||
Item: &pb.Role{
|
||||
Name: &wrapperspb.StringValue{Value: "name"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Create a valid Project Scoped Role",
|
||||
req: &pbs.CreateRoleRequest{
|
||||
ProjectId: defaultProjRole.GetScopeId(),
|
||||
Item: &pb.Role{
|
||||
Name: &wrapperspb.StringValue{Value: "name"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
},
|
||||
},
|
||||
res: &pbs.CreateRoleResponse{
|
||||
Uri: fmt.Sprintf("orgs/%s/projects/%s/roles/%s_", defaultOrgRole.GetScopeId(), defaultProjRole.GetScopeId(), iam.RolePrefix),
|
||||
Item: &pb.Role{
|
||||
Name: &wrapperspb.StringValue{Value: "name"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
@@ -363,22 +505,24 @@ func TestCreate(t *testing.T) {
|
||||
|
||||
func TestUpdate(t *testing.T) {
|
||||
require := require.New(t)
|
||||
u, repoFn := createDefaultRoleAndRepo(t)
|
||||
or, pr, repoFn := createDefaultRolesAndRepo(t)
|
||||
tested, err := roles.NewService(repoFn)
|
||||
require.NoError(err, "Error when getting new role service.")
|
||||
|
||||
resetRole := func() {
|
||||
resetRoles := func() {
|
||||
repo, err := repoFn()
|
||||
require.NoError(err, "Couldn't get a new repo")
|
||||
u, _, err = repo.UpdateRole(context.Background(), u, []string{"Name", "Description"})
|
||||
or, _, err = repo.UpdateRole(context.Background(), or, []string{"Name", "Description"})
|
||||
require.NoError(err, "Failed to reset the role")
|
||||
pr, _, err = repo.UpdateRole(context.Background(), pr, []string{"Name", "Description"})
|
||||
require.NoError(err, "Failed to reset the role")
|
||||
}
|
||||
|
||||
created, err := ptypes.Timestamp(u.GetCreateTime().GetTimestamp())
|
||||
created, err := ptypes.Timestamp(or.GetCreateTime().GetTimestamp())
|
||||
require.NoError(err, "Error converting proto to timestamp")
|
||||
toMerge := &pbs.UpdateRoleRequest{
|
||||
OrgId: u.GetScopeId(),
|
||||
Id: u.GetPublicId(),
|
||||
OrgId: or.GetScopeId(),
|
||||
Id: or.GetPublicId(),
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
@@ -400,10 +544,10 @@ func TestUpdate(t *testing.T) {
|
||||
},
|
||||
res: &pbs.UpdateRoleResponse{
|
||||
Item: &pb.Role{
|
||||
Id: u.GetPublicId(),
|
||||
Id: or.GetPublicId(),
|
||||
Name: &wrapperspb.StringValue{Value: "new"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
CreatedTime: u.GetCreateTime().GetTimestamp(),
|
||||
CreatedTime: or.GetCreateTime().GetTimestamp(),
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
@@ -421,10 +565,56 @@ func TestUpdate(t *testing.T) {
|
||||
},
|
||||
res: &pbs.UpdateRoleResponse{
|
||||
Item: &pb.Role{
|
||||
Id: u.GetPublicId(),
|
||||
Id: or.GetPublicId(),
|
||||
Name: &wrapperspb.StringValue{Value: "new"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
CreatedTime: u.GetCreateTime().GetTimestamp(),
|
||||
CreatedTime: or.GetCreateTime().GetTimestamp(),
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Update an Existing Project Scoped Role",
|
||||
req: &pbs.UpdateRoleRequest{
|
||||
ProjectId: pr.GetScopeId(),
|
||||
Id: pr.GetPublicId(),
|
||||
UpdateMask: &field_mask.FieldMask{
|
||||
Paths: []string{"name", "description"},
|
||||
},
|
||||
Item: &pb.Role{
|
||||
Name: &wrapperspb.StringValue{Value: "new"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
},
|
||||
},
|
||||
res: &pbs.UpdateRoleResponse{
|
||||
Item: &pb.Role{
|
||||
Id: pr.GetPublicId(),
|
||||
Name: &wrapperspb.StringValue{Value: "new"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
CreatedTime: pr.GetCreateTime().GetTimestamp(),
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Multiple Paths in single string",
|
||||
req: &pbs.UpdateRoleRequest{
|
||||
ProjectId: pr.GetScopeId(),
|
||||
Id: pr.GetPublicId(),
|
||||
UpdateMask: &field_mask.FieldMask{
|
||||
Paths: []string{"name,description"},
|
||||
},
|
||||
Item: &pb.Role{
|
||||
Name: &wrapperspb.StringValue{Value: "new"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
},
|
||||
},
|
||||
res: &pbs.UpdateRoleResponse{
|
||||
Item: &pb.Role{
|
||||
Id: pr.GetPublicId(),
|
||||
Name: &wrapperspb.StringValue{Value: "new"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
CreatedTime: pr.GetCreateTime().GetTimestamp(),
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
@@ -473,9 +663,9 @@ func TestUpdate(t *testing.T) {
|
||||
},
|
||||
res: &pbs.UpdateRoleResponse{
|
||||
Item: &pb.Role{
|
||||
Id: u.GetPublicId(),
|
||||
Id: or.GetPublicId(),
|
||||
Description: &wrapperspb.StringValue{Value: "default"},
|
||||
CreatedTime: u.GetCreateTime().GetTimestamp(),
|
||||
CreatedTime: or.GetCreateTime().GetTimestamp(),
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
@@ -493,10 +683,10 @@ func TestUpdate(t *testing.T) {
|
||||
},
|
||||
res: &pbs.UpdateRoleResponse{
|
||||
Item: &pb.Role{
|
||||
Id: u.GetPublicId(),
|
||||
Id: or.GetPublicId(),
|
||||
Name: &wrapperspb.StringValue{Value: "updated"},
|
||||
Description: &wrapperspb.StringValue{Value: "default"},
|
||||
CreatedTime: u.GetCreateTime().GetTimestamp(),
|
||||
CreatedTime: or.GetCreateTime().GetTimestamp(),
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
@@ -514,10 +704,10 @@ func TestUpdate(t *testing.T) {
|
||||
},
|
||||
res: &pbs.UpdateRoleResponse{
|
||||
Item: &pb.Role{
|
||||
Id: u.GetPublicId(),
|
||||
Id: or.GetPublicId(),
|
||||
Name: &wrapperspb.StringValue{Value: "default"},
|
||||
Description: &wrapperspb.StringValue{Value: "notignored"},
|
||||
CreatedTime: u.GetCreateTime().GetTimestamp(),
|
||||
CreatedTime: or.GetCreateTime().GetTimestamp(),
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
@@ -541,7 +731,7 @@ func TestUpdate(t *testing.T) {
|
||||
{
|
||||
name: "Cant change Id",
|
||||
req: &pbs.UpdateRoleRequest{
|
||||
Id: u.GetPublicId(),
|
||||
Id: or.GetPublicId(),
|
||||
UpdateMask: &field_mask.FieldMask{
|
||||
Paths: []string{"id"},
|
||||
},
|
||||
@@ -582,7 +772,7 @@ func TestUpdate(t *testing.T) {
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
defer resetRole()
|
||||
defer resetRoles()
|
||||
assert := assert.New(t)
|
||||
req := proto.Clone(toMerge).(*pbs.UpdateRoleRequest)
|
||||
proto.Merge(req, tc.req)
|
||||
@@ -37,7 +37,7 @@ type Service struct {
|
||||
// NewService returns a user service which handles user related requests to watchtower.
|
||||
func NewService(repo func() (*iam.Repository, error)) (Service, error) {
|
||||
if repo == nil {
|
||||
return Service{}, fmt.Errorf("nil iam repostiroy provided")
|
||||
return Service{}, fmt.Errorf("nil iam repository provided")
|
||||
}
|
||||
return Service{repoFn: repo}, nil
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user