Files
upstream-ory-keto/cmd/check/root.go
upstream-mirror 9456a4e922
Some checks failed
CI / Run lints and checks (push) Failing after 47s
CI / Run tests (push) Failing after 1s
CI / Run race tests (push) Failing after 5s
CI / Build CLI docs (push) Has been skipped
CI / Run buf actions (push) Failing after 5s
CodeQL / Analyze (go) (push) Failing after 3s
CodeQL / Analyze (javascript) (push) Failing after 2s
Docker Image Scanners / scanners (push) Failing after 3s
Format / format (push) Failing after 3s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Draft newsletter (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
snapshot of https://github.com/ory/keto.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:08:34 +05:00

93 lines
2.3 KiB
Go

// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package check
import (
"fmt"
"github.com/spf13/cobra"
"github.com/ory/x/cmdx"
"github.com/ory/keto/cmd/client"
"github.com/ory/keto/cmd/helpers"
rts "github.com/ory/keto/gen/go/ory/keto/relation_tuples/v1alpha2"
)
type checkOutput struct {
Allowed bool `json:"allowed"`
}
func (o *checkOutput) String() string {
if o.Allowed {
return "Allowed\n"
}
return "Denied\n"
}
const FlagMaxDepth = "max-depth"
func NewCheckCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "check <subject_namespace>:<subject_id> <relation> <object_namespace>:<object_id>",
Short: "Check whether a subject has a relation on an object",
Long: "Check whether a subject has a relation on an object.\n\n" +
"Example:\n" +
" keto check User:Alice view Doc:readme\n\n" +
"Legacy format 'keto check <subject_namespace>:<subject_id> <relation> <object_namespace> <object_id>' is deprecated.",
Args: cobra.RangeArgs(3, 4),
RunE: check,
}
client.RegisterRemoteURLFlags(cmd.Flags())
cmdx.RegisterFormatFlags(cmd.Flags())
cmd.Flags().Int32P(FlagMaxDepth, "d", 0, "Maximum depth of the search tree. If the value is less than 1 or greater than the global max-depth then the global max-depth will be used instead.")
return cmd
}
func check(cmd *cobra.Command, args []string) error {
namespace, object, err := helpers.ParseNamespaceObject(cmd, args[2:])
if err != nil {
return err
}
sub, err := helpers.ParseSubject(args[0])
if err != nil {
return fmt.Errorf("could not parse subject %q: %w", args[0], err)
}
conn, err := client.GetReadConn(cmd)
if err != nil {
return err
}
defer func() { _ = conn.Close() }()
maxDepth, err := cmd.Flags().GetInt32(FlagMaxDepth)
if err != nil {
return err
}
cl := rts.NewCheckServiceClient(conn)
resp, err := cl.Check(cmd.Context(), &rts.CheckRequest{
Tuple: &rts.RelationTuple{
Namespace: namespace,
Object: object,
Relation: args[1],
Subject: sub,
},
MaxDepth: maxDepth,
})
if err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not make request: %s\n", err)
return cmdx.FailSilently(cmd)
}
cmdx.PrintJSONAble(cmd, &checkOutput{Allowed: resp.Allowed})
return nil
}
func RegisterCommandsRecursive(parent *cobra.Command) {
parent.AddCommand(NewCheckCmd())
}