Subtype Id registration moved to its own package (#1416)

* Allow repo packages to register subtypes and id prefixes. 
* Refactor out request authentication logic to a new package as a sibling to handler.
This commit is contained in:
Todd Knight authored and GitHub committed 2021-07-27 09:59:52 -07:00
1 parent ab6f3eaeb4
commit 10e8aeae4b
62 files changed
+603 -597

No files matched your search

@@ -14,10 +14,12 @@ import (
"github.com/hashicorp/boundary/internal/intglobals"
"github.com/hashicorp/boundary/internal/perms"
"github.com/hashicorp/boundary/internal/requests"
requestauth "github.com/hashicorp/boundary/internal/servers/controller/auth"
"github.com/hashicorp/boundary/internal/servers/controller/common"
"github.com/hashicorp/boundary/internal/servers/controller/handlers"
"github.com/hashicorp/boundary/internal/types/action"
"github.com/hashicorp/boundary/internal/types/resource"
"github.com/hashicorp/boundary/internal/types/subtypes"
"github.com/hashicorp/go-bexpr"
"google.golang.org/grpc/codes"
"google.golang.org/protobuf/types/known/wrapperspb"
@@ -33,8 +35,8 @@ var (
// IdActions contains the set of actions that can be performed on
// individual resources
IdActions = map[auth.Subtype]action.ActionSet{
auth.OidcSubtype: {
IdActions = map[subtypes.Subtype]action.ActionSet{
oidc.Subtype: {
action.NoOp,
action.Read,
action.Update,
@@ -105,7 +107,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[auth.SubtypeFromId(mg.GetPublicId())], auth.WithResource(&res)).Strings()
authorizedActions := authResults.FetchActionSetForId(ctx, mg.GetPublicId(), IdActions[auth.SubtypeFromId(mg.GetPublicId())], requestauth.WithResource(&res)).Strings()
if len(authorizedActions) == 0 {
continue
}
@@ -274,7 +276,7 @@ func (s Service) getFromRepo(ctx context.Context, id string) (auth.ManagedGroup,
var out auth.ManagedGroup
var memberIds []string
switch auth.SubtypeFromId(id) {
case auth.OidcSubtype:
case oidc.Subtype:
repo, err := s.oidcRepoFn()
if err != nil {
return nil, nil, err
@@ -346,7 +348,7 @@ func (s Service) createInRepo(ctx context.Context, am auth.AuthMethod, item *pb.
}
var out auth.ManagedGroup
switch auth.SubtypeFromId(am.GetPublicId()) {
case auth.OidcSubtype:
case oidc.Subtype:
am, err := s.createOidcInRepo(ctx, am, item)
if err != nil {
return nil, errors.Wrap(err, op)
@@ -406,7 +408,7 @@ func (s Service) updateInRepo(ctx context.Context, scopeId, authMethodId string,
const op = "managed_groups.(Service).updateInRepo"
var out auth.ManagedGroup
switch auth.SubtypeFromId(req.GetId()) {
case auth.OidcSubtype:
case oidc.Subtype:
mg, err := s.updateOidcInRepo(ctx, scopeId, authMethodId, req.GetId(), req.GetUpdateMask().GetPaths(), req.GetItem())
if err != nil {
return nil, errors.Wrap(err, op)
@@ -424,7 +426,7 @@ func (s Service) deleteFromRepo(ctx context.Context, scopeId, id string) (bool,
var rows int
var err error
switch auth.SubtypeFromId(id) {
case auth.OidcSubtype:
case oidc.Subtype:
repo, iErr := s.oidcRepoFn()
if iErr != nil {
return false, iErr
@@ -445,7 +447,7 @@ func (s Service) listFromRepo(ctx context.Context, authMethodId string) ([]auth.
var outUl []auth.ManagedGroup
switch auth.SubtypeFromId(authMethodId) {
case auth.OidcSubtype:
case oidc.Subtype:
oidcRepo, err := s.oidcRepoFn()
if err != nil {
return nil, errors.Wrap(err, op)
@@ -461,8 +463,9 @@ func (s Service) listFromRepo(ctx context.Context, authMethodId string) ([]auth.
return outUl, nil
}
func (s Service) parentAndAuthResult(ctx context.Context, id string, a action.Type) (auth.AuthMethod, auth.VerifyResults) {
res := auth.VerifyResults{}
func (s Service) parentAndAuthResult(ctx context.Context, id string, a action.Type) (auth.AuthMethod, requestauth.VerifyResults) {
const op = "managed_groups.(Service)."
res := requestauth.VerifyResults{}
oidcRepo, err := s.oidcRepoFn()
if err != nil {
res.Error = err
@@ -470,13 +473,13 @@ func (s Service) parentAndAuthResult(ctx context.Context, id string, a action.Ty
}
var parentId string
opts := []auth.Option{auth.WithType(resource.ManagedGroup), auth.WithAction(a)}
opts := []requestauth.Option{requestauth.WithType(resource.ManagedGroup), requestauth.WithAction(a)}
switch a {
case action.List, action.Create:
parentId = id
default:
switch auth.SubtypeFromId(id) {
case auth.OidcSubtype:
case oidc.Subtype:
acct, err := oidcRepo.LookupManagedGroup(ctx, id)
if err != nil {
res.Error = err
@@ -487,13 +490,16 @@ func (s Service) parentAndAuthResult(ctx context.Context, id string, a action.Ty
return nil, res
}
parentId = acct.GetAuthMethodId()
default:
res.Error = errors.New(errors.InvalidPublicId, op, "unrecognized managed group subtype")
return nil, res
}
opts = append(opts, auth.WithId(id))
opts = append(opts, requestauth.WithId(id))
}
var authMeth auth.AuthMethod
switch auth.SubtypeFromId(parentId) {
case auth.OidcSubtype:
case oidc.Subtype:
am, err := oidcRepo.LookupAuthMethod(ctx, parentId)
if err != nil {
res.Error = err
@@ -504,9 +510,13 @@ func (s Service) parentAndAuthResult(ctx context.Context, id string, a action.Ty
return nil, res
}
authMeth = am
opts = append(opts, requestauth.WithScopeId(am.GetScopeId()))
default:
res.Error = errors.New(errors.InvalidPublicId, op, "unrecognized auth method subtype")
return nil, res
}
opts = append(opts, auth.WithScopeId(authMeth.GetScopeId()), auth.WithPin(parentId))
return authMeth, auth.Verify(ctx, opts...)
opts = append(opts, requestauth.WithPin(parentId))
return authMeth, requestauth.Verify(ctx, opts...)
}
func toProto(ctx context.Context, in auth.ManagedGroup, opt ...handlers.Option) (*pb.ManagedGroup, error) {
@@ -550,7 +560,7 @@ func toProto(ctx context.Context, in auth.ManagedGroup, opt ...handlers.Option)
switch i := in.(type) {
case *oidc.ManagedGroup:
if outputFields.Has(globals.TypeField) {
out.Type = auth.OidcSubtype.String()
out.Type = oidc.Subtype.String()
}
if !outputFields.Has(globals.AttributesField) {
break
@@ -591,8 +601,8 @@ func validateCreateRequest(req *pbs.CreateManagedGroupRequest) error {
badFields[globals.AuthMethodIdField] = "This field is required."
}
switch auth.SubtypeFromId(req.GetItem().GetAuthMethodId()) {
case auth.OidcSubtype:
if req.GetItem().GetType() != "" && req.GetItem().GetType() != auth.OidcSubtype.String() {
case oidc.Subtype:
if req.GetItem().GetType() != "" && req.GetItem().GetType() != oidc.Subtype.String() {
badFields[globals.TypeField] = "Doesn't match the parent resource's type."
}
attrs := &pb.OidcManagedGroupAttributes{}
@@ -621,8 +631,8 @@ func validateUpdateRequest(req *pbs.UpdateManagedGroupRequest) error {
return handlers.ValidateUpdateRequest(req, req.GetItem(), func() map[string]string {
badFields := map[string]string{}
switch auth.SubtypeFromId(req.GetId()) {
case auth.OidcSubtype:
if req.GetItem().GetType() != "" && req.GetItem().GetType() != auth.OidcSubtype.String() {
case oidc.Subtype:
if req.GetItem().GetType() != "" && req.GetItem().GetType() != oidc.Subtype.String() {
badFields[globals.TypeField] = "Cannot modify the resource type."
}
attrs := &pb.OidcManagedGroupAttributes{}
@@ -638,6 +648,8 @@ func validateUpdateRequest(req *pbs.UpdateManagedGroupRequest) error {
}
}
}
default:
badFields[globals.IdField] = "Unrecognized resource type."
}
return badFields
}, intglobals.OidcManagedGroupPrefix)
@@ -11,8 +11,8 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/boundary/globals"
"github.com/hashicorp/boundary/internal/auth"
"github.com/hashicorp/boundary/internal/auth/oidc"
"github.com/hashicorp/boundary/internal/auth/password"
"github.com/hashicorp/boundary/internal/authtoken"
"github.com/hashicorp/boundary/internal/db"
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/managedgroups"
@@ -21,6 +21,7 @@ import (
"github.com/hashicorp/boundary/internal/iam"
"github.com/hashicorp/boundary/internal/intglobals"
"github.com/hashicorp/boundary/internal/kms"
"github.com/hashicorp/boundary/internal/servers/controller/auth"
"github.com/hashicorp/boundary/internal/servers/controller/common"
"github.com/hashicorp/boundary/internal/servers/controller/handlers"
"github.com/hashicorp/boundary/internal/servers/controller/handlers/managed_groups"
@@ -213,7 +214,7 @@ func TestListOidc(t *testing.T) {
UpdatedTime: mg.GetUpdateTime().GetTimestamp(),
Scope: &scopepb.ScopeInfo{Id: o.GetPublicId(), Type: scope.Org.String(), ParentScopeId: scope.Global.String()},
Version: 1,
Type: auth.OidcSubtype.String(),
Type: oidc.Subtype.String(),
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{
"filter": structpb.NewStringValue(oidc.TestFakeManagedGroupFilter),
}},
@@ -232,7 +233,7 @@ func TestListOidc(t *testing.T) {
UpdatedTime: mg.GetUpdateTime().GetTimestamp(),
Scope: &scopepb.ScopeInfo{Id: o.GetPublicId(), Type: scope.Org.String(), ParentScopeId: scope.Global.String()},
Version: 1,
Type: auth.OidcSubtype.String(),
Type: oidc.Subtype.String(),
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{
"filter": structpb.NewStringValue(oidc.TestFakeManagedGroupFilter),
}},
@@ -483,7 +484,7 @@ func TestCreateOidc(t *testing.T) {
AuthMethodId: am.GetPublicId(),
Name: &wrapperspb.StringValue{Value: "name"},
Description: &wrapperspb.StringValue{Value: "desc"},
Type: auth.OidcSubtype.String(),
Type: oidc.Subtype.String(),
Attributes: createAttr(),
},
},
@@ -495,7 +496,7 @@ func TestCreateOidc(t *testing.T) {
Description: &wrapperspb.StringValue{Value: "desc"},
Scope: &scopepb.ScopeInfo{Id: o.GetPublicId(), Type: scope.Org.String(), ParentScopeId: scope.Global.String()},
Version: 1,
Type: auth.OidcSubtype.String(),
Type: oidc.Subtype.String(),
Attributes: func() *structpb.Struct {
a := createAttr()
a.Fields["filter"] = structpb.NewStringValue(oidc.TestFakeManagedGroupFilter)
@@ -519,7 +520,7 @@ func TestCreateOidc(t *testing.T) {
AuthMethodId: am.GetPublicId(),
Scope: &scopepb.ScopeInfo{Id: o.GetPublicId(), Type: scope.Org.String(), ParentScopeId: scope.Global.String()},
Version: 1,
Type: auth.OidcSubtype.String(),
Type: oidc.Subtype.String(),
Attributes: func() *structpb.Struct {
a := createAttr()
a.Fields["filter"] = structpb.NewStringValue(oidc.TestFakeManagedGroupFilter)
@@ -534,7 +535,7 @@ func TestCreateOidc(t *testing.T) {
req: &pbs.CreateManagedGroupRequest{
Item: &pb.ManagedGroup{
AuthMethodId: am.GetPublicId(),
Type: auth.PasswordSubtype.String(),
Type: password.Subtype.String(),
Attributes: createAttr(),
},
},
@@ -547,7 +548,7 @@ func TestCreateOidc(t *testing.T) {
Item: &pb.ManagedGroup{
AuthMethodId: am.GetPublicId(),
Id: intglobals.OidcManagedGroupPrefix + "_notallowed",
Type: auth.OidcSubtype.String(),
Type: oidc.Subtype.String(),
Attributes: createAttr(),
},
},
@@ -560,7 +561,7 @@ func TestCreateOidc(t *testing.T) {
Item: &pb.ManagedGroup{
AuthMethodId: am.GetPublicId(),
CreatedTime: timestamppb.Now(),
Type: auth.OidcSubtype.String(),
Type: oidc.Subtype.String(),
Attributes: createAttr(),
},
},
@@ -573,7 +574,7 @@ func TestCreateOidc(t *testing.T) {
Item: &pb.ManagedGroup{
AuthMethodId: am.GetPublicId(),
UpdatedTime: timestamppb.Now(),
Type: auth.OidcSubtype.String(),
Type: oidc.Subtype.String(),
Attributes: createAttr(),
},
},
@@ -586,7 +587,7 @@ func TestCreateOidc(t *testing.T) {
Item: &pb.ManagedGroup{
AuthMethodId: am.GetPublicId(),
UpdatedTime: timestamppb.Now(),
Type: auth.OidcSubtype.String(),
Type: oidc.Subtype.String(),
Attributes: func() *structpb.Struct {
a := createAttr()
a.Fields["filter"] = structpb.NewStringValue("foobar")
@@ -691,7 +692,7 @@ func TestUpdateOidc(t *testing.T) {
Item: &pb.ManagedGroup{
Name: &wrapperspb.StringValue{Value: "new"},
Description: &wrapperspb.StringValue{Value: "desc"},
Type: auth.OidcSubtype.String(),
Type: oidc.Subtype.String(),
},
},
res: &pbs.UpdateManagedGroupResponse{
@@ -699,7 +700,7 @@ func TestUpdateOidc(t *testing.T) {
AuthMethodId: am.GetPublicId(),
Name: &wrapperspb.StringValue{Value: "new"},
Description: &wrapperspb.StringValue{Value: "desc"},
Type: auth.OidcSubtype.String(),
Type: oidc.Subtype.String(),
Attributes: defaultAttributes,
Scope: defaultScopeInfo,
AuthorizedActions: oidcAuthorizedActions,
@@ -715,7 +716,7 @@ func TestUpdateOidc(t *testing.T) {
Item: &pb.ManagedGroup{
Name: &wrapperspb.StringValue{Value: "new"},
Description: &wrapperspb.StringValue{Value: "desc"},
Type: auth.OidcSubtype.String(),
Type: oidc.Subtype.String(),
},
},
res: &pbs.UpdateManagedGroupResponse{
@@ -723,7 +724,7 @@ func TestUpdateOidc(t *testing.T) {
AuthMethodId: am.GetPublicId(),
Name: &wrapperspb.StringValue{Value: "new"},
Description: &wrapperspb.StringValue{Value: "desc"},
Type: auth.OidcSubtype.String(),
Type: oidc.Subtype.String(),
Attributes: defaultAttributes,
Scope: defaultScopeInfo,
AuthorizedActions: oidcAuthorizedActions,
@@ -793,7 +794,7 @@ func TestUpdateOidc(t *testing.T) {
Item: &pb.ManagedGroup{
AuthMethodId: am.GetPublicId(),
Description: &wrapperspb.StringValue{Value: "default"},
Type: auth.OidcSubtype.String(),
Type: oidc.Subtype.String(),
Attributes: defaultAttributes,
Scope: defaultScopeInfo,
AuthorizedActions: oidcAuthorizedActions,
@@ -817,7 +818,7 @@ func TestUpdateOidc(t *testing.T) {
AuthMethodId: am.GetPublicId(),
Name: &wrapperspb.StringValue{Value: "updated"},
Description: &wrapperspb.StringValue{Value: "default"},
Type: auth.OidcSubtype.String(),
Type: oidc.Subtype.String(),
Attributes: defaultAttributes,
Scope: defaultScopeInfo,
AuthorizedActions: oidcAuthorizedActions,
@@ -841,7 +842,7 @@ func TestUpdateOidc(t *testing.T) {
AuthMethodId: am.GetPublicId(),
Name: &wrapperspb.StringValue{Value: "default"},
Description: &wrapperspb.StringValue{Value: "notignored"},
Type: auth.OidcSubtype.String(),
Type: oidc.Subtype.String(),
Attributes: defaultAttributes,
Scope: defaultScopeInfo,
AuthorizedActions: oidcAuthorizedActions,
@@ -943,7 +944,7 @@ func TestUpdateOidc(t *testing.T) {
AuthMethodId: am.GetPublicId(),
Name: &wrapperspb.StringValue{Value: "default"},
Description: &wrapperspb.StringValue{Value: "default"},
Type: auth.OidcSubtype.String(),
Type: oidc.Subtype.String(),
Attributes: modifiedAttributes,
Scope: defaultScopeInfo,
AuthorizedActions: oidcAuthorizedActions,
@@ -6,8 +6,8 @@ import (
"testing"
"github.com/hashicorp/boundary/globals"
"github.com/hashicorp/boundary/internal/auth"
"github.com/hashicorp/boundary/internal/auth/oidc"
"github.com/hashicorp/boundary/internal/auth/password"
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/managedgroups"
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
"github.com/hashicorp/boundary/internal/intglobals"
@@ -46,7 +46,7 @@ func TestValidateCreateRequest(t *testing.T) {
{
name: "missing oidc attributes",
item: &pb.ManagedGroup{
Type: auth.OidcSubtype.String(),
Type: oidc.Subtype.String(),
AuthMethodId: oidc.AuthMethodPrefix + "_1234567890",
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{}},
},
@@ -55,7 +55,7 @@ func TestValidateCreateRequest(t *testing.T) {
{
name: "bad oidc attributes",
item: &pb.ManagedGroup{
Type: auth.OidcSubtype.String(),
Type: oidc.Subtype.String(),
AuthMethodId: oidc.AuthMethodPrefix + "_1234567890",
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{
"filter": structpb.NewStringValue("foobar"),
@@ -66,7 +66,7 @@ func TestValidateCreateRequest(t *testing.T) {
{
name: "no oidc errors",
item: &pb.ManagedGroup{
Type: auth.OidcSubtype.String(),
Type: oidc.Subtype.String(),
AuthMethodId: oidc.AuthMethodPrefix + "_1234567890",
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{
"filter": structpb.NewStringValue(`"/foo/bar" == "zipzap"`),
@@ -101,7 +101,7 @@ func TestValidateUpdateRequest(t *testing.T) {
req: &pbs.UpdateManagedGroupRequest{
Id: intglobals.OidcManagedGroupPrefix + "_1234567890",
Item: &pb.ManagedGroup{
Type: auth.PasswordSubtype.String(),
Type: password.Subtype.String(),
},
},
errContains: fieldError(globals.TypeField, "Cannot modify the resource type."),