Files
threshold/internal/servers/testing.go
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

143 lines
4.2 KiB
Go
Raw Normal View History

2022-05-10 10:03:08 -04:00
package servers
import (
"context"
"math/rand"
"strings"
2022-05-10 10:03:08 -04:00
"testing"
"time"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/db/timestamp"
"github.com/hashicorp/boundary/internal/iam"
"github.com/hashicorp/boundary/internal/kms"
"github.com/hashicorp/boundary/internal/servers/store"
"github.com/hashicorp/boundary/internal/types/scope"
2022-05-10 10:03:08 -04:00
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/timestamppb"
)
const defaultLength = 20
// Generate random bytes for byte fields
func populateBytes(length int) []byte {
fieldBytes := make([]byte, length)
rand.Read(fieldBytes)
return fieldBytes
}
func TestKmsKey(ctx context.Context, t *testing.T, conn *db.DB, wrapper wrapping.Wrapper) string {
2022-05-10 10:03:08 -04:00
t.Helper()
org, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
kmsCache := kms.TestKms(t, conn, wrapper)
databaseWrapper, err := kmsCache.GetWrapper(context.Background(), org.PublicId, kms.KeyPurposeDatabase)
require.NoError(t, err)
testKey, err := databaseWrapper.KeyId(ctx)
require.NoError(t, err)
return testKey
}
2022-05-20 11:55:54 -07:00
func TestRootCertificate(ctx context.Context, t *testing.T, conn *db.DB, kmsKey string) *RootCertificate {
2022-05-10 10:03:08 -04:00
t.Helper()
rw := db.New(conn)
beforeTimestamp := &timestamp.Timestamp{Timestamp: timestamppb.New(time.Now().Add(-1 * time.Hour))}
afterTimestamp := &timestamp.Timestamp{Timestamp: timestamppb.New(time.Now().Add(1 * time.Hour))}
rootCertKeys := RootCertificateKeys{
publicKey: populateBytes(defaultLength),
privateKey: populateBytes(defaultLength),
}
cert, err := newRootCertificate(ctx, rand.Uint64(), populateBytes(defaultLength), beforeTimestamp, afterTimestamp,
rootCertKeys, kmsKey, CurrentState)
err = rw.Create(ctx, cert)
require.NoError(t, err)
2022-05-20 11:55:54 -07:00
return cert
2022-05-10 10:03:08 -04:00
}
2022-05-20 11:55:54 -07:00
func TestWorkerAuth(ctx context.Context, t *testing.T, conn *db.DB, worker *Worker, kmsKey string) *WorkerAuth {
2022-05-10 10:03:08 -04:00
t.Helper()
rw := db.New(conn)
wSignPubKey := populateBytes(defaultLength)
wEncPubKey := populateBytes(defaultLength)
workerKeys := WorkerKeys{workerSigningPubKey: wSignPubKey, workerEncryptionPubKey: wEncPubKey}
controllerKey := populateBytes(defaultLength)
nonce := populateBytes(defaultLength)
opt := []Option{
WithKeyId(kmsKey),
WithWorkerKeys(workerKeys),
WithControllerEncryptionPrivateKey(controllerKey),
WithNonce(nonce),
}
workerAuth, err := newWorkerAuth(ctx, "worker-key-identifier", worker.PublicId, opt...)
require.NoError(t, err)
err = rw.Create(ctx, workerAuth)
require.NoError(t, err)
2022-05-20 11:55:54 -07:00
return workerAuth
2022-05-10 10:03:08 -04:00
}
2022-05-24 15:08:02 -07:00
// TestWorker inserts a worker into the db to satisfy foreign key constraints.
2022-06-03 11:19:04 -07:00
// The worker provided fields are auto generated. WithName, WithDescription,
// and WithAddress are applied to the resource name, description and address if
// present.
func TestWorker(t *testing.T, conn *db.DB, wrapper wrapping.Wrapper, opt ...Option) *Worker {
2022-05-10 10:03:08 -04:00
t.Helper()
rw := db.New(conn)
kms := kms.TestKms(t, conn, wrapper)
serversRepo, err := NewRepository(rw, rw, kms)
require.NoError(t, err)
2022-06-03 11:19:04 -07:00
ctx := context.Background()
2022-05-10 10:03:08 -04:00
2022-06-03 11:19:04 -07:00
namePart, err := newWorkerId(ctx)
2022-05-10 10:03:08 -04:00
require.NoError(t, err)
2022-06-03 11:19:04 -07:00
name := "test-worker-" + strings.ToLower(namePart)
2022-06-06 14:25:14 -04:00
id, err := newWorkerId(ctx)
require.NoError(t, err)
wrk := NewWorkerForStatus(scope.Global.String(),
2022-05-20 11:55:54 -07:00
WithName(name),
WithAddress("127.0.0.1"))
2022-06-06 14:25:14 -04:00
wrk, err = serversRepo.UpsertWorkerStatus(ctx, wrk, WithPublicId(id))
2022-05-10 10:03:08 -04:00
require.NoError(t, err)
require.NotNil(t, wrk)
2022-06-03 11:19:04 -07:00
opts := getOpts(opt...)
if len(opts.withWorkerTags) > 0 {
var tags []interface{}
for _, t := range opts.withWorkerTags {
tags = append(tags, &store.WorkerTag{
WorkerId: wrk.GetPublicId(),
Key: t.Key,
Value: t.Value,
Source: "api",
})
}
require.NoError(t, rw.CreateItems(ctx, tags))
}
2022-06-03 11:19:04 -07:00
var mask []string
if opts.withName != "" {
wrk.Name = opts.withName
mask = append(mask, "name")
}
if opts.withDescription != "" {
wrk.Description = opts.withDescription
mask = append(mask, "description")
}
if opts.withAddress != "" {
wrk.Address = opts.withAddress
mask = append(mask, "address")
}
if len(mask) > 0 {
var n int
wrk, n, err = serversRepo.UpdateWorker(ctx, wrk, wrk.Version, mask)
require.NoError(t, err)
require.Equal(t, 1, n)
require.NotNil(t, wrk)
}
2022-05-24 15:08:02 -07:00
return wrk
2022-05-10 10:03:08 -04:00
}