2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-03-13 18:11:59 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2015-07-10 22:18:02 +00:00
|
|
|
"bytes"
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2017-07-31 15:28:06 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
2016-01-15 15:55:35 +00:00
|
|
|
"crypto/rand"
|
2015-11-19 01:26:03 +00:00
|
|
|
"crypto/sha256"
|
2016-08-15 13:42:42 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2017-07-31 15:28:06 +00:00
|
|
|
"crypto/x509/pkix"
|
|
|
|
"encoding/base64"
|
2016-08-15 13:42:42 +00:00
|
|
|
"encoding/pem"
|
2018-05-20 06:42:15 +00:00
|
|
|
"errors"
|
2015-07-10 15:56:14 +00:00
|
|
|
"fmt"
|
2017-04-07 22:50:03 +00:00
|
|
|
"io"
|
2017-07-31 15:28:06 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"math/big"
|
|
|
|
mathrand "math/rand"
|
2015-07-10 22:18:02 +00:00
|
|
|
"net"
|
2016-08-15 13:42:42 +00:00
|
|
|
"net/http"
|
2017-04-07 22:50:03 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2022-11-29 19:38:33 +00:00
|
|
|
"reflect"
|
2017-05-24 00:36:20 +00:00
|
|
|
"sync"
|
2018-09-18 03:03:00 +00:00
|
|
|
"sync/atomic"
|
2015-09-02 19:56:58 +00:00
|
|
|
"time"
|
2015-03-13 18:11:59 +00:00
|
|
|
|
2019-09-17 00:50:51 +00:00
|
|
|
"github.com/armon/go-metrics"
|
2021-04-20 22:25:04 +00:00
|
|
|
"github.com/hashicorp/go-cleanhttp"
|
2020-06-16 18:12:22 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2021-07-16 00:17:31 +00:00
|
|
|
"github.com/hashicorp/go-secure-stdlib/reloadutil"
|
2020-06-16 18:12:22 +00:00
|
|
|
raftlib "github.com/hashicorp/raft"
|
2017-02-24 15:45:29 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
2015-04-02 01:36:13 +00:00
|
|
|
"github.com/hashicorp/vault/audit"
|
2022-12-01 10:44:44 +00:00
|
|
|
auditFile "github.com/hashicorp/vault/builtin/audit/file"
|
2019-10-08 17:57:15 +00:00
|
|
|
"github.com/hashicorp/vault/command/server"
|
2022-11-29 19:38:33 +00:00
|
|
|
"github.com/hashicorp/vault/helper/constants"
|
2023-02-03 21:24:16 +00:00
|
|
|
"github.com/hashicorp/vault/helper/experiments"
|
2020-10-13 23:38:21 +00:00
|
|
|
"github.com/hashicorp/vault/helper/metricsutil"
|
2019-02-14 19:55:32 +00:00
|
|
|
"github.com/hashicorp/vault/helper/namespace"
|
2023-02-01 13:33:16 +00:00
|
|
|
"github.com/hashicorp/vault/helper/testhelpers/corehelpers"
|
2023-02-03 22:27:11 +00:00
|
|
|
"github.com/hashicorp/vault/helper/testhelpers/pluginhelpers"
|
2020-10-13 23:38:21 +00:00
|
|
|
"github.com/hashicorp/vault/internalshared/configutil"
|
2022-08-30 02:42:26 +00:00
|
|
|
v5 "github.com/hashicorp/vault/sdk/database/dbplugin/v5"
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/framework"
|
|
|
|
"github.com/hashicorp/vault/sdk/helper/consts"
|
|
|
|
"github.com/hashicorp/vault/sdk/helper/logging"
|
2022-08-30 02:42:26 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/pluginutil"
|
2023-04-24 13:57:37 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/testcluster"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
|
|
|
"github.com/hashicorp/vault/sdk/physical"
|
|
|
|
physInmem "github.com/hashicorp/vault/sdk/physical/inmem"
|
2022-08-30 02:42:26 +00:00
|
|
|
backendplugin "github.com/hashicorp/vault/sdk/plugin"
|
2020-06-16 18:12:22 +00:00
|
|
|
"github.com/hashicorp/vault/vault/cluster"
|
|
|
|
"github.com/hashicorp/vault/vault/seal"
|
2020-06-23 19:04:13 +00:00
|
|
|
"github.com/mitchellh/copystructure"
|
2021-04-20 22:25:04 +00:00
|
|
|
"github.com/mitchellh/go-testing-interface"
|
2020-06-23 19:04:13 +00:00
|
|
|
"golang.org/x/crypto/ed25519"
|
|
|
|
"golang.org/x/net/http2"
|
2015-03-13 18:11:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// This file contains a number of methods that are useful for unit
|
|
|
|
// tests within other packages.
|
|
|
|
|
2015-07-10 22:18:02 +00:00
|
|
|
const (
|
|
|
|
testSharedPublicKey = `
|
2015-07-10 23:27:21 +00:00
|
|
|
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC9i+hFxZHGo6KblVme4zrAcJstR6I0PTJozW286X4WyvPnkMYDQ5mnhEYC7UWCvjoTWbPEXPX7NjhRtwQTGD67bV+lrxgfyzK1JZbUXK4PwgKJvQD+XyyWYMzDgGSQY61KUSqCxymSm/9NZkPU3ElaQ9xQuTzPpztM4ROfb8f2Yv6/ZESZsTo0MTAkp8Pcy+WkioI/uJ1H7zqs0EA4OMY4aDJRu0UtP4rTVeYNEAuRXdX+eH4aW3KMvhzpFTjMbaJHJXlEeUm2SaX5TNQyTOvghCeQILfYIL/Ca2ij8iwCmulwdV6eQGfd4VDu40PvSnmfoaE38o6HaPnX0kUcnKiT
|
2015-07-10 22:18:02 +00:00
|
|
|
`
|
|
|
|
testSharedPrivateKey = `
|
|
|
|
-----BEGIN RSA PRIVATE KEY-----
|
|
|
|
MIIEogIBAAKCAQEAvYvoRcWRxqOim5VZnuM6wHCbLUeiND0yaM1tvOl+Fsrz55DG
|
|
|
|
A0OZp4RGAu1Fgr46E1mzxFz1+zY4UbcEExg+u21fpa8YH8sytSWW1FyuD8ICib0A
|
|
|
|
/l8slmDMw4BkkGOtSlEqgscpkpv/TWZD1NxJWkPcULk8z6c7TOETn2/H9mL+v2RE
|
|
|
|
mbE6NDEwJKfD3MvlpIqCP7idR+86rNBAODjGOGgyUbtFLT+K01XmDRALkV3V/nh+
|
|
|
|
GltyjL4c6RU4zG2iRyV5RHlJtkml+UzUMkzr4IQnkCC32CC/wmtoo/IsAprpcHVe
|
|
|
|
nkBn3eFQ7uND70p5n6GhN/KOh2j519JFHJyokwIDAQABAoIBAHX7VOvBC3kCN9/x
|
|
|
|
+aPdup84OE7Z7MvpX6w+WlUhXVugnmsAAVDczhKoUc/WktLLx2huCGhsmKvyVuH+
|
|
|
|
MioUiE+vx75gm3qGx5xbtmOfALVMRLopjCnJYf6EaFA0ZeQ+NwowNW7Lu0PHmAU8
|
|
|
|
Z3JiX8IwxTz14DU82buDyewO7v+cEr97AnERe3PUcSTDoUXNaoNxjNpEJkKREY6h
|
|
|
|
4hAY676RT/GsRcQ8tqe/rnCqPHNd7JGqL+207FK4tJw7daoBjQyijWuB7K5chSal
|
|
|
|
oPInylM6b13ASXuOAOT/2uSUBWmFVCZPDCmnZxy2SdnJGbsJAMl7Ma3MUlaGvVI+
|
|
|
|
Tfh1aQkCgYEA4JlNOabTb3z42wz6mz+Nz3JRwbawD+PJXOk5JsSnV7DtPtfgkK9y
|
|
|
|
6FTQdhnozGWShAvJvc+C4QAihs9AlHXoaBY5bEU7R/8UK/pSqwzam+MmxmhVDV7G
|
|
|
|
IMQPV0FteoXTaJSikhZ88mETTegI2mik+zleBpVxvfdhE5TR+lq8Br0CgYEA2AwJ
|
|
|
|
CUD5CYUSj09PluR0HHqamWOrJkKPFPwa+5eiTTCzfBBxImYZh7nXnWuoviXC0sg2
|
|
|
|
AuvCW+uZ48ygv/D8gcz3j1JfbErKZJuV+TotK9rRtNIF5Ub7qysP7UjyI7zCssVM
|
|
|
|
kuDd9LfRXaB/qGAHNkcDA8NxmHW3gpln4CFdSY8CgYANs4xwfercHEWaJ1qKagAe
|
|
|
|
rZyrMpffAEhicJ/Z65lB0jtG4CiE6w8ZeUMWUVJQVcnwYD+4YpZbX4S7sJ0B8Ydy
|
|
|
|
AhkSr86D/92dKTIt2STk6aCN7gNyQ1vW198PtaAWH1/cO2UHgHOy3ZUt5X/Uwxl9
|
|
|
|
cex4flln+1Viumts2GgsCQKBgCJH7psgSyPekK5auFdKEr5+Gc/jB8I/Z3K9+g4X
|
|
|
|
5nH3G1PBTCJYLw7hRzw8W/8oALzvddqKzEFHphiGXK94Lqjt/A4q1OdbCrhiE68D
|
|
|
|
My21P/dAKB1UYRSs9Y8CNyHCjuZM9jSMJ8vv6vG/SOJPsnVDWVAckAbQDvlTHC9t
|
|
|
|
O98zAoGAcbW6uFDkrv0XMCpB9Su3KaNXOR0wzag+WIFQRXCcoTvxVi9iYfUReQPi
|
|
|
|
oOyBJU/HMVvBfv4g+OVFLVgSwwm6owwsouZ0+D/LasbuHqYyqYqdyPJQYzWA2Y+F
|
|
|
|
+B6f4RoPdSXj24JHPg/ioRxjaj094UXJxua2yfkcecGNEuBQHSs=
|
|
|
|
-----END RSA PRIVATE KEY-----
|
|
|
|
`
|
|
|
|
)
|
|
|
|
|
2015-03-13 18:11:59 +00:00
|
|
|
// TestCore returns a pure in-memory, uninitialized core for testing.
|
2021-02-18 20:40:18 +00:00
|
|
|
func TestCore(t testing.T) *Core {
|
2017-09-15 04:21:35 +00:00
|
|
|
return TestCoreWithSeal(t, nil, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestCoreRaw returns a pure in-memory, uninitialized core for testing. The raw
|
|
|
|
// storage endpoints are enabled with this core.
|
2021-02-18 20:40:18 +00:00
|
|
|
func TestCoreRaw(t testing.T) *Core {
|
2017-09-15 04:21:35 +00:00
|
|
|
return TestCoreWithSeal(t, nil, true)
|
2016-04-25 19:39:04 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 13:52:49 +00:00
|
|
|
// TestCoreNewSeal returns a pure in-memory, uninitialized core with
|
|
|
|
// the new seal configuration.
|
2021-02-18 20:40:18 +00:00
|
|
|
func TestCoreNewSeal(t testing.T) *Core {
|
2017-10-23 17:42:04 +00:00
|
|
|
seal := NewTestSeal(t, nil)
|
|
|
|
return TestCoreWithSeal(t, seal, false)
|
2017-01-12 00:55:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
// TestCoreWithConfig returns a pure in-memory, uninitialized core with the
|
2018-11-13 16:16:10 +00:00
|
|
|
// specified core configurations overridden for testing.
|
2021-02-18 20:40:18 +00:00
|
|
|
func TestCoreWithConfig(t testing.T, conf *CoreConfig) *Core {
|
2018-09-18 03:03:00 +00:00
|
|
|
return TestCoreWithSealAndUI(t, conf)
|
|
|
|
}
|
|
|
|
|
2016-04-25 19:39:04 +00:00
|
|
|
// TestCoreWithSeal returns a pure in-memory, uninitialized core with the
|
|
|
|
// specified seal for testing.
|
2021-02-18 20:40:18 +00:00
|
|
|
func TestCoreWithSeal(t testing.T, testSeal Seal, enableRaw bool) *Core {
|
2018-09-18 03:03:00 +00:00
|
|
|
conf := &CoreConfig{
|
2018-11-07 01:21:24 +00:00
|
|
|
Seal: testSeal,
|
|
|
|
EnableUI: false,
|
|
|
|
EnableRaw: enableRaw,
|
2023-02-01 13:33:16 +00:00
|
|
|
BuiltinRegistry: corehelpers.NewMockBuiltinRegistry(),
|
2022-12-01 10:44:44 +00:00
|
|
|
AuditBackends: map[string]audit.Factory{
|
|
|
|
"file": auditFile.Factory,
|
|
|
|
},
|
2018-09-18 03:03:00 +00:00
|
|
|
}
|
|
|
|
return TestCoreWithSealAndUI(t, conf)
|
|
|
|
}
|
|
|
|
|
2021-10-13 15:06:33 +00:00
|
|
|
func TestCoreWithCustomResponseHeaderAndUI(t testing.T, CustomResponseHeaders map[string]map[string]string, enableUI bool) (*Core, [][]byte, string) {
|
|
|
|
confRaw := &server.Config{
|
|
|
|
SharedConfig: &configutil.SharedConfig{
|
|
|
|
Listeners: []*configutil.Listener{
|
|
|
|
{
|
|
|
|
Type: "tcp",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
CustomResponseHeaders: CustomResponseHeaders,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DisableMlock: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
conf := &CoreConfig{
|
|
|
|
RawConfig: confRaw,
|
|
|
|
EnableUI: enableUI,
|
|
|
|
EnableRaw: true,
|
2023-02-01 13:33:16 +00:00
|
|
|
BuiltinRegistry: corehelpers.NewMockBuiltinRegistry(),
|
2021-10-13 15:06:33 +00:00
|
|
|
}
|
|
|
|
core := TestCoreWithSealAndUI(t, conf)
|
|
|
|
return testCoreUnsealed(t, core)
|
|
|
|
}
|
|
|
|
|
2021-02-18 20:40:18 +00:00
|
|
|
func TestCoreUI(t testing.T, enableUI bool) *Core {
|
2018-09-18 03:03:00 +00:00
|
|
|
conf := &CoreConfig{
|
2018-11-07 01:21:24 +00:00
|
|
|
EnableUI: enableUI,
|
|
|
|
EnableRaw: true,
|
2023-02-01 13:33:16 +00:00
|
|
|
BuiltinRegistry: corehelpers.NewMockBuiltinRegistry(),
|
2018-09-18 03:03:00 +00:00
|
|
|
}
|
|
|
|
return TestCoreWithSealAndUI(t, conf)
|
|
|
|
}
|
|
|
|
|
2021-02-18 20:40:18 +00:00
|
|
|
func TestCoreWithSealAndUI(t testing.T, opts *CoreConfig) *Core {
|
2022-02-23 15:33:52 +00:00
|
|
|
c := TestCoreWithSealAndUINoCleanup(t, opts)
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
t.Log("panic closing core during cleanup", "panic", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
err := c.ShutdownWait()
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("shutdown returned error: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCoreWithSealAndUINoCleanup(t testing.T, opts *CoreConfig) *Core {
|
2018-04-03 00:46:59 +00:00
|
|
|
logger := logging.NewVaultLogger(log.Trace)
|
2017-08-03 17:24:27 +00:00
|
|
|
physicalBackend, err := physInmem.NewInmem(nil, logger)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-02-16 18:16:06 +00:00
|
|
|
|
2019-08-06 19:21:23 +00:00
|
|
|
errInjector := physical.NewErrorInjector(physicalBackend, 0, logger)
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
// Start off with base test core config
|
2019-08-06 19:21:23 +00:00
|
|
|
conf := testCoreConfig(t, errInjector, logger)
|
2017-02-16 18:16:06 +00:00
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
// Override config values with ones that gets passed in
|
|
|
|
conf.EnableUI = opts.EnableUI
|
|
|
|
conf.EnableRaw = opts.EnableRaw
|
2022-12-15 20:19:19 +00:00
|
|
|
conf.EnableIntrospection = opts.EnableIntrospection
|
2018-09-18 03:03:00 +00:00
|
|
|
conf.Seal = opts.Seal
|
|
|
|
conf.LicensingConfig = opts.LicensingConfig
|
2018-11-19 21:13:16 +00:00
|
|
|
conf.DisableKeyEncodingChecks = opts.DisableKeyEncodingChecks
|
2019-10-04 07:29:51 +00:00
|
|
|
conf.MetricsHelper = opts.MetricsHelper
|
2020-07-22 17:52:10 +00:00
|
|
|
conf.MetricSink = opts.MetricSink
|
Vault-1403 Switch Expiration Manager to use Fairsharing Backpressure (#1709) (#10932)
* basic pool and start testing
* refactor a bit for testing
* workFunc, start/stop safety, testing
* cleanup function for worker quit, more tests
* redo public/private members
* improve tests, export types, switch uuid package
* fix loop capture bug, cleanup
* cleanup tests
* update worker pool file name, other improvements
* add job manager prototype
* remove remnants
* add functions to wait for job manager and worker pool to stop, other fixes
* test job manager functionality, fix bugs
* encapsulate how jobs are distributed to workers
* make worker job channel read only
* add job interface, more testing, fixes
* set name for dispatcher
* fix test races
* wire up expiration manager most of the way
* dispatcher and job manager constructors don't return errors
* logger now dependency injected
* make some members private, test fcn to get worker pool size
* make GetNumWorkers public
* Update helper/fairshare/jobmanager_test.go
Co-authored-by: Brian Kassouf <briankassouf@users.noreply.github.com>
* update fairsharing usage, add tests
* make workerpool private
* remove custom worker names
* concurrency improvements
* remove worker pool cleanup function
* remove cleanup func from job manager, remove non blocking stop from fairshare
* update job manager for new constructor
* stop job manager when expiration manager stopped
* unset env var after test
* stop fairshare when started in tests
* stop leaking job manager goroutine
* prototype channel for waking up to assign work
* fix typo/bug and add tests
* improve job manager wake up, fix test typo
* put channel drain back
* better start/pause test for job manager
* comment cleanup
* degrade possible noisy log
* remove closure, clean up context
* improve revocation context timer
* test: reduce number of revocation workers during many tests
* Update vault/expiration.go
Co-authored-by: Brian Kassouf <briankassouf@users.noreply.github.com>
* feedback tweaks
Co-authored-by: Brian Kassouf <briankassouf@users.noreply.github.com>
Co-authored-by: Brian Kassouf <briankassouf@users.noreply.github.com>
2021-02-17 22:30:27 +00:00
|
|
|
conf.NumExpirationWorkers = numExpirationWorkersTest
|
2021-04-06 23:40:43 +00:00
|
|
|
conf.RawConfig = opts.RawConfig
|
2021-04-20 22:25:04 +00:00
|
|
|
conf.EnableResponseHeaderHostname = opts.EnableResponseHeaderHostname
|
2022-02-17 19:43:07 +00:00
|
|
|
conf.DisableSSCTokens = opts.DisableSSCTokens
|
2022-04-14 20:54:23 +00:00
|
|
|
conf.PluginDirectory = opts.PluginDirectory
|
2023-01-11 19:32:05 +00:00
|
|
|
conf.DetectDeadlocks = opts.DetectDeadlocks
|
2023-02-03 21:24:16 +00:00
|
|
|
conf.Experiments = []string{experiments.VaultExperimentEventsAlpha1}
|
2023-04-21 19:29:37 +00:00
|
|
|
conf.CensusAgent = opts.CensusAgent
|
2017-02-16 18:16:06 +00:00
|
|
|
|
2019-03-08 21:46:24 +00:00
|
|
|
if opts.Logger != nil {
|
|
|
|
conf.Logger = opts.Logger
|
|
|
|
}
|
|
|
|
|
2021-11-10 18:35:31 +00:00
|
|
|
if opts.RedirectAddr != "" {
|
|
|
|
conf.RedirectAddr = opts.RedirectAddr
|
|
|
|
}
|
|
|
|
|
2018-12-12 01:21:23 +00:00
|
|
|
for k, v := range opts.LogicalBackends {
|
|
|
|
conf.LogicalBackends[k] = v
|
|
|
|
}
|
|
|
|
for k, v := range opts.CredentialBackends {
|
|
|
|
conf.CredentialBackends[k] = v
|
|
|
|
}
|
|
|
|
|
2019-03-08 21:46:24 +00:00
|
|
|
for k, v := range opts.AuditBackends {
|
|
|
|
conf.AuditBackends[k] = v
|
|
|
|
}
|
|
|
|
|
2022-09-07 13:06:15 +00:00
|
|
|
conf.ActivityLogConfig = opts.ActivityLogConfig
|
2023-03-31 15:05:16 +00:00
|
|
|
testApplyEntBaseConfig(conf, opts)
|
2022-09-07 13:06:15 +00:00
|
|
|
|
2017-02-16 18:16:06 +00:00
|
|
|
c, err := NewCore(conf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-02-18 20:40:18 +00:00
|
|
|
func testCoreConfig(t testing.T, physicalBackend physical.Backend, logger log.Logger) *CoreConfig {
|
2017-10-23 18:59:37 +00:00
|
|
|
t.Helper()
|
2015-04-02 01:36:13 +00:00
|
|
|
noopAudits := map[string]audit.Factory{
|
2023-02-01 13:33:16 +00:00
|
|
|
"noop": corehelpers.NoopAuditFactory(nil),
|
2015-04-02 01:36:13 +00:00
|
|
|
}
|
2017-10-23 18:59:37 +00:00
|
|
|
|
2015-04-01 03:24:51 +00:00
|
|
|
noopBackends := make(map[string]logical.Factory)
|
2018-01-19 06:44:44 +00:00
|
|
|
noopBackends["noop"] = func(ctx context.Context, config *logical.BackendConfig) (logical.Backend, error) {
|
2015-09-04 20:58:12 +00:00
|
|
|
b := new(framework.Backend)
|
2018-01-19 06:44:44 +00:00
|
|
|
b.Setup(ctx, config)
|
2018-11-07 01:21:24 +00:00
|
|
|
b.BackendType = logical.TypeCredential
|
2015-09-04 20:58:12 +00:00
|
|
|
return b, nil
|
2015-04-01 03:24:51 +00:00
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
noopBackends["http"] = func(ctx context.Context, config *logical.BackendConfig) (logical.Backend, error) {
|
2015-05-27 21:19:12 +00:00
|
|
|
return new(rawHTTP), nil
|
|
|
|
}
|
2017-10-11 17:21:20 +00:00
|
|
|
|
|
|
|
credentialBackends := make(map[string]logical.Factory)
|
|
|
|
for backendName, backendFactory := range noopBackends {
|
|
|
|
credentialBackends[backendName] = backendFactory
|
|
|
|
}
|
|
|
|
for backendName, backendFactory := range testCredentialBackends {
|
|
|
|
credentialBackends[backendName] = backendFactory
|
|
|
|
}
|
|
|
|
|
2015-07-10 15:56:14 +00:00
|
|
|
logicalBackends := make(map[string]logical.Factory)
|
|
|
|
for backendName, backendFactory := range noopBackends {
|
|
|
|
logicalBackends[backendName] = backendFactory
|
|
|
|
}
|
2017-10-23 18:59:37 +00:00
|
|
|
|
2017-09-15 13:02:29 +00:00
|
|
|
logicalBackends["kv"] = LeasedPassthroughBackendFactory
|
2015-07-10 15:56:14 +00:00
|
|
|
for backendName, backendFactory := range testLogicalBackends {
|
|
|
|
logicalBackends[backendName] = backendFactory
|
|
|
|
}
|
2015-04-01 03:24:51 +00:00
|
|
|
|
2016-04-25 19:39:04 +00:00
|
|
|
conf := &CoreConfig{
|
2015-04-01 03:24:51 +00:00
|
|
|
Physical: physicalBackend,
|
2015-04-02 01:36:13 +00:00
|
|
|
AuditBackends: noopAudits,
|
2015-07-10 15:56:14 +00:00
|
|
|
LogicalBackends: logicalBackends,
|
2017-10-11 17:21:20 +00:00
|
|
|
CredentialBackends: credentialBackends,
|
2015-04-29 01:12:57 +00:00
|
|
|
DisableMlock: true,
|
2016-04-26 03:10:32 +00:00
|
|
|
Logger: logger,
|
2023-02-01 13:33:16 +00:00
|
|
|
BuiltinRegistry: corehelpers.NewMockBuiltinRegistry(),
|
2016-04-25 19:39:04 +00:00
|
|
|
}
|
|
|
|
|
2017-02-16 18:16:06 +00:00
|
|
|
return conf
|
2015-03-13 18:11:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestCoreInit initializes the core with a single key, and returns
|
2015-03-24 18:37:07 +00:00
|
|
|
// the key that must be used to unseal the core and a root token.
|
2021-02-18 20:40:18 +00:00
|
|
|
func TestCoreInit(t testing.T, core *Core) ([][]byte, string) {
|
2017-10-23 18:59:37 +00:00
|
|
|
t.Helper()
|
2019-04-04 17:02:44 +00:00
|
|
|
secretShares, _, root := TestCoreInitClusterWrapperSetup(t, core, nil)
|
2017-10-23 18:59:37 +00:00
|
|
|
return secretShares, root
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 20:40:18 +00:00
|
|
|
func TestCoreInitClusterWrapperSetup(t testing.T, core *Core, handler http.Handler) ([][]byte, [][]byte, string) {
|
2017-10-23 18:59:37 +00:00
|
|
|
t.Helper()
|
2017-05-24 14:38:48 +00:00
|
|
|
core.SetClusterHandler(handler)
|
2017-10-23 18:59:37 +00:00
|
|
|
|
|
|
|
barrierConfig := &SealConfig{
|
|
|
|
SecretShares: 3,
|
|
|
|
SecretThreshold: 3,
|
|
|
|
}
|
|
|
|
|
2019-10-18 18:46:00 +00:00
|
|
|
switch core.seal.StoredKeysSupported() {
|
2020-01-11 01:39:52 +00:00
|
|
|
case seal.StoredKeysNotSupported:
|
2019-10-18 18:46:00 +00:00
|
|
|
barrierConfig.StoredShares = 0
|
|
|
|
default:
|
|
|
|
barrierConfig.StoredShares = 1
|
2017-10-23 18:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
recoveryConfig := &SealConfig{
|
|
|
|
SecretShares: 3,
|
|
|
|
SecretThreshold: 3,
|
|
|
|
}
|
|
|
|
|
2019-10-18 18:46:00 +00:00
|
|
|
initParams := &InitParams{
|
2017-10-23 18:59:37 +00:00
|
|
|
BarrierConfig: barrierConfig,
|
|
|
|
RecoveryConfig: recoveryConfig,
|
2019-10-18 18:46:00 +00:00
|
|
|
}
|
2020-01-11 01:39:52 +00:00
|
|
|
if core.seal.StoredKeysSupported() == seal.StoredKeysNotSupported {
|
2019-10-18 18:46:00 +00:00
|
|
|
initParams.LegacyShamirSeal = true
|
|
|
|
}
|
|
|
|
result, err := core.Initialize(context.Background(), initParams)
|
2015-03-13 18:11:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2022-02-17 19:43:07 +00:00
|
|
|
innerToken, err := core.DecodeSSCToken(result.RootToken)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
return result.SecretShares, result.RecoveryShares, innerToken
|
2015-03-15 00:47:11 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 13:42:42 +00:00
|
|
|
func TestCoreUnseal(core *Core, key []byte) (bool, error) {
|
|
|
|
return core.Unseal(key)
|
|
|
|
}
|
|
|
|
|
2022-09-06 18:11:04 +00:00
|
|
|
func TestCoreSeal(core *Core) error {
|
|
|
|
return core.sealInternal()
|
|
|
|
}
|
|
|
|
|
2015-03-15 00:47:11 +00:00
|
|
|
// TestCoreUnsealed returns a pure in-memory core that is already
|
|
|
|
// initialized and unsealed.
|
2021-02-18 20:40:18 +00:00
|
|
|
func TestCoreUnsealed(t testing.T) (*Core, [][]byte, string) {
|
2017-10-23 18:59:37 +00:00
|
|
|
t.Helper()
|
2015-03-15 00:47:11 +00:00
|
|
|
core := TestCore(t)
|
2017-09-15 04:21:35 +00:00
|
|
|
return testCoreUnsealed(t, core)
|
|
|
|
}
|
|
|
|
|
2022-09-07 13:06:15 +00:00
|
|
|
func SetupMetrics(conf *CoreConfig) *metrics.InmemSink {
|
|
|
|
inmemSink := metrics.NewInmemSink(1000000*time.Hour, 2000000*time.Hour)
|
|
|
|
conf.MetricSink = metricsutil.NewClusterMetricSink("test-cluster", inmemSink)
|
|
|
|
conf.MetricsHelper = metricsutil.NewMetricsHelper(inmemSink, false)
|
|
|
|
return inmemSink
|
|
|
|
}
|
|
|
|
|
2021-02-18 20:40:18 +00:00
|
|
|
func TestCoreUnsealedWithMetrics(t testing.T) (*Core, [][]byte, string, *metrics.InmemSink) {
|
2020-07-22 17:52:10 +00:00
|
|
|
t.Helper()
|
|
|
|
conf := &CoreConfig{
|
2023-02-01 13:33:16 +00:00
|
|
|
BuiltinRegistry: corehelpers.NewMockBuiltinRegistry(),
|
2020-07-22 17:52:10 +00:00
|
|
|
}
|
2022-09-07 13:06:15 +00:00
|
|
|
sink := SetupMetrics(conf)
|
2020-07-22 17:52:10 +00:00
|
|
|
core, keys, root := testCoreUnsealed(t, TestCoreWithSealAndUI(t, conf))
|
2022-09-07 13:06:15 +00:00
|
|
|
return core, keys, root, sink
|
2020-07-22 17:52:10 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 04:21:35 +00:00
|
|
|
// TestCoreUnsealedRaw returns a pure in-memory core that is already
|
|
|
|
// initialized, unsealed, and with raw endpoints enabled.
|
2021-02-18 20:40:18 +00:00
|
|
|
func TestCoreUnsealedRaw(t testing.T) (*Core, [][]byte, string) {
|
2017-10-23 18:59:37 +00:00
|
|
|
t.Helper()
|
2017-09-15 04:21:35 +00:00
|
|
|
core := TestCoreRaw(t)
|
|
|
|
return testCoreUnsealed(t, core)
|
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
// TestCoreUnsealedWithConfig returns a pure in-memory core that is already
|
2018-11-13 16:16:10 +00:00
|
|
|
// initialized, unsealed, with the any provided core config values overridden.
|
2021-02-18 20:40:18 +00:00
|
|
|
func TestCoreUnsealedWithConfig(t testing.T, conf *CoreConfig) (*Core, [][]byte, string) {
|
2018-09-18 03:03:00 +00:00
|
|
|
t.Helper()
|
|
|
|
core := TestCoreWithConfig(t, conf)
|
|
|
|
return testCoreUnsealed(t, core)
|
|
|
|
}
|
|
|
|
|
2021-02-18 20:40:18 +00:00
|
|
|
func testCoreUnsealed(t testing.T, core *Core) (*Core, [][]byte, string) {
|
2017-10-23 18:59:37 +00:00
|
|
|
t.Helper()
|
2022-09-06 18:11:04 +00:00
|
|
|
token, keys := TestInitUnsealCore(t, core)
|
|
|
|
|
|
|
|
testCoreAddSecretMount(t, core, token)
|
|
|
|
return core, keys, token
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInitUnsealCore(t testing.T, core *Core) (string, [][]byte) {
|
2017-01-17 20:43:10 +00:00
|
|
|
keys, token := TestCoreInit(t, core)
|
|
|
|
for _, key := range keys {
|
|
|
|
if _, err := TestCoreUnseal(core, TestKeyCopy(key)); err != nil {
|
|
|
|
t.Fatalf("unseal err: %s", err)
|
|
|
|
}
|
2015-03-15 00:47:11 +00:00
|
|
|
}
|
2018-07-24 20:57:25 +00:00
|
|
|
if core.Sealed() {
|
2015-03-15 00:47:11 +00:00
|
|
|
t.Fatal("should not be sealed")
|
|
|
|
}
|
|
|
|
|
2022-09-06 18:11:04 +00:00
|
|
|
return token, keys
|
2015-03-13 18:11:59 +00:00
|
|
|
}
|
2015-03-15 01:25:36 +00:00
|
|
|
|
2021-02-18 20:40:18 +00:00
|
|
|
func testCoreAddSecretMount(t testing.T, core *Core, token string) {
|
2019-02-14 19:55:32 +00:00
|
|
|
kvReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
ClientToken: token,
|
|
|
|
Path: "sys/mounts/secret",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"type": "kv",
|
|
|
|
"path": "secret/",
|
|
|
|
"description": "key/value secret storage",
|
|
|
|
"options": map[string]string{
|
|
|
|
"version": "1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := core.HandleRequest(namespace.RootContext(nil), kvReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp.IsError() {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 20:40:18 +00:00
|
|
|
func TestCoreUnsealedBackend(t testing.T, backend physical.Backend) (*Core, [][]byte, string) {
|
2017-10-23 18:59:37 +00:00
|
|
|
t.Helper()
|
2018-04-03 00:46:59 +00:00
|
|
|
logger := logging.NewVaultLogger(log.Trace)
|
2017-02-16 18:16:06 +00:00
|
|
|
conf := testCoreConfig(t, backend, logger)
|
2017-10-23 17:42:04 +00:00
|
|
|
conf.Seal = NewTestSeal(t, nil)
|
Vault-1403 Switch Expiration Manager to use Fairsharing Backpressure (#1709) (#10932)
* basic pool and start testing
* refactor a bit for testing
* workFunc, start/stop safety, testing
* cleanup function for worker quit, more tests
* redo public/private members
* improve tests, export types, switch uuid package
* fix loop capture bug, cleanup
* cleanup tests
* update worker pool file name, other improvements
* add job manager prototype
* remove remnants
* add functions to wait for job manager and worker pool to stop, other fixes
* test job manager functionality, fix bugs
* encapsulate how jobs are distributed to workers
* make worker job channel read only
* add job interface, more testing, fixes
* set name for dispatcher
* fix test races
* wire up expiration manager most of the way
* dispatcher and job manager constructors don't return errors
* logger now dependency injected
* make some members private, test fcn to get worker pool size
* make GetNumWorkers public
* Update helper/fairshare/jobmanager_test.go
Co-authored-by: Brian Kassouf <briankassouf@users.noreply.github.com>
* update fairsharing usage, add tests
* make workerpool private
* remove custom worker names
* concurrency improvements
* remove worker pool cleanup function
* remove cleanup func from job manager, remove non blocking stop from fairshare
* update job manager for new constructor
* stop job manager when expiration manager stopped
* unset env var after test
* stop fairshare when started in tests
* stop leaking job manager goroutine
* prototype channel for waking up to assign work
* fix typo/bug and add tests
* improve job manager wake up, fix test typo
* put channel drain back
* better start/pause test for job manager
* comment cleanup
* degrade possible noisy log
* remove closure, clean up context
* improve revocation context timer
* test: reduce number of revocation workers during many tests
* Update vault/expiration.go
Co-authored-by: Brian Kassouf <briankassouf@users.noreply.github.com>
* feedback tweaks
Co-authored-by: Brian Kassouf <briankassouf@users.noreply.github.com>
Co-authored-by: Brian Kassouf <briankassouf@users.noreply.github.com>
2021-02-17 22:30:27 +00:00
|
|
|
conf.NumExpirationWorkers = numExpirationWorkersTest
|
2017-02-16 18:16:06 +00:00
|
|
|
|
|
|
|
core, err := NewCore(conf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2016-01-09 02:21:02 +00:00
|
|
|
|
2017-02-16 18:16:06 +00:00
|
|
|
keys, token := TestCoreInit(t, core)
|
|
|
|
for _, key := range keys {
|
|
|
|
if _, err := TestCoreUnseal(core, TestKeyCopy(key)); err != nil {
|
|
|
|
t.Fatalf("unseal err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := core.UnsealWithStoredKeys(context.Background()); err != nil {
|
2017-10-23 18:59:37 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-07-24 20:57:25 +00:00
|
|
|
if core.Sealed() {
|
2017-02-16 18:16:06 +00:00
|
|
|
t.Fatal("should not be sealed")
|
|
|
|
}
|
|
|
|
|
2021-02-12 20:04:48 +00:00
|
|
|
t.Cleanup(func() {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
t.Log("panic closing core during cleanup", "panic", r)
|
|
|
|
}
|
|
|
|
}()
|
2022-02-23 15:33:52 +00:00
|
|
|
err := core.ShutdownWait()
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("shutdown returned error: %v", err)
|
|
|
|
}
|
2021-02-12 20:04:48 +00:00
|
|
|
})
|
|
|
|
|
2017-02-16 18:16:06 +00:00
|
|
|
return core, keys, token
|
|
|
|
}
|
|
|
|
|
2015-03-15 01:25:36 +00:00
|
|
|
// TestKeyCopy is a silly little function to just copy the key so that
|
|
|
|
// it can be used with Unseal easily.
|
|
|
|
func TestKeyCopy(key []byte) []byte {
|
|
|
|
result := make([]byte, len(key))
|
|
|
|
copy(result, key)
|
|
|
|
return result
|
|
|
|
}
|
2015-04-02 01:36:13 +00:00
|
|
|
|
2021-09-27 16:08:07 +00:00
|
|
|
func TestDynamicSystemView(c *Core, ns *namespace.Namespace) *dynamicSystemView {
|
2017-04-07 22:50:03 +00:00
|
|
|
me := &MountEntry{
|
|
|
|
Config: MountConfig{
|
|
|
|
DefaultLeaseTTL: 24 * time.Hour,
|
|
|
|
MaxLeaseTTL: 2 * 24 * time.Hour,
|
|
|
|
},
|
2021-09-27 16:08:07 +00:00
|
|
|
NamespaceID: namespace.RootNamespace.ID,
|
|
|
|
namespace: namespace.RootNamespace,
|
|
|
|
}
|
|
|
|
|
|
|
|
if ns != nil {
|
|
|
|
me.NamespaceID = ns.ID
|
|
|
|
me.namespace = ns
|
2017-04-07 22:50:03 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 12:56:36 +00:00
|
|
|
return &dynamicSystemView{c, me, c.perfStandby}
|
2017-04-07 22:50:03 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 18:31:57 +00:00
|
|
|
// TestAddTestPlugin registers the testFunc as part of the plugin command to the
|
2018-09-20 17:50:29 +00:00
|
|
|
// plugin catalog. If provided, uses tmpDir as the plugin directory.
|
2022-10-05 08:29:29 +00:00
|
|
|
// NB: The test func you pass in MUST be in the same package as the parent test,
|
|
|
|
// or the test func won't be compiled into the test binary being run and the output
|
|
|
|
// will be something like:
|
|
|
|
// stderr (ignored by go-plugin): "testing: warning: no tests to run"
|
|
|
|
// stdout: "PASS"
|
2022-09-09 16:32:28 +00:00
|
|
|
func TestAddTestPlugin(t testing.T, c *Core, name string, pluginType consts.PluginType, version string, testFunc string, env []string, tempDir string) {
|
2017-04-10 21:12:28 +00:00
|
|
|
file, err := os.Open(os.Args[0])
|
2017-04-07 22:50:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2018-09-20 17:50:29 +00:00
|
|
|
dirPath := filepath.Dir(os.Args[0])
|
|
|
|
fileName := filepath.Base(os.Args[0])
|
2017-03-23 22:54:15 +00:00
|
|
|
|
2018-09-20 17:50:29 +00:00
|
|
|
if tempDir != "" {
|
|
|
|
fi, err := file.Stat()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-12-15 18:31:57 +00:00
|
|
|
|
2018-09-20 17:50:29 +00:00
|
|
|
// Copy over the file to the temp dir
|
|
|
|
dst := filepath.Join(tempDir, fileName)
|
2023-02-23 23:10:13 +00:00
|
|
|
|
|
|
|
// delete the file first to avoid notary failures in macOS
|
|
|
|
_ = os.Remove(dst) // ignore error
|
2018-09-20 17:50:29 +00:00
|
|
|
out, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fi.Mode())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer out.Close()
|
2017-12-15 18:31:57 +00:00
|
|
|
|
2018-09-20 17:50:29 +00:00
|
|
|
if _, err = io.Copy(out, file); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = out.Sync()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-02-23 17:07:48 +00:00
|
|
|
// Ensure that the file is closed and written. This seems to be
|
|
|
|
// necessary on Linux systems.
|
|
|
|
out.Close()
|
2017-12-15 18:31:57 +00:00
|
|
|
|
2018-09-20 17:50:29 +00:00
|
|
|
dirPath = tempDir
|
2017-12-15 18:31:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 17:50:29 +00:00
|
|
|
// Determine plugin directory full path, evaluating potential symlink path
|
|
|
|
fullPath, err := filepath.EvalSymlinks(dirPath)
|
2017-12-15 18:31:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-09-20 17:50:29 +00:00
|
|
|
reader, err := os.Open(filepath.Join(fullPath, fileName))
|
2017-12-15 18:31:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-12-15 18:38:01 +00:00
|
|
|
defer reader.Close()
|
2017-12-15 18:31:57 +00:00
|
|
|
|
|
|
|
// Find out the sha256
|
|
|
|
hash := sha256.New()
|
|
|
|
|
|
|
|
_, err = io.Copy(hash, reader)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sum := hash.Sum(nil)
|
|
|
|
|
|
|
|
// Set core's plugin directory and plugin catalog directory
|
|
|
|
c.pluginDirectory = fullPath
|
|
|
|
c.pluginCatalog.directory = fullPath
|
|
|
|
|
2018-01-18 00:19:28 +00:00
|
|
|
args := []string{fmt.Sprintf("--test.run=%s", testFunc)}
|
2022-08-25 20:31:42 +00:00
|
|
|
err = c.pluginCatalog.Set(context.Background(), name, pluginType, version, fileName, args, env, sum)
|
2017-12-15 18:31:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 02:42:26 +00:00
|
|
|
// TestRunTestPlugin runs the testFunc which has already been registered to the
|
|
|
|
// plugin catalog and returns a pluginClient. This can be called after calling
|
|
|
|
// TestAddTestPlugin.
|
|
|
|
func TestRunTestPlugin(t testing.T, c *Core, pluginType consts.PluginType, pluginName string) *pluginClient {
|
|
|
|
t.Helper()
|
|
|
|
config := TestPluginClientConfig(c, pluginType, pluginName)
|
|
|
|
client, err := c.pluginCatalog.NewPluginClient(context.Background(), config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPluginClientConfig(c *Core, pluginType consts.PluginType, pluginName string) pluginutil.PluginClientConfig {
|
2022-12-07 18:29:51 +00:00
|
|
|
dsv := TestDynamicSystemView(c, nil)
|
2022-08-30 02:42:26 +00:00
|
|
|
switch pluginType {
|
|
|
|
case consts.PluginTypeCredential, consts.PluginTypeSecrets:
|
|
|
|
return pluginutil.PluginClientConfig{
|
|
|
|
Name: pluginName,
|
|
|
|
PluginType: pluginType,
|
|
|
|
PluginSets: backendplugin.PluginSet,
|
|
|
|
HandshakeConfig: backendplugin.HandshakeConfig,
|
|
|
|
Logger: log.NewNullLogger(),
|
|
|
|
AutoMTLS: true,
|
|
|
|
IsMetadataMode: false,
|
|
|
|
Wrapper: dsv,
|
|
|
|
}
|
|
|
|
case consts.PluginTypeDatabase:
|
|
|
|
return pluginutil.PluginClientConfig{
|
|
|
|
Name: pluginName,
|
|
|
|
PluginType: pluginType,
|
|
|
|
PluginSets: v5.PluginSets,
|
|
|
|
HandshakeConfig: v5.HandshakeConfig,
|
|
|
|
Logger: log.NewNullLogger(),
|
|
|
|
AutoMTLS: true,
|
|
|
|
IsMetadataMode: false,
|
2022-12-07 18:29:51 +00:00
|
|
|
Wrapper: dsv,
|
2022-08-30 02:42:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return pluginutil.PluginClientConfig{}
|
|
|
|
}
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
var (
|
|
|
|
testLogicalBackends = map[string]logical.Factory{}
|
|
|
|
testCredentialBackends = map[string]logical.Factory{}
|
|
|
|
)
|
2015-07-10 15:56:14 +00:00
|
|
|
|
2017-10-11 17:21:20 +00:00
|
|
|
// This adds a credential backend for the test core. This needs to be
|
|
|
|
// invoked before the test core is created.
|
|
|
|
func AddTestCredentialBackend(name string, factory logical.Factory) error {
|
|
|
|
if name == "" {
|
|
|
|
return fmt.Errorf("missing backend name")
|
|
|
|
}
|
|
|
|
if factory == nil {
|
|
|
|
return fmt.Errorf("missing backend factory function")
|
|
|
|
}
|
|
|
|
testCredentialBackends[name] = factory
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-10 15:56:14 +00:00
|
|
|
// This adds a logical backend for the test core. This needs to be
|
|
|
|
// invoked before the test core is created.
|
|
|
|
func AddTestLogicalBackend(name string, factory logical.Factory) error {
|
|
|
|
if name == "" {
|
2018-04-09 18:35:21 +00:00
|
|
|
return fmt.Errorf("missing backend name")
|
2015-07-10 15:56:14 +00:00
|
|
|
}
|
|
|
|
if factory == nil {
|
2018-04-09 18:35:21 +00:00
|
|
|
return fmt.Errorf("missing backend factory function")
|
2015-07-10 15:56:14 +00:00
|
|
|
}
|
|
|
|
testLogicalBackends[name] = factory
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-27 21:19:12 +00:00
|
|
|
type rawHTTP struct{}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (n *rawHTTP) HandleRequest(ctx context.Context, req *logical.Request) (*logical.Response, error) {
|
2015-05-27 21:19:12 +00:00
|
|
|
return &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
logical.HTTPStatusCode: 200,
|
|
|
|
logical.HTTPContentType: "plain/text",
|
|
|
|
logical.HTTPRawBody: []byte("hello world"),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (n *rawHTTP) HandleExistenceCheck(ctx context.Context, req *logical.Request) (bool, bool, error) {
|
2016-01-12 20:09:16 +00:00
|
|
|
return false, false, nil
|
2016-01-07 20:10:05 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 21:19:12 +00:00
|
|
|
func (n *rawHTTP) SpecialPaths() *logical.Paths {
|
|
|
|
return &logical.Paths{Unauthenticated: []string{"*"}}
|
|
|
|
}
|
2015-09-02 19:56:58 +00:00
|
|
|
|
|
|
|
func (n *rawHTTP) System() logical.SystemView {
|
|
|
|
return logical.StaticSystemView{
|
|
|
|
DefaultLeaseTTLVal: time.Hour * 24,
|
2016-09-28 22:32:49 +00:00
|
|
|
MaxLeaseTTLVal: time.Hour * 24 * 32,
|
2015-09-02 19:56:58 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-10 14:11:37 +00:00
|
|
|
|
Backend plugin system (#2874)
* Add backend plugin changes
* Fix totp backend plugin tests
* Fix logical/plugin InvalidateKey test
* Fix plugin catalog CRUD test, fix NoopBackend
* Clean up commented code block
* Fix system backend mount test
* Set plugin_name to omitempty, fix handleMountTable config parsing
* Clean up comments, keep shim connections alive until cleanup
* Include pluginClient, disallow LookupPlugin call from within a plugin
* Add wrapper around backendPluginClient for proper cleanup
* Add logger shim tests
* Add logger, storage, and system shim tests
* Use pointer receivers for system view shim
* Use plugin name if no path is provided on mount
* Enable plugins for auth backends
* Add backend type attribute, move builtin/plugin/package
* Fix merge conflict
* Fix missing plugin name in mount config
* Add integration tests on enabling auth backend plugins
* Remove dependency cycle on mock-plugin
* Add passthrough backend plugin, use logical.BackendType to determine lease generation
* Remove vault package dependency on passthrough package
* Add basic impl test for passthrough plugin
* Incorporate feedback; set b.backend after shims creation on backendPluginServer
* Fix totp plugin test
* Add plugin backends docs
* Fix tests
* Fix builtin/plugin tests
* Remove flatten from PluginRunner fields
* Move mock plugin to logical/plugin, remove totp and passthrough plugins
* Move pluginMap into newPluginClient
* Do not create storage RPC connection on HandleRequest and HandleExistenceCheck
* Change shim logger's Fatal to no-op
* Change BackendType to uint32, match UX backend types
* Change framework.Backend Setup signature
* Add Setup func to logical.Backend interface
* Move OptionallyEnableMlock call into plugin.Serve, update docs and comments
* Remove commented var in plugin package
* RegisterLicense on logical.Backend interface (#3017)
* Add RegisterLicense to logical.Backend interface
* Update RegisterLicense to use callback func on framework.Backend
* Refactor framework.Backend.RegisterLicense
* plugin: Prevent plugin.SystemViewClient.ResponseWrapData from getting JWTs
* plugin: Revert BackendType to remove TypePassthrough and related references
* Fix typo in plugin backends docs
2017-07-20 17:28:40 +00:00
|
|
|
func (n *rawHTTP) Logger() log.Logger {
|
2018-04-03 00:46:59 +00:00
|
|
|
return logging.NewVaultLogger(log.Trace)
|
Backend plugin system (#2874)
* Add backend plugin changes
* Fix totp backend plugin tests
* Fix logical/plugin InvalidateKey test
* Fix plugin catalog CRUD test, fix NoopBackend
* Clean up commented code block
* Fix system backend mount test
* Set plugin_name to omitempty, fix handleMountTable config parsing
* Clean up comments, keep shim connections alive until cleanup
* Include pluginClient, disallow LookupPlugin call from within a plugin
* Add wrapper around backendPluginClient for proper cleanup
* Add logger shim tests
* Add logger, storage, and system shim tests
* Use pointer receivers for system view shim
* Use plugin name if no path is provided on mount
* Enable plugins for auth backends
* Add backend type attribute, move builtin/plugin/package
* Fix merge conflict
* Fix missing plugin name in mount config
* Add integration tests on enabling auth backend plugins
* Remove dependency cycle on mock-plugin
* Add passthrough backend plugin, use logical.BackendType to determine lease generation
* Remove vault package dependency on passthrough package
* Add basic impl test for passthrough plugin
* Incorporate feedback; set b.backend after shims creation on backendPluginServer
* Fix totp plugin test
* Add plugin backends docs
* Fix tests
* Fix builtin/plugin tests
* Remove flatten from PluginRunner fields
* Move mock plugin to logical/plugin, remove totp and passthrough plugins
* Move pluginMap into newPluginClient
* Do not create storage RPC connection on HandleRequest and HandleExistenceCheck
* Change shim logger's Fatal to no-op
* Change BackendType to uint32, match UX backend types
* Change framework.Backend Setup signature
* Add Setup func to logical.Backend interface
* Move OptionallyEnableMlock call into plugin.Serve, update docs and comments
* Remove commented var in plugin package
* RegisterLicense on logical.Backend interface (#3017)
* Add RegisterLicense to logical.Backend interface
* Update RegisterLicense to use callback func on framework.Backend
* Refactor framework.Backend.RegisterLicense
* plugin: Prevent plugin.SystemViewClient.ResponseWrapData from getting JWTs
* plugin: Revert BackendType to remove TypePassthrough and related references
* Fix typo in plugin backends docs
2017-07-20 17:28:40 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (n *rawHTTP) Cleanup(ctx context.Context) {
|
2015-09-10 14:11:37 +00:00
|
|
|
// noop
|
|
|
|
}
|
2016-01-15 15:55:35 +00:00
|
|
|
|
2019-07-05 23:55:40 +00:00
|
|
|
func (n *rawHTTP) Initialize(ctx context.Context, req *logical.InitializationRequest) error {
|
2017-01-13 19:51:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (n *rawHTTP) InvalidateKey(context.Context, string) {
|
2017-01-07 23:18:22 +00:00
|
|
|
// noop
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (n *rawHTTP) Setup(ctx context.Context, config *logical.BackendConfig) error {
|
Backend plugin system (#2874)
* Add backend plugin changes
* Fix totp backend plugin tests
* Fix logical/plugin InvalidateKey test
* Fix plugin catalog CRUD test, fix NoopBackend
* Clean up commented code block
* Fix system backend mount test
* Set plugin_name to omitempty, fix handleMountTable config parsing
* Clean up comments, keep shim connections alive until cleanup
* Include pluginClient, disallow LookupPlugin call from within a plugin
* Add wrapper around backendPluginClient for proper cleanup
* Add logger shim tests
* Add logger, storage, and system shim tests
* Use pointer receivers for system view shim
* Use plugin name if no path is provided on mount
* Enable plugins for auth backends
* Add backend type attribute, move builtin/plugin/package
* Fix merge conflict
* Fix missing plugin name in mount config
* Add integration tests on enabling auth backend plugins
* Remove dependency cycle on mock-plugin
* Add passthrough backend plugin, use logical.BackendType to determine lease generation
* Remove vault package dependency on passthrough package
* Add basic impl test for passthrough plugin
* Incorporate feedback; set b.backend after shims creation on backendPluginServer
* Fix totp plugin test
* Add plugin backends docs
* Fix tests
* Fix builtin/plugin tests
* Remove flatten from PluginRunner fields
* Move mock plugin to logical/plugin, remove totp and passthrough plugins
* Move pluginMap into newPluginClient
* Do not create storage RPC connection on HandleRequest and HandleExistenceCheck
* Change shim logger's Fatal to no-op
* Change BackendType to uint32, match UX backend types
* Change framework.Backend Setup signature
* Add Setup func to logical.Backend interface
* Move OptionallyEnableMlock call into plugin.Serve, update docs and comments
* Remove commented var in plugin package
* RegisterLicense on logical.Backend interface (#3017)
* Add RegisterLicense to logical.Backend interface
* Update RegisterLicense to use callback func on framework.Backend
* Refactor framework.Backend.RegisterLicense
* plugin: Prevent plugin.SystemViewClient.ResponseWrapData from getting JWTs
* plugin: Revert BackendType to remove TypePassthrough and related references
* Fix typo in plugin backends docs
2017-07-20 17:28:40 +00:00
|
|
|
// noop
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *rawHTTP) Type() logical.BackendType {
|
2018-11-07 01:21:24 +00:00
|
|
|
return logical.TypeLogical
|
Backend plugin system (#2874)
* Add backend plugin changes
* Fix totp backend plugin tests
* Fix logical/plugin InvalidateKey test
* Fix plugin catalog CRUD test, fix NoopBackend
* Clean up commented code block
* Fix system backend mount test
* Set plugin_name to omitempty, fix handleMountTable config parsing
* Clean up comments, keep shim connections alive until cleanup
* Include pluginClient, disallow LookupPlugin call from within a plugin
* Add wrapper around backendPluginClient for proper cleanup
* Add logger shim tests
* Add logger, storage, and system shim tests
* Use pointer receivers for system view shim
* Use plugin name if no path is provided on mount
* Enable plugins for auth backends
* Add backend type attribute, move builtin/plugin/package
* Fix merge conflict
* Fix missing plugin name in mount config
* Add integration tests on enabling auth backend plugins
* Remove dependency cycle on mock-plugin
* Add passthrough backend plugin, use logical.BackendType to determine lease generation
* Remove vault package dependency on passthrough package
* Add basic impl test for passthrough plugin
* Incorporate feedback; set b.backend after shims creation on backendPluginServer
* Fix totp plugin test
* Add plugin backends docs
* Fix tests
* Fix builtin/plugin tests
* Remove flatten from PluginRunner fields
* Move mock plugin to logical/plugin, remove totp and passthrough plugins
* Move pluginMap into newPluginClient
* Do not create storage RPC connection on HandleRequest and HandleExistenceCheck
* Change shim logger's Fatal to no-op
* Change BackendType to uint32, match UX backend types
* Change framework.Backend Setup signature
* Add Setup func to logical.Backend interface
* Move OptionallyEnableMlock call into plugin.Serve, update docs and comments
* Remove commented var in plugin package
* RegisterLicense on logical.Backend interface (#3017)
* Add RegisterLicense to logical.Backend interface
* Update RegisterLicense to use callback func on framework.Backend
* Refactor framework.Backend.RegisterLicense
* plugin: Prevent plugin.SystemViewClient.ResponseWrapData from getting JWTs
* plugin: Revert BackendType to remove TypePassthrough and related references
* Fix typo in plugin backends docs
2017-07-20 17:28:40 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
func GenerateRandBytes(length int) ([]byte, error) {
|
|
|
|
if length < 0 {
|
|
|
|
return nil, fmt.Errorf("length must be >= 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, length)
|
|
|
|
if length == 0 {
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := rand.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if n != length {
|
|
|
|
return nil, fmt.Errorf("unable to read %d bytes; only read %d", length, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
|
2021-02-18 20:40:18 +00:00
|
|
|
func TestWaitActive(t testing.T, core *Core) {
|
2017-09-01 05:02:03 +00:00
|
|
|
t.Helper()
|
2018-05-20 06:42:15 +00:00
|
|
|
if err := TestWaitActiveWithError(core); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 20:40:18 +00:00
|
|
|
func TestWaitActiveForwardingReady(t testing.T, core *Core) {
|
2020-09-16 19:31:06 +00:00
|
|
|
TestWaitActive(t, core)
|
|
|
|
|
|
|
|
deadline := time.Now().Add(2 * time.Second)
|
|
|
|
for time.Now().Before(deadline) {
|
|
|
|
if _, ok := core.getClusterListener().Handler(consts.RequestForwardingALPN); ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
t.Fatal("timed out waiting for request forwarding handler to be registered")
|
|
|
|
}
|
|
|
|
|
2018-05-20 06:42:15 +00:00
|
|
|
func TestWaitActiveWithError(core *Core) error {
|
2016-08-15 13:42:42 +00:00
|
|
|
start := time.Now()
|
|
|
|
var standby bool
|
|
|
|
var err error
|
2019-02-06 02:01:18 +00:00
|
|
|
for time.Now().Sub(start) < 30*time.Second {
|
2016-08-15 13:42:42 +00:00
|
|
|
standby, err = core.Standby()
|
|
|
|
if err != nil {
|
2018-05-20 06:42:15 +00:00
|
|
|
return err
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
if !standby {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if standby {
|
2018-05-20 06:42:15 +00:00
|
|
|
return errors.New("should not be in standby mode")
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
2018-05-20 06:42:15 +00:00
|
|
|
return nil
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
|
2017-07-03 18:54:01 +00:00
|
|
|
type TestCluster struct {
|
2019-06-24 01:50:27 +00:00
|
|
|
BarrierKeys [][]byte
|
|
|
|
RecoveryKeys [][]byte
|
|
|
|
CACert *x509.Certificate
|
|
|
|
CACertBytes []byte
|
|
|
|
CACertPEM []byte
|
|
|
|
CACertPEMFile string
|
|
|
|
CAKey *ecdsa.PrivateKey
|
|
|
|
CAKeyPEM []byte
|
|
|
|
Cores []*TestClusterCore
|
|
|
|
ID string
|
2023-02-03 22:27:11 +00:00
|
|
|
Plugins []pluginhelpers.TestPlugin
|
2019-06-24 01:50:27 +00:00
|
|
|
RootToken string
|
|
|
|
RootCAs *x509.CertPool
|
|
|
|
TempDir string
|
|
|
|
ClientAuthRequired bool
|
2019-07-23 19:17:37 +00:00
|
|
|
Logger log.Logger
|
2019-08-23 19:51:25 +00:00
|
|
|
CleanupFunc func()
|
|
|
|
SetupFunc func()
|
2020-06-16 18:12:22 +00:00
|
|
|
|
2023-04-12 13:46:15 +00:00
|
|
|
cleanupFuncs []func()
|
|
|
|
base *CoreConfig
|
|
|
|
LicensePublicKey ed25519.PublicKey
|
|
|
|
LicensePrivateKey ed25519.PrivateKey
|
|
|
|
opts *TestClusterOptions
|
2017-07-03 18:54:01 +00:00
|
|
|
}
|
|
|
|
|
2023-04-28 19:43:30 +00:00
|
|
|
func (c *TestCluster) SetRootToken(token string) {
|
|
|
|
c.RootToken = token
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:06:38 +00:00
|
|
|
func (c *TestCluster) Start() {
|
2022-11-29 19:38:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TestCluster) start(t testing.T) {
|
|
|
|
t.Helper()
|
2020-05-14 12:31:02 +00:00
|
|
|
for i, core := range c.Cores {
|
2017-07-03 18:54:01 +00:00
|
|
|
if core.Server != nil {
|
|
|
|
for _, ln := range core.Listeners {
|
2020-05-14 12:31:02 +00:00
|
|
|
c.Logger.Info("starting listener for test core", "core", i, "port", ln.Address.Port)
|
2017-07-03 18:54:01 +00:00
|
|
|
go core.Server.Serve(ln)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-23 19:51:25 +00:00
|
|
|
if c.SetupFunc != nil {
|
|
|
|
c.SetupFunc()
|
|
|
|
}
|
2022-11-29 19:38:33 +00:00
|
|
|
|
|
|
|
if c.opts != nil && c.opts.SkipInit {
|
|
|
|
// SkipInit implies that vault may not be ready to service requests, or that
|
|
|
|
// we're restarting a cluster from an existing storage.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
activeCore := -1
|
|
|
|
WAITACTIVE:
|
|
|
|
for i := 0; i < 60; i++ {
|
|
|
|
for i, core := range c.Cores {
|
|
|
|
if standby, _ := core.Core.Standby(); !standby {
|
|
|
|
activeCore = i
|
|
|
|
break WAITACTIVE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
if activeCore == -1 {
|
|
|
|
t.Fatalf("no core became active")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case c.opts == nil:
|
|
|
|
case c.opts.NoDefaultQuotas:
|
|
|
|
case c.opts.HandlerFunc == nil:
|
|
|
|
// If no HandlerFunc is provided that means that we can't actually do
|
|
|
|
// regular vault requests.
|
|
|
|
case reflect.TypeOf(c.opts.HandlerFunc).PkgPath() != "github.com/hashicorp/vault/http":
|
|
|
|
case reflect.TypeOf(c.opts.HandlerFunc).Name() != "Handler":
|
|
|
|
default:
|
|
|
|
cli := c.Cores[activeCore].Client
|
|
|
|
_, err := cli.Logical().Write("sys/quotas/rate-limit/rl-NewTestCluster", map[string]interface{}{
|
|
|
|
"rate": 1000000,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error setting up global rate limit quota: %v", err)
|
|
|
|
}
|
|
|
|
if constants.IsEnterprise {
|
|
|
|
_, err = cli.Logical().Write("sys/quotas/lease-count/lc-NewTestCluster", map[string]interface{}{
|
|
|
|
"max_leases": 1000000,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error setting up global lease count quota: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-03 18:54:01 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 20:34:06 +00:00
|
|
|
// UnsealCores uses the cluster barrier keys to unseal the test cluster cores
|
|
|
|
func (c *TestCluster) UnsealCores(t testing.T) {
|
2019-10-18 18:46:00 +00:00
|
|
|
t.Helper()
|
2019-10-22 13:35:48 +00:00
|
|
|
if err := c.UnsealCoresWithError(false); err != nil {
|
2018-05-20 06:42:15 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 13:35:48 +00:00
|
|
|
func (c *TestCluster) UnsealCoresWithError(useStoredKeys bool) error {
|
|
|
|
unseal := func(core *Core) error {
|
|
|
|
for _, key := range c.BarrierKeys {
|
|
|
|
if _, err := core.Unseal(TestKeyCopy(key)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if useStoredKeys {
|
|
|
|
unseal = func(core *Core) error {
|
|
|
|
return core.UnsealWithStoredKeys(context.Background())
|
|
|
|
}
|
|
|
|
}
|
2018-03-27 20:34:06 +00:00
|
|
|
|
|
|
|
// Unseal first core
|
2019-10-22 13:35:48 +00:00
|
|
|
if err := unseal(c.Cores[0].Core); err != nil {
|
|
|
|
return fmt.Errorf("unseal core %d err: %s", 0, err)
|
2018-03-27 20:34:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify unsealed
|
2018-07-24 20:57:25 +00:00
|
|
|
if c.Cores[0].Sealed() {
|
2018-05-20 06:42:15 +00:00
|
|
|
return fmt.Errorf("should not be sealed")
|
2018-03-27 20:34:06 +00:00
|
|
|
}
|
|
|
|
|
2018-05-20 06:42:15 +00:00
|
|
|
if err := TestWaitActiveWithError(c.Cores[0].Core); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-27 20:34:06 +00:00
|
|
|
|
|
|
|
// Unseal other cores
|
2019-10-22 13:35:48 +00:00
|
|
|
for i := 1; i < len(c.Cores); i++ {
|
|
|
|
if err := unseal(c.Cores[i].Core); err != nil {
|
|
|
|
return fmt.Errorf("unseal core %d err: %s", i, err)
|
2018-03-27 20:34:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let them come fully up to standby
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
|
|
|
// Ensure cluster connection info is populated.
|
|
|
|
// Other cores should not come up as leaders.
|
2019-10-22 13:35:48 +00:00
|
|
|
for i := 1; i < len(c.Cores); i++ {
|
2018-03-27 20:34:06 +00:00
|
|
|
isLeader, _, _, err := c.Cores[i].Leader()
|
|
|
|
if err != nil {
|
2018-05-20 06:42:15 +00:00
|
|
|
return err
|
2018-03-27 20:34:06 +00:00
|
|
|
}
|
|
|
|
if isLeader {
|
2018-05-20 06:42:15 +00:00
|
|
|
return fmt.Errorf("core[%d] should not be leader", i)
|
2018-03-27 20:34:06 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-20 06:42:15 +00:00
|
|
|
|
|
|
|
return nil
|
2018-03-27 20:34:06 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 20:40:18 +00:00
|
|
|
func (c *TestCluster) UnsealCore(t testing.T, core *TestClusterCore) {
|
2021-05-19 20:07:58 +00:00
|
|
|
err := c.AttemptUnsealCore(core)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TestCluster) AttemptUnsealCore(core *TestClusterCore) error {
|
2020-02-13 21:27:31 +00:00
|
|
|
var keys [][]byte
|
|
|
|
if core.seal.RecoveryKeySupported() {
|
|
|
|
keys = c.RecoveryKeys
|
|
|
|
} else {
|
|
|
|
keys = c.BarrierKeys
|
|
|
|
}
|
|
|
|
for _, key := range keys {
|
2019-06-20 19:14:58 +00:00
|
|
|
if _, err := core.Core.Unseal(TestKeyCopy(key)); err != nil {
|
2021-05-19 20:07:58 +00:00
|
|
|
return fmt.Errorf("unseal err: %w", err)
|
2019-06-20 19:14:58 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-19 20:07:58 +00:00
|
|
|
return nil
|
2019-06-20 19:14:58 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 20:40:18 +00:00
|
|
|
func (c *TestCluster) UnsealCoreWithStoredKeys(t testing.T, core *TestClusterCore) {
|
2020-10-23 18:16:04 +00:00
|
|
|
t.Helper()
|
2020-06-11 19:07:59 +00:00
|
|
|
if err := core.UnsealWithStoredKeys(context.Background()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-01 05:02:03 +00:00
|
|
|
func (c *TestCluster) EnsureCoresSealed(t testing.T) {
|
|
|
|
t.Helper()
|
|
|
|
if err := c.ensureCoresSealed(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 20:40:18 +00:00
|
|
|
func (c *TestClusterCore) Seal(t testing.T) {
|
2019-02-06 02:01:18 +00:00
|
|
|
t.Helper()
|
|
|
|
if err := c.Core.sealInternal(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-20 20:13:37 +00:00
|
|
|
func (c *TestClusterCore) LogicalStorage() logical.Storage {
|
|
|
|
return c.barrier
|
|
|
|
}
|
|
|
|
|
2020-06-16 18:12:22 +00:00
|
|
|
func (c *TestClusterCore) stop() error {
|
|
|
|
c.Logger().Info("stopping vault test core")
|
|
|
|
|
|
|
|
if c.Listeners != nil {
|
|
|
|
for _, ln := range c.Listeners {
|
|
|
|
ln.Close()
|
|
|
|
}
|
|
|
|
c.Logger().Info("listeners successfully shut down")
|
|
|
|
}
|
|
|
|
if c.licensingStopCh != nil {
|
|
|
|
close(c.licensingStopCh)
|
|
|
|
c.licensingStopCh = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.Shutdown(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
timeout := time.Now().Add(60 * time.Second)
|
|
|
|
for {
|
|
|
|
if time.Now().After(timeout) {
|
|
|
|
return errors.New("timeout waiting for core to seal")
|
|
|
|
}
|
|
|
|
if c.Sealed() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(250 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Logger().Info("vault test core stopped")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-29 12:59:01 +00:00
|
|
|
func (c *TestClusterCore) StopAutomaticRollbacks() {
|
|
|
|
c.rollback.StopTicker()
|
|
|
|
}
|
|
|
|
|
Add cross-cluster revocation queues for PKI (#18784)
* Add global, cross-cluster revocation queue to PKI
This adds a global, cross-cluster replicated revocation queue, allowing
operators to revoke certificates by serial number across any cluster. We
don't support revoking with private key (PoP) in the initial
implementation.
In particular, building on the PBPWF work, we add a special storage
location for handling non-local revocations which gets replicated up to
the active, primary cluster node and back down to all secondary PR
clusters. These then check the pending revocation entry and revoke the
serial locally if it exists, writing a cross-cluster confirmation entry.
Listing capabilities are present under pki/certs/revocation-queue,
allowing operators to see which certs are present. However, a future
improvement to the tidy subsystem will allow automatic cleanup of stale
entries.
Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>
* Allow tidying revocation queue entries
No manual operator control of revocation queue entries are allowed.
However, entries are stored with their request time, allowing tidy to,
after a suitable safety buffer, remove these unconfirmed and presumably
invalid requests.
Notably, when a cluster goes offline, it will be unable to process
cross-cluster revocations for certificates it holds. If tidy runs,
potentially valid revocations may be removed. However, it is up to the
administrator to ensure the tidy window is sufficiently long that any
required maintenance is done (or, prior to maintenance when an issue is
first noticed, tidy is temporarily disabled).
Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>
* Only allow enabling global revocation queue on Vault Enterprise
Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>
* Use a locking queue to handle revocation requests
This queue attempts to guarantee that PKI's invalidateFunc won't have
to wait long to execute: by locking only around access to the queue
proper, and internally using a list, we minimize the time spent locked,
waiting for queue accesses.
Previously, we held a lock during tidy and processing that would've
prevented us from processing invalidateFunc calls.
Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>
* use_global_queue->cross_cluster_revocation
Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>
* Grab revocation storage lock when processing queue
We need to grab the storage lock as we'll actively be revoking new
certificates in the revocation queue. This ensures nobody else is
competing for storage access, across periodic funcs, new revocations,
and tidy operations.
Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>
* Fix expected tidy status test
Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>
* Allow probing RollbackManager directly in tests
Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>
* Address review feedback on revocationQueue
Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>
* Add more cancel checks, fix starting manual tidy
Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>
Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>
2023-01-23 14:29:27 +00:00
|
|
|
func (c *TestClusterCore) GrabRollbackLock() {
|
|
|
|
// Ensure we don't hold this lock while there are in flight rollbacks.
|
|
|
|
c.rollback.inflightAll.Wait()
|
|
|
|
c.rollback.inflightLock.Lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TestClusterCore) ReleaseRollbackLock() {
|
|
|
|
c.rollback.inflightLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TestClusterCore) TriggerRollbacks() {
|
|
|
|
c.rollback.triggerRollbacks()
|
|
|
|
}
|
|
|
|
|
2023-01-31 16:05:16 +00:00
|
|
|
func (c *TestClusterCore) TLSConfig() *tls.Config {
|
|
|
|
return c.tlsConfig.Clone()
|
|
|
|
}
|
|
|
|
|
2023-04-20 20:13:37 +00:00
|
|
|
func (c *TestClusterCore) ClusterListener() *cluster.Listener {
|
|
|
|
return c.getClusterListener()
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:06:38 +00:00
|
|
|
func (c *TestCluster) Cleanup() {
|
2019-07-23 19:17:37 +00:00
|
|
|
c.Logger.Info("cleaning up vault cluster")
|
2023-02-01 13:33:16 +00:00
|
|
|
if tl, ok := c.Logger.(*corehelpers.TestLogger); ok {
|
2020-09-23 17:40:00 +00:00
|
|
|
tl.StopLogging()
|
2019-07-23 19:17:37 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
wg := &sync.WaitGroup{}
|
2017-08-15 21:06:38 +00:00
|
|
|
for _, core := range c.Cores {
|
2018-09-18 03:03:00 +00:00
|
|
|
wg.Add(1)
|
|
|
|
lc := core
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2020-06-16 18:12:22 +00:00
|
|
|
if err := lc.stop(); err != nil {
|
2020-09-23 17:40:00 +00:00
|
|
|
// Note that this log won't be seen if using TestLogger, due to
|
|
|
|
// the above call to StopLogging.
|
2020-06-16 18:12:22 +00:00
|
|
|
lc.Logger().Error("error during cleanup", "error", err)
|
2018-09-18 03:03:00 +00:00
|
|
|
}
|
|
|
|
}()
|
2017-07-03 18:54:01 +00:00
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
wg.Wait()
|
2017-09-01 05:02:03 +00:00
|
|
|
|
|
|
|
// Remove any temp dir that exists
|
|
|
|
if c.TempDir != "" {
|
|
|
|
os.RemoveAll(c.TempDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give time to actually shut down/clean up before the next test
|
|
|
|
time.Sleep(time.Second)
|
2019-08-23 19:51:25 +00:00
|
|
|
if c.CleanupFunc != nil {
|
|
|
|
c.CleanupFunc()
|
|
|
|
}
|
2017-09-01 05:02:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TestCluster) ensureCoresSealed() error {
|
2017-08-15 21:06:38 +00:00
|
|
|
for _, core := range c.Cores {
|
|
|
|
if err := core.Shutdown(); err != nil {
|
2017-09-01 05:02:03 +00:00
|
|
|
return err
|
2017-08-15 21:06:38 +00:00
|
|
|
}
|
|
|
|
timeout := time.Now().Add(60 * time.Second)
|
|
|
|
for {
|
|
|
|
if time.Now().After(timeout) {
|
2017-09-01 05:02:03 +00:00
|
|
|
return fmt.Errorf("timeout waiting for core to seal")
|
2017-08-15 21:06:38 +00:00
|
|
|
}
|
2018-07-24 20:57:25 +00:00
|
|
|
if core.Sealed() {
|
2017-11-30 14:43:07 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(250 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
func SetReplicationFailureMode(core *TestClusterCore, mode uint32) {
|
|
|
|
atomic.StoreUint32(core.Core.replicationFailure, mode)
|
|
|
|
}
|
|
|
|
|
2016-08-15 13:42:42 +00:00
|
|
|
type TestListener struct {
|
|
|
|
net.Listener
|
|
|
|
Address *net.TCPAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestClusterCore struct {
|
|
|
|
*Core
|
2019-06-20 19:14:58 +00:00
|
|
|
CoreConfig *CoreConfig
|
|
|
|
Client *api.Client
|
|
|
|
Handler http.Handler
|
2020-06-16 18:12:22 +00:00
|
|
|
Address *net.TCPAddr
|
2019-06-20 19:14:58 +00:00
|
|
|
Listeners []*TestListener
|
2020-02-15 19:58:05 +00:00
|
|
|
ReloadFuncs *map[string][]reloadutil.ReloadFunc
|
2019-06-20 19:14:58 +00:00
|
|
|
ReloadFuncsLock *sync.RWMutex
|
|
|
|
Server *http.Server
|
|
|
|
ServerCert *x509.Certificate
|
|
|
|
ServerCertBytes []byte
|
|
|
|
ServerCertPEM []byte
|
|
|
|
ServerKey *ecdsa.PrivateKey
|
|
|
|
ServerKeyPEM []byte
|
2023-01-31 16:05:16 +00:00
|
|
|
tlsConfig *tls.Config
|
2019-06-20 19:14:58 +00:00
|
|
|
UnderlyingStorage physical.Backend
|
|
|
|
UnderlyingRawStorage physical.Backend
|
2020-06-23 19:04:13 +00:00
|
|
|
UnderlyingHAStorage physical.HABackend
|
2019-06-20 19:14:58 +00:00
|
|
|
Barrier SecurityBarrier
|
|
|
|
NodeID string
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
|
2019-08-23 19:51:25 +00:00
|
|
|
type PhysicalBackendBundle struct {
|
|
|
|
Backend physical.Backend
|
|
|
|
HABackend physical.HABackend
|
|
|
|
Cleanup func()
|
|
|
|
}
|
|
|
|
|
2022-11-29 19:38:33 +00:00
|
|
|
type HandlerHandler interface {
|
|
|
|
Handler(*HandlerProperties) http.Handler
|
|
|
|
}
|
|
|
|
|
2017-07-31 15:28:06 +00:00
|
|
|
type TestClusterOptions struct {
|
2019-10-15 04:55:31 +00:00
|
|
|
KeepStandbysSealed bool
|
|
|
|
SkipInit bool
|
2022-11-29 19:38:33 +00:00
|
|
|
HandlerFunc HandlerHandler
|
2019-10-15 04:55:31 +00:00
|
|
|
DefaultHandlerProperties HandlerProperties
|
2020-05-14 12:31:02 +00:00
|
|
|
|
2020-06-11 19:07:59 +00:00
|
|
|
// BaseListenAddress is used to explicitly assign ports in sequence to the
|
2020-07-09 21:16:31 +00:00
|
|
|
// listener of each core. It should be a string of the form
|
2020-06-11 19:07:59 +00:00
|
|
|
// "127.0.0.1:20000"
|
|
|
|
//
|
|
|
|
// WARNING: Using an explicitly assigned port above 30000 may clash with
|
|
|
|
// ephemeral ports that have been assigned by the OS in other tests. The
|
2020-07-09 21:16:31 +00:00
|
|
|
// use of explicitly assigned ports below 30000 is strongly recommended.
|
|
|
|
// In addition, you should be careful to use explicitly assigned ports that
|
2020-06-11 19:07:59 +00:00
|
|
|
// do not clash with any other explicitly assigned ports in other tests.
|
2020-05-14 12:31:02 +00:00
|
|
|
BaseListenAddress string
|
|
|
|
|
2020-06-11 19:07:59 +00:00
|
|
|
// BaseClusterListenPort is used to explicitly assign ports in sequence to
|
|
|
|
// the cluster listener of each core. If BaseClusterListenPort is
|
|
|
|
// specified, then BaseListenAddress must also be specified. Each cluster
|
|
|
|
// listener will use the same host as the one specified in
|
|
|
|
// BaseListenAddress.
|
|
|
|
//
|
|
|
|
// WARNING: Using an explicitly assigned port above 30000 may clash with
|
|
|
|
// ephemeral ports that have been assigned by the OS in other tests. The
|
2020-07-09 21:16:31 +00:00
|
|
|
// use of explicitly assigned ports below 30000 is strongly recommended.
|
|
|
|
// In addition, you should be careful to use explicitly assigned ports that
|
2020-06-11 19:07:59 +00:00
|
|
|
// do not clash with any other explicitly assigned ports in other tests.
|
2020-05-14 12:31:02 +00:00
|
|
|
BaseClusterListenPort int
|
|
|
|
|
2020-10-23 18:16:04 +00:00
|
|
|
NumCores int
|
|
|
|
SealFunc func() Seal
|
|
|
|
UnwrapSealFunc func() Seal
|
|
|
|
Logger log.Logger
|
|
|
|
TempDir string
|
|
|
|
CACert []byte
|
|
|
|
CAKey *ecdsa.PrivateKey
|
2019-08-23 19:51:25 +00:00
|
|
|
// PhysicalFactory is used to create backends.
|
|
|
|
// The int argument is the index of the core within the cluster, i.e. first
|
|
|
|
// core in cluster will have 0, second 1, etc.
|
|
|
|
// If the backend is shared across the cluster (i.e. is not Raft) then it
|
|
|
|
// should return nil when coreIdx != 0.
|
2021-03-03 18:59:50 +00:00
|
|
|
PhysicalFactory func(t testing.T, coreIdx int, logger log.Logger, conf map[string]interface{}) *PhysicalBackendBundle
|
2019-08-23 19:51:25 +00:00
|
|
|
// FirstCoreNumber is used to assign a unique number to each core within
|
|
|
|
// a multi-cluster setup.
|
|
|
|
FirstCoreNumber int
|
|
|
|
RequireClientAuth bool
|
|
|
|
// SetupFunc is called after the cluster is started.
|
2021-02-18 20:40:18 +00:00
|
|
|
SetupFunc func(t testing.T, c *TestCluster)
|
2020-01-17 07:03:02 +00:00
|
|
|
PR1103Disabled bool
|
|
|
|
|
|
|
|
// ClusterLayers are used to override the default cluster connection layer
|
|
|
|
ClusterLayers cluster.NetworkLayerSet
|
2021-03-17 21:23:13 +00:00
|
|
|
// InmemClusterLayers is a shorthand way of asking for ClusterLayers to be
|
|
|
|
// built using the inmem implementation.
|
|
|
|
InmemClusterLayers bool
|
2020-06-16 18:12:22 +00:00
|
|
|
|
|
|
|
// RaftAddressProvider is used to set the raft ServerAddressProvider on
|
|
|
|
// each core.
|
|
|
|
//
|
|
|
|
// If SkipInit is true, then RaftAddressProvider has no effect.
|
|
|
|
// RaftAddressProvider should only be specified if the underlying physical
|
|
|
|
// storage is Raft.
|
|
|
|
RaftAddressProvider raftlib.ServerAddressProvider
|
2020-10-29 17:55:26 +00:00
|
|
|
|
|
|
|
CoreMetricSinkProvider func(clusterName string) (*metricsutil.ClusterMetricSink, *metricsutil.MetricsHelper)
|
2021-03-03 18:59:50 +00:00
|
|
|
|
|
|
|
PhysicalFactoryConfig map[string]interface{}
|
2021-05-17 18:10:26 +00:00
|
|
|
LicensePublicKey ed25519.PublicKey
|
|
|
|
LicensePrivateKey ed25519.PrivateKey
|
2022-05-20 20:49:11 +00:00
|
|
|
|
|
|
|
// this stores the vault version that should be used for each core config
|
2022-10-06 18:24:16 +00:00
|
|
|
VersionMap map[int]string
|
|
|
|
RedundancyZoneMap map[int]string
|
|
|
|
KVVersion string
|
|
|
|
EffectiveSDKVersionMap map[int]string
|
2022-11-29 19:38:33 +00:00
|
|
|
|
|
|
|
NoDefaultQuotas bool
|
2023-02-03 22:27:11 +00:00
|
|
|
|
|
|
|
Plugins *TestPluginConfig
|
2023-02-15 19:57:57 +00:00
|
|
|
|
|
|
|
// if populated, the callback is called for every request
|
|
|
|
RequestResponseCallback func(logical.Backend, *logical.Request, *logical.Response)
|
2023-06-09 22:04:11 +00:00
|
|
|
|
|
|
|
// ABCDLoggerNames names the loggers according to our ABCD convention when generating 4 clusters
|
|
|
|
ABCDLoggerNames bool
|
2023-02-03 22:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type TestPluginConfig struct {
|
|
|
|
Typ consts.PluginType
|
|
|
|
Versions []string
|
2017-07-31 15:28:06 +00:00
|
|
|
}
|
|
|
|
|
2017-09-01 05:02:03 +00:00
|
|
|
var DefaultNumCores = 3
|
|
|
|
|
|
|
|
type certInfo struct {
|
|
|
|
cert *x509.Certificate
|
|
|
|
certPEM []byte
|
|
|
|
certBytes []byte
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
keyPEM []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTestCluster creates a new test cluster based on the provided core config
|
|
|
|
// and test cluster options.
|
2017-10-23 18:59:37 +00:00
|
|
|
//
|
|
|
|
// N.B. Even though a single base CoreConfig is provided, NewTestCluster will instantiate a
|
|
|
|
// core config for each core it creates. If separate seal per core is desired, opts.SealFunc
|
|
|
|
// can be provided to generate a seal for each one. Otherwise, the provided base.Seal will be
|
|
|
|
// shared among cores. NewCore's default behavior is to generate a new DefaultSeal if the
|
|
|
|
// provided Seal in coreConfig (i.e. base.Seal) is nil.
|
2019-07-23 19:17:37 +00:00
|
|
|
//
|
|
|
|
// If opts.Logger is provided, it takes precedence and will be used as the cluster
|
|
|
|
// logger and will be the basis for each core's logger. If no opts.Logger is
|
|
|
|
// given, one will be generated based on t.Name() for the cluster logger, and if
|
|
|
|
// no base.Logger is given will also be used as the basis for each core's logger.
|
2021-02-18 20:40:18 +00:00
|
|
|
func NewTestCluster(t testing.T, base *CoreConfig, opts *TestClusterOptions) *TestCluster {
|
2018-02-22 05:23:37 +00:00
|
|
|
var err error
|
|
|
|
|
2017-09-01 05:02:03 +00:00
|
|
|
var numCores int
|
|
|
|
if opts == nil || opts.NumCores == 0 {
|
|
|
|
numCores = DefaultNumCores
|
|
|
|
} else {
|
|
|
|
numCores = opts.NumCores
|
|
|
|
}
|
|
|
|
|
2017-07-31 15:28:06 +00:00
|
|
|
certIPs := []net.IP{
|
|
|
|
net.IPv6loopback,
|
|
|
|
net.ParseIP("127.0.0.1"),
|
|
|
|
}
|
|
|
|
var baseAddr *net.TCPAddr
|
2017-07-31 16:13:29 +00:00
|
|
|
if opts != nil && opts.BaseListenAddress != "" {
|
2017-07-31 15:28:06 +00:00
|
|
|
baseAddr, err = net.ResolveTCPAddr("tcp", opts.BaseListenAddress)
|
2023-01-11 19:32:05 +00:00
|
|
|
|
2017-07-31 15:28:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("could not parse given base IP")
|
|
|
|
}
|
|
|
|
certIPs = append(certIPs, baseAddr.IP)
|
2020-06-16 18:12:22 +00:00
|
|
|
} else {
|
|
|
|
baseAddr = &net.TCPAddr{
|
|
|
|
IP: net.ParseIP("127.0.0.1"),
|
|
|
|
Port: 0,
|
2020-05-14 12:31:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-31 15:28:06 +00:00
|
|
|
var testCluster TestCluster
|
2020-06-16 18:12:22 +00:00
|
|
|
testCluster.base = base
|
2019-07-23 19:17:37 +00:00
|
|
|
|
2020-05-01 16:26:26 +00:00
|
|
|
switch {
|
|
|
|
case opts != nil && opts.Logger != nil:
|
2019-07-23 19:17:37 +00:00
|
|
|
testCluster.Logger = opts.Logger
|
2020-05-01 16:26:26 +00:00
|
|
|
default:
|
2023-02-01 13:33:16 +00:00
|
|
|
testCluster.Logger = corehelpers.NewTestLogger(t)
|
2019-07-23 19:17:37 +00:00
|
|
|
}
|
|
|
|
|
2017-12-11 23:02:35 +00:00
|
|
|
if opts != nil && opts.TempDir != "" {
|
|
|
|
if _, err := os.Stat(opts.TempDir); os.IsNotExist(err) {
|
2021-04-08 16:43:39 +00:00
|
|
|
if err := os.MkdirAll(opts.TempDir, 0o700); err != nil {
|
2017-12-11 23:02:35 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
testCluster.TempDir = opts.TempDir
|
|
|
|
} else {
|
|
|
|
tempDir, err := ioutil.TempDir("", "vault-test-cluster-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
testCluster.TempDir = tempDir
|
2017-07-31 15:28:06 +00:00
|
|
|
}
|
|
|
|
|
2018-02-22 05:23:37 +00:00
|
|
|
var caKey *ecdsa.PrivateKey
|
|
|
|
if opts != nil && opts.CAKey != nil {
|
|
|
|
caKey = opts.CAKey
|
|
|
|
} else {
|
|
|
|
caKey, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
}
|
|
|
|
testCluster.CAKey = caKey
|
2018-02-22 05:23:37 +00:00
|
|
|
var caBytes []byte
|
|
|
|
if opts != nil && len(opts.CACert) > 0 {
|
|
|
|
caBytes = opts.CACert
|
|
|
|
} else {
|
|
|
|
caCertTemplate := &x509.Certificate{
|
|
|
|
Subject: pkix.Name{
|
|
|
|
CommonName: "localhost",
|
|
|
|
},
|
|
|
|
DNSNames: []string{"localhost"},
|
|
|
|
IPAddresses: certIPs,
|
|
|
|
KeyUsage: x509.KeyUsage(x509.KeyUsageCertSign | x509.KeyUsageCRLSign),
|
|
|
|
SerialNumber: big.NewInt(mathrand.Int63()),
|
|
|
|
NotBefore: time.Now().Add(-30 * time.Second),
|
|
|
|
NotAfter: time.Now().Add(262980 * time.Hour),
|
|
|
|
BasicConstraintsValid: true,
|
2018-09-04 16:29:18 +00:00
|
|
|
IsCA: true,
|
2018-02-22 05:23:37 +00:00
|
|
|
}
|
|
|
|
caBytes, err = x509.CreateCertificate(rand.Reader, caCertTemplate, caCertTemplate, caKey.Public(), caKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
caCert, err := x509.ParseCertificate(caBytes)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
testCluster.CACert = caCert
|
|
|
|
testCluster.CACertBytes = caBytes
|
|
|
|
testCluster.RootCAs = x509.NewCertPool()
|
|
|
|
testCluster.RootCAs.AddCert(caCert)
|
|
|
|
caCertPEMBlock := &pem.Block{
|
|
|
|
Type: "CERTIFICATE",
|
|
|
|
Bytes: caBytes,
|
|
|
|
}
|
|
|
|
testCluster.CACertPEM = pem.EncodeToMemory(caCertPEMBlock)
|
2017-07-31 19:31:44 +00:00
|
|
|
testCluster.CACertPEMFile = filepath.Join(testCluster.TempDir, "ca_cert.pem")
|
2021-04-08 16:43:39 +00:00
|
|
|
err = ioutil.WriteFile(testCluster.CACertPEMFile, testCluster.CACertPEM, 0o755)
|
2017-07-31 15:28:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
marshaledCAKey, err := x509.MarshalECPrivateKey(caKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
caKeyPEMBlock := &pem.Block{
|
|
|
|
Type: "EC PRIVATE KEY",
|
|
|
|
Bytes: marshaledCAKey,
|
|
|
|
}
|
|
|
|
testCluster.CAKeyPEM = pem.EncodeToMemory(caKeyPEMBlock)
|
2021-04-08 16:43:39 +00:00
|
|
|
err = ioutil.WriteFile(filepath.Join(testCluster.TempDir, "ca_key.pem"), testCluster.CAKeyPEM, 0o755)
|
2017-07-31 15:28:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
|
2017-09-01 05:02:03 +00:00
|
|
|
var certInfoSlice []*certInfo
|
2016-08-15 13:42:42 +00:00
|
|
|
|
2017-09-01 05:02:03 +00:00
|
|
|
//
|
|
|
|
// Certs generation
|
|
|
|
//
|
|
|
|
for i := 0; i < numCores; i++ {
|
|
|
|
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
certTemplate := &x509.Certificate{
|
|
|
|
Subject: pkix.Name{
|
|
|
|
CommonName: "localhost",
|
|
|
|
},
|
2020-02-05 18:45:16 +00:00
|
|
|
// Include host.docker.internal for the sake of benchmark-vault running on MacOS/Windows.
|
|
|
|
// This allows Prometheus running in docker to scrape the cluster for metrics.
|
|
|
|
DNSNames: []string{"localhost", "host.docker.internal"},
|
2017-09-01 05:02:03 +00:00
|
|
|
IPAddresses: certIPs,
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{
|
|
|
|
x509.ExtKeyUsageServerAuth,
|
|
|
|
x509.ExtKeyUsageClientAuth,
|
|
|
|
},
|
|
|
|
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment | x509.KeyUsageKeyAgreement,
|
|
|
|
SerialNumber: big.NewInt(mathrand.Int63()),
|
|
|
|
NotBefore: time.Now().Add(-30 * time.Second),
|
|
|
|
NotAfter: time.Now().Add(262980 * time.Hour),
|
|
|
|
}
|
|
|
|
certBytes, err := x509.CreateCertificate(rand.Reader, certTemplate, caCert, key.Public(), caKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cert, err := x509.ParseCertificate(certBytes)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
certPEMBlock := &pem.Block{
|
|
|
|
Type: "CERTIFICATE",
|
|
|
|
Bytes: certBytes,
|
|
|
|
}
|
|
|
|
certPEM := pem.EncodeToMemory(certPEMBlock)
|
|
|
|
marshaledKey, err := x509.MarshalECPrivateKey(key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
keyPEMBlock := &pem.Block{
|
|
|
|
Type: "EC PRIVATE KEY",
|
|
|
|
Bytes: marshaledKey,
|
|
|
|
}
|
|
|
|
keyPEM := pem.EncodeToMemory(keyPEMBlock)
|
2016-08-15 13:42:42 +00:00
|
|
|
|
2017-09-01 05:02:03 +00:00
|
|
|
certInfoSlice = append(certInfoSlice, &certInfo{
|
|
|
|
cert: cert,
|
|
|
|
certPEM: certPEM,
|
|
|
|
certBytes: certBytes,
|
|
|
|
key: key,
|
|
|
|
keyPEM: keyPEM,
|
|
|
|
})
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Listener setup
|
|
|
|
//
|
2020-06-16 18:12:22 +00:00
|
|
|
addresses := []*net.TCPAddr{}
|
2017-09-01 05:02:03 +00:00
|
|
|
listeners := [][]*TestListener{}
|
|
|
|
servers := []*http.Server{}
|
|
|
|
handlers := []http.Handler{}
|
|
|
|
tlsConfigs := []*tls.Config{}
|
2020-02-15 19:58:05 +00:00
|
|
|
certGetters := []*reloadutil.CertificateGetter{}
|
2017-09-01 05:02:03 +00:00
|
|
|
for i := 0; i < numCores; i++ {
|
2020-06-16 18:12:22 +00:00
|
|
|
addr := &net.TCPAddr{
|
|
|
|
IP: baseAddr.IP,
|
|
|
|
Port: 0,
|
|
|
|
}
|
|
|
|
if baseAddr.Port != 0 {
|
|
|
|
addr.Port = baseAddr.Port + i
|
|
|
|
}
|
|
|
|
|
|
|
|
ln, err := net.ListenTCP("tcp", addr)
|
2017-09-01 05:02:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-06-16 18:12:22 +00:00
|
|
|
addresses = append(addresses, addr)
|
|
|
|
|
2017-09-01 05:02:03 +00:00
|
|
|
certFile := filepath.Join(testCluster.TempDir, fmt.Sprintf("node%d_port_%d_cert.pem", i+1, ln.Addr().(*net.TCPAddr).Port))
|
|
|
|
keyFile := filepath.Join(testCluster.TempDir, fmt.Sprintf("node%d_port_%d_key.pem", i+1, ln.Addr().(*net.TCPAddr).Port))
|
2021-04-08 16:43:39 +00:00
|
|
|
err = ioutil.WriteFile(certFile, certInfoSlice[i].certPEM, 0o755)
|
2017-09-01 05:02:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-04-08 16:43:39 +00:00
|
|
|
err = ioutil.WriteFile(keyFile, certInfoSlice[i].keyPEM, 0o755)
|
2017-09-01 05:02:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
tlsCert, err := tls.X509KeyPair(certInfoSlice[i].certPEM, certInfoSlice[i].keyPEM)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-02-15 19:58:05 +00:00
|
|
|
certGetter := reloadutil.NewCertificateGetter(certFile, keyFile, "")
|
2017-09-01 05:02:03 +00:00
|
|
|
certGetters = append(certGetters, certGetter)
|
2020-05-14 13:19:27 +00:00
|
|
|
certGetter.Reload()
|
2017-09-01 05:02:03 +00:00
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{tlsCert},
|
|
|
|
RootCAs: testCluster.RootCAs,
|
|
|
|
ClientCAs: testCluster.RootCAs,
|
2017-11-09 20:55:23 +00:00
|
|
|
ClientAuth: tls.RequestClientCert,
|
2017-09-01 05:02:03 +00:00
|
|
|
NextProtos: []string{"h2", "http/1.1"},
|
|
|
|
GetCertificate: certGetter.GetCertificate,
|
|
|
|
}
|
2019-06-24 01:50:27 +00:00
|
|
|
if opts != nil && opts.RequireClientAuth {
|
|
|
|
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert
|
|
|
|
testCluster.ClientAuthRequired = true
|
|
|
|
}
|
2017-09-01 05:02:03 +00:00
|
|
|
tlsConfigs = append(tlsConfigs, tlsConfig)
|
2021-04-08 16:43:39 +00:00
|
|
|
lns := []*TestListener{
|
|
|
|
{
|
|
|
|
Listener: tls.NewListener(ln, tlsConfig),
|
|
|
|
Address: ln.Addr().(*net.TCPAddr),
|
|
|
|
},
|
2017-09-01 05:02:03 +00:00
|
|
|
}
|
|
|
|
listeners = append(listeners, lns)
|
|
|
|
var handler http.Handler = http.NewServeMux()
|
|
|
|
handlers = append(handlers, handler)
|
|
|
|
server := &http.Server{
|
2018-08-21 15:23:18 +00:00
|
|
|
Handler: handler,
|
2019-07-23 19:17:37 +00:00
|
|
|
ErrorLog: testCluster.Logger.StandardLogger(nil),
|
2017-09-01 05:02:03 +00:00
|
|
|
}
|
|
|
|
servers = append(servers, server)
|
2017-02-27 17:49:35 +00:00
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
|
2017-07-31 15:28:06 +00:00
|
|
|
// Create three cores with the same physical and different redirect/cluster
|
|
|
|
// addrs.
|
2016-11-08 15:31:35 +00:00
|
|
|
// N.B.: On OSX, instead of random ports, it assigns new ports to new
|
|
|
|
// listeners sequentially. Aside from being a bad idea in a security sense,
|
|
|
|
// it also broke tests that assumed it was OK to just use the port above
|
2017-07-31 15:28:06 +00:00
|
|
|
// the redirect addr. This has now been changed to 105 ports above, but if
|
2016-11-08 15:31:35 +00:00
|
|
|
// we ever do more than three nodes in a cluster it may need to be bumped.
|
2017-07-31 15:28:06 +00:00
|
|
|
// Note: it's 105 so that we don't conflict with a running Consul by
|
|
|
|
// default.
|
2016-08-15 13:42:42 +00:00
|
|
|
coreConfig := &CoreConfig{
|
|
|
|
LogicalBackends: make(map[string]logical.Factory),
|
|
|
|
CredentialBackends: make(map[string]logical.Factory),
|
|
|
|
AuditBackends: make(map[string]audit.Factory),
|
2017-09-01 05:02:03 +00:00
|
|
|
RedirectAddr: fmt.Sprintf("https://127.0.0.1:%d", listeners[0][0].Address.Port),
|
2019-04-04 17:02:44 +00:00
|
|
|
ClusterAddr: "https://127.0.0.1:0",
|
2016-08-15 13:42:42 +00:00
|
|
|
DisableMlock: true,
|
2017-07-31 15:28:06 +00:00
|
|
|
EnableUI: true,
|
2018-06-03 22:14:51 +00:00
|
|
|
EnableRaw: true,
|
2023-02-01 13:33:16 +00:00
|
|
|
BuiltinRegistry: corehelpers.NewMockBuiltinRegistry(),
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if base != nil {
|
2023-01-11 19:32:05 +00:00
|
|
|
coreConfig.DetectDeadlocks = TestDeadlockDetection
|
2019-10-08 17:57:15 +00:00
|
|
|
coreConfig.RawConfig = base.RawConfig
|
2017-07-31 15:28:06 +00:00
|
|
|
coreConfig.DisableCache = base.DisableCache
|
|
|
|
coreConfig.EnableUI = base.EnableUI
|
|
|
|
coreConfig.DefaultLeaseTTL = base.DefaultLeaseTTL
|
|
|
|
coreConfig.MaxLeaseTTL = base.MaxLeaseTTL
|
|
|
|
coreConfig.CacheSize = base.CacheSize
|
|
|
|
coreConfig.PluginDirectory = base.PluginDirectory
|
|
|
|
coreConfig.Seal = base.Seal
|
2020-06-11 19:07:59 +00:00
|
|
|
coreConfig.UnwrapSeal = base.UnwrapSeal
|
2017-07-31 15:28:06 +00:00
|
|
|
coreConfig.DevToken = base.DevToken
|
2018-06-03 22:14:51 +00:00
|
|
|
coreConfig.EnableRaw = base.EnableRaw
|
2018-09-18 03:03:00 +00:00
|
|
|
coreConfig.DisableSealWrap = base.DisableSealWrap
|
|
|
|
coreConfig.DisableCache = base.DisableCache
|
2019-04-11 15:12:37 +00:00
|
|
|
coreConfig.LicensingConfig = base.LicensingConfig
|
2021-06-03 17:30:30 +00:00
|
|
|
coreConfig.License = base.License
|
|
|
|
coreConfig.LicensePath = base.LicensePath
|
2019-06-21 17:38:21 +00:00
|
|
|
coreConfig.DisablePerformanceStandby = base.DisablePerformanceStandby
|
2019-07-01 16:08:55 +00:00
|
|
|
coreConfig.MetricsHelper = base.MetricsHelper
|
2020-10-15 21:15:58 +00:00
|
|
|
coreConfig.MetricSink = base.MetricSink
|
2019-10-17 17:33:00 +00:00
|
|
|
coreConfig.SecureRandomReader = base.SecureRandomReader
|
2020-08-10 10:23:44 +00:00
|
|
|
coreConfig.DisableSentinelTrace = base.DisableSentinelTrace
|
2020-10-29 17:55:26 +00:00
|
|
|
coreConfig.ClusterName = base.ClusterName
|
2021-03-03 18:59:50 +00:00
|
|
|
coreConfig.DisableAutopilot = base.DisableAutopilot
|
2020-07-09 21:16:31 +00:00
|
|
|
|
2018-11-07 01:21:24 +00:00
|
|
|
if base.BuiltinRegistry != nil {
|
|
|
|
coreConfig.BuiltinRegistry = base.BuiltinRegistry
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
|
|
|
|
if !coreConfig.DisableMlock {
|
|
|
|
base.DisableMlock = false
|
|
|
|
}
|
|
|
|
|
2017-02-28 23:17:19 +00:00
|
|
|
if base.Physical != nil {
|
|
|
|
coreConfig.Physical = base.Physical
|
|
|
|
}
|
|
|
|
|
|
|
|
if base.HAPhysical != nil {
|
|
|
|
coreConfig.HAPhysical = base.HAPhysical
|
|
|
|
}
|
|
|
|
|
2016-08-15 13:42:42 +00:00
|
|
|
// Used to set something non-working to test fallback
|
|
|
|
switch base.ClusterAddr {
|
|
|
|
case "empty":
|
|
|
|
coreConfig.ClusterAddr = ""
|
|
|
|
case "":
|
|
|
|
default:
|
|
|
|
coreConfig.ClusterAddr = base.ClusterAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
if base.LogicalBackends != nil {
|
|
|
|
for k, v := range base.LogicalBackends {
|
|
|
|
coreConfig.LogicalBackends[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if base.CredentialBackends != nil {
|
|
|
|
for k, v := range base.CredentialBackends {
|
|
|
|
coreConfig.CredentialBackends[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if base.AuditBackends != nil {
|
|
|
|
for k, v := range base.AuditBackends {
|
|
|
|
coreConfig.AuditBackends[k] = v
|
|
|
|
}
|
|
|
|
}
|
2016-12-02 11:31:06 +00:00
|
|
|
if base.Logger != nil {
|
|
|
|
coreConfig.Logger = base.Logger
|
|
|
|
}
|
2017-07-31 19:31:44 +00:00
|
|
|
|
2017-08-30 20:28:23 +00:00
|
|
|
coreConfig.ClusterCipherSuites = base.ClusterCipherSuites
|
2017-07-31 19:31:44 +00:00
|
|
|
coreConfig.DisableCache = base.DisableCache
|
|
|
|
coreConfig.DevToken = base.DevToken
|
2019-10-15 04:55:31 +00:00
|
|
|
coreConfig.RecoveryMode = base.RecoveryMode
|
2020-10-29 23:47:34 +00:00
|
|
|
coreConfig.ActivityLogConfig = base.ActivityLogConfig
|
2021-04-20 22:25:04 +00:00
|
|
|
coreConfig.EnableResponseHeaderHostname = base.EnableResponseHeaderHostname
|
|
|
|
coreConfig.EnableResponseHeaderRaftNodeID = base.EnableResponseHeaderRaftNodeID
|
2022-10-13 13:59:07 +00:00
|
|
|
coreConfig.RollbackPeriod = base.RollbackPeriod
|
2022-12-14 18:06:33 +00:00
|
|
|
coreConfig.PendingRemovalMountsAllowed = base.PendingRemovalMountsAllowed
|
2022-12-15 18:09:36 +00:00
|
|
|
coreConfig.ExpirationRevokeRetryBase = base.ExpirationRevokeRetryBase
|
2020-09-22 21:47:13 +00:00
|
|
|
testApplyEntBaseConfig(coreConfig, base)
|
2019-07-02 22:18:40 +00:00
|
|
|
}
|
2020-10-29 17:55:26 +00:00
|
|
|
if coreConfig.ClusterName == "" {
|
|
|
|
coreConfig.ClusterName = t.Name()
|
|
|
|
}
|
2019-07-02 22:18:40 +00:00
|
|
|
|
2020-10-29 23:47:34 +00:00
|
|
|
if coreConfig.ClusterName == "" {
|
|
|
|
coreConfig.ClusterName = t.Name()
|
|
|
|
}
|
|
|
|
|
2020-07-27 20:10:26 +00:00
|
|
|
if coreConfig.ClusterHeartbeatInterval == 0 {
|
|
|
|
// Set this lower so that state populates quickly to standby nodes
|
|
|
|
coreConfig.ClusterHeartbeatInterval = 2 * time.Second
|
|
|
|
}
|
|
|
|
|
2019-10-08 17:57:15 +00:00
|
|
|
if coreConfig.RawConfig == nil {
|
2020-05-21 20:07:50 +00:00
|
|
|
c := new(server.Config)
|
|
|
|
c.SharedConfig = &configutil.SharedConfig{LogFormat: logging.UnspecifiedFormat.String()}
|
|
|
|
coreConfig.RawConfig = c
|
2019-10-08 17:57:15 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 22:18:40 +00:00
|
|
|
addAuditBackend := len(coreConfig.AuditBackends) == 0
|
|
|
|
if addAuditBackend {
|
2023-02-01 13:33:16 +00:00
|
|
|
coreConfig.AuditBackends["noop"] = corehelpers.NoopAuditFactory(nil)
|
2016-12-01 20:25:05 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 19:14:58 +00:00
|
|
|
if coreConfig.Physical == nil && (opts == nil || opts.PhysicalFactory == nil) {
|
2019-07-23 19:17:37 +00:00
|
|
|
coreConfig.Physical, err = physInmem.NewInmem(nil, testCluster.Logger)
|
2017-08-03 17:24:27 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-02-28 23:17:19 +00:00
|
|
|
}
|
2019-06-20 19:14:58 +00:00
|
|
|
if coreConfig.HAPhysical == nil && (opts == nil || opts.PhysicalFactory == nil) {
|
2019-07-23 19:17:37 +00:00
|
|
|
haPhys, err := physInmem.NewInmemHA(nil, testCluster.Logger)
|
2017-08-03 17:24:27 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
coreConfig.HAPhysical = haPhys.(physical.HABackend)
|
2017-02-28 23:17:19 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 13:46:15 +00:00
|
|
|
if testCluster.LicensePublicKey == nil {
|
|
|
|
pubKey, priKey, err := GenerateTestLicenseKeys()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
testCluster.LicensePublicKey = pubKey
|
|
|
|
testCluster.LicensePrivateKey = priKey
|
|
|
|
}
|
|
|
|
|
2021-03-17 21:23:13 +00:00
|
|
|
if opts != nil && opts.InmemClusterLayers {
|
|
|
|
if opts.ClusterLayers != nil {
|
|
|
|
t.Fatalf("cannot specify ClusterLayers when InmemClusterLayers is true")
|
|
|
|
}
|
|
|
|
inmemCluster, err := cluster.NewInmemLayerCluster("inmem-cluster", numCores, testCluster.Logger.Named("inmem-cluster"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
opts.ClusterLayers = inmemCluster
|
|
|
|
}
|
|
|
|
|
2023-02-03 22:27:11 +00:00
|
|
|
if opts != nil && opts.Plugins != nil {
|
|
|
|
var pluginDir string
|
|
|
|
var cleanup func(t testing.T)
|
|
|
|
|
|
|
|
if coreConfig.PluginDirectory == "" {
|
|
|
|
pluginDir, cleanup = corehelpers.MakeTestPluginDir(t)
|
|
|
|
coreConfig.PluginDirectory = pluginDir
|
|
|
|
t.Cleanup(func() { cleanup(t) })
|
|
|
|
}
|
|
|
|
|
|
|
|
var plugins []pluginhelpers.TestPlugin
|
|
|
|
for _, version := range opts.Plugins.Versions {
|
|
|
|
plugins = append(plugins, pluginhelpers.CompilePlugin(t, opts.Plugins.Typ, version, coreConfig.PluginDirectory))
|
|
|
|
}
|
|
|
|
testCluster.Plugins = plugins
|
|
|
|
}
|
|
|
|
|
2020-06-16 18:12:22 +00:00
|
|
|
// Create cores
|
|
|
|
testCluster.cleanupFuncs = []func(){}
|
2017-09-01 05:02:03 +00:00
|
|
|
cores := []*Core{}
|
2018-10-23 06:34:02 +00:00
|
|
|
coreConfigs := []*CoreConfig{}
|
2020-06-16 18:12:22 +00:00
|
|
|
|
2017-09-01 05:02:03 +00:00
|
|
|
for i := 0; i < numCores; i++ {
|
2023-04-12 13:46:15 +00:00
|
|
|
cleanup, c, localConfig, handler := testCluster.newCore(t, i, coreConfig, opts, listeners[i], testCluster.LicensePublicKey)
|
2020-06-16 18:12:22 +00:00
|
|
|
|
|
|
|
testCluster.cleanupFuncs = append(testCluster.cleanupFuncs, cleanup)
|
|
|
|
cores = append(cores, c)
|
|
|
|
coreConfigs = append(coreConfigs, &localConfig)
|
|
|
|
|
|
|
|
if handler != nil {
|
|
|
|
handlers[i] = handler
|
|
|
|
servers[i].Handler = handlers[i]
|
2019-06-20 19:14:58 +00:00
|
|
|
}
|
2020-06-16 18:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clustering setup
|
|
|
|
for i := 0; i < numCores; i++ {
|
|
|
|
testCluster.setupClusterListener(t, i, cores[i], coreConfigs[i], opts, listeners[i], handlers[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create TestClusterCores
|
|
|
|
var ret []*TestClusterCore
|
|
|
|
for i := 0; i < numCores; i++ {
|
|
|
|
tcc := &TestClusterCore{
|
|
|
|
Core: cores[i],
|
|
|
|
CoreConfig: coreConfigs[i],
|
|
|
|
ServerKey: certInfoSlice[i].key,
|
|
|
|
ServerKeyPEM: certInfoSlice[i].keyPEM,
|
|
|
|
ServerCert: certInfoSlice[i].cert,
|
|
|
|
ServerCertBytes: certInfoSlice[i].certBytes,
|
|
|
|
ServerCertPEM: certInfoSlice[i].certPEM,
|
|
|
|
Address: addresses[i],
|
|
|
|
Listeners: listeners[i],
|
|
|
|
Handler: handlers[i],
|
|
|
|
Server: servers[i],
|
2023-01-31 16:05:16 +00:00
|
|
|
tlsConfig: tlsConfigs[i],
|
2020-06-16 18:12:22 +00:00
|
|
|
Barrier: cores[i].barrier,
|
|
|
|
NodeID: fmt.Sprintf("core-%d", i),
|
|
|
|
UnderlyingRawStorage: coreConfigs[i].Physical,
|
2020-06-23 19:04:13 +00:00
|
|
|
UnderlyingHAStorage: coreConfigs[i].HAPhysical,
|
2020-01-17 07:03:02 +00:00
|
|
|
}
|
2020-06-16 18:12:22 +00:00
|
|
|
tcc.ReloadFuncs = &cores[i].reloadFuncs
|
|
|
|
tcc.ReloadFuncsLock = &cores[i].reloadFuncsLock
|
|
|
|
tcc.ReloadFuncsLock.Lock()
|
|
|
|
(*tcc.ReloadFuncs)["listener|tcp"] = []reloadutil.ReloadFunc{certGetters[i].Reload}
|
|
|
|
tcc.ReloadFuncsLock.Unlock()
|
2020-01-17 07:03:02 +00:00
|
|
|
|
2020-07-10 17:11:18 +00:00
|
|
|
testAdjustUnderlyingStorage(tcc)
|
2020-06-16 18:12:22 +00:00
|
|
|
|
|
|
|
ret = append(ret, tcc)
|
|
|
|
}
|
|
|
|
testCluster.Cores = ret
|
|
|
|
|
|
|
|
// Initialize cores
|
|
|
|
if opts == nil || !opts.SkipInit {
|
|
|
|
testCluster.initCores(t, opts, addAuditBackend)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign clients
|
|
|
|
for i := 0; i < numCores; i++ {
|
2022-01-27 18:06:34 +00:00
|
|
|
testCluster.Cores[i].Client = testCluster.getAPIClient(t, opts, listeners[i][0].Address.Port, tlsConfigs[i])
|
2020-06-16 18:12:22 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 13:46:15 +00:00
|
|
|
// Extra Setup
|
|
|
|
for _, tcc := range testCluster.Cores {
|
|
|
|
testExtraTestCoreSetup(t, testCluster.LicensePrivateKey, tcc)
|
|
|
|
}
|
|
|
|
|
2020-06-16 18:12:22 +00:00
|
|
|
// Cleanup
|
|
|
|
testCluster.CleanupFunc = func() {
|
|
|
|
for _, c := range testCluster.cleanupFuncs {
|
|
|
|
c()
|
|
|
|
}
|
2023-02-01 13:33:16 +00:00
|
|
|
if l, ok := testCluster.Logger.(*corehelpers.TestLogger); ok {
|
2020-06-16 18:12:22 +00:00
|
|
|
if t.Failed() {
|
|
|
|
_ = l.File.Close()
|
|
|
|
} else {
|
|
|
|
_ = os.Remove(l.Path)
|
2019-04-12 15:37:34 +00:00
|
|
|
}
|
2019-04-11 15:12:37 +00:00
|
|
|
}
|
2020-06-16 18:12:22 +00:00
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
|
2020-06-16 18:12:22 +00:00
|
|
|
// Setup
|
|
|
|
if opts != nil {
|
|
|
|
if opts.SetupFunc != nil {
|
|
|
|
testCluster.SetupFunc = func() {
|
|
|
|
opts.SetupFunc(t, &testCluster)
|
|
|
|
}
|
2019-09-17 00:50:51 +00:00
|
|
|
}
|
2020-06-16 18:12:22 +00:00
|
|
|
}
|
2019-09-17 00:50:51 +00:00
|
|
|
|
2022-11-29 19:38:33 +00:00
|
|
|
testCluster.opts = opts
|
|
|
|
testCluster.start(t)
|
2020-06-16 18:12:22 +00:00
|
|
|
return &testCluster
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopCore performs an orderly shutdown of a core.
|
2021-02-18 20:40:18 +00:00
|
|
|
func (cluster *TestCluster) StopCore(t testing.T, idx int) {
|
2020-06-16 18:12:22 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
if idx < 0 || idx > len(cluster.Cores) {
|
|
|
|
t.Fatalf("invalid core index %d", idx)
|
|
|
|
}
|
|
|
|
tcc := cluster.Cores[idx]
|
|
|
|
tcc.Logger().Info("stopping core", "core", idx)
|
|
|
|
|
2021-04-29 18:32:41 +00:00
|
|
|
// Stop listeners and call Finalize()
|
2020-06-16 18:12:22 +00:00
|
|
|
if err := tcc.stop(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run cleanup
|
|
|
|
cluster.cleanupFuncs[idx]()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restart a TestClusterCore that was stopped, by replacing the
|
|
|
|
// underlying Core.
|
2021-02-18 20:40:18 +00:00
|
|
|
func (cluster *TestCluster) StartCore(t testing.T, idx int, opts *TestClusterOptions) {
|
2020-06-16 18:12:22 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
if idx < 0 || idx > len(cluster.Cores) {
|
|
|
|
t.Fatalf("invalid core index %d", idx)
|
|
|
|
}
|
|
|
|
tcc := cluster.Cores[idx]
|
|
|
|
tcc.Logger().Info("restarting core", "core", idx)
|
|
|
|
|
|
|
|
// Set up listeners
|
|
|
|
ln, err := net.ListenTCP("tcp", tcc.Address)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-04-08 16:43:39 +00:00
|
|
|
tcc.Listeners = []*TestListener{
|
|
|
|
{
|
2023-01-31 16:05:16 +00:00
|
|
|
Listener: tls.NewListener(ln, tcc.tlsConfig),
|
2021-04-08 16:43:39 +00:00
|
|
|
Address: ln.Addr().(*net.TCPAddr),
|
|
|
|
},
|
2020-06-16 18:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tcc.Handler = http.NewServeMux()
|
|
|
|
tcc.Server = &http.Server{
|
|
|
|
Handler: tcc.Handler,
|
|
|
|
ErrorLog: cluster.Logger.StandardLogger(nil),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new Core
|
2023-04-12 13:46:15 +00:00
|
|
|
cleanup, newCore, localConfig, coreHandler := cluster.newCore(t, idx, tcc.CoreConfig, opts, tcc.Listeners, cluster.LicensePublicKey)
|
2020-06-16 18:12:22 +00:00
|
|
|
if coreHandler != nil {
|
|
|
|
tcc.Handler = coreHandler
|
|
|
|
tcc.Server.Handler = coreHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
cluster.cleanupFuncs[idx] = cleanup
|
|
|
|
tcc.Core = newCore
|
|
|
|
tcc.CoreConfig = &localConfig
|
|
|
|
tcc.UnderlyingRawStorage = localConfig.Physical
|
|
|
|
|
|
|
|
cluster.setupClusterListener(
|
|
|
|
t, idx, newCore, tcc.CoreConfig,
|
|
|
|
opts, tcc.Listeners, tcc.Handler)
|
|
|
|
|
2023-01-31 16:05:16 +00:00
|
|
|
tcc.Client = cluster.getAPIClient(t, opts, tcc.Listeners[0].Address.Port, tcc.tlsConfig)
|
2020-06-16 18:12:22 +00:00
|
|
|
|
2020-07-10 17:11:18 +00:00
|
|
|
testAdjustUnderlyingStorage(tcc)
|
2023-04-12 13:46:15 +00:00
|
|
|
testExtraTestCoreSetup(t, cluster.LicensePrivateKey, tcc)
|
2020-06-16 18:12:22 +00:00
|
|
|
|
|
|
|
// Start listeners
|
|
|
|
for _, ln := range tcc.Listeners {
|
|
|
|
tcc.Logger().Info("starting listener for core", "port", ln.Address.Port)
|
|
|
|
go tcc.Server.Serve(ln)
|
|
|
|
}
|
|
|
|
|
|
|
|
tcc.Logger().Info("restarted test core", "core", idx)
|
|
|
|
}
|
|
|
|
|
2023-04-12 13:46:15 +00:00
|
|
|
func (testCluster *TestCluster) newCore(t testing.T, idx int, coreConfig *CoreConfig, opts *TestClusterOptions, listeners []*TestListener, pubKey ed25519.PublicKey) (func(), *Core, CoreConfig, http.Handler) {
|
2020-06-16 18:12:22 +00:00
|
|
|
localConfig := *coreConfig
|
|
|
|
cleanupFunc := func() {}
|
|
|
|
var handler http.Handler
|
|
|
|
|
|
|
|
var disablePR1103 bool
|
|
|
|
if opts != nil && opts.PR1103Disabled {
|
|
|
|
disablePR1103 = true
|
|
|
|
}
|
|
|
|
|
|
|
|
var firstCoreNumber int
|
|
|
|
if opts != nil {
|
|
|
|
firstCoreNumber = opts.FirstCoreNumber
|
|
|
|
}
|
|
|
|
|
|
|
|
localConfig.RedirectAddr = fmt.Sprintf("https://127.0.0.1:%d", listeners[0].Address.Port)
|
|
|
|
|
|
|
|
// if opts.SealFunc is provided, use that to generate a seal for the config instead
|
|
|
|
if opts != nil && opts.SealFunc != nil {
|
|
|
|
localConfig.Seal = opts.SealFunc()
|
|
|
|
}
|
2020-10-23 18:16:04 +00:00
|
|
|
if opts != nil && opts.UnwrapSealFunc != nil {
|
|
|
|
localConfig.UnwrapSeal = opts.UnwrapSealFunc()
|
|
|
|
}
|
2020-06-16 18:12:22 +00:00
|
|
|
|
|
|
|
if coreConfig.Logger == nil || (opts != nil && opts.Logger != nil) {
|
|
|
|
localConfig.Logger = testCluster.Logger.Named(fmt.Sprintf("core%d", idx))
|
|
|
|
}
|
2022-10-06 18:24:16 +00:00
|
|
|
|
|
|
|
if opts != nil && opts.EffectiveSDKVersionMap != nil {
|
|
|
|
localConfig.EffectiveSDKVersion = opts.EffectiveSDKVersionMap[idx]
|
|
|
|
}
|
|
|
|
|
2020-06-16 18:12:22 +00:00
|
|
|
if opts != nil && opts.PhysicalFactory != nil {
|
2022-05-20 20:49:11 +00:00
|
|
|
pfc := opts.PhysicalFactoryConfig
|
|
|
|
if pfc == nil {
|
|
|
|
pfc = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
if len(opts.VersionMap) > 0 {
|
|
|
|
pfc["autopilot_upgrade_version"] = opts.VersionMap[idx]
|
|
|
|
}
|
|
|
|
if len(opts.RedundancyZoneMap) > 0 {
|
|
|
|
pfc["autopilot_redundancy_zone"] = opts.RedundancyZoneMap[idx]
|
|
|
|
}
|
|
|
|
physBundle := opts.PhysicalFactory(t, idx, localConfig.Logger, pfc)
|
2020-06-16 18:12:22 +00:00
|
|
|
switch {
|
|
|
|
case physBundle == nil && coreConfig.Physical != nil:
|
|
|
|
case physBundle == nil && coreConfig.Physical == nil:
|
|
|
|
t.Fatal("PhysicalFactory produced no physical and none in CoreConfig")
|
|
|
|
case physBundle != nil:
|
2020-06-23 19:04:13 +00:00
|
|
|
// Storage backend setup
|
|
|
|
if physBundle.Backend != nil {
|
|
|
|
testCluster.Logger.Info("created physical backend", "instance", idx)
|
|
|
|
coreConfig.Physical = physBundle.Backend
|
|
|
|
localConfig.Physical = physBundle.Backend
|
|
|
|
}
|
|
|
|
|
|
|
|
// HA Backend setup
|
2020-06-16 18:12:22 +00:00
|
|
|
haBackend := physBundle.HABackend
|
|
|
|
if haBackend == nil {
|
|
|
|
if ha, ok := physBundle.Backend.(physical.HABackend); ok {
|
|
|
|
haBackend = ha
|
|
|
|
}
|
|
|
|
}
|
|
|
|
coreConfig.HAPhysical = haBackend
|
|
|
|
localConfig.HAPhysical = haBackend
|
2020-06-23 19:04:13 +00:00
|
|
|
|
|
|
|
// Cleanup setup
|
2020-06-16 18:12:22 +00:00
|
|
|
if physBundle.Cleanup != nil {
|
|
|
|
cleanupFunc = physBundle.Cleanup
|
2019-10-15 04:55:31 +00:00
|
|
|
}
|
2017-09-01 05:02:03 +00:00
|
|
|
}
|
2020-06-16 18:12:22 +00:00
|
|
|
}
|
2018-10-23 06:34:02 +00:00
|
|
|
|
2020-06-16 18:12:22 +00:00
|
|
|
if opts != nil && opts.ClusterLayers != nil {
|
|
|
|
localConfig.ClusterNetworkLayer = opts.ClusterLayers.Layers()[idx]
|
2021-03-03 18:59:50 +00:00
|
|
|
localConfig.ClusterAddr = "https://" + localConfig.ClusterNetworkLayer.Listeners()[0].Addr().String()
|
2020-06-16 18:12:22 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 13:46:15 +00:00
|
|
|
switch {
|
|
|
|
case localConfig.LicensingConfig != nil:
|
|
|
|
if pubKey != nil {
|
|
|
|
localConfig.LicensingConfig.AdditionalPublicKeys = append(localConfig.LicensingConfig.AdditionalPublicKeys, pubKey)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
localConfig.LicensingConfig = testGetLicensingConfig(pubKey)
|
|
|
|
}
|
2020-06-16 18:12:22 +00:00
|
|
|
|
|
|
|
if localConfig.MetricsHelper == nil {
|
|
|
|
inm := metrics.NewInmemSink(10*time.Second, time.Minute)
|
|
|
|
metrics.DefaultInmemSignal(inm)
|
|
|
|
localConfig.MetricsHelper = metricsutil.NewMetricsHelper(inm, false)
|
|
|
|
}
|
2020-10-29 17:55:26 +00:00
|
|
|
if opts != nil && opts.CoreMetricSinkProvider != nil {
|
2020-10-29 23:47:34 +00:00
|
|
|
localConfig.MetricSink, localConfig.MetricsHelper = opts.CoreMetricSinkProvider(localConfig.ClusterName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts != nil && opts.CoreMetricSinkProvider != nil {
|
2020-10-29 17:55:26 +00:00
|
|
|
localConfig.MetricSink, localConfig.MetricsHelper = opts.CoreMetricSinkProvider(localConfig.ClusterName)
|
|
|
|
}
|
2020-06-16 18:12:22 +00:00
|
|
|
|
Vault-1403 Switch Expiration Manager to use Fairsharing Backpressure (#1709) (#10932)
* basic pool and start testing
* refactor a bit for testing
* workFunc, start/stop safety, testing
* cleanup function for worker quit, more tests
* redo public/private members
* improve tests, export types, switch uuid package
* fix loop capture bug, cleanup
* cleanup tests
* update worker pool file name, other improvements
* add job manager prototype
* remove remnants
* add functions to wait for job manager and worker pool to stop, other fixes
* test job manager functionality, fix bugs
* encapsulate how jobs are distributed to workers
* make worker job channel read only
* add job interface, more testing, fixes
* set name for dispatcher
* fix test races
* wire up expiration manager most of the way
* dispatcher and job manager constructors don't return errors
* logger now dependency injected
* make some members private, test fcn to get worker pool size
* make GetNumWorkers public
* Update helper/fairshare/jobmanager_test.go
Co-authored-by: Brian Kassouf <briankassouf@users.noreply.github.com>
* update fairsharing usage, add tests
* make workerpool private
* remove custom worker names
* concurrency improvements
* remove worker pool cleanup function
* remove cleanup func from job manager, remove non blocking stop from fairshare
* update job manager for new constructor
* stop job manager when expiration manager stopped
* unset env var after test
* stop fairshare when started in tests
* stop leaking job manager goroutine
* prototype channel for waking up to assign work
* fix typo/bug and add tests
* improve job manager wake up, fix test typo
* put channel drain back
* better start/pause test for job manager
* comment cleanup
* degrade possible noisy log
* remove closure, clean up context
* improve revocation context timer
* test: reduce number of revocation workers during many tests
* Update vault/expiration.go
Co-authored-by: Brian Kassouf <briankassouf@users.noreply.github.com>
* feedback tweaks
Co-authored-by: Brian Kassouf <briankassouf@users.noreply.github.com>
Co-authored-by: Brian Kassouf <briankassouf@users.noreply.github.com>
2021-02-17 22:30:27 +00:00
|
|
|
localConfig.NumExpirationWorkers = numExpirationWorkersTest
|
|
|
|
|
2020-06-16 18:12:22 +00:00
|
|
|
c, err := NewCore(&localConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
c.coreNumber = firstCoreNumber + idx
|
|
|
|
c.PR1103disabled = disablePR1103
|
|
|
|
if opts != nil && opts.HandlerFunc != nil {
|
|
|
|
props := opts.DefaultHandlerProperties
|
|
|
|
props.Core = c
|
|
|
|
if props.ListenerConfig != nil && props.ListenerConfig.MaxRequestDuration == 0 {
|
|
|
|
props.ListenerConfig.MaxRequestDuration = DefaultMaxRequestDuration
|
|
|
|
}
|
2022-11-29 19:38:33 +00:00
|
|
|
handler = opts.HandlerFunc.Handler(&props)
|
2020-06-16 18:12:22 +00:00
|
|
|
}
|
|
|
|
|
2023-02-15 19:57:57 +00:00
|
|
|
if opts != nil && opts.RequestResponseCallback != nil {
|
|
|
|
c.requestResponseCallback = opts.RequestResponseCallback
|
|
|
|
}
|
|
|
|
|
2020-06-16 18:12:22 +00:00
|
|
|
// Set this in case the Seal was manually set before the core was
|
|
|
|
// created
|
|
|
|
if localConfig.Seal != nil {
|
|
|
|
localConfig.Seal.SetCore(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cleanupFunc, c, localConfig, handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (testCluster *TestCluster) setupClusterListener(
|
2021-02-18 20:40:18 +00:00
|
|
|
t testing.T, idx int, core *Core, coreConfig *CoreConfig,
|
2022-04-14 20:54:23 +00:00
|
|
|
opts *TestClusterOptions, listeners []*TestListener, handler http.Handler,
|
|
|
|
) {
|
2020-06-16 18:12:22 +00:00
|
|
|
if coreConfig.ClusterAddr == "" {
|
|
|
|
return
|
2017-07-31 15:28:06 +00:00
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
|
2020-05-14 12:31:02 +00:00
|
|
|
clusterAddrGen := func(lns []*TestListener, port int) []*net.TCPAddr {
|
2016-08-19 15:03:53 +00:00
|
|
|
ret := make([]*net.TCPAddr, len(lns))
|
2016-08-15 13:42:42 +00:00
|
|
|
for i, ln := range lns {
|
2016-08-19 15:03:53 +00:00
|
|
|
ret[i] = &net.TCPAddr{
|
|
|
|
IP: ln.Address.IP,
|
2020-05-14 12:31:02 +00:00
|
|
|
Port: port,
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2020-06-16 18:12:22 +00:00
|
|
|
baseClusterListenPort := 0
|
|
|
|
if opts != nil && opts.BaseClusterListenPort != 0 {
|
|
|
|
if opts.BaseListenAddress == "" {
|
|
|
|
t.Fatal("BaseListenAddress is not specified")
|
2017-09-01 05:02:03 +00:00
|
|
|
}
|
2020-06-16 18:12:22 +00:00
|
|
|
baseClusterListenPort = opts.BaseClusterListenPort
|
2017-09-01 05:02:03 +00:00
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
|
2020-06-16 18:12:22 +00:00
|
|
|
port := 0
|
|
|
|
if baseClusterListenPort != 0 {
|
|
|
|
port = baseClusterListenPort + idx
|
|
|
|
}
|
|
|
|
core.Logger().Info("assigning cluster listener for test core", "core", idx, "port", port)
|
|
|
|
core.SetClusterListenerAddrs(clusterAddrGen(listeners, port))
|
|
|
|
core.SetClusterHandler(handler)
|
|
|
|
}
|
|
|
|
|
2021-02-18 20:40:18 +00:00
|
|
|
func (tc *TestCluster) initCores(t testing.T, opts *TestClusterOptions, addAuditBackend bool) {
|
2020-06-16 18:12:22 +00:00
|
|
|
leader := tc.Cores[0]
|
|
|
|
|
|
|
|
bKeys, rKeys, root := TestCoreInitClusterWrapperSetup(t, leader.Core, leader.Handler)
|
|
|
|
barrierKeys, _ := copystructure.Copy(bKeys)
|
|
|
|
tc.BarrierKeys = barrierKeys.([][]byte)
|
|
|
|
recoveryKeys, _ := copystructure.Copy(rKeys)
|
|
|
|
tc.RecoveryKeys = recoveryKeys.([][]byte)
|
|
|
|
tc.RootToken = root
|
|
|
|
|
|
|
|
// Write root token and barrier keys
|
2021-04-08 16:43:39 +00:00
|
|
|
err := ioutil.WriteFile(filepath.Join(tc.TempDir, "root_token"), []byte(root), 0o755)
|
2020-06-16 18:12:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
for i, key := range tc.BarrierKeys {
|
|
|
|
buf.Write([]byte(base64.StdEncoding.EncodeToString(key)))
|
|
|
|
if i < len(tc.BarrierKeys)-1 {
|
|
|
|
buf.WriteRune('\n')
|
2017-10-23 18:59:37 +00:00
|
|
|
}
|
2020-06-16 18:12:22 +00:00
|
|
|
}
|
2021-04-08 16:43:39 +00:00
|
|
|
err = ioutil.WriteFile(filepath.Join(tc.TempDir, "barrier_keys"), buf.Bytes(), 0o755)
|
2020-06-16 18:12:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for i, key := range tc.RecoveryKeys {
|
|
|
|
buf.Write([]byte(base64.StdEncoding.EncodeToString(key)))
|
|
|
|
if i < len(tc.RecoveryKeys)-1 {
|
|
|
|
buf.WriteRune('\n')
|
2017-10-23 18:59:37 +00:00
|
|
|
}
|
2020-06-16 18:12:22 +00:00
|
|
|
}
|
2021-04-08 16:43:39 +00:00
|
|
|
err = ioutil.WriteFile(filepath.Join(tc.TempDir, "recovery_keys"), buf.Bytes(), 0o755)
|
2020-06-16 18:12:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-31 15:28:06 +00:00
|
|
|
|
2020-06-16 18:12:22 +00:00
|
|
|
// Unseal first core
|
|
|
|
for _, key := range bKeys {
|
|
|
|
if _, err := leader.Core.Unseal(TestKeyCopy(key)); err != nil {
|
|
|
|
t.Fatalf("unseal err: %s", err)
|
2017-01-17 20:43:10 +00:00
|
|
|
}
|
2020-06-16 18:12:22 +00:00
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
|
2020-06-16 18:12:22 +00:00
|
|
|
ctx := context.Background()
|
2018-01-19 06:44:44 +00:00
|
|
|
|
2020-06-16 18:12:22 +00:00
|
|
|
// If stored keys is supported, the above will no no-op, so trigger auto-unseal
|
|
|
|
// using stored keys to try to unseal
|
|
|
|
if err := leader.Core.UnsealWithStoredKeys(ctx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-10-23 18:59:37 +00:00
|
|
|
|
2020-06-16 18:12:22 +00:00
|
|
|
// Verify unsealed
|
|
|
|
if leader.Core.Sealed() {
|
|
|
|
t.Fatal("should not be sealed")
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
|
2020-06-16 18:12:22 +00:00
|
|
|
TestWaitActive(t, leader.Core)
|
2016-08-15 13:42:42 +00:00
|
|
|
|
2022-09-13 16:11:00 +00:00
|
|
|
kvVersion := "1"
|
|
|
|
if opts != nil {
|
|
|
|
kvVersion = opts.KVVersion
|
|
|
|
}
|
|
|
|
|
2020-06-16 18:12:22 +00:00
|
|
|
// Existing tests rely on this; we can make a toggle to disable it
|
|
|
|
// later if we want
|
|
|
|
kvReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
ClientToken: tc.RootToken,
|
|
|
|
Path: "sys/mounts/secret",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"type": "kv",
|
|
|
|
"path": "secret/",
|
|
|
|
"description": "key/value secret storage",
|
|
|
|
"options": map[string]string{
|
2022-09-13 16:11:00 +00:00
|
|
|
"version": kvVersion,
|
2019-02-14 19:55:32 +00:00
|
|
|
},
|
2020-06-16 18:12:22 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := leader.Core.HandleRequest(namespace.RootContext(ctx), kvReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp.IsError() {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-10-18 18:46:00 +00:00
|
|
|
|
2020-06-16 18:12:22 +00:00
|
|
|
cfg, err := leader.Core.seal.BarrierConfig(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-10-23 18:59:37 +00:00
|
|
|
|
2020-06-16 18:12:22 +00:00
|
|
|
// Unseal other cores unless otherwise specified
|
|
|
|
numCores := len(tc.Cores)
|
|
|
|
if (opts == nil || !opts.KeepStandbysSealed) && numCores > 1 {
|
|
|
|
for i := 1; i < numCores; i++ {
|
|
|
|
tc.Cores[i].Core.seal.SetCachedBarrierConfig(cfg)
|
|
|
|
for _, key := range bKeys {
|
|
|
|
if _, err := tc.Cores[i].Core.Unseal(TestKeyCopy(key)); err != nil {
|
|
|
|
t.Fatalf("unseal err: %s", err)
|
2017-10-23 18:59:37 +00:00
|
|
|
}
|
2017-01-17 20:43:10 +00:00
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
|
2020-06-16 18:12:22 +00:00
|
|
|
// If stored keys is supported, the above will no no-op, so trigger auto-unseal
|
|
|
|
// using stored keys
|
|
|
|
if err := tc.Cores[i].Core.UnsealWithStoredKeys(ctx); err != nil {
|
|
|
|
t.Fatal(err)
|
2017-09-01 05:02:03 +00:00
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 18:12:22 +00:00
|
|
|
// Let them come fully up to standby
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
|
|
|
// Ensure cluster connection info is populated.
|
|
|
|
// Other cores should not come up as leaders.
|
|
|
|
for i := 1; i < numCores; i++ {
|
|
|
|
isLeader, _, _, err := tc.Cores[i].Core.Leader()
|
2019-07-02 22:18:40 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-06-16 18:12:22 +00:00
|
|
|
if isLeader {
|
|
|
|
t.Fatalf("core[%d] should not be leader", i)
|
2019-07-02 22:18:40 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-28 23:17:19 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 18:12:22 +00:00
|
|
|
//
|
|
|
|
// Set test cluster core(s) and test cluster
|
|
|
|
//
|
|
|
|
cluster, err := leader.Core.Cluster(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
tc.ID = cluster.ID
|
|
|
|
|
|
|
|
if addAuditBackend {
|
|
|
|
// Enable auditing.
|
|
|
|
auditReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
ClientToken: tc.RootToken,
|
|
|
|
Path: "sys/audit/noop",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"type": "noop",
|
2017-02-24 15:45:29 +00:00
|
|
|
},
|
|
|
|
}
|
2020-06-16 18:12:22 +00:00
|
|
|
resp, err = leader.Core.HandleRequest(namespace.RootContext(ctx), auditReq)
|
2017-02-24 15:45:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-06-16 18:12:22 +00:00
|
|
|
if resp.IsError() {
|
|
|
|
t.Fatal(err)
|
2017-09-01 05:02:03 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-16 18:12:22 +00:00
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
|
2020-06-16 18:12:22 +00:00
|
|
|
func (testCluster *TestCluster) getAPIClient(
|
2021-02-18 20:40:18 +00:00
|
|
|
t testing.T, opts *TestClusterOptions,
|
2022-04-14 20:54:23 +00:00
|
|
|
port int, tlsConfig *tls.Config,
|
|
|
|
) *api.Client {
|
2020-06-16 18:12:22 +00:00
|
|
|
transport := cleanhttp.DefaultPooledTransport()
|
|
|
|
transport.TLSClientConfig = tlsConfig.Clone()
|
|
|
|
if err := http2.ConfigureTransport(transport); err != nil {
|
|
|
|
t.Fatal(err)
|
2019-08-23 19:51:25 +00:00
|
|
|
}
|
2020-06-16 18:12:22 +00:00
|
|
|
client := &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
CheckRedirect: func(*http.Request, []*http.Request) error {
|
|
|
|
// This can of course be overridden per-test by using its own client
|
|
|
|
return fmt.Errorf("redirects not allowed in these tests")
|
|
|
|
},
|
2019-08-23 19:51:25 +00:00
|
|
|
}
|
2020-06-16 18:12:22 +00:00
|
|
|
config := api.DefaultConfig()
|
|
|
|
if config.Error != nil {
|
|
|
|
t.Fatal(config.Error)
|
|
|
|
}
|
|
|
|
config.Address = fmt.Sprintf("https://127.0.0.1:%d", port)
|
|
|
|
config.HttpClient = client
|
|
|
|
config.MaxRetries = 0
|
|
|
|
apiClient, err := api.NewClient(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if opts == nil || !opts.SkipInit {
|
|
|
|
apiClient.SetToken(testCluster.RootToken)
|
|
|
|
}
|
|
|
|
return apiClient
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
2023-04-24 13:57:37 +00:00
|
|
|
|
|
|
|
func (c *TestCluster) GetBarrierOrRecoveryKeys() [][]byte {
|
|
|
|
if c.Cores[0].SealAccess().RecoveryKeySupported() {
|
|
|
|
return c.GetRecoveryKeys()
|
|
|
|
} else {
|
|
|
|
return c.GetBarrierKeys()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TestCluster) GetCACertPEMFile() string {
|
|
|
|
return c.CACertPEMFile
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TestCluster) ClusterID() string {
|
|
|
|
return c.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TestCluster) Nodes() []testcluster.VaultClusterNode {
|
|
|
|
ret := make([]testcluster.VaultClusterNode, len(c.Cores))
|
|
|
|
for i, core := range c.Cores {
|
|
|
|
ret[i] = core
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TestCluster) SetBarrierKeys(keys [][]byte) {
|
|
|
|
c.BarrierKeys = make([][]byte, len(keys))
|
|
|
|
for i, k := range keys {
|
|
|
|
c.BarrierKeys[i] = TestKeyCopy(k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TestCluster) SetRecoveryKeys(keys [][]byte) {
|
|
|
|
c.RecoveryKeys = make([][]byte, len(keys))
|
|
|
|
for i, k := range keys {
|
|
|
|
c.RecoveryKeys[i] = TestKeyCopy(k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TestCluster) GetBarrierKeys() [][]byte {
|
|
|
|
ret := make([][]byte, len(c.BarrierKeys))
|
|
|
|
for i, k := range c.BarrierKeys {
|
|
|
|
ret[i] = TestKeyCopy(k)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TestCluster) GetRecoveryKeys() [][]byte {
|
|
|
|
ret := make([][]byte, len(c.RecoveryKeys))
|
|
|
|
for i, k := range c.RecoveryKeys {
|
|
|
|
ret[i] = TestKeyCopy(k)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TestCluster) NamedLogger(name string) log.Logger {
|
|
|
|
return c.Logger.Named(name)
|
|
|
|
}
|
|
|
|
|
2023-05-03 17:49:45 +00:00
|
|
|
func (c *TestCluster) GetRootToken() string {
|
|
|
|
return c.RootToken
|
|
|
|
}
|
|
|
|
|
2023-04-24 13:57:37 +00:00
|
|
|
func (c *TestClusterCore) Name() string {
|
|
|
|
return c.NodeID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TestClusterCore) APIClient() *api.Client {
|
|
|
|
return c.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ testcluster.VaultCluster = &TestCluster{}
|
|
|
|
_ testcluster.VaultClusterNode = &TestClusterCore{}
|
|
|
|
)
|