Skip to content

Commit

Permalink
update query pkg to support role-binding V2, add v2 APIs
Browse files Browse the repository at this point in the history
Signed-off-by: Bailin He <[email protected]>
  • Loading branch information
bailinhe committed Apr 22, 2024
1 parent ca13cbc commit f84e342
Show file tree
Hide file tree
Showing 10 changed files with 2,400 additions and 20 deletions.
58 changes: 41 additions & 17 deletions cmd/createrole.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ import (
"go.infratographer.com/permissions-api/internal/query"
"go.infratographer.com/permissions-api/internal/spicedbx"
"go.infratographer.com/permissions-api/internal/storage"
"go.infratographer.com/permissions-api/internal/types"
)

const (
createRoleFlagSubject = "subject"
createRoleFlagResource = "resource"
createRoleFlagActions = "actions"
createRoleFlagName = "name"
createRoleFlagIsV2 = "v2"
)

var (
createRoleCmd = &cobra.Command{
Use: "create-role",
Short: "create role in SpiceDB directly",
Run: func(cmd *cobra.Command, _ []string) {
createRole(cmd.Context(), globalCfg)
},
}
)
var createRoleCmd = &cobra.Command{
Use: "create-role",
Short: "create role in SpiceDB directly",
Run: func(cmd *cobra.Command, _ []string) {
createRole(cmd.Context(), globalCfg)
},
}

func init() {
rootCmd.AddCommand(createRoleCmd)
Expand All @@ -42,20 +42,23 @@ func init() {
flags.StringSlice(createRoleFlagActions, []string{}, "actions to assign to created role")
flags.String(createRoleFlagResource, "", "resource to bind to created role")
flags.String(createRoleFlagName, "", "name of role to create")
flags.Bool(createRoleFlagIsV2, false, "create a v2 role")

v := viper.GetViper()

viperx.MustBindFlag(v, createRoleFlagSubject, flags.Lookup(createRoleFlagSubject))
viperx.MustBindFlag(v, createRoleFlagActions, flags.Lookup(createRoleFlagActions))
viperx.MustBindFlag(v, createRoleFlagResource, flags.Lookup(createRoleFlagResource))
viperx.MustBindFlag(v, createRoleFlagName, flags.Lookup(createRoleFlagName))
viperx.MustBindFlag(v, createRoleFlagIsV2, flags.Lookup(createRoleFlagIsV2))
}

func createRole(ctx context.Context, cfg *config.AppConfig) {
subjectIDStr := viper.GetString(createRoleFlagSubject)
actions := viper.GetStringSlice(createRoleFlagActions)
resourceIDStr := viper.GetString(createRoleFlagResource)
name := viper.GetString(createRoleFlagName)
v2 := viper.GetBool(createRoleFlagIsV2)

if subjectIDStr == "" || len(actions) == 0 || resourceIDStr == "" || name == "" {
logger.Fatal("invalid config")
Expand Down Expand Up @@ -125,14 +128,35 @@ func createRole(ctx context.Context, cfg *config.AppConfig) {
logger.Fatalw("error creating subject resource", "error", err)
}

role, err := engine.CreateRole(ctx, subjectResource, resource, name, actions)
if err != nil {
logger.Fatalw("error creating role", "error", err)
}
if v2 {
role, err := engine.CreateRoleV2(ctx, subjectResource, resource, name, actions)
if err != nil {
logger.Fatalw("error creating role", "error", err)
}

if err = engine.AssignSubjectRole(ctx, subjectResource, role); err != nil {
logger.Fatalw("error creating role", "error", err)
}
rbsubj := []types.RoleBindingSubject{{SubjectResource: subjectResource}}

roleres, err := engine.NewResourceFromID(role.ID)
if err != nil {
logger.Fatalw("error creating role resource", "error", err)
}

logger.Infow("role successfully created", "role_id", role.ID)
rb, err := engine.CreateRoleBinding(ctx, resource, roleres, rbsubj)
if err != nil {
logger.Fatalw("error creating role binding", "error", err)
}

logger.Infof("created role %s[%s] and role-binding %s", role.Name, role.ID, rb.ID)
} else {
role, err := engine.CreateRole(ctx, subjectResource, resource, name, actions)
if err != nil {
logger.Fatalw("error creating role", "error", err)
}

if err = engine.AssignSubjectRole(ctx, subjectResource, role); err != nil {
logger.Fatalw("error creating role", "error", err)
}

logger.Infow("role successfully created", "role_id", role.ID)
}
}
Loading

0 comments on commit f84e342

Please sign in to comment.