* Update go-kms-wrapping dependency * Apply changes suggested by Jeff * Fix listener test * Add note about config fix to CHANGELOG * Review comments Co-authored-by: Dan Heath <76443935+Dan-Heath@users.noreply.github.com> Co-authored-by: Dan Heath <76443935+Dan-Heath@users.noreply.github.com>
This commit is contained in:
1 parent
58f86936e7
commit
df02501dcc
15 files changed
+271
-25
No files matched your search
@@ -2,6 +2,7 @@ package base
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
@@ -10,6 +11,8 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/hashicorp/boundary/internal/observability/event"
|
||||
"github.com/hashicorp/go-hclog"
|
||||
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
|
||||
"github.com/hashicorp/go-kms-wrapping/v2/extras/multi"
|
||||
"github.com/hashicorp/go-secure-stdlib/configutil/v2"
|
||||
"github.com/hashicorp/go-secure-stdlib/listenerutil"
|
||||
"github.com/mitchellh/cli"
|
||||
@@ -31,7 +34,7 @@ func Test_NewServer(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestServer_SetupKMSes(t *testing.T) {
|
||||
func TestServer_SetupKMSes_Purposes(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
purposes []string
|
||||
@@ -61,6 +64,41 @@ func TestServer_SetupKMSes(t *testing.T) {
|
||||
globals.KmsPurposeWorkerAuthStorage, globals.KmsPurposeConfig,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "previous root without root",
|
||||
purposes: []string{globals.KmsPurposePreviousRoot},
|
||||
wantErrContains: fmt.Sprintf("KMS block contains '%s' without '%s'", globals.KmsPurposePreviousRoot, globals.KmsPurposeRoot),
|
||||
},
|
||||
{
|
||||
name: "root and previous in the same stanza",
|
||||
purposes: []string{globals.KmsPurposeRoot, globals.KmsPurposePreviousRoot},
|
||||
wantErrContains: fmt.Sprintf("KMS blocks with purposes '%s' and '%s' must have different key IDs", globals.KmsPurposeRoot, globals.KmsPurposePreviousRoot),
|
||||
},
|
||||
{
|
||||
name: "duplicate root purposes",
|
||||
purposes: []string{globals.KmsPurposeRoot, globals.KmsPurposeRoot},
|
||||
wantErrContains: fmt.Sprintf("Duplicate KMS block for purpose '%s'", globals.KmsPurposeRoot),
|
||||
},
|
||||
{
|
||||
name: "duplicate previous root purposes",
|
||||
purposes: []string{globals.KmsPurposePreviousRoot, globals.KmsPurposePreviousRoot},
|
||||
wantErrContains: fmt.Sprintf("Duplicate KMS block for purpose '%s'", globals.KmsPurposePreviousRoot),
|
||||
},
|
||||
{
|
||||
name: "duplicate worker auth purposes",
|
||||
purposes: []string{globals.KmsPurposeWorkerAuth, globals.KmsPurposeWorkerAuth},
|
||||
wantErrContains: fmt.Sprintf("Duplicate KMS block for purpose '%s'", globals.KmsPurposeWorkerAuth),
|
||||
},
|
||||
{
|
||||
name: "duplicate worker auth storage purposes",
|
||||
purposes: []string{globals.KmsPurposeWorkerAuthStorage, globals.KmsPurposeWorkerAuthStorage},
|
||||
wantErrContains: fmt.Sprintf("Duplicate KMS block for purpose '%s'", globals.KmsPurposeWorkerAuthStorage),
|
||||
},
|
||||
{
|
||||
name: "duplicate recovery purposes",
|
||||
purposes: []string{globals.KmsPurposeRecovery, globals.KmsPurposeRecovery},
|
||||
wantErrContains: fmt.Sprintf("Duplicate KMS block for purpose '%s'", globals.KmsPurposeRecovery),
|
||||
},
|
||||
}
|
||||
logger := hclog.Default()
|
||||
serLock := new(sync.Mutex)
|
||||
@@ -102,6 +140,104 @@ func TestServer_SetupKMSes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestServer_SetupKMSes_RootMigration(t *testing.T) {
|
||||
t.Parallel()
|
||||
t.Run("correctly-pools-root-and-previous", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
logger := hclog.Default()
|
||||
serLock := new(sync.Mutex)
|
||||
conf := &configutil.SharedConfig{
|
||||
Seals: []*configutil.KMS{
|
||||
{
|
||||
Type: "aead",
|
||||
Purpose: []string{
|
||||
globals.KmsPurposeRoot,
|
||||
},
|
||||
Config: map[string]string{
|
||||
"key_id": "root",
|
||||
},
|
||||
},
|
||||
{
|
||||
Type: "aead",
|
||||
Purpose: []string{
|
||||
globals.KmsPurposePreviousRoot,
|
||||
},
|
||||
Config: map[string]string{
|
||||
"key_id": "previous_root",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
s := NewServer(&Command{Context: context.Background()})
|
||||
require.NoError(s.SetupEventing(logger, serLock, "setup-kms-testing"))
|
||||
err := s.SetupKMSes(s.Context, cli.NewMockUi(), &config.Config{SharedConfig: conf})
|
||||
require.NoError(err)
|
||||
require.NotNil(s.RootKms)
|
||||
typ, err := s.RootKms.Type(s.Context)
|
||||
require.NoError(err)
|
||||
assert.Equal(wrapping.WrapperTypePooled, typ)
|
||||
// Ensure that the root is the encryptor
|
||||
keyId, err := s.RootKms.KeyId(s.Context)
|
||||
require.NoError(err)
|
||||
assert.Equal("root", keyId)
|
||||
// Ensure that the previous root is in the wrapper too
|
||||
assert.Equal([]string{"previous_root", "root"}, s.RootKms.(*multi.PooledWrapper).AllKeyIds())
|
||||
})
|
||||
t.Run("errors-on-previous-without-root", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
require := require.New(t)
|
||||
logger := hclog.Default()
|
||||
serLock := new(sync.Mutex)
|
||||
conf := &configutil.SharedConfig{
|
||||
Seals: []*configutil.KMS{
|
||||
{
|
||||
Type: "aead",
|
||||
Purpose: []string{
|
||||
globals.KmsPurposePreviousRoot,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
s := NewServer(&Command{Context: context.Background()})
|
||||
require.NoError(s.SetupEventing(logger, serLock, "setup-kms-testing"))
|
||||
err := s.SetupKMSes(s.Context, cli.NewMockUi(), &config.Config{SharedConfig: conf})
|
||||
require.Error(err)
|
||||
})
|
||||
t.Run("errors-on-previous-and-root-with-same-key-id", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
require := require.New(t)
|
||||
logger := hclog.Default()
|
||||
serLock := new(sync.Mutex)
|
||||
conf := &configutil.SharedConfig{
|
||||
Seals: []*configutil.KMS{
|
||||
{
|
||||
Type: "aead",
|
||||
Purpose: []string{
|
||||
globals.KmsPurposeRoot,
|
||||
},
|
||||
Config: map[string]string{
|
||||
"key_id": "root",
|
||||
},
|
||||
},
|
||||
{
|
||||
Type: "aead",
|
||||
Purpose: []string{
|
||||
globals.KmsPurposePreviousRoot,
|
||||
},
|
||||
Config: map[string]string{
|
||||
"key_id": "root",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
s := NewServer(&Command{Context: context.Background()})
|
||||
require.NoError(s.SetupEventing(logger, serLock, "setup-kms-testing"))
|
||||
err := s.SetupKMSes(s.Context, cli.NewMockUi(), &config.Config{SharedConfig: conf})
|
||||
require.Error(err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestServer_SetupEventing(t *testing.T) {
|
||||
// DO NOT run these test in parallel since they have a dependency on
|
||||
// event.sysEventer
|
||||
|
||||
@@ -27,11 +27,13 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/observability/event"
|
||||
"github.com/hashicorp/boundary/internal/types/scope"
|
||||
"github.com/hashicorp/boundary/internal/util"
|
||||
kms_plugin_assets "github.com/hashicorp/boundary/plugins/kms"
|
||||
plgpb "github.com/hashicorp/boundary/sdk/pbs/plugin"
|
||||
"github.com/hashicorp/boundary/version"
|
||||
"github.com/hashicorp/go-hclog"
|
||||
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
|
||||
"github.com/hashicorp/go-kms-wrapping/v2/extras/multi"
|
||||
"github.com/hashicorp/go-multierror"
|
||||
configutil "github.com/hashicorp/go-secure-stdlib/configutil/v2"
|
||||
"github.com/hashicorp/go-secure-stdlib/gatedwriter"
|
||||
@@ -544,6 +546,7 @@ func (b *Server) SetupKMSes(ctx context.Context, ui cli.Ui, config *config.Confi
|
||||
sharedConfig := config.SharedConfig
|
||||
var pluginLogger hclog.Logger
|
||||
var err error
|
||||
var previousRootKms wrapping.Wrapper
|
||||
for _, kms := range sharedConfig.Seals {
|
||||
for _, purpose := range kms.Purpose {
|
||||
purpose = strings.ToLower(purpose)
|
||||
@@ -554,7 +557,10 @@ func (b *Server) SetupKMSes(ctx context.Context, ui cli.Ui, config *config.Confi
|
||||
if opts.withSkipWorkerAuthKmsInstantiation {
|
||||
continue
|
||||
}
|
||||
case globals.KmsPurposeRoot, globals.KmsPurposeConfig, globals.KmsPurposeWorkerAuthStorage:
|
||||
case globals.KmsPurposeRoot,
|
||||
globals.KmsPurposePreviousRoot,
|
||||
globals.KmsPurposeConfig,
|
||||
globals.KmsPurposeWorkerAuthStorage:
|
||||
case globals.KmsPurposeRecovery:
|
||||
if config.Controller != nil && config.DevRecoveryKey != "" {
|
||||
kms.Config["key"] = config.DevRecoveryKey
|
||||
@@ -615,22 +621,59 @@ func (b *Server) SetupKMSes(ctx context.Context, ui cli.Ui, config *config.Confi
|
||||
|
||||
kms.Purpose = origPurpose
|
||||
switch purpose {
|
||||
case globals.KmsPurposePreviousRoot:
|
||||
if previousRootKms != nil {
|
||||
return fmt.Errorf("Duplicate KMS block for purpose '%s'. You may need to remove all but the last KMS block for this purpose.", purpose)
|
||||
}
|
||||
previousRootKms = wrapper
|
||||
case globals.KmsPurposeRoot:
|
||||
if b.RootKms != nil {
|
||||
return fmt.Errorf("Duplicate KMS block for purpose '%s'. You may need to remove all but the last KMS block for this purpose.", purpose)
|
||||
}
|
||||
b.RootKms = wrapper
|
||||
case globals.KmsPurposeWorkerAuth:
|
||||
if b.WorkerAuthKms != nil {
|
||||
return fmt.Errorf("Duplicate KMS block for purpose '%s'. You may need to remove all but the last KMS block for this purpose.", purpose)
|
||||
}
|
||||
b.WorkerAuthKms = wrapper
|
||||
case globals.KmsPurposeWorkerAuthStorage:
|
||||
if b.WorkerAuthStorageKms != nil {
|
||||
return fmt.Errorf("Duplicate KMS block for purpose '%s'. You may need to remove all but the last KMS block for this purpose.", purpose)
|
||||
}
|
||||
b.WorkerAuthStorageKms = wrapper
|
||||
case globals.KmsPurposeRecovery:
|
||||
if b.RecoveryKms != nil {
|
||||
return fmt.Errorf("Duplicate KMS block for purpose '%s'. You may need to remove all but the last KMS block for this purpose.", purpose)
|
||||
}
|
||||
b.RecoveryKms = wrapper
|
||||
case globals.KmsPurposeConfig:
|
||||
// Do nothing, can be set in same file but not needed at runtime
|
||||
continue
|
||||
default:
|
||||
return fmt.Errorf("KMS purpose of %q is unknown", purpose)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Handle previous root KMS
|
||||
if previousRootKms != nil {
|
||||
if util.IsNil(b.RootKms) {
|
||||
return fmt.Errorf("KMS block contains '%s' without '%s'", globals.KmsPurposePreviousRoot, globals.KmsPurposeRoot)
|
||||
}
|
||||
mw, err := multi.NewPooledWrapper(ctx, previousRootKms)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create multi wrapper: %w", err)
|
||||
}
|
||||
ok, err := mw.SetEncryptingWrapper(ctx, b.RootKms)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to set root wrapper as active in multi wrapper: %w", err)
|
||||
}
|
||||
if !ok {
|
||||
return fmt.Errorf("KMS blocks with purposes '%s' and '%s' must have different key IDs", globals.KmsPurposeRoot, globals.KmsPurposePreviousRoot)
|
||||
}
|
||||
b.RootKms = mw
|
||||
}
|
||||
|
||||
// prepare a secure random reader
|
||||
b.SecureRandomReader, err = configutil.CreateSecureRandomReaderFunc(config.SharedConfig, b.RootKms)
|
||||
if err != nil {
|
||||
|
||||
@@ -88,9 +88,7 @@ func TestServer_ReloadListener(t *testing.T) {
|
||||
wd += "/test-fixtures/reload/"
|
||||
|
||||
td, err := os.MkdirTemp("", "boundary-test-")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(err)
|
||||
defer os.RemoveAll(td)
|
||||
|
||||
controllerKey := config.DevKeyGeneration()
|
||||
@@ -103,13 +101,16 @@ func TestServer_ReloadListener(t *testing.T) {
|
||||
UseDevAuthMethod: true,
|
||||
UseDevTarget: true,
|
||||
})
|
||||
// Unset auto-created KMSes that are overwritten by config on startup
|
||||
cmd.RootKms = nil
|
||||
cmd.WorkerAuthKms = nil
|
||||
cmd.RecoveryKms = nil
|
||||
|
||||
defer func() {
|
||||
if cmd.DevDatabaseCleanupFunc != nil {
|
||||
require.NoError(cmd.DevDatabaseCleanupFunc())
|
||||
}
|
||||
}()
|
||||
require.NoError(err)
|
||||
|
||||
// Setup initial certs
|
||||
inBytes, err := os.ReadFile(wd + "bundle1.pem")
|
||||
|
||||
Reference in new issue
Block a user