-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor logic for getting current subject and checking permissions Some functions, like currentSubject and checkAction, have logic that is either duplicated or could be reused elsewhere for things like checking access to actions in permissions-api itself. This commit moves some of that logic around so it is easier for other handlers (such as the handlers for roles) to use. Signed-off-by: John Schaeffer <[email protected]> * Add permissions checks for role creation This commit adds permissions checks for role creation, as well as the action and bindings to the example policy. Signed-off-by: John Schaeffer <[email protected]> * Add create-role command to bootstrap permissions-api This commit adds a command to create roles directly in SpiceDB, bypassing permissions checks. The intent of this command is to bootstrap a new permissions-api deployment with enough access to start provisioning roles using some subject. Signed-off-by: John Schaeffer <[email protected]> * Add permissions checks for other role operations This commit adds permissions checks for getting, listing, updating, and deleting roles. Signed-off-by: John Schaeffer <[email protected]> * Fix linting whitespace issue in checkActionWithResponse Signed-off-by: John Schaeffer <[email protected]> * Check role permissions based on the resource, not the role One of the quirks of our current role model is that roles don't belong to a resource outright - instead, their binding to a resource is inferred by the actions that can be performed. This means that we can't use the role itself to make authorization decisions. This commit updates permissions checks for roles to use the role's resource rather than the role itself for checking permissions. Signed-off-by: John Schaeffer <[email protected]> --------- Signed-off-by: John Schaeffer <[email protected]>
- Loading branch information
1 parent
dce3403
commit 5a66391
Showing
6 changed files
with
274 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"go.infratographer.com/permissions-api/internal/config" | ||
"go.infratographer.com/permissions-api/internal/iapl" | ||
"go.infratographer.com/permissions-api/internal/query" | ||
"go.infratographer.com/permissions-api/internal/spicedbx" | ||
"go.infratographer.com/x/gidx" | ||
"go.infratographer.com/x/viperx" | ||
) | ||
|
||
const ( | ||
createRoleFlagSubject = "subject" | ||
createRoleFlagResource = "resource" | ||
createRoleFlagActions = "actions" | ||
) | ||
|
||
var ( | ||
createRoleCmd = &cobra.Command{ | ||
Use: "create-role", | ||
Short: "create role in SpiceDB directly", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
createRole(cmd.Context(), globalCfg) | ||
}, | ||
} | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(createRoleCmd) | ||
|
||
flags := createRoleCmd.Flags() | ||
flags.String(createRoleFlagSubject, "", "subject to assign to created role") | ||
flags.StringSlice(createRoleFlagActions, []string{}, "actions to assign to created role") | ||
flags.String(createRoleFlagResource, "", "resource to bind to created 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)) | ||
} | ||
|
||
func createRole(ctx context.Context, cfg *config.AppConfig) { | ||
subjectIDStr := viper.GetString(createRoleFlagSubject) | ||
actions := viper.GetStringSlice(createRoleFlagActions) | ||
resourceIDStr := viper.GetString(createRoleFlagResource) | ||
|
||
if subjectIDStr == "" || len(actions) == 0 || resourceIDStr == "" { | ||
logger.Fatal("invalid config") | ||
} | ||
|
||
spiceClient, err := spicedbx.NewClient(cfg.SpiceDB, cfg.Tracing.Enabled) | ||
if err != nil { | ||
logger.Fatalw("unable to initialize spicedb client", "error", err) | ||
} | ||
|
||
var policy iapl.Policy | ||
|
||
if cfg.SpiceDB.PolicyFile != "" { | ||
policy, err = iapl.NewPolicyFromFile(cfg.SpiceDB.PolicyFile) | ||
if err != nil { | ||
logger.Fatalw("unable to load new policy from schema file", "policy_file", cfg.SpiceDB.PolicyFile, "error", err) | ||
} | ||
} else { | ||
logger.Warn("no spicedb policy file defined, using default policy") | ||
|
||
policy = iapl.DefaultPolicy() | ||
} | ||
|
||
if err = policy.Validate(); err != nil { | ||
logger.Fatalw("invalid spicedb policy", "error", err) | ||
} | ||
|
||
resourceID, err := gidx.Parse(resourceIDStr) | ||
if err != nil { | ||
logger.Fatalw("error parsing resource ID", "error", err) | ||
} | ||
|
||
subjectID, err := gidx.Parse(subjectIDStr) | ||
if err != nil { | ||
logger.Fatalw("error parsing subject ID", "error", err) | ||
} | ||
|
||
engine := query.NewEngine("infratographer", spiceClient, query.WithPolicy(policy), query.WithLogger(logger)) | ||
|
||
resource, err := engine.NewResourceFromID(resourceID) | ||
if err != nil { | ||
logger.Fatalw("error creating resource", "error", err) | ||
} | ||
|
||
subjectResource, err := engine.NewResourceFromID(subjectID) | ||
if err != nil { | ||
logger.Fatalw("error creating subject resource", "error", err) | ||
} | ||
|
||
role, _, err := engine.CreateRole(ctx, resource, actions) | ||
if err != nil { | ||
logger.Fatalw("error creating role", "error", err) | ||
} | ||
|
||
_, err = engine.AssignSubjectRole(ctx, subjectResource, role) | ||
if err != nil { | ||
logger.Fatalw("error creating role", "error", err) | ||
} | ||
|
||
logger.Infow("role successfully created", "role_id", role.ID) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
oidc: | ||
issuer: http://mock-oauth2-server:8081/default | ||
|
||
spicedb: | ||
policyFile: /workspace/policy.example.yaml |
Oops, something went wrong.