Add generic read/update/delete commands (#3992)

* Add generic read/update/delete commands

This adds three generic commands that use the resource prefix to
determine the subcommand to run and pass through args over to that
subcommand. Since this occurs very early on and to avoid loading
essentially every internal resource package, I moved subtype definitions
to globals.

This replaces #3967

Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>

* De-duplicate Subtype code (#4031)

Co-authored-by: Timothy Messier <tim.messier@gmail.com>

---------

Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
Co-authored-by: Timothy Messier <tim.messier@gmail.com>
This commit is contained in:
authored and GitHub committed 2023-11-15 19:55:46 -05:00
1 parent 5c84a8861a
commit b6df5693b5
59 files changed
+1032 -857

No files matched your search

@@ -44,7 +44,7 @@ var (
// IdActions contains the set of actions that can be performed on
// individual resources
IdActions = map[subtypes.Subtype]action.ActionSet{
IdActions = map[globals.Subtype]action.ActionSet{
oidc.Subtype: {
action.NoOp,
action.Read,
@@ -137,7 +137,7 @@ func (s Service) ListManagedGroups(ctx context.Context, req *pbs.ListManagedGrou
}
for _, mg := range ul {
res.Id = mg.GetPublicId()
authorizedActions := authResults.FetchActionSetForId(ctx, mg.GetPublicId(), IdActions[subtypes.SubtypeFromId(domain, mg.GetPublicId())], requestauth.WithResource(&res)).Strings()
authorizedActions := authResults.FetchActionSetForId(ctx, mg.GetPublicId(), IdActions[globals.ResourceInfoFromPrefix(mg.GetPublicId()).Subtype], requestauth.WithResource(&res)).Strings()
if len(authorizedActions) == 0 {
continue
}
@@ -198,7 +198,7 @@ func (s Service) GetManagedGroup(ctx context.Context, req *pbs.GetManagedGroupRe
outputOpts = append(outputOpts, handlers.WithScope(authResults.Scope))
}
if outputFields.Has(globals.AuthorizedActionsField) {
outputOpts = append(outputOpts, handlers.WithAuthorizedActions(authResults.FetchActionSetForId(ctx, mg.GetPublicId(), IdActions[subtypes.SubtypeFromId(domain, mg.GetPublicId())]).Strings()))
outputOpts = append(outputOpts, handlers.WithAuthorizedActions(authResults.FetchActionSetForId(ctx, mg.GetPublicId(), IdActions[globals.ResourceInfoFromPrefix(mg.GetPublicId()).Subtype]).Strings()))
}
if outputFields.Has(globals.MemberIdsField) {
outputOpts = append(outputOpts, handlers.WithMemberIds(memberIds))
@@ -240,7 +240,7 @@ func (s Service) CreateManagedGroup(ctx context.Context, req *pbs.CreateManagedG
outputOpts = append(outputOpts, handlers.WithScope(authResults.Scope))
}
if outputFields.Has(globals.AuthorizedActionsField) {
outputOpts = append(outputOpts, handlers.WithAuthorizedActions(authResults.FetchActionSetForId(ctx, mg.GetPublicId(), IdActions[subtypes.SubtypeFromId(domain, mg.GetPublicId())]).Strings()))
outputOpts = append(outputOpts, handlers.WithAuthorizedActions(authResults.FetchActionSetForId(ctx, mg.GetPublicId(), IdActions[globals.ResourceInfoFromPrefix(mg.GetPublicId()).Subtype]).Strings()))
}
item, err := toProto(ctx, mg, outputOpts...)
@@ -279,7 +279,7 @@ func (s Service) UpdateManagedGroup(ctx context.Context, req *pbs.UpdateManagedG
outputOpts = append(outputOpts, handlers.WithScope(authResults.Scope))
}
if outputFields.Has(globals.AuthorizedActionsField) {
outputOpts = append(outputOpts, handlers.WithAuthorizedActions(authResults.FetchActionSetForId(ctx, mg.GetPublicId(), IdActions[subtypes.SubtypeFromId(domain, mg.GetPublicId())]).Strings()))
outputOpts = append(outputOpts, handlers.WithAuthorizedActions(authResults.FetchActionSetForId(ctx, mg.GetPublicId(), IdActions[globals.ResourceInfoFromPrefix(mg.GetPublicId()).Subtype]).Strings()))
}
item, err := toProto(ctx, mg, outputOpts...)
@@ -309,7 +309,7 @@ func (s Service) DeleteManagedGroup(ctx context.Context, req *pbs.DeleteManagedG
func (s Service) getFromRepo(ctx context.Context, id string) (auth.ManagedGroup, []string, error) {
var out auth.ManagedGroup
var memberIds []string
switch subtypes.SubtypeFromId(domain, id) {
switch globals.ResourceInfoFromPrefix(id).Subtype {
case oidc.Subtype:
repo, err := s.oidcRepoFn()
if err != nil {
@@ -432,7 +432,7 @@ func (s Service) createInRepo(ctx context.Context, am auth.AuthMethod, item *pb.
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing item")
}
var out auth.ManagedGroup
switch subtypes.SubtypeFromId(domain, am.GetPublicId()) {
switch globals.ResourceInfoFromPrefix(am.GetPublicId()).Subtype {
case oidc.Subtype:
am, err := s.createOidcInRepo(ctx, am, item)
if err != nil {
@@ -534,7 +534,7 @@ func (s Service) updateLdapInRepo(ctx context.Context, scopeId, amId, id string,
func (s Service) updateInRepo(ctx context.Context, scopeId, authMethodId string, req *pbs.UpdateManagedGroupRequest) (auth.ManagedGroup, error) {
const op = "managed_groups.(Service).updateInRepo"
var out auth.ManagedGroup
switch subtypes.SubtypeFromId(domain, req.GetId()) {
switch globals.ResourceInfoFromPrefix(req.GetId()).Subtype {
case oidc.Subtype:
mg, err := s.updateOidcInRepo(ctx, scopeId, authMethodId, req.GetId(), req.GetUpdateMask().GetPaths(), req.GetItem())
if err != nil {
@@ -561,7 +561,7 @@ func (s Service) deleteFromRepo(ctx context.Context, scopeId, id string) (bool,
const op = "managed_groups.(Service).deleteFromRepo"
var rows int
var err error
switch subtypes.SubtypeFromId(domain, id) {
switch globals.ResourceInfoFromPrefix(id).Subtype {
case oidc.Subtype:
repo, iErr := s.oidcRepoFn()
if iErr != nil {
@@ -588,7 +588,7 @@ func (s Service) listFromRepo(ctx context.Context, authMethodId string) ([]auth.
const op = "managed_groups.(Service).listFromRepo"
var outUl []auth.ManagedGroup
switch subtypes.SubtypeFromId(domain, authMethodId) {
switch globals.ResourceInfoFromPrefix(authMethodId).Subtype {
case oidc.Subtype:
oidcRepo, err := s.oidcRepoFn()
if err != nil {
@@ -637,7 +637,7 @@ func (s Service) parentAndAuthResult(ctx context.Context, id string, a action.Ty
case action.List, action.Create:
parentId = id
default:
switch subtypes.SubtypeFromId(domain, id) {
switch globals.ResourceInfoFromPrefix(id).Subtype {
case oidc.Subtype:
grp, err := oidcRepo.LookupManagedGroup(ctx, id)
if err != nil {
@@ -668,7 +668,7 @@ func (s Service) parentAndAuthResult(ctx context.Context, id string, a action.Ty
}
var authMeth auth.AuthMethod
switch subtypes.SubtypeFromId(domain, parentId) {
switch globals.ResourceInfoFromPrefix(parentId).Subtype {
case oidc.Subtype:
am, err := oidcRepo.LookupAuthMethod(ctx, parentId)
if err != nil {
@@ -799,7 +799,7 @@ func validateCreateRequest(ctx context.Context, req *pbs.CreateManagedGroupReque
if req.GetItem().GetAuthMethodId() == "" {
badFields[globals.AuthMethodIdField] = "This field is required."
}
switch subtypes.SubtypeFromId(domain, req.GetItem().GetAuthMethodId()) {
switch globals.ResourceInfoFromPrefix(req.GetItem().GetAuthMethodId()).Subtype {
case oidc.Subtype:
if req.GetItem().GetType() != "" && req.GetItem().GetType() != oidc.Subtype.String() {
badFields[globals.TypeField] = "Doesn't match the parent resource's type."
@@ -842,7 +842,7 @@ func validateUpdateRequest(ctx context.Context, req *pbs.UpdateManagedGroupReque
}
return handlers.ValidateUpdateRequest(req, req.GetItem(), func() map[string]string {
badFields := map[string]string{}
switch subtypes.SubtypeFromId(domain, req.GetId()) {
switch globals.ResourceInfoFromPrefix(req.GetId()).Subtype {
case oidc.Subtype:
if req.GetItem().GetType() != "" && req.GetItem().GetType() != oidc.Subtype.String() {
badFields[globals.TypeField] = "Cannot modify the resource type."