Files
threshold/internal/kms/kms_test.go
T
Jim 785241237e refactor (kms): adopt the go-kms-wrapping/extras/kms package (#2027)
* feature (db): add Db.UnderlyingDB()

This is needed to allow for integration with bits that need a *dbw.DB

* feature (kms): add new go-kms-wrapping/extras/kms dependency

* refactor (kms): schema changes required to adopt extras/kms pkg

* refactor (kms/iam): rename to DeprecateCreateKeysTx(...)

* refactor (kms): remove deprecated test

* refactor (kms): refactor Kms type

This refactor includes renaming the existing type to DeprecatedKms
and then refactoring the Kms type to become a wrapper of
go-kms-wrapping/extras/kms

* refactor (kms): delete all the unneeded bits

All this stuff is not needed now that we're using
go-kms-wrapping/extras/kms

* refactor (db): fixup tests after kms refactor

* refactor (kms/tests): use testing.TB for test helpers

Our test helper packages were not possible to use
with benchmarks. This refactor switches to testing.TB
to allow both tests and benchmarks to use the helpers.

* tests (session benchmarks): refactor to use new Kms type

* refactor (schema): rm test which relies on deprecated Kms code

* refactor (kms): post rebase on main changes required

* chore (kms refactor): re-add unit test from main

* refactor (kms): uses migrations that don't include KMS schema changes
2022-05-13 13:53:22 -04:00

177 lines
4.4 KiB
Go

package kms
import (
"context"
"testing"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/errors"
"github.com/hashicorp/go-dbw"
wrappingKms "github.com/hashicorp/go-kms-wrapping/extras/kms/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_New(t *testing.T) {
t.Parallel()
testCtx := context.Background()
conn, _ := db.TestSetup(t, "postgres")
rw := db.New(conn)
tests := []struct {
name string
r *db.Db
w *db.Db
want *Kms
wantErr bool
wantErrMatch *errors.Template
wantErrContains string
}{
{
name: "nil-reader",
w: rw,
wantErr: true,
wantErrContains: "missing reader",
wantErrMatch: errors.T(errors.InvalidParameter),
},
{
name: "nil-writer",
r: rw,
wantErr: true,
wantErrContains: "missing writer",
wantErrMatch: errors.T(errors.InvalidParameter),
},
{
name: "success",
r: rw,
w: rw,
want: &Kms{
reader: rw,
underlying: func() *wrappingKms.Kms {
purposes := make([]wrappingKms.KeyPurpose, 0, len(ValidDekPurposes()))
for _, p := range ValidDekPurposes() {
purposes = append(purposes, wrappingKms.KeyPurpose(p.String()))
}
purposes = append(purposes, wrappingKms.KeyPurpose(KeyPurposeWorkerAuth.String()), wrappingKms.KeyPurpose(KeyPurposeRecovery.String()))
r := dbw.New(rw.UnderlyingDB())
w := dbw.New(rw.UnderlyingDB())
wrapped, err := wrappingKms.New(r, w, purposes, wrappingKms.WithCache(true))
require.NoError(t, err)
return wrapped
}(),
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
got, err := New(testCtx, tc.r, tc.w)
if tc.wantErr {
require.Error(err)
if tc.wantErrMatch != nil {
assert.Truef(errors.Match(tc.wantErrMatch, err), "expected %q and got err: %+v", tc.wantErrMatch.Code, err)
}
if tc.wantErrContains != "" {
assert.Contains(err.Error(), tc.wantErrContains)
}
return
}
require.NoError(err)
assert.Equal(tc.want, got)
})
}
}
func Test_NewUsingReaderWriter(t *testing.T) {
t.Parallel()
testCtx := context.Background()
conn, _ := db.TestSetup(t, "postgres")
rw := db.New(conn)
tests := []struct {
name string
r db.Reader
w db.Writer
want *Kms
wantErr bool
wantErrMatch *errors.Template
wantErrContains string
}{
{
name: "nil-reader",
w: rw,
wantErr: true,
wantErrContains: "missing reader",
wantErrMatch: errors.T(errors.InvalidParameter),
},
{
name: "nil-writer",
r: rw,
wantErr: true,
wantErrContains: "missing writer",
wantErrMatch: errors.T(errors.InvalidParameter),
},
{
name: "invalid-reader",
r: &invalidReader{},
w: rw,
wantErr: true,
wantErrContains: "unable to convert to db.DB",
wantErrMatch: errors.T(errors.InvalidParameter),
},
{
name: "invalid-writer",
r: rw,
w: &invalidWriter{},
wantErr: true,
wantErrContains: "unable to convert to db.DB",
wantErrMatch: errors.T(errors.InvalidParameter),
},
{
name: "success",
r: rw,
w: rw,
want: &Kms{
reader: rw,
underlying: func() *wrappingKms.Kms {
purposes := stdNewKmsPurposes()
r := dbw.New(rw.UnderlyingDB())
w := dbw.New(rw.UnderlyingDB())
wrapped, err := wrappingKms.New(r, w, purposes, wrappingKms.WithCache(true))
require.NoError(t, err)
return wrapped
}(),
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
got, err := NewUsingReaderWriter(testCtx, tc.r, tc.w)
if tc.wantErr {
require.Error(err)
if tc.wantErrMatch != nil {
assert.Truef(errors.Match(tc.wantErrMatch, err), "expected %q and got err: %+v", tc.wantErrMatch.Code, err)
}
if tc.wantErrContains != "" {
assert.Contains(err.Error(), tc.wantErrContains)
}
return
}
require.NoError(err)
assert.Equal(tc.want, got)
})
}
}
type invalidReader struct {
db.Reader
}
type invalidWriter struct {
db.Writer
}