From 27fd28bdef9695ba2dce691e46ae194689e322f9 Mon Sep 17 00:00:00 2001 From: aeneasr Date: Mon, 29 Apr 2019 13:06:11 +0200 Subject: [PATCH 1/4] Allow configuration files and update UPGRADE guide. Signed-off-by: aeneasr --- .circleci/config.yml | 3 +- UPGRADE.md | 9 ++++ cmd/root.go | 61 ++++++++++++++++++++++++++ driver/configuration/provider_viper.go | 15 +++++-- 4 files changed, 84 insertions(+), 4 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 53aa24d60..44d1f0ef4 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -92,11 +92,12 @@ jobs: working_directory: /go/src/github.com/ory/keto steps: - checkout + - run: ./scripts/run-configuration.sh - run: git config --global user.email "hi@ory.am" - run: git config --global user.name "ORY Continuous Integration" - run: "git clone https://arekkas:$DOCS_TOKEN_PUSH@github.com/ory/docs.git ../docs" - run: "cp ./docs/api.swagger.json ../docs/apis/keto.json" - - run: ./scripts/run-configuration.sh + - run: "cp configuration.md ../docs/docs/keto/configuration.md" - run: "(cd ../docs && git add -A && git commit -a -m \"Updates ORY Keto Swagger and config definitions\" && git push origin) || exit 0" release-changelog: diff --git a/UPGRADE.md b/UPGRADE.md index 6f3d8f2e0..c720fb4e9 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -27,6 +27,15 @@ before finalizing the upgrade process. ## 0.3.0-sandbox +### Configuration + +The configuration management was updated and now allows configuration via a config file. Environment +variables can still be used to configure ORY Keto but have been updated. However, old env vars still work +but will yield a warning. + +An overview of an exemplary configuration file can be found +in [./docs/config.yml](https://github.com/ory/hydra/blob/master/docs/config.yaml). + ### ORY Access Control Policies Allowed Endpoint Endpoint `/engines/acp/ory/{flavor}/allowed` now returns a 403 error when the request is disallowed. diff --git a/cmd/root.go b/cmd/root.go index 8852a5916..ad75c2833 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,6 +17,9 @@ package cmd import ( "fmt" "os" + "path/filepath" + "runtime" + "strings" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -36,6 +39,8 @@ var RootCmd = &cobra.Command{ Use: "keto", } +var cfgFile string + var logger *logrus.Logger = new(logrus.Logger) //var cmdHandler = client.NewHandler() @@ -55,6 +60,7 @@ func init() { // Here you will define your flags and configuration settings. // Cobra supports Persistent Flags, which, if defined here, // will be global for your application. + RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "Config file (default is $HOME/.keto.yaml)") // Cobra also supports local flags, which will only run // when this action is called directly. @@ -63,7 +69,62 @@ func init() { // initConfig reads in config file and ENV variables if set. func initConfig() { + if cfgFile != "" { + // enable ability to specify config file via flag + viper.SetConfigFile(cfgFile) + } else { + path := absPathify("$HOME") + if _, err := os.Stat(filepath.Join(path, ".keto.yml")); err != nil { + _, _ = os.Create(filepath.Join(path, ".keto.yml")) + } + + viper.SetConfigType("yaml") + viper.SetConfigName(".keto") // name of config file (without extension) + viper.AddConfigPath("$HOME") // adding home directory as first search path + } + + viper.SetDefault("LOG_LEVEL", "info") + + viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.AutomaticEnv() // read in environment variables that match *logger = *logrusx.New() + + // If a config file is found, read it in. + if err := viper.ReadInConfig(); err != nil { + fmt.Printf(`Config file not found because "%s"`, err) + fmt.Println("") + } +} + +func absPathify(inPath string) string { + if strings.HasPrefix(inPath, "$HOME") { + inPath = userHomeDir() + inPath[5:] + } + + if strings.HasPrefix(inPath, "$") { + end := strings.Index(inPath, string(os.PathSeparator)) + inPath = os.Getenv(inPath[1:end]) + inPath[end:] + } + + if filepath.IsAbs(inPath) { + return filepath.Clean(inPath) + } + + p, err := filepath.Abs(inPath) + if err == nil { + return filepath.Clean(p) + } + return "" +} + +func userHomeDir() string { + if runtime.GOOS == "windows" { + home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") + if home == "" { + home = os.Getenv("USERPROFILE") + } + return home + } + return os.Getenv("HOME") } diff --git a/driver/configuration/provider_viper.go b/driver/configuration/provider_viper.go index 067adf3db..f9a1e6faa 100644 --- a/driver/configuration/provider_viper.go +++ b/driver/configuration/provider_viper.go @@ -13,12 +13,16 @@ import ( "github.com/ory/x/viperx" ) -const ViperKeyDSN = "dsn" +const ( + ViperKeyDSN = "dsn" + ViperKeyHost = "serve.host" + ViperKeyPort = "serve.port" +) func init() { viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.AutomaticEnv() - viper.SetDefault("PORT", "4466") + viper.SetDefault(ViperKeyPort, "4466") } type ViperProvider struct { @@ -30,8 +34,13 @@ func NewViperProvider(l logrus.FieldLogger) Provider { } func (v *ViperProvider) ListenOn() string { - return fmt.Sprintf("%s:%s", viper.GetString("HOST"), viper.GetString("PORT")) + return fmt.Sprintf( + "%s:%d", + viperx.GetString(v.l, ViperKeyHost, "", "HOST"), + viperx.GetInt(v.l, ViperKeyPort, 4466, "PORT"), + ) } + func (v *ViperProvider) CORSEnabled() bool { return corsx.IsEnabled(v.l, "serve") } From 427513e7d8d6d2a15ccff863acba2afa44ed0083 Mon Sep 17 00:00:00 2001 From: aeneasr Date: Mon, 29 Apr 2019 14:07:31 +0200 Subject: [PATCH 2/4] u Signed-off-by: aeneasr --- cmd/root.go | 3 ++- driver/configuration/provider_viper.go | 14 ++------------ 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index ad75c2833..2add009b9 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -83,7 +83,8 @@ func initConfig() { viper.AddConfigPath("$HOME") // adding home directory as first search path } - viper.SetDefault("LOG_LEVEL", "info") + viper.SetDefault("serve.port", "4466") + viper.SetDefault("log.level", "info") viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.AutomaticEnv() // read in environment variables that match diff --git a/driver/configuration/provider_viper.go b/driver/configuration/provider_viper.go index f9a1e6faa..10f0c971f 100644 --- a/driver/configuration/provider_viper.go +++ b/driver/configuration/provider_viper.go @@ -2,15 +2,11 @@ package configuration import ( "fmt" - "strings" - - "github.com/rs/cors" - "github.com/sirupsen/logrus" - "github.com/spf13/viper" - "github.com/ory/x/corsx" "github.com/ory/x/tracing" "github.com/ory/x/viperx" + "github.com/rs/cors" + "github.com/sirupsen/logrus" ) const ( @@ -19,12 +15,6 @@ const ( ViperKeyPort = "serve.port" ) -func init() { - viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) - viper.AutomaticEnv() - viper.SetDefault(ViperKeyPort, "4466") -} - type ViperProvider struct { l logrus.FieldLogger } From 5a841c8bc0406148a927b3016b9bb85677045dcf Mon Sep 17 00:00:00 2001 From: aeneasr Date: Mon, 29 Apr 2019 14:21:39 +0200 Subject: [PATCH 3/4] u Signed-off-by: aeneasr --- cmd/0_init_test.go | 4 ++-- driver/configuration/provider_viper.go | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/cmd/0_init_test.go b/cmd/0_init_test.go index 50adec066..22a21ed7b 100644 --- a/cmd/0_init_test.go +++ b/cmd/0_init_test.go @@ -12,8 +12,8 @@ var port int func init() { var osArgs = make([]string, len(os.Args)) port = gotil.RandomTCPPort() - os.Setenv("DATABASE_URL", "memory") - os.Setenv("PORT", fmt.Sprintf("%d", port)) + os.Setenv("DSN", "memory") + os.Setenv("SERVE_PORT", fmt.Sprintf("%d", port)) os.Setenv("KETO_URL", fmt.Sprintf("http://127.0.0.1:%d", port)) copy(osArgs, os.Args) } diff --git a/driver/configuration/provider_viper.go b/driver/configuration/provider_viper.go index 10f0c971f..fb2c59c82 100644 --- a/driver/configuration/provider_viper.go +++ b/driver/configuration/provider_viper.go @@ -2,11 +2,13 @@ package configuration import ( "fmt" + + "github.com/rs/cors" + "github.com/sirupsen/logrus" + "github.com/ory/x/corsx" "github.com/ory/x/tracing" "github.com/ory/x/viperx" - "github.com/rs/cors" - "github.com/sirupsen/logrus" ) const ( From 9e7bedb55b8f181f8cfbf0041c32f40a05dbdf77 Mon Sep 17 00:00:00 2001 From: aeneasr Date: Mon, 29 Apr 2019 14:50:31 +0200 Subject: [PATCH 4/4] ci: Use image that includes bash/sh for release docs Signed-off-by: aeneasr --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 44d1f0ef4..d5486b01a 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -88,7 +88,7 @@ jobs: release-docs: docker: - - image: alpine/git:1.0.4 + - image: circleci/golang:1.12 # if we use something else bash may be missing working_directory: /go/src/github.com/ory/keto steps: - checkout