2020-08-19 16:47:18 -04:00
|
|
|
package kms
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2022-05-13 13:53:22 -04:00
|
|
|
"fmt"
|
2020-08-19 16:47:18 -04:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/hashicorp/boundary/internal/db"
|
2022-03-16 17:04:07 -04:00
|
|
|
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
|
2020-08-19 16:47:18 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
2022-05-13 13:53:22 -04:00
|
|
|
// TestKms creates a kms for testing.
|
2022-04-25 15:52:44 -04:00
|
|
|
func TestKms(t testing.TB, conn *db.DB, rootWrapper wrapping.Wrapper) *Kms {
|
2020-08-19 16:47:18 -04:00
|
|
|
t.Helper()
|
|
|
|
|
require := require.New(t)
|
|
|
|
|
rw := db.New(conn)
|
2022-05-13 13:53:22 -04:00
|
|
|
kms, err := New(context.Background(), rw, rw)
|
2020-08-19 16:47:18 -04:00
|
|
|
require.NoError(err)
|
2022-03-16 17:04:07 -04:00
|
|
|
err = kms.AddExternalWrappers(context.Background(), WithRootWrapper(rootWrapper))
|
2020-08-19 16:47:18 -04:00
|
|
|
require.NoError(err)
|
2022-05-13 13:53:22 -04:00
|
|
|
_, err = kms.underlying.GetExternalRootWrapper()
|
2020-09-02 14:37:19 -04:00
|
|
|
require.NoError(err)
|
2022-05-13 13:53:22 -04:00
|
|
|
return kms
|
2020-09-02 14:37:19 -04:00
|
|
|
}
|
|
|
|
|
|
2022-05-13 13:53:22 -04:00
|
|
|
// TestKmsDeleteKeyPurpose allows you to delete a KeyPurpose for testing.
|
|
|
|
|
func TestKmsDeleteKeyPurpose(t testing.TB, conn *db.DB, purpose KeyPurpose) {
|
|
|
|
|
db.TestDeleteWhere(t, conn, func() interface{} { i := dataKey{}; return &i }(), fmt.Sprintf("purpose='%s'", purpose.String()))
|
2020-09-02 14:37:19 -04:00
|
|
|
}
|
|
|
|
|
|
2022-05-13 13:53:22 -04:00
|
|
|
// TestKmsDeleteAllKeys allows you to delete all the keys for testing.
|
|
|
|
|
func TestKmsDeleteAllKeys(t testing.TB, conn *db.DB) {
|
|
|
|
|
db.TestDeleteWhere(t, conn, func() interface{} { i := rootKey{}; return &i }(), "1=1")
|
2020-09-02 14:37:19 -04:00
|
|
|
}
|
|
|
|
|
|
2022-05-13 13:53:22 -04:00
|
|
|
type dataKey struct{}
|
2021-01-26 14:37:10 -05:00
|
|
|
|
2022-05-13 13:53:22 -04:00
|
|
|
func (*dataKey) TableName() string { return "kms_data_key" }
|