Files
threshold/internal/daemon/controller/controller_test.go
T
hashicorp-copywrite[bot]andJohan Brandhorst-Satzkorn 29da0bcb92 [COMPLIANCE] License changes (#3567)
* 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>
2023-08-11 10:31:53 -07:00

322 lines
8.3 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package controller
import (
"context"
"os"
"path/filepath"
"sync"
"testing"
"github.com/hashicorp/boundary/internal/cmd/base"
"github.com/hashicorp/boundary/internal/cmd/config"
"github.com/hashicorp/boundary/internal/iam"
"github.com/hashicorp/boundary/internal/kms"
"github.com/hashicorp/boundary/internal/types/scope"
boundary_plugin_assets "github.com/hashicorp/boundary/plugins/boundary"
"github.com/hashicorp/go-secure-stdlib/listenerutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestController_New(t *testing.T) {
t.Run("ReconcileKeys", func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
testCtx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
tc := &TestController{
t: t,
ctx: ctx,
cancel: cancel,
opts: nil,
shutdownDoneCh: make(chan struct{}),
shutdownOnce: new(sync.Once),
}
// TestControllerConfig(...) will create initial scopes
conf := TestControllerConfig(t, ctx, tc, nil)
org, _ := iam.TestScopes(t, iam.TestRepo(t, conf.Server.Database, conf.RootKms))
// this tests a scenario where there is an audit DEK
_, err := New(testCtx, conf)
require.NoError(err)
verifyFn := func() {
// verify audit DEKs
kmsCache := kms.TestKms(t, conf.Server.Database, conf.RootKms)
w, err := kmsCache.GetWrapper(testCtx, scope.Global.String(), kms.KeyPurposeAudit)
require.NoError(err)
assert.NotNil(w)
w, err = kmsCache.GetWrapper(testCtx, scope.Global.String(), kms.KeyPurposeOidc)
require.NoError(err)
assert.NotNil(w)
w, err = kmsCache.GetWrapper(testCtx, org.PublicId, kms.KeyPurposeOidc)
require.NoError(err)
assert.NotNil(w)
}
verifyFn()
// this tests a scenario where there is NOT an audit DEK
kms.TestKmsDeleteKeyPurpose(t, conf.Database, kms.KeyPurposeAudit)
kms.TestKmsDeleteKeyPurpose(t, conf.Database, kms.KeyPurposeOidc)
// re-init an empty cache and assert that the DEKs are not there.
kmsCache := kms.TestKms(t, conf.Server.Database, conf.RootKms)
w, err := kmsCache.GetWrapper(testCtx, scope.Global.String(), kms.KeyPurposeAudit)
require.Error(err)
assert.Nil(w)
w, err = kmsCache.GetWrapper(testCtx, scope.Global.String(), kms.KeyPurposeOidc)
require.Error(err)
assert.Nil(w)
w, err = kmsCache.GetWrapper(testCtx, org.PublicId, kms.KeyPurposeOidc)
require.Error(err)
assert.Nil(w)
// New(...) will reconcile the keys
_, err = New(testCtx, conf)
require.NoError(err)
// verify audit DEKs
verifyFn()
})
}
func TestControllerNewListenerConfig(t *testing.T) {
tests := []struct {
name string
listeners []*base.ServerListener
assertions func(t *testing.T, c *Controller)
expErr bool
expErrMsg string
}{
{
name: "valid listener configuration",
listeners: []*base.ServerListener{
{
Config: &listenerutil.ListenerConfig{
Purpose: []string{"api"},
},
},
{
Config: &listenerutil.ListenerConfig{
Purpose: []string{"api"},
},
},
{
Config: &listenerutil.ListenerConfig{
Purpose: []string{"cluster"},
},
},
},
assertions: func(t *testing.T, c *Controller) {
require.Len(t, c.apiListeners, 2)
require.NotNil(t, c.clusterListener)
},
},
{
name: "listeners are required",
listeners: []*base.ServerListener{},
expErr: true,
expErrMsg: "no api listeners found",
},
{
name: "listeners are required - not nil",
listeners: []*base.ServerListener{nil, nil},
expErr: true,
expErrMsg: "no api listeners found",
},
{
name: "listeners are required - with config",
listeners: []*base.ServerListener{{}, {}},
expErr: true,
expErrMsg: "no api listeners found",
},
{
name: "listeners are required - with purposes",
listeners: []*base.ServerListener{
{
Config: &listenerutil.ListenerConfig{Purpose: nil},
},
{
Config: &listenerutil.ListenerConfig{Purpose: nil},
},
},
expErr: true,
expErrMsg: "no api listeners found",
},
{
name: "both api and cluster listeners are required",
listeners: []*base.ServerListener{
{
Config: &listenerutil.ListenerConfig{
Purpose: []string{"api"},
},
},
},
expErr: true,
expErrMsg: "exactly one cluster listener is required",
},
{
name: "both api and cluster listeners are required 2",
listeners: []*base.ServerListener{
{
Config: &listenerutil.ListenerConfig{
Purpose: []string{"cluster"},
},
},
},
expErr: true,
expErrMsg: "no api listeners found",
},
{
name: "only one cluster listener is allowed",
listeners: []*base.ServerListener{
{
Config: &listenerutil.ListenerConfig{
Purpose: []string{"api"},
},
},
{
Config: &listenerutil.ListenerConfig{
Purpose: []string{"cluster"},
},
},
{
Config: &listenerutil.ListenerConfig{
Purpose: []string{"cluster"},
},
},
},
expErr: true,
expErrMsg: "exactly one cluster listener is required",
},
{
name: "only one purpose is allowed per listener",
listeners: []*base.ServerListener{
{
Config: &listenerutil.ListenerConfig{
Purpose: []string{"api", "cluster"},
},
},
},
expErr: true,
expErrMsg: `found listener with multiple purposes "api,cluster"`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
tc := &TestController{
t: t,
ctx: ctx,
cancel: cancel,
opts: nil,
shutdownDoneCh: make(chan struct{}),
shutdownOnce: new(sync.Once),
}
conf := TestControllerConfig(t, ctx, tc, nil)
conf.Listeners = tt.listeners
c, err := New(ctx, conf)
if tt.expErr {
require.EqualError(t, err, tt.expErrMsg)
require.Nil(t, c)
return
}
require.NoError(t, err)
require.NotNil(t, c)
tt.assertions(t, c)
})
}
}
func TestController_NewPluginsConfig(t *testing.T) {
require := require.New(t)
testCtx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
tc := &TestController{
t: t,
ctx: ctx,
cancel: cancel,
opts: nil,
shutdownDoneCh: make(chan struct{}),
shutdownOnce: new(sync.Once),
}
initialConfig, err := config.DevController()
require.NoError(err)
tmpDir := t.TempDir()
initialConfig.Plugins.ExecutionDir = tmpDir
conf := TestControllerConfig(t, ctx, tc, &TestControllerOpts{Config: initialConfig})
conf.EnabledPlugins = []base.EnabledPlugin{
base.EnabledPluginAws,
base.EnabledPluginHostAzure,
}
_, err = New(testCtx, conf)
require.NoError(err)
// Check that both plugins were written to the temp dir
files, err := os.ReadDir(tmpDir)
require.NoError(err)
require.Len(files, 2)
for _, file := range files {
name := filepath.Base(file.Name())
// Remove random chars and hyphen
name = name[0 : len(name)-6]
switch name {
case boundary_plugin_assets.PluginPrefix + "aws",
boundary_plugin_assets.PluginPrefix + "azure":
default:
require.Fail("unexpected name", name)
}
}
}
func Test_ControllerStart(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
tc := &TestController{
t: t,
ctx: ctx,
cancel: cancel,
opts: nil,
shutdownDoneCh: make(chan struct{}),
shutdownOnce: new(sync.Once),
}
conf := TestControllerConfig(t, ctx, tc, nil)
c, err := New(ctx, conf)
require.NoError(t, err)
err = c.Start()
require.NoError(t, err)
t.Cleanup(func() {
assert.NoError(t, c.Shutdown())
})
sqlDb, err := tc.DbConn().SqlDB(ctx)
require.NoError(t, err)
rows, err := sqlDb.QueryContext(ctx, "select name from job")
require.NoError(t, err)
t.Cleanup(func() {
assert.NoError(t, rows.Close())
})
var jobNames []string
for rows.Next() {
var jobName string
err := rows.Scan(&jobName)
require.NoError(t, err)
jobNames = append(jobNames, jobName)
}
require.NoError(t, rows.Err())
// Check that the monitor job and at least one of the rewrapping jobs has been registered
assert.Contains(t, jobNames, "data-key-version-destruction-monitor-job")
assert.Contains(t, jobNames, "session-rewrapping-job")
}