From 51adde4b2cdec9eed9b375a00d195776bf9b763a Mon Sep 17 00:00:00 2001 From: Timothy Messier Date: Wed, 11 Oct 2023 18:26:16 +0000 Subject: [PATCH] feat(handlers): Register actions for each resource type This registers the ActionSets for each resource when the handler packages are initialized. This then allows other packages to retrieve the ActionSets for a given resource from the action package. The initial consumer of this will be the ratelimit package so that it can programmatically enumerate all of the valid combinations of a resource and action. This leaves a number of TODO to refactor the handler packages to also retrieve the action sets from the registered actions instead of using a package variable. This is left as a TODO since there is some inconsistencies in how these package variables are used between the handlers. For example a number of packages use a map of subtypes to ActionSets which is then used withing the handler implementation. Others use a map, but don't seem to use it at all. And others just use an ActionSet. --- .../handlers/accounts/account_service.go | 4 ++++ .../controller/handlers/authmethods/z_init.go | 20 +++++++++++++++++++ .../handlers/authtokens/authtoken_service.go | 5 +++++ .../credentiallibrary_service.go | 3 +++ .../credentials/credential_service.go | 3 +++ .../credentialstore_service.go | 3 +++ .../handlers/groups/group_service.go | 3 +++ .../host_catalogs/host_catalog_service.go | 3 +++ .../handlers/host_sets/host_set_service.go | 4 ++++ .../controller/handlers/hosts/host_service.go | 4 ++++ .../managed_groups/managed_group_service.go | 4 ++++ .../controller/handlers/roles/role_service.go | 3 +++ .../handlers/scopes/scope_service.go | 4 ++++ .../session_recording_service.go | 6 ++++++ .../handlers/sessions/session_service.go | 5 +++++ .../storage_buckets/storage_bucket_service.go | 6 ++++++ .../handlers/targets/target_service.go | 5 +++++ .../controller/handlers/users/user_service.go | 2 ++ .../handlers/workers/worker_service.go | 3 +++ 19 files changed, 90 insertions(+) create mode 100644 internal/daemon/controller/handlers/authmethods/z_init.go diff --git a/internal/daemon/controller/handlers/accounts/account_service.go b/internal/daemon/controller/handlers/accounts/account_service.go index 35051d5cb..07f5a533a 100644 --- a/internal/daemon/controller/handlers/accounts/account_service.go +++ b/internal/daemon/controller/handlers/accounts/account_service.go @@ -28,6 +28,7 @@ import ( "github.com/hashicorp/boundary/internal/types/resource" "github.com/hashicorp/boundary/internal/types/subtypes" pb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/accounts" + "golang.org/x/exp/maps" "google.golang.org/grpc/codes" "google.golang.org/protobuf/types/known/structpb" "google.golang.org/protobuf/types/known/wrapperspb" @@ -108,6 +109,9 @@ func init() { if oidcMaskManager, err = handlers.NewMaskManager(context.Background(), handlers.MaskDestination{&oidcstore.Account{}}, handlers.MaskSource{&pb.Account{}, &pb.OidcAccountAttributes{}}); err != nil { panic(err) } + + // TODO: refactor to remove IdActions and CollectionActions package variables + action.RegisterResource(resource.Account, action.Union(maps.Values(IdActions)...), CollectionActions) } // Service handles request as described by the pbs.AccountServiceServer interface. diff --git a/internal/daemon/controller/handlers/authmethods/z_init.go b/internal/daemon/controller/handlers/authmethods/z_init.go new file mode 100644 index 000000000..0fa1a7f32 --- /dev/null +++ b/internal/daemon/controller/handlers/authmethods/z_init.go @@ -0,0 +1,20 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: BUSL-1.1 + +package authmethods + +import ( + "github.com/hashicorp/boundary/internal/types/action" + "github.com/hashicorp/boundary/internal/types/resource" + "golang.org/x/exp/maps" +) + +// init functions process in alphabetical order of filenames within a package. +// this needs to run last since it used the IdActions variable which gets +// modified in other init functions in this package. +func init() { + // TODO: refactor to remove IdActions and CollectionActions package variables + // Then this registration can happen in authmethod_service.go and we don't + // need to worry about the order of init functions in a package. + action.RegisterResource(resource.AuthMethod, action.Union(maps.Values(IdActions)...), CollectionActions) +} diff --git a/internal/daemon/controller/handlers/authtokens/authtoken_service.go b/internal/daemon/controller/handlers/authtokens/authtoken_service.go index 1a87df8bc..853fc4d79 100644 --- a/internal/daemon/controller/handlers/authtokens/authtoken_service.go +++ b/internal/daemon/controller/handlers/authtokens/authtoken_service.go @@ -42,6 +42,11 @@ var ( ) ) +func init() { + // TODO: refactor to remove IdActionsMap and CollectionActions package variables + action.RegisterResource(resource.AuthToken, IdActions, CollectionActions) +} + // Service handles request as described by the pbs.AuthTokenServiceServer interface. type Service struct { pbs.UnsafeAuthTokenServiceServer diff --git a/internal/daemon/controller/handlers/credentiallibraries/credentiallibrary_service.go b/internal/daemon/controller/handlers/credentiallibraries/credentiallibrary_service.go index a72dfbdf5..f58d69ab2 100644 --- a/internal/daemon/controller/handlers/credentiallibraries/credentiallibrary_service.go +++ b/internal/daemon/controller/handlers/credentiallibraries/credentiallibrary_service.go @@ -103,6 +103,9 @@ func init() { ); err != nil { panic(err) } + + // TODO: refactor to remove IdActionsMap and CollectionActions package variables + action.RegisterResource(resource.CredentialLibrary, IdActions, CollectionActions) } // Service handles request as described by the pbs.CredentialLibraryServiceServer interface. diff --git a/internal/daemon/controller/handlers/credentials/credential_service.go b/internal/daemon/controller/handlers/credentials/credential_service.go index c6e824baa..beb9c5cba 100644 --- a/internal/daemon/controller/handlers/credentials/credential_service.go +++ b/internal/daemon/controller/handlers/credentials/credential_service.go @@ -86,6 +86,9 @@ func init() { ); err != nil { panic(err) } + + // TODO: refactor to remove IdActionsMap and CollectionActions package variables + action.RegisterResource(resource.Credential, IdActions, CollectionActions) } // Service handles request as described by the pbs.CredentialServiceServer interface. diff --git a/internal/daemon/controller/handlers/credentialstores/credentialstore_service.go b/internal/daemon/controller/handlers/credentialstores/credentialstore_service.go index 813fb05e8..b73a42083 100644 --- a/internal/daemon/controller/handlers/credentialstores/credentialstore_service.go +++ b/internal/daemon/controller/handlers/credentialstores/credentialstore_service.go @@ -86,6 +86,9 @@ func init() { ); err != nil { panic(err) } + + // TODO: refactor to remove IdActionsMap and CollectionActions package variables + action.RegisterResource(resource.CredentialStore, IdActions, CollectionActions) } // Service handles request as described by the pbs.CredentialStoreServiceServer interface. diff --git a/internal/daemon/controller/handlers/groups/group_service.go b/internal/daemon/controller/handlers/groups/group_service.go index f67d24064..cc12412df 100644 --- a/internal/daemon/controller/handlers/groups/group_service.go +++ b/internal/daemon/controller/handlers/groups/group_service.go @@ -59,6 +59,9 @@ func init() { ); err != nil { panic(err) } + + // TODO: refactor to remove IdActionsMap and CollectionActions package variables + action.RegisterResource(resource.Group, IdActions, CollectionActions) } // Service handles request as described by the pbs.GroupServiceServer interface. diff --git a/internal/daemon/controller/handlers/host_catalogs/host_catalog_service.go b/internal/daemon/controller/handlers/host_catalogs/host_catalog_service.go index b0070dd3e..22be14e28 100644 --- a/internal/daemon/controller/handlers/host_catalogs/host_catalog_service.go +++ b/internal/daemon/controller/handlers/host_catalogs/host_catalog_service.go @@ -89,6 +89,9 @@ func init() { ); err != nil { panic(err) } + + // TODO: refactor to remove idActionsMap and CollectionActions package variables + action.RegisterResource(resource.HostCatalog, IdActions, CollectionActions) } type Service struct { diff --git a/internal/daemon/controller/handlers/host_sets/host_set_service.go b/internal/daemon/controller/handlers/host_sets/host_set_service.go index 8f3dadc11..530e568e3 100644 --- a/internal/daemon/controller/handlers/host_sets/host_set_service.go +++ b/internal/daemon/controller/handlers/host_sets/host_set_service.go @@ -28,6 +28,7 @@ import ( pb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/hostsets" "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/plugins" "github.com/hashicorp/go-secure-stdlib/strutil" + "golang.org/x/exp/maps" "google.golang.org/grpc/codes" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/known/structpb" @@ -83,6 +84,9 @@ func init() { ); err != nil { panic(err) } + + // TODO: refactor to remove idActionsMap and CollectionActions package variables + action.RegisterResource(resource.HostSet, action.Union(maps.Values(idActionsTypeMap)...), CollectionActions) } type Service struct { diff --git a/internal/daemon/controller/handlers/hosts/host_service.go b/internal/daemon/controller/handlers/hosts/host_service.go index 8eeef2617..2617a5e42 100644 --- a/internal/daemon/controller/handlers/hosts/host_service.go +++ b/internal/daemon/controller/handlers/hosts/host_service.go @@ -27,6 +27,7 @@ import ( "github.com/hashicorp/boundary/internal/types/subtypes" pb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/hosts" "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/plugins" + "golang.org/x/exp/maps" "google.golang.org/grpc/codes" "google.golang.org/protobuf/types/known/wrapperspb" ) @@ -68,6 +69,9 @@ func init() { ); err != nil { panic(err) } + + // TODO: refactor to remove idActionsMap and CollectionActions package variables + action.RegisterResource(resource.Host, action.Union(maps.Values(idActionsTypeMap)...), CollectionActions) } type Service struct { diff --git a/internal/daemon/controller/handlers/managed_groups/managed_group_service.go b/internal/daemon/controller/handlers/managed_groups/managed_group_service.go index 55e9466fc..7d9e835eb 100644 --- a/internal/daemon/controller/handlers/managed_groups/managed_group_service.go +++ b/internal/daemon/controller/handlers/managed_groups/managed_group_service.go @@ -26,6 +26,7 @@ import ( "github.com/hashicorp/boundary/internal/types/subtypes" pb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/managedgroups" "github.com/hashicorp/go-bexpr" + "golang.org/x/exp/maps" "google.golang.org/grpc/codes" "google.golang.org/protobuf/types/known/wrapperspb" ) @@ -83,6 +84,9 @@ func init() { ); err != nil { panic(err) } + + // TODO: refactor to remove IdActions and CollectionActions package variables + action.RegisterResource(resource.ManagedGroup, action.Union(maps.Values(IdActions)...), CollectionActions) } // Service handles request as described by the pbs.ManagedGroupServiceServer interface. diff --git a/internal/daemon/controller/handlers/roles/role_service.go b/internal/daemon/controller/handlers/roles/role_service.go index fb88f43dd..4a7551abe 100644 --- a/internal/daemon/controller/handlers/roles/role_service.go +++ b/internal/daemon/controller/handlers/roles/role_service.go @@ -63,6 +63,9 @@ func init() { ); err != nil { panic(err) } + + // TODO: refactor to remove IdActions and CollectionActions package variables + action.RegisterResource(resource.Role, IdActions, CollectionActions) } // Service handles request as described by the pbs.RoleServiceServer interface. diff --git a/internal/daemon/controller/handlers/scopes/scope_service.go b/internal/daemon/controller/handlers/scopes/scope_service.go index c87313090..f385fac10 100644 --- a/internal/daemon/controller/handlers/scopes/scope_service.go +++ b/internal/daemon/controller/handlers/scopes/scope_service.go @@ -67,6 +67,7 @@ var ( action.DestroyScopeKeyVersion, ) + // TODO: get this from action registry scopeCollectionTypeMapMap = map[string]map[resource.Type]action.ActionSet{ scope.Global.String(): { resource.AuthMethod: authmethods.CollectionActions, @@ -117,6 +118,9 @@ func init() { ); err != nil { panic(err) } + + // TODO: refactor to remove IdActions and CollectionActions package variables + action.RegisterResource(resource.Scope, IdActions, CollectionActions) } // Service handles requests as described by the pbs.ScopeServiceServer interface. diff --git a/internal/daemon/controller/handlers/session_recordings/session_recording_service.go b/internal/daemon/controller/handlers/session_recordings/session_recording_service.go index 5cff170a3..0741bf955 100644 --- a/internal/daemon/controller/handlers/session_recordings/session_recording_service.go +++ b/internal/daemon/controller/handlers/session_recordings/session_recording_service.go @@ -12,6 +12,7 @@ import ( intglobals "github.com/hashicorp/boundary/internal/globals" "github.com/hashicorp/boundary/internal/kms" "github.com/hashicorp/boundary/internal/types/action" + "github.com/hashicorp/boundary/internal/types/resource" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" ) @@ -32,6 +33,11 @@ var ( ) ) +func init() { + // TODO: refactor to remove IdActions and CollectionActions package variables + action.RegisterResource(resource.SessionRecording, IdActions, CollectionActions) +} + // NewServiceFn returns a storage bucket service which is not implemented in OSS var NewServiceFn = func(ctx context.Context, iamRepoFn common.IamRepoFactory, diff --git a/internal/daemon/controller/handlers/sessions/session_service.go b/internal/daemon/controller/handlers/sessions/session_service.go index 589b4070e..4c24a8878 100644 --- a/internal/daemon/controller/handlers/sessions/session_service.go +++ b/internal/daemon/controller/handlers/sessions/session_service.go @@ -44,6 +44,11 @@ var ( ) ) +func init() { + // TODO: refactor to remove IdActions and CollectionActions package variables + action.RegisterResource(resource.Session, IdActions, CollectionActions) +} + // Service handles request as described by the pbs.SessionServiceServer interface. type Service struct { pbs.UnsafeSessionServiceServer diff --git a/internal/daemon/controller/handlers/storage_buckets/storage_bucket_service.go b/internal/daemon/controller/handlers/storage_buckets/storage_bucket_service.go index 62eb333b7..1061c9897 100644 --- a/internal/daemon/controller/handlers/storage_buckets/storage_bucket_service.go +++ b/internal/daemon/controller/handlers/storage_buckets/storage_bucket_service.go @@ -10,6 +10,7 @@ import ( pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services" "github.com/hashicorp/boundary/internal/globals" "github.com/hashicorp/boundary/internal/types/action" + "github.com/hashicorp/boundary/internal/types/resource" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" ) @@ -32,6 +33,11 @@ var ( ) ) +func init() { + // TODO: refactor to remove IdActions and CollectionActions package variables + action.RegisterResource(resource.StorageBucket, IdActions, CollectionActions) +} + // NewServiceFn returns a storage bucket service which is not implemented in OSS var NewServiceFn = func(ctx context.Context, pluginStorageRepoFn common.PluginStorageBucketRepoFactory, diff --git a/internal/daemon/controller/handlers/targets/target_service.go b/internal/daemon/controller/handlers/targets/target_service.go index fdccef245..2e95e52e1 100644 --- a/internal/daemon/controller/handlers/targets/target_service.go +++ b/internal/daemon/controller/handlers/targets/target_service.go @@ -102,6 +102,11 @@ var ( WorkerFilterDeprecationMessage = fmt.Sprintf("This field is deprecated. Use %s instead.", globals.EgressWorkerFilterField) ) +func init() { + // TODO: refactor to remove IdActions and CollectionActions package variables + action.RegisterResource(resource.Target, IdActions, CollectionActions) +} + func IngressWorkerFilterUnsupported(string) error { return fmt.Errorf("Ingress Worker Filter field is not supported in OSS") } diff --git a/internal/daemon/controller/handlers/users/user_service.go b/internal/daemon/controller/handlers/users/user_service.go index 56b5d099c..f66b175c4 100644 --- a/internal/daemon/controller/handlers/users/user_service.go +++ b/internal/daemon/controller/handlers/users/user_service.go @@ -59,6 +59,8 @@ func init() { ); err != nil { panic(err) } + // TODO: refactor to remove IdActions and CollectionActions package variables + action.RegisterResource(resource.User, IdActions, CollectionActions) } // Service handles request as described by the pbs.UserServiceServer interface. diff --git a/internal/daemon/controller/handlers/workers/worker_service.go b/internal/daemon/controller/handlers/workers/worker_service.go index a62f15911..16a1d154c 100644 --- a/internal/daemon/controller/handlers/workers/worker_service.go +++ b/internal/daemon/controller/handlers/workers/worker_service.go @@ -80,6 +80,9 @@ func init() { ); err != nil { panic(err) } + + // TODO: refactor to remove IdActions and CollectionActions package variables + action.RegisterResource(resource.Worker, IdActions, CollectionActions) } func emptyDownstreamWorkers(context.Context, string, common.Downstreamers) []string {