Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: moved WatchAndValidateViper to viperx #384

Merged
merged 8 commits into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 0 additions & 35 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ import (
"fmt"
"os"

"github.com/fsnotify/fsnotify"
"github.com/gobuffalo/packr/v2"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

_ "github.com/ory/jsonschema/v3/fileloader"
_ "github.com/ory/jsonschema/v3/httploader"

"github.com/ory/viper"
"github.com/ory/x/viperx"
)

Expand All @@ -58,36 +56,3 @@ func Execute() {
func init() {
viperx.RegisterConfigFlag(RootCmd, "oathkeeper")
}

func watchAndValidateViper() {
logger = viperx.InitializeConfig("oathkeeper", "", logger)

schema, err := schemas.Find("config.schema.json")
if err != nil {
logger.WithError(err).Fatal("Unable to open configuration JSON Schema.")
}

if err := viperx.Validate("config.schema.json", schema); err != nil {
viperx.LoggerWithValidationErrorFields(logger, err).
Fatal("The configuration is invalid and could not be loaded.")
}

viperx.AddWatcher(func(event fsnotify.Event) error {
if err := viperx.Validate("config.schema.json", schema); err != nil {
viperx.LoggerWithValidationErrorFields(logger, err).
Error("The changed configuration is invalid and could not be loaded. Rolling back to the last working configuration revision. Please address the validation errors before restarting ORY Oathkeeper.")
return viperx.ErrRollbackConfigurationChanges
}
return nil
})

viperx.WatchConfig(logger, &viperx.WatchOptions{
Immutables: []string{"serve", "profiling", "log"},
OnImmutableChange: func(key string) {
logger.
WithField("key", key).
WithField("reset_to", fmt.Sprintf("%v", viper.Get(key))).
Error("A configuration value marked as immutable has changed. Rolling back to the last working configuration revision. To reload the values please restart ORY Oathkeeper.")
},
})
}
7 changes: 6 additions & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ on configuration options, open the configuration documentation:
>> https://www.ory.sh/docs/oathkeeper/configuration <<
`,
Run: func(cmd *cobra.Command, args []string) {
watchAndValidateViper()
logger = viperx.InitializeConfig("oathkeeper", "", logger)
schema, err := schemas.Find("config.schema.json")
if err != nil {
logger.WithError(err).Fatal("Unable to open configuration JSON Schema.")
}
viperx.WatchAndValidateViper(logger, schema, "ORY Oathkeeper", []string{"serve", "profiling", "log"})
zepatrik marked this conversation as resolved.
Show resolved Hide resolved
server.RunServe(x.Version, x.Commit, x.Date)(cmd, args)
},
}
Expand Down
30 changes: 16 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ require (
github.com/auth0/go-jwt-middleware v0.0.0-20170425171159-5493cabe49f7
github.com/blang/semver v3.5.1+incompatible
github.com/bxcodec/faker v2.0.1+incompatible
github.com/dgraph-io/ristretto v0.0.1
github.com/dgraph-io/ristretto v0.0.2
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/dlclark/regexp2 v1.2.0
github.com/fsnotify/fsnotify v1.4.7
github.com/fsnotify/fsnotify v1.4.9
github.com/ghodss/yaml v1.0.0
github.com/go-bindata/go-bindata v3.1.2+incompatible // indirect
github.com/go-openapi/errors v0.19.2
github.com/go-openapi/runtime v0.19.5
github.com/go-openapi/strfmt v0.19.3
Expand All @@ -29,27 +30,27 @@ require (
github.com/huandu/xstrings v1.2.0 // indirect
github.com/imdario/mergo v0.3.7
github.com/julienschmidt/httprouter v1.2.0
github.com/lib/pq v1.0.0
github.com/mattn/goveralls v0.0.3
github.com/lib/pq v1.2.0
github.com/mattn/goveralls v0.0.5
github.com/mitchellh/mapstructure v1.2.2 // indirect
github.com/ory/fosite v0.29.2
github.com/ory/go-acc v0.0.0-20181118080137-ddc355013f90
github.com/ory/go-acc v0.2.1
github.com/ory/go-convenience v0.1.0
github.com/ory/gojsonschema v1.2.0
github.com/ory/graceful v0.1.1
github.com/ory/herodot v0.6.2
github.com/ory/herodot v0.7.0
github.com/ory/jsonschema/v3 v3.0.1
github.com/ory/ladon v1.1.0
github.com/ory/sdk/swagutil v0.0.0-20200202121523-307941feee4b
github.com/ory/viper v1.5.7
github.com/ory/x v0.0.95
github.com/ory/viper v1.7.3
github.com/ory/x v0.0.106
github.com/pborman/uuid v1.2.0
github.com/pelletier/go-toml v1.6.0 // indirect
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2
github.com/pkg/errors v0.9.1
github.com/rs/cors v1.6.0
github.com/sirupsen/logrus v1.4.2
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.5 // indirect
github.com/sirupsen/logrus v1.5.0
github.com/spf13/cobra v0.0.6
github.com/spf13/viper v1.6.2 // indirect
github.com/sqs/goreturns v0.0.0-20181028201513-538ac6014518
github.com/square/go-jose v2.3.1+incompatible
github.com/stretchr/testify v1.4.0
Expand All @@ -58,9 +59,10 @@ require (
github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce
github.com/urfave/negroni v1.0.0
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
golang.org/x/crypto v0.0.0-20200117160349-530e935923ad
golang.org/x/crypto v0.0.0-20200320181102-891825fb96df
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
golang.org/x/tools v0.0.0-20200203215610-ab391d50b528
golang.org/x/sys v0.0.0-20200321134203-328b4cd54aae // indirect
golang.org/x/tools v0.0.0-20200323171731-e609210bcd3f
gopkg.in/square/go-jose.v2 v2.3.1
)

Expand Down
Loading