2022-01-05 18:02:03 +00:00
|
|
|
//go:build !enterprise
|
2018-02-09 21:37:40 +00:00
|
|
|
|
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
proto "github.com/golang/protobuf/proto"
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2022-08-23 19:37:16 +00:00
|
|
|
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/physical"
|
|
|
|
"github.com/hashicorp/vault/sdk/physical/inmem"
|
2018-02-09 21:37:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSealUnwrapper(t *testing.T) {
|
2018-04-03 00:46:59 +00:00
|
|
|
logger := log.New(&log.LoggerOptions{
|
2018-02-09 21:37:40 +00:00
|
|
|
Mutex: &sync.Mutex{},
|
2018-04-03 00:46:59 +00:00
|
|
|
})
|
2018-02-09 21:37:40 +00:00
|
|
|
|
|
|
|
// Test without transactions
|
2018-04-03 00:46:59 +00:00
|
|
|
phys, err := inmem.NewInmemHA(nil, logger)
|
2018-02-09 21:37:40 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
performTestSealUnwrapper(t, phys, logger)
|
|
|
|
|
|
|
|
// Test with transactions
|
2018-04-03 00:46:59 +00:00
|
|
|
tPhys, err := inmem.NewTransactionalInmemHA(nil, logger)
|
2018-02-09 21:37:40 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
performTestSealUnwrapper(t, tPhys, logger)
|
|
|
|
}
|
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
func performTestSealUnwrapper(t *testing.T, phys physical.Backend, logger log.Logger) {
|
2018-02-09 21:37:40 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
base := &CoreConfig{
|
|
|
|
Physical: phys,
|
|
|
|
}
|
|
|
|
cluster := NewTestCluster(t, base, &TestClusterOptions{
|
2018-04-03 00:46:59 +00:00
|
|
|
Logger: logger,
|
2018-02-09 21:37:40 +00:00
|
|
|
})
|
|
|
|
cluster.Start()
|
|
|
|
defer cluster.Cleanup()
|
|
|
|
|
|
|
|
// Read a value and then save it back in a proto message
|
|
|
|
entry, err := phys.Get(ctx, "core/master")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(entry.Value) == 0 {
|
|
|
|
t.Fatal("got no value for master")
|
|
|
|
}
|
|
|
|
// Save the original for comparison later
|
|
|
|
origBytes := make([]byte, len(entry.Value))
|
|
|
|
copy(origBytes, entry.Value)
|
2022-08-23 19:37:16 +00:00
|
|
|
se := &wrapping.BlobInfo{
|
2018-02-09 21:37:40 +00:00
|
|
|
Ciphertext: entry.Value,
|
|
|
|
}
|
|
|
|
seb, err := proto.Marshal(se)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Write the canary
|
|
|
|
entry.Value = append(seb, 's')
|
|
|
|
// Save the protobuf value for comparison later
|
|
|
|
pBytes := make([]byte, len(entry.Value))
|
|
|
|
copy(pBytes, entry.Value)
|
|
|
|
if err = phys.Put(ctx, entry); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point we should be able to read through the standby cores,
|
|
|
|
// successfully decode it, but be able to unmarshal it when read back from
|
|
|
|
// the underlying physical store. When we read from active, it should both
|
|
|
|
// successfully decode it and persist it back.
|
|
|
|
checkValue := func(core *Core, wrapped bool) {
|
|
|
|
entry, err := core.physical.Get(ctx, "core/master")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-11-01 18:43:00 +00:00
|
|
|
if entry == nil {
|
|
|
|
t.Fatal("nil entry")
|
|
|
|
}
|
2018-02-09 21:37:40 +00:00
|
|
|
if !bytes.Equal(entry.Value, origBytes) {
|
2018-09-18 03:03:00 +00:00
|
|
|
t.Fatalf("mismatched original bytes and unwrapped entry bytes:\ngot:\n%v\nexpected:\n%v", entry.Value, origBytes)
|
2018-02-09 21:37:40 +00:00
|
|
|
}
|
|
|
|
underlyingEntry, err := phys.Get(ctx, "core/master")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
switch wrapped {
|
|
|
|
case true:
|
|
|
|
if !bytes.Equal(underlyingEntry.Value, pBytes) {
|
2018-09-18 03:03:00 +00:00
|
|
|
t.Fatalf("mismatched original bytes and proto entry bytes:\ngot:\n%v\nexpected:\n%v", underlyingEntry.Value, pBytes)
|
2018-02-09 21:37:40 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
if !bytes.Equal(underlyingEntry.Value, origBytes) {
|
2018-09-18 03:03:00 +00:00
|
|
|
t.Fatalf("mismatched original bytes and unwrapped entry bytes:\ngot:\n%v\nexpected:\n%v", underlyingEntry.Value, origBytes)
|
2018-02-09 21:37:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TestWaitActive(t, cluster.Cores[0].Core)
|
|
|
|
checkValue(cluster.Cores[2].Core, true)
|
|
|
|
checkValue(cluster.Cores[1].Core, true)
|
|
|
|
checkValue(cluster.Cores[0].Core, false)
|
|
|
|
}
|