Merge BYOW GA branch to main (#2398)

* feat(workers): Add ability to read and reinitialize cert authority (#2312)

* feat(workers): Return Release Version on worker list and read (#2377)

* feat(cli): Read and reinitialize worker certificate authority (#2387)
This commit is contained in:
Irena Rindos authored and GitHub committed 2022-09-08 13:19:21 -04:00
1 parent cee5ff7599
commit 18dff62b7b
50 files changed
+2401 -462

No files matched your search

+1 -1
View File
@@ -352,7 +352,7 @@ func New(ctx context.Context, conf *Config) (*Controller, error) {
if err != nil {
return nil, fmt.Errorf("unable to instantiate worker auth repository: %w", err)
}
err = server.RotateRoots(ctx, serversRepo)
_, err = server.RotateRoots(ctx, serversRepo)
if err != nil {
return nil, fmt.Errorf("unable to ensure worker auth roots exist: %w", err)
}
+1 -1
View File
@@ -226,7 +226,7 @@ func (c *Controller) registerGrpcServices(s *grpc.Server) error {
services.RegisterCredentialLibraryServiceServer(s, cl)
}
if _, ok := currentServices[services.WorkerService_ServiceDesc.ServiceName]; !ok {
ws, err := workers.NewService(c.baseContext, c.ServersRepoFn, c.IamRepoFn)
ws, err := workers.NewService(c.baseContext, c.ServersRepoFn, c.IamRepoFn, c.WorkerAuthRepoStorageFn)
if err != nil {
return fmt.Errorf("failed to create worker handler service: %w", err)
}
@@ -100,6 +100,8 @@ var globalAuthorizedCollectionActions = map[string]*structpb.ListValue{
structpb.NewStringValue("create:controller-led"),
structpb.NewStringValue("create:worker-led"),
structpb.NewStringValue("list"),
structpb.NewStringValue("read-certificate-authority"),
structpb.NewStringValue("reinitialize-certificate-authority"),
},
},
}
@@ -2,6 +2,8 @@ package workers
import (
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"strings"
@@ -56,6 +58,8 @@ var (
action.CreateControllerLed,
action.CreateWorkerLed,
action.List,
action.ReadCertificateAuthority,
action.ReinitializeCertificateAuthority,
}
)
@@ -70,14 +74,17 @@ func init() {
type Service struct {
pbs.UnsafeWorkerServiceServer
repoFn common.ServersRepoFactory
iamRepoFn common.IamRepoFactory
repoFn common.ServersRepoFactory
workerAuthFn common.WorkerAuthRepoStorageFactory
iamRepoFn common.IamRepoFactory
}
var _ pbs.WorkerServiceServer = (*Service)(nil)
// NewService returns a worker service which handles worker related requests to boundary.
func NewService(ctx context.Context, repo common.ServersRepoFactory, iamRepoFn common.IamRepoFactory) (Service, error) {
func NewService(ctx context.Context, repo common.ServersRepoFactory, iamRepoFn common.IamRepoFactory,
workerAuthFn common.WorkerAuthRepoStorageFactory,
) (Service, error) {
const op = "workers.NewService"
if repo == nil {
return Service{}, errors.New(ctx, errors.InvalidParameter, op, "missing server repository")
@@ -85,7 +92,10 @@ func NewService(ctx context.Context, repo common.ServersRepoFactory, iamRepoFn c
if iamRepoFn == nil {
return Service{}, errors.New(ctx, errors.InvalidParameter, op, "missing iam repository")
}
return Service{repoFn: repo, iamRepoFn: iamRepoFn}, nil
if workerAuthFn == nil {
return Service{}, errors.New(ctx, errors.InvalidParameter, op, "missing worker auth repository")
}
return Service{repoFn: repo, iamRepoFn: iamRepoFn, workerAuthFn: workerAuthFn}, nil
}
// ListWorkers implements the interface pbs.WorkerServiceServer.
@@ -448,6 +458,54 @@ func (s Service) RemoveWorkerTags(ctx context.Context, req *pbs.RemoveWorkerTags
return &pbs.RemoveWorkerTagsResponse{Item: item}, nil
}
// ReadCertificateAuthority will list the next and current certificates for the worker certificate authority
func (s Service) ReadCertificateAuthority(ctx context.Context, req *pbs.ReadCertificateAuthorityRequest) (*pbs.ReadCertificateAuthorityResponse, error) {
const op = "workers.(Service).ReadCertificateAuthority"
if err := validateReadCaRequest(req); err != nil {
return nil, err
}
authResults := s.authResult(ctx, req.GetScopeId(), action.ReadCertificateAuthority)
if authResults.Error != nil {
return nil, authResults.Error
}
caProto, err := s.listCertificateAuthorityFromRepo(ctx)
if err != nil {
return nil, err
}
ca := certificateAuthorityToProto(caProto)
return &pbs.ReadCertificateAuthorityResponse{Item: ca}, nil
}
// ReinitializeCertificateAuthority will delete and regenerate the next and current certificates for the worker certificate authority
func (s Service) ReinitializeCertificateAuthority(ctx context.Context, req *pbs.ReinitializeCertificateAuthorityRequest) (*pbs.ReinitializeCertificateAuthorityResponse, error) {
const op = "workers.(Service).ReinitializeCertificateAuthority"
if err := validateReinitCaRequest(req); err != nil {
return nil, err
}
authResults := s.authResult(ctx, req.GetScopeId(), action.ReinitializeCertificateAuthority)
if authResults.Error != nil {
return nil, authResults.Error
}
repo, err := s.workerAuthFn()
if err != nil {
return nil, err
}
rootCerts, err := server.ReinitializeRoots(ctx, repo)
if err != nil {
return nil, err
}
ca := certificateAuthorityToProto(rootCerts)
return &pbs.ReinitializeCertificateAuthorityResponse{Item: ca}, nil
}
func (s Service) listFromRepo(ctx context.Context, scopeIds []string) ([]*server.Worker, error) {
repo, err := s.repoFn()
if err != nil {
@@ -633,7 +691,7 @@ func (s Service) authResult(ctx context.Context, id string, a action.Type) auth.
var parentId string
opts := []auth.Option{auth.WithType(resource.Worker), auth.WithAction(a)}
switch a {
case action.List, action.CreateWorkerLed, action.CreateControllerLed:
case action.List, action.CreateWorkerLed, action.CreateControllerLed, action.ReadCertificateAuthority, action.ReinitializeCertificateAuthority:
parentId = id
default:
w, err := repo.LookupWorker(ctx, id)
@@ -652,6 +710,47 @@ func (s Service) authResult(ctx context.Context, id string, a action.Type) auth.
return auth.Verify(ctx, opts...)
}
func (s Service) listCertificateAuthorityFromRepo(ctx context.Context) (*types.RootCertificates, error) {
repo, err := s.workerAuthFn()
if err != nil {
return nil, err
}
certs := &types.RootCertificates{Id: server.CaId}
err = repo.Load(ctx, certs)
if err != nil {
return nil, err
}
return certs, nil
}
func certificateAuthorityToProto(in *types.RootCertificates) *pb.CertificateAuthority {
certs := make([]*pb.Certificate, 0)
current := in.GetCurrent()
currentSha := sha256.Sum256(current.PublicKeyPkix)
currentCert := &pb.Certificate{
Id: string(server.CurrentState),
PublicKeySha256: hex.EncodeToString(currentSha[:]),
NotBeforeTime: current.NotBefore,
NotAfterTime: current.NotAfter,
}
certs = append(certs, currentCert)
next := in.GetNext()
nextSha := sha256.Sum256(next.PublicKeyPkix)
nextCert := &pb.Certificate{
Id: string(server.NextState),
PublicKeySha256: hex.EncodeToString(nextSha[:]),
NotBeforeTime: next.NotBefore,
NotAfterTime: next.NotAfter,
}
certs = append(certs, nextCert)
return &pb.CertificateAuthority{Certs: certs}
}
func toProto(ctx context.Context, in *server.Worker, opt ...handlers.Option) (*pb.Worker, error) {
const op = "workers.toProto"
opts := handlers.GetOpts(opt...)
@@ -679,6 +778,9 @@ func toProto(ctx context.Context, in *server.Worker, opt ...handlers.Option) (*p
if outputFields.Has(globals.UpdatedTimeField) {
out.UpdatedTime = in.GetUpdateTime().GetTimestamp()
}
if outputFields.Has(globals.ReleaseVersionField) {
out.ReleaseVersion = in.GetReleaseVersion()
}
if outputFields.Has(globals.VersionField) {
out.Version = in.GetVersion()
}
@@ -990,3 +1092,26 @@ func validateRemoveTagsRequest(req *pbs.RemoveWorkerTagsRequest) error {
}
return nil
}
func validateReadCaRequest(req *pbs.ReadCertificateAuthorityRequest) error {
badFields := map[string]string{}
if req.GetScopeId() != scope.Global.String() {
badFields["scope_id"] = "Must be 'global' when reading."
}
if len(badFields) > 0 {
return handlers.InvalidArgumentErrorf("Error in provided request.", badFields)
}
return nil
}
func validateReinitCaRequest(req *pbs.ReinitializeCertificateAuthorityRequest) error {
badFields := map[string]string{}
if req.GetScopeId() != scope.Global.String() {
badFields["scope_id"] = "Must be 'global' when reinitializing certs."
}
if len(badFields) > 0 {
return handlers.InvalidArgumentErrorf("Error in provided request.", badFields)
}
return nil
}
@@ -73,6 +73,7 @@ func equalTags(t *testing.T, expected map[string]*structpb.ListValue, actual map
}
func TestGet(t *testing.T) {
ctx := context.Background()
conn, _ := db.TestSetup(t, "postgres")
wrap := db.TestWrapper(t)
iamRepo := iam.TestRepo(t, conn, wrap)
@@ -87,6 +88,12 @@ func TestGet(t *testing.T) {
return repo, nil
}
workerAuthRepo, err := server.NewRepositoryStorage(ctx, rw, rw, kms)
require.NoError(t, err)
workerAuthRepoFn := func() (*server.WorkerAuthRepositoryStorage, error) {
return workerAuthRepo, nil
}
kmsWorker := server.TestKmsWorker(t, conn, wrap,
server.WithName("test kms worker names"),
server.WithDescription("test kms worker description"),
@@ -109,6 +116,7 @@ func TestGet(t *testing.T) {
ActiveConnectionCount: &wrapperspb.UInt32Value{Value: 0},
AuthorizedActions: strutil.StrListDelete(kmsAuthzActions, action.Update.String()),
LastStatusTime: kmsWorker.GetLastStatusTime().GetTimestamp(),
ReleaseVersion: kmsWorker.ReleaseVersion,
CanonicalTags: map[string]*structpb.ListValue{
"key": structListValue(t, "val"),
},
@@ -149,6 +157,7 @@ func TestGet(t *testing.T) {
AuthorizedActions: testAuthorizedActions,
ActiveConnectionCount: &wrapperspb.UInt32Value{Value: 0},
LastStatusTime: pkiWorker.GetLastStatusTime().GetTimestamp(),
ReleaseVersion: pkiWorker.ReleaseVersion,
CanonicalTags: map[string]*structpb.ListValue{
"config": structListValue(t, "test"),
},
@@ -198,7 +207,7 @@ func TestGet(t *testing.T) {
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
s, err := NewService(context.Background(), repoFn, iamRepoFn)
s, err := NewService(context.Background(), repoFn, iamRepoFn, workerAuthRepoFn)
require.NoError(t, err, "Couldn't create new worker service.")
got, err := s.GetWorker(auth.DisabledAuthTestContext(iamRepoFn, tc.scopeId), tc.req)
@@ -214,6 +223,7 @@ func TestGet(t *testing.T) {
}
func TestList(t *testing.T) {
ctx := context.Background()
conn, _ := db.TestSetup(t, "postgres")
wrap := db.TestWrapper(t)
iamRepo := iam.TestRepo(t, conn, wrap)
@@ -226,6 +236,12 @@ func TestList(t *testing.T) {
return server.NewRepository(rw, rw, kms)
}
workerAuthRepo, err := server.NewRepositoryStorage(ctx, rw, rw, kms)
require.NoError(t, err)
workerAuthRepoFn := func() (*server.WorkerAuthRepositoryStorage, error) {
return workerAuthRepo, nil
}
var wantKmsWorkers []*pb.Worker
for i := 0; i < 10; i++ {
w := server.TestKmsWorker(t, conn, wrap, server.WithName(fmt.Sprintf("kms-worker%d", i)))
@@ -244,6 +260,7 @@ func TestList(t *testing.T) {
Address: w.GetAddress(),
Type: KmsWorkerType,
LastStatusTime: w.GetLastStatusTime().GetTimestamp(),
ReleaseVersion: w.ReleaseVersion,
})
}
@@ -263,6 +280,7 @@ func TestList(t *testing.T) {
Address: w.GetAddress(),
Type: PkiWorkerType,
LastStatusTime: w.GetLastStatusTime().GetTimestamp(),
ReleaseVersion: w.ReleaseVersion,
})
}
@@ -312,7 +330,7 @@ func TestList(t *testing.T) {
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
s, err := NewService(context.Background(), repoFn, iamRepoFn)
s, err := NewService(context.Background(), repoFn, iamRepoFn, workerAuthRepoFn)
require.NoError(err, "Couldn't create new worker service.")
// Test with a non-anon user
@@ -359,7 +377,13 @@ func TestDelete(t *testing.T) {
}
ctx := context.Background()
s, err := NewService(ctx, repoFn, iamRepoFn)
workerAuthRepo, err := server.NewRepositoryStorage(ctx, rw, rw, kms)
require.NoError(t, err)
workerAuthRepoFn := func() (*server.WorkerAuthRepositoryStorage, error) {
return workerAuthRepo, nil
}
s, err := NewService(ctx, repoFn, iamRepoFn, workerAuthRepoFn)
require.NoError(t, err, "Error when getting new worker service.")
w := server.TestKmsWorker(t, conn, wrap)
@@ -426,6 +450,12 @@ func TestUpdate(t *testing.T) {
return repo, nil
}
workerAuthRepo, err := server.NewRepositoryStorage(ctx, rw, rw, kms)
require.NoError(t, err)
workerAuthRepoFn := func() (*server.WorkerAuthRepositoryStorage, error) {
return workerAuthRepo, nil
}
wkr := server.TestPkiWorker(t, conn, wrapper,
server.WithName("default"),
server.WithDescription("default"))
@@ -443,7 +473,7 @@ func TestUpdate(t *testing.T) {
toMerge := &pbs.UpdateWorkerRequest{
Id: wkr.GetPublicId(),
}
workerService, err := NewService(ctx, repoFn, iamRepoFn)
workerService, err := NewService(ctx, repoFn, iamRepoFn, workerAuthRepoFn)
require.NoError(t, err)
expectedScope := &scopes.ScopeInfo{Id: scope.Global.String(), Type: scope.Global.String(), Name: scope.Global.String(), Description: "Global Scope"}
@@ -837,6 +867,12 @@ func TestUpdate_KMS(t *testing.T) {
return repo, nil
}
workerAuthRepo, err := server.NewRepositoryStorage(ctx, rw, rw, kms)
require.NoError(t, err)
workerAuthRepoFn := func() (*server.WorkerAuthRepositoryStorage, error) {
return workerAuthRepo, nil
}
wkr := server.TestKmsWorker(t, conn, wrapper,
server.WithName("default"),
server.WithDescription("default"))
@@ -844,7 +880,7 @@ func TestUpdate_KMS(t *testing.T) {
toMerge := &pbs.UpdateWorkerRequest{
Id: wkr.GetPublicId(),
}
workerService, err := NewService(ctx, repoFn, iamRepoFn)
workerService, err := NewService(ctx, repoFn, iamRepoFn, workerAuthRepoFn)
require.NoError(t, err)
cases := []struct {
@@ -901,7 +937,9 @@ func TestUpdate_KMS(t *testing.T) {
func TestUpdate_BadVersion(t *testing.T) {
t.Parallel()
ctx := context.Background()
conn, _ := db.TestSetup(t, "postgres")
rw := db.New(conn)
wrapper := db.TestWrapper(t)
kms := kms.TestKms(t, conn, wrapper)
@@ -909,18 +947,21 @@ func TestUpdate_BadVersion(t *testing.T) {
iamRepoFn := func() (*iam.Repository, error) {
return iamRepo, nil
}
workerAuthRepo, err := server.NewRepositoryStorage(ctx, rw, rw, kms)
require.NoError(t, err)
workerAuthRepoFn := func() (*server.WorkerAuthRepositoryStorage, error) {
return workerAuthRepo, nil
}
_, proj := iam.TestScopes(t, iamRepo)
rw := db.New(conn)
repo, err := server.NewRepository(rw, rw, kms)
require.NoError(t, err, "Couldn't create new worker repo.")
repoFn := func() (*server.Repository, error) {
return repo, nil
}
ctx := context.Background()
workerService, err := NewService(ctx, repoFn, iamRepoFn)
workerService, err := NewService(ctx, repoFn, iamRepoFn, workerAuthRepoFn)
require.NoError(t, err, "Failed to create a new host set service.")
wkr := server.TestPkiWorker(t, conn, wrapper)
@@ -951,9 +992,15 @@ func TestCreateWorkerLed(t *testing.T) {
repoFn := func() (*server.Repository, error) {
return server.NewRepository(rw, rw, testKms)
}
testCtx := context.Background()
testSrv, err := NewService(testCtx, repoFn, iamRepoFn)
testCtx := context.Background()
workerAuthRepo, err := server.NewRepositoryStorage(testCtx, rw, rw, testKms)
require.NoError(t, err)
workerAuthRepoFn := func() (*server.WorkerAuthRepositoryStorage, error) {
return workerAuthRepo, nil
}
testSrv, err := NewService(testCtx, repoFn, iamRepoFn, workerAuthRepoFn)
require.NoError(t, err, "Error when getting new worker service.")
// Get an initial set of authorized node credentials
@@ -1205,7 +1252,7 @@ func TestCreateWorkerLed(t *testing.T) {
repoFn := func() (*server.Repository, error) {
return server.NewRepository(rw, &db.Db{}, testKms)
}
testSrv, err := NewService(testCtx, repoFn, iamRepoFn)
testSrv, err := NewService(testCtx, repoFn, iamRepoFn, workerAuthRepoFn)
require.NoError(t, err, "Error when getting new worker service.")
return testSrv
}(),
@@ -1238,7 +1285,7 @@ func TestCreateWorkerLed(t *testing.T) {
return server.NewRepository(rw, rw, testKms)
}
}
testSrv, err := NewService(testCtx, repoFn, iamRepoFn)
testSrv, err := NewService(testCtx, repoFn, iamRepoFn, workerAuthRepoFn)
require.NoError(t, err, "Error when getting new worker service.")
return testSrv
}(),
@@ -1333,12 +1380,16 @@ func TestCreateControllerLed(t *testing.T) {
}
testCtx := context.Background()
testSrv, err := NewService(testCtx, repoFn, iamRepoFn)
rootStorage, err := server.NewRepositoryStorage(testCtx, rw, rw, testKms)
require.NoError(t, err)
authRepoFn := func() (*server.WorkerAuthRepositoryStorage, error) {
return rootStorage, nil
}
testSrv, err := NewService(testCtx, repoFn, iamRepoFn, authRepoFn)
require.NoError(t, err, "Error when getting new worker service.")
// Get an initial set of authorized node credentials
rootStorage, err := server.NewRepositoryStorage(testCtx, rw, rw, testKms)
require.NoError(t, err)
_, err = rotation.RotateRootCertificates(testCtx, rootStorage)
require.NoError(t, err)
@@ -1549,7 +1600,7 @@ func TestCreateControllerLed(t *testing.T) {
repoFn := func() (*server.Repository, error) {
return server.NewRepository(rw, &db.Db{}, testKms)
}
testSrv, err := NewService(testCtx, repoFn, iamRepoFn)
testSrv, err := NewService(testCtx, repoFn, iamRepoFn, authRepoFn)
require.NoError(t, err, "Error when getting new worker service.")
return testSrv
}(),
@@ -1581,7 +1632,7 @@ func TestCreateControllerLed(t *testing.T) {
return server.NewRepository(rw, rw, testKms)
}
}
testSrv, err := NewService(testCtx, repoFn, iamRepoFn)
testSrv, err := NewService(testCtx, repoFn, iamRepoFn, authRepoFn)
require.NoError(t, err, "Error when getting new worker service.")
return testSrv
}(),
@@ -1664,6 +1715,7 @@ func TestCreateControllerLed(t *testing.T) {
func TestService_AddWorkerTags(t *testing.T) {
t.Parallel()
ctx := context.Background()
assert, require := assert.New(t), require.New(t)
conn, _ := db.TestSetup(t, "postgres")
@@ -1678,7 +1730,12 @@ func TestService_AddWorkerTags(t *testing.T) {
repoFn := func() (*server.Repository, error) {
return server.NewRepository(rw, rw, testKms)
}
s, err := NewService(context.Background(), repoFn, iamRepoFn)
workerAuthRepo, err := server.NewRepositoryStorage(ctx, rw, rw, testKms)
require.NoError(err)
workerAuthRepoFn := func() (*server.WorkerAuthRepositoryStorage, error) {
return workerAuthRepo, nil
}
s, err := NewService(context.Background(), repoFn, iamRepoFn, workerAuthRepoFn)
require.NoError(err)
worker := server.TestKmsWorker(t, conn, wrapper)
@@ -1809,7 +1866,7 @@ func TestService_AddWorkerTags(t *testing.T) {
func TestService_SetWorkerTags(t *testing.T) {
t.Parallel()
assert, require := assert.New(t), require.New(t)
ctx := context.Background()
conn, _ := db.TestSetup(t, "postgres")
wrapper := db.TestWrapper(t)
iamRepo := iam.TestRepo(t, conn, wrapper)
@@ -1822,7 +1879,12 @@ func TestService_SetWorkerTags(t *testing.T) {
repoFn := func() (*server.Repository, error) {
return server.NewRepository(rw, rw, testKms)
}
s, err := NewService(context.Background(), repoFn, iamRepoFn)
workerAuthRepo, err := server.NewRepositoryStorage(ctx, rw, rw, testKms)
require.NoError(err)
workerAuthRepoFn := func() (*server.WorkerAuthRepositoryStorage, error) {
return workerAuthRepo, nil
}
s, err := NewService(context.Background(), repoFn, iamRepoFn, workerAuthRepoFn)
require.NoError(err)
worker := server.TestKmsWorker(t, conn, wrapper)
@@ -1956,7 +2018,7 @@ func TestService_SetWorkerTags(t *testing.T) {
func TestService_RemoveWorkerTags(t *testing.T) {
t.Parallel()
assert, require := assert.New(t), require.New(t)
ctx := context.Background()
conn, _ := db.TestSetup(t, "postgres")
wrapper := db.TestWrapper(t)
iamRepo := iam.TestRepo(t, conn, wrapper)
@@ -1969,7 +2031,12 @@ func TestService_RemoveWorkerTags(t *testing.T) {
repoFn := func() (*server.Repository, error) {
return server.NewRepository(rw, rw, testKms)
}
s, err := NewService(context.Background(), repoFn, iamRepoFn)
workerAuthRepo, err := server.NewRepositoryStorage(ctx, rw, rw, testKms)
require.NoError(err)
workerAuthRepoFn := func() (*server.WorkerAuthRepositoryStorage, error) {
return workerAuthRepo, nil
}
s, err := NewService(context.Background(), repoFn, iamRepoFn, workerAuthRepoFn)
require.NoError(err)
worker := server.TestKmsWorker(t, conn, wrapper)
@@ -2140,3 +2207,157 @@ func TestService_RemoveWorkerTags(t *testing.T) {
})
}
}
func TestReadCertificateAuthority(t *testing.T) {
t.Parallel()
require, assert := require.New(t), assert.New(t)
ctx := context.Background()
wrapper := db.TestWrapper(t)
conn, _ := db.TestSetup(t, "postgres")
rw := db.New(conn)
kmsCache := kms.TestKms(t, conn, wrapper)
iamRepo := iam.TestRepo(t, conn, wrapper)
iamRepoFn := func() (*iam.Repository, error) {
return iamRepo, nil
}
repoFn := func() (*server.Repository, error) {
return server.NewRepository(rw, rw, kmsCache)
}
workerAuthRepo, err := server.NewRepositoryStorage(ctx, rw, rw, kmsCache)
require.NoError(err)
workerAuthRepoFn := func() (*server.WorkerAuthRepositoryStorage, error) {
return workerAuthRepo, nil
}
// Store CA and check that initial version updates
_, err = rotation.RotateRootCertificates(ctx, workerAuthRepo)
require.NoError(err)
testSrv, err := NewService(ctx, repoFn, iamRepoFn, workerAuthRepoFn)
require.NoError(err, "Error when getting new worker service.")
tests := []struct {
name string
scopeId string
req *pbs.ReadCertificateAuthorityRequest
wantErr bool
wantErrIs error
wantErrContains string
}{
{
name: "invalid-scope",
scopeId: scope.Global.String(),
req: &pbs.ReadCertificateAuthorityRequest{
ScopeId: "invalid-scope",
},
wantErr: true,
wantErrIs: handlers.ApiErrorWithCode(codes.InvalidArgument),
wantErrContains: "Must be 'global'",
},
{
name: "success",
scopeId: scope.Global.String(),
req: &pbs.ReadCertificateAuthorityRequest{
ScopeId: "global",
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got, err := testSrv.ReadCertificateAuthority(auth.DisabledAuthTestContext(iamRepoFn, tc.scopeId), tc.req)
if tc.wantErr {
require.Error(err)
assert.Nil(got)
if tc.wantErrIs != nil {
assert.ErrorIs(err, tc.wantErrIs)
}
if tc.wantErrContains != "" {
assert.Contains(err.Error(), tc.wantErrContains)
}
return
}
require.NoError(err)
require.NotNil(got)
})
}
}
func TestReinitializeCertificateAuthority(t *testing.T) {
t.Parallel()
require, assert := require.New(t), assert.New(t)
ctx := context.Background()
wrapper := db.TestWrapper(t)
conn, _ := db.TestSetup(t, "postgres")
rw := db.New(conn)
kmsCache := kms.TestKms(t, conn, wrapper)
iamRepo := iam.TestRepo(t, conn, wrapper)
iamRepoFn := func() (*iam.Repository, error) {
return iamRepo, nil
}
repoFn := func() (*server.Repository, error) {
return server.NewRepository(rw, rw, kmsCache)
}
workerAuthRepo, err := server.NewRepositoryStorage(ctx, rw, rw, kmsCache)
require.NoError(err)
workerAuthRepoFn := func() (*server.WorkerAuthRepositoryStorage, error) {
return workerAuthRepo, nil
}
// Store CA and check that initial version updates
_, err = rotation.RotateRootCertificates(ctx, workerAuthRepo)
require.NoError(err)
testSrv, err := NewService(ctx, repoFn, iamRepoFn, workerAuthRepoFn)
require.NoError(err, "Error when getting new worker service.")
tests := []struct {
name string
scopeId string
req *pbs.ReinitializeCertificateAuthorityRequest
wantErr bool
wantErrIs error
wantErrContains string
}{
{
name: "invalid-id",
scopeId: scope.Global.String(),
req: &pbs.ReinitializeCertificateAuthorityRequest{
ScopeId: "bogus",
},
wantErr: true,
wantErrIs: handlers.ApiErrorWithCode(codes.InvalidArgument),
wantErrContains: "Must be 'global'",
},
{
name: "success",
scopeId: scope.Global.String(),
req: &pbs.ReinitializeCertificateAuthorityRequest{
ScopeId: "global",
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got, err := testSrv.ReinitializeCertificateAuthority(auth.DisabledAuthTestContext(iamRepoFn, tc.scopeId), tc.req)
if tc.wantErr {
require.Error(err)
assert.Nil(got)
if tc.wantErrIs != nil {
assert.ErrorIs(err, tc.wantErrIs)
}
if tc.wantErrContains != "" {
assert.Contains(err.Error(), tc.wantErrContains)
}
return
}
require.NoError(err)
require.NotNil(got)
})
}
}