Skip to content

Commit

Permalink
feat: make servicedata key public
Browse files Browse the repository at this point in the history
  • Loading branch information
ishanarya0 committed Jul 4, 2024
1 parent c09e507 commit e60f933
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func BuildAPIDependencies(
serviceDataRepository := postgres.NewServiceDataRepository(dbc)
serviceDataService := servicedata.NewService(logger, serviceDataRepository, resourceService, relationService, projectService, userService, activityService)

relationAdapter := adapter.NewRelation(groupService, userService, relationService)
relationAdapter := adapter.NewRelation(groupService, userService, relationService, roleService)

dependencies := api.Deps{
OrgService: organizationService,
Expand Down
21 changes: 20 additions & 1 deletion internal/adapter/relation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,38 @@ package adapter

import (
"context"
"errors"
"fmt"
"slices"

"github.com/goto/shield/core/group"
"github.com/goto/shield/core/relation"
"github.com/goto/shield/core/role"
"github.com/goto/shield/core/user"
"github.com/goto/shield/internal/schema"
"github.com/goto/shield/pkg/uuid"
)

const WILDCARD = "*"

type Relation struct {
groupService *group.Service
userService *user.Service
relationService *relation.Service
roleService *role.Service
}

func NewRelation(
groupService *group.Service,
userService *user.Service,
relationService *relation.Service,
roleService *role.Service,
) *Relation {
return &Relation{
groupService: groupService,
userService: userService,
relationService: relationService,
roleService: roleService,
}
}

Expand All @@ -36,7 +44,18 @@ func (a Relation) TransformRelation(ctx context.Context, rlt relation.RelationV2
if rel.Subject.Namespace == schema.UserPrincipal || rel.Subject.Namespace == "user" {
userID := rel.Subject.ID

if !uuid.IsValid(userID) {
if userID == WILDCARD {
roleID := rel.Object.NamespaceID + ":" + rel.Subject.RoleID
role, err := a.roleService.Get(ctx, roleID)
if err != nil {
return relation.RelationV2{}, err
}
if !slices.Contains(role.Types, schema.UserPrincipalWildcard) {
return relation.RelationV2{}, errors.New("this does not allow wildcard")
}
}

if !uuid.IsValid(userID) && userID != WILDCARD {
fetchedUser, err := a.userService.GetByEmail(ctx, rel.Subject.ID)
if err != nil {
return relation.RelationV2{}, fmt.Errorf("%w: %s", relation.ErrFetchingUser, err.Error())
Expand Down
7 changes: 4 additions & 3 deletions internal/schema/predefined.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ const (
MembershipPermission = "membership"

// principals
UserPrincipal = "shield/user"
GroupPrincipal = "shield/group"
UserPrincipal = "shield/user"
GroupPrincipal = "shield/group"
UserPrincipalWildcard = "shield/user:*"
)

var InheritedRelations = map[string]bool{
Expand Down Expand Up @@ -131,7 +132,7 @@ var ServiceDataKeyConfig = NamespaceConfig{
},
Roles: map[string][]string{
EditorRole: {UserPrincipal, GroupPrincipal},
ViewerRole: {UserPrincipal, GroupPrincipal},
ViewerRole: {UserPrincipal, GroupPrincipal, UserPrincipalWildcard},
OwnerRole: {UserPrincipal, GroupPrincipal},
},
Permissions: map[string][]string{
Expand Down
3 changes: 2 additions & 1 deletion internal/store/postgres/role_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ func (r RoleRepository) Upsert(ctx context.Context, rl role.Role) (string, error
"metadata": goqu.L("$5"),
}).OnConflict(
goqu.DoUpdate("id", goqu.Record{
"name": goqu.L("$2"),
"types": goqu.L("$3"),
"metadata": goqu.L("$5"),
},
)).Returning("id").ToSQL()
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions internal/store/spicedb/schema_generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ func GenerateSchema(namespaceConfig schema.NamespaceConfigMapType) []string {

func processPrincipal(s string) string {
return map[string]string{
"shield/group": "shield/group#membership",
"shield/user": "shield/user",
"shield/group": "shield/group#membership",
"shield/user": "shield/user",
"shield/user:*": "shield/user:*",
}[s]
}
3 changes: 2 additions & 1 deletion test/integration_test/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/goto/shield/core/project"
"github.com/goto/shield/core/relation"
"github.com/goto/shield/core/resource"
"github.com/goto/shield/core/role"
"github.com/goto/shield/core/rule"
"github.com/goto/shield/core/user"
"github.com/goto/shield/internal/adapter"
Expand Down Expand Up @@ -309,7 +310,7 @@ func buildPipeline(logger log.Logger, proxy http.Handler, ruleService *rule.Serv

func hookPipeline(log log.Logger) hook.Service {
rootHook := hook.New()
relationAdapter := adapter.NewRelation(&group.Service{}, &user.Service{}, &relation.Service{})
relationAdapter := adapter.NewRelation(&group.Service{}, &user.Service{}, &relation.Service{}, &role.Service{})
return authz_hook.New(log, rootHook, rootHook, &resource.Service{}, &relation.Service{}, relationAdapter, "X-Auth-Email")
}

Expand Down

0 comments on commit e60f933

Please sign in to comment.