Pathing updated to support new and old styles (#323)

This commit is contained in:
Todd Knight authored and GitHub committed 2020-09-02 19:43:41 -07:00
1 parent 2a98432143
commit 37e56ab46b
85 files changed
+11689 -4462

No files matched your search

+12 -5
View File
@@ -206,15 +206,22 @@ func TestHandler_CORS(t *testing.T) {
scopeId := "global"
if c.provideScopeId {
scopeId = org.GetPublicId()
if c.method == http.MethodPost {
body = map[string]interface{}{}
}
if c.method == http.MethodPost {
body = map[string]interface{}{
"scope_id": scopeId,
}
}
req, err = client.NewRequest(tc.Context(), c.method, "scopes", body)
require.NoError(t, err)
q := url.Values{}
q.Add("scope_id", scopeId)
req.URL.RawQuery = q.Encode()
if c.method == http.MethodGet {
q := url.Values{}
q.Add("scope_id", scopeId)
req.URL.RawQuery = q.Encode()
}
// Append headers
if c.origin != "" {
+56 -11
View File
@@ -96,10 +96,26 @@ func TestHandleImplementedPaths(t *testing.T) {
for verb, paths := range map[string][]string{
"GET": {
// new paths
"v1/targets",
"v1/targets/someid",
"v1/scopes",
"v1/scopes/someid",
"v1/auth-tokens",
"v1/auth-tokens/someid",
"v1/auth-methods",
"v1/auth-methods/someid",
"v1/accounts",
"v1/accounts/someid",
"v1/groups",
"v1/groups/someid",
"v1/host-catalogs",
"v1/host-catalogs/someid",
"v1/host-sets",
"v1/host-sets/someid",
"v1/hosts",
"v1/hosts/someid",
"v1/roles",
"v1/roles/someid",
"v1/users",
"v1/users/someid",
// old paths
"v1/scopes",
@@ -126,14 +142,29 @@ func TestHandleImplementedPaths(t *testing.T) {
"POST": {
// Creation end points
// new paths
"v1/targets",
"v1/targets/someid:add-host-sets",
"v1/targets/someid:set-host-sets",
"v1/targets/someid:remove-host-sets",
"v1/scopes",
"v1/groups",
"v1/roles",
"v1/users",
"v1/auth-methods",
"v1/accounts",
"v1/host-catalogs",
"v1/host-sets",
"v1/host-sets/someid:add-hosts",
"v1/host-sets/someid:set-hosts",
"v1/host-sets/someid:remove-hosts",
"v1/hosts",
// custom methods
"v1/auth-methods/someid:authenticate",
"v1/accounts/someid:set-password",
"v1/accounts/someid:change-password",
"v1/roles/someid:add-principals",
"v1/roles/someid:set-principals",
"v1/roles/someid:remove-principals",
"v1/roles/someid:add-grants",
"v1/roles/someid:set-grants",
"v1/roles/someid:remove-grants",
"v1/groups/someid:add-members",
"v1/groups/someid:set-members",
"v1/groups/someid:remove-members",
// old paths
"v1/scopes",
@@ -162,8 +193,16 @@ func TestHandleImplementedPaths(t *testing.T) {
},
"DELETE": {
// new paths
"v1/targets/someid",
"v1/scopes/someid",
"v1/users/someid",
"v1/roles/someid",
"v1/groups/someid",
"v1/auth-tokens/someid",
"v1/auth-methods/someid",
"v1/accounts/someid",
"v1/host-catalogs/someid",
"v1/host-sets/someid",
"v1/hosts/someid",
// old paths
"v1/scopes/someid",
@@ -179,8 +218,14 @@ func TestHandleImplementedPaths(t *testing.T) {
},
"PATCH": {
// new paths
"v1/targets/someid",
"v1/scopes/someid",
"v1/users/someid",
"v1/roles/someid",
"v1/groups/someid",
"v1/auth-methods/someid",
"v1/host-catalogs/someid",
"v1/host-sets/someid",
"v1/hosts/someid",
// old paths
"v1/scopes/someid",
@@ -90,12 +90,12 @@ func (s Service) CreateAccount(ctx context.Context, req *pbs.CreateAccountReques
if err := validateCreateRequest(req); err != nil {
return nil, err
}
u, err := s.createInRepo(ctx, req.GetAuthMethodId(), authResults.Scope.GetId(), req.GetItem())
u, err := s.createInRepo(ctx, req.GetItem().GetAuthMethodId(), authResults.Scope.GetId(), req.GetItem())
if err != nil {
return nil, err
}
u.Scope = authResults.Scope
return &pbs.CreateAccountResponse{Item: u, Uri: fmt.Sprintf("scopes/%s/auth-methods/%s/accounts/%s", authResults.Scope.GetId(), u.GetAuthMethodId(), u.GetId())}, nil
return &pbs.CreateAccountResponse{Item: u, Uri: fmt.Sprintf("accounts/%s", u.GetId())}, nil
}
// UpdateAccount implements the interface pbs.AccountServiceServer.
@@ -107,7 +107,7 @@ func (s Service) UpdateAccount(ctx context.Context, req *pbs.UpdateAccountReques
if err := validateUpdateRequest(req); err != nil {
return nil, err
}
u, err := s.updateInRepo(ctx, req.GetAuthMethodId(), authResults.Scope.GetId(), req.GetId(), req.GetUpdateMask().GetPaths(), req.GetItem())
u, err := s.updateInRepo(ctx, authResults.Scope.GetId(), req.GetId(), req.GetUpdateMask().GetPaths(), req.GetItem())
if err != nil {
return nil, err
}
@@ -218,7 +218,7 @@ func (s Service) createInRepo(ctx context.Context, authMethodId, scopeId string,
return toProto(out)
}
func (s Service) updateInRepo(ctx context.Context, authMethodId, scopeId, id string, mask []string, item *pb.Account) (*pb.Account, error) {
func (s Service) updateInRepo(ctx context.Context, scopeId, id string, mask []string, item *pb.Account) (*pb.Account, error) {
var opts []password.Option
if desc := item.GetDescription(); desc != nil {
opts = append(opts, password.WithDescription(desc.GetValue()))
@@ -226,7 +226,7 @@ func (s Service) updateInRepo(ctx context.Context, authMethodId, scopeId, id str
if name := item.GetName(); name != nil {
opts = append(opts, password.WithName(name.GetValue()))
}
u, err := password.NewAccount(authMethodId, opts...)
u, err := password.NewAccount("ignored", opts...)
if err != nil {
return nil, status.Errorf(codes.Internal, "Unable to build auth method for update: %v.", err)
}
@@ -350,22 +350,16 @@ func toProto(in *password.Account) (*pb.Account, error) {
// * All required parameters are set
// * There are no conflicting parameters provided
func validateGetRequest(req *pbs.GetAccountRequest) error {
return handlers.ValidateGetRequest(password.AccountPrefix, req, func() map[string]string {
badFields := map[string]string{}
if !handlers.ValidId(password.AuthMethodPrefix, req.GetAuthMethodId()) {
badFields["auth_method_id"] = "Invalid formatted identifier."
}
return badFields
})
return handlers.ValidateGetRequest(password.AccountPrefix, req, handlers.NoopValidatorFn)
}
func validateCreateRequest(req *pbs.CreateAccountRequest) error {
return handlers.ValidateCreateRequest(req.GetItem(), func() map[string]string {
badFields := map[string]string{}
if req.GetItem().GetAuthMethodId() != "" {
badFields["auth_method_id"] = "This is a read only field."
if req.GetItem().GetAuthMethodId() == "" {
badFields["auth_method_id"] = "This field is required."
}
switch auth.SubtypeFromId(req.GetAuthMethodId()) {
switch auth.SubtypeFromId(req.GetItem().GetAuthMethodId()) {
case auth.PasswordSubtype:
if req.GetItem().GetType() != "" && req.GetItem().GetType() != auth.PasswordSubtype.String() {
badFields["type"] = "Doesn't match the parent resource's type."
@@ -383,20 +377,11 @@ func validateCreateRequest(req *pbs.CreateAccountRequest) error {
}
func validateUpdateRequest(req *pbs.UpdateAccountRequest) error {
return handlers.ValidateUpdateRequest(password.AccountPrefix, req, req.GetItem(), func() map[string]string {
badFields := map[string]string{}
return badFields
})
return handlers.ValidateUpdateRequest(password.AccountPrefix, req, req.GetItem(), handlers.NoopValidatorFn)
}
func validateDeleteRequest(req *pbs.DeleteAccountRequest) error {
return handlers.ValidateDeleteRequest(password.AccountPrefix, req, func() map[string]string {
badFields := map[string]string{}
if !handlers.ValidId(password.AuthMethodPrefix, req.GetAuthMethodId()) {
badFields["auth_method_id"] = "Invalid formatted identifier."
}
return badFields
})
return handlers.ValidateDeleteRequest(password.AccountPrefix, req, handlers.NoopValidatorFn)
}
func validateListRequest(req *pbs.ListAccountsRequest) error {
@@ -415,9 +400,6 @@ func validateChangePasswordRequest(req *pbs.ChangePasswordRequest) error {
if !handlers.ValidId(password.AccountPrefix, req.GetId()) {
badFields["id"] = "Improperly formatted identifier."
}
if !handlers.ValidId(password.AuthMethodPrefix, req.GetAuthMethodId()) {
badFields["auth_method_id"] = "Invalid formatted identifier."
}
if req.GetVersion() == 0 {
badFields["version"] = "Existing resource version is required for an update."
}
@@ -438,9 +420,6 @@ func validateSetPasswordRequest(req *pbs.SetPasswordRequest) error {
if !handlers.ValidId(password.AccountPrefix, req.GetId()) {
badFields["id"] = "Improperly formatted identifier."
}
if !handlers.ValidId(password.AuthMethodPrefix, req.GetAuthMethodId()) {
badFields["auth_method_id"] = "Invalid formatted identifier."
}
if req.GetVersion() == 0 {
badFields["version"] = "Existing resource version is required for an update."
}
@@ -64,25 +64,25 @@ func TestGet(t *testing.T) {
}{
{
name: "Get an existing account",
req: &pbs.GetAccountRequest{AuthMethodId: wireAccount.GetAuthMethodId(), Id: wireAccount.GetId()},
req: &pbs.GetAccountRequest{Id: wireAccount.GetId()},
res: &pbs.GetAccountResponse{Item: &wireAccount},
errCode: codes.OK,
},
{
name: "Get a non existing account",
req: &pbs.GetAccountRequest{AuthMethodId: wireAccount.GetAuthMethodId(), Id: password.AccountPrefix + "_DoesntExis"},
req: &pbs.GetAccountRequest{Id: password.AccountPrefix + "_DoesntExis"},
res: nil,
errCode: codes.NotFound,
},
{
name: "Wrong id prefix",
req: &pbs.GetAccountRequest{AuthMethodId: wireAccount.GetAuthMethodId(), Id: "j_1234567890"},
req: &pbs.GetAccountRequest{Id: "j_1234567890"},
res: nil,
errCode: codes.InvalidArgument,
},
{
name: "space in id",
req: &pbs.GetAccountRequest{AuthMethodId: wireAccount.GetAuthMethodId(), Id: authtoken.AuthTokenPrefix + "_1 23456789"},
req: &pbs.GetAccountRequest{Id: authtoken.AuthTokenPrefix + "_1 23456789"},
res: nil,
errCode: codes.InvalidArgument,
},
@@ -192,11 +192,8 @@ func TestDelete(t *testing.T) {
}
o, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrap))
ams := password.TestAuthMethods(t, conn, o.GetPublicId(), 2)
am1, wrongAm := ams[0], ams[1]
am1 := password.TestAuthMethods(t, conn, o.GetPublicId(), 1)[0]
ac := password.TestAccounts(t, conn, am1.GetPublicId(), 1)[0]
wrongAc := password.TestAccounts(t, conn, wrongAm.GetPublicId(), 1)[0]
s, err := accounts.NewService(repoFn)
require.NoError(t, err, "Error when getting new user service.")
@@ -211,32 +208,17 @@ func TestDelete(t *testing.T) {
{
name: "Delete an existing token",
req: &pbs.DeleteAccountRequest{
AuthMethodId: am1.GetPublicId(),
Id: ac.GetPublicId(),
Id: ac.GetPublicId(),
},
res: &pbs.DeleteAccountResponse{
Existed: true,
},
errCode: codes.OK,
},
{
name: "Delete account from wrong auth method",
req: &pbs.DeleteAccountRequest{
AuthMethodId: am1.GetPublicId(),
Id: wrongAc.GetPublicId(),
},
// TODO(toddknight): This should return Existed:false. Figure out if this test is testing something valid
// and if so make it pass.
res: &pbs.DeleteAccountResponse{
Existed: true,
},
errCode: codes.OK,
},
{
name: "Delete bad account id",
req: &pbs.DeleteAccountRequest{
AuthMethodId: am1.GetPublicId(),
Id: password.AccountPrefix + "_doesntexis",
Id: password.AccountPrefix + "_doesntexis",
},
res: &pbs.DeleteAccountResponse{
Existed: false,
@@ -246,8 +228,7 @@ func TestDelete(t *testing.T) {
{
name: "Bad account id formatting",
req: &pbs.DeleteAccountRequest{
AuthMethodId: am1.GetPublicId(),
Id: "bad_format",
Id: "bad_format",
},
res: nil,
errCode: codes.InvalidArgument,
@@ -280,8 +261,7 @@ func TestDelete_twice(t *testing.T) {
s, err := accounts.NewService(repoFn)
require.NoError(t, err, "Error when getting new user service")
req := &pbs.DeleteAccountRequest{
AuthMethodId: am.GetPublicId(),
Id: ac.GetPublicId(),
Id: ac.GetPublicId(),
}
got, gErr := s.DeleteAccount(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), req)
assert.NoError(gErr, "First attempt")
@@ -328,16 +308,16 @@ func TestCreate(t *testing.T) {
{
name: "Create a valid Account",
req: &pbs.CreateAccountRequest{
AuthMethodId: defaultAccount.GetAuthMethodId(),
Item: &pb.Account{
Name: &wrapperspb.StringValue{Value: "name"},
Description: &wrapperspb.StringValue{Value: "desc"},
Type: "password",
Attributes: createAttr("validaccount", ""),
AuthMethodId: defaultAccount.GetAuthMethodId(),
Name: &wrapperspb.StringValue{Value: "name"},
Description: &wrapperspb.StringValue{Value: "desc"},
Type: "password",
Attributes: createAttr("validaccount", ""),
},
},
res: &pbs.CreateAccountResponse{
Uri: fmt.Sprintf("scopes/%s/auth-methods/%s/accounts/%s_", o.GetPublicId(), defaultAccount.GetAuthMethodId(), password.AccountPrefix),
Uri: fmt.Sprintf("accounts/%s_", password.AccountPrefix),
Item: &pb.Account{
AuthMethodId: defaultAccount.GetAuthMethodId(),
Name: &wrapperspb.StringValue{Value: "name"},
@@ -353,13 +333,13 @@ func TestCreate(t *testing.T) {
{
name: "Create a valid Account without type defined",
req: &pbs.CreateAccountRequest{
AuthMethodId: defaultAccount.GetAuthMethodId(),
Item: &pb.Account{
Attributes: createAttr("notypedefined", ""),
AuthMethodId: defaultAccount.GetAuthMethodId(),
Attributes: createAttr("notypedefined", ""),
},
},
res: &pbs.CreateAccountResponse{
Uri: fmt.Sprintf("scopes/%s/auth-methods/%s/accounts/%s_", o.GetPublicId(), defaultAccount.GetAuthMethodId(), password.AccountPrefix),
Uri: fmt.Sprintf("accounts/%s_", password.AccountPrefix),
Item: &pb.Account{
AuthMethodId: defaultAccount.GetAuthMethodId(),
Scope: &scopepb.ScopeInfo{Id: o.GetPublicId(), Type: scope.Org.String()},
@@ -373,15 +353,15 @@ func TestCreate(t *testing.T) {
{
name: "Create a valid Account with password defined",
req: &pbs.CreateAccountRequest{
AuthMethodId: defaultAccount.GetAuthMethodId(),
Item: &pb.Account{
Name: &wrapperspb.StringValue{Value: "name_with_password"},
Description: &wrapperspb.StringValue{Value: "desc"},
Attributes: createAttr("haspassword", "somepassword"),
AuthMethodId: defaultAccount.GetAuthMethodId(),
Name: &wrapperspb.StringValue{Value: "name_with_password"},
Description: &wrapperspb.StringValue{Value: "desc"},
Attributes: createAttr("haspassword", "somepassword"),
},
},
res: &pbs.CreateAccountResponse{
Uri: fmt.Sprintf("scopes/%s/auth-methods/%s/accounts/%s_", o.GetPublicId(), defaultAccount.GetAuthMethodId(), password.AccountPrefix),
Uri: fmt.Sprintf("accounts/%s_", password.AccountPrefix),
Item: &pb.Account{
AuthMethodId: defaultAccount.GetAuthMethodId(),
Name: &wrapperspb.StringValue{Value: "name_with_password"},
@@ -397,10 +377,10 @@ func TestCreate(t *testing.T) {
{
name: "Cant specify mismatching type",
req: &pbs.CreateAccountRequest{
AuthMethodId: defaultAccount.GetAuthMethodId(),
Item: &pb.Account{
Type: "wrong",
Attributes: createAttr("nopwprovided", ""),
AuthMethodId: defaultAccount.GetAuthMethodId(),
Type: "wrong",
Attributes: createAttr("nopwprovided", ""),
},
},
res: nil,
@@ -409,24 +389,11 @@ func TestCreate(t *testing.T) {
{
name: "Can't specify Id",
req: &pbs.CreateAccountRequest{
AuthMethodId: defaultAccount.GetAuthMethodId(),
Item: &pb.Account{
Id: password.AccountPrefix + "_notallowed",
Type: "password",
Attributes: createAttr("cantprovideid", ""),
},
},
res: nil,
errCode: codes.InvalidArgument,
},
{
name: "Can't specify AuthMethodId",
req: &pbs.CreateAccountRequest{
AuthMethodId: defaultAccount.GetAuthMethodId(),
Item: &pb.Account{
AuthMethodId: defaultAccount.GetAuthMethodId(),
Id: password.AccountPrefix + "_notallowed",
Type: "password",
Attributes: createAttr("noauthmethod", ""),
Attributes: createAttr("cantprovideid", ""),
},
},
res: nil,
@@ -435,11 +402,11 @@ func TestCreate(t *testing.T) {
{
name: "Can't specify Created Time",
req: &pbs.CreateAccountRequest{
AuthMethodId: defaultAccount.GetAuthMethodId(),
Item: &pb.Account{
CreatedTime: ptypes.TimestampNow(),
Type: "password",
Attributes: createAttr("nocreatedtime", ""),
AuthMethodId: defaultAccount.GetAuthMethodId(),
CreatedTime: ptypes.TimestampNow(),
Type: "password",
Attributes: createAttr("nocreatedtime", ""),
},
},
res: nil,
@@ -448,11 +415,11 @@ func TestCreate(t *testing.T) {
{
name: "Can't specify Update Time",
req: &pbs.CreateAccountRequest{
AuthMethodId: defaultAccount.GetAuthMethodId(),
Item: &pb.Account{
UpdatedTime: ptypes.TimestampNow(),
Type: "password",
Attributes: createAttr("noupdatetime", ""),
AuthMethodId: defaultAccount.GetAuthMethodId(),
UpdatedTime: ptypes.TimestampNow(),
Type: "password",
Attributes: createAttr("noupdatetime", ""),
},
},
res: nil,
@@ -461,9 +428,9 @@ func TestCreate(t *testing.T) {
{
name: "Must specify login name for password type",
req: &pbs.CreateAccountRequest{
AuthMethodId: defaultAccount.GetAuthMethodId(),
Item: &pb.Account{
Type: "password",
AuthMethodId: defaultAccount.GetAuthMethodId(),
Type: "password",
},
},
res: nil,
@@ -522,19 +489,19 @@ func TestUpdate(t *testing.T) {
t.Helper()
acc, err := tested.CreateAccount(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())),
&pbs.CreateAccountRequest{
AuthMethodId: am.GetPublicId(),
Item: &pb.Account{
Name: wrapperspb.String("default"),
Description: wrapperspb.String("default"),
Type: "password",
Attributes: defaultAttributes,
AuthMethodId: am.GetPublicId(),
Name: wrapperspb.String("default"),
Description: wrapperspb.String("default"),
Type: "password",
Attributes: defaultAttributes,
}},
)
require.NoError(t, err)
clean := func() {
_, err := tested.DeleteAccount(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())),
&pbs.DeleteAccountRequest{AuthMethodId: am.GetPublicId(), Id: acc.GetItem().GetId()})
&pbs.DeleteAccountRequest{Id: acc.GetItem().GetId()})
require.NoError(t, err)
}
@@ -550,7 +517,6 @@ func TestUpdate(t *testing.T) {
{
name: "Update an Existing AuthMethod",
req: &pbs.UpdateAccountRequest{
AuthMethodId: am.GetPublicId(),
UpdateMask: &field_mask.FieldMask{
Paths: []string{"name", "description"},
},
@@ -574,7 +540,6 @@ func TestUpdate(t *testing.T) {
{
name: "Multiple Paths in single string",
req: &pbs.UpdateAccountRequest{
AuthMethodId: am.GetPublicId(),
UpdateMask: &field_mask.FieldMask{
Paths: []string{"name,description"},
},
@@ -598,7 +563,6 @@ func TestUpdate(t *testing.T) {
{
name: "No Update Mask",
req: &pbs.UpdateAccountRequest{
AuthMethodId: am.GetPublicId(),
Item: &pb.Account{
Name: &wrapperspb.StringValue{Value: "updated name"},
Description: &wrapperspb.StringValue{Value: "updated desc"},
@@ -610,8 +574,7 @@ func TestUpdate(t *testing.T) {
{
name: "No Paths in Mask",
req: &pbs.UpdateAccountRequest{
AuthMethodId: am.GetPublicId(),
UpdateMask: &field_mask.FieldMask{Paths: []string{}},
UpdateMask: &field_mask.FieldMask{Paths: []string{}},
Item: &pb.Account{
Name: &wrapperspb.StringValue{Value: "updated name"},
Description: &wrapperspb.StringValue{Value: "updated desc"},
@@ -623,8 +586,7 @@ func TestUpdate(t *testing.T) {
{
name: "Only non-existant paths in Mask",
req: &pbs.UpdateAccountRequest{
AuthMethodId: am.GetPublicId(),
UpdateMask: &field_mask.FieldMask{Paths: []string{"nonexistant_field"}},
UpdateMask: &field_mask.FieldMask{Paths: []string{"nonexistant_field"}},
Item: &pb.Account{
Name: &wrapperspb.StringValue{Value: "updated name"},
Description: &wrapperspb.StringValue{Value: "updated desc"},
@@ -636,7 +598,6 @@ func TestUpdate(t *testing.T) {
{
name: "Unset Name",
req: &pbs.UpdateAccountRequest{
AuthMethodId: am.GetPublicId(),
UpdateMask: &field_mask.FieldMask{
Paths: []string{"name"},
},
@@ -659,7 +620,6 @@ func TestUpdate(t *testing.T) {
{
name: "Update Only Name",
req: &pbs.UpdateAccountRequest{
AuthMethodId: am.GetPublicId(),
UpdateMask: &field_mask.FieldMask{
Paths: []string{"name"},
},
@@ -684,7 +644,6 @@ func TestUpdate(t *testing.T) {
{
name: "Update Only Description",
req: &pbs.UpdateAccountRequest{
AuthMethodId: am.GetPublicId(),
UpdateMask: &field_mask.FieldMask{
Paths: []string{"description"},
},
@@ -709,7 +668,6 @@ func TestUpdate(t *testing.T) {
{
name: "Update Only LoginName",
req: &pbs.UpdateAccountRequest{
AuthMethodId: am.GetPublicId(),
UpdateMask: &field_mask.FieldMask{
Paths: []string{"attributes.login_name"},
},
@@ -736,8 +694,7 @@ func TestUpdate(t *testing.T) {
{
name: "Update a Non Existing Account",
req: &pbs.UpdateAccountRequest{
AuthMethodId: am.GetPublicId(),
Id: password.AccountPrefix + "_DoesntExis",
Id: password.AccountPrefix + "_DoesntExis",
UpdateMask: &field_mask.FieldMask{
Paths: []string{"description"},
},
@@ -751,7 +708,6 @@ func TestUpdate(t *testing.T) {
{
name: "Cant change Id",
req: &pbs.UpdateAccountRequest{
AuthMethodId: am.GetPublicId(),
UpdateMask: &field_mask.FieldMask{
Paths: []string{"id"},
},
@@ -766,7 +722,6 @@ func TestUpdate(t *testing.T) {
{
name: "Cant specify Created Time",
req: &pbs.UpdateAccountRequest{
AuthMethodId: am.GetPublicId(),
UpdateMask: &field_mask.FieldMask{
Paths: []string{"created_time"},
},
@@ -780,7 +735,6 @@ func TestUpdate(t *testing.T) {
{
name: "Cant specify Updated Time",
req: &pbs.UpdateAccountRequest{
AuthMethodId: am.GetPublicId(),
UpdateMask: &field_mask.FieldMask{
Paths: []string{"updated_time"},
},
@@ -794,7 +748,6 @@ func TestUpdate(t *testing.T) {
{
name: "Cant specify Type",
req: &pbs.UpdateAccountRequest{
AuthMethodId: am.GetPublicId(),
UpdateMask: &field_mask.FieldMask{
Paths: []string{"type"},
},
@@ -875,10 +828,10 @@ func TestSetPassword(t *testing.T) {
}
attrs, err := handlers.ProtoToStruct(pwAttrs)
createResp, err := tested.CreateAccount(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), &pbs.CreateAccountRequest{
AuthMethodId: am.GetPublicId(),
Item: &pb.Account{
Type: "password",
Attributes: attrs,
AuthMethodId: am.GetPublicId(),
Type: "password",
Attributes: attrs,
},
})
require.NoError(t, err)
@@ -915,10 +868,9 @@ func TestSetPassword(t *testing.T) {
acct := createAccount(t, tt.oldPw)
setResp, err := tested.SetPassword(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), &pbs.SetPasswordRequest{
AuthMethodId: acct.GetAuthMethodId(),
Id: acct.GetId(),
Version: acct.GetVersion(),
Password: tt.newPw,
Id: acct.GetId(),
Version: acct.GetVersion(),
Password: tt.newPw,
})
require.NoError(err)
assert.Equal(acct.GetVersion()+1, setResp.GetItem().GetVersion())
@@ -932,32 +884,22 @@ func TestSetPassword(t *testing.T) {
defaultAcct := createAccount(t, "")
badRequestCases := []struct {
name string
authMethodId string
accountId string
version uint32
password string
name string
accountId string
version uint32
password string
}{
{
name: "empty auth method",
authMethodId: "",
accountId: defaultAcct.GetId(),
version: defaultAcct.GetVersion(),
password: "somepassword",
name: "empty account id",
accountId: "",
version: defaultAcct.GetVersion(),
password: "somepassword",
},
{
name: "empty account id",
authMethodId: defaultAcct.GetAuthMethodId(),
accountId: "",
version: defaultAcct.GetVersion(),
password: "somepassword",
},
{
name: "unset version",
authMethodId: defaultAcct.GetAuthMethodId(),
accountId: defaultAcct.GetId(),
version: 0,
password: "somepassword",
name: "unset version",
accountId: defaultAcct.GetId(),
version: 0,
password: "somepassword",
},
}
@@ -966,10 +908,9 @@ func TestSetPassword(t *testing.T) {
assert := assert.New(t)
setResp, err := tested.SetPassword(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), &pbs.SetPasswordRequest{
AuthMethodId: tt.authMethodId,
Id: tt.accountId,
Version: tt.version,
Password: tt.password,
Id: tt.accountId,
Version: tt.version,
Password: tt.password,
})
assert.Error(err)
assert.Nil(setResp)
@@ -1000,10 +941,10 @@ func TestChangePassword(t *testing.T) {
}
attrs, err := handlers.ProtoToStruct(pwAttrs)
createResp, err := tested.CreateAccount(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), &pbs.CreateAccountRequest{
AuthMethodId: am.GetPublicId(),
Item: &pb.Account{
Type: "password",
Attributes: attrs,
AuthMethodId: am.GetPublicId(),
Type: "password",
Attributes: attrs,
},
})
require.NoError(t, err)
@@ -1017,11 +958,10 @@ func TestChangePassword(t *testing.T) {
acct := createAccount(t, "originalpassword")
changeResp, err := tested.ChangePassword(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), &pbs.ChangePasswordRequest{
AuthMethodId: acct.GetAuthMethodId(),
Id: acct.GetId(),
Version: acct.GetVersion(),
OldPassword: "originalpassword",
NewPassword: "a different password",
Id: acct.GetId(),
Version: acct.GetVersion(),
OldPassword: "originalpassword",
NewPassword: "a different password",
})
require.NoError(err)
assert.Equal(acct.GetVersion()+1, changeResp.GetItem().GetVersion())
@@ -1037,11 +977,10 @@ func TestChangePassword(t *testing.T) {
acct := createAccount(t, "originalpassword")
changeResp, err := tested.ChangePassword(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), &pbs.ChangePasswordRequest{
AuthMethodId: acct.GetAuthMethodId(),
Id: acct.GetId(),
Version: acct.GetVersion(),
OldPassword: "thewrongpassword",
NewPassword: "a different password",
Id: acct.GetId(),
Version: acct.GetVersion(),
OldPassword: "thewrongpassword",
NewPassword: "a different password",
})
assert.Error(err)
assert.Nil(changeResp)
@@ -1111,11 +1050,10 @@ func TestChangePassword(t *testing.T) {
assert := assert.New(t)
changeResp, err := tested.ChangePassword(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), &pbs.ChangePasswordRequest{
AuthMethodId: tt.authMethodId,
Id: tt.accountId,
Version: tt.version,
OldPassword: tt.oldPW,
NewPassword: tt.newPW,
Id: tt.accountId,
Version: tt.version,
OldPassword: tt.oldPW,
NewPassword: tt.newPW,
})
assert.Error(err)
assert.Nil(changeResp)
@@ -95,7 +95,7 @@ func (s Service) CreateAuthMethod(ctx context.Context, req *pbs.CreateAuthMethod
return nil, err
}
u.Scope = authResults.Scope
return &pbs.CreateAuthMethodResponse{Item: u, Uri: fmt.Sprintf("scopes/%s/auth-methods/%s", authResults.Scope.GetId(), u.GetId())}, nil
return &pbs.CreateAuthMethodResponse{Item: u, Uri: fmt.Sprintf("auth-methods/%s", u.GetId())}, nil
}
// UpdateAuthMethod implements the interface pbs.AuthMethodServiceServer.
@@ -257,6 +257,7 @@ func (s Service) deleteFromRepo(ctx context.Context, scopeId, id string) (bool,
func toProto(in *password.AuthMethod) (*pb.AuthMethod, error) {
out := pb.AuthMethod{
Id: in.GetPublicId(),
ScopeId: in.GetScopeId(),
CreatedTime: in.GetCreateTime().GetTimestamp(),
UpdatedTime: in.GetUpdateTime().GetTimestamp(),
Version: in.GetVersion(),
@@ -43,6 +43,7 @@ func TestGet(t *testing.T) {
wantU := &pb.AuthMethod{
Id: am.GetPublicId(),
ScopeId: am.GetScopeId(),
CreatedTime: am.CreateTime.GetTimestamp(),
UpdatedTime: am.UpdateTime.GetTimestamp(),
Type: "password",
@@ -125,6 +126,7 @@ func TestList(t *testing.T) {
for _, am := range password.TestAuthMethods(t, conn, oWithAuthMethods.GetPublicId(), 3) {
wantSomeAuthMethods = append(wantSomeAuthMethods, &pb.AuthMethod{
Id: am.GetPublicId(),
ScopeId: oWithAuthMethods.GetPublicId(),
CreatedTime: am.GetCreateTime().GetTimestamp(),
UpdatedTime: am.GetUpdateTime().GetTimestamp(),
Scope: &scopepb.ScopeInfo{Id: oWithAuthMethods.GetPublicId(), Type: scope.Org.String()},
@@ -141,6 +143,7 @@ func TestList(t *testing.T) {
for _, aa := range password.TestAuthMethods(t, conn, oWithOtherAuthMethods.GetPublicId(), 3) {
wantOtherAuthMethods = append(wantOtherAuthMethods, &pb.AuthMethod{
Id: aa.GetPublicId(),
ScopeId: oWithOtherAuthMethods.GetPublicId(),
CreatedTime: aa.GetCreateTime().GetTimestamp(),
UpdatedTime: aa.GetUpdateTime().GetTimestamp(),
Scope: &scopepb.ScopeInfo{Id: oWithOtherAuthMethods.GetPublicId(), Type: scope.Org.String()},
@@ -311,14 +314,16 @@ func TestCreate(t *testing.T) {
{
name: "Create a valid AuthMethod",
req: &pbs.CreateAuthMethodRequest{Item: &pb.AuthMethod{
ScopeId: o.GetPublicId(),
Name: &wrapperspb.StringValue{Value: "name"},
Description: &wrapperspb.StringValue{Value: "desc"},
Type: "password",
}},
res: &pbs.CreateAuthMethodResponse{
Uri: fmt.Sprintf("scopes/%s/auth-methods/%s_", o.GetPublicId(), password.AuthMethodPrefix),
Uri: fmt.Sprintf("auth-methods/%s_", password.AuthMethodPrefix),
Item: &pb.AuthMethod{
Id: defaultAm.GetPublicId(),
ScopeId: o.GetPublicId(),
CreatedTime: defaultAm.GetCreateTime().GetTimestamp(),
UpdatedTime: defaultAm.GetUpdateTime().GetTimestamp(),
Name: &wrapperspb.StringValue{Value: "name"},
@@ -337,8 +342,9 @@ func TestCreate(t *testing.T) {
{
name: "Can't specify Id",
req: &pbs.CreateAuthMethodRequest{Item: &pb.AuthMethod{
Id: password.AuthMethodPrefix + "_notallowed",
Type: "password",
ScopeId: o.GetPublicId(),
Id: password.AuthMethodPrefix + "_notallowed",
Type: "password",
}},
res: nil,
errCode: codes.InvalidArgument,
@@ -346,6 +352,7 @@ func TestCreate(t *testing.T) {
{
name: "Can't specify Created Time",
req: &pbs.CreateAuthMethodRequest{Item: &pb.AuthMethod{
ScopeId: o.GetPublicId(),
CreatedTime: ptypes.TimestampNow(),
}},
res: nil,
@@ -354,6 +361,7 @@ func TestCreate(t *testing.T) {
{
name: "Can't specify Update Time",
req: &pbs.CreateAuthMethodRequest{Item: &pb.AuthMethod{
ScopeId: o.GetPublicId(),
UpdatedTime: ptypes.TimestampNow(),
Type: "password",
}},
@@ -363,6 +371,7 @@ func TestCreate(t *testing.T) {
{
name: "Can't specify Update Time",
req: &pbs.CreateAuthMethodRequest{Item: &pb.AuthMethod{
ScopeId: o.GetPublicId(),
UpdatedTime: ptypes.TimestampNow(),
Type: "password",
}},
@@ -372,6 +381,7 @@ func TestCreate(t *testing.T) {
{
name: "Must specify type",
req: &pbs.CreateAuthMethodRequest{Item: &pb.AuthMethod{
ScopeId: o.GetPublicId(),
Name: &wrapperspb.StringValue{Value: "must specify type"},
Description: &wrapperspb.StringValue{Value: "must specify type"},
}},
@@ -381,6 +391,7 @@ func TestCreate(t *testing.T) {
{
name: "Attributes must be valid for type",
req: &pbs.CreateAuthMethodRequest{Item: &pb.AuthMethod{
ScopeId: o.GetPublicId(),
Name: &wrapperspb.StringValue{Value: "Attributes must be valid for type"},
Description: &wrapperspb.StringValue{Value: "Attributes must be valid for type"},
Type: "password",
@@ -479,6 +490,7 @@ func TestUpdate(t *testing.T) {
},
res: &pbs.UpdateAuthMethodResponse{
Item: &pb.AuthMethod{
ScopeId: o.GetPublicId(),
Name: &wrapperspb.StringValue{Value: "new"},
Description: &wrapperspb.StringValue{Value: "desc"},
Type: "password",
@@ -504,6 +516,7 @@ func TestUpdate(t *testing.T) {
},
res: &pbs.UpdateAuthMethodResponse{
Item: &pb.AuthMethod{
ScopeId: o.GetPublicId(),
Name: &wrapperspb.StringValue{Value: "new"},
Description: &wrapperspb.StringValue{Value: "desc"},
Type: "password",
@@ -560,6 +573,7 @@ func TestUpdate(t *testing.T) {
},
res: &pbs.UpdateAuthMethodResponse{
Item: &pb.AuthMethod{
ScopeId: o.GetPublicId(),
Description: &wrapperspb.StringValue{Value: "default"},
Type: "password",
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{
@@ -584,6 +598,7 @@ func TestUpdate(t *testing.T) {
},
res: &pbs.UpdateAuthMethodResponse{
Item: &pb.AuthMethod{
ScopeId: o.GetPublicId(),
Name: &wrapperspb.StringValue{Value: "updated"},
Description: &wrapperspb.StringValue{Value: "default"},
Type: "password",
@@ -609,6 +624,7 @@ func TestUpdate(t *testing.T) {
},
res: &pbs.UpdateAuthMethodResponse{
Item: &pb.AuthMethod{
ScopeId: o.GetPublicId(),
Name: &wrapperspb.StringValue{Value: "default"},
Description: &wrapperspb.StringValue{Value: "notignored"},
Type: "password",
@@ -707,6 +723,7 @@ func TestUpdate(t *testing.T) {
},
res: &pbs.UpdateAuthMethodResponse{
Item: &pb.AuthMethod{
ScopeId: o.GetPublicId(),
Name: &wrapperspb.StringValue{Value: "default"},
Description: &wrapperspb.StringValue{Value: "default"},
Type: "password",
@@ -736,6 +753,7 @@ func TestUpdate(t *testing.T) {
},
res: &pbs.UpdateAuthMethodResponse{
Item: &pb.AuthMethod{
ScopeId: o.GetPublicId(),
Name: &wrapperspb.StringValue{Value: "default"},
Description: &wrapperspb.StringValue{Value: "default"},
Type: "password",
@@ -11,6 +11,7 @@ import (
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/authtokens"
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
"github.com/hashicorp/boundary/internal/servers/controller/handlers"
"github.com/hashicorp/boundary/internal/types/scope"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
@@ -39,7 +40,7 @@ func (s Service) ListAuthTokens(ctx context.Context, req *pbs.ListAuthTokensRequ
if err := validateListRequest(req); err != nil {
return nil, err
}
ul, err := s.listFromRepo(ctx, authResults.Scope.GetId())
ul, err := s.listFromRepo(ctx, req.GetScopeId())
if err != nil {
return nil, err
}
@@ -135,6 +136,7 @@ func (s Service) listFromRepo(ctx context.Context, orgId string) ([]*pb.AuthToke
func toProto(in *authtoken.AuthToken) *pb.AuthToken {
out := pb.AuthToken{
Id: in.GetPublicId(),
ScopeId: in.GetScopeId(),
CreatedTime: in.GetCreateTime().GetTimestamp(),
UpdatedTime: in.GetUpdateTime().GetTimestamp(),
ApproximateLastUsedTime: in.GetApproximateLastAccessTime().GetTimestamp(),
@@ -158,8 +160,11 @@ func validateDeleteRequest(req *pbs.DeleteAuthTokenRequest) error {
return handlers.ValidateDeleteRequest(authtoken.AuthTokenPrefix, req, handlers.NoopValidatorFn)
}
func validateListRequest(_ *pbs.ListAuthTokensRequest) error {
func validateListRequest(req *pbs.ListAuthTokensRequest) error {
badFields := map[string]string{}
if !handlers.ValidId(scope.Org.Prefix(), req.GetScopeId()) {
badFields["scope_id"] = "Incorrectly formatted identifier."
}
if len(badFields) > 0 {
return handlers.InvalidArgumentErrorf("Improperly formatted identifier.", badFields)
}
@@ -39,6 +39,7 @@ func TestGet(t *testing.T) {
wireAuthToken := pb.AuthToken{
Id: at.GetPublicId(),
ScopeId: at.GetScopeId(),
UserId: at.GetIamUserId(),
AuthMethodId: at.GetAuthMethodId(),
CreatedTime: at.GetCreateTime().GetTimestamp(),
@@ -107,6 +108,7 @@ func TestList(t *testing.T) {
at := authtoken.TestAuthToken(t, conn, kms, orgWithSomeTokens.GetPublicId())
wantSomeTokens = append(wantSomeTokens, &pb.AuthToken{
Id: at.GetPublicId(),
ScopeId: at.GetScopeId(),
UserId: at.GetIamUserId(),
AuthMethodId: at.GetAuthMethodId(),
CreatedTime: at.GetCreateTime().GetTimestamp(),
@@ -123,6 +125,7 @@ func TestList(t *testing.T) {
at := authtoken.TestAuthToken(t, conn, kms, orgWithOtherTokens.GetPublicId())
wantOtherTokens = append(wantOtherTokens, &pb.AuthToken{
Id: at.GetPublicId(),
ScopeId: at.GetScopeId(),
UserId: at.GetIamUserId(),
AuthMethodId: at.GetAuthMethodId(),
CreatedTime: at.GetCreateTime().GetTimestamp(),
@@ -170,7 +173,7 @@ func TestList(t *testing.T) {
s, err := authtokens.NewService(repoFn)
require.NoError(t, err, "Couldn't create new user service.")
got, gErr := s.ListAuthTokens(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scope)), &pbs.ListAuthTokensRequest{})
got, gErr := s.ListAuthTokens(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scope)), &pbs.ListAuthTokensRequest{ScopeId: tc.scope})
assert.Equal(t, tc.errCode, status.Code(gErr), "ListAuthTokens() with scope %q got error %v, wanted %v", tc.scope, gErr, tc.errCode)
assert.Empty(t, cmp.Diff(got, tc.res, protocmp.Transform(), protocmp.SortRepeatedFields(got)), "ListAuthTokens() with scope %q got response %q, wanted %q", tc.scope, got, tc.res)
})
@@ -12,6 +12,7 @@ import (
"github.com/hashicorp/boundary/internal/iam"
"github.com/hashicorp/boundary/internal/iam/store"
"github.com/hashicorp/boundary/internal/servers/controller/handlers"
"github.com/hashicorp/boundary/internal/types/scope"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/wrapperspb"
@@ -52,7 +53,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, authResults.Scope.GetId())
gl, err := s.listFromRepo(ctx, req.GetScopeId())
if err != nil {
return nil, err
}
@@ -88,12 +89,12 @@ func (s Service) CreateGroup(ctx context.Context, req *pbs.CreateGroupRequest) (
if err := validateCreateRequest(req); err != nil {
return nil, err
}
u, err := s.createInRepo(ctx, authResults.Scope.GetId(), req.GetItem())
u, err := s.createInRepo(ctx, req.GetItem().GetScopeId(), req.GetItem())
if err != nil {
return nil, err
}
u.Scope = authResults.Scope
return &pbs.CreateGroupResponse{Item: u, Uri: fmt.Sprintf("scopes/%s/groups/%s", authResults.Scope.GetId(), u.GetId())}, nil
return &pbs.CreateGroupResponse{Item: u, Uri: fmt.Sprintf("groups/%s", u.GetId())}, nil
}
// UpdateGroup implements the interface pbs.GroupServiceServer.
@@ -344,6 +345,7 @@ func (s Service) removeMembersInRepo(ctx context.Context, groupId string, userId
func toProto(in *iam.Group, members []*iam.GroupMember) *pb.Group {
out := pb.Group{
Id: in.GetPublicId(),
ScopeId: in.GetScopeId(),
CreatedTime: in.GetCreateTime().GetTimestamp(),
UpdatedTime: in.GetUpdateTime().GetTimestamp(),
Version: in.Version,
@@ -374,7 +376,13 @@ func validateGetRequest(req *pbs.GetGroupRequest) error {
}
func validateCreateRequest(req *pbs.CreateGroupRequest) error {
return handlers.ValidateCreateRequest(req.GetItem(), handlers.NoopValidatorFn)
return handlers.ValidateCreateRequest(req.GetItem(), func() map[string]string {
badFields := map[string]string{}
if !handlers.ValidId(scope.Org.Prefix(), req.GetItem().GetScopeId()) && !handlers.ValidId(scope.Project.Prefix(), req.GetItem().GetScopeId()) {
badFields["scope_id"] = "Incorrectly formatted identifier."
}
return badFields
})
}
func validateUpdateRequest(req *pbs.UpdateGroupRequest) error {
@@ -387,6 +395,9 @@ func validateDeleteRequest(req *pbs.DeleteGroupRequest) error {
func validateListRequest(req *pbs.ListGroupsRequest) error {
badFields := map[string]string{}
if !handlers.ValidId(scope.Org.Prefix(), req.GetScopeId()) && !handlers.ValidId(scope.Project.Prefix(), req.GetScopeId()) {
badFields["scope_id"] = "Incorrectly formatted identifier."
}
if len(badFields) > 0 {
return handlers.InvalidArgumentErrorf("Improperly formatted identifier.", badFields)
}
@@ -90,6 +90,7 @@ func TestGet(t *testing.T) {
wantOrgGroup := &pb.Group{
Id: og.GetPublicId(),
ScopeId: og.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: og.GetScopeId(), Type: scope.Org.String()},
Name: &wrapperspb.StringValue{Value: og.GetName()},
Description: &wrapperspb.StringValue{Value: og.GetDescription()},
@@ -107,6 +108,7 @@ func TestGet(t *testing.T) {
wantProjGroup := &pb.Group{
Id: pg.GetPublicId(),
ScopeId: pg.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: pg.GetScopeId(), Type: scope.Project.String()},
Name: &wrapperspb.StringValue{Value: pg.GetName()},
Description: &wrapperspb.StringValue{Value: pg.GetDescription()},
@@ -214,6 +216,7 @@ func TestList(t *testing.T) {
og := iam.TestGroup(t, conn, oWithGroups.GetPublicId())
wantOrgGroups = append(wantOrgGroups, &pb.Group{
Id: og.GetPublicId(),
ScopeId: og.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: oWithGroups.GetPublicId(), Type: scope.Org.String()},
CreatedTime: og.GetCreateTime().GetTimestamp(),
UpdatedTime: og.GetUpdateTime().GetTimestamp(),
@@ -222,6 +225,7 @@ func TestList(t *testing.T) {
pg := iam.TestGroup(t, conn, pWithGroups.GetPublicId())
wantProjGroups = append(wantProjGroups, &pb.Group{
Id: pg.GetPublicId(),
ScopeId: pg.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: pWithGroups.GetPublicId(), Type: scope.Project.String()},
CreatedTime: pg.GetCreateTime().GetTimestamp(),
UpdatedTime: pg.GetUpdateTime().GetTimestamp(),
@@ -266,7 +270,7 @@ func TestList(t *testing.T) {
s, err := groups.NewService(repoFn)
require.NoError(err, "Couldn't create new group service.")
got, gErr := s.ListGroups(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), &pbs.ListGroupsRequest{})
got, gErr := s.ListGroups(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), &pbs.ListGroupsRequest{ScopeId: tc.scopeId})
assert.Equal(tc.errCode, status.Code(gErr), "ListGroups(%q) got error %v, wanted %v", tc.scopeId, gErr, tc.errCode)
assert.Empty(cmp.Diff(got, tc.res, protocmp.Transform()), "ListGroups(%q) got response %q, wanted %q", tc.scopeId, got, tc.res)
})
@@ -414,21 +418,21 @@ func TestCreate(t *testing.T) {
cases := []struct {
name string
scopeId string
req *pbs.CreateGroupRequest
res *pbs.CreateGroupResponse
errCode codes.Code
}{
{
name: "Create a valid Group",
scopeId: defaultOGroup.GetScopeId(),
name: "Create a valid Group",
req: &pbs.CreateGroupRequest{Item: &pb.Group{
ScopeId: defaultOGroup.GetScopeId(),
Name: &wrapperspb.StringValue{Value: "name"},
Description: &wrapperspb.StringValue{Value: "desc"},
}},
res: &pbs.CreateGroupResponse{
Uri: fmt.Sprintf("scopes/%s/groups/%s_", defaultOGroup.GetScopeId(), iam.GroupPrefix),
Uri: fmt.Sprintf("groups/%s_", iam.GroupPrefix),
Item: &pb.Group{
ScopeId: defaultOGroup.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: defaultOGroup.GetScopeId(), Type: scope.Org.String()},
Name: &wrapperspb.StringValue{Value: "name"},
Description: &wrapperspb.StringValue{Value: "desc"},
@@ -438,17 +442,18 @@ func TestCreate(t *testing.T) {
errCode: codes.OK,
},
{
name: "Create a valid Project Scoped Group",
scopeId: defaultPGroup.GetScopeId(),
name: "Create a valid Project Scoped Group",
req: &pbs.CreateGroupRequest{
Item: &pb.Group{
ScopeId: defaultPGroup.GetScopeId(),
Name: &wrapperspb.StringValue{Value: "name"},
Description: &wrapperspb.StringValue{Value: "desc"},
},
},
res: &pbs.CreateGroupResponse{
Uri: fmt.Sprintf("scopes/%s/groups/%s_", defaultPGroup.GetScopeId(), iam.GroupPrefix),
Uri: fmt.Sprintf("groups/%s_", iam.GroupPrefix),
Item: &pb.Group{
ScopeId: defaultPGroup.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: defaultPGroup.GetScopeId(), Type: scope.Project.String()},
Name: &wrapperspb.StringValue{Value: "name"},
Description: &wrapperspb.StringValue{Value: "desc"},
@@ -491,7 +496,7 @@ func TestCreate(t *testing.T) {
s, err := groups.NewService(repo)
require.NoError(err, "Error when getting new group service.")
got, gErr := s.CreateGroup(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), req)
got, gErr := s.CreateGroup(auth.DisabledAuthTestContext(auth.WithScopeId(req.GetItem().GetScopeId())), req)
assert.Equal(tc.errCode, status.Code(gErr), "CreateGroup(%+v) got error %v, wanted %v", req, gErr, tc.errCode)
if got != nil {
assert.Contains(got.GetUri(), tc.res.Uri)
@@ -580,6 +585,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateGroupResponse{
Item: &pb.Group{
Id: og.GetPublicId(),
ScopeId: og.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: og.GetScopeId(), Type: scope.Org.String()},
Name: &wrapperspb.StringValue{Value: "new"},
Description: &wrapperspb.StringValue{Value: "desc"},
@@ -610,6 +616,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateGroupResponse{
Item: &pb.Group{
Id: og.GetPublicId(),
ScopeId: og.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: og.GetScopeId(), Type: scope.Org.String()},
Name: &wrapperspb.StringValue{Value: "new"},
Description: &wrapperspb.StringValue{Value: "desc"},
@@ -641,6 +648,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateGroupResponse{
Item: &pb.Group{
Id: pg.GetPublicId(),
ScopeId: pg.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: pg.GetScopeId(), Type: scope.Project.String()},
Name: &wrapperspb.StringValue{Value: "new"},
Description: &wrapperspb.StringValue{Value: "desc"},
@@ -672,6 +680,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateGroupResponse{
Item: &pb.Group{
Id: pg.GetPublicId(),
ScopeId: pg.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: pg.GetScopeId(), Type: scope.Project.String()},
Name: &wrapperspb.StringValue{Value: "new"},
Description: &wrapperspb.StringValue{Value: "desc"},
@@ -735,6 +744,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateGroupResponse{
Item: &pb.Group{
Id: og.GetPublicId(),
ScopeId: og.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: og.GetScopeId(), Type: scope.Org.String()},
Description: &wrapperspb.StringValue{Value: "default"},
CreatedTime: og.GetCreateTime().GetTimestamp(),
@@ -764,6 +774,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateGroupResponse{
Item: &pb.Group{
Id: og.GetPublicId(),
ScopeId: og.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: og.GetScopeId(), Type: scope.Org.String()},
Name: &wrapperspb.StringValue{Value: "updated"},
Description: &wrapperspb.StringValue{Value: "default"},
@@ -794,6 +805,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateGroupResponse{
Item: &pb.Group{
Id: og.GetPublicId(),
ScopeId: og.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: og.GetScopeId(), Type: scope.Org.String()},
Name: &wrapperspb.StringValue{Value: "default"},
Description: &wrapperspb.StringValue{Value: "notignored"},
@@ -94,7 +94,7 @@ func (s Service) CreateHostCatalog(ctx context.Context, req *pbs.CreateHostCatal
hc.Scope = authResults.Scope
return &pbs.CreateHostCatalogResponse{
Item: hc,
Uri: fmt.Sprintf("scopes/%s/host-catalogs/%s", authResults.Scope.GetId(), hc.GetId()),
Uri: fmt.Sprintf("host-catalogs/%s", hc.GetId()),
}, nil
}
@@ -235,6 +235,7 @@ func (s Service) deleteFromRepo(ctx context.Context, id string) (bool, error) {
func toProto(in *static.HostCatalog) *pb.HostCatalog {
out := pb.HostCatalog{
Id: in.GetPublicId(),
ScopeId: in.GetScopeId(),
CreatedTime: in.GetCreateTime().GetTimestamp(),
UpdatedTime: in.GetUpdateTime().GetTimestamp(),
Version: in.GetVersion(),
@@ -62,6 +62,7 @@ func TestGet(t *testing.T) {
pHostCatalog := &pb.HostCatalog{
Id: hc.GetPublicId(),
ScopeId: hc.GetScopeId(),
Scope: &scopepb.ScopeInfo{Id: proj.GetPublicId(), Type: scope.Project.String()},
Name: &wrappers.StringValue{Value: hc.GetName()},
Description: &wrappers.StringValue{Value: hc.GetDescription()},
@@ -139,6 +140,7 @@ func TestList(t *testing.T) {
for _, hc := range static.TestCatalogs(t, conn, pWithCatalogs.GetPublicId(), 3) {
wantSomeCatalogs = append(wantSomeCatalogs, &pb.HostCatalog{
Id: hc.GetPublicId(),
ScopeId: hc.GetScopeId(),
CreatedTime: hc.GetCreateTime().GetTimestamp(),
UpdatedTime: hc.GetUpdateTime().GetTimestamp(),
Scope: &scopepb.ScopeInfo{Id: pWithCatalogs.GetPublicId(), Type: scope.Project.String()},
@@ -151,6 +153,7 @@ func TestList(t *testing.T) {
for _, hc := range static.TestCatalogs(t, conn, pWithOtherCatalogs.GetPublicId(), 3) {
wantOtherCatalogs = append(wantOtherCatalogs, &pb.HostCatalog{
Id: hc.GetPublicId(),
ScopeId: hc.GetScopeId(),
CreatedTime: hc.GetCreateTime().GetTimestamp(),
UpdatedTime: hc.GetUpdateTime().GetTimestamp(),
Scope: &scopepb.ScopeInfo{Id: pWithOtherCatalogs.GetPublicId(), Type: scope.Project.String()},
@@ -325,8 +328,9 @@ func TestCreate(t *testing.T) {
Type: "static",
}},
res: &pbs.CreateHostCatalogResponse{
Uri: fmt.Sprintf("scopes/%s/host-catalogs/%s_", proj.GetPublicId(), static.HostCatalogPrefix),
Uri: fmt.Sprintf("host-catalogs/%s_", static.HostCatalogPrefix),
Item: &pb.HostCatalog{
ScopeId: proj.GetPublicId(),
Scope: &scopepb.ScopeInfo{Id: proj.GetPublicId(), Type: scope.Project.String()},
Name: &wrappers.StringValue{Value: "name"},
Description: &wrappers.StringValue{Value: "desc"},
@@ -456,6 +460,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateHostCatalogResponse{
Item: &pb.HostCatalog{
Id: hc.GetPublicId(),
ScopeId: hc.GetScopeId(),
Scope: &scopepb.ScopeInfo{Id: proj.GetPublicId(), Type: scope.Project.String()},
Name: &wrappers.StringValue{Value: "new"},
Description: &wrappers.StringValue{Value: "desc"},
@@ -479,6 +484,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateHostCatalogResponse{
Item: &pb.HostCatalog{
Id: hc.GetPublicId(),
ScopeId: hc.GetScopeId(),
Scope: &scopepb.ScopeInfo{Id: proj.GetPublicId(), Type: scope.Project.String()},
Name: &wrappers.StringValue{Value: "new"},
Description: &wrappers.StringValue{Value: "desc"},
@@ -533,6 +539,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateHostCatalogResponse{
Item: &pb.HostCatalog{
Id: hc.GetPublicId(),
ScopeId: hc.GetScopeId(),
Scope: &scopepb.ScopeInfo{Id: proj.GetPublicId(), Type: scope.Project.String()},
Description: &wrappers.StringValue{Value: "default"},
CreatedTime: hc.GetCreateTime().GetTimestamp(),
@@ -554,6 +561,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateHostCatalogResponse{
Item: &pb.HostCatalog{
Id: hc.GetPublicId(),
ScopeId: hc.GetScopeId(),
Scope: &scopepb.ScopeInfo{Id: proj.GetPublicId(), Type: scope.Project.String()},
Name: &wrappers.StringValue{Value: "default"},
CreatedTime: hc.GetCreateTime().GetTimestamp(),
@@ -576,6 +584,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateHostCatalogResponse{
Item: &pb.HostCatalog{
Id: hc.GetPublicId(),
ScopeId: hc.GetScopeId(),
Scope: &scopepb.ScopeInfo{Id: proj.GetPublicId(), Type: scope.Project.String()},
Name: &wrappers.StringValue{Value: "updated"},
Description: &wrappers.StringValue{Value: "default"},
@@ -599,6 +608,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateHostCatalogResponse{
Item: &pb.HostCatalog{
Id: hc.GetPublicId(),
ScopeId: hc.GetScopeId(),
Scope: &scopepb.ScopeInfo{Id: proj.GetPublicId(), Type: scope.Project.String()},
Name: &wrappers.StringValue{Value: "default"},
Description: &wrappers.StringValue{Value: "notignored"},
@@ -133,11 +133,7 @@ func (s Service) CreateHostSet(ctx context.Context, req *pbs.CreateHostSetReques
return nil, err
}
hcId := req.GetItem().GetHostCatalogId()
if hcId == "" {
hcId = req.GetHostCatalogId()
}
hostCatalog, _, err := s.getHostStructs(ctx, hcId, false)
hostCatalog, _, err := s.getHostStructs(ctx, req.GetItem().GetHostCatalogId(), false)
if err != nil {
return nil, err
}
@@ -150,14 +146,14 @@ func (s Service) CreateHostSet(ctx context.Context, req *pbs.CreateHostSetReques
return nil, authResults.Error
}
h, err := s.createInRepo(ctx, authResults.Scope.GetId(), hcId, req.GetItem())
h, err := s.createInRepo(ctx, authResults.Scope.GetId(), req.GetItem().GetHostCatalogId(), req.GetItem())
if err != nil {
return nil, err
}
h.Scope = authResults.Scope
return &pbs.CreateHostSetResponse{
Item: h,
Uri: fmt.Sprintf("scopes/%s/host-catalogs/%s/host-sets/%s", authResults.Scope.GetId(), hcId, h.GetId()),
Uri: fmt.Sprintf("host-sets/%s", h.GetId()),
}, nil
}
@@ -485,15 +481,11 @@ func validateGetRequest(req *pbs.GetHostSetRequest) error {
func validateCreateRequest(req *pbs.CreateHostSetRequest) error {
return handlers.ValidateCreateRequest(req.GetItem(), func() map[string]string {
hcId := req.GetItem().GetHostCatalogId()
if hcId == "" {
hcId = req.GetHostCatalogId()
}
badFields := map[string]string{}
if !handlers.ValidId(static.HostCatalogPrefix, hcId) {
if !handlers.ValidId(static.HostCatalogPrefix, req.GetItem().GetHostCatalogId()) {
badFields["host_catalog_id"] = "The field is incorrectly formatted."
}
switch host.SubtypeFromId(hcId) {
switch host.SubtypeFromId(req.GetItem().GetHostCatalogId()) {
case host.StaticSubtype:
if req.GetItem().GetType() != "" && req.GetItem().GetType() != host.StaticSubtype.String() {
badFields["type"] = "Doesn't match the parent resource's type."
@@ -228,8 +228,7 @@ func TestDelete(t *testing.T) {
name: "Bad Host Id formatting",
scopeId: proj.GetPublicId(),
req: &pbs.DeleteHostSetRequest{
HostCatalogId: hc.GetPublicId(),
Id: static.HostSetPrefix + "_bad_format",
Id: static.HostSetPrefix + "_bad_format",
},
res: nil,
errCode: codes.InvalidArgument,
@@ -264,8 +263,7 @@ func TestDelete_twice(t *testing.T) {
s, err := host_sets.NewService(repoFn)
require.NoError(t, err, "Couldn't create a new host set service.")
req := &pbs.DeleteHostSetRequest{
HostCatalogId: hc.GetPublicId(),
Id: h.GetPublicId(),
Id: h.GetPublicId(),
}
ctx := auth.DisabledAuthTestContext(auth.WithScopeId(proj.GetPublicId()))
got, gErr := s.DeleteHostSet(ctx, req)
@@ -290,10 +288,6 @@ func TestCreate(t *testing.T) {
}
hc := static.TestCatalogs(t, conn, proj.GetPublicId(), 1)[0]
toMerge := &pbs.CreateHostSetRequest{
HostCatalogId: hc.GetPublicId(),
}
defaultHcCreated, err := ptypes.Timestamp(hc.GetCreateTime().GetTimestamp())
require.NoError(t, err)
@@ -306,12 +300,13 @@ func TestCreate(t *testing.T) {
{
name: "Create a valid Host",
req: &pbs.CreateHostSetRequest{Item: &pb.HostSet{
Name: &wrappers.StringValue{Value: "name"},
Description: &wrappers.StringValue{Value: "desc"},
Type: "static",
HostCatalogId: hc.GetPublicId(),
Name: &wrappers.StringValue{Value: "name"},
Description: &wrappers.StringValue{Value: "desc"},
Type: "static",
}},
res: &pbs.CreateHostSetResponse{
Uri: fmt.Sprintf("scopes/%s/host-catalogs/%s/host-sets/%s_", proj.GetPublicId(), hc.GetPublicId(), static.HostSetPrefix),
Uri: fmt.Sprintf("host-sets/%s_", static.HostSetPrefix),
Item: &pb.HostSet{
HostCatalogId: hc.GetPublicId(),
Scope: &scopes.ScopeInfo{Id: proj.GetPublicId(), Type: scope.Project.String()},
@@ -325,20 +320,22 @@ func TestCreate(t *testing.T) {
{
name: "Create with unknown type",
req: &pbs.CreateHostSetRequest{Item: &pb.HostSet{
Name: &wrappers.StringValue{Value: "name"},
Description: &wrappers.StringValue{Value: "desc"},
Type: "ThisIsMadeUp",
HostCatalogId: hc.GetPublicId(),
Name: &wrappers.StringValue{Value: "name"},
Description: &wrappers.StringValue{Value: "desc"},
Type: "ThisIsMadeUp",
}},
errCode: codes.InvalidArgument,
},
{
name: "Create with no type",
req: &pbs.CreateHostSetRequest{Item: &pb.HostSet{
Name: &wrappers.StringValue{Value: "no type name"},
Description: &wrappers.StringValue{Value: "no type desc"},
HostCatalogId: hc.GetPublicId(),
Name: &wrappers.StringValue{Value: "no type name"},
Description: &wrappers.StringValue{Value: "no type desc"},
}},
res: &pbs.CreateHostSetResponse{
Uri: fmt.Sprintf("scopes/%s/host-catalogs/%s/host-sets/%s_", proj.GetPublicId(), hc.GetPublicId(), static.HostSetPrefix),
Uri: fmt.Sprintf("host-sets/%s_", static.HostSetPrefix),
Item: &pb.HostSet{
HostCatalogId: hc.GetPublicId(),
Scope: &scopes.ScopeInfo{Id: proj.GetPublicId(), Type: scope.Project.String()},
@@ -352,7 +349,8 @@ func TestCreate(t *testing.T) {
{
name: "Can't specify Id",
req: &pbs.CreateHostSetRequest{Item: &pb.HostSet{
Id: "not allowed to be set",
HostCatalogId: hc.GetPublicId(),
Id: "not allowed to be set",
}},
res: nil,
errCode: codes.InvalidArgument,
@@ -360,7 +358,8 @@ func TestCreate(t *testing.T) {
{
name: "Can't specify Created Time",
req: &pbs.CreateHostSetRequest{Item: &pb.HostSet{
CreatedTime: ptypes.TimestampNow(),
HostCatalogId: hc.GetPublicId(),
CreatedTime: ptypes.TimestampNow(),
}},
res: nil,
errCode: codes.InvalidArgument,
@@ -368,7 +367,8 @@ func TestCreate(t *testing.T) {
{
name: "Can't specify Update Time",
req: &pbs.CreateHostSetRequest{Item: &pb.HostSet{
UpdatedTime: ptypes.TimestampNow(),
HostCatalogId: hc.GetPublicId(),
UpdatedTime: ptypes.TimestampNow(),
}},
res: nil,
errCode: codes.InvalidArgument,
@@ -377,14 +377,12 @@ func TestCreate(t *testing.T) {
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
req := proto.Clone(toMerge).(*pbs.CreateHostSetRequest)
proto.Merge(req, tc.req)
s, err := host_sets.NewService(repoFn)
require.NoError(err, "Failed to create a new host set service.")
got, gErr := s.CreateHostSet(auth.DisabledAuthTestContext(auth.WithScopeId(proj.GetPublicId())), req)
assert.Equal(tc.errCode, status.Code(gErr), "CreateHostSet(%+v) got error %v, wanted %v", req, gErr, tc.errCode)
got, gErr := s.CreateHostSet(auth.DisabledAuthTestContext(auth.WithScopeId(proj.GetPublicId())), tc.req)
assert.Equal(tc.errCode, status.Code(gErr), "CreateHostSet(%+v) got error %v, wanted %v", tc.req, gErr, tc.errCode)
if got != nil {
assert.Contains(got.GetUri(), tc.res.GetUri())
assert.True(strings.HasPrefix(got.GetItem().GetId(), static.HostSetPrefix), got.GetItem().GetId())
@@ -404,7 +402,7 @@ func TestCreate(t *testing.T) {
if tc.res != nil {
tc.res.Item.Version = 1
}
assert.Empty(cmp.Diff(got, tc.res, protocmp.Transform()), "CreateHostSet(%q) got response %q, wanted %q", req, got, tc.res)
assert.Empty(cmp.Diff(got, tc.res, protocmp.Transform()), "CreateHostSet(%q) got response %q, wanted %q", tc.req, got, tc.res)
})
}
}
@@ -796,10 +794,9 @@ func TestAddHostSetHosts(t *testing.T) {
ss := static.TestSets(t, conn, hc.GetPublicId(), 1)[0]
tc.setup(ss)
req := &pbs.AddHostSetHostsRequest{
HostCatalogId: hc.GetPublicId(),
Id: ss.GetPublicId(),
Version: ss.GetVersion(),
HostIds: tc.addHosts,
Id: ss.GetPublicId(),
Version: ss.GetVersion(),
HostIds: tc.addHosts,
}
got, err := s.AddHostSetHosts(auth.DisabledAuthTestContext(auth.WithScopeId(proj.GetPublicId())), req)
@@ -93,14 +93,14 @@ func (s Service) CreateHost(ctx context.Context, req *pbs.CreateHostRequest) (*p
if err := validateCreateRequest(req); err != nil {
return nil, err
}
h, err := s.createInRepo(ctx, authResults.Scope.GetId(), req.GetHostCatalogId(), req.GetItem())
h, err := s.createInRepo(ctx, authResults.Scope.GetId(), req.GetItem().GetHostCatalogId(), req.GetItem())
if err != nil {
return nil, err
}
h.Scope = authResults.Scope
return &pbs.CreateHostResponse{
Item: h,
Uri: fmt.Sprintf("scopes/%s/host-catalogs/%s/hosts/%s", authResults.Scope.GetId(), req.GetHostCatalogId(), h.GetId()),
Uri: fmt.Sprintf("hosts/%s", h.GetId()),
}, nil
}
@@ -117,7 +117,7 @@ func (s Service) UpdateHost(ctx context.Context, req *pbs.UpdateHostRequest) (*p
if err := validateUpdateRequest(req); err != nil {
return nil, err
}
hc, err := s.updateInRepo(ctx, authResults.Scope.GetId(), req.GetHostCatalogId(), req.GetId(), req.GetUpdateMask().GetPaths(), req.GetItem())
hc, err := s.updateInRepo(ctx, authResults.Scope.GetId(), req.GetId(), req.GetUpdateMask().GetPaths(), req.GetItem())
if err != nil {
return nil, err
}
@@ -190,7 +190,7 @@ func (s Service) createInRepo(ctx context.Context, scopeId, catalogId string, it
return toProto(out, nil)
}
func (s Service) updateInRepo(ctx context.Context, scopeId, catalogId, id string, mask []string, item *pb.Host) (*pb.Host, error) {
func (s Service) updateInRepo(ctx context.Context, scopeId, id string, mask []string, item *pb.Host) (*pb.Host, error) {
ha := &pb.StaticHostAttributes{}
if err := handlers.StructToProto(item.GetAttributes(), ha); err != nil {
return nil, status.Errorf(codes.Internal, "Failed converting attributes to subtype proto: %s", err)
@@ -205,7 +205,7 @@ func (s Service) updateInRepo(ctx context.Context, scopeId, catalogId, id string
if addr := ha.GetAddress(); addr != nil {
opts = append(opts, static.WithAddress(addr.GetValue()))
}
h, err := static.NewHost(catalogId, opts...)
h, err := static.NewHost("ignored", opts...)
if err != nil {
return nil, status.Errorf(codes.Internal, "Unable to build host for update: %v.", err)
}
@@ -294,26 +294,16 @@ func toProto(in *static.Host, members []*static.HostSet) (*pb.Host, error) {
// * The type asserted by the ID and/or field is known
// * If relevant, the type derived from the id prefix matches what is claimed by the type field
func validateGetRequest(req *pbs.GetHostRequest) error {
return handlers.ValidateGetRequest(static.HostPrefix, req, func() map[string]string {
badFields := map[string]string{}
if !handlers.ValidId(static.HostCatalogPrefix, req.GetHostCatalogId()) {
badFields["host_catalog_id"] = "The field is incorrectly formatted."
}
return badFields
})
return handlers.ValidateGetRequest(static.HostPrefix, req, handlers.NoopValidatorFn)
}
func validateCreateRequest(req *pbs.CreateHostRequest) error {
return handlers.ValidateCreateRequest(req.GetItem(), func() map[string]string {
badFields := map[string]string{}
if !handlers.ValidId(static.HostCatalogPrefix, req.GetHostCatalogId()) {
if !handlers.ValidId(static.HostCatalogPrefix, req.GetItem().GetHostCatalogId()) {
badFields["host_catalog_id"] = "The field is incorrectly formatted."
}
if req.GetItem().GetHostCatalogId() != "" {
badFields["host_catalog_id"] = "This field is read only."
}
switch host.SubtypeFromId(req.GetHostCatalogId()) {
switch host.SubtypeFromId(req.GetItem().GetHostCatalogId()) {
case host.StaticSubtype:
if req.GetItem().GetType() != "" && req.GetItem().GetType() != host.StaticSubtype.String() {
badFields["type"] = "Doesn't match the parent resource's type."
@@ -333,12 +323,6 @@ func validateCreateRequest(req *pbs.CreateHostRequest) error {
func validateUpdateRequest(req *pbs.UpdateHostRequest) error {
return handlers.ValidateUpdateRequest(static.HostPrefix, req, req.GetItem(), func() map[string]string {
badFields := map[string]string{}
if !handlers.ValidId(static.HostCatalogPrefix, req.GetHostCatalogId()) {
badFields["host_catalog_id"] = "The field is incorrectly formatted."
}
if req.GetItem().GetHostCatalogId() != "" {
badFields["host_catalog_id"] = "This is a read only field and cannot be specified in an update request."
}
if req.GetItem().GetType() != "" {
badFields["type"] = "This is a read only field and cannot be specified in an update request."
}
@@ -347,13 +331,7 @@ func validateUpdateRequest(req *pbs.UpdateHostRequest) error {
}
func validateDeleteRequest(req *pbs.DeleteHostRequest) error {
return handlers.ValidateDeleteRequest(static.HostPrefix, req, func() map[string]string {
badFields := map[string]string{}
if !handlers.ValidId(static.HostCatalogPrefix, req.GetHostCatalogId()) {
badFields["host_catalog_id"] = "The field is incorrectly formatted."
}
return badFields
})
return handlers.ValidateDeleteRequest(static.HostPrefix, req, handlers.NoopValidatorFn)
}
func validateListRequest(req *pbs.ListHostsRequest) error {
@@ -45,10 +45,6 @@ func TestGet(t *testing.T) {
hc := static.TestCatalogs(t, conn, proj.GetPublicId(), 1)[0]
h := static.TestHosts(t, conn, hc.GetPublicId(), 1)[0]
toMerge := &pbs.GetHostRequest{
HostCatalogId: hc.GetPublicId(),
}
pHost := &pb.Host{
HostCatalogId: hc.GetPublicId(),
Id: h.GetPublicId(),
@@ -95,19 +91,16 @@ func TestGet(t *testing.T) {
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
assert := assert.New(t)
req := proto.Clone(toMerge).(*pbs.GetHostRequest)
proto.Merge(req, tc.req)
s, err := hosts.NewService(repoFn)
require.NoError(t, err, "Couldn't create a new host set service.")
got, gErr := s.GetHost(auth.DisabledAuthTestContext(auth.WithScopeId(proj.GetPublicId())), req)
assert.Equal(tc.errCode, status.Code(gErr), "GetHost(%+v) got error %v, wanted %v", req, gErr, tc.errCode)
got, gErr := s.GetHost(auth.DisabledAuthTestContext(auth.WithScopeId(proj.GetPublicId())), tc.req)
assert.Equal(tc.errCode, status.Code(gErr), "GetHost(%+v) got error %v, wanted %v", tc.req, gErr, tc.errCode)
if tc.res != nil {
tc.res.Item.Version = 1
}
assert.Empty(cmp.Diff(got, tc.res, protocmp.Transform()), "GetHost(%q) got response %q, wanted %q", req, got, tc.res)
assert.Empty(cmp.Diff(got, tc.res, protocmp.Transform()), "GetHost(%q) got response %q, wanted %q", tc.req, got, tc.res)
})
}
}
@@ -202,8 +195,7 @@ func TestDelete(t *testing.T) {
name: "Delete an Existing Host",
scopeId: proj.GetPublicId(),
req: &pbs.DeleteHostRequest{
HostCatalogId: hc.GetPublicId(),
Id: h.GetPublicId(),
Id: h.GetPublicId(),
},
res: &pbs.DeleteHostResponse{
Existed: true,
@@ -214,20 +206,7 @@ func TestDelete(t *testing.T) {
name: "Delete bad id Host",
scopeId: proj.GetPublicId(),
req: &pbs.DeleteHostRequest{
HostCatalogId: hc.GetPublicId(),
Id: static.HostPrefix + "_doesntexis",
},
res: &pbs.DeleteHostResponse{
Existed: false,
},
errCode: codes.OK,
},
{
name: "Delete bad host catalog id in Host",
scopeId: proj.GetPublicId(),
req: &pbs.DeleteHostRequest{
HostCatalogId: static.HostCatalogPrefix + "_doesntexis",
Id: h.GetPublicId(),
Id: static.HostPrefix + "_doesntexis",
},
res: &pbs.DeleteHostResponse{
Existed: false,
@@ -238,8 +217,7 @@ func TestDelete(t *testing.T) {
name: "Bad Host Id formatting",
scopeId: proj.GetPublicId(),
req: &pbs.DeleteHostRequest{
HostCatalogId: hc.GetPublicId(),
Id: static.HostPrefix + "_bad_format",
Id: static.HostPrefix + "_bad_format",
},
res: nil,
errCode: codes.InvalidArgument,
@@ -274,8 +252,7 @@ func TestDelete_twice(t *testing.T) {
s, err := hosts.NewService(repoFn)
require.NoError(t, err, "Couldn't create a new host set service.")
req := &pbs.DeleteHostRequest{
HostCatalogId: hc.GetPublicId(),
Id: h.GetPublicId(),
Id: h.GetPublicId(),
}
ctx := auth.DisabledAuthTestContext(auth.WithScopeId(proj.GetPublicId()))
got, gErr := s.DeleteHost(ctx, req)
@@ -300,10 +277,6 @@ func TestCreate(t *testing.T) {
}
hc := static.TestCatalogs(t, conn, proj.GetPublicId(), 1)[0]
toMerge := &pbs.CreateHostRequest{
HostCatalogId: hc.GetPublicId(),
}
defaultHcCreated, err := ptypes.Timestamp(hc.GetCreateTime().GetTimestamp())
require.NoError(t, err)
@@ -316,15 +289,16 @@ func TestCreate(t *testing.T) {
{
name: "Create a valid Host",
req: &pbs.CreateHostRequest{Item: &pb.Host{
Name: &wrappers.StringValue{Value: "name"},
Description: &wrappers.StringValue{Value: "desc"},
Type: "static",
HostCatalogId: hc.GetPublicId(),
Name: &wrappers.StringValue{Value: "name"},
Description: &wrappers.StringValue{Value: "desc"},
Type: "static",
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{
"address": structpb.NewStringValue("123.456.789"),
}},
}},
res: &pbs.CreateHostResponse{
Uri: fmt.Sprintf("scopes/%s/host-catalogs/%s/hosts/%s_", proj.GetPublicId(), hc.GetPublicId(), static.HostPrefix),
Uri: fmt.Sprintf("hosts/%s_", static.HostPrefix),
Item: &pb.Host{
HostCatalogId: hc.GetPublicId(),
Scope: &scopes.ScopeInfo{Id: proj.GetPublicId(), Type: scope.Project.String()},
@@ -341,33 +315,36 @@ func TestCreate(t *testing.T) {
{
name: "Create without address",
req: &pbs.CreateHostRequest{Item: &pb.Host{
Name: &wrappers.StringValue{Value: "name"},
Description: &wrappers.StringValue{Value: "desc"},
Type: "static",
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{}},
HostCatalogId: hc.GetPublicId(),
Name: &wrappers.StringValue{Value: "name"},
Description: &wrappers.StringValue{Value: "desc"},
Type: "static",
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{}},
}},
errCode: codes.InvalidArgument,
},
{
name: "Create with unknown type",
req: &pbs.CreateHostRequest{Item: &pb.Host{
Name: &wrappers.StringValue{Value: "name"},
Description: &wrappers.StringValue{Value: "desc"},
Type: "ThisIsMadeUp",
HostCatalogId: hc.GetPublicId(),
Name: &wrappers.StringValue{Value: "name"},
Description: &wrappers.StringValue{Value: "desc"},
Type: "ThisIsMadeUp",
}},
errCode: codes.InvalidArgument,
},
{
name: "Create with no type",
req: &pbs.CreateHostRequest{Item: &pb.Host{
Name: &wrappers.StringValue{Value: "no type name"},
Description: &wrappers.StringValue{Value: "no type desc"},
HostCatalogId: hc.GetPublicId(),
Name: &wrappers.StringValue{Value: "no type name"},
Description: &wrappers.StringValue{Value: "no type desc"},
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{
"address": structpb.NewStringValue("123.456.789"),
}},
}},
res: &pbs.CreateHostResponse{
Uri: fmt.Sprintf("scopes/%s/host-catalogs/%s/hosts/%s_", proj.GetPublicId(), hc.GetPublicId(), static.HostPrefix),
Uri: fmt.Sprintf("hosts/%s_", static.HostPrefix),
Item: &pb.Host{
HostCatalogId: hc.GetPublicId(),
Scope: &scopes.ScopeInfo{Id: proj.GetPublicId(), Type: scope.Project.String()},
@@ -384,7 +361,8 @@ func TestCreate(t *testing.T) {
{
name: "Can't specify Id",
req: &pbs.CreateHostRequest{Item: &pb.Host{
Id: "not allowed to be set",
HostCatalogId: hc.GetPublicId(),
Id: "not allowed to be set",
}},
res: nil,
errCode: codes.InvalidArgument,
@@ -392,7 +370,8 @@ func TestCreate(t *testing.T) {
{
name: "Can't specify Created Time",
req: &pbs.CreateHostRequest{Item: &pb.Host{
CreatedTime: ptypes.TimestampNow(),
HostCatalogId: hc.GetPublicId(),
CreatedTime: ptypes.TimestampNow(),
}},
res: nil,
errCode: codes.InvalidArgument,
@@ -409,14 +388,11 @@ func TestCreate(t *testing.T) {
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
req := proto.Clone(toMerge).(*pbs.CreateHostRequest)
proto.Merge(req, tc.req)
s, err := hosts.NewService(repoFn)
require.NoError(err, "Failed to create a new host set service.")
got, gErr := s.CreateHost(auth.DisabledAuthTestContext(auth.WithScopeId(proj.GetPublicId())), req)
assert.Equal(tc.errCode, status.Code(gErr), "CreateHost(%+v) got error %v, wanted %v", req, gErr, tc.errCode)
got, gErr := s.CreateHost(auth.DisabledAuthTestContext(auth.WithScopeId(proj.GetPublicId())), tc.req)
assert.Equal(tc.errCode, status.Code(gErr), "CreateHost(%+v) got error %v, wanted %v", tc.req, gErr, tc.errCode)
if got != nil {
assert.Contains(got.GetUri(), tc.res.GetUri())
assert.True(strings.HasPrefix(got.GetItem().GetId(), static.HostPrefix))
@@ -436,7 +412,7 @@ func TestCreate(t *testing.T) {
if tc.res != nil {
tc.res.Item.Version = 1
}
assert.Empty(cmp.Diff(got, tc.res, protocmp.Transform()), "CreateHost(%q) got response %q, wanted %q", req, got, tc.res)
assert.Empty(cmp.Diff(got, tc.res, protocmp.Transform()), "CreateHost(%q) got response %q, wanted %q", tc.req, got, tc.res)
})
}
}
@@ -475,8 +451,7 @@ func TestUpdate(t *testing.T) {
hCreated, err := ptypes.Timestamp(h.GetCreateTime().GetTimestamp())
require.NoError(t, err, "Failed to convert proto to timestamp")
toMerge := &pbs.UpdateHostRequest{
HostCatalogId: hc.GetPublicId(),
Id: h.GetPublicId(),
Id: h.GetPublicId(),
}
tested, err := hosts.NewService(repoFn)
@@ -55,7 +55,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, authResults.Scope.GetId())
gl, err := s.listFromRepo(ctx, req.GetScopeId())
if err != nil {
return nil, err
}
@@ -91,12 +91,12 @@ func (s Service) CreateRole(ctx context.Context, req *pbs.CreateRoleRequest) (*p
if err := validateCreateRequest(req, authResults.Scope); err != nil {
return nil, err
}
r, err := s.createInRepo(ctx, authResults.Scope.GetId(), req.GetItem())
r, err := s.createInRepo(ctx, req.GetItem().GetScopeId(), req.GetItem())
if err != nil {
return nil, err
}
r.Scope = authResults.Scope
return &pbs.CreateRoleResponse{Item: r, Uri: fmt.Sprintf("scopes/%s/roles/%s", authResults.Scope.GetId(), r.GetId())}, nil
return &pbs.CreateRoleResponse{Item: r, Uri: fmt.Sprintf("roles/%s", r.GetId())}, nil
}
// UpdateRole implements the interface pbs.RoleServiceServer.
@@ -471,6 +471,7 @@ func (s Service) removeGrantsInRepo(ctx context.Context, roleId string, grants [
func toProto(in *iam.Role, principals []iam.PrincipalRole, grants []*iam.RoleGrant) *pb.Role {
out := pb.Role{
Id: in.GetPublicId(),
ScopeId: in.GetScopeId(),
CreatedTime: in.GetCreateTime().GetTimestamp(),
UpdatedTime: in.GetUpdateTime().GetTimestamp(),
Version: in.GetVersion(),
@@ -533,6 +534,11 @@ func validateCreateRequest(req *pbs.CreateRoleRequest, s *scopes.ScopeInfo) erro
return handlers.ValidateCreateRequest(req.GetItem(), func() map[string]string {
badFields := map[string]string{}
item := req.GetItem()
if !handlers.ValidId(scope.Org.Prefix(), item.GetScopeId()) &&
!handlers.ValidId(scope.Project.Prefix(), item.GetScopeId()) &&
scope.Global.String() != item.GetScopeId() {
badFields["scope_id"] = "Improperly formatted field."
}
if item.GetGrantScopeId() != nil && s.GetType() == scope.Project.String() {
if item.GetGrantScopeId().Value != s.GetId() {
badFields["grant_scope_id"] = "Must be empty or set to the project_id when the scope type is project."
@@ -578,6 +584,9 @@ func validateDeleteRequest(req *pbs.DeleteRoleRequest) error {
func validateListRequest(req *pbs.ListRolesRequest) error {
badFields := map[string]string{}
if !handlers.ValidId(scope.Org.Prefix(), req.GetScopeId()) && !handlers.ValidId(scope.Project.Prefix(), req.GetScopeId()) {
badFields["scope_id"] = "Improperly formatted field."
}
if len(badFields) > 0 {
return handlers.InvalidArgumentErrorf("Improperly formatted identifier.", badFields)
}
@@ -77,6 +77,7 @@ func TestGet(t *testing.T) {
wantOrgRole := &pb.Role{
Id: or.GetPublicId(),
ScopeId: or.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: or.GetScopeId(), Type: scope.Org.String()},
Name: &wrapperspb.StringValue{Value: or.GetName()},
Description: &wrapperspb.StringValue{Value: or.GetDescription()},
@@ -88,6 +89,7 @@ func TestGet(t *testing.T) {
wantProjRole := &pb.Role{
Id: pr.GetPublicId(),
ScopeId: pr.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: pr.GetScopeId(), Type: scope.Project.String()},
Name: &wrapperspb.StringValue{Value: pr.GetName()},
Description: &wrapperspb.StringValue{Value: pr.GetDescription()},
@@ -189,6 +191,7 @@ func TestList(t *testing.T) {
or := iam.TestRole(t, conn, oWithRoles.GetPublicId())
wantOrgRoles = append(wantOrgRoles, &pb.Role{
Id: or.GetPublicId(),
ScopeId: or.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: or.GetScopeId(), Type: scope.Org.String()},
CreatedTime: or.GetCreateTime().GetTimestamp(),
UpdatedTime: or.GetUpdateTime().GetTimestamp(),
@@ -198,6 +201,7 @@ func TestList(t *testing.T) {
pr := iam.TestRole(t, conn, pWithRoles.GetPublicId())
wantProjRoles = append(wantProjRoles, &pb.Role{
Id: pr.GetPublicId(),
ScopeId: pr.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: pr.GetScopeId(), Type: scope.Project.String()},
CreatedTime: pr.GetCreateTime().GetTimestamp(),
UpdatedTime: pr.GetUpdateTime().GetTimestamp(),
@@ -208,36 +212,31 @@ func TestList(t *testing.T) {
cases := []struct {
name string
scopeId string
req *pbs.ListRolesRequest
res *pbs.ListRolesResponse
errCode codes.Code
}{
{
name: "List Many Role",
scopeId: oWithRoles.GetPublicId(),
req: &pbs.ListRolesRequest{},
req: &pbs.ListRolesRequest{ScopeId: oWithRoles.GetPublicId()},
res: &pbs.ListRolesResponse{Items: wantOrgRoles},
errCode: codes.OK,
},
{
name: "List No Roles",
scopeId: oNoRoles.GetPublicId(),
req: &pbs.ListRolesRequest{},
req: &pbs.ListRolesRequest{ScopeId: oNoRoles.GetPublicId()},
res: &pbs.ListRolesResponse{},
errCode: codes.OK,
},
{
name: "List Many Project Role",
scopeId: pWithRoles.GetPublicId(),
req: &pbs.ListRolesRequest{},
req: &pbs.ListRolesRequest{ScopeId: pWithRoles.GetPublicId()},
res: &pbs.ListRolesResponse{Items: wantProjRoles},
errCode: codes.OK,
},
{
name: "List No Project Roles",
scopeId: pNoRoles.GetPublicId(),
req: &pbs.ListRolesRequest{},
req: &pbs.ListRolesRequest{ScopeId: pNoRoles.GetPublicId()},
res: &pbs.ListRolesResponse{},
errCode: codes.OK,
},
@@ -247,7 +246,7 @@ func TestList(t *testing.T) {
s, err := roles.NewService(repoFn)
require.NoError(err, "Couldn't create new role service.")
got, gErr := s.ListRoles(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), tc.req)
got, gErr := s.ListRoles(auth.DisabledAuthTestContext(auth.WithScopeId(tc.req.GetScopeId())), tc.req)
assert.Equal(tc.errCode, status.Code(gErr), "ListRoles(%+v) got error %v, wanted %v", tc.req, gErr, tc.errCode)
assert.Empty(cmp.Diff(got, tc.res, protocmp.Transform()), "ListRoles(%q) got response %q, wanted %q", tc.req, got, tc.res)
})
@@ -314,7 +313,6 @@ func TestDelete(t *testing.T) {
name: "Project Scoped Delete bad Role id",
scopeId: pr.GetPublicId(),
req: &pbs.DeleteRoleRequest{
Id: iam.RolePrefix + "_doesntexis",
},
res: &pbs.DeleteRoleResponse{
@@ -373,22 +371,22 @@ func TestCreate(t *testing.T) {
cases := []struct {
name string
scopeId string
req *pbs.CreateRoleRequest
res *pbs.CreateRoleResponse
errCode codes.Code
}{
{
name: "Create a valid Role",
scopeId: defaultOrgRole.GetScopeId(),
name: "Create a valid Role",
req: &pbs.CreateRoleRequest{Item: &pb.Role{
ScopeId: defaultOrgRole.GetScopeId(),
Name: &wrapperspb.StringValue{Value: "name"},
Description: &wrapperspb.StringValue{Value: "desc"},
GrantScopeId: &wrapperspb.StringValue{Value: defaultProjRole.ScopeId},
}},
res: &pbs.CreateRoleResponse{
Uri: fmt.Sprintf("scopes/%s/roles/%s_", defaultOrgRole.GetScopeId(), iam.RolePrefix),
Uri: fmt.Sprintf("roles/%s_", iam.RolePrefix),
Item: &pb.Role{
ScopeId: defaultOrgRole.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: defaultOrgRole.GetScopeId(), Type: scope.Org.String()},
Name: &wrapperspb.StringValue{Value: "name"},
Description: &wrapperspb.StringValue{Value: "desc"},
@@ -399,17 +397,18 @@ func TestCreate(t *testing.T) {
errCode: codes.OK,
},
{
name: "Create a valid Project Scoped Role",
scopeId: defaultProjRole.GetScopeId(),
name: "Create a valid Project Scoped Role",
req: &pbs.CreateRoleRequest{
Item: &pb.Role{
ScopeId: defaultProjRole.GetScopeId(),
Name: &wrapperspb.StringValue{Value: "name"},
Description: &wrapperspb.StringValue{Value: "desc"},
},
},
res: &pbs.CreateRoleResponse{
Uri: fmt.Sprintf("scopes/%s/roles/%s_", defaultProjRole.GetScopeId(), iam.RolePrefix),
Uri: fmt.Sprintf("roles/%s_", iam.RolePrefix),
Item: &pb.Role{
ScopeId: defaultProjRole.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: defaultProjRole.GetScopeId(), Type: scope.Project.String()},
Name: &wrapperspb.StringValue{Value: "name"},
Description: &wrapperspb.StringValue{Value: "desc"},
@@ -420,10 +419,10 @@ func TestCreate(t *testing.T) {
errCode: codes.OK,
},
{
name: "Invalid grant scope ID",
scopeId: defaultProjRole.GetScopeId(),
name: "Invalid grant scope ID",
req: &pbs.CreateRoleRequest{
Item: &pb.Role{
ScopeId: defaultProjRole.GetScopeId(),
Name: &wrapperspb.StringValue{Value: "name"},
Description: &wrapperspb.StringValue{Value: "desc"},
GrantScopeId: &wrapperspb.StringValue{Value: defaultOrgRole.GetScopeId()},
@@ -433,27 +432,27 @@ func TestCreate(t *testing.T) {
errCode: codes.InvalidArgument,
},
{
name: "Can't specify Id",
scopeId: defaultProjRole.GetScopeId(),
name: "Can't specify Id",
req: &pbs.CreateRoleRequest{Item: &pb.Role{
Id: iam.RolePrefix + "_notallowed",
ScopeId: defaultProjRole.GetScopeId(),
Id: iam.RolePrefix + "_notallowed",
}},
res: nil,
errCode: codes.InvalidArgument,
},
{
name: "Can't specify Created Time",
scopeId: defaultProjRole.GetScopeId(),
name: "Can't specify Created Time",
req: &pbs.CreateRoleRequest{Item: &pb.Role{
ScopeId: defaultProjRole.GetScopeId(),
CreatedTime: ptypes.TimestampNow(),
}},
res: nil,
errCode: codes.InvalidArgument,
},
{
name: "Can't specify Update Time",
scopeId: defaultProjRole.GetScopeId(),
name: "Can't specify Update Time",
req: &pbs.CreateRoleRequest{Item: &pb.Role{
ScopeId: defaultProjRole.GetScopeId(),
UpdatedTime: ptypes.TimestampNow(),
}},
res: nil,
@@ -469,7 +468,7 @@ func TestCreate(t *testing.T) {
s, err := roles.NewService(repo)
require.NoError(err, "Error when getting new role service.")
got, gErr := s.CreateRole(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), req)
got, gErr := s.CreateRole(auth.DisabledAuthTestContext(auth.WithScopeId(tc.req.GetItem().GetScopeId())), req)
assert.Equal(tc.errCode, status.Code(gErr), "CreateRole(%+v) got error %v, wanted %v", req, gErr, tc.errCode)
if got != nil {
assert.Contains(got.GetUri(), tc.res.Uri)
@@ -581,6 +580,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateRoleResponse{
Item: &pb.Role{
Id: or.GetPublicId(),
ScopeId: or.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: or.GetScopeId(), Type: scope.Org.String()},
Name: &wrapperspb.StringValue{Value: "new"},
Description: &wrapperspb.StringValue{Value: "desc"},
@@ -609,6 +609,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateRoleResponse{
Item: &pb.Role{
Id: or.GetPublicId(),
ScopeId: or.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: or.GetScopeId(), Type: scope.Org.String()},
Name: &wrapperspb.StringValue{Value: "new"},
Description: &wrapperspb.StringValue{Value: "desc"},
@@ -638,6 +639,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateRoleResponse{
Item: &pb.Role{
Id: pr.GetPublicId(),
ScopeId: pr.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: pr.GetScopeId(), Type: scope.Project.String()},
Name: &wrapperspb.StringValue{Value: "new"},
Description: &wrapperspb.StringValue{Value: "desc"},
@@ -667,6 +669,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateRoleResponse{
Item: &pb.Role{
Id: pr.GetPublicId(),
ScopeId: pr.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: pr.GetScopeId(), Type: scope.Project.String()},
Name: &wrapperspb.StringValue{Value: "new"},
Description: &wrapperspb.StringValue{Value: "desc"},
@@ -728,6 +731,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateRoleResponse{
Item: &pb.Role{
Id: or.GetPublicId(),
ScopeId: or.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: or.GetScopeId(), Type: scope.Org.String()},
Description: &wrapperspb.StringValue{Value: "default"},
CreatedTime: or.GetCreateTime().GetTimestamp(),
@@ -755,6 +759,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateRoleResponse{
Item: &pb.Role{
Id: or.GetPublicId(),
ScopeId: or.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: or.GetScopeId(), Type: scope.Org.String()},
Name: &wrapperspb.StringValue{Value: "updated"},
Description: &wrapperspb.StringValue{Value: "default"},
@@ -783,6 +788,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateRoleResponse{
Item: &pb.Role{
Id: or.GetPublicId(),
ScopeId: or.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: or.GetScopeId(), Type: scope.Org.String()},
Name: &wrapperspb.StringValue{Value: "default"},
Description: &wrapperspb.StringValue{Value: "notignored"},
@@ -92,13 +92,13 @@ func (s Service) GetScope(ctx context.Context, req *pbs.GetScopeRequest) (*pbs.G
// CreateScope implements the interface pbs.ScopeServiceServer.
func (s Service) CreateScope(ctx context.Context, req *pbs.CreateScopeRequest) (*pbs.CreateScopeResponse, error) {
if req.GetScopeId() == "" {
if req.GetItem().GetScopeId() == "" {
return nil, handlers.InvalidArgumentErrorf(
"Argument errors found in the request.",
map[string]string{"scope_id": "Missing value for scope_id"},
)
}
authResults := auth.Verify(ctx, auth.WithScopeId(req.GetScopeId()))
authResults := auth.Verify(ctx, auth.WithScopeId(req.GetItem().GetScopeId()))
if authResults.Error != nil {
return nil, authResults.Error
}
@@ -290,6 +290,7 @@ func (s Service) listFromRepo(ctx context.Context, scopeId string) ([]*pb.Scope,
func ToProto(in *iam.Scope) *pb.Scope {
out := pb.Scope{
Id: in.GetPublicId(),
ScopeId: in.GetParentId(),
CreatedTime: in.GetCreateTime().GetTimestamp(),
UpdatedTime: in.GetUpdateTime().GetTimestamp(),
Version: in.GetVersion(),
@@ -62,6 +62,7 @@ func TestGet(t *testing.T) {
oScope := &pb.Scope{
Id: org.GetPublicId(),
ScopeId: org.GetParentId(),
Scope: &pb.ScopeInfo{Id: "global", Type: scope.Global.String()},
Name: &wrapperspb.StringValue{Value: org.GetName()},
Description: &wrapperspb.StringValue{Value: org.GetDescription()},
@@ -72,6 +73,7 @@ func TestGet(t *testing.T) {
pScope := &pb.Scope{
Id: proj.GetPublicId(),
ScopeId: proj.GetParentId(),
Scope: &pb.ScopeInfo{Id: oScope.Id, Type: scope.Org.String()},
Name: &wrapperspb.StringValue{Value: proj.GetName()},
Description: &wrapperspb.StringValue{Value: proj.GetDescription()},
@@ -216,6 +218,7 @@ func TestList(t *testing.T) {
require.NoError(t, err)
wantOrgs = append(wantOrgs, &pb.Scope{
Id: o.GetPublicId(),
ScopeId: globalScope.GetId(),
Scope: globalScope,
CreatedTime: o.GetCreateTime().GetTimestamp(),
UpdatedTime: o.GetUpdateTime().GetTimestamp(),
@@ -233,6 +236,7 @@ func TestList(t *testing.T) {
require.NoError(t, err)
wantProjects = append(wantProjects, &pb.Scope{
Id: p.GetPublicId(),
ScopeId: oWithProjects.GetPublicId(),
Scope: &pb.ScopeInfo{Id: oWithProjects.GetPublicId(), Type: scope.Org.String()},
CreatedTime: p.GetCreateTime().GetTimestamp(),
UpdatedTime: p.GetUpdateTime().GetTimestamp(),
@@ -423,8 +427,8 @@ func TestCreate(t *testing.T) {
name: "Create a valid Project",
scopeId: defaultOrg.GetPublicId(),
req: &pbs.CreateScopeRequest{
ScopeId: defaultOrg.GetPublicId(),
Item: &pb.Scope{
ScopeId: defaultOrg.GetPublicId(),
Name: &wrapperspb.StringValue{Value: "name"},
Description: &wrapperspb.StringValue{Value: "desc"},
},
@@ -432,6 +436,7 @@ func TestCreate(t *testing.T) {
res: &pbs.CreateScopeResponse{
Uri: "scopes/p_",
Item: &pb.Scope{
ScopeId: defaultOrg.GetPublicId(),
Scope: &pb.ScopeInfo{Id: defaultOrg.GetPublicId(), Type: scope.Org.String()},
Name: &wrapperspb.StringValue{Value: "name"},
Description: &wrapperspb.StringValue{Value: "desc"},
@@ -444,8 +449,8 @@ func TestCreate(t *testing.T) {
name: "Create a valid Org",
scopeId: scope.Global.String(),
req: &pbs.CreateScopeRequest{
ScopeId: scope.Global.String(),
Item: &pb.Scope{
ScopeId: scope.Global.String(),
Name: &wrapperspb.StringValue{Value: "name"},
Description: &wrapperspb.StringValue{Value: "desc"},
},
@@ -453,6 +458,7 @@ func TestCreate(t *testing.T) {
res: &pbs.CreateScopeResponse{
Uri: "scopes/o_",
Item: &pb.Scope{
ScopeId: scope.Global.String(),
Scope: &pb.ScopeInfo{Id: scope.Global.String(), Type: scope.Global.String()},
Name: &wrapperspb.StringValue{Value: "name"},
Description: &wrapperspb.StringValue{Value: "desc"},
@@ -621,6 +627,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateScopeResponse{
Item: &pb.Scope{
Id: proj.GetPublicId(),
ScopeId: org.GetPublicId(),
Scope: &pb.ScopeInfo{Id: org.GetPublicId(), Type: scope.Org.String()},
Name: &wrapperspb.StringValue{Value: "new"},
Description: &wrapperspb.StringValue{Value: "desc"},
@@ -644,6 +651,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateScopeResponse{
Item: &pb.Scope{
Id: org.GetPublicId(),
ScopeId: scope.Global.String(),
Scope: &pb.ScopeInfo{Id: scope.Global.String(), Type: scope.Global.String()},
Name: &wrapperspb.StringValue{Value: "new"},
Description: &wrapperspb.StringValue{Value: "desc"},
@@ -667,6 +675,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateScopeResponse{
Item: &pb.Scope{
Id: proj.GetPublicId(),
ScopeId: org.GetPublicId(),
Scope: &pb.ScopeInfo{Id: org.GetPublicId(), Type: scope.Org.String()},
Name: &wrapperspb.StringValue{Value: "new"},
Description: &wrapperspb.StringValue{Value: "desc"},
@@ -724,6 +733,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateScopeResponse{
Item: &pb.Scope{
Id: proj.GetPublicId(),
ScopeId: org.GetPublicId(),
Scope: &pb.ScopeInfo{Id: org.GetPublicId(), Type: scope.Org.String()},
Description: &wrapperspb.StringValue{Value: "defaultProj"},
CreatedTime: proj.GetCreateTime().GetTimestamp(),
@@ -745,6 +755,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateScopeResponse{
Item: &pb.Scope{
Id: proj.GetPublicId(),
ScopeId: org.GetPublicId(),
Scope: &pb.ScopeInfo{Id: org.GetPublicId(), Type: scope.Org.String()},
Name: &wrappers.StringValue{Value: "defaultProj"},
CreatedTime: proj.GetCreateTime().GetTimestamp(),
@@ -767,6 +778,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateScopeResponse{
Item: &pb.Scope{
Id: proj.GetPublicId(),
ScopeId: org.GetPublicId(),
Scope: &pb.ScopeInfo{Id: org.GetPublicId(), Type: scope.Org.String()},
Name: &wrapperspb.StringValue{Value: "updated"},
Description: &wrapperspb.StringValue{Value: "defaultProj"},
@@ -790,6 +802,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateScopeResponse{
Item: &pb.Scope{
Id: proj.GetPublicId(),
ScopeId: org.GetPublicId(),
Scope: &pb.ScopeInfo{Id: org.GetPublicId(), Type: scope.Org.String()},
Name: &wrapperspb.StringValue{Value: "defaultProj"},
Description: &wrapperspb.StringValue{Value: "notignored"},
@@ -12,6 +12,7 @@ import (
"github.com/hashicorp/boundary/internal/iam"
"github.com/hashicorp/boundary/internal/iam/store"
"github.com/hashicorp/boundary/internal/servers/controller/handlers"
"github.com/hashicorp/boundary/internal/types/scope"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/wrapperspb"
@@ -52,7 +53,7 @@ func (s Service) ListUsers(ctx context.Context, req *pbs.ListUsersRequest) (*pbs
if err := validateListRequest(req); err != nil {
return nil, err
}
ul, err := s.listFromRepo(ctx, authResults.Scope.GetId())
ul, err := s.listFromRepo(ctx, req.GetScopeId())
if err != nil {
return nil, err
}
@@ -93,7 +94,7 @@ func (s Service) CreateUser(ctx context.Context, req *pbs.CreateUserRequest) (*p
return nil, err
}
u.Scope = authResults.Scope
return &pbs.CreateUserResponse{Item: u, Uri: fmt.Sprintf("scopes/%s/users/%s", authResults.Scope.GetId(), u.GetId())}, nil
return &pbs.CreateUserResponse{Item: u, Uri: fmt.Sprintf("users/%s", u.GetId())}, nil
}
// UpdateUser implements the interface pbs.UserServiceServer.
@@ -239,6 +240,7 @@ func (s Service) listFromRepo(ctx context.Context, orgId string) ([]*pb.User, er
func toProto(in *iam.User) *pb.User {
out := pb.User{
Id: in.GetPublicId(),
ScopeId: in.GetScopeId(),
CreatedTime: in.GetCreateTime().GetTimestamp(),
UpdatedTime: in.GetUpdateTime().GetTimestamp(),
Version: in.GetVersion(),
@@ -262,7 +264,13 @@ func validateGetRequest(req *pbs.GetUserRequest) error {
}
func validateCreateRequest(req *pbs.CreateUserRequest) error {
return handlers.ValidateCreateRequest(req.GetItem(), handlers.NoopValidatorFn)
return handlers.ValidateCreateRequest(req.GetItem(), func() map[string]string {
badFields := map[string]string{}
if !handlers.ValidId(scope.Org.Prefix(), req.GetItem().GetScopeId()) {
badFields["scope_id"] = "Invalidly formatted required identifer."
}
return badFields
})
}
func validateUpdateRequest(req *pbs.UpdateUserRequest) error {
@@ -275,6 +283,9 @@ func validateDeleteRequest(req *pbs.DeleteUserRequest) error {
func validateListRequest(req *pbs.ListUsersRequest) error {
badFields := map[string]string{}
if !handlers.ValidId(scope.Org.Prefix(), req.GetScopeId()) {
badFields["scope_id"] = "Invalidly formatted required identifer."
}
if len(badFields) > 0 {
return handlers.InvalidArgumentErrorf("Improperly formatted identifier.", badFields)
}
@@ -50,6 +50,7 @@ func TestGet(t *testing.T) {
wantU := &pb.User{
Id: u.GetPublicId(),
ScopeId: u.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: u.ScopeId, Type: scope.Org.String()},
Name: &wrapperspb.StringValue{Value: u.GetName()},
Description: &wrapperspb.StringValue{Value: u.GetDescription()},
@@ -126,6 +127,7 @@ func TestList(t *testing.T) {
require.NoError(err)
wantUsers = append(wantUsers, &pb.User{
Id: u.GetPublicId(),
ScopeId: u.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: u.GetScopeId(), Type: scope.Org.String()},
CreatedTime: u.GetCreateTime().GetTimestamp(),
UpdatedTime: u.GetUpdateTime().GetTimestamp(),
@@ -135,22 +137,19 @@ func TestList(t *testing.T) {
cases := []struct {
name string
scopeId string
req *pbs.ListUsersRequest
res *pbs.ListUsersResponse
errCode codes.Code
}{
{
name: "List Many Users",
scopeId: oWithUsers.GetPublicId(),
req: &pbs.ListUsersRequest{},
req: &pbs.ListUsersRequest{ScopeId: oWithUsers.GetPublicId()},
res: &pbs.ListUsersResponse{Items: wantUsers},
errCode: codes.OK,
},
{
name: "List No Users",
scopeId: oNoUsers.GetPublicId(),
req: &pbs.ListUsersRequest{},
req: &pbs.ListUsersRequest{ScopeId: oNoUsers.GetPublicId()},
res: &pbs.ListUsersResponse{},
errCode: codes.OK,
},
@@ -160,7 +159,7 @@ func TestList(t *testing.T) {
s, err := users.NewService(repoFn)
require.NoError(err, "Couldn't create new user service.")
got, gErr := s.ListUsers(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), tc.req)
got, gErr := s.ListUsers(auth.DisabledAuthTestContext(auth.WithScopeId(tc.req.GetScopeId())), tc.req)
assert.Equal(tc.errCode, status.Code(gErr), "ListUsers(%+v) got error %v, wanted %v", tc.req, gErr, tc.errCode)
assert.Empty(cmp.Diff(got, tc.res, protocmp.Transform()), "ListUsers(%q) got response %q, wanted %q", tc.req, got, tc.res)
})
@@ -243,7 +242,6 @@ func TestCreate(t *testing.T) {
defaultUser, repo := createDefaultUserAndRepo(t)
defaultCreated, err := ptypes.Timestamp(defaultUser.GetCreateTime().GetTimestamp())
require.NoError(err, "Error converting proto to timestamp.")
toMerge := &pbs.CreateUserRequest{}
cases := []struct {
name string
@@ -254,12 +252,14 @@ func TestCreate(t *testing.T) {
{
name: "Create a valid User",
req: &pbs.CreateUserRequest{Item: &pb.User{
ScopeId: defaultUser.GetScopeId(),
Name: &wrapperspb.StringValue{Value: "name"},
Description: &wrapperspb.StringValue{Value: "desc"},
}},
res: &pbs.CreateUserResponse{
Uri: fmt.Sprintf("scopes/%s/users/u_", defaultUser.GetScopeId()),
Uri: fmt.Sprintf("users/%s_", iam.UserPrefix),
Item: &pb.User{
ScopeId: defaultUser.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: defaultUser.GetScopeId(), Type: scope.Org.String()},
Name: &wrapperspb.StringValue{Value: "name"},
Description: &wrapperspb.StringValue{Value: "desc"},
@@ -271,7 +271,8 @@ func TestCreate(t *testing.T) {
{
name: "Can't specify Id",
req: &pbs.CreateUserRequest{Item: &pb.User{
Id: iam.UserPrefix + "_notallowed",
ScopeId: defaultUser.GetScopeId(),
Id: iam.UserPrefix + "_notallowed",
}},
res: nil,
errCode: codes.InvalidArgument,
@@ -279,6 +280,7 @@ func TestCreate(t *testing.T) {
{
name: "Can't specify Created Time",
req: &pbs.CreateUserRequest{Item: &pb.User{
ScopeId: defaultUser.GetScopeId(),
CreatedTime: ptypes.TimestampNow(),
}},
res: nil,
@@ -287,6 +289,7 @@ func TestCreate(t *testing.T) {
{
name: "Can't specify Update Time",
req: &pbs.CreateUserRequest{Item: &pb.User{
ScopeId: defaultUser.GetScopeId(),
UpdatedTime: ptypes.TimestampNow(),
}},
res: nil,
@@ -296,14 +299,11 @@ func TestCreate(t *testing.T) {
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
assert := assert.New(t)
req := proto.Clone(toMerge).(*pbs.CreateUserRequest)
proto.Merge(req, tc.req)
s, err := users.NewService(repo)
require.NoError(err, "Error when getting new user service.")
got, gErr := s.CreateUser(auth.DisabledAuthTestContext(auth.WithScopeId(defaultUser.GetScopeId())), req)
assert.Equal(tc.errCode, status.Code(gErr), "CreateUser(%+v) got error %v, wanted %v", req, gErr, tc.errCode)
got, gErr := s.CreateUser(auth.DisabledAuthTestContext(auth.WithScopeId(defaultUser.GetScopeId())), tc.req)
assert.Equal(tc.errCode, status.Code(gErr), "CreateUser(%+v) got error %v, wanted %v", tc.req, gErr, tc.errCode)
if got != nil {
assert.Contains(got.GetUri(), tc.res.Uri)
assert.True(strings.HasPrefix(got.GetItem().GetId(), iam.UserPrefix+"_"))
@@ -320,7 +320,7 @@ func TestCreate(t *testing.T) {
got.Item.Id, tc.res.Item.Id = "", ""
got.Item.CreatedTime, got.Item.UpdatedTime, tc.res.Item.CreatedTime, tc.res.Item.UpdatedTime = nil, nil, nil, nil
}
assert.Empty(cmp.Diff(got, tc.res, protocmp.Transform()), "CreateUser(%q) got response %q, wanted %q", req, got, tc.res)
assert.Empty(cmp.Diff(got, tc.res, protocmp.Transform()), "CreateUser(%q) got response %q, wanted %q", tc.req, got, tc.res)
})
}
}
@@ -367,6 +367,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateUserResponse{
Item: &pb.User{
Id: u.GetPublicId(),
ScopeId: u.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: u.GetScopeId(), Type: scope.Org.String()},
Name: &wrapperspb.StringValue{Value: "new"},
Description: &wrapperspb.StringValue{Value: "desc"},
@@ -389,6 +390,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateUserResponse{
Item: &pb.User{
Id: u.GetPublicId(),
ScopeId: u.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: u.GetScopeId(), Type: scope.Org.String()},
Name: &wrapperspb.StringValue{Value: "new"},
Description: &wrapperspb.StringValue{Value: "desc"},
@@ -442,6 +444,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateUserResponse{
Item: &pb.User{
Id: u.GetPublicId(),
ScopeId: u.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: u.GetScopeId(), Type: scope.Org.String()},
Description: &wrapperspb.StringValue{Value: "default"},
CreatedTime: u.GetCreateTime().GetTimestamp(),
@@ -463,6 +466,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateUserResponse{
Item: &pb.User{
Id: u.GetPublicId(),
ScopeId: u.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: u.GetScopeId(), Type: scope.Org.String()},
Name: &wrapperspb.StringValue{Value: "updated"},
Description: &wrapperspb.StringValue{Value: "default"},
@@ -485,6 +489,7 @@ func TestUpdate(t *testing.T) {
res: &pbs.UpdateUserResponse{
Item: &pb.User{
Id: u.GetPublicId(),
ScopeId: u.GetScopeId(),
Scope: &scopes.ScopeInfo{Id: u.GetScopeId(), Type: scope.Org.String()},
Name: &wrapperspb.StringValue{Value: "default"},
Description: &wrapperspb.StringValue{Value: "notignored"},
@@ -96,7 +96,7 @@ type GetRequest interface {
func ValidateGetRequest(prefix string, r GetRequest, fn CustomValidatorFunc) error {
badFields := map[string]string{}
if !ValidId(prefix, r.GetId()) {
badFields["id"] = "Invalid formatted group id."
badFields["id"] = "Invalid formatted identifier."
}
for k, v := range fn() {
badFields[k] = v