Update API codes (#336)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2020-09-08 23:57:26 -04:00
1 parent 99d5456d7a
commit f94f21fd97
74 files changed
+1557 -1802

No files matched your search

-8
View File
@@ -228,14 +228,6 @@ func wrapHandlerWithCommonFuncs(h http.Handler, c *Controller, props HandlerProp
}
requestInfo.PublicId, requestInfo.Token, requestInfo.TokenFormat = auth.GetTokenFromRequest(c.logger, c.kms, r)
if requestInfo.TokenFormat == auth.AuthTokenTypeInvalid {
if disableAuthzFailures {
requestInfo.TokenFormat = auth.AuthTokenTypeBearer
} else {
w.WriteHeader(http.StatusForbidden)
return
}
}
ctx = auth.NewVerifierContext(ctx, c.logger, c.IamRepoFn, c.AuthTokenRepoFn, c.ServersRepoFn, c.kms, requestInfo)
// Set the context back on the request
@@ -125,11 +125,11 @@ func (s Service) DeleteAccount(ctx context.Context, req *pbs.DeleteAccountReques
if authResults.Error != nil {
return nil, authResults.Error
}
existed, err := s.deleteFromRepo(ctx, authResults.Scope.GetId(), req.GetId())
_, err := s.deleteFromRepo(ctx, authResults.Scope.GetId(), req.GetId())
if err != nil {
return nil, err
}
return &pbs.DeleteAccountResponse{Existed: existed}, nil
return nil, nil
}
// ChangePassword implements the interface pbs.AccountServiceServer.
@@ -343,7 +343,7 @@ func (s Service) parentAndAuthResult(ctx context.Context, id string, a action.Ty
return nil, res
}
if acct == nil {
res.Error = handlers.ForbiddenError()
res.Error = handlers.NotFoundError()
return nil, res
}
parentId = acct.GetAuthMethodId()
@@ -356,7 +356,7 @@ func (s Service) parentAndAuthResult(ctx context.Context, id string, a action.Ty
return nil, res
}
if authMeth == nil {
res.Error = handlers.ForbiddenError()
res.Error = handlers.NotFoundError()
return nil, res
}
opts = append(opts, auth.WithScopeId(authMeth.GetScopeId()), auth.WithPin(parentId))
@@ -413,6 +413,8 @@ func validateCreateRequest(req *pbs.CreateAccountRequest) error {
if pwAttrs.GetLoginName() == "" {
badFields["login_name"] = "This is a required field for this type."
}
default:
badFields["auth_method_id"] = "Unknown auth method type from ID."
}
return badFields
})
@@ -72,7 +72,7 @@ func TestGet(t *testing.T) {
name: "Get a non existing account",
req: &pbs.GetAccountRequest{Id: password.AccountPrefix + "_DoesntExis"},
res: nil,
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Wrong id prefix",
@@ -165,7 +165,7 @@ func TestList(t *testing.T) {
{
name: "Unfound Auth Method",
authMethod: password.AuthMethodPrefix + "_DoesntExis",
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
}
for _, tc := range cases {
@@ -208,9 +208,6 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteAccountRequest{
Id: ac.GetPublicId(),
},
res: &pbs.DeleteAccountResponse{
Existed: true,
},
errCode: codes.OK,
},
{
@@ -218,7 +215,7 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteAccountRequest{
Id: password.AccountPrefix + "_doesntexis",
},
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Bad account id formatting",
@@ -257,12 +254,11 @@ func TestDelete_twice(t *testing.T) {
req := &pbs.DeleteAccountRequest{
Id: ac.GetPublicId(),
}
got, gErr := s.DeleteAccount(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), req)
_, gErr := s.DeleteAccount(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), req)
assert.NoError(gErr, "First attempt")
assert.True(got.GetExisted(), "Expected existed to be true for the first delete.")
got, gErr = s.DeleteAccount(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), req)
_, gErr = s.DeleteAccount(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), req)
assert.Error(gErr, "Second attempt")
assert.Equal(codes.PermissionDenied, status.Code(gErr), "Expected permission denied for the second delete.")
assert.Equal(codes.NotFound, status.Code(gErr), "Expected permission denied for the second delete.")
}
func TestCreate(t *testing.T) {
@@ -710,7 +706,7 @@ func TestUpdate(t *testing.T) {
Description: &wrapperspb.StringValue{Value: "desc"},
},
},
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Cant change Id",
@@ -149,7 +149,7 @@ func (s Service) authResult(ctx context.Context, id string, a action.Type) auth.
return res
}
if authMeth == nil {
res.Error = handlers.ForbiddenError()
res.Error = handlers.NotFoundError()
return res
}
@@ -130,11 +130,11 @@ func (s Service) DeleteAuthMethod(ctx context.Context, req *pbs.DeleteAuthMethod
if authResults.Error != nil {
return nil, authResults.Error
}
existed, err := s.deleteFromRepo(ctx, authResults.Scope.GetId(), req.GetId())
_, err := s.deleteFromRepo(ctx, authResults.Scope.GetId(), req.GetId())
if err != nil {
return nil, err
}
return &pbs.DeleteAuthMethodResponse{Existed: existed}, nil
return nil, nil
}
func (s Service) getFromRepo(ctx context.Context, id string) (*pb.AuthMethod, error) {
@@ -279,7 +279,7 @@ func (s Service) authResult(ctx context.Context, id string, a action.Type) auth.
return res
}
if scp == nil {
res.Error = handlers.ForbiddenError()
res.Error = handlers.NotFoundError()
return res
}
default:
@@ -294,7 +294,7 @@ func (s Service) authResult(ctx context.Context, id string, a action.Type) auth.
return res
}
if authMeth == nil {
res.Error = handlers.ForbiddenError()
res.Error = handlers.NotFoundError()
return res
}
parentId = authMeth.GetScopeId()
@@ -382,8 +382,12 @@ func validateDeleteRequest(req *pbs.DeleteAuthMethodRequest) error {
return handlers.ValidateDeleteRequest(password.AuthMethodPrefix, req, handlers.NoopValidatorFn)
}
func validateListRequest(_ *pbs.ListAuthMethodsRequest) error {
func validateListRequest(req *pbs.ListAuthMethodsRequest) error {
badFields := map[string]string{}
if !handlers.ValidId(scope.Org.Prefix(), req.GetScopeId()) &&
req.GetScopeId() != scope.Global.String() {
badFields["scope_id"] = "This field must be a valid project scope id."
}
if len(badFields) > 0 {
return handlers.InvalidArgumentErrorf("Improperly formatted identifier.", badFields)
}
@@ -80,7 +80,7 @@ func TestGet(t *testing.T) {
scopeId: o.GetPublicId(),
req: &pbs.GetAuthMethodRequest{Id: password.AuthMethodPrefix + "_DoesntExis"},
res: nil,
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Wrong id prefix",
@@ -186,11 +186,10 @@ func TestList(t *testing.T) {
res: &pbs.ListAuthMethodsResponse{},
errCode: codes.OK,
},
// TODO: When an auth method doesn't exist, we should return a 404 instead of an empty list.
{
name: "Unfound Auth Method",
scopeId: password.AuthMethodPrefix + "_DoesntExis",
errCode: codes.PermissionDenied,
scopeId: "o_DoesntExis",
errCode: codes.NotFound,
},
}
for _, tc := range cases {
@@ -236,9 +235,6 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteAuthMethodRequest{
Id: am.GetPublicId(),
},
res: &pbs.DeleteAuthMethodResponse{
Existed: true,
},
errCode: codes.OK,
},
{
@@ -246,14 +242,13 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteAuthMethodRequest{
Id: password.AuthMethodPrefix + "_doesntexis",
},
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Bad AuthMethod Id formatting",
req: &pbs.DeleteAuthMethodRequest{
Id: "bad_format",
},
res: nil,
errCode: codes.InvalidArgument,
},
}
@@ -290,12 +285,11 @@ func TestDelete_twice(t *testing.T) {
req := &pbs.DeleteAuthMethodRequest{
Id: am.GetPublicId(),
}
got, gErr := s.DeleteAuthMethod(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), req)
_, gErr := s.DeleteAuthMethod(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), req)
assert.NoError(gErr, "First attempt")
assert.True(got.GetExisted(), "Expected existed to be true for the first delete.")
got, gErr = s.DeleteAuthMethod(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), req)
_, gErr = s.DeleteAuthMethod(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), req)
assert.Error(gErr, "Second attempt")
assert.Equal(codes.PermissionDenied, status.Code(gErr), "Expected permission denied for the second delete.")
assert.Equal(codes.NotFound, status.Code(gErr), "Expected permission denied for the second delete.")
}
func TestCreate(t *testing.T) {
@@ -705,7 +699,7 @@ func TestUpdate(t *testing.T) {
Description: &wrapperspb.StringValue{Value: "desc"},
},
},
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Cant change Id",
@@ -83,11 +83,11 @@ func (s Service) DeleteAuthToken(ctx context.Context, req *pbs.DeleteAuthTokenRe
if authResults.Error != nil {
return nil, authResults.Error
}
existed, err := s.deleteFromRepo(ctx, req.GetId())
_, err := s.deleteFromRepo(ctx, req.GetId())
if err != nil {
return nil, err
}
return &pbs.DeleteAuthTokenResponse{Existed: existed}, nil
return nil, nil
}
func (s Service) getFromRepo(ctx context.Context, id string) (*pb.AuthToken, error) {
@@ -159,7 +159,7 @@ func (s Service) authResult(ctx context.Context, id string, a action.Type) auth.
return res
}
if scp == nil {
res.Error = handlers.ForbiddenError()
res.Error = handlers.NotFoundError()
return res
}
default:
@@ -174,7 +174,7 @@ func (s Service) authResult(ctx context.Context, id string, a action.Type) auth.
return res
}
if authTok == nil {
res.Error = handlers.ForbiddenError()
res.Error = handlers.NotFoundError()
return res
}
parentId = authTok.GetScopeId()
@@ -213,7 +213,8 @@ func validateDeleteRequest(req *pbs.DeleteAuthTokenRequest) error {
func validateListRequest(req *pbs.ListAuthTokensRequest) error {
badFields := map[string]string{}
if !handlers.ValidId(scope.Org.Prefix(), req.GetScopeId()) {
if !handlers.ValidId(scope.Org.Prefix(), req.GetScopeId()) &&
req.GetScopeId() != scope.Global.String() {
badFields["scope_id"] = "Incorrectly formatted identifier."
}
if len(badFields) > 0 {
@@ -68,7 +68,7 @@ func TestGet(t *testing.T) {
name: "Get a non existing auth token",
req: &pbs.GetAuthTokenRequest{Id: authtoken.AuthTokenPrefix + "_DoesntExis"},
res: nil,
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Wrong id prefix",
@@ -170,7 +170,7 @@ func TestList(t *testing.T) {
{
name: "Unfound Org",
scope: scope.Org.Prefix() + "_DoesntExis",
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
}
for _, tc := range cases {
@@ -217,9 +217,6 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteAuthTokenRequest{
Id: at.GetPublicId(),
},
res: &pbs.DeleteAuthTokenResponse{
Existed: true,
},
errCode: codes.OK,
},
{
@@ -228,7 +225,7 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteAuthTokenRequest{
Id: authtoken.AuthTokenPrefix + "_doesntexis",
},
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Bad token id formatting",
@@ -236,7 +233,6 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteAuthTokenRequest{
Id: "bad_format",
},
res: nil,
errCode: codes.InvalidArgument,
},
}
@@ -272,10 +268,9 @@ func TestDelete_twice(t *testing.T) {
req := &pbs.DeleteAuthTokenRequest{
Id: at.GetPublicId(),
}
got, gErr := s.DeleteAuthToken(auth.DisabledAuthTestContext(auth.WithScopeId(at.GetScopeId())), req)
_, gErr := s.DeleteAuthToken(auth.DisabledAuthTestContext(auth.WithScopeId(at.GetScopeId())), req)
assert.NoError(gErr, "First attempt")
assert.True(got.GetExisted(), "Expected existed to be true for the first delete.")
got, gErr = s.DeleteAuthToken(auth.DisabledAuthTestContext(auth.WithScopeId(at.GetScopeId())), req)
_, gErr = s.DeleteAuthToken(auth.DisabledAuthTestContext(auth.WithScopeId(at.GetScopeId())), req)
assert.Error(gErr, "Second attempt")
assert.Equal(codes.PermissionDenied, status.Code(gErr), "Expected permission denied for the second delete.")
assert.Equal(codes.NotFound, status.Code(gErr), "Expected permission denied for the second delete.")
}
@@ -16,6 +16,11 @@ import (
)
// NotFoundError returns an ApiError indicating a resource couldn't be found.
func NotFoundError() error {
return status.Error(codes.NotFound, "Resource not found.")
}
// NotFoundErrorf returns an ApiError indicating a resource couldn't be found.
func NotFoundErrorf(msg string, a ...interface{}) error {
return status.Errorf(codes.NotFound, msg, a...)
}
@@ -24,6 +29,10 @@ func ForbiddenError() error {
return status.Error(codes.PermissionDenied, "Forbidden.")
}
func UnauthenticatedError() error {
return status.Error(codes.Unauthenticated, "Unauthenticated, or invalid token.")
}
func InvalidArgumentErrorf(msg string, fields map[string]string) error {
st := status.New(codes.InvalidArgument, msg)
br := &errdetails.BadRequest{}
@@ -126,11 +126,11 @@ func (s Service) DeleteGroup(ctx context.Context, req *pbs.DeleteGroupRequest) (
if authResults.Error != nil {
return nil, authResults.Error
}
existed, err := s.deleteFromRepo(ctx, req.GetId())
_, err := s.deleteFromRepo(ctx, req.GetId())
if err != nil {
return nil, err
}
return &pbs.DeleteGroupResponse{Existed: existed}, nil
return nil, nil
}
// AddGroupMembers implements the interface pbs.GroupServiceServer.
@@ -364,7 +364,7 @@ func (s Service) authResult(ctx context.Context, id string, a action.Type) auth.
return res
}
if scp == nil {
res.Error = handlers.ForbiddenError()
res.Error = handlers.NotFoundError()
return res
}
default:
@@ -374,7 +374,7 @@ func (s Service) authResult(ctx context.Context, id string, a action.Type) auth.
return res
}
if grp == nil {
res.Error = handlers.ForbiddenError()
res.Error = handlers.NotFoundError()
return res
}
parentId = grp.GetScopeId()
@@ -439,7 +439,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()) {
if !handlers.ValidId(scope.Org.Prefix(), req.GetScopeId()) &&
!handlers.ValidId(scope.Project.Prefix(), req.GetScopeId()) &&
req.GetScopeId() != scope.Global.String() {
badFields["scope_id"] = "Incorrectly formatted identifier."
}
if len(badFields) > 0 {
@@ -142,7 +142,7 @@ func TestGet(t *testing.T) {
name: "Get a non existant Group",
req: &pbs.GetGroupRequest{Id: iam.GroupPrefix + "_DoesntExis"},
res: nil,
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Wrong id prefix",
@@ -168,7 +168,7 @@ func TestGet(t *testing.T) {
scopeId: pg.GetScopeId(),
req: &pbs.GetGroupRequest{Id: iam.GroupPrefix + "_DoesntExis"},
res: nil,
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Project Scoped Wrong id prefix",
@@ -297,9 +297,6 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteGroupRequest{
Id: og.GetPublicId(),
},
res: &pbs.DeleteGroupResponse{
Existed: true,
},
errCode: codes.OK,
},
{
@@ -308,8 +305,7 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteGroupRequest{
Id: iam.GroupPrefix + "_doesntexis",
},
res: nil,
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Bad Group Id formatting",
@@ -317,7 +313,6 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteGroupRequest{
Id: "bad_format",
},
res: nil,
errCode: codes.InvalidArgument,
},
{
@@ -326,9 +321,6 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteGroupRequest{
Id: pg.GetPublicId(),
},
res: &pbs.DeleteGroupResponse{
Existed: true,
},
errCode: codes.OK,
},
{
@@ -337,8 +329,7 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteGroupRequest{
Id: iam.GroupPrefix + "_doesntexis",
},
res: nil,
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
}
for _, tc := range cases {
@@ -363,24 +354,22 @@ func TestDelete_twice(t *testing.T) {
Id: og.GetPublicId(),
}
ctx := auth.DisabledAuthTestContext(auth.WithScopeId(scopeId))
got, gErr := s.DeleteGroup(ctx, req)
_, gErr := s.DeleteGroup(ctx, req)
assert.NoError(gErr, "First attempt")
assert.True(got.GetExisted(), "Expected existed to be true for the first delete.")
got, gErr = s.DeleteGroup(ctx, req)
_, gErr = s.DeleteGroup(ctx, req)
assert.Error(gErr, "Second attempt")
assert.Equal(codes.PermissionDenied, status.Code(gErr), "Expected permission denied for the second delete.")
assert.Equal(codes.NotFound, status.Code(gErr), "Expected permission denied for the second delete.")
scopeId = pg.GetScopeId()
projReq := &pbs.DeleteGroupRequest{
Id: pg.GetPublicId(),
}
ctx = auth.DisabledAuthTestContext(auth.WithScopeId(scopeId))
got, gErr = s.DeleteGroup(ctx, projReq)
_, gErr = s.DeleteGroup(ctx, projReq)
assert.NoError(gErr, "First attempt")
assert.True(got.GetExisted(), "Expected existed to be true for the first delete.")
got, gErr = s.DeleteGroup(ctx, projReq)
_, gErr = s.DeleteGroup(ctx, projReq)
assert.Error(gErr, "Second attempt")
assert.Equal(codes.PermissionDenied, status.Code(gErr), "Expected permission denied for the second delete.")
assert.Equal(codes.NotFound, status.Code(gErr), "Expected permission denied for the second delete.")
}
func TestCreate(t *testing.T) {
@@ -826,7 +815,7 @@ func TestUpdate(t *testing.T) {
Description: &wrapperspb.StringValue{Value: "desc"},
},
},
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Cant change Id",
@@ -131,11 +131,11 @@ func (s Service) DeleteHostCatalog(ctx context.Context, req *pbs.DeleteHostCatal
if authResults.Error != nil {
return nil, authResults.Error
}
existed, err := s.deleteFromRepo(ctx, req.GetId())
_, err := s.deleteFromRepo(ctx, req.GetId())
if err != nil {
return nil, err
}
return &pbs.DeleteHostCatalogResponse{Existed: existed}, nil
return nil, nil
}
func (s Service) getFromRepo(ctx context.Context, id string) (*pb.HostCatalog, error) {
@@ -258,7 +258,7 @@ func (s Service) authResult(ctx context.Context, id string, a action.Type) auth.
return res
}
if scp == nil {
res.Error = handlers.ForbiddenError()
res.Error = handlers.NotFoundError()
return res
}
default:
@@ -273,7 +273,7 @@ func (s Service) authResult(ctx context.Context, id string, a action.Type) auth.
return res
}
if cat == nil {
res.Error = handlers.ForbiddenError()
res.Error = handlers.NotFoundError()
return res
}
parentId = cat.GetScopeId()
@@ -91,7 +91,7 @@ func TestGet(t *testing.T) {
name: "Get a non existing Host Catalog",
req: &pbs.GetHostCatalogRequest{Id: static.HostCatalogPrefix + "_DoesntExis"},
res: nil,
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Wrong id prefix",
@@ -196,7 +196,7 @@ func TestList(t *testing.T) {
{
name: "Unfound Catalogs",
scopeId: scope.Project.Prefix() + "_DoesntExis",
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Bad scope level",
@@ -238,9 +238,6 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteHostCatalogRequest{
Id: hc.GetPublicId(),
},
res: &pbs.DeleteHostCatalogResponse{
Existed: true,
},
errCode: codes.OK,
},
{
@@ -249,7 +246,7 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteHostCatalogRequest{
Id: static.HostCatalogPrefix + "_doesntexis",
},
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Bad HostCatalog Id formatting",
@@ -257,7 +254,6 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteHostCatalogRequest{
Id: static.HostCatalogPrefix + "_bad_format",
},
res: nil,
errCode: codes.InvalidArgument,
},
}
@@ -282,12 +278,11 @@ func TestDelete_twice(t *testing.T) {
Id: hc.GetPublicId(),
}
ctx := auth.DisabledAuthTestContext(auth.WithScopeId(proj.GetPublicId()))
got, gErr := s.DeleteHostCatalog(ctx, req)
_, gErr := s.DeleteHostCatalog(ctx, req)
assert.NoError(gErr, "First attempt")
assert.True(got.GetExisted(), "Expected existed to be true for the first delete.")
got, gErr = s.DeleteHostCatalog(ctx, req)
_, gErr = s.DeleteHostCatalog(ctx, req)
assert.Error(gErr, "Second attempt")
assert.Equal(codes.PermissionDenied, status.Code(gErr), "Expected permission denied for the second delete.")
assert.Equal(codes.NotFound, status.Code(gErr), "Expected permission denied for the second delete.")
}
func TestCreate(t *testing.T) {
@@ -649,7 +644,7 @@ func TestUpdate(t *testing.T) {
Description: &wrappers.StringValue{Value: "desc"},
},
},
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Cant change Id",
@@ -126,11 +126,11 @@ func (s Service) DeleteHostSet(ctx context.Context, req *pbs.DeleteHostSetReques
if authResults.Error != nil {
return nil, authResults.Error
}
existed, err := s.deleteFromRepo(ctx, authResults.Scope.GetId(), req.GetId())
_, err := s.deleteFromRepo(ctx, authResults.Scope.GetId(), req.GetId())
if err != nil {
return nil, err
}
return &pbs.DeleteHostSetResponse{Existed: existed}, nil
return nil, nil
}
// AddHostSetHosts implements the interface pbs.HostSetServiceServer.
@@ -359,7 +359,7 @@ func (s Service) parentAndAuthResult(ctx context.Context, id string, a action.Ty
return nil, res
}
if set == nil {
res.Error = handlers.ForbiddenError()
res.Error = handlers.NotFoundError()
return nil, res
}
parentId = set.GetCatalogId()
@@ -372,7 +372,7 @@ func (s Service) parentAndAuthResult(ctx context.Context, id string, a action.Ty
return nil, res
}
if cat == nil {
res.Error = handlers.ForbiddenError()
res.Error = handlers.NotFoundError()
return nil, res
}
opts = append(opts, auth.WithScopeId(cat.GetScopeId()), auth.WithPin(parentId))
@@ -76,7 +76,7 @@ func TestGet(t *testing.T) {
name: "Get a non existing Host Set",
req: &pbs.GetHostSetRequest{Id: static.HostSetPrefix + "_DoesntExis"},
res: nil,
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Wrong id prefix",
@@ -201,9 +201,6 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteHostSetRequest{
Id: h.GetPublicId(),
},
res: &pbs.DeleteHostSetResponse{
Existed: true,
},
errCode: codes.OK,
},
{
@@ -212,8 +209,7 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteHostSetRequest{
Id: static.HostSetPrefix + "_doesntexis",
},
res: nil,
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Delete bad host catalog id in Host",
@@ -221,8 +217,7 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteHostSetRequest{
Id: h.GetPublicId(),
},
res: nil,
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Bad Host Id formatting",
@@ -230,7 +225,6 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteHostSetRequest{
Id: static.HostSetPrefix + "_bad_format",
},
res: nil,
errCode: codes.InvalidArgument,
},
}
@@ -266,12 +260,11 @@ func TestDelete_twice(t *testing.T) {
Id: h.GetPublicId(),
}
ctx := auth.DisabledAuthTestContext(auth.WithScopeId(proj.GetPublicId()))
got, gErr := s.DeleteHostSet(ctx, req)
_, gErr := s.DeleteHostSet(ctx, req)
assert.NoError(gErr, "First attempt")
assert.True(got.GetExisted(), "Expected existed to be true for the first delete.")
_, gErr = s.DeleteHostSet(ctx, req)
assert.Error(gErr, "Second attempt")
assert.Equal(codes.PermissionDenied, status.Code(gErr), "Expected permission denied for the second delete.")
assert.Equal(codes.NotFound, status.Code(gErr), "Expected permission denied for the second delete.")
}
func TestCreate(t *testing.T) {
@@ -665,7 +658,7 @@ func TestUpdate(t *testing.T) {
Description: &wrappers.StringValue{Value: "desc"},
},
},
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Cant change Id",
@@ -126,11 +126,11 @@ func (s Service) DeleteHost(ctx context.Context, req *pbs.DeleteHostRequest) (*p
if authResults.Error != nil {
return nil, authResults.Error
}
existed, err := s.deleteFromRepo(ctx, authResults.Scope.GetId(), req.GetId())
_, err := s.deleteFromRepo(ctx, authResults.Scope.GetId(), req.GetId())
if err != nil {
return nil, err
}
return &pbs.DeleteHostResponse{Existed: existed}, nil
return nil, nil
}
func (s Service) getFromRepo(ctx context.Context, id string) (*pb.Host, error) {
@@ -272,7 +272,7 @@ func (s Service) parentAndAuthResult(ctx context.Context, id string, a action.Ty
return nil, res
}
if h == nil {
res.Error = handlers.ForbiddenError()
res.Error = handlers.NotFoundError()
return nil, res
}
parentId = h.GetCatalogId()
@@ -285,7 +285,7 @@ func (s Service) parentAndAuthResult(ctx context.Context, id string, a action.Ty
return nil, res
}
if cat == nil {
res.Error = handlers.ForbiddenError()
res.Error = handlers.NotFoundError()
return nil, res
}
opts = append(opts, auth.WithScopeId(cat.GetScopeId()), auth.WithPin(parentId))
@@ -73,7 +73,7 @@ func TestGet(t *testing.T) {
name: "Get a non existing Host Set",
req: &pbs.GetHostRequest{Id: static.HostPrefix + "_DoesntExis"},
res: nil,
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Wrong id prefix",
@@ -197,9 +197,6 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteHostRequest{
Id: h.GetPublicId(),
},
res: &pbs.DeleteHostResponse{
Existed: true,
},
errCode: codes.OK,
},
{
@@ -208,7 +205,7 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteHostRequest{
Id: static.HostPrefix + "_doesntexis",
},
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Bad Host Id formatting",
@@ -216,7 +213,6 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteHostRequest{
Id: static.HostPrefix + "_bad_format",
},
res: nil,
errCode: codes.InvalidArgument,
},
}
@@ -252,12 +248,11 @@ func TestDelete_twice(t *testing.T) {
Id: h.GetPublicId(),
}
ctx := auth.DisabledAuthTestContext(auth.WithScopeId(proj.GetPublicId()))
got, gErr := s.DeleteHost(ctx, req)
_, gErr := s.DeleteHost(ctx, req)
assert.NoError(gErr, "First attempt")
assert.True(got.GetExisted(), "Expected existed to be true for the first delete.")
got, gErr = s.DeleteHost(ctx, req)
_, gErr = s.DeleteHost(ctx, req)
assert.Error(gErr, "Second attempt")
assert.Equal(codes.PermissionDenied, status.Code(gErr), "Expected permission denied for the second delete.")
assert.Equal(codes.NotFound, status.Code(gErr), "Expected permission denied for the second delete.")
}
func TestCreate(t *testing.T) {
@@ -678,7 +673,7 @@ func TestUpdate(t *testing.T) {
Description: &wrappers.StringValue{Value: "desc"},
},
},
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Cant change Id",
@@ -127,11 +127,11 @@ func (s Service) DeleteRole(ctx context.Context, req *pbs.DeleteRoleRequest) (*p
if authResults.Error != nil {
return nil, authResults.Error
}
existed, err := s.deleteFromRepo(ctx, req.GetId())
_, err := s.deleteFromRepo(ctx, req.GetId())
if err != nil {
return nil, err
}
return &pbs.DeleteRoleResponse{Existed: existed}, nil
return nil, nil
}
// AddRolePrincipals implements the interface pbs.RoleServiceServer.
@@ -489,7 +489,7 @@ func (s Service) authResult(ctx context.Context, id string, a action.Type) auth.
return res
}
if scp == nil {
res.Error = handlers.ForbiddenError()
res.Error = handlers.NotFoundError()
return res
}
default:
@@ -499,7 +499,7 @@ func (s Service) authResult(ctx context.Context, id string, a action.Type) auth.
return res
}
if r == nil {
res.Error = handlers.ForbiddenError()
res.Error = handlers.NotFoundError()
return res
}
parentId = r.GetScopeId()
@@ -625,7 +625,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()) {
if !handlers.ValidId(scope.Org.Prefix(), req.GetScopeId()) &&
!handlers.ValidId(scope.Project.Prefix(), req.GetScopeId()) &&
req.GetScopeId() != scope.Global.String() {
badFields["scope_id"] = "Improperly formatted field."
}
if len(badFields) > 0 {
@@ -117,7 +117,7 @@ func TestGet(t *testing.T) {
name: "Get a non existant Role",
req: &pbs.GetRoleRequest{Id: iam.RolePrefix + "_DoesntExis"},
res: nil,
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Wrong id prefix",
@@ -143,7 +143,7 @@ func TestGet(t *testing.T) {
scopeId: pr.GetScopeId(),
req: &pbs.GetRoleRequest{Id: iam.RolePrefix + "_DoesntExis"},
res: nil,
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Project Scoped Wrong id prefix",
@@ -273,9 +273,6 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteRoleRequest{
Id: or.GetPublicId(),
},
res: &pbs.DeleteRoleResponse{
Existed: true,
},
errCode: codes.OK,
},
{
@@ -284,7 +281,7 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteRoleRequest{
Id: iam.RolePrefix + "_doesntexis",
},
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Bad Role Id formatting",
@@ -292,7 +289,6 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteRoleRequest{
Id: "bad_format",
},
res: nil,
errCode: codes.InvalidArgument,
},
{
@@ -301,9 +297,6 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteRoleRequest{
Id: pr.GetPublicId(),
},
res: &pbs.DeleteRoleResponse{
Existed: true,
},
errCode: codes.OK,
},
{
@@ -312,7 +305,7 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteRoleRequest{
Id: iam.RolePrefix + "_doesntexis",
},
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
}
for _, tc := range cases {
@@ -336,23 +329,21 @@ func TestDelete_twice(t *testing.T) {
Id: or.GetPublicId(),
}
ctx := auth.DisabledAuthTestContext(auth.WithScopeId(or.GetPublicId()))
got, gErr := s.DeleteRole(ctx, req)
_, gErr := s.DeleteRole(ctx, req)
assert.NoError(gErr, "First attempt")
assert.True(got.GetExisted(), "Expected existed to be true for the first delete.")
got, gErr = s.DeleteRole(ctx, req)
_, gErr = s.DeleteRole(ctx, req)
assert.Error(gErr, "Second attempt")
assert.Equal(codes.PermissionDenied, status.Code(gErr), "Expected permission denied for the second delete.")
assert.Equal(codes.NotFound, status.Code(gErr), "Expected permission denied for the second delete.")
projReq := &pbs.DeleteRoleRequest{
Id: pr.GetPublicId(),
}
ctx = auth.DisabledAuthTestContext(auth.WithScopeId(pr.GetPublicId()))
got, gErr = s.DeleteRole(ctx, projReq)
_, gErr = s.DeleteRole(ctx, projReq)
assert.NoError(gErr, "First attempt")
assert.True(got.GetExisted(), "Expected existed to be true for the first delete.")
got, gErr = s.DeleteRole(ctx, projReq)
_, gErr = s.DeleteRole(ctx, projReq)
assert.Error(gErr, "Second attempt")
assert.Equal(codes.PermissionDenied, status.Code(gErr), "Expected permission denied for the second delete.")
assert.Equal(codes.NotFound, status.Code(gErr), "Expected permission denied for the second delete.")
}
@@ -829,7 +820,7 @@ func TestUpdate(t *testing.T) {
Description: &wrapperspb.StringValue{Value: "desc"},
},
},
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Cant change Id",
@@ -132,11 +132,11 @@ func (s Service) DeleteScope(ctx context.Context, req *pbs.DeleteScopeRequest) (
if authResults.Error != nil {
return nil, authResults.Error
}
existed, err := s.deleteFromRepo(ctx, req.GetId())
_, err := s.deleteFromRepo(ctx, req.GetId())
if err != nil {
return nil, err
}
return &pbs.DeleteScopeResponse{Existed: existed}, nil
return nil, nil
}
func (s Service) getFromRepo(ctx context.Context, id string) (*pb.Scope, error) {
@@ -297,7 +297,7 @@ func (s Service) authResult(ctx context.Context, id string, a action.Type) auth.
return res
}
if s == nil {
res.Error = handlers.ForbiddenError()
res.Error = handlers.NotFoundError()
return res
}
default:
@@ -307,7 +307,7 @@ func (s Service) authResult(ctx context.Context, id string, a action.Type) auth.
return res
}
if s == nil {
res.Error = handlers.ForbiddenError()
res.Error = handlers.NotFoundError()
return res
}
parentId = s.GetParentId()
@@ -103,7 +103,7 @@ func TestGet(t *testing.T) {
scopeId: "global",
req: &pbs.GetScopeRequest{Id: "o_DoesntExis"},
res: nil,
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Get an existing project",
@@ -117,7 +117,7 @@ func TestGet(t *testing.T) {
scopeId: org.GetPublicId(),
req: &pbs.GetScopeRequest{Id: "p_DoesntExis"},
res: nil,
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Wrong id prefix",
@@ -304,9 +304,6 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteScopeRequest{
Id: proj.GetPublicId(),
},
res: &pbs.DeleteScopeResponse{
Existed: true,
},
errCode: codes.OK,
},
{
@@ -315,7 +312,7 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteScopeRequest{
Id: "p_doesntexis",
},
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Bad Project Id formatting",
@@ -323,7 +320,6 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteScopeRequest{
Id: "bad_format",
},
res: nil,
errCode: codes.InvalidArgument,
},
{
@@ -332,9 +328,6 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteScopeRequest{
Id: org.GetPublicId(),
},
res: &pbs.DeleteScopeResponse{
Existed: true,
},
errCode: codes.OK,
},
{
@@ -343,7 +336,7 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteScopeRequest{
Id: "p_doesntexis",
},
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Bad Org Id formatting",
@@ -351,7 +344,6 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteScopeRequest{
Id: "bad_format",
},
res: nil,
errCode: codes.InvalidArgument,
},
}
@@ -376,23 +368,21 @@ func TestDelete_twice(t *testing.T) {
req := &pbs.DeleteScopeRequest{
Id: proj.GetPublicId(),
}
got, gErr := s.DeleteScope(ctx, req)
_, gErr := s.DeleteScope(ctx, req)
assert.NoError(gErr, "First attempt")
assert.True(got.GetExisted(), "Expected existed to be true for the first delete.")
got, gErr = s.DeleteScope(ctx, req)
_, gErr = s.DeleteScope(ctx, req)
assert.Error(gErr, "Second attempt")
assert.Equal(codes.PermissionDenied, status.Code(gErr), "Expected permission denied for the second delete.")
assert.Equal(codes.NotFound, status.Code(gErr), "Expected not found for the second delete.")
ctx = auth.DisabledAuthTestContext(auth.WithScopeId(scope.Global.String()))
req = &pbs.DeleteScopeRequest{
Id: org.GetPublicId(),
}
got, gErr = s.DeleteScope(ctx, req)
_, gErr = s.DeleteScope(ctx, req)
assert.NoError(gErr, "First attempt")
assert.True(got.GetExisted(), "Expected existed to be true for the first delete.")
got, gErr = s.DeleteScope(ctx, req)
_, gErr = s.DeleteScope(ctx, req)
assert.Error(gErr, "Second attempt")
assert.Equal(codes.PermissionDenied, status.Code(gErr), "Expected permission denied for the second delete.")
assert.Equal(codes.NotFound, status.Code(gErr), "Expected not found for the second delete.")
}
func TestCreate(t *testing.T) {
@@ -913,7 +903,7 @@ func TestUpdate(t *testing.T) {
Description: &wrapperspb.StringValue{Value: "desc"},
},
},
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Cant change Id",
@@ -130,11 +130,11 @@ func (s Service) DeleteTarget(ctx context.Context, req *pbs.DeleteTargetRequest)
if authResults.Error != nil {
return nil, authResults.Error
}
existed, err := s.deleteFromRepo(ctx, req.GetId())
_, err := s.deleteFromRepo(ctx, req.GetId())
if err != nil {
return nil, err
}
return &pbs.DeleteTargetResponse{Existed: existed}, nil
return nil, nil
}
// AddTargetHostSets implements the interface pbs.TargetServiceServer.
@@ -368,7 +368,7 @@ func (s Service) authResult(ctx context.Context, id string, a action.Type) auth.
return res
}
if scp == nil {
res.Error = handlers.ForbiddenError()
res.Error = handlers.NotFoundError()
return res
}
default:
@@ -383,7 +383,7 @@ func (s Service) authResult(ctx context.Context, id string, a action.Type) auth.
return res
}
if t == nil {
res.Error = handlers.ForbiddenError()
res.Error = handlers.NotFoundError()
return res
}
parentId = t.GetScopeId()
@@ -79,7 +79,7 @@ func TestGet(t *testing.T) {
name: "Get a non existing Target",
req: &pbs.GetTargetRequest{Id: target.TcpTargetPrefix + "_DoesntExis"},
res: nil,
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Wrong id prefix",
@@ -210,9 +210,6 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteTargetRequest{
Id: tar.GetPublicId(),
},
res: &pbs.DeleteTargetResponse{
Existed: true,
},
errCode: codes.OK,
},
{
@@ -221,8 +218,7 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteTargetRequest{
Id: target.TcpTargetPrefix + "_doesntexis",
},
res: nil,
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Bad target id formatting",
@@ -230,7 +226,6 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteTargetRequest{
Id: target.TcpTargetPrefix + "_bad_format",
},
res: nil,
errCode: codes.InvalidArgument,
},
}
@@ -268,12 +263,11 @@ func TestDelete_twice(t *testing.T) {
Id: tar.GetPublicId(),
}
ctx := auth.DisabledAuthTestContext(auth.WithScopeId(proj.GetPublicId()))
got, gErr := s.DeleteTarget(ctx, req)
_, gErr := s.DeleteTarget(ctx, req)
assert.NoError(gErr, "First attempt")
assert.True(got.GetExisted(), "Expected existed to be true for the first delete.")
_, gErr = s.DeleteTarget(ctx, req)
assert.Error(gErr, "Second attempt")
assert.Equal(codes.PermissionDenied, status.Code(gErr), "Expected permission denied for the second delete.")
assert.Equal(codes.NotFound, status.Code(gErr), "Expected permission denied for the second delete.")
}
func TestCreate(t *testing.T) {
@@ -655,7 +649,7 @@ func TestUpdate(t *testing.T) {
Description: wrapperspb.String("desc"),
},
},
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Cant change Id",
@@ -126,11 +126,11 @@ func (s Service) DeleteUser(ctx context.Context, req *pbs.DeleteUserRequest) (*p
if authResults.Error != nil {
return nil, authResults.Error
}
existed, err := s.deleteFromRepo(ctx, req.GetId())
_, err := s.deleteFromRepo(ctx, req.GetId())
if err != nil {
return nil, err
}
return &pbs.DeleteUserResponse{Existed: existed}, nil
return nil, nil
}
func (s Service) getFromRepo(ctx context.Context, id string) (*pb.User, error) {
@@ -259,7 +259,7 @@ func (s Service) authResult(ctx context.Context, id string, a action.Type) auth.
return res
}
if scp == nil {
res.Error = handlers.ForbiddenError()
res.Error = handlers.NotFoundError()
return res
}
default:
@@ -269,7 +269,7 @@ func (s Service) authResult(ctx context.Context, id string, a action.Type) auth.
return res
}
if u == nil {
res.Error = handlers.ForbiddenError()
res.Error = handlers.NotFoundError()
return res
}
parentId = u.GetScopeId()
@@ -326,7 +326,8 @@ func validateDeleteRequest(req *pbs.DeleteUserRequest) error {
func validateListRequest(req *pbs.ListUsersRequest) error {
badFields := map[string]string{}
if !handlers.ValidId(scope.Org.Prefix(), req.GetScopeId()) {
if !handlers.ValidId(scope.Org.Prefix(), req.GetScopeId()) &&
req.GetScopeId() != scope.Global.String() {
badFields["scope_id"] = "Invalidly formatted required identifer."
}
if len(badFields) > 0 {
@@ -75,7 +75,7 @@ func TestGet(t *testing.T) {
name: "Get a non existant User",
req: &pbs.GetUserRequest{Id: iam.UserPrefix + "_DoesntExis"},
res: nil,
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Wrong id prefix",
@@ -184,9 +184,6 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteUserRequest{
Id: u.GetPublicId(),
},
res: &pbs.DeleteUserResponse{
Existed: true,
},
errCode: codes.OK,
},
{
@@ -194,14 +191,13 @@ func TestDelete(t *testing.T) {
req: &pbs.DeleteUserRequest{
Id: iam.UserPrefix + "_doesntexis",
},
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Bad User Id formatting",
req: &pbs.DeleteUserRequest{
Id: "bad_format",
},
res: nil,
errCode: codes.InvalidArgument,
},
}
@@ -226,12 +222,11 @@ func TestDelete_twice(t *testing.T) {
Id: u.GetPublicId(),
}
ctx := auth.DisabledAuthTestContext(auth.WithScopeId(u.GetScopeId()))
got, gErr := s.DeleteUser(ctx, req)
_, gErr := s.DeleteUser(ctx, req)
assert.NoError(gErr, "First attempt")
assert.True(got.GetExisted(), "Expected existed to be true for the first delete.")
got, gErr = s.DeleteUser(ctx, req)
_, gErr = s.DeleteUser(ctx, req)
assert.Error(gErr, "Second attempt")
assert.Equal(codes.PermissionDenied, status.Code(gErr), "Expected permission denied for the second delete.")
assert.Equal(codes.NotFound, status.Code(gErr), "Expected permission denied for the second delete.")
}
func TestCreate(t *testing.T) {
@@ -526,7 +521,7 @@ func TestUpdate(t *testing.T) {
Description: &wrapperspb.StringValue{Value: "desc"},
},
},
errCode: codes.PermissionDenied,
errCode: codes.NotFound,
},
{
name: "Cant change Id",