diff --git a/cmd/namespace/validate.go b/cmd/namespace/validate.go index c33be048b..e995c994e 100644 --- a/cmd/namespace/validate.go +++ b/cmd/namespace/validate.go @@ -2,7 +2,6 @@ package namespace import ( "bytes" - "context" "fmt" "io/ioutil" @@ -147,7 +146,7 @@ func validateConfigFile(cmd *cobra.Command, fn string) error { switch t := ns.(type) { case string: logger := logrusx.New("cmd", "0") - cw, err := config.NewNamespaceWatcher(context.Background(), logger, t) + cw, err := config.NewNamespaceWatcher(cmd.Context(), logger, t) if err != nil { _, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Encountered error reading config: %+v\n", err) return cmdx.FailSilently(cmd) @@ -188,7 +187,7 @@ func validateConfigFile(cmd *cobra.Command, fn string) error { } return nil default: - _, _ = fmt.Fprintf(cmd.ErrOrStderr(), "unknown type for key 'namespaces' in config file: %v\n", t) + _, _ = fmt.Fprintf(cmd.ErrOrStderr(), "unknown type %T for key 'namespaces' in config file: %v\n", t, t) return cmdx.FailSilently(cmd) } } else { diff --git a/cmd/namespace/validate_test.go b/cmd/namespace/validate_test.go index 4d21e6d4c..4ec6c8315 100644 --- a/cmd/namespace/validate_test.go +++ b/cmd/namespace/validate_test.go @@ -3,6 +3,7 @@ package namespace import ( "fmt" "io/ioutil" + "path/filepath" "testing" "github.com/ory/x/cmdx" @@ -50,13 +51,13 @@ func TestValidateConfigNamespaces(t *testing.T) { t.Run("case=read valid config with embedded namespaces", func(t *testing.T) { dir := t.TempDir() - fnyaml := dir + "/keto.yaml" + fnyaml := filepath.Join(dir, "keto.yaml") require.NoError(t, ioutil.WriteFile(fnyaml, []byte(configEmbeddedYaml), fileMode)) - fnjson := dir + "/keto.json" + fnjson := filepath.Join(dir, "keto.json") require.NoError(t, ioutil.WriteFile(fnjson, []byte(configEmbeddedJson), fileMode)) - fntoml := dir + "/keto.toml" + fntoml := filepath.Join(dir, "keto.toml") require.NoError(t, ioutil.WriteFile(fntoml, []byte(configEmbeddedToml), fileMode)) stdOut := cmd.ExecNoErr(t, "validate", "-c", fnyaml+","+fnjson+","+fntoml) @@ -66,9 +67,9 @@ func TestValidateConfigNamespaces(t *testing.T) { t.Run("case=supports 3 namespace file formats", func(t *testing.T) { dir := t.TempDir() nsfiles := map[string]string{ - dir + "/ns.yaml": "name: testns0\nid: 0", - dir + "/ns.json": "{\"name\": \"testns0\",\"id\": 0}", - dir + "/ns.toml": "name = \"testns0\"\nid = 0", + filepath.Join(dir, "ns.yaml"): "name: testns0\nid: 0", + filepath.Join(dir, "ns.json"): "{\"name\": \"testns0\",\"id\": 0}", + filepath.Join(dir, "ns.toml"): "name = \"testns0\"\nid = 0", } for fn, contents := range nsfiles { require.NoError(t, ioutil.WriteFile(fn, []byte(contents), fileMode)) @@ -79,7 +80,7 @@ func TestValidateConfigNamespaces(t *testing.T) { }) t.Run("case=unknown namespace format gives error", func(t *testing.T) { - fn := t.TempDir() + "/ns.txt" + fn := filepath.Join(t.TempDir(), "ns.txt") require.NoError(t, ioutil.WriteFile(fn, []byte("name: ns\nid: 0"), fileMode)) stdOut := cmd.ExecExpectedErr(t, "validate", fn) @@ -87,7 +88,7 @@ func TestValidateConfigNamespaces(t *testing.T) { }) t.Run("case=config passed as varg fails", func(t *testing.T) { - fn := t.TempDir() + "/keto.yaml" + fn := filepath.Join(t.TempDir(), "keto.yaml") require.NoError(t, ioutil.WriteFile(fn, []byte(configEmbeddedYaml), fileMode)) // interprets config file as namespace file when `-c` flag is not passed @@ -96,7 +97,7 @@ func TestValidateConfigNamespaces(t *testing.T) { }) t.Run("case=read config with invalid embedded namespace", func(t *testing.T) { - fn := t.TempDir() + "/keto.yaml" + fn := filepath.Join(t.TempDir(), "keto.yaml") require.NoError(t, ioutil.WriteFile(fn, []byte(`{"namespaces":[{"id":"x","name":"x"}]}`), fileMode)) stdOut := cmd.ExecExpectedErr(t, "validate", "--config", fn) @@ -106,9 +107,9 @@ func TestValidateConfigNamespaces(t *testing.T) { t.Run("case=read config with namespace as dir reference", func(t *testing.T) { nsdir := t.TempDir() - fn := t.TempDir() + "/config.yaml" - require.NoError(t, ioutil.WriteFile(nsdir+"/ns0.yaml", []byte(nsyaml), fileMode)) - require.NoError(t, ioutil.WriteFile(nsdir+"/ns1.json", []byte(nsjson), fileMode)) + fn := filepath.Join(t.TempDir(), "config.yaml") + require.NoError(t, ioutil.WriteFile(filepath.Join(nsdir, "ns0.yaml"), []byte(nsyaml), fileMode)) + require.NoError(t, ioutil.WriteFile(filepath.Join(nsdir, "ns1.json"), []byte(nsjson), fileMode)) require.NoError(t, ioutil.WriteFile(fn, []byte(fmt.Sprintf(configReference, nsdir)), fileMode)) cmd.PersistentArgs = []string{} @@ -121,8 +122,8 @@ func TestValidateConfigNamespaces(t *testing.T) { t.Run("case=read config with dir reference bad namespaces", func(t *testing.T) { nsdir := t.TempDir() - fn := t.TempDir() + "/config.yaml" - require.NoError(t, ioutil.WriteFile(nsdir+"/ns0.yaml", []byte("name: badId\nid: foo"), fileMode)) + fn := filepath.Join(t.TempDir(), "config.yaml") + require.NoError(t, ioutil.WriteFile(filepath.Join(nsdir, "ns0.yaml"), []byte("name: badId\nid: foo"), fileMode)) require.NoError(t, ioutil.WriteFile(fn, []byte(fmt.Sprintf(configReference, nsdir)), fileMode)) cmd.PersistentArgs = []string{} @@ -136,7 +137,7 @@ func validateCommand() *cobra.Command { Use: "keto", Short: "Global and consistent permission and authorization server", } - configx.RegisterConfigFlag(cmd.PersistentFlags(), []string{"/tmp/non-exist.yaml"}) + configx.RegisterConfigFlag(cmd.PersistentFlags(), []string{}) cmd.AddCommand(NewValidateCmd()) return cmd }