feature (worker): add CreateWorker(...) service (aka API) (#2143)
This commit is contained in:
14 files changed
+773
-233
No files matched your search
@@ -97,7 +97,7 @@ var globalAuthorizedCollectionActions = map[string]*structpb.ListValue{
|
||||
},
|
||||
"workers": {
|
||||
Values: []*structpb.Value{
|
||||
structpb.NewStringValue("create:worker-request"),
|
||||
structpb.NewStringValue("create:worker-led"),
|
||||
structpb.NewStringValue("list"),
|
||||
},
|
||||
},
|
||||
|
||||
@@ -19,7 +19,10 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/types/resource"
|
||||
"github.com/hashicorp/boundary/internal/types/scope"
|
||||
pb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/workers"
|
||||
"github.com/hashicorp/nodeenrollment/types"
|
||||
"github.com/mr-tron/base58"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"google.golang.org/protobuf/types/known/structpb"
|
||||
"google.golang.org/protobuf/types/known/wrapperspb"
|
||||
)
|
||||
@@ -39,7 +42,7 @@ var (
|
||||
// CollectionActions contains the set of actions that can be performed on
|
||||
// this collection
|
||||
CollectionActions = action.ActionSet{
|
||||
action.CreateWorkerRequest,
|
||||
action.CreateWorkerLed,
|
||||
action.List,
|
||||
}
|
||||
)
|
||||
@@ -186,6 +189,53 @@ func (s Service) GetWorker(ctx context.Context, req *pbs.GetWorkerRequest) (*pbs
|
||||
return &pbs.GetWorkerResponse{Item: item}, nil
|
||||
}
|
||||
|
||||
// CreateWorker implements the interface pbs.WorkerServiceServer.
|
||||
func (s Service) CreateWorkerLed(ctx context.Context, req *pbs.CreateWorkerLedRequest) (*pbs.CreateWorkerLedResponse, error) {
|
||||
const op = "workers.(Service).CreateWorkerLed"
|
||||
|
||||
if err := validateCreateRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
authResults := s.authResult(ctx, req.GetItem().GetScopeId(), action.CreateWorkerLed)
|
||||
if authResults.Error != nil {
|
||||
return nil, authResults.Error
|
||||
}
|
||||
reqBytes, err := base58.FastBase58Decoding(req.GetItem().WorkerAuthToken.GetValue())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: error decoding node_credentials_token: %w", op, err)
|
||||
}
|
||||
// Decode the proto into the request
|
||||
creds := new(types.FetchNodeCredentialsRequest)
|
||||
if err := proto.Unmarshal(reqBytes, creds); err != nil {
|
||||
return nil, fmt.Errorf("%s: error unmarshaling node_credentials_token: %w", op, err)
|
||||
}
|
||||
created, err := s.createInRepo(ctx, req.GetItem(), creds)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: error creating worker: %w", op, err)
|
||||
}
|
||||
|
||||
outputFields, ok := requests.OutputFields(ctx)
|
||||
if !ok {
|
||||
return nil, errors.New(ctx, errors.Internal, op, "no request context found")
|
||||
}
|
||||
|
||||
outputOpts := make([]handlers.Option, 0, 3)
|
||||
outputOpts = append(outputOpts, handlers.WithOutputFields(&outputFields))
|
||||
if outputFields.Has(globals.ScopeField) {
|
||||
outputOpts = append(outputOpts, handlers.WithScope(authResults.Scope))
|
||||
}
|
||||
if outputFields.Has(globals.AuthorizedActionsField) {
|
||||
outputOpts = append(outputOpts, handlers.WithAuthorizedActions(authResults.FetchActionSetForId(ctx, created.GetPublicId(), IdActions).Strings()))
|
||||
}
|
||||
|
||||
item, err := toProto(ctx, created, outputOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pbs.CreateWorkerLedResponse{Item: item}, nil
|
||||
}
|
||||
|
||||
// DeleteWorker implements the interface pbs.WorkerServiceServer.
|
||||
func (s Service) DeleteWorker(ctx context.Context, req *pbs.DeleteWorkerRequest) (*pbs.DeleteWorkerResponse, error) {
|
||||
if err := validateDeleteRequest(req); err != nil {
|
||||
@@ -270,6 +320,25 @@ func (s Service) getFromRepo(ctx context.Context, id string) (*servers.Worker, e
|
||||
return w, nil
|
||||
}
|
||||
|
||||
func (s Service) createInRepo(ctx context.Context, worker *pb.Worker, creds *types.FetchNodeCredentialsRequest) (*servers.Worker, error) {
|
||||
const op = "workers.(Service).createInRepo"
|
||||
repo, err := s.repoFn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
newWorker := servers.NewWorker(
|
||||
worker.GetScopeId(),
|
||||
servers.WithName(worker.GetName().GetValue()),
|
||||
servers.WithDescription(worker.GetDescription().GetValue()),
|
||||
servers.WithAddress(worker.GetAddress().GetValue()),
|
||||
)
|
||||
retWorker, err := repo.CreateWorker(ctx, newWorker, servers.WithFetchNodeCredentialsRequest(creds))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to create worker"))
|
||||
}
|
||||
return retWorker, nil
|
||||
}
|
||||
|
||||
func (s Service) deleteFromRepo(ctx context.Context, id string) (bool, error) {
|
||||
const op = "workers.(Service).deleteFromRepo"
|
||||
repo, err := s.repoFn()
|
||||
@@ -329,7 +398,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.Create:
|
||||
case action.List, action.CreateWorkerLed:
|
||||
parentId = id
|
||||
default:
|
||||
w, err := repo.LookupWorker(ctx, id)
|
||||
@@ -420,8 +489,10 @@ func toProto(ctx context.Context, in *servers.Worker, opt ...handlers.Option) (*
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("error preparing config tags proto"))
|
||||
}
|
||||
}
|
||||
out.GetWorkerConfig().Address = in.GetWorkerReportedAddress()
|
||||
out.GetWorkerConfig().Name = in.GetWorkerReportedName()
|
||||
if out.WorkerConfig != nil {
|
||||
out.GetWorkerConfig().Address = in.GetWorkerReportedAddress()
|
||||
out.GetWorkerConfig().Name = in.GetWorkerReportedName()
|
||||
}
|
||||
}
|
||||
|
||||
return &out, nil
|
||||
@@ -492,3 +563,41 @@ func validateUpdateRequest(req *pbs.UpdateWorkerRequest) error {
|
||||
return badFields
|
||||
}, servers.WorkerPrefix)
|
||||
}
|
||||
|
||||
func validateCreateRequest(req *pbs.CreateWorkerLedRequest) error {
|
||||
return handlers.ValidateCreateRequest(req.GetItem(), func() map[string]string {
|
||||
const (
|
||||
mustBeGlobalMsg = "Must be 'global'"
|
||||
cannotBeEmptyMsg = "Cannot be empty."
|
||||
readOnlyFieldMsg = "This is a read only field."
|
||||
)
|
||||
badFields := map[string]string{}
|
||||
if scope.Global.String() != req.GetItem().GetScopeId() {
|
||||
badFields[globals.ScopeIdField] = mustBeGlobalMsg
|
||||
}
|
||||
// FIXME: in the future, we won't require this token since we'll support
|
||||
// the server led flow where a token is returned when a worker is created.
|
||||
if req.GetItem().WorkerAuthToken == nil {
|
||||
badFields[globals.WorkerAuthTokenField] = cannotBeEmptyMsg
|
||||
}
|
||||
if req.GetItem().CanonicalAddress != "" {
|
||||
badFields[globals.CanonicalAddressField] = readOnlyFieldMsg
|
||||
}
|
||||
if req.GetItem().Tags != nil {
|
||||
badFields[globals.TagsField] = readOnlyFieldMsg
|
||||
}
|
||||
if req.GetItem().CanonicalTags != nil {
|
||||
badFields[globals.CanonicalTagsField] = readOnlyFieldMsg
|
||||
}
|
||||
if req.GetItem().LastStatusTime != nil {
|
||||
badFields[globals.LastStatusTimeField] = readOnlyFieldMsg
|
||||
}
|
||||
if req.GetItem().WorkerConfig != nil {
|
||||
badFields[globals.WorkerConfigField] = readOnlyFieldMsg
|
||||
}
|
||||
if req.GetItem().AuthorizedActions != nil {
|
||||
badFields[globals.AuthorizedActionsField] = readOnlyFieldMsg
|
||||
}
|
||||
return badFields
|
||||
})
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/hashicorp/boundary/globals"
|
||||
"github.com/hashicorp/boundary/internal/daemon/controller/auth"
|
||||
"github.com/hashicorp/boundary/internal/daemon/controller/handlers"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
@@ -17,7 +18,12 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/servers"
|
||||
"github.com/hashicorp/boundary/internal/types/scope"
|
||||
"github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/scopes"
|
||||
"github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/workers"
|
||||
pb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/workers"
|
||||
"github.com/hashicorp/nodeenrollment/rotation"
|
||||
"github.com/hashicorp/nodeenrollment/storage/file"
|
||||
"github.com/hashicorp/nodeenrollment/types"
|
||||
"github.com/mr-tron/base58"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/genproto/protobuf/field_mask"
|
||||
@@ -775,3 +781,396 @@ func TestUpdate_BadVersion(t *testing.T) {
|
||||
assert.Error(t, err)
|
||||
assert.True(t, errors.Is(err, handlers.NotFoundError()), "Got %v, wanted not found error.", err)
|
||||
}
|
||||
|
||||
func TestCreateWorkerLed(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
testRootWrapper := db.TestWrapper(t)
|
||||
iamRepo := iam.TestRepo(t, conn, testRootWrapper)
|
||||
iamRepoFn := func() (*iam.Repository, error) {
|
||||
return iamRepo, nil
|
||||
}
|
||||
rw := db.New(conn)
|
||||
testKms := kms.TestKms(t, conn, testRootWrapper)
|
||||
repoFn := func() (*servers.Repository, error) {
|
||||
return servers.NewRepository(rw, rw, testKms)
|
||||
}
|
||||
testCtx := context.Background()
|
||||
|
||||
testSrv, err := NewService(testCtx, repoFn, iamRepoFn)
|
||||
require.NoError(t, err, "Error when getting new worker service.")
|
||||
|
||||
// Get an initial set of authorized node credentials
|
||||
rootStorage, err := servers.NewRepositoryStorage(testCtx, rw, rw, testKms)
|
||||
require.NoError(t, err)
|
||||
_, err = rotation.RotateRootCertificates(testCtx, rootStorage)
|
||||
require.NoError(t, err)
|
||||
|
||||
fetchReqFn := func() string {
|
||||
// This happens on the worker
|
||||
fileStorage, err := file.New(testCtx)
|
||||
require.NoError(t, err)
|
||||
defer fileStorage.Cleanup()
|
||||
|
||||
nodeCreds, err := types.NewNodeCredentials(testCtx, fileStorage)
|
||||
require.NoError(t, err)
|
||||
// Create request using worker id
|
||||
fetchReq, err := nodeCreds.CreateFetchNodeCredentialsRequest(testCtx)
|
||||
require.NoError(t, err)
|
||||
|
||||
fetchEncoded, err := proto.Marshal(fetchReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
return base58.Encode(fetchEncoded)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
scopeId string
|
||||
service Service
|
||||
req *pbs.CreateWorkerLedRequest
|
||||
res *pbs.CreateWorkerLedResponse
|
||||
wantErr bool
|
||||
wantErrIs error
|
||||
wantErrContains string
|
||||
}{
|
||||
{
|
||||
name: "invalid-scope",
|
||||
service: testSrv,
|
||||
scopeId: scope.Global.String(),
|
||||
req: &pbs.CreateWorkerLedRequest{
|
||||
Item: &workers.Worker{
|
||||
ScopeId: "invalid-scope",
|
||||
WorkerAuthToken: &wrapperspb.StringValue{Value: fetchReqFn()},
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
wantErrIs: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
wantErrContains: "Must be 'global'",
|
||||
},
|
||||
{
|
||||
name: "missing-node-credentials-token",
|
||||
service: testSrv,
|
||||
scopeId: scope.Global.String(),
|
||||
req: &pbs.CreateWorkerLedRequest{
|
||||
Item: &workers.Worker{
|
||||
ScopeId: scope.Global.String(),
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
wantErrIs: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
wantErrContains: globals.WorkerAuthTokenField,
|
||||
},
|
||||
{
|
||||
name: "invalid-id",
|
||||
service: testSrv,
|
||||
scopeId: scope.Global.String(),
|
||||
req: &pbs.CreateWorkerLedRequest{
|
||||
Item: &workers.Worker{
|
||||
ScopeId: scope.Global.String(),
|
||||
WorkerAuthToken: &wrapperspb.StringValue{Value: fetchReqFn()},
|
||||
Id: "invalid-id",
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
wantErrIs: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
wantErrContains: globals.IdField,
|
||||
},
|
||||
{
|
||||
name: "invalid-canonical-address",
|
||||
service: testSrv,
|
||||
scopeId: scope.Global.String(),
|
||||
req: &pbs.CreateWorkerLedRequest{
|
||||
Item: &workers.Worker{
|
||||
ScopeId: scope.Global.String(),
|
||||
WorkerAuthToken: &wrapperspb.StringValue{Value: fetchReqFn()},
|
||||
CanonicalAddress: "invalid-canonical-address",
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
wantErrIs: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
wantErrContains: globals.CanonicalAddressField,
|
||||
},
|
||||
{
|
||||
name: "invalid-tags",
|
||||
service: testSrv,
|
||||
scopeId: scope.Global.String(),
|
||||
req: &pbs.CreateWorkerLedRequest{
|
||||
Item: &workers.Worker{
|
||||
ScopeId: scope.Global.String(),
|
||||
WorkerAuthToken: &wrapperspb.StringValue{Value: fetchReqFn()},
|
||||
Tags: map[string]*structpb.ListValue{
|
||||
"invalid": {Values: []*structpb.Value{
|
||||
structpb.NewStringValue("invalid-tags"),
|
||||
}},
|
||||
},
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
wantErrIs: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
wantErrContains: globals.TagsField,
|
||||
},
|
||||
{
|
||||
name: "invalid-canonical-tags",
|
||||
service: testSrv,
|
||||
scopeId: scope.Global.String(),
|
||||
req: &pbs.CreateWorkerLedRequest{
|
||||
Item: &workers.Worker{
|
||||
ScopeId: scope.Global.String(),
|
||||
WorkerAuthToken: &wrapperspb.StringValue{Value: fetchReqFn()},
|
||||
CanonicalTags: map[string]*structpb.ListValue{
|
||||
"invalid": {Values: []*structpb.Value{
|
||||
structpb.NewStringValue("invalid-canonical-tags"),
|
||||
}},
|
||||
},
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
wantErrIs: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
wantErrContains: globals.CanonicalTagsField,
|
||||
},
|
||||
{
|
||||
name: "invalid-last-status-time",
|
||||
service: testSrv,
|
||||
scopeId: scope.Global.String(),
|
||||
req: &pbs.CreateWorkerLedRequest{
|
||||
Item: &workers.Worker{
|
||||
ScopeId: scope.Global.String(),
|
||||
WorkerAuthToken: &wrapperspb.StringValue{Value: fetchReqFn()},
|
||||
LastStatusTime: timestamppb.Now(),
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
wantErrIs: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
wantErrContains: globals.LastStatusTimeField,
|
||||
},
|
||||
{
|
||||
name: "invalid-worker-config",
|
||||
service: testSrv,
|
||||
scopeId: scope.Global.String(),
|
||||
req: &pbs.CreateWorkerLedRequest{
|
||||
Item: &workers.Worker{
|
||||
ScopeId: scope.Global.String(),
|
||||
WorkerAuthToken: &wrapperspb.StringValue{Value: fetchReqFn()},
|
||||
WorkerConfig: &pb.WorkerConfig{},
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
wantErrIs: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
wantErrContains: globals.WorkerConfigField,
|
||||
},
|
||||
{
|
||||
name: "invalid-authorized-actions",
|
||||
service: testSrv,
|
||||
scopeId: scope.Global.String(),
|
||||
req: &pbs.CreateWorkerLedRequest{
|
||||
Item: &workers.Worker{
|
||||
ScopeId: scope.Global.String(),
|
||||
WorkerAuthToken: &wrapperspb.StringValue{Value: fetchReqFn()},
|
||||
AuthorizedActions: []string{"invalid-authorized-actions"},
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
wantErrIs: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
wantErrContains: globals.AuthorizedActionsField,
|
||||
},
|
||||
{
|
||||
name: "invalid-create-time",
|
||||
service: testSrv,
|
||||
scopeId: scope.Global.String(),
|
||||
req: &pbs.CreateWorkerLedRequest{
|
||||
Item: &workers.Worker{
|
||||
ScopeId: scope.Global.String(),
|
||||
WorkerAuthToken: &wrapperspb.StringValue{Value: fetchReqFn()},
|
||||
CreatedTime: timestamppb.Now(),
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
wantErrIs: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
wantErrContains: globals.CreatedTimeField,
|
||||
},
|
||||
{
|
||||
name: "invalid-update-time",
|
||||
service: testSrv,
|
||||
scopeId: scope.Global.String(),
|
||||
req: &pbs.CreateWorkerLedRequest{
|
||||
Item: &workers.Worker{
|
||||
ScopeId: scope.Global.String(),
|
||||
WorkerAuthToken: &wrapperspb.StringValue{Value: fetchReqFn()},
|
||||
UpdatedTime: timestamppb.Now(),
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
wantErrIs: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
wantErrContains: globals.UpdatedTimeField,
|
||||
},
|
||||
{
|
||||
name: "invalid-version",
|
||||
service: testSrv,
|
||||
scopeId: scope.Global.String(),
|
||||
req: &pbs.CreateWorkerLedRequest{
|
||||
Item: &workers.Worker{
|
||||
ScopeId: scope.Global.String(),
|
||||
WorkerAuthToken: &wrapperspb.StringValue{Value: fetchReqFn()},
|
||||
Version: 1,
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
wantErrIs: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
wantErrContains: globals.VersionField,
|
||||
},
|
||||
{
|
||||
name: "invalid-auth",
|
||||
service: testSrv,
|
||||
scopeId: "splat-auth-scope",
|
||||
req: &pbs.CreateWorkerLedRequest{
|
||||
Item: &workers.Worker{
|
||||
ScopeId: scope.Global.String(),
|
||||
WorkerAuthToken: &wrapperspb.StringValue{Value: fetchReqFn()},
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
wantErrContains: "non-existent scope \"splat-auth-scope\"",
|
||||
},
|
||||
{
|
||||
name: "invalid-base58-encoding",
|
||||
service: testSrv,
|
||||
scopeId: scope.Global.String(),
|
||||
req: &pbs.CreateWorkerLedRequest{
|
||||
Item: &workers.Worker{
|
||||
ScopeId: scope.Global.String(),
|
||||
WorkerAuthToken: &wrapperspb.StringValue{Value: "invalid;semicolon"},
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
wantErrContains: "error decoding node_credentials_token",
|
||||
},
|
||||
{
|
||||
name: "invalid-marshal",
|
||||
service: testSrv,
|
||||
scopeId: scope.Global.String(),
|
||||
req: &pbs.CreateWorkerLedRequest{
|
||||
Item: &workers.Worker{
|
||||
ScopeId: scope.Global.String(),
|
||||
WorkerAuthToken: &wrapperspb.StringValue{Value: "notNodeCreds"},
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
wantErrContains: "error unmarshaling node_credentials_token",
|
||||
},
|
||||
{
|
||||
name: "create-error",
|
||||
service: func() Service {
|
||||
repoFn := func() (*servers.Repository, error) {
|
||||
return servers.NewRepository(rw, &db.Db{}, testKms)
|
||||
}
|
||||
testSrv, err := NewService(testCtx, repoFn, iamRepoFn)
|
||||
require.NoError(t, err, "Error when getting new worker service.")
|
||||
return testSrv
|
||||
}(),
|
||||
scopeId: scope.Global.String(),
|
||||
req: &pbs.CreateWorkerLedRequest{
|
||||
Item: &workers.Worker{
|
||||
ScopeId: scope.Global.String(),
|
||||
Name: &wrapperspb.StringValue{Value: "success"},
|
||||
Description: &wrapperspb.StringValue{Value: "success-description"},
|
||||
WorkerAuthToken: &wrapperspb.StringValue{Value: fetchReqFn()},
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
wantErrContains: "error creating worker",
|
||||
},
|
||||
{
|
||||
name: "bad-repo-function-in-create",
|
||||
service: func() Service {
|
||||
// var cnt gives us a way for the repoFn to fail the 2nd time
|
||||
// it's called so we can get past the AuthRequest check in
|
||||
// CreateWorker(...) and test the createInRepo(...) failure
|
||||
// path if the repoFn returns and err.
|
||||
cnt := 0
|
||||
repoFn := func() (*servers.Repository, error) {
|
||||
cnt = cnt + 1
|
||||
switch {
|
||||
case cnt > 1:
|
||||
return nil, errors.New(testCtx, errors.Internal, "bad-repo-function", "error creating repo")
|
||||
default:
|
||||
return servers.NewRepository(rw, rw, testKms)
|
||||
}
|
||||
}
|
||||
testSrv, err := NewService(testCtx, repoFn, iamRepoFn)
|
||||
require.NoError(t, err, "Error when getting new worker service.")
|
||||
return testSrv
|
||||
}(),
|
||||
scopeId: scope.Global.String(),
|
||||
req: &pbs.CreateWorkerLedRequest{
|
||||
Item: &workers.Worker{
|
||||
ScopeId: scope.Global.String(),
|
||||
Name: &wrapperspb.StringValue{Value: "success"},
|
||||
Description: &wrapperspb.StringValue{Value: "success-description"},
|
||||
WorkerAuthToken: &wrapperspb.StringValue{Value: fetchReqFn()},
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
wantErrContains: "error creating worker: bad-repo-function: error creating repo",
|
||||
},
|
||||
{
|
||||
name: "success",
|
||||
service: testSrv,
|
||||
scopeId: scope.Global.String(),
|
||||
req: &pbs.CreateWorkerLedRequest{
|
||||
Item: &workers.Worker{
|
||||
ScopeId: scope.Global.String(),
|
||||
Name: &wrapperspb.StringValue{Value: "success"},
|
||||
Description: &wrapperspb.StringValue{Value: "success-description"},
|
||||
WorkerAuthToken: &wrapperspb.StringValue{Value: fetchReqFn()},
|
||||
},
|
||||
},
|
||||
res: &pbs.CreateWorkerLedResponse{
|
||||
Item: &workers.Worker{
|
||||
ScopeId: scope.Global.String(),
|
||||
Name: &wrapperspb.StringValue{Value: "success"},
|
||||
Description: &wrapperspb.StringValue{Value: "success-description"},
|
||||
Version: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
got, err := tc.service.CreateWorkerLed(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)
|
||||
{
|
||||
// we only need to check that these "read-only" fields were set
|
||||
// outbound. Other portions of the domain is responsible for
|
||||
// testing that these values are correct and there's no reason
|
||||
// to repeat those tests here.
|
||||
assert.NotEmpty(got.GetItem().GetId())
|
||||
assert.NotEmpty(got.GetItem().CreatedTime)
|
||||
assert.NotEmpty(got.GetItem().UpdatedTime)
|
||||
assert.NotEmpty(got.GetItem().Scope)
|
||||
assert.NotEmpty(got.GetItem().AuthorizedActions)
|
||||
{
|
||||
tc.res.Item.Id = got.GetItem().GetId()
|
||||
tc.res.Item.CreatedTime = got.GetItem().GetCreatedTime()
|
||||
tc.res.Item.UpdatedTime = got.GetItem().GetUpdatedTime()
|
||||
tc.res.Item.Scope = got.GetItem().Scope
|
||||
tc.res.Item.AuthorizedActions = got.GetItem().GetAuthorizedActions()
|
||||
}
|
||||
}
|
||||
assert.Equal(tc.res, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user