2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2021-10-13 19:24:31 +00:00
|
|
|
package kv
|
2019-04-16 16:53:24 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"strings"
|
2019-06-04 22:48:31 +00:00
|
|
|
"sync"
|
2019-04-16 16:53:24 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
logicalKv "github.com/hashicorp/vault-plugin-secrets-kv"
|
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
"github.com/hashicorp/vault/helper/testhelpers"
|
|
|
|
vaulthttp "github.com/hashicorp/vault/http"
|
2019-04-25 16:08:03 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
|
|
|
"github.com/hashicorp/vault/sdk/physical"
|
2019-04-16 16:53:24 +00:00
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
"github.com/kr/pretty"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Tests the regression in
|
|
|
|
// https://github.com/hashicorp/vault-plugin-secrets-kv/pull/31
|
|
|
|
func TestKVv2_UpgradePaths(t *testing.T) {
|
2019-06-04 22:48:31 +00:00
|
|
|
m := new(sync.Mutex)
|
2019-04-16 16:53:24 +00:00
|
|
|
logOut := new(bytes.Buffer)
|
2020-05-21 20:07:50 +00:00
|
|
|
|
2019-04-16 16:53:24 +00:00
|
|
|
logger := hclog.New(&hclog.LoggerOptions{
|
|
|
|
Output: logOut,
|
2019-06-04 22:48:31 +00:00
|
|
|
Mutex: m,
|
2019-04-16 16:53:24 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
coreConfig := &vault.CoreConfig{
|
|
|
|
LogicalBackends: map[string]logical.Factory{
|
|
|
|
"kv": logicalKv.Factory,
|
|
|
|
},
|
|
|
|
EnableRaw: true,
|
|
|
|
Logger: logger,
|
|
|
|
}
|
|
|
|
cluster := vault.NewTestCluster(t, coreConfig, &vault.TestClusterOptions{
|
|
|
|
HandlerFunc: vaulthttp.Handler,
|
|
|
|
})
|
|
|
|
cluster.Start()
|
|
|
|
defer cluster.Cleanup()
|
|
|
|
|
|
|
|
core := cluster.Cores[0]
|
|
|
|
vault.TestWaitActive(t, core.Core)
|
|
|
|
client := core.Client
|
|
|
|
|
|
|
|
// Enable KVv2
|
2022-04-07 19:12:58 +00:00
|
|
|
err := client.Sys().Mount("kv", &api.MountInput{
|
2019-04-16 16:53:24 +00:00
|
|
|
Type: "kv-v2",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cluster.EnsureCoresSealed(t)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// Delete the policy from storage, to trigger the clean slate necessary for
|
|
|
|
// the error
|
|
|
|
mounts, err := core.UnderlyingStorage.List(ctx, "logical/")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
kvMount := mounts[0]
|
|
|
|
basePaths, err := core.UnderlyingStorage.List(ctx, "logical/"+kvMount)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
basePath := basePaths[0]
|
|
|
|
|
|
|
|
beforeList, err := core.UnderlyingStorage.List(ctx, "logical/"+kvMount+basePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Log(pretty.Sprint(beforeList))
|
|
|
|
|
|
|
|
// Delete policy/archive
|
|
|
|
if err = logical.ClearView(ctx, physical.NewView(core.UnderlyingStorage, "logical/"+kvMount+basePath+"policy/")); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err = logical.ClearView(ctx, physical.NewView(core.UnderlyingStorage, "logical/"+kvMount+basePath+"archive/")); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
afterList, err := core.UnderlyingStorage.List(ctx, "logical/"+kvMount+basePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Log(pretty.Sprint(afterList))
|
|
|
|
|
|
|
|
testhelpers.EnsureCoresUnsealed(t, cluster)
|
|
|
|
|
|
|
|
// Need to give it time to actually set up
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
|
2019-06-04 22:48:31 +00:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
2019-04-16 16:53:24 +00:00
|
|
|
if strings.Contains(logOut.String(), "cannot write to storage during setup") {
|
|
|
|
t.Fatal("got a cannot write to storage during setup error")
|
|
|
|
}
|
|
|
|
}
|