Skip to content

Commit

Permalink
Clean up service
Browse files Browse the repository at this point in the history
  • Loading branch information
Uruemu Aganbi authored and cameronperera committed Mar 27, 2024
1 parent 4a0464f commit 8650ced
Showing 1 changed file with 42 additions and 40 deletions.
82 changes: 42 additions & 40 deletions internal/daemon/controller/handlers/apptokens/app_token_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/hashicorp/boundary/internal/perms"
"github.com/hashicorp/boundary/internal/types/action"
"github.com/hashicorp/boundary/internal/types/resource"
apptokens "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/apptokens"
"github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/apptokens"
pb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/apptokens"
"google.golang.org/grpc/codes"
"google.golang.org/protobuf/types/known/wrapperspb"
Expand Down Expand Up @@ -276,14 +276,11 @@ func toProto(ctx context.Context, in apptoken.AppToken, opt ...handlers.Option)
}
}
if outputFields.Has(globals.GrantsField) {
for _, g := range in.GetGrants() {
grant, err := grantToProto(ctx, g, in.GetScopeId(), opt...)
if err != nil {
return nil, err
}
out.Grants = append(out.Grants, grant)

grantspb, err := grantToProto(ctx, in.GetGrants(), in.GetScopeId(), opt...)
if err != nil {
return nil, err
}
out.Grants = grantspb
}
if outputFields.Has(globals.GrantScopeIdField) {
out.GrantScopeId = wrapperspb.String(in.GetGrantScopeId())
Expand All @@ -295,15 +292,14 @@ func toProto(ctx context.Context, in apptoken.AppToken, opt ...handlers.Option)
return &out, nil
}

// TODO: convert all grants and return an array of grants
func grantToProto(ctx context.Context, in *store.AppTokenGrant, scopeId string, opt ...handlers.Option) (*apptokens.Grant, error) {
func grantToProto(ctx context.Context, in []*store.AppTokenGrant, scopeId string, opt ...handlers.Option) ([]*apptokens.Grant, error) {
const op = "apptoken_service.grantToProto"

switch {
case in == nil:
return nil, handlers.ApiErrorWithCodeAndMessage(codes.Internal, "nil grant when building apptoken_grant proto")
return nil, handlers.ApiErrorWithCodeAndMessage(codes.Internal, "empty grants slice when building apptoken_grant proto")
case scopeId == "":
return nil, errors.New(ctx, "scope id is empty when building apptoken_grant proto")
return nil, handlers.ApiErrorWithCodeAndMessage(codes.Internal, "scope id is empty when building apptoken_grant proto")
}

opts := handlers.GetOpts(opt...)
Expand All @@ -312,36 +308,42 @@ func grantToProto(ctx context.Context, in *store.AppTokenGrant, scopeId string,
}
outputFields := *opts.WithOutputFields

out := &apptokens.Grant{}
if outputFields.Has(globals.CanonicalGrantField) {
out.Canonical = in.GetCanonicalGrant()
}
if outputFields.Has(globals.JsonGrantField) {
// TODO: replicate https://github.com/hashicorp/boundary/blob/590228d956fe4099eac1ae43d0d8d0bc3869d009/internal/daemon/controller/handlers/roles/role_service.go#L942-L947
parsed, err := perms.Parse(ctx, scopeId, in.GetRawGrant())
if err != nil {
// This should never happen as we validate on the way in, but let's
// return what we can since we are still returning the raw grant
out = &pb.Grant{
Raw: in.GetRawGrant(),
Canonical: "<parse_error>",
Json: nil,
}
} else {
_, actions := parsed.Actions()
out = &pb.Grant{
Raw: in.GetRawGrant(),
Canonical: in.GetCanonicalGrant(),
Json: &pb.GrantJson{
Ids: parsed.Ids(),
Type: parsed.Type().String(),
Actions: actions,
},
out := []*apptokens.Grant{}
for _, g := range in {
if g == nil {
return nil, handlers.ApiErrorWithCodeAndMessage(codes.Internal, "nil grant when building apptoken_grant proto")
}
grantPb := &apptokens.Grant{}
if outputFields.Has(globals.CanonicalGrantField) {
grantPb.Canonical = g.GetCanonicalGrant()
}
if outputFields.Has(globals.JsonGrantField) {
parsed, err := perms.Parse(ctx, scopeId, g.GetRawGrant())
if err != nil {
// This should never happen as we validate on the way in, but let's
// return what we can since we are still returning the raw grant
grantPb = &pb.Grant{
Raw: g.GetRawGrant(),
Canonical: "<parse_error>",
Json: nil,
}
} else {
_, actions := parsed.Actions()
grantPb = &pb.Grant{
Raw: g.GetRawGrant(),
Canonical: g.GetCanonicalGrant(),
Json: &pb.GrantJson{
Ids: parsed.Ids(),
Type: parsed.Type().String(),
Actions: actions,
},
}
}
}
}
if outputFields.Has(globals.RawGrantField) {
out.Raw = in.GetRawGrant()
if outputFields.Has(globals.RawGrantField) {
grantPb.Raw = g.GetRawGrant()
}
out = append(out, grantPb)
}
return out, nil
}

0 comments on commit 8650ced

Please sign in to comment.