mirror of
https://github.com/hashicorp/vault.git
synced 2025-08-07 23:27:01 +02:00
* Seal HA: Use new SealWrappedValue type to abstract seal wrapped values Introduce SealWrappedValue to abstract seal wrapped values. Make SealWrappedValue capable of marshalling into a BlobInfo, when there is plaintext or a single encryption, or to a custom serialization consisting of a header, length and a marshalled MultiWrapValue protobuf. * Vault-13769: Support configuring and using multiple seals for unsealing * Make sealWrapBackend start using multiple seals * Make seal.Access no longer implement wrapping.Wrapper. Instead, add the Encrypt and Decrypt methods to the Access interface. * Make raft snapshot system use funcs SealWrapValue + UnsealWrapValue. Move the snapshot.Sealer implementation to the vault package to avoid circular imports. * Update sealWrapBackend to use multiple seals for encryption. Use all the encryption wrappers when storing seal wrapped values. Try do decrypt using the highest priority wrapper, but try all combinations of encrypted values and wrappers if necessary. * Allow the use of multiple seals for entropy augmentation Add seal_name variable in entropy stanza Add new MultiSourcer to accommodate the new entropy augmentation behavior. * Individually health check each wrapper, and add a sys/seal-backend-status endpoint. * Address a race, and also a failed test mock that I didn't catch * Track partial wrapping failures... ... where one or more but not all access.Encrypts fail for a given write. Note these failures by adding a time ordered UUID storage entry containing the path in a special subdirectory of root storage. Adds a callback pattern to accomplish this, with certain high value writes like initial barrier key storage not allowing a partial failure. The followup work would be to detect return to health and iterate through these storage entries, rewrapping. * Add new data structure to track seal config generation (#4492) * Add new data structure to track seal config generation * Remove import cycle * Fix undefined variable errors * update comment * Update setSeal response * Fix setSealResponse in operator_diagnose * Scope the wrapper health check locks individually (#4491) * Refactor setSeal function in server.go. (#4505) Refactor setSeal function in server.go. * Decouple CreateSecureRandomReaderFunc from seal package. Instead of using a list of seal.SealInfo structs, make CreateSecureRandomReaderFunc use a list of new EntropySourcerInfo structs. This brakes the denpency of package configutil on the seal package. * Move SealGenerationInfo tracking to the seal Access. * Move SealGenerationInfo tracking to the seal Access. The SealGenerationInfo is now kept track by a Seal's Access instead of by the Config object. The access implementation now records the correct generation number on seal wrapped values. * Only store and read SealGenerationInfo if VAULT_ENABLE_SEAL_HA_BETA is true. * Add MultiWrapValue protobuf message MultiWrapValue can be used to keep track of different encryptions of a value. --------- Co-authored-by: Victor Rodriguez <vrizo@hashicorp.com> * Use generation to determine if a seal wrapped value is up-to-date. (#4542) * Add logging to seal Access implementation. * Seal HA buf format run (#4561) * Run buf format. * Add buf.lock to ensure go-kms-wrapping module is imported. * Vault-18958: Add unit tests for config checks * Add safety logic for seal configuration changes * Revert "Add safety logic for seal configuration changes" This reverts commit 7fec48035a5cf274e5a4d98901716d08d766ce90. * changes and tests for checking seal config * add ent tests * remove check for empty name and add type into test cases * add error message for empty name * fix no seals test --------- Co-authored-by: divyapola5 <divya@hashicorp.com> * Handle migrations between single-wrapper and multi-wrapper autoSeals * Extract method SetPhysicalSealConfig. * Extract function physicalSealConfig. The extracted function is the only code now reading SealConfig entries from storage. * Extract function setPhysicalSealConfig. The extracted function is the only code now writing SealConfig entries from storage (except for migration from the old recovery config path). * Move SealConfig to new file vault/seal_config.go. * Add SealConfigType quasy-enumeration. SealConfigType is to serve as the typed values for field SealConfig.Type. * Rename Seal.RecoveryType to RecoverySealConfigType. Make RecoverySealConfigType return a SealConfigType instead of a string. * Rename Seal.BarrierType to BarrierSealConfigType. Make BarrierSealConfigType return a SealConfigType. Remove seal.SealType (really a two-step rename to SealConfigType). * Add Seal methods ClearBarrierConfig and ClearRecoveryConfig. * Handle autoseal <-> multiseal migrations. While going between single-wrapper and multiple-wrapper autoseals are not migrations that require an unwrap seal (such as going from shamir to autoseal), the stored "barrier" SealConfig needs to be updated in these cases. Specifically, the value of SealConfg.Type is "multiseal" for autoSeals that have more than one wrapper; on the other hand, for autoseals with a single wrapper, SealConfig.Type is the type of the wrapper. * Remove error return value from NewAutoSeal constructor. * Automatically rewrap partially seal wrapped values on an interval * Add in rewrapping of partially wrapped values on an interval, regardless of seal health/status. * Don't set SealGenerationInfo Rewrapped flag in the partial rewrap call. * Unexport the SealGenerationInfo's Rewrapped field, add a mutex to it for thread safe access, and add accessor methods for it. * Add a success callback to the manual seal rewrap process that updates the SealGenerationInfo's rewrapped field. This is done via a callback to avoid an import cycle in the SealRewrap code. * Fix a failing seal wrap backend test which was broken by the unexporting of SealGenerationInfo's Rewrapped field. * Nil check the seal rewrap success callback before calling it. * Change SealGenerationInfo rewrapped parameter to an atomic.Bool rather than a sync.RWMutex for simplicity and performance. * Add nil check for SealAccess before updating SealGenerationInfo rewrapped status during seal rewrap call. * Update partial rewrap check interval from 10 seconds to 1 minute. * Update a reference to SealGenerationInfo Rewrapped field to use new getter method. * Fix up some data raciness in partial rewrapping. * Account for possibly nil storage entry when retrieving partially wrapped value. * Allow multi-wrapper autoSeals to include disabled seal wrappers. * Restore propagation of wrapper configuration errors by setSeal. Function setSeal is meant to propagate non KeyNotFound errors returned by calls to configutil.ConfigureWrapper. * Remove unused Access methods SetConfig and Type. * Allow multi-wrapper autoSeals to include disabled seal wrappers. Make it possible for an autoSeal that uses multiple wrappers to include disabled wrappers that can be used to decrypt entries, but are skipped for encryption. e an unwrapSeal when there are disabled seals. * Fix bug with not providing name (#4580) * add suffix to name defaults * add comment * only change name for disabled seal * Only attempt to rewrap partial values when all seals are healthy. * Only attempt to rewrap partial values when all seals are healthy. * Change logging level from info to debug for notice about rewrap skipping based on seal health. * Remove stale TODOs and commented out code. --------- Co-authored-by: rculpepper <rculpepper@hashicorp.com> Co-authored-by: Larroyo <95649169+DeLuci@users.noreply.github.com> Co-authored-by: Scott G. Miller <smiller@hashicorp.com> Co-authored-by: Divya Pola <87338962+divyapola5@users.noreply.github.com> Co-authored-by: Matt Schultz <matt.schultz@hashicorp.com> Co-authored-by: divyapola5 <divya@hashicorp.com> Co-authored-by: Rachel Culpepper <84159930+rculpepper@users.noreply.github.com>
335 lines
9.3 KiB
Go
335 lines
9.3 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package command
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
log "github.com/hashicorp/go-hclog"
|
|
kv "github.com/hashicorp/vault-plugin-secrets-kv"
|
|
"github.com/hashicorp/vault/api"
|
|
"github.com/hashicorp/vault/audit"
|
|
"github.com/hashicorp/vault/builtin/logical/pki"
|
|
"github.com/hashicorp/vault/builtin/logical/ssh"
|
|
"github.com/hashicorp/vault/builtin/logical/transit"
|
|
"github.com/hashicorp/vault/helper/benchhelpers"
|
|
"github.com/hashicorp/vault/helper/builtinplugins"
|
|
"github.com/hashicorp/vault/sdk/logical"
|
|
"github.com/hashicorp/vault/sdk/physical/inmem"
|
|
"github.com/hashicorp/vault/vault"
|
|
"github.com/hashicorp/vault/vault/seal"
|
|
"github.com/mitchellh/cli"
|
|
|
|
auditFile "github.com/hashicorp/vault/builtin/audit/file"
|
|
credUserpass "github.com/hashicorp/vault/builtin/credential/userpass"
|
|
vaulthttp "github.com/hashicorp/vault/http"
|
|
)
|
|
|
|
var (
|
|
defaultVaultLogger = log.NewNullLogger()
|
|
|
|
defaultVaultCredentialBackends = map[string]logical.Factory{
|
|
"userpass": credUserpass.Factory,
|
|
}
|
|
|
|
defaultVaultAuditBackends = map[string]audit.Factory{
|
|
"file": auditFile.Factory,
|
|
}
|
|
|
|
defaultVaultLogicalBackends = map[string]logical.Factory{
|
|
"generic-leased": vault.LeasedPassthroughBackendFactory,
|
|
"pki": pki.Factory,
|
|
"ssh": ssh.Factory,
|
|
"transit": transit.Factory,
|
|
"kv": kv.Factory,
|
|
}
|
|
)
|
|
|
|
// assertNoTabs asserts the CLI help has no tab characters.
|
|
func assertNoTabs(tb testing.TB, c cli.Command) {
|
|
tb.Helper()
|
|
|
|
if strings.ContainsRune(c.Help(), '\t') {
|
|
tb.Errorf("%#v help output contains tabs", c)
|
|
}
|
|
}
|
|
|
|
// testVaultServer creates a test vault cluster and returns a configured API
|
|
// client and closer function.
|
|
func testVaultServer(tb testing.TB) (*api.Client, func()) {
|
|
tb.Helper()
|
|
|
|
client, _, closer := testVaultServerUnseal(tb)
|
|
return client, closer
|
|
}
|
|
|
|
func testVaultServerWithSecrets(ctx context.Context, tb testing.TB) (*api.Client, func()) {
|
|
tb.Helper()
|
|
|
|
client, _, closer := testVaultServerUnseal(tb)
|
|
|
|
// enable kv-v1 backend
|
|
if err := client.Sys().Mount("kv-v1/", &api.MountInput{
|
|
Type: "kv-v1",
|
|
}); err != nil {
|
|
tb.Fatal(err)
|
|
}
|
|
|
|
// enable kv-v2 backend
|
|
if err := client.Sys().Mount("kv-v2/", &api.MountInput{
|
|
Type: "kv-v2",
|
|
}); err != nil {
|
|
tb.Fatal(err)
|
|
}
|
|
|
|
// populate dummy secrets
|
|
for _, path := range []string{
|
|
"foo",
|
|
"app-1/foo",
|
|
"app-1/bar",
|
|
"app-1/nested/baz",
|
|
} {
|
|
if err := client.KVv1("kv-v1").Put(ctx, path, map[string]interface{}{
|
|
"user": "test",
|
|
"password": "Hashi123",
|
|
}); err != nil {
|
|
tb.Fatal(err)
|
|
}
|
|
|
|
if _, err := client.KVv2("kv-v2").Put(ctx, path, map[string]interface{}{
|
|
"user": "test",
|
|
"password": "Hashi123",
|
|
}); err != nil {
|
|
tb.Fatal(err)
|
|
}
|
|
}
|
|
|
|
return client, closer
|
|
}
|
|
|
|
func testVaultServerWithKVVersion(tb testing.TB, kvVersion string) (*api.Client, func()) {
|
|
tb.Helper()
|
|
|
|
client, _, closer := testVaultServerUnsealWithKVVersionWithSeal(tb, kvVersion, nil)
|
|
return client, closer
|
|
}
|
|
|
|
func testVaultServerAllBackends(tb testing.TB) (*api.Client, func()) {
|
|
tb.Helper()
|
|
|
|
client, _, closer := testVaultServerCoreConfig(tb, &vault.CoreConfig{
|
|
CredentialBackends: credentialBackends,
|
|
AuditBackends: auditBackends,
|
|
LogicalBackends: logicalBackends,
|
|
BuiltinRegistry: builtinplugins.Registry,
|
|
})
|
|
return client, closer
|
|
}
|
|
|
|
// testVaultServerAutoUnseal creates a test vault cluster and sets it up with auto unseal
|
|
// the function returns a client, the recovery keys, and a closer function
|
|
func testVaultServerAutoUnseal(tb testing.TB) (*api.Client, []string, func()) {
|
|
testSeal, _ := seal.NewTestSeal(nil)
|
|
autoSeal := vault.NewAutoSeal(testSeal)
|
|
return testVaultServerUnsealWithKVVersionWithSeal(tb, "1", autoSeal)
|
|
}
|
|
|
|
// testVaultServerUnseal creates a test vault cluster and returns a configured
|
|
// API client, list of unseal keys (as strings), and a closer function.
|
|
func testVaultServerUnseal(tb testing.TB) (*api.Client, []string, func()) {
|
|
return testVaultServerUnsealWithKVVersionWithSeal(tb, "1", nil)
|
|
}
|
|
|
|
func testVaultServerUnsealWithKVVersionWithSeal(tb testing.TB, kvVersion string, seal vault.Seal) (*api.Client, []string, func()) {
|
|
tb.Helper()
|
|
|
|
return testVaultServerCoreConfigWithOpts(tb, &vault.CoreConfig{
|
|
CredentialBackends: defaultVaultCredentialBackends,
|
|
AuditBackends: defaultVaultAuditBackends,
|
|
LogicalBackends: defaultVaultLogicalBackends,
|
|
BuiltinRegistry: builtinplugins.Registry,
|
|
Seal: seal,
|
|
}, &vault.TestClusterOptions{
|
|
HandlerFunc: vaulthttp.Handler,
|
|
NumCores: 1,
|
|
KVVersion: kvVersion,
|
|
})
|
|
}
|
|
|
|
// testVaultServerUnseal creates a test vault cluster and returns a configured
|
|
// API client, list of unseal keys (as strings), and a closer function
|
|
// configured with the given plugin directory.
|
|
func testVaultServerPluginDir(tb testing.TB, pluginDir string) (*api.Client, []string, func()) {
|
|
tb.Helper()
|
|
|
|
return testVaultServerCoreConfig(tb, &vault.CoreConfig{
|
|
CredentialBackends: defaultVaultCredentialBackends,
|
|
AuditBackends: defaultVaultAuditBackends,
|
|
LogicalBackends: defaultVaultLogicalBackends,
|
|
PluginDirectory: pluginDir,
|
|
BuiltinRegistry: builtinplugins.Registry,
|
|
})
|
|
}
|
|
|
|
func testVaultServerCoreConfig(tb testing.TB, coreConfig *vault.CoreConfig) (*api.Client, []string, func()) {
|
|
return testVaultServerCoreConfigWithOpts(tb, coreConfig, &vault.TestClusterOptions{
|
|
HandlerFunc: vaulthttp.Handler,
|
|
NumCores: 1, // Default is 3, but we don't need that many
|
|
})
|
|
}
|
|
|
|
// testVaultServerCoreConfig creates a new vault cluster with the given core
|
|
// configuration. This is a lower-level test helper. If the seal config supports recovery keys, then
|
|
// recovery keys are returned. Otherwise, unseal keys are returned
|
|
func testVaultServerCoreConfigWithOpts(tb testing.TB, coreConfig *vault.CoreConfig, opts *vault.TestClusterOptions) (*api.Client, []string, func()) {
|
|
tb.Helper()
|
|
|
|
cluster := vault.NewTestCluster(benchhelpers.TBtoT(tb), coreConfig, opts)
|
|
cluster.Start()
|
|
|
|
// Make it easy to get access to the active
|
|
core := cluster.Cores[0].Core
|
|
vault.TestWaitActive(benchhelpers.TBtoT(tb), core)
|
|
|
|
// Get the client already setup for us!
|
|
client := cluster.Cores[0].Client
|
|
client.SetToken(cluster.RootToken)
|
|
|
|
var keys [][]byte
|
|
if coreConfig.Seal != nil && coreConfig.Seal.RecoveryKeySupported() {
|
|
keys = cluster.RecoveryKeys
|
|
} else {
|
|
keys = cluster.BarrierKeys
|
|
}
|
|
|
|
return client, encodeKeys(keys), cluster.Cleanup
|
|
}
|
|
|
|
// Convert the unseal keys to base64 encoded, since these are how the user
|
|
// will get them.
|
|
func encodeKeys(rawKeys [][]byte) []string {
|
|
keys := make([]string, len(rawKeys))
|
|
for i := range rawKeys {
|
|
keys[i] = base64.StdEncoding.EncodeToString(rawKeys[i])
|
|
}
|
|
return keys
|
|
}
|
|
|
|
// testVaultServerUninit creates an uninitialized server.
|
|
func testVaultServerUninit(tb testing.TB) (*api.Client, func()) {
|
|
tb.Helper()
|
|
|
|
inm, err := inmem.NewInmem(nil, defaultVaultLogger)
|
|
if err != nil {
|
|
tb.Fatal(err)
|
|
}
|
|
|
|
core, err := vault.NewCore(&vault.CoreConfig{
|
|
DisableMlock: true,
|
|
Physical: inm,
|
|
CredentialBackends: defaultVaultCredentialBackends,
|
|
AuditBackends: defaultVaultAuditBackends,
|
|
LogicalBackends: defaultVaultLogicalBackends,
|
|
BuiltinRegistry: builtinplugins.Registry,
|
|
})
|
|
if err != nil {
|
|
tb.Fatal(err)
|
|
}
|
|
|
|
ln, addr := vaulthttp.TestServer(tb, core)
|
|
|
|
client, err := api.NewClient(&api.Config{
|
|
Address: addr,
|
|
})
|
|
if err != nil {
|
|
tb.Fatal(err)
|
|
}
|
|
|
|
closer := func() {
|
|
core.Shutdown()
|
|
ln.Close()
|
|
}
|
|
|
|
return client, closer
|
|
}
|
|
|
|
// testVaultServerBad creates an http server that returns a 500 on each request
|
|
// to simulate failures.
|
|
func testVaultServerBad(tb testing.TB) (*api.Client, func()) {
|
|
tb.Helper()
|
|
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
tb.Fatal(err)
|
|
}
|
|
|
|
server := &http.Server{
|
|
Addr: "127.0.0.1:0",
|
|
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
http.Error(w, "500 internal server error", http.StatusInternalServerError)
|
|
}),
|
|
ReadTimeout: 1 * time.Second,
|
|
ReadHeaderTimeout: 1 * time.Second,
|
|
WriteTimeout: 1 * time.Second,
|
|
IdleTimeout: 1 * time.Second,
|
|
}
|
|
|
|
go func() {
|
|
if err := server.Serve(listener); err != nil && err != http.ErrServerClosed {
|
|
tb.Fatal(err)
|
|
}
|
|
}()
|
|
|
|
client, err := api.NewClient(&api.Config{
|
|
Address: "http://" + listener.Addr().String(),
|
|
})
|
|
if err != nil {
|
|
tb.Fatal(err)
|
|
}
|
|
|
|
return client, func() {
|
|
ctx, done := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer done()
|
|
|
|
server.Shutdown(ctx)
|
|
}
|
|
}
|
|
|
|
// testTokenAndAccessor creates a new authentication token capable of being renewed with
|
|
// the default policy attached. It returns the token and it's accessor.
|
|
func testTokenAndAccessor(tb testing.TB, client *api.Client) (string, string) {
|
|
tb.Helper()
|
|
|
|
secret, err := client.Auth().Token().Create(&api.TokenCreateRequest{
|
|
Policies: []string{"default"},
|
|
TTL: "30m",
|
|
})
|
|
if err != nil {
|
|
tb.Fatal(err)
|
|
}
|
|
if secret == nil || secret.Auth == nil || secret.Auth.ClientToken == "" {
|
|
tb.Fatalf("missing auth data: %#v", secret)
|
|
}
|
|
return secret.Auth.ClientToken, secret.Auth.Accessor
|
|
}
|
|
|
|
func testClient(tb testing.TB, addr string, token string) *api.Client {
|
|
tb.Helper()
|
|
config := api.DefaultConfig()
|
|
config.Address = addr
|
|
client, err := api.NewClient(config)
|
|
if err != nil {
|
|
tb.Fatal(err)
|
|
}
|
|
client.SetToken(token)
|
|
|
|
return client
|
|
}
|