refact(credentiallibrary): Switch subtype attributes to oneof

This updates the credential library resources to use a oneof for
attributes. This allows requests to the credential library service to
leverage the new subtypes.AttributeTransformerInterceptor so that
attributes are strongly typed prior to the service handler.
This commit is contained in:
Timothy Messier authored and Johan Brandhorst-Satzkorn committed 2022-04-20 13:32:32 -04:00
1 parent 95315a3e0a
commit 3d4ba0389e
12 files changed
+542 -529

No files matched your search

@@ -18,6 +18,7 @@ import (
"github.com/hashicorp/boundary/internal/servers/controller/handlers"
"github.com/hashicorp/boundary/internal/types/action"
"github.com/hashicorp/boundary/internal/types/resource"
"github.com/hashicorp/boundary/internal/types/subtypes"
pb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/credentiallibraries"
"github.com/hashicorp/go-secure-stdlib/strutil"
"google.golang.org/grpc/codes"
@@ -27,10 +28,12 @@ import (
)
const (
attributesPathField = "attributes"
vaultPathField = "attributes.path"
httpMethodField = "attributes.http_method"
httpRequestBodyField = "attributes.http_request_body"
credentialMappingPathField = "credential_mapping_overrides"
domain = "credential"
)
// Credential mapping override attributes
@@ -420,7 +423,7 @@ func (s Service) authResult(ctx context.Context, id string, a action.Type) auth.
default:
opts = append(opts, auth.WithId(id))
switch credential.SubtypeFromId(id) {
switch subtypes.SubtypeFromId(domain, id) {
case vault.Subtype:
cl, err := repo.LookupCredentialLibrary(ctx, id)
if err != nil {
@@ -445,7 +448,7 @@ func (s Service) authResult(ctx context.Context, id string, a action.Type) auth.
opts = append(opts, auth.WithPin(parentId))
switch credential.SubtypeFromId(parentId) {
switch subtypes.SubtypeFromId(domain, parentId) {
case vault.Subtype:
cs, err := repo.LookupCredentialStore(ctx, parentId)
if err != nil {
@@ -482,7 +485,7 @@ func toProto(in credential.Library, opt ...handlers.Option) (*pb.CredentialLibra
out.CredentialStoreId = in.GetStoreId()
}
if outputFields.Has(globals.TypeField) {
out.Type = credential.SubtypeFromId(in.GetPublicId()).String()
out.Type = subtypes.SubtypeFromId(domain, in.GetPublicId()).String()
}
if outputFields.Has(globals.DescriptionField) && in.GetDescription() != "" {
out.Description = wrapperspb.String(in.GetDescription())
@@ -505,7 +508,7 @@ func toProto(in credential.Library, opt ...handlers.Option) (*pb.CredentialLibra
if outputFields.Has(globals.AuthorizedActionsField) {
out.AuthorizedActions = opts.WithAuthorizedActions
}
switch credential.SubtypeFromId(in.GetPublicId()) {
switch subtypes.SubtypeFromId(domain, in.GetPublicId()) {
case vault.Subtype:
vaultIn, ok := in.(*vault.CredentialLibrary)
if !ok {
@@ -544,10 +547,8 @@ func toProto(in credential.Library, opt ...handlers.Option) (*pb.CredentialLibra
if vaultIn.GetHttpRequestBody() != nil {
attrs.HttpRequestBody = wrapperspb.String(string(vaultIn.GetHttpRequestBody()))
}
var err error
out.Attributes, err = handlers.ProtoToStruct(attrs)
if err != nil {
return nil, errors.WrapDeprecated(err, op, errors.WithMsg("failed to convert resource from storage to api"))
out.Attrs = &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: attrs,
}
}
}
@@ -564,11 +565,7 @@ func toStorageVaultLibrary(storeId string, in *pb.CredentialLibrary) (out *vault
opts = append(opts, vault.WithDescription(in.GetDescription().GetValue()))
}
attrs := &pb.VaultCredentialLibraryAttributes{}
if err := handlers.StructToProto(in.GetAttributes(), attrs); err != nil {
return nil, errors.WrapDeprecated(err, op, errors.WithMsg("unable to parse the attributes"))
}
attrs := in.GetVaultCredentialLibraryAttributes()
if attrs.GetHttpMethod() != nil {
opts = append(opts, vault.WithMethod(vault.Method(strings.ToUpper(attrs.GetHttpMethod().GetValue()))))
}
@@ -612,15 +609,14 @@ func validateGetRequest(req *pbs.GetCredentialLibraryRequest) error {
func validateCreateRequest(req *pbs.CreateCredentialLibraryRequest) error {
return handlers.ValidateCreateRequest(req.GetItem(), func() map[string]string {
badFields := map[string]string{}
switch credential.SubtypeFromId(req.GetItem().GetCredentialStoreId()) {
switch subtypes.SubtypeFromId(domain, req.GetItem().GetCredentialStoreId()) {
case vault.Subtype:
if t := req.GetItem().GetType(); t != "" && credential.SubtypeFromType(t) != vault.Subtype {
if t := req.GetItem().GetType(); t != "" && subtypes.SubtypeFromType(domain, t) != vault.Subtype {
badFields[globals.CredentialStoreIdField] = "If included, type must match that of the credential store."
}
attrs := &pb.VaultCredentialLibraryAttributes{}
if err := handlers.StructToProto(req.GetItem().GetAttributes(), attrs); err != nil {
badFields[globals.AttributesField] = "Attribute fields do not match the expected format."
break
attrs := req.GetItem().GetVaultCredentialLibraryAttributes()
if attrs == nil {
badFields[attributesPathField] = "This is a required field."
}
if attrs.GetPath().GetValue() == "" {
badFields[vaultPathField] = "This is a required field."
@@ -642,29 +638,27 @@ func validateCreateRequest(req *pbs.CreateCredentialLibraryRequest) error {
func validateUpdateRequest(req *pbs.UpdateCredentialLibraryRequest, currentCredentialType credential.Type) error {
return handlers.ValidateUpdateRequest(req, req.GetItem(), func() map[string]string {
badFields := map[string]string{}
switch credential.SubtypeFromId(req.GetId()) {
switch subtypes.SubtypeFromId(domain, req.GetId()) {
case vault.Subtype:
if req.GetItem().GetType() != "" && credential.SubtypeFromType(req.GetItem().GetType()) != vault.Subtype {
if req.GetItem().GetType() != "" && subtypes.SubtypeFromType(domain, req.GetItem().GetType()) != vault.Subtype {
badFields[globals.TypeField] = "Cannot modify resource type."
}
if req.GetItem().GetCredentialType() != "" && req.GetItem().GetCredentialType() != string(currentCredentialType) {
badFields[globals.CredentialTypeField] = "Cannot modify credential type."
}
attrs := &pb.VaultCredentialLibraryAttributes{}
if err := handlers.StructToProto(req.GetItem().GetAttributes(), attrs); err != nil {
badFields[globals.AttributesField] = "Attribute fields do not match the expected format."
break
attrs := req.GetItem().GetVaultCredentialLibraryAttributes()
if attrs != nil {
if handlers.MaskContains(req.GetUpdateMask().GetPaths(), vaultPathField) && attrs.GetPath().GetValue() == "" {
badFields[vaultPathField] = "This is a required field and cannot be set to empty."
}
if m := attrs.GetHttpMethod(); handlers.MaskContains(req.GetUpdateMask().GetPaths(), httpMethodField) && m != nil && !strutil.StrListContains([]string{"GET", "POST"}, strings.ToUpper(m.GetValue())) {
badFields[httpMethodField] = "If set, value must be 'GET' or 'POST'."
}
if b := attrs.GetHttpRequestBody(); b != nil && strings.ToUpper(attrs.GetHttpMethod().GetValue()) == "GET" {
badFields[httpRequestBodyField] = fmt.Sprintf("Field can only be set if %q is set to the value 'POST'.", httpMethodField)
}
validateMapping(badFields, currentCredentialType, req.GetItem().CredentialMappingOverrides.AsMap())
}
if handlers.MaskContains(req.GetUpdateMask().GetPaths(), vaultPathField) && attrs.GetPath().GetValue() == "" {
badFields[vaultPathField] = "This is a required field and cannot be set to empty."
}
if m := attrs.GetHttpMethod(); handlers.MaskContains(req.GetUpdateMask().GetPaths(), httpMethodField) && m != nil && !strutil.StrListContains([]string{"GET", "POST"}, strings.ToUpper(m.GetValue())) {
badFields[httpMethodField] = "If set, value must be 'GET' or 'POST'."
}
if b := attrs.GetHttpRequestBody(); b != nil && strings.ToUpper(attrs.GetHttpMethod().GetValue()) == "GET" {
badFields[httpRequestBodyField] = fmt.Sprintf("Field can only be set if %q is set to the value 'POST'.", httpMethodField)
}
validateMapping(badFields, currentCredentialType, req.GetItem().CredentialMappingOverrides.AsMap())
}
return badFields
}, vault.CredentialLibraryPrefix)
@@ -66,42 +66,51 @@ func TestList(t *testing.T) {
Version: l.GetVersion(),
Type: vault.Subtype.String(),
AuthorizedActions: testAuthorizedActions,
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String(l.GetVaultPath()),
HttpMethod: wrapperspb.String(l.GetHttpMethod()),
})
require.NoError(t, err)
return attrs
}(),
},
},
})
}
cases := []struct {
name string
req *pbs.ListCredentialLibrariesRequest
res *pbs.ListCredentialLibrariesResponse
err error
name string
req *pbs.ListCredentialLibrariesRequest
res *pbs.ListCredentialLibrariesResponse
anonRes *pbs.ListCredentialLibrariesResponse
err error
}{
{
name: "List Many Libraries",
req: &pbs.ListCredentialLibrariesRequest{CredentialStoreId: store.GetPublicId()},
res: &pbs.ListCredentialLibrariesResponse{Items: wantLibraries},
name: "List Many Libraries",
req: &pbs.ListCredentialLibrariesRequest{CredentialStoreId: store.GetPublicId()},
res: &pbs.ListCredentialLibrariesResponse{Items: wantLibraries},
anonRes: &pbs.ListCredentialLibrariesResponse{Items: wantLibraries},
},
{
name: "List No Libraries",
req: &pbs.ListCredentialLibrariesRequest{CredentialStoreId: storeNoLibs.GetPublicId()},
res: &pbs.ListCredentialLibrariesResponse{},
name: "List No Libraries",
req: &pbs.ListCredentialLibrariesRequest{CredentialStoreId: storeNoLibs.GetPublicId()},
res: &pbs.ListCredentialLibrariesResponse{},
anonRes: &pbs.ListCredentialLibrariesResponse{},
},
{
name: "Filter to One Libraries",
req: &pbs.ListCredentialLibrariesRequest{CredentialStoreId: store.GetPublicId(), Filter: fmt.Sprintf(`"/item/id"==%q`, wantLibraries[1].GetId())},
res: &pbs.ListCredentialLibrariesResponse{Items: wantLibraries[1:2]},
name: "Filter to One Libraries",
req: &pbs.ListCredentialLibrariesRequest{CredentialStoreId: store.GetPublicId(), Filter: fmt.Sprintf(`"/item/id"==%q`, wantLibraries[1].GetId())},
res: &pbs.ListCredentialLibrariesResponse{Items: wantLibraries[1:2]},
anonRes: &pbs.ListCredentialLibrariesResponse{Items: wantLibraries[1:2]},
},
{
name: "Filter to No Libraries",
req: &pbs.ListCredentialLibrariesRequest{CredentialStoreId: store.GetPublicId(), Filter: `"/item/id"=="doesnt match"`},
res: &pbs.ListCredentialLibrariesResponse{},
name: "Filter on Attribute",
req: &pbs.ListCredentialLibrariesRequest{CredentialStoreId: store.GetPublicId(), Filter: fmt.Sprintf(`"/item/attributes/path"==%q`, wantLibraries[2].GetVaultCredentialLibraryAttributes().GetPath().Value)},
res: &pbs.ListCredentialLibrariesResponse{Items: wantLibraries[2:3]},
anonRes: &pbs.ListCredentialLibrariesResponse{}, // anonymous user does not have access to attributes
},
{
name: "Filter to No Libraries",
req: &pbs.ListCredentialLibrariesRequest{CredentialStoreId: store.GetPublicId(), Filter: `"/item/id"=="doesnt match"`},
res: &pbs.ListCredentialLibrariesResponse{},
anonRes: &pbs.ListCredentialLibrariesResponse{},
},
{
name: "Filter Bad Format",
@@ -134,7 +143,7 @@ func TestList(t *testing.T) {
// Test anonymous listing
got, gErr = s.ListCredentialLibraries(auth.DisabledAuthTestContext(iamRepoFn, prj.GetPublicId(), auth.WithUserId(auth.AnonymousUserId)), tc.req)
require.NoError(t, gErr)
assert.Len(t, got.Items, len(tc.res.Items))
assert.Len(t, got.Items, len(tc.anonRes.Items))
for _, item := range got.GetItems() {
require.Nil(t, item.CreatedTime)
require.Nil(t, item.UpdatedTime)
@@ -176,11 +185,9 @@ func TestCreate(t *testing.T) {
name: "missing vault path",
req: &pbs.CreateCredentialLibraryRequest{Item: &pb.CredentialLibrary{
CredentialStoreId: store.GetPublicId(),
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{})
require.NoError(t, err)
return attrs
}(),
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{},
},
}},
idPrefix: vault.CredentialLibraryPrefix + "_",
wantErr: true,
@@ -190,13 +197,11 @@ func TestCreate(t *testing.T) {
req: &pbs.CreateCredentialLibraryRequest{Item: &pb.CredentialLibrary{
CredentialStoreId: store.GetPublicId(),
Id: vault.CredentialLibraryPrefix + "_notallowed",
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String("something"),
})
require.NoError(t, err)
return attrs
}(),
},
},
}},
res: nil,
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
@@ -206,13 +211,11 @@ func TestCreate(t *testing.T) {
req: &pbs.CreateCredentialLibraryRequest{Item: &pb.CredentialLibrary{
CredentialStoreId: store.GetPublicId(),
CreatedTime: timestamppb.Now(),
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String("something"),
})
require.NoError(t, err)
return attrs
}(),
},
},
}},
res: nil,
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
@@ -222,13 +225,11 @@ func TestCreate(t *testing.T) {
req: &pbs.CreateCredentialLibraryRequest{Item: &pb.CredentialLibrary{
CredentialStoreId: store.GetPublicId(),
UpdatedTime: timestamppb.Now(),
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String("something"),
})
require.NoError(t, err)
return attrs
}(),
},
},
}},
res: nil,
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
@@ -238,13 +239,11 @@ func TestCreate(t *testing.T) {
req: &pbs.CreateCredentialLibraryRequest{Item: &pb.CredentialLibrary{
CredentialStoreId: store.GetPublicId(),
Type: "static",
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String("something"),
})
require.NoError(t, err)
return attrs
}(),
},
},
}},
res: nil,
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
@@ -253,29 +252,11 @@ func TestCreate(t *testing.T) {
name: "parent id must be included",
req: &pbs.CreateCredentialLibraryRequest{Item: &pb.CredentialLibrary{
Type: vault.Subtype.String(),
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String("something"),
})
require.NoError(t, err)
return attrs
}(),
}},
res: nil,
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
},
{
name: "Attributes must be valid for vault type",
req: &pbs.CreateCredentialLibraryRequest{Item: &pb.CredentialLibrary{
CredentialStoreId: store.GetPublicId(),
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String("something"),
})
require.NoError(t, err)
attrs.Fields["invalid"] = structpb.NewStringValue("foo")
return attrs
}(),
},
},
}},
res: nil,
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
@@ -284,14 +265,12 @@ func TestCreate(t *testing.T) {
name: "Cannot specify a http request body when http method is get",
req: &pbs.CreateCredentialLibraryRequest{Item: &pb.CredentialLibrary{
CredentialStoreId: store.GetPublicId(),
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String("something"),
HttpRequestBody: wrapperspb.String("foo"),
})
require.NoError(t, err)
return attrs
}(),
},
},
}},
res: nil,
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
@@ -300,14 +279,12 @@ func TestCreate(t *testing.T) {
name: "Invalid Method",
req: &pbs.CreateCredentialLibraryRequest{Item: &pb.CredentialLibrary{
CredentialStoreId: store.GetPublicId(),
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String("something"),
HttpMethod: wrapperspb.String("PATCH"),
})
require.NoError(t, err)
return attrs
}(),
},
},
}},
res: nil,
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
@@ -316,14 +293,12 @@ func TestCreate(t *testing.T) {
name: "Request Body With GET Method",
req: &pbs.CreateCredentialLibraryRequest{Item: &pb.CredentialLibrary{
CredentialStoreId: store.GetPublicId(),
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String("something"),
HttpRequestBody: wrapperspb.String("foo"),
})
require.NoError(t, err)
return attrs
}(),
},
},
}},
res: nil,
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
@@ -332,13 +307,11 @@ func TestCreate(t *testing.T) {
name: "Invalid credential type",
req: &pbs.CreateCredentialLibraryRequest{Item: &pb.CredentialLibrary{
CredentialStoreId: store.GetPublicId(),
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String("something"),
})
require.NoError(t, err)
return attrs
}(),
},
},
CredentialType: "fake-type",
}},
res: nil,
@@ -348,13 +321,11 @@ func TestCreate(t *testing.T) {
name: "Invalid user_password mapping",
req: &pbs.CreateCredentialLibraryRequest{Item: &pb.CredentialLibrary{
CredentialStoreId: store.GetPublicId(),
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String("something"),
})
require.NoError(t, err)
return attrs
}(),
},
},
CredentialType: string(credential.UserPasswordType),
CredentialMappingOverrides: func() *structpb.Struct {
v := map[string]interface{}{
@@ -373,15 +344,13 @@ func TestCreate(t *testing.T) {
name: "Using POST method",
req: &pbs.CreateCredentialLibraryRequest{Item: &pb.CredentialLibrary{
CredentialStoreId: store.GetPublicId(),
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String("something"),
HttpMethod: wrapperspb.String("post"),
HttpRequestBody: wrapperspb.String("foo"),
})
require.NoError(t, err)
return attrs
}(),
},
},
}},
idPrefix: vault.CredentialLibraryPrefix + "_",
res: &pbs.CreateCredentialLibraryResponse{
@@ -394,15 +363,13 @@ func TestCreate(t *testing.T) {
Scope: &scopepb.ScopeInfo{Id: prj.GetPublicId(), Type: prj.GetType(), ParentScopeId: prj.GetParentId()},
Version: 1,
Type: vault.Subtype.String(),
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String("something"),
HttpMethod: wrapperspb.String("POST"),
HttpRequestBody: wrapperspb.String("foo"),
})
require.NoError(t, err)
return attrs
}(),
},
},
AuthorizedActions: testAuthorizedActions,
},
},
@@ -413,13 +380,11 @@ func TestCreate(t *testing.T) {
CredentialStoreId: store.GetPublicId(),
Name: &wrapperspb.StringValue{Value: "name"},
Description: &wrapperspb.StringValue{Value: "desc"},
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String("something"),
})
require.NoError(t, err)
return attrs
}(),
},
},
}},
idPrefix: vault.CredentialLibraryPrefix + "_",
res: &pbs.CreateCredentialLibraryResponse{
@@ -434,14 +399,12 @@ func TestCreate(t *testing.T) {
Scope: &scopepb.ScopeInfo{Id: prj.GetPublicId(), Type: prj.GetType(), ParentScopeId: prj.GetParentId()},
Version: 1,
Type: vault.Subtype.String(),
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String("something"),
HttpMethod: wrapperspb.String("GET"),
})
require.NoError(t, err)
return attrs
}(),
},
},
AuthorizedActions: testAuthorizedActions,
},
},
@@ -450,13 +413,11 @@ func TestCreate(t *testing.T) {
name: "Create a valid vault CredentialLibrary user_password type",
req: &pbs.CreateCredentialLibraryRequest{Item: &pb.CredentialLibrary{
CredentialStoreId: store.GetPublicId(),
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String("something"),
})
require.NoError(t, err)
return attrs
}(),
},
},
CredentialType: string(credential.UserPasswordType),
}},
idPrefix: vault.CredentialLibraryPrefix + "_",
@@ -470,14 +431,12 @@ func TestCreate(t *testing.T) {
Scope: &scopepb.ScopeInfo{Id: prj.GetPublicId(), Type: prj.GetType(), ParentScopeId: prj.GetParentId()},
Version: 1,
Type: vault.Subtype.String(),
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String("something"),
HttpMethod: wrapperspb.String("GET"),
})
require.NoError(t, err)
return attrs
}(),
},
},
CredentialType: string(credential.UserPasswordType),
AuthorizedActions: testAuthorizedActions,
},
@@ -487,13 +446,11 @@ func TestCreate(t *testing.T) {
name: "Create a valid vault CredentialLibrary user_password type with username mapping",
req: &pbs.CreateCredentialLibraryRequest{Item: &pb.CredentialLibrary{
CredentialStoreId: store.GetPublicId(),
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String("something"),
})
require.NoError(t, err)
return attrs
}(),
},
},
CredentialMappingOverrides: func() *structpb.Struct {
v := map[string]interface{}{
usernameAttribute: "user-test",
@@ -515,14 +472,12 @@ func TestCreate(t *testing.T) {
Scope: &scopepb.ScopeInfo{Id: prj.GetPublicId(), Type: prj.GetType(), ParentScopeId: prj.GetParentId()},
Version: 1,
Type: vault.Subtype.String(),
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String("something"),
HttpMethod: wrapperspb.String("GET"),
})
require.NoError(t, err)
return attrs
}(),
},
},
CredentialType: string(credential.UserPasswordType),
CredentialMappingOverrides: func() *structpb.Struct {
v := map[string]interface{}{
@@ -540,13 +495,11 @@ func TestCreate(t *testing.T) {
name: "Create a valid vault CredentialLibrary user_password type with username/password mapping",
req: &pbs.CreateCredentialLibraryRequest{Item: &pb.CredentialLibrary{
CredentialStoreId: store.GetPublicId(),
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String("something"),
})
require.NoError(t, err)
return attrs
}(),
},
},
CredentialMappingOverrides: func() *structpb.Struct {
v := map[string]interface{}{
usernameAttribute: "user-test",
@@ -569,14 +522,12 @@ func TestCreate(t *testing.T) {
Scope: &scopepb.ScopeInfo{Id: prj.GetPublicId(), Type: prj.GetType(), ParentScopeId: prj.GetParentId()},
Version: 1,
Type: vault.Subtype.String(),
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String("something"),
HttpMethod: wrapperspb.String("GET"),
})
require.NoError(t, err)
return attrs
}(),
},
},
CredentialType: string(credential.UserPasswordType),
CredentialMappingOverrides: func() *structpb.Struct {
v := map[string]interface{}{
@@ -685,14 +636,12 @@ func TestGet(t *testing.T) {
CreatedTime: unspecifiedLib.CreateTime.GetTimestamp(),
UpdatedTime: unspecifiedLib.UpdateTime.GetTimestamp(),
Version: 1,
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String(unspecifiedLib.GetVaultPath()),
HttpMethod: wrapperspb.String(unspecifiedLib.GetHttpMethod()),
})
require.NoError(t, err)
return attrs
}(),
},
},
},
},
},
@@ -709,14 +658,12 @@ func TestGet(t *testing.T) {
CreatedTime: userPassLib.CreateTime.GetTimestamp(),
UpdatedTime: userPassLib.UpdateTime.GetTimestamp(),
Version: 1,
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String(userPassLib.GetVaultPath()),
HttpMethod: wrapperspb.String(userPassLib.GetHttpMethod()),
})
require.NoError(t, err)
return attrs
}(),
},
},
CredentialType: "user_password",
CredentialMappingOverrides: func() *structpb.Struct {
v := map[string]interface{}{
@@ -900,19 +847,17 @@ func TestUpdate(t *testing.T) {
req: &pbs.UpdateCredentialLibraryRequest{
UpdateMask: fieldmask(httpMethodField),
Item: &pb.CredentialLibrary{
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
HttpMethod: wrapperspb.String("pOsT"),
})
require.NoError(t, err)
return attrs
}(),
},
},
},
},
res: func(in *pb.CredentialLibrary) *pb.CredentialLibrary {
out := proto.Clone(in).(*pb.CredentialLibrary)
out.Attributes.Fields["path"] = structpb.NewStringValue("vault/path")
out.Attributes.Fields["http_method"] = structpb.NewStringValue("POST")
out.GetVaultCredentialLibraryAttributes().Path = wrapperspb.String("vault/path")
out.GetVaultCredentialLibraryAttributes().HttpMethod = wrapperspb.String("POST")
return out
},
},
@@ -921,21 +866,19 @@ func TestUpdate(t *testing.T) {
req: &pbs.UpdateCredentialLibraryRequest{
UpdateMask: fieldmask(httpRequestBodyField, httpMethodField),
Item: &pb.CredentialLibrary{
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
HttpMethod: wrapperspb.String("pOsT"),
HttpRequestBody: wrapperspb.String("body"),
})
require.NoError(t, err)
return attrs
}(),
},
},
},
},
res: func(in *pb.CredentialLibrary) *pb.CredentialLibrary {
out := proto.Clone(in).(*pb.CredentialLibrary)
out.Attributes.Fields["path"] = structpb.NewStringValue("vault/path")
out.Attributes.Fields["http_method"] = structpb.NewStringValue("POST")
out.Attributes.Fields["http_request_body"] = structpb.NewStringValue("body")
out.GetVaultCredentialLibraryAttributes().Path = wrapperspb.String("vault/path")
out.GetVaultCredentialLibraryAttributes().HttpMethod = wrapperspb.String("POST")
out.GetVaultCredentialLibraryAttributes().HttpRequestBody = wrapperspb.String("body")
return out
},
},
@@ -944,18 +887,16 @@ func TestUpdate(t *testing.T) {
req: &pbs.UpdateCredentialLibraryRequest{
UpdateMask: fieldmask(vaultPathField),
Item: &pb.CredentialLibrary{
Attributes: func() *structpb.Struct {
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String("something"),
})
require.NoError(t, err)
return attrs
}(),
},
},
},
},
res: func(in *pb.CredentialLibrary) *pb.CredentialLibrary {
out := proto.Clone(in).(*pb.CredentialLibrary)
out.Attributes.Fields["path"] = structpb.NewStringValue("something")
out.GetVaultCredentialLibraryAttributes().Path = wrapperspb.String("something")
return out
},
},
@@ -1274,9 +1215,9 @@ func TestUpdate(t *testing.T) {
UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{httpRequestBodyField}},
Item: &pb.CredentialLibrary{
Version: vl.GetVersion(),
Attributes: &structpb.Struct{
Fields: map[string]*structpb.Value{
"http_request_body": structpb.NewStringValue("body"),
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
HttpRequestBody: wrapperspb.String("body"),
},
},
},
@@ -1290,16 +1231,16 @@ func TestUpdate(t *testing.T) {
UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{httpMethodField}},
Item: &pb.CredentialLibrary{
Version: vl.GetVersion(),
Attributes: &structpb.Struct{
Fields: map[string]*structpb.Value{
"http_method": structpb.NewStringValue("POST"),
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
HttpMethod: wrapperspb.String("POST"),
},
},
},
})
require.NoError(t, err)
require.NotNil(t, cl)
require.Equal(t, "POST", cl.GetItem().GetAttributes().GetFields()["http_method"].GetStringValue())
require.Equal(t, "POST", cl.GetItem().GetVaultCredentialLibraryAttributes().GetHttpMethod().Value)
// Can set request body on POST
cl, err = s.UpdateCredentialLibrary(ctx, &pbs.UpdateCredentialLibraryRequest{
@@ -1307,17 +1248,17 @@ func TestUpdate(t *testing.T) {
UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{httpRequestBodyField}},
Item: &pb.CredentialLibrary{
Version: cl.Item.GetVersion(),
Attributes: &structpb.Struct{
Fields: map[string]*structpb.Value{
"http_request_body": structpb.NewStringValue("body"),
Attrs: &pb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &pb.VaultCredentialLibraryAttributes{
HttpRequestBody: wrapperspb.String("body"),
},
},
},
})
require.NoError(t, err)
require.NotNil(t, cl)
require.Equal(t, "POST", cl.GetItem().GetAttributes().GetFields()["http_method"].GetStringValue())
require.Equal(t, "body", cl.GetItem().GetAttributes().GetFields()["http_request_body"].GetStringValue())
require.Equal(t, "POST", cl.GetItem().GetVaultCredentialLibraryAttributes().GetHttpMethod().Value)
require.Equal(t, "body", cl.GetItem().GetVaultCredentialLibraryAttributes().GetHttpRequestBody().Value)
// Cannot unset POST method (defaulting to GET) when request body is set
_, err = s.UpdateCredentialLibrary(ctx, &pbs.UpdateCredentialLibraryRequest{
@@ -1339,7 +1280,7 @@ func TestUpdate(t *testing.T) {
})
assert.NoError(t, err)
require.NotNil(t, cl)
assert.Equal(t, "GET", cl.GetItem().GetAttributes().GetFields()["http_method"].GetStringValue())
assert.Nil(t, cl.GetItem().GetAttributes().GetFields()["http_request_body"])
assert.Equal(t, "GET", cl.GetItem().GetVaultCredentialLibraryAttributes().GetHttpMethod().Value)
assert.Nil(t, cl.GetItem().GetVaultCredentialLibraryAttributes().GetHttpRequestBody())
})
}
@@ -22,6 +22,7 @@ import (
"github.com/hashicorp/boundary/internal/types/action"
"github.com/hashicorp/boundary/internal/types/resource"
"github.com/hashicorp/boundary/internal/types/scope"
"github.com/hashicorp/boundary/internal/types/subtypes"
pb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/credentialstores"
"google.golang.org/grpc/codes"
"google.golang.org/protobuf/types/known/wrapperspb"
@@ -34,6 +35,7 @@ const (
caCertsField = "attributes.ca_cert"
clientCertField = "attributes.client_certificate"
clientCertKeyField = "attributes.certificate_key"
domain = "credential"
)
var (
@@ -478,7 +480,7 @@ func toProto(in credential.Store, opt ...handlers.Option) (*pb.CredentialStore,
out.ScopeId = in.GetScopeId()
}
if outputFields.Has(globals.TypeField) {
out.Type = credential.SubtypeFromId(in.GetPublicId()).String()
out.Type = subtypes.SubtypeFromId(domain, in.GetPublicId()).String()
}
if outputFields.Has(globals.DescriptionField) && in.GetDescription() != "" {
out.Description = wrapperspb.String(in.GetDescription())
@@ -505,7 +507,7 @@ func toProto(in credential.Store, opt ...handlers.Option) (*pb.CredentialStore,
out.AuthorizedCollectionActions = opts.WithAuthorizedCollectionActions
}
if outputFields.Has(globals.AttributesField) {
switch credential.SubtypeFromId(in.GetPublicId()) {
switch subtypes.SubtypeFromId(domain, in.GetPublicId()) {
case vault.Subtype:
vaultIn, ok := in.(*vault.CredentialStore)
if !ok {
@@ -616,7 +618,7 @@ func validateCreateRequest(req *pbs.CreateCredentialStoreRequest) error {
if !handlers.ValidId(handlers.Id(req.GetItem().GetScopeId()), scope.Project.Prefix()) {
badFields["scope_id"] = "This field must be a valid project scope id."
}
switch credential.SubtypeFromType(req.GetItem().GetType()) {
switch subtypes.SubtypeFromType(domain, req.GetItem().GetType()) {
case vault.Subtype:
attrs := &pb.VaultCredentialStoreAttributes{}
if err := handlers.StructToProto(req.GetItem().GetAttributes(), attrs); err != nil {
@@ -659,9 +661,9 @@ func validateCreateRequest(req *pbs.CreateCredentialStoreRequest) error {
func validateUpdateRequest(req *pbs.UpdateCredentialStoreRequest) error {
return handlers.ValidateUpdateRequest(req, req.GetItem(), func() map[string]string {
badFields := map[string]string{}
switch credential.SubtypeFromId(req.GetId()) {
switch subtypes.SubtypeFromId(domain, req.GetId()) {
case vault.Subtype:
if req.GetItem().GetType() != "" && credential.SubtypeFromType(req.GetItem().GetType()) != vault.Subtype {
if req.GetItem().GetType() != "" && subtypes.SubtypeFromType(domain, req.GetItem().GetType()) != vault.Subtype {
badFields["type"] = "Cannot modify resource type."
}
attrs := &pb.VaultCredentialStoreAttributes{}
@@ -46,6 +46,10 @@ import (
"google.golang.org/protobuf/types/known/wrapperspb"
)
const (
credentialDomain = "credential"
)
var (
// IdActions contains the set of actions that can be performed on
// individual resources
@@ -1140,14 +1144,14 @@ func (s Service) AuthorizeSession(ctx context.Context, req *pbs.AuthorizeSession
Name: l.GetName(),
Description: l.GetDescription(),
CredentialStoreId: l.GetStoreId(),
Type: credential.SubtypeFromId(l.GetPublicId()).String(),
Type: subtypes.SubtypeFromId(credentialDomain, l.GetPublicId()).String(),
},
CredentialSource: &pb.CredentialSource{
Id: l.GetPublicId(),
Name: l.GetName(),
Description: l.GetDescription(),
CredentialStoreId: l.GetStoreId(),
Type: credential.SubtypeFromId(l.GetPublicId()).String(),
Type: subtypes.SubtypeFromId(credentialDomain, l.GetPublicId()).String(),
CredentialType: credType,
},
Secret: &pb.SessionSecret{
@@ -2712,11 +2712,19 @@ func TestAuthorizeSession(t *testing.T) {
CredentialStoreId: store.GetPublicId(),
Name: wrapperspb.String("Library Name"),
Description: wrapperspb.String("Library Description"),
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{
"path": structpb.NewStringValue(path.Join("pki", "issue", "boundary")),
"http_method": structpb.NewStringValue("POST"),
"http_request_body": structpb.NewStringValue(`{"common_name":"boundary.com"}`),
}},
Attrs: &credpb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &credpb.VaultCredentialLibraryAttributes{
Path: &wrapperspb.StringValue{
Value: path.Join("pki", "issue", "boundary"),
},
HttpMethod: &wrapperspb.StringValue{
Value: "POST",
},
HttpRequestBody: &wrapperspb.StringValue{
Value: `{"common_name":"boundary.com"}`,
},
},
},
}})
require.NoError(t, err)
@@ -2934,10 +2942,12 @@ func TestAuthorizeSessionTypedCredentials(t *testing.T) {
CredentialStoreId: store.GetPublicId(),
Name: wrapperspb.String("Userpassword Library"),
Description: wrapperspb.String("Userpassword Library Description"),
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{
"path": structpb.NewStringValue(path.Join("secret", "data", "default-userpass")),
"http_method": structpb.NewStringValue("GET"),
}},
Attrs: &credpb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &credpb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String(path.Join("secret", "data", "default-userpass")),
HttpMethod: wrapperspb.String("GET"),
},
},
CredentialType: "user_password",
}})
require.NoError(t, err)
@@ -2946,10 +2956,12 @@ func TestAuthorizeSessionTypedCredentials(t *testing.T) {
CredentialStoreId: store.GetPublicId(),
Name: wrapperspb.String("Unspecified Library"),
Description: wrapperspb.String("Unspecified Library Description"),
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{
"path": structpb.NewStringValue(path.Join("secret", "data", "default-userpass")),
"http_method": structpb.NewStringValue("GET"),
}},
Attrs: &credpb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &credpb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String(path.Join("secret", "data", "default-userpass")),
HttpMethod: wrapperspb.String("GET"),
},
},
}})
require.NoError(t, err)
@@ -2961,10 +2973,12 @@ func TestAuthorizeSessionTypedCredentials(t *testing.T) {
CredentialStoreId: store.GetPublicId(),
Name: wrapperspb.String("Userpassword Mapping Library"),
Description: wrapperspb.String("Userpassword Mapping Library Description"),
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{
"path": structpb.NewStringValue(path.Join("secret", "data", "non-default-userpass")),
"http_method": structpb.NewStringValue("GET"),
}},
Attrs: &credpb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &credpb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String(path.Join("secret", "data", "non-default-userpass")),
HttpMethod: wrapperspb.String("GET"),
},
},
CredentialType: "user_password",
CredentialMappingOverrides: &structpb.Struct{Fields: map[string]*structpb.Value{
"username_attribute": structpb.NewStringValue("non-default-user"),
@@ -3218,9 +3232,11 @@ func TestAuthorizeSession_Errors(t *testing.T) {
clsResp, err := credService.CreateCredentialLibrary(ctx, &pbs.CreateCredentialLibraryRequest{Item: &credpb.CredentialLibrary{
CredentialStoreId: store.GetPublicId(),
Description: wrapperspb.String(fmt.Sprintf("Library Description for target %q", tar.GetName())),
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{
"path": structpb.NewStringValue(path.Join("database", "creds", "opened")),
}},
Attrs: &credpb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &credpb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String(path.Join("database", "creds", "opened")),
},
},
}})
require.NoError(t, err)
@@ -3240,9 +3256,11 @@ func TestAuthorizeSession_Errors(t *testing.T) {
clsResp, err := credService.CreateCredentialLibrary(ctx, &pbs.CreateCredentialLibraryRequest{Item: &credpb.CredentialLibrary{
CredentialStoreId: store.GetPublicId(),
Description: wrapperspb.String(fmt.Sprintf("Library Description for target %q", tar.GetName())),
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{
"path": structpb.NewStringValue("bad path"),
}},
Attrs: &credpb.CredentialLibrary_VaultCredentialLibraryAttributes{
VaultCredentialLibraryAttributes: &credpb.VaultCredentialLibraryAttributes{
Path: wrapperspb.String("bad path"),
},
},
}})
require.NoError(t, err)