Merge pull request #1796 from hashicorp/llb-credential-type-mapping
This allows users to set the type of credential that a credential library will provide. A User Password credential type and an Unspecified credential type are included with the expectation that additional credential types will be added in the future. This change also adds the credential type attribute to existing credential libraries and sets it to Unspecified. The credential type can only be set when a credential library is created. The vault credential library includes functionality for converting a Vault Secret into a User Password Credential. This mapping is enabled by default, however, the user can override the attributes used for this mapping.
This commit is contained in:
47 files changed
+3850
-518
No files matched your search
+176
-16
@@ -21,13 +21,22 @@ import (
|
||||
pb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/credentiallibraries"
|
||||
"github.com/hashicorp/go-secure-stdlib/strutil"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"google.golang.org/protobuf/types/known/structpb"
|
||||
"google.golang.org/protobuf/types/known/wrapperspb"
|
||||
)
|
||||
|
||||
const (
|
||||
vaultPathField = "attributes.path"
|
||||
httpMethodField = "attributes.http_method"
|
||||
httpRequestBodyField = "attributes.http_request_body"
|
||||
vaultPathField = "attributes.path"
|
||||
httpMethodField = "attributes.http_method"
|
||||
httpRequestBodyField = "attributes.http_request_body"
|
||||
credentialMappingPathField = "credential_mapping_overrides"
|
||||
)
|
||||
|
||||
// Credential mapping override attributes
|
||||
const (
|
||||
usernameAttribute string = "username_attribute"
|
||||
passwordAttribute string = "password_attribute"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -220,14 +229,24 @@ func (s Service) CreateCredentialLibrary(ctx context.Context, req *pbs.CreateCre
|
||||
func (s Service) UpdateCredentialLibrary(ctx context.Context, req *pbs.UpdateCredentialLibraryRequest) (*pbs.UpdateCredentialLibraryResponse, error) {
|
||||
const op = "credentiallibraries.(Service).UpdateCredentialLibrary"
|
||||
|
||||
if err := validateUpdateRequest(req); err != nil {
|
||||
repo, err := s.repoFn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cur, err := repo.LookupCredentialLibrary(ctx, req.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
currentCredentialType := credential.Type(cur.GetCredentialType())
|
||||
|
||||
if err := validateUpdateRequest(req, currentCredentialType); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
authResults := s.authResult(ctx, req.GetId(), action.Update)
|
||||
if authResults.Error != nil {
|
||||
return nil, authResults.Error
|
||||
}
|
||||
cl, err := s.updateInRepo(ctx, authResults.Scope.GetId(), req.GetId(), req.GetUpdateMask().GetPaths(), req.GetItem())
|
||||
cl, err := s.updateInRepo(ctx, authResults.Scope.GetId(), req.GetId(), req.GetUpdateMask().GetPaths(), req.GetItem(), currentCredentialType, cur.MappingOverride)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -319,23 +338,46 @@ func (s Service) createInRepo(ctx context.Context, scopeId string, item *pb.Cred
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s Service) updateInRepo(ctx context.Context, projId, id string, mask []string, item *pb.CredentialLibrary) (credential.Library, error) {
|
||||
func (s Service) updateInRepo(
|
||||
ctx context.Context,
|
||||
projId, id string,
|
||||
masks []string,
|
||||
in *pb.CredentialLibrary,
|
||||
currentCredentialType credential.Type,
|
||||
currentMapping vault.MappingOverride) (credential.Library, error) {
|
||||
|
||||
const op = "credentiallibraries.(Service).updateInRepo"
|
||||
|
||||
var dbMasks []string
|
||||
item := proto.Clone(in).(*pb.CredentialLibrary)
|
||||
item.CredentialType = string(currentCredentialType)
|
||||
|
||||
mapping, update := getMappingUpdates(currentCredentialType, currentMapping, item.GetCredentialMappingOverrides().AsMap(), masks)
|
||||
if update {
|
||||
// got mapping update, append mapping override db field mask
|
||||
dbMasks = append(dbMasks, vault.MappingOverrideField)
|
||||
mappingStruct, err := structpb.NewStruct(mapping)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
item.CredentialMappingOverrides = mappingStruct
|
||||
}
|
||||
|
||||
cl, err := toStorageVaultLibrary(item.GetCredentialStoreId(), item)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
cl.PublicId = id
|
||||
|
||||
dbMask := maskManager.Translate(mask)
|
||||
if len(dbMask) == 0 {
|
||||
dbMasks = append(dbMasks, maskManager.Translate(masks)...)
|
||||
if len(dbMasks) == 0 {
|
||||
return nil, handlers.InvalidArgumentErrorf("No valid fields included in the update mask.", map[string]string{"update_mask": "No valid fields provided in the update mask."})
|
||||
}
|
||||
repo, err := s.repoFn()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
out, rowsUpdated, err := repo.UpdateCredentialLibrary(ctx, projId, cl, item.GetVersion(), dbMask)
|
||||
out, rowsUpdated, err := repo.UpdateCredentialLibrary(ctx, projId, cl, item.GetVersion(), dbMasks)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to update credential library"))
|
||||
}
|
||||
@@ -463,13 +505,36 @@ func toProto(in credential.Library, opt ...handlers.Option) (*pb.CredentialLibra
|
||||
if outputFields.Has(globals.AuthorizedActionsField) {
|
||||
out.AuthorizedActions = opts.WithAuthorizedActions
|
||||
}
|
||||
if outputFields.Has(globals.AttributesField) {
|
||||
switch credential.SubtypeFromId(in.GetPublicId()) {
|
||||
case vault.Subtype:
|
||||
vaultIn, ok := in.(*vault.CredentialLibrary)
|
||||
if !ok {
|
||||
return nil, errors.NewDeprecated(errors.Internal, op, "unable to cast to vault credential library")
|
||||
switch credential.SubtypeFromId(in.GetPublicId()) {
|
||||
case vault.Subtype:
|
||||
vaultIn, ok := in.(*vault.CredentialLibrary)
|
||||
if !ok {
|
||||
return nil, errors.NewDeprecated(errors.Internal, op, "unable to cast to vault credential library")
|
||||
}
|
||||
|
||||
if outputFields.Has(globals.CredentialTypeField) && vaultIn.GetCredentialType() != string(credential.UnspecifiedType) {
|
||||
out.CredentialType = vaultIn.GetCredentialType()
|
||||
if outputFields.Has(globals.CredentialMappingOverridesField) && vaultIn.MappingOverride != nil {
|
||||
m := make(map[string]interface{})
|
||||
switch mapping := vaultIn.MappingOverride.(type) {
|
||||
case *vault.UserPasswordOverride:
|
||||
if mapping.UsernameAttribute != "" {
|
||||
m[usernameAttribute] = mapping.UsernameAttribute
|
||||
}
|
||||
if mapping.PasswordAttribute != "" {
|
||||
m[passwordAttribute] = mapping.PasswordAttribute
|
||||
}
|
||||
}
|
||||
if len(m) > 0 {
|
||||
mp, err := structpb.NewStruct(m)
|
||||
if err != nil {
|
||||
return nil, errors.NewDeprecated(errors.Internal, op, "creating proto struct for mapping override")
|
||||
}
|
||||
out.CredentialMappingOverrides = mp
|
||||
}
|
||||
}
|
||||
}
|
||||
if outputFields.Has(globals.AttributesField) {
|
||||
attrs := &pb.VaultCredentialLibraryAttributes{
|
||||
Path: wrapperspb.String(vaultIn.GetVaultPath()),
|
||||
}
|
||||
@@ -511,6 +576,23 @@ func toStorageVaultLibrary(storeId string, in *pb.CredentialLibrary) (out *vault
|
||||
opts = append(opts, vault.WithRequestBody([]byte(attrs.GetHttpRequestBody().GetValue())))
|
||||
}
|
||||
|
||||
credentialType := credential.Type(in.GetCredentialType())
|
||||
switch credentialType {
|
||||
case credential.UserPasswordType:
|
||||
opts = append(opts, vault.WithCredentialType(credentialType))
|
||||
overrides := in.CredentialMappingOverrides.AsMap()
|
||||
var mapOpts []vault.Option
|
||||
if username := overrides[usernameAttribute]; username != nil {
|
||||
mapOpts = append(mapOpts, vault.WithOverrideUsernameAttribute(username.(string)))
|
||||
}
|
||||
if password := overrides[passwordAttribute]; password != nil {
|
||||
mapOpts = append(mapOpts, vault.WithOverridePasswordAttribute(password.(string)))
|
||||
}
|
||||
if len(mapOpts) > 0 {
|
||||
opts = append(opts, vault.WithMappingOverride(vault.NewUserPasswordOverride(mapOpts...)))
|
||||
}
|
||||
}
|
||||
|
||||
cs, err := vault.NewCredentialLibrary(storeId, attrs.GetPath().GetValue(), opts...)
|
||||
if err != nil {
|
||||
return nil, errors.WrapDeprecated(err, op, errors.WithMsg("unable to build credential library"))
|
||||
@@ -549,6 +631,7 @@ func validateCreateRequest(req *pbs.CreateCredentialLibraryRequest) error {
|
||||
if b := attrs.GetHttpRequestBody(); b != nil && strings.ToUpper(attrs.GetHttpMethod().GetValue()) != "POST" {
|
||||
badFields[httpRequestBodyField] = fmt.Sprintf("Field can only be set if %q is set to the value 'POST'.", httpMethodField)
|
||||
}
|
||||
validateMapping(badFields, credential.Type(req.GetItem().GetCredentialType()), req.GetItem().CredentialMappingOverrides.AsMap())
|
||||
default:
|
||||
badFields[globals.CredentialStoreIdField] = "This field must be a valid credential store id."
|
||||
}
|
||||
@@ -556,7 +639,7 @@ func validateCreateRequest(req *pbs.CreateCredentialLibraryRequest) error {
|
||||
})
|
||||
}
|
||||
|
||||
func validateUpdateRequest(req *pbs.UpdateCredentialLibraryRequest) 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()) {
|
||||
@@ -564,6 +647,9 @@ func validateUpdateRequest(req *pbs.UpdateCredentialLibraryRequest) error {
|
||||
if req.GetItem().GetType() != "" && credential.SubtypeFromType(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."
|
||||
@@ -578,6 +664,7 @@ func validateUpdateRequest(req *pbs.UpdateCredentialLibraryRequest) error {
|
||||
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)
|
||||
@@ -600,3 +687,76 @@ func validateListRequest(req *pbs.ListCredentialLibrariesRequest) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateMapping(badFields map[string]string, credentialType credential.Type, overrides map[string]interface{}) {
|
||||
validFields := make(map[string]bool)
|
||||
switch credentialType {
|
||||
case "", credential.UnspecifiedType:
|
||||
if len(overrides) > 0 {
|
||||
badFields[globals.CredentialMappingOverridesField] = fmt.Sprintf("This field can only be set if %q is set", globals.CredentialTypeField)
|
||||
}
|
||||
return
|
||||
case credential.UserPasswordType:
|
||||
validFields[usernameAttribute] = true
|
||||
validFields[passwordAttribute] = true
|
||||
default:
|
||||
badFields[globals.CredentialTypeField] = fmt.Sprintf("Unknown credential type %q", credentialType)
|
||||
return
|
||||
}
|
||||
|
||||
for k, v := range overrides {
|
||||
if ok := validFields[k]; !ok {
|
||||
badFields[globals.CredentialMappingOverridesField+"."+k] = fmt.Sprintf("Invalid mapping override for credential type %q", credentialType)
|
||||
continue
|
||||
}
|
||||
if _, ok := v.(string); v != nil && !ok {
|
||||
badFields[globals.CredentialMappingOverridesField+"."+k] = fmt.Sprintf("Mapping value must be a string or a null to clear, got %T", v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getMappingUpdates(credentialType credential.Type, current vault.MappingOverride, new map[string]interface{}, apiMasks []string) (map[string]interface{}, bool) {
|
||||
ret := make(map[string]interface{})
|
||||
masks := make(map[string]bool)
|
||||
for _, m := range apiMasks {
|
||||
if m == credentialMappingPathField {
|
||||
// got top level credential mapping change request, this mask
|
||||
// can only be provided when clearing the entire override.
|
||||
return nil, true
|
||||
}
|
||||
|
||||
credMappingPrefix := fmt.Sprintf("%v.", credentialMappingPathField)
|
||||
if s := strings.SplitN(m, credMappingPrefix, 2); len(s) == 2 {
|
||||
masks[s[1]] = true
|
||||
}
|
||||
}
|
||||
if len(masks) == 0 {
|
||||
// no mapping updates
|
||||
return nil, false
|
||||
}
|
||||
|
||||
switch credentialType {
|
||||
case credential.UserPasswordType:
|
||||
var currentUser, currentPass interface{}
|
||||
if overrides, ok := current.(*vault.UserPasswordOverride); ok {
|
||||
currentUser = overrides.UsernameAttribute
|
||||
currentPass = overrides.PasswordAttribute
|
||||
}
|
||||
|
||||
switch {
|
||||
case masks[usernameAttribute]:
|
||||
ret[usernameAttribute] = new[usernameAttribute]
|
||||
default:
|
||||
ret[usernameAttribute] = currentUser
|
||||
}
|
||||
|
||||
switch {
|
||||
case masks[passwordAttribute]:
|
||||
ret[passwordAttribute] = new[passwordAttribute]
|
||||
default:
|
||||
ret[passwordAttribute] = currentPass
|
||||
}
|
||||
}
|
||||
|
||||
return ret, true
|
||||
}
|
||||
+477
-13
@@ -1,12 +1,14 @@
|
||||
package credentiallibraries
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/hashicorp/boundary/internal/credential"
|
||||
"github.com/hashicorp/boundary/internal/credential/vault"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
@@ -326,6 +328,47 @@ func TestCreate(t *testing.T) {
|
||||
res: nil,
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "Invalid credential 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)
|
||||
return attrs
|
||||
}(),
|
||||
CredentialType: "fake-type",
|
||||
}},
|
||||
res: nil,
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "Invalid user_password mapping",
|
||||
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)
|
||||
return attrs
|
||||
}(),
|
||||
CredentialType: string(credential.UserPasswordType),
|
||||
CredentialMappingOverrides: func() *structpb.Struct {
|
||||
v := map[string]interface{}{
|
||||
usernameAttribute: "user-test",
|
||||
"invalid": "invalid-test",
|
||||
}
|
||||
ret, err := structpb.NewStruct(v)
|
||||
require.NoError(t, err)
|
||||
return ret
|
||||
}(),
|
||||
}},
|
||||
res: nil,
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "Using POST method",
|
||||
req: &pbs.CreateCredentialLibraryRequest{Item: &pb.CredentialLibrary{
|
||||
@@ -403,6 +446,151 @@ 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{
|
||||
Path: wrapperspb.String("something"),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
return attrs
|
||||
}(),
|
||||
CredentialType: string(credential.UserPasswordType),
|
||||
}},
|
||||
idPrefix: vault.CredentialLibraryPrefix + "_",
|
||||
res: &pbs.CreateCredentialLibraryResponse{
|
||||
Uri: fmt.Sprintf("credential-libraries/%s_", vault.CredentialLibraryPrefix),
|
||||
Item: &pb.CredentialLibrary{
|
||||
Id: store.GetPublicId(),
|
||||
CredentialStoreId: store.GetPublicId(),
|
||||
CreatedTime: store.GetCreateTime().GetTimestamp(),
|
||||
UpdatedTime: store.GetUpdateTime().GetTimestamp(),
|
||||
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{
|
||||
Path: wrapperspb.String("something"),
|
||||
HttpMethod: wrapperspb.String("GET"),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
return attrs
|
||||
}(),
|
||||
CredentialType: string(credential.UserPasswordType),
|
||||
AuthorizedActions: testAuthorizedActions,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
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{
|
||||
Path: wrapperspb.String("something"),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
return attrs
|
||||
}(),
|
||||
CredentialMappingOverrides: func() *structpb.Struct {
|
||||
v := map[string]interface{}{
|
||||
usernameAttribute: "user-test",
|
||||
}
|
||||
ret, err := structpb.NewStruct(v)
|
||||
require.NoError(t, err)
|
||||
return ret
|
||||
}(),
|
||||
CredentialType: string(credential.UserPasswordType),
|
||||
}},
|
||||
idPrefix: vault.CredentialLibraryPrefix + "_",
|
||||
res: &pbs.CreateCredentialLibraryResponse{
|
||||
Uri: fmt.Sprintf("credential-libraries/%s_", vault.CredentialLibraryPrefix),
|
||||
Item: &pb.CredentialLibrary{
|
||||
Id: store.GetPublicId(),
|
||||
CredentialStoreId: store.GetPublicId(),
|
||||
CreatedTime: store.GetCreateTime().GetTimestamp(),
|
||||
UpdatedTime: store.GetUpdateTime().GetTimestamp(),
|
||||
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{
|
||||
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{}{
|
||||
usernameAttribute: "user-test",
|
||||
}
|
||||
ret, err := structpb.NewStruct(v)
|
||||
require.NoError(t, err)
|
||||
return ret
|
||||
}(),
|
||||
AuthorizedActions: testAuthorizedActions,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
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{
|
||||
Path: wrapperspb.String("something"),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
return attrs
|
||||
}(),
|
||||
CredentialMappingOverrides: func() *structpb.Struct {
|
||||
v := map[string]interface{}{
|
||||
usernameAttribute: "user-test",
|
||||
passwordAttribute: "pass-test",
|
||||
}
|
||||
ret, err := structpb.NewStruct(v)
|
||||
require.NoError(t, err)
|
||||
return ret
|
||||
}(),
|
||||
CredentialType: string(credential.UserPasswordType),
|
||||
}},
|
||||
idPrefix: vault.CredentialLibraryPrefix + "_",
|
||||
res: &pbs.CreateCredentialLibraryResponse{
|
||||
Uri: fmt.Sprintf("credential-libraries/%s_", vault.CredentialLibraryPrefix),
|
||||
Item: &pb.CredentialLibrary{
|
||||
Id: store.GetPublicId(),
|
||||
CredentialStoreId: store.GetPublicId(),
|
||||
CreatedTime: store.GetCreateTime().GetTimestamp(),
|
||||
UpdatedTime: store.GetUpdateTime().GetTimestamp(),
|
||||
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{
|
||||
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{}{
|
||||
usernameAttribute: "user-test",
|
||||
passwordAttribute: "pass-test",
|
||||
}
|
||||
ret, err := structpb.NewStruct(v)
|
||||
require.NoError(t, err)
|
||||
return ret
|
||||
}(),
|
||||
AuthorizedActions: testAuthorizedActions,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
@@ -461,10 +649,23 @@ func TestGet(t *testing.T) {
|
||||
_, prj := iam.TestScopes(t, iamRepo)
|
||||
|
||||
store := vault.TestCredentialStores(t, conn, wrapper, prj.GetPublicId(), 1)[0]
|
||||
vl := vault.TestCredentialLibraries(t, conn, wrapper, store.GetPublicId(), 1)[0]
|
||||
unspecifiedLib := vault.TestCredentialLibraries(t, conn, wrapper, store.GetPublicId(), 1)[0]
|
||||
s, err := NewService(repoFn, iamRepoFn)
|
||||
require.NoError(t, err)
|
||||
|
||||
repo, err := repoFn()
|
||||
require.NoError(t, err)
|
||||
lib, err := vault.NewCredentialLibrary(store.GetPublicId(), "vault/path",
|
||||
vault.WithCredentialType("user_password"),
|
||||
vault.WithMappingOverride(
|
||||
vault.NewUserPasswordOverride(
|
||||
vault.WithOverrideUsernameAttribute("user"),
|
||||
vault.WithOverridePasswordAttribute("pass"),
|
||||
)))
|
||||
require.NoError(t, err)
|
||||
userPassLib, err := repo.CreateCredentialLibrary(context.Background(), prj.GetPublicId(), lib)
|
||||
require.NoError(t, err)
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
id string
|
||||
@@ -473,21 +674,21 @@ func TestGet(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
name: "success",
|
||||
id: vl.GetPublicId(),
|
||||
id: unspecifiedLib.GetPublicId(),
|
||||
res: &pbs.GetCredentialLibraryResponse{
|
||||
Item: &pb.CredentialLibrary{
|
||||
Id: vl.GetPublicId(),
|
||||
CredentialStoreId: vl.GetStoreId(),
|
||||
Id: unspecifiedLib.GetPublicId(),
|
||||
CredentialStoreId: unspecifiedLib.GetStoreId(),
|
||||
Scope: &scopepb.ScopeInfo{Id: store.GetScopeId(), Type: scope.Project.String(), ParentScopeId: prj.GetParentId()},
|
||||
Type: vault.Subtype.String(),
|
||||
AuthorizedActions: testAuthorizedActions,
|
||||
CreatedTime: vl.CreateTime.GetTimestamp(),
|
||||
UpdatedTime: vl.UpdateTime.GetTimestamp(),
|
||||
CreatedTime: unspecifiedLib.CreateTime.GetTimestamp(),
|
||||
UpdatedTime: unspecifiedLib.UpdateTime.GetTimestamp(),
|
||||
Version: 1,
|
||||
Attributes: func() *structpb.Struct {
|
||||
attrs, err := handlers.ProtoToStruct(&pb.VaultCredentialLibraryAttributes{
|
||||
Path: wrapperspb.String(vl.GetVaultPath()),
|
||||
HttpMethod: wrapperspb.String(vl.GetHttpMethod()),
|
||||
Path: wrapperspb.String(unspecifiedLib.GetVaultPath()),
|
||||
HttpMethod: wrapperspb.String(unspecifiedLib.GetHttpMethod()),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
return attrs
|
||||
@@ -495,6 +696,40 @@ func TestGet(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "success-userpassword",
|
||||
id: userPassLib.GetPublicId(),
|
||||
res: &pbs.GetCredentialLibraryResponse{
|
||||
Item: &pb.CredentialLibrary{
|
||||
Id: userPassLib.GetPublicId(),
|
||||
CredentialStoreId: userPassLib.GetStoreId(),
|
||||
Scope: &scopepb.ScopeInfo{Id: store.GetScopeId(), Type: scope.Project.String(), ParentScopeId: prj.GetParentId()},
|
||||
Type: vault.Subtype.String(),
|
||||
AuthorizedActions: testAuthorizedActions,
|
||||
CreatedTime: userPassLib.CreateTime.GetTimestamp(),
|
||||
UpdatedTime: userPassLib.UpdateTime.GetTimestamp(),
|
||||
Version: 1,
|
||||
Attributes: func() *structpb.Struct {
|
||||
attrs, err := handlers.ProtoToStruct(&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{}{
|
||||
usernameAttribute: "user",
|
||||
passwordAttribute: "pass",
|
||||
}
|
||||
ret, err := structpb.NewStruct(v)
|
||||
require.NoError(t, err)
|
||||
return ret
|
||||
}(),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "not found error",
|
||||
id: fmt.Sprintf("%s_1234567890", vault.CredentialLibraryPrefix),
|
||||
@@ -612,8 +847,14 @@ func TestUpdate(t *testing.T) {
|
||||
cs := vault.TestCredentialStores(t, conn, wrapper, prj.GetPublicId(), 2)
|
||||
store, diffStore := cs[0], cs[1]
|
||||
|
||||
freshLibrary := func() (*vault.CredentialLibrary, func()) {
|
||||
vl := vault.TestCredentialLibraries(t, conn, wrapper, store.GetPublicId(), 1)[0]
|
||||
freshLibrary := func(opt ...vault.Option) (*vault.CredentialLibrary, func()) {
|
||||
repo, err := repoFn()
|
||||
require.NoError(t, err)
|
||||
lib, err := vault.NewCredentialLibrary(store.GetPublicId(), "vault/path", opt...)
|
||||
require.NoError(t, err)
|
||||
|
||||
vl, err := repo.CreateCredentialLibrary(ctx, prj.GetPublicId(), lib)
|
||||
require.NoError(t, err)
|
||||
clean := func() {
|
||||
_, err := s.DeleteCredentialLibrary(ctx, &pbs.DeleteCredentialLibraryRequest{Id: vl.GetPublicId()})
|
||||
require.NoError(t, err)
|
||||
@@ -629,8 +870,12 @@ func TestUpdate(t *testing.T) {
|
||||
_, token := v.CreateToken(t)
|
||||
_ = token
|
||||
|
||||
usernameAttrField := fmt.Sprintf("%v.%v", credentialMappingPathField, usernameAttribute)
|
||||
passwordAttrField := fmt.Sprintf("%v.%v", credentialMappingPathField, passwordAttribute)
|
||||
|
||||
successCases := []struct {
|
||||
name string
|
||||
opts []vault.Option
|
||||
req *pbs.UpdateCredentialLibraryRequest
|
||||
res func(*pb.CredentialLibrary) *pb.CredentialLibrary
|
||||
}{
|
||||
@@ -666,7 +911,7 @@ func TestUpdate(t *testing.T) {
|
||||
},
|
||||
res: func(in *pb.CredentialLibrary) *pb.CredentialLibrary {
|
||||
out := proto.Clone(in).(*pb.CredentialLibrary)
|
||||
out.Attributes.Fields["path"] = structpb.NewStringValue("vault/path0")
|
||||
out.Attributes.Fields["path"] = structpb.NewStringValue("vault/path")
|
||||
out.Attributes.Fields["http_method"] = structpb.NewStringValue("POST")
|
||||
return out
|
||||
},
|
||||
@@ -688,7 +933,7 @@ func TestUpdate(t *testing.T) {
|
||||
},
|
||||
res: func(in *pb.CredentialLibrary) *pb.CredentialLibrary {
|
||||
out := proto.Clone(in).(*pb.CredentialLibrary)
|
||||
out.Attributes.Fields["path"] = structpb.NewStringValue("vault/path0")
|
||||
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")
|
||||
return out
|
||||
@@ -714,12 +959,226 @@ func TestUpdate(t *testing.T) {
|
||||
return out
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "user-password-attributes-change-username-attribute",
|
||||
opts: []vault.Option{
|
||||
vault.WithCredentialType("user_password"),
|
||||
vault.WithMappingOverride(
|
||||
vault.NewUserPasswordOverride(
|
||||
vault.WithOverrideUsernameAttribute("orig-user"),
|
||||
vault.WithOverridePasswordAttribute("orig-pass"),
|
||||
)),
|
||||
},
|
||||
req: &pbs.UpdateCredentialLibraryRequest{
|
||||
UpdateMask: fieldmask(usernameAttrField),
|
||||
Item: &pb.CredentialLibrary{
|
||||
CredentialMappingOverrides: func() *structpb.Struct {
|
||||
v := map[string]interface{}{
|
||||
usernameAttribute: "changed-user",
|
||||
}
|
||||
ret, err := structpb.NewStruct(v)
|
||||
require.NoError(t, err)
|
||||
return ret
|
||||
}(),
|
||||
},
|
||||
},
|
||||
res: func(in *pb.CredentialLibrary) *pb.CredentialLibrary {
|
||||
out := proto.Clone(in).(*pb.CredentialLibrary)
|
||||
out.CredentialMappingOverrides.Fields[usernameAttribute] = structpb.NewStringValue("changed-user")
|
||||
return out
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "user-password-attributes-change-password-attribute",
|
||||
opts: []vault.Option{
|
||||
vault.WithCredentialType("user_password"),
|
||||
vault.WithMappingOverride(
|
||||
vault.NewUserPasswordOverride(
|
||||
vault.WithOverrideUsernameAttribute("orig-user"),
|
||||
vault.WithOverridePasswordAttribute("orig-pass"),
|
||||
)),
|
||||
},
|
||||
req: &pbs.UpdateCredentialLibraryRequest{
|
||||
UpdateMask: fieldmask(passwordAttrField),
|
||||
Item: &pb.CredentialLibrary{
|
||||
CredentialMappingOverrides: func() *structpb.Struct {
|
||||
v := map[string]interface{}{
|
||||
passwordAttribute: "changed-pass",
|
||||
}
|
||||
ret, err := structpb.NewStruct(v)
|
||||
require.NoError(t, err)
|
||||
return ret
|
||||
}(),
|
||||
},
|
||||
},
|
||||
res: func(in *pb.CredentialLibrary) *pb.CredentialLibrary {
|
||||
out := proto.Clone(in).(*pb.CredentialLibrary)
|
||||
out.CredentialMappingOverrides.Fields[passwordAttribute] = structpb.NewStringValue("changed-pass")
|
||||
return out
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "user-password-attributes-change-username-and-password-attributes",
|
||||
opts: []vault.Option{
|
||||
vault.WithCredentialType("user_password"),
|
||||
vault.WithMappingOverride(
|
||||
vault.NewUserPasswordOverride(
|
||||
vault.WithOverrideUsernameAttribute("orig-user"),
|
||||
vault.WithOverridePasswordAttribute("orig-pass"),
|
||||
)),
|
||||
},
|
||||
req: &pbs.UpdateCredentialLibraryRequest{
|
||||
UpdateMask: fieldmask(passwordAttrField, usernameAttrField),
|
||||
Item: &pb.CredentialLibrary{
|
||||
CredentialMappingOverrides: func() *structpb.Struct {
|
||||
v := map[string]interface{}{
|
||||
usernameAttribute: "changed-user",
|
||||
passwordAttribute: "changed-pass",
|
||||
}
|
||||
ret, err := structpb.NewStruct(v)
|
||||
require.NoError(t, err)
|
||||
return ret
|
||||
}(),
|
||||
},
|
||||
},
|
||||
res: func(in *pb.CredentialLibrary) *pb.CredentialLibrary {
|
||||
out := proto.Clone(in).(*pb.CredentialLibrary)
|
||||
out.CredentialMappingOverrides.Fields[usernameAttribute] = structpb.NewStringValue("changed-user")
|
||||
out.CredentialMappingOverrides.Fields[passwordAttribute] = structpb.NewStringValue("changed-pass")
|
||||
return out
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "no-mapping-override-change-username-and-password-attributes",
|
||||
opts: []vault.Option{
|
||||
vault.WithCredentialType("user_password"),
|
||||
},
|
||||
req: &pbs.UpdateCredentialLibraryRequest{
|
||||
UpdateMask: fieldmask(passwordAttrField, usernameAttrField),
|
||||
Item: &pb.CredentialLibrary{
|
||||
CredentialMappingOverrides: func() *structpb.Struct {
|
||||
v := map[string]interface{}{
|
||||
usernameAttribute: "new-user",
|
||||
passwordAttribute: "new-pass",
|
||||
}
|
||||
ret, err := structpb.NewStruct(v)
|
||||
require.NoError(t, err)
|
||||
return ret
|
||||
}(),
|
||||
},
|
||||
},
|
||||
res: func(in *pb.CredentialLibrary) *pb.CredentialLibrary {
|
||||
out := proto.Clone(in).(*pb.CredentialLibrary)
|
||||
v := map[string]interface{}{
|
||||
usernameAttribute: "new-user",
|
||||
passwordAttribute: "new-pass",
|
||||
}
|
||||
var err error
|
||||
out.CredentialMappingOverrides, err = structpb.NewStruct(v)
|
||||
require.NoError(t, err)
|
||||
return out
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "user-password-attributes-delete-mapping-override",
|
||||
opts: []vault.Option{
|
||||
vault.WithCredentialType("user_password"),
|
||||
vault.WithMappingOverride(
|
||||
vault.NewUserPasswordOverride(
|
||||
vault.WithOverrideUsernameAttribute("orig-user"),
|
||||
vault.WithOverridePasswordAttribute("orig-pass"),
|
||||
)),
|
||||
},
|
||||
req: &pbs.UpdateCredentialLibraryRequest{
|
||||
UpdateMask: fieldmask(credentialMappingPathField),
|
||||
Item: &pb.CredentialLibrary{
|
||||
CredentialMappingOverrides: nil,
|
||||
},
|
||||
},
|
||||
res: func(in *pb.CredentialLibrary) *pb.CredentialLibrary {
|
||||
out := proto.Clone(in).(*pb.CredentialLibrary)
|
||||
out.CredentialMappingOverrides = nil
|
||||
return out
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "no-mapping-override-delete-mapping-override",
|
||||
opts: []vault.Option{
|
||||
vault.WithCredentialType("user_password"),
|
||||
},
|
||||
req: &pbs.UpdateCredentialLibraryRequest{
|
||||
UpdateMask: fieldmask(credentialMappingPathField),
|
||||
Item: &pb.CredentialLibrary{
|
||||
CredentialMappingOverrides: nil,
|
||||
},
|
||||
},
|
||||
res: func(in *pb.CredentialLibrary) *pb.CredentialLibrary {
|
||||
out := proto.Clone(in).(*pb.CredentialLibrary)
|
||||
out.CredentialMappingOverrides = nil
|
||||
return out
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "user-password-attributes-delete-mapping-override-field-specific",
|
||||
opts: []vault.Option{
|
||||
vault.WithCredentialType("user_password"),
|
||||
vault.WithMappingOverride(
|
||||
vault.NewUserPasswordOverride(
|
||||
vault.WithOverrideUsernameAttribute("orig-user"),
|
||||
vault.WithOverridePasswordAttribute("orig-pass"),
|
||||
)),
|
||||
},
|
||||
req: &pbs.UpdateCredentialLibraryRequest{
|
||||
UpdateMask: fieldmask(passwordAttrField, usernameAttrField),
|
||||
Item: &pb.CredentialLibrary{
|
||||
CredentialMappingOverrides: func() *structpb.Struct {
|
||||
v := map[string]interface{}{
|
||||
usernameAttribute: nil,
|
||||
passwordAttribute: nil,
|
||||
}
|
||||
ret, err := structpb.NewStruct(v)
|
||||
require.NoError(t, err)
|
||||
return ret
|
||||
}(),
|
||||
},
|
||||
},
|
||||
res: func(in *pb.CredentialLibrary) *pb.CredentialLibrary {
|
||||
out := proto.Clone(in).(*pb.CredentialLibrary)
|
||||
out.CredentialMappingOverrides = nil
|
||||
return out
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "no-mapping-override-delete-mapping-override-field-specific",
|
||||
opts: []vault.Option{
|
||||
vault.WithCredentialType("user_password"),
|
||||
},
|
||||
req: &pbs.UpdateCredentialLibraryRequest{
|
||||
UpdateMask: fieldmask(passwordAttrField, usernameAttrField),
|
||||
Item: &pb.CredentialLibrary{
|
||||
CredentialMappingOverrides: func() *structpb.Struct {
|
||||
v := map[string]interface{}{
|
||||
usernameAttribute: nil,
|
||||
passwordAttribute: nil,
|
||||
}
|
||||
ret, err := structpb.NewStruct(v)
|
||||
require.NoError(t, err)
|
||||
return ret
|
||||
}(),
|
||||
},
|
||||
},
|
||||
res: func(in *pb.CredentialLibrary) *pb.CredentialLibrary {
|
||||
out := proto.Clone(in).(*pb.CredentialLibrary)
|
||||
out.CredentialMappingOverrides = nil
|
||||
return out
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range successCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
st, cleanup := freshLibrary()
|
||||
st, cleanup := freshLibrary(tc.opts...)
|
||||
defer cleanup()
|
||||
|
||||
if tc.req.Item.GetVersion() == 0 {
|
||||
@@ -782,6 +1241,11 @@ func TestUpdate(t *testing.T) {
|
||||
path: "authorized actions",
|
||||
item: &pb.CredentialLibrary{AuthorizedActions: append(testAuthorizedActions, "another")},
|
||||
},
|
||||
{
|
||||
name: "read only credential type",
|
||||
path: "credential_type",
|
||||
item: &pb.CredentialLibrary{CredentialType: string(credential.UserPasswordType)},
|
||||
},
|
||||
}
|
||||
for _, tc := range errCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
|
||||
Reference in new issue
Block a user