2022-05-20 11:55:54 -07:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-02-02 17:15:17 -08:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
2022-06-20 18:20:17 -07:00
|
|
|
package server
|
2022-05-20 11:55:54 -07:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2022-09-02 14:32:41 -04:00
|
|
|
"crypto/rand"
|
2022-05-20 11:55:54 -07:00
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2023-03-10 15:06:39 -05:00
|
|
|
"github.com/hashicorp/boundary/globals"
|
2022-05-20 11:55:54 -07:00
|
|
|
"github.com/hashicorp/boundary/internal/db"
|
2022-09-02 14:32:41 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/kms"
|
2022-05-20 11:55:54 -07:00
|
|
|
"github.com/hashicorp/boundary/internal/types/scope"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
2022-06-15 08:27:08 -07:00
|
|
|
func TestTestKmsWorker(t *testing.T) {
|
2022-05-20 11:55:54 -07:00
|
|
|
conn, _ := db.TestSetup(t, "postgres")
|
|
|
|
|
wrapper := db.TestWrapper(t)
|
2022-06-03 11:19:04 -07:00
|
|
|
const (
|
|
|
|
|
name = "test name"
|
|
|
|
|
description = "test description"
|
|
|
|
|
address = "test address"
|
|
|
|
|
)
|
2023-02-01 11:24:56 -07:00
|
|
|
tWorker := TestKmsWorker(t, conn, wrapper, WithName(name), WithDescription(description), WithAddress(address), WithOperationalState(ShutdownOperationalState.String()))
|
2022-05-20 11:55:54 -07:00
|
|
|
assert.NotNil(t, tWorker)
|
2023-03-10 15:06:39 -05:00
|
|
|
assert.True(t, strings.HasPrefix(tWorker.GetPublicId(), globals.WorkerPrefix))
|
2022-05-20 11:55:54 -07:00
|
|
|
|
2022-06-01 13:25:29 -07:00
|
|
|
lkpWorker := NewWorker(scope.Global.String())
|
|
|
|
|
lkpWorker.PublicId = tWorker.GetPublicId()
|
2022-05-20 11:55:54 -07:00
|
|
|
rw := db.New(conn)
|
|
|
|
|
require.NoError(t, rw.LookupById(context.Background(), lkpWorker))
|
|
|
|
|
assert.NotNil(t, lkpWorker)
|
2022-06-03 11:19:04 -07:00
|
|
|
assert.NotNil(t, lkpWorker.GetLastStatusTime())
|
2022-06-15 08:27:08 -07:00
|
|
|
assert.Equal(t, KmsWorkerType.String(), lkpWorker.GetType())
|
2022-06-03 11:19:04 -07:00
|
|
|
assert.Equal(t, name, lkpWorker.GetName())
|
|
|
|
|
assert.Equal(t, description, lkpWorker.GetDescription())
|
|
|
|
|
assert.Equal(t, address, lkpWorker.GetAddress())
|
2023-02-01 11:24:56 -07:00
|
|
|
assert.Equal(t, ShutdownOperationalState.String(), lkpWorker.OperationalState)
|
2022-05-20 11:55:54 -07:00
|
|
|
}
|
2022-06-15 08:27:08 -07:00
|
|
|
|
|
|
|
|
func TestTestPkiWorker(t *testing.T) {
|
|
|
|
|
conn, _ := db.TestSetup(t, "postgres")
|
|
|
|
|
wrapper := db.TestWrapper(t)
|
2022-09-02 14:32:41 -04:00
|
|
|
kmsCache := kms.TestKms(t, conn, wrapper)
|
|
|
|
|
require.NoError(t, kmsCache.CreateKeys(context.Background(), scope.Global.String(), kms.WithRandomReader(rand.Reader)))
|
2022-06-15 08:27:08 -07:00
|
|
|
const (
|
|
|
|
|
name = "test name"
|
|
|
|
|
description = "test description"
|
|
|
|
|
)
|
|
|
|
|
tWorker := TestPkiWorker(t, conn, wrapper, WithName(name), WithDescription(description))
|
|
|
|
|
assert.NotNil(t, tWorker)
|
2023-03-10 15:06:39 -05:00
|
|
|
assert.True(t, strings.HasPrefix(tWorker.GetPublicId(), globals.WorkerPrefix))
|
2022-06-15 08:27:08 -07:00
|
|
|
|
|
|
|
|
lkpWorker := NewWorker(scope.Global.String())
|
|
|
|
|
lkpWorker.PublicId = tWorker.GetPublicId()
|
|
|
|
|
rw := db.New(conn)
|
|
|
|
|
require.NoError(t, rw.LookupById(context.Background(), lkpWorker))
|
|
|
|
|
assert.NotNil(t, lkpWorker)
|
|
|
|
|
assert.Equal(t, PkiWorkerType.String(), lkpWorker.GetType())
|
|
|
|
|
assert.Equal(t, name, lkpWorker.GetName())
|
|
|
|
|
assert.Equal(t, description, lkpWorker.GetDescription())
|
|
|
|
|
assert.Nil(t, lkpWorker.GetLastStatusTime())
|
2022-06-15 12:42:50 -07:00
|
|
|
|
|
|
|
|
var keyId string
|
|
|
|
|
authorizedWorker := TestPkiWorker(t, conn, wrapper, WithTestPkiWorkerAuthorizedKeyId(&keyId))
|
|
|
|
|
assert.NotNil(t, authorizedWorker)
|
2023-03-10 15:06:39 -05:00
|
|
|
assert.True(t, strings.HasPrefix(authorizedWorker.GetPublicId(), globals.WorkerPrefix))
|
2022-06-15 12:42:50 -07:00
|
|
|
assert.NotEmpty(t, keyId)
|
2022-06-15 08:27:08 -07:00
|
|
|
}
|