Skip to content

Commit

Permalink
Allow configuration files and update UPGRADE guide. (#102)
Browse files Browse the repository at this point in the history
Signed-off-by: aeneasr <[email protected]>
  • Loading branch information
aeneasr authored Apr 29, 2019
1 parent 605d2f4 commit 3934dc6
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 "[email protected]"
- run: git config --global user.name "ORY Continuous Integration"
- run: "git clone https://arekkas:[email protected]/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:
Expand Down
9 changes: 9 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions cmd/0_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
62 changes: 62 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ package cmd
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -36,6 +39,8 @@ var RootCmd = &cobra.Command{
Use: "keto",
}

var cfgFile string

var logger *logrus.Logger = new(logrus.Logger)

//var cmdHandler = client.NewHandler()
Expand All @@ -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.
Expand All @@ -63,7 +69,63 @@ 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("serve.port", "4466")
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")
}
21 changes: 11 additions & 10 deletions driver/configuration/provider_viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@ 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"
)

const ViperKeyDSN = "dsn"

func init() {
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
viper.SetDefault("PORT", "4466")
}
const (
ViperKeyDSN = "dsn"
ViperKeyHost = "serve.host"
ViperKeyPort = "serve.port"
)

type ViperProvider struct {
l logrus.FieldLogger
Expand All @@ -30,8 +26,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")
}
Expand Down

0 comments on commit 3934dc6

Please sign in to comment.