API Errors: Hide and log internal errors (#411)
This commit is contained in:
1 parent
ac9a7735e1
commit
2f8d7f0a32
27 files changed
+1465
-1205
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package authmethods_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -15,11 +16,11 @@ import (
|
||||
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
|
||||
"github.com/hashicorp/boundary/internal/iam"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers/authmethods"
|
||||
"github.com/hashicorp/boundary/internal/types/scope"
|
||||
"google.golang.org/genproto/protobuf/field_mask"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/protobuf/testing/protocmp"
|
||||
"google.golang.org/protobuf/types/known/structpb"
|
||||
"google.golang.org/protobuf/types/known/wrapperspb"
|
||||
@@ -66,35 +67,34 @@ func TestGet(t *testing.T) {
|
||||
scopeId string
|
||||
req *pbs.GetAuthMethodRequest
|
||||
res *pbs.GetAuthMethodResponse
|
||||
errCode codes.Code
|
||||
err error
|
||||
}{
|
||||
{
|
||||
name: "Get an Existing AuthMethod",
|
||||
scopeId: o.GetPublicId(),
|
||||
req: &pbs.GetAuthMethodRequest{Id: am.GetPublicId()},
|
||||
res: &pbs.GetAuthMethodResponse{Item: wantU},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Get a non existant AuthMethod",
|
||||
scopeId: o.GetPublicId(),
|
||||
req: &pbs.GetAuthMethodRequest{Id: password.AuthMethodPrefix + "_DoesntExis"},
|
||||
res: nil,
|
||||
errCode: codes.NotFound,
|
||||
err: handlers.ApiErrorWithCode(codes.NotFound),
|
||||
},
|
||||
{
|
||||
name: "Wrong id prefix",
|
||||
scopeId: o.GetPublicId(),
|
||||
req: &pbs.GetAuthMethodRequest{Id: "j_1234567890"},
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "space in id",
|
||||
scopeId: o.GetPublicId(),
|
||||
req: &pbs.GetAuthMethodRequest{Id: password.AuthMethodPrefix + "_1 23456789"},
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
@@ -105,7 +105,10 @@ func TestGet(t *testing.T) {
|
||||
require.NoError(err, "Couldn't create new auth_method service.")
|
||||
|
||||
got, gErr := s.GetAuthMethod(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), tc.req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "GetAuthMethod(%+v) got error %v, wanted %v", tc.req, gErr, tc.errCode)
|
||||
if tc.err != nil {
|
||||
require.Error(gErr)
|
||||
assert.True(errors.Is(gErr, tc.err), "GetAuthMethod(%+v) got error %v, wanted %v", tc.req, gErr, tc.err)
|
||||
}
|
||||
assert.Empty(cmp.Diff(got, tc.res, protocmp.Transform()), "GetAuthMethod(%q) got response %q, wanted %q", tc.req, got, tc.res)
|
||||
})
|
||||
}
|
||||
@@ -166,30 +169,27 @@ func TestList(t *testing.T) {
|
||||
name string
|
||||
scopeId string
|
||||
res *pbs.ListAuthMethodsResponse
|
||||
errCode codes.Code
|
||||
err error
|
||||
}{
|
||||
{
|
||||
name: "List Some Auth Methods",
|
||||
scopeId: oWithAuthMethods.GetPublicId(),
|
||||
res: &pbs.ListAuthMethodsResponse{Items: wantSomeAuthMethods},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "List Other Auth Methods",
|
||||
scopeId: oWithOtherAuthMethods.GetPublicId(),
|
||||
res: &pbs.ListAuthMethodsResponse{Items: wantOtherAuthMethods},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "List No Auth Methods",
|
||||
scopeId: oNoAuthMethods.GetPublicId(),
|
||||
res: &pbs.ListAuthMethodsResponse{},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Unfound Auth Method",
|
||||
scopeId: "o_DoesntExis",
|
||||
errCode: codes.NotFound,
|
||||
err: handlers.ApiErrorWithCode(codes.NotFound),
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
@@ -199,7 +199,10 @@ func TestList(t *testing.T) {
|
||||
require.NoError(err, "Couldn't create new auth_method service.")
|
||||
|
||||
got, gErr := s.ListAuthMethods(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), &pbs.ListAuthMethodsRequest{ScopeId: tc.scopeId})
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "ListAuthMethods() for scope %q got error %v, wanted %v", tc.scopeId, gErr, tc.errCode)
|
||||
if tc.err != nil {
|
||||
require.Error(gErr)
|
||||
assert.True(errors.Is(gErr, tc.err), "ListAuthMethods() for scope %q got error %v, wanted %v", tc.scopeId, gErr, tc.err)
|
||||
}
|
||||
assert.Empty(cmp.Diff(got, tc.res, protocmp.Transform()), "ListAuthMethods() for scope %q got response %q, wanted %q", tc.scopeId, got, tc.res)
|
||||
})
|
||||
}
|
||||
@@ -225,39 +228,41 @@ func TestDelete(t *testing.T) {
|
||||
require.NoError(t, err, "Error when getting new auth_method service.")
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
req *pbs.DeleteAuthMethodRequest
|
||||
res *pbs.DeleteAuthMethodResponse
|
||||
errCode codes.Code
|
||||
name string
|
||||
req *pbs.DeleteAuthMethodRequest
|
||||
res *pbs.DeleteAuthMethodResponse
|
||||
err error
|
||||
}{
|
||||
{
|
||||
name: "Delete an Existing AuthMethod",
|
||||
req: &pbs.DeleteAuthMethodRequest{
|
||||
Id: am.GetPublicId(),
|
||||
},
|
||||
res: &pbs.DeleteAuthMethodResponse{},
|
||||
errCode: codes.OK,
|
||||
res: &pbs.DeleteAuthMethodResponse{},
|
||||
},
|
||||
{
|
||||
name: "Delete bad auth_method id",
|
||||
req: &pbs.DeleteAuthMethodRequest{
|
||||
Id: password.AuthMethodPrefix + "_doesntexis",
|
||||
},
|
||||
errCode: codes.NotFound,
|
||||
err: handlers.ApiErrorWithCode(codes.NotFound),
|
||||
},
|
||||
{
|
||||
name: "Bad AuthMethod Id formatting",
|
||||
req: &pbs.DeleteAuthMethodRequest{
|
||||
Id: "bad_format",
|
||||
},
|
||||
errCode: codes.InvalidArgument,
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
got, gErr := s.DeleteAuthMethod(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), tc.req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "DeleteAuthMethod(%+v) got error %v, wanted %v", tc.req, gErr, tc.errCode)
|
||||
if tc.err != nil {
|
||||
require.Error(gErr)
|
||||
assert.True(errors.Is(gErr, tc.err), "DeleteAuthMethod(%+v) got error %v, wanted %v", tc.req, gErr, tc.err)
|
||||
}
|
||||
assert.EqualValuesf(tc.res, got, "DeleteAuthMethod(%q) got response %q, wanted %q", tc.req, got, tc.res)
|
||||
})
|
||||
}
|
||||
@@ -290,7 +295,7 @@ func TestDelete_twice(t *testing.T) {
|
||||
assert.NoError(gErr, "First attempt")
|
||||
_, gErr = s.DeleteAuthMethod(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), req)
|
||||
assert.Error(gErr, "Second attempt")
|
||||
assert.Equal(codes.NotFound, status.Code(gErr), "Expected permission denied for the second delete.")
|
||||
assert.True(errors.Is(gErr, handlers.ApiErrorWithCode(codes.NotFound)), "Expected permission denied for the second delete.")
|
||||
}
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
@@ -312,10 +317,10 @@ func TestCreate(t *testing.T) {
|
||||
require.NoError(t, err, "Error converting proto to timestamp.")
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
req *pbs.CreateAuthMethodRequest
|
||||
res *pbs.CreateAuthMethodResponse
|
||||
errCode codes.Code
|
||||
name string
|
||||
req *pbs.CreateAuthMethodRequest
|
||||
res *pbs.CreateAuthMethodResponse
|
||||
err error
|
||||
}{
|
||||
{
|
||||
name: "Create a valid AuthMethod",
|
||||
@@ -343,7 +348,6 @@ func TestCreate(t *testing.T) {
|
||||
}},
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Create a global AuthMethod",
|
||||
@@ -371,7 +375,6 @@ func TestCreate(t *testing.T) {
|
||||
}},
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Can't specify Id",
|
||||
@@ -380,8 +383,8 @@ func TestCreate(t *testing.T) {
|
||||
Id: password.AuthMethodPrefix + "_notallowed",
|
||||
Type: "password",
|
||||
}},
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
res: nil,
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "Can't specify Created Time",
|
||||
@@ -389,8 +392,8 @@ func TestCreate(t *testing.T) {
|
||||
ScopeId: o.GetPublicId(),
|
||||
CreatedTime: ptypes.TimestampNow(),
|
||||
}},
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
res: nil,
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "Can't specify Update Time",
|
||||
@@ -399,8 +402,8 @@ func TestCreate(t *testing.T) {
|
||||
UpdatedTime: ptypes.TimestampNow(),
|
||||
Type: "password",
|
||||
}},
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
res: nil,
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "Can't specify Update Time",
|
||||
@@ -409,8 +412,8 @@ func TestCreate(t *testing.T) {
|
||||
UpdatedTime: ptypes.TimestampNow(),
|
||||
Type: "password",
|
||||
}},
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
res: nil,
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "Must specify type",
|
||||
@@ -419,8 +422,8 @@ func TestCreate(t *testing.T) {
|
||||
Name: &wrapperspb.StringValue{Value: "must specify type"},
|
||||
Description: &wrapperspb.StringValue{Value: "must specify type"},
|
||||
}},
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
res: nil,
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "Attributes must be valid for type",
|
||||
@@ -434,8 +437,8 @@ func TestCreate(t *testing.T) {
|
||||
"min_login_name_length": structpb.NewNumberValue(3),
|
||||
}},
|
||||
}},
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
res: nil,
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
@@ -446,7 +449,10 @@ func TestCreate(t *testing.T) {
|
||||
require.NoError(err, "Error when getting new auth_method service.")
|
||||
|
||||
got, gErr := s.CreateAuthMethod(auth.DisabledAuthTestContext(auth.WithScopeId(tc.req.GetItem().GetScopeId())), tc.req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "CreateAuthMethod(%+v) got error %v, wanted %v", tc.req, gErr, tc.errCode)
|
||||
if tc.err != nil {
|
||||
require.Error(gErr)
|
||||
assert.True(errors.Is(gErr, tc.err), "CreateAuthMethod(%+v) got error %v, wanted %v", tc.req, gErr, tc.err)
|
||||
}
|
||||
if tc.res == nil {
|
||||
require.Nil(got)
|
||||
}
|
||||
@@ -510,10 +516,10 @@ func TestUpdate(t *testing.T) {
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
req *pbs.UpdateAuthMethodRequest
|
||||
res *pbs.UpdateAuthMethodResponse
|
||||
errCode codes.Code
|
||||
name string
|
||||
req *pbs.UpdateAuthMethodRequest
|
||||
res *pbs.UpdateAuthMethodResponse
|
||||
err error
|
||||
}{
|
||||
{
|
||||
name: "Update an Existing AuthMethod",
|
||||
@@ -540,7 +546,6 @@ func TestUpdate(t *testing.T) {
|
||||
Scope: defaultScopeInfo,
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Multiple Paths in single string",
|
||||
@@ -567,7 +572,6 @@ func TestUpdate(t *testing.T) {
|
||||
Scope: defaultScopeInfo,
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "No Update Mask",
|
||||
@@ -577,7 +581,7 @@ func TestUpdate(t *testing.T) {
|
||||
Description: &wrapperspb.StringValue{Value: "updated desc"},
|
||||
},
|
||||
},
|
||||
errCode: codes.InvalidArgument,
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "No Paths in Mask",
|
||||
@@ -588,7 +592,7 @@ func TestUpdate(t *testing.T) {
|
||||
Description: &wrapperspb.StringValue{Value: "updated desc"},
|
||||
},
|
||||
},
|
||||
errCode: codes.InvalidArgument,
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "Only non-existant paths in Mask",
|
||||
@@ -599,7 +603,7 @@ func TestUpdate(t *testing.T) {
|
||||
Description: &wrapperspb.StringValue{Value: "updated desc"},
|
||||
},
|
||||
},
|
||||
errCode: codes.InvalidArgument,
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "Cant change type",
|
||||
@@ -610,7 +614,7 @@ func TestUpdate(t *testing.T) {
|
||||
Type: "oidc",
|
||||
},
|
||||
},
|
||||
errCode: codes.InvalidArgument,
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "Unset Name",
|
||||
@@ -634,7 +638,6 @@ func TestUpdate(t *testing.T) {
|
||||
Scope: defaultScopeInfo,
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Update Only Name",
|
||||
@@ -660,7 +663,6 @@ func TestUpdate(t *testing.T) {
|
||||
Scope: defaultScopeInfo,
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Update Only Description",
|
||||
@@ -686,7 +688,6 @@ func TestUpdate(t *testing.T) {
|
||||
Scope: defaultScopeInfo,
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Update a Non Existing AuthMethod",
|
||||
@@ -700,7 +701,7 @@ func TestUpdate(t *testing.T) {
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
},
|
||||
},
|
||||
errCode: codes.NotFound,
|
||||
err: handlers.ApiErrorWithCode(codes.NotFound),
|
||||
},
|
||||
{
|
||||
name: "Cant change Id",
|
||||
@@ -713,8 +714,8 @@ func TestUpdate(t *testing.T) {
|
||||
Name: &wrapperspb.StringValue{Value: "new"},
|
||||
Description: &wrapperspb.StringValue{Value: "new desc"},
|
||||
}},
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
res: nil,
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "Cant specify Created Time",
|
||||
@@ -726,8 +727,8 @@ func TestUpdate(t *testing.T) {
|
||||
CreatedTime: ptypes.TimestampNow(),
|
||||
},
|
||||
},
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
res: nil,
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "Cant specify Updated Time",
|
||||
@@ -739,8 +740,8 @@ func TestUpdate(t *testing.T) {
|
||||
UpdatedTime: ptypes.TimestampNow(),
|
||||
},
|
||||
},
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
res: nil,
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "Cant specify Type",
|
||||
@@ -752,8 +753,8 @@ func TestUpdate(t *testing.T) {
|
||||
Type: "oidc",
|
||||
},
|
||||
},
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
res: nil,
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "Update login name length",
|
||||
@@ -783,7 +784,6 @@ func TestUpdate(t *testing.T) {
|
||||
Scope: defaultScopeInfo,
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Update password length",
|
||||
@@ -813,7 +813,6 @@ func TestUpdate(t *testing.T) {
|
||||
Scope: defaultScopeInfo,
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
@@ -834,7 +833,10 @@ func TestUpdate(t *testing.T) {
|
||||
}
|
||||
|
||||
got, gErr := tested.UpdateAuthMethod(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), tc.req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "UpdateAuthMethod(%+v) got error %v, wanted %v", tc.req, gErr, tc.errCode)
|
||||
if tc.err != nil {
|
||||
require.Error(gErr)
|
||||
assert.True(errors.Is(gErr, tc.err), "UpdateAuthMethod(%+v) got error %v, wanted %v", tc.req, gErr, tc.err)
|
||||
}
|
||||
|
||||
if tc.res == nil {
|
||||
require.Nil(got)
|
||||
|
||||
Reference in new issue
Block a user