* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * Update copyright file headers to BUS-1.1 * Rerun make gen This will pick up the last of the license changes * Revert "Rerun make gen" This reverts commit dbe0968bffd92745d8663c6940debae098395a6b. * Remove copywrite from generation script The current version of copywrite is not sophisticated enough to only apply the copywrite changes to certain subdirectories, so to avoid accidentally relicensing our api and sdk modules, we will stop running the copywrite script until such time that it can do the right thing. * License protobuf API as MPL The protobuf API files are used to generate the SDK, since we want the SDK to be MPL, the protobuf files must also be MPL. This extends to all protobuf files whose sources end up in the SDK directory, including: - internal/proto/controller/api - internal/proto/controller/custom_options - internal/proto/plugin It also includes the generated service directory in internal/gen/controller/api/services, since they are generated from the protobuf sources used for the SDK. * Change website license file name This makes the naming scheme consistent with the rest of the repository. * Add note on licensing to README * Set copywrite license to BUSL * Revert "Add note on licensing to README" This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe. --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
324 lines
9.8 KiB
Go
324 lines
9.8 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package target_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/boundary/globals"
|
|
"github.com/hashicorp/boundary/internal/credential"
|
|
"github.com/hashicorp/boundary/internal/credential/static"
|
|
"github.com/hashicorp/boundary/internal/credential/vault"
|
|
"github.com/hashicorp/boundary/internal/db"
|
|
"github.com/hashicorp/boundary/internal/errors"
|
|
"github.com/hashicorp/boundary/internal/iam"
|
|
"github.com/hashicorp/boundary/internal/kms"
|
|
"github.com/hashicorp/boundary/internal/target"
|
|
"github.com/hashicorp/boundary/internal/target/store"
|
|
"github.com/hashicorp/boundary/internal/target/targettest"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type hooks struct{}
|
|
|
|
func (h hooks) NewTarget(ctx context.Context, projectId string, opt ...target.Option) (target.Target, error) {
|
|
return targettest.New(ctx, projectId, opt...)
|
|
}
|
|
|
|
func (h hooks) AllocTarget() target.Target {
|
|
return targettest.Alloc()
|
|
}
|
|
|
|
func (h hooks) Vet(ctx context.Context, t target.Target) error {
|
|
return targettest.Vet(ctx, t)
|
|
}
|
|
|
|
func (h hooks) VetForUpdate(ctx context.Context, t target.Target, paths []string) error {
|
|
return targettest.VetForUpdate(ctx, t, paths)
|
|
}
|
|
|
|
func (h hooks) VetCredentialSources(ctx context.Context, cls []*target.CredentialLibrary, creds []*target.StaticCredential) error {
|
|
return targettest.VetCredentialSources(ctx, cls, creds)
|
|
}
|
|
|
|
func TestRepository_SetTargetCredentialSources(t *testing.T) {
|
|
ctx := context.Background()
|
|
target.Register(targettest.Subtype, hooks{}, globals.TcpTargetPrefix)
|
|
|
|
t.Parallel()
|
|
conn, _ := db.TestSetup(t, "postgres")
|
|
rw := db.New(conn)
|
|
wrapper := db.TestWrapper(t)
|
|
testKms := kms.TestKms(t, conn, wrapper)
|
|
repo, err := target.NewRepository(context.Background(), rw, rw, testKms)
|
|
require.NoError(t, err)
|
|
|
|
iamRepo := iam.TestRepo(t, conn, wrapper)
|
|
_, proj := iam.TestScopes(t, iamRepo)
|
|
|
|
storeVault := vault.TestCredentialStores(t, conn, wrapper, proj.GetPublicId(), 1)[0]
|
|
credLibs := vault.TestCredentialLibraries(t, conn, wrapper, storeVault.GetPublicId(), 2)
|
|
lib1 := credLibs[0]
|
|
lib2 := credLibs[1]
|
|
|
|
storeStatic := static.TestCredentialStores(t, conn, wrapper, proj.GetPublicId(), 1)[0]
|
|
credsStatic := static.TestUsernamePasswordCredentials(t, conn, wrapper, "u", "p", storeStatic.GetPublicId(), proj.GetPublicId(), 2)
|
|
cred1 := credsStatic[0]
|
|
cred2 := credsStatic[1]
|
|
|
|
setupFn := func(tar target.Target) ([]target.CredentialSource, target.CredentialSources) {
|
|
credLibs := vault.TestCredentialLibraries(t, conn, wrapper, storeVault.GetPublicId(), 5)
|
|
var ids target.CredentialSources
|
|
for _, cl := range credLibs {
|
|
ids.BrokeredCredentialIds = append(ids.BrokeredCredentialIds, cl.GetPublicId())
|
|
}
|
|
creds := static.TestUsernamePasswordCredentials(t, conn, wrapper, "u", "p", storeStatic.GetPublicId(), proj.GetPublicId(), 5)
|
|
for _, cred := range creds {
|
|
ids.BrokeredCredentialIds = append(ids.BrokeredCredentialIds, cred.GetPublicId())
|
|
}
|
|
|
|
target, err := repo.AddTargetCredentialSources(ctx, tar.GetPublicId(), 1, ids)
|
|
require.NoError(t, err)
|
|
|
|
credentialSources := target.GetCredentialSources()
|
|
|
|
require.Equal(t, 10, len(credentialSources))
|
|
return credentialSources, ids
|
|
}
|
|
type args struct {
|
|
targetVersion uint32
|
|
ids target.CredentialSources
|
|
addToOrigSources bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
setup func(target.Target) ([]target.CredentialSource, target.CredentialSources)
|
|
args args
|
|
wantAffectedRows int
|
|
wantErr bool
|
|
wantErrCode errors.Code
|
|
}{
|
|
{
|
|
name: "clear",
|
|
setup: setupFn,
|
|
args: args{
|
|
targetVersion: 2,
|
|
},
|
|
wantErr: false,
|
|
wantAffectedRows: 10,
|
|
},
|
|
{
|
|
name: "no-change",
|
|
setup: setupFn,
|
|
args: args{
|
|
targetVersion: 2,
|
|
addToOrigSources: true,
|
|
},
|
|
wantErr: false,
|
|
wantAffectedRows: 0,
|
|
},
|
|
{
|
|
name: "add-cred-library",
|
|
setup: setupFn,
|
|
args: args{
|
|
targetVersion: 2,
|
|
ids: target.CredentialSources{
|
|
BrokeredCredentialIds: []string{lib1.PublicId, lib2.PublicId},
|
|
},
|
|
addToOrigSources: true,
|
|
},
|
|
wantErr: false,
|
|
wantAffectedRows: 2,
|
|
},
|
|
{
|
|
name: "add-cred-static",
|
|
setup: setupFn,
|
|
args: args{
|
|
targetVersion: 2,
|
|
ids: target.CredentialSources{
|
|
BrokeredCredentialIds: []string{cred1.PublicId, cred2.PublicId},
|
|
},
|
|
addToOrigSources: true,
|
|
},
|
|
wantErr: false,
|
|
wantAffectedRows: 2,
|
|
},
|
|
{
|
|
name: "add-cred-lib-and-static",
|
|
setup: setupFn,
|
|
args: args{
|
|
targetVersion: 2,
|
|
ids: target.CredentialSources{
|
|
BrokeredCredentialIds: []string{cred1.PublicId, lib1.PublicId, cred2.PublicId, lib2.PublicId},
|
|
},
|
|
addToOrigSources: true,
|
|
},
|
|
wantErr: false,
|
|
wantAffectedRows: 4,
|
|
},
|
|
{
|
|
name: "zero version",
|
|
setup: setupFn,
|
|
args: args{
|
|
targetVersion: 0,
|
|
ids: target.CredentialSources{
|
|
BrokeredCredentialIds: []string{lib1.PublicId, lib2.PublicId},
|
|
},
|
|
addToOrigSources: true,
|
|
},
|
|
wantErr: true,
|
|
wantErrCode: errors.InvalidParameter,
|
|
},
|
|
{
|
|
name: "bad version",
|
|
setup: setupFn,
|
|
args: args{
|
|
targetVersion: 1000,
|
|
ids: target.CredentialSources{
|
|
BrokeredCredentialIds: []string{lib1.PublicId, lib2.PublicId},
|
|
},
|
|
addToOrigSources: true,
|
|
},
|
|
wantErr: true,
|
|
wantErrCode: errors.VersionMismatch,
|
|
},
|
|
{
|
|
name: "remove existing and add cred libs",
|
|
setup: setupFn,
|
|
args: args{
|
|
targetVersion: 2,
|
|
ids: target.CredentialSources{
|
|
BrokeredCredentialIds: []string{lib1.PublicId, lib2.PublicId},
|
|
},
|
|
addToOrigSources: false,
|
|
},
|
|
wantErr: false,
|
|
wantAffectedRows: 12,
|
|
},
|
|
{
|
|
name: "remove existing and add cred static",
|
|
setup: setupFn,
|
|
args: args{
|
|
targetVersion: 2,
|
|
ids: target.CredentialSources{
|
|
BrokeredCredentialIds: []string{cred1.PublicId, cred2.PublicId},
|
|
},
|
|
addToOrigSources: false,
|
|
},
|
|
wantErr: false,
|
|
wantAffectedRows: 12,
|
|
},
|
|
{
|
|
name: "remove existing and add cred static and lib",
|
|
setup: setupFn,
|
|
args: args{
|
|
targetVersion: 2,
|
|
ids: target.CredentialSources{
|
|
BrokeredCredentialIds: []string{cred1.PublicId, lib2.PublicId},
|
|
},
|
|
addToOrigSources: false,
|
|
},
|
|
wantErr: false,
|
|
wantAffectedRows: 12,
|
|
},
|
|
{
|
|
name: "injected-application-credential-purpose",
|
|
setup: setupFn,
|
|
args: args{
|
|
targetVersion: 2,
|
|
ids: target.CredentialSources{
|
|
InjectedApplicationCredentialIds: []string{lib1.PublicId},
|
|
},
|
|
addToOrigSources: true,
|
|
},
|
|
wantAffectedRows: 1,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
assert, require := assert.New(t), require.New(t)
|
|
|
|
ctx := context.Background()
|
|
tar := targettest.TestNewTestTarget(ctx, t, conn, proj.PublicId, tt.name)
|
|
|
|
var origCredSources []target.CredentialSource
|
|
wantCredSources := make(map[string]target.CredentialSource)
|
|
if tt.setup != nil {
|
|
var origCredIds target.CredentialSources
|
|
origCredSources, origCredIds = tt.setup(tar)
|
|
|
|
if tt.args.addToOrigSources {
|
|
tt.args.ids.BrokeredCredentialIds = append(tt.args.ids.BrokeredCredentialIds, origCredIds.BrokeredCredentialIds...)
|
|
tt.args.ids.InjectedApplicationCredentialIds = append(tt.args.ids.InjectedApplicationCredentialIds, origCredIds.InjectedApplicationCredentialIds...)
|
|
}
|
|
}
|
|
|
|
byPurpose := map[credential.Purpose][]string{
|
|
credential.BrokeredPurpose: tt.args.ids.BrokeredCredentialIds,
|
|
credential.InjectedApplicationPurpose: tt.args.ids.InjectedApplicationCredentialIds,
|
|
}
|
|
for purpose, ids := range byPurpose {
|
|
for _, id := range ids {
|
|
wantCredSources[id+"_"+string(purpose)] = &target.TargetCredentialSource{
|
|
CredentialSource: &store.CredentialSource{
|
|
CredentialSourceId: id,
|
|
CredentialPurpose: string(purpose),
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
origTarget, err := repo.LookupTarget(ctx, tar.GetPublicId())
|
|
require.NoError(err)
|
|
|
|
assert.Equal(origCredSources, origTarget.GetCredentialSources())
|
|
|
|
_, gotSources, affectedRows, err := repo.SetTargetCredentialSources(ctx, tar.GetPublicId(), tt.args.targetVersion, tt.args.ids)
|
|
if tt.wantErr {
|
|
require.Error(err)
|
|
assert.Equal(0, affectedRows)
|
|
assert.Truef(errors.Match(errors.T(tt.wantErrCode), err), "unexpected error %s", err.Error())
|
|
return
|
|
}
|
|
t.Log(err)
|
|
require.NoError(err)
|
|
assert.Equal(tt.wantAffectedRows, affectedRows)
|
|
|
|
for _, cs := range gotSources {
|
|
w, ok := wantCredSources[cs.Id()+"_"+string(cs.CredentialPurpose())]
|
|
assert.True(ok, "got unexpected credentialsource %v", cs)
|
|
assert.Equal(w.Id(), cs.Id())
|
|
assert.Equal(w.CredentialPurpose(), cs.CredentialPurpose())
|
|
}
|
|
|
|
foundTarget, err := repo.LookupTarget(ctx, tar.GetPublicId())
|
|
require.NoError(err)
|
|
if tt.name != "no-change" {
|
|
assert.Equalf(tt.args.targetVersion+1, foundTarget.GetVersion(), "%s unexpected version: %d/%d", tt.name, tt.args.targetVersion+1, foundTarget.GetVersion())
|
|
assert.Equalf(origTarget.GetVersion(), foundTarget.GetVersion()-1, "%s unexpected version: %d/%d", tt.name, origTarget.GetVersion(), foundTarget.GetVersion()-1)
|
|
}
|
|
})
|
|
}
|
|
t.Run("missing-target-id", func(t *testing.T) {
|
|
assert, require := assert.New(t), require.New(t)
|
|
|
|
_, _, _, err := repo.SetTargetCredentialSources(context.Background(), "", 1,
|
|
target.CredentialSources{BrokeredCredentialIds: []string{lib1.PublicId}})
|
|
|
|
require.Error(err)
|
|
assert.Truef(errors.Match(errors.T(errors.InvalidParameter), err), "unexpected error %s", err.Error())
|
|
})
|
|
t.Run("target-not-found", func(t *testing.T) {
|
|
assert, require := assert.New(t), require.New(t)
|
|
|
|
_, _, _, err := repo.SetTargetCredentialSources(context.Background(), "fake-target-id", 1,
|
|
target.CredentialSources{BrokeredCredentialIds: []string{lib1.PublicId}})
|
|
|
|
require.Error(err)
|
|
assert.Truef(errors.Match(errors.T(errors.RecordNotFound), err), "unexpected error %s", err.Error())
|
|
})
|
|
}
|