snapshot of https://github.com/ory/keto.git (master, 2026-09-16; blobs > 131072B omitted)
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

This commit is contained in:
upstream-mirror
2026-09-16 01:08:34 +05:00
commit 9456a4e922
2560 changed files with 120911 additions and 0 deletions

87
cmd/expand/root.go Normal file
View File

@@ -0,0 +1,87 @@
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package expand
import (
"fmt"
"github.com/ory/x/cmdx"
"github.com/spf13/cobra"
"github.com/ory/keto/cmd/client"
"github.com/ory/keto/cmd/helpers"
rts "github.com/ory/keto/gen/go/ory/keto/relation_tuples/v1alpha2"
"github.com/ory/keto/ketoapi"
)
const FlagMaxDepth = "max-depth"
func NewExpandCmd() *cobra.Command {
var maxDepth int32
cmd := &cobra.Command{
Use: "expand <relation> <object_namespace>:<object_id>",
Short: "Expand all subjects that have the relation on the object",
Long: "Expand all subjects that have the relation on the object, including how access is granted.\n\n" +
"Example:\n" +
" keto expand view Doc:readme\n\n",
Args: cobra.RangeArgs(2, 3),
RunE: func(cmd *cobra.Command, args []string) error {
namespace, object, err := helpers.ParseNamespaceObject(cmd, args[1:])
if err != nil {
return err
}
conn, err := client.GetReadConn(cmd)
if err != nil {
return err
}
defer func() { _ = conn.Close() }()
cl := rts.NewExpandServiceClient(conn)
resp, err := cl.Expand(cmd.Context(), &rts.ExpandRequest{
Subject: rts.NewSubjectSet(namespace, object, args[0]),
MaxDepth: maxDepth,
})
if err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not make request: %s\n", err)
return cmdx.FailSilently(cmd)
}
var tree *ketoapi.Tree[*ketoapi.RelationTuple]
if resp.Tree != nil {
tree = ketoapi.TreeFromProto[*ketoapi.RelationTuple](resp.Tree)
}
cmdx.PrintJSONAble(cmd, tree)
format, err := cmd.Flags().GetString(cmdx.FlagFormat)
if err != nil {
return err
}
quiet, err := cmd.Flags().GetBool(cmdx.FlagQuiet)
if err != nil {
return err
}
switch format {
case string(cmdx.FormatDefault), "":
if tree == nil && !quiet {
_, _ = fmt.Fprint(cmd.OutOrStdout(), "Got an empty tree. This probably means that the requested relation tuple is not present in Keto.")
}
_, _ = fmt.Fprintln(cmd.OutOrStdout())
}
return nil
},
}
client.RegisterRemoteURLFlags(cmd.Flags())
cmdx.RegisterJSONFormatFlags(cmd.Flags())
cmdx.RegisterNoiseFlags(cmd.Flags())
cmd.Flags().Int32VarP(&maxDepth, FlagMaxDepth, "d", 0, "Maximum depth of the tree to be returned. 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 RegisterCommandsRecursive(parent *cobra.Command) {
parent.AddCommand(NewExpandCmd())
}

76
cmd/expand/root_test.go Normal file
View File

@@ -0,0 +1,76 @@
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package expand
import (
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ory/x/cmdx"
"github.com/ory/keto/cmd/client/clienttest"
"github.com/ory/keto/cmd/relationtuple"
"github.com/ory/keto/internal/namespace"
)
func TestExpandCommand(t *testing.T) {
nspace := &namespace.Namespace{Name: t.Name()}
nspaceUser := &namespace.Namespace{Name: "User"}
newCmd := func() *cobra.Command {
cmd := &cobra.Command{
Use: "keto",
}
RegisterCommandsRecursive(cmd)
relationtuple.RegisterCommandsRecursive(cmd)
return cmd
}
ts := clienttest.NewTestServer(t, []*namespace.Namespace{nspace, nspaceUser}, newCmd)
tuple := clienttest.RandomTupleWithSubjectSet(nspace.Name, nspaceUser.Name)
nsObj := tuple.Namespace + ":" + tuple.Object
rel := tuple.Relation
subjectSet := tuple.SubjectSet.String()
// create tuple with subjectSet
ts.Cmd.ExecNoErr(t, "relation-tuple", "create", subjectSet, rel, nsObj)
t.Run("case=expands existing tuple as JSON", func(t *testing.T) {
stdOut := ts.Cmd.ExecNoErr(t, "expand", rel, nsObj,
"--"+cmdx.FlagFormat, string(cmdx.FormatJSON))
assert.Contains(t, stdOut, tuple.SubjectSet.Object)
})
t.Run("case=unknown tuple returns single node tree", func(t *testing.T) {
stdOut := ts.Cmd.ExecNoErr(t, "expand", rel, nspace.Name+":unknown-obj",
"--"+cmdx.FlagFormat, string(cmdx.FormatJSON))
assert.Contains(t, stdOut, rel)
assert.Contains(t, stdOut, nspace.Name)
})
t.Run("case=unknown tuple prints empty tree in default format", func(t *testing.T) {
stdOut := ts.Cmd.ExecNoErr(t, "expand", rel, nspace.Name+":unknown-obj")
assert.Contains(t, stdOut, rel)
assert.Contains(t, stdOut, nspace.Name)
})
t.Run("case=3-arg format still works with deprecation warning", func(t *testing.T) {
stdOut, stdErr, err := ts.Cmd.Exec(nil, "expand", rel, nspace.Name, tuple.Object,
"--"+cmdx.FlagFormat, string(cmdx.FormatJSON))
require.NoError(t, err)
assert.Contains(t, stdOut, tuple.SubjectSet.Object)
require.Contains(t, stdErr, "deprecated")
})
t.Run("case=errors on invalid namespace:object format", func(t *testing.T) {
_, stdErr, err := ts.Cmd.Exec(nil, "expand", rel, "no-colon-here")
require.Error(t, err)
require.Contains(t, stdErr, "expected <object_namespace>:<object_id> format")
})
}