Skip to content

Commit

Permalink
enhancement: Config override from flags (#121)
Browse files Browse the repository at this point in the history
Allows overriding the config values defined in the file by values
supplied via the command line using the `--set` flag.
  • Loading branch information
charithe committed May 26, 2021
1 parent 3b8ad7b commit df6865c
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 32 deletions.
2 changes: 2 additions & 0 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,5 @@ go.uber.org/automaxprocs Unknown
go.uber.org/config Unknown MIT
go.uber.org/multierr Unknown MIT
go.uber.org/zap Unknown MIT
helm.sh/helm/v3/pkg/strvals Unknown Apache-2.0
sigs.k8s.io/yaml Unknown MIT
18 changes: 15 additions & 3 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ package server

import (
"context"
"fmt"
"os"
"os/signal"

"github.com/google/gops/agent"
"github.com/spf13/cobra"
"go.uber.org/automaxprocs/maxprocs"
"go.uber.org/zap"
"helm.sh/helm/v3/pkg/strvals"

"github.com/cerbos/cerbos/internal/config"
"github.com/cerbos/cerbos/internal/observability/logging"
Expand All @@ -20,8 +22,9 @@ import (

type serverArgs struct {
configFile string
logLevel string
configOverrides []string
debugListenAddr string
logLevel string
zpagesEnabled bool
}

Expand All @@ -36,8 +39,9 @@ func NewCommand() *cobra.Command {
}

cmd.Flags().StringVar(&args.configFile, "config", "", "Path to config file")
cmd.Flags().StringVar(&args.logLevel, "log-level", "INFO", "Log level")
cmd.Flags().StringSliceVar(&args.configOverrides, "set", nil, "Config overrides")
cmd.Flags().StringVar(&args.debugListenAddr, "debug-listen-addr", "", "Address to start the gops listener")
cmd.Flags().StringVar(&args.logLevel, "log-level", "INFO", "Log level")
cmd.Flags().BoolVar(&args.zpagesEnabled, "zpages-enabled", false, "Enable zpages for debugging")

_ = cmd.Flags().MarkHidden("zpages-enabled")
Expand Down Expand Up @@ -66,8 +70,16 @@ func doRun(_ *cobra.Command, _ []string) error {
ctx, stopFunc := signal.NotifyContext(context.Background(), os.Interrupt)
defer stopFunc()

// load any config overrides
confOverrides := map[string]interface{}{}
for _, override := range args.configOverrides {
if err := strvals.ParseInto(override, confOverrides); err != nil {
return fmt.Errorf("failed to parse config override [%s]: %w", override, err)
}
}

// load configuration
if err := config.Load(args.configFile); err != nil {
if err := config.Load(args.configFile, confOverrides); err != nil {
return err
}

Expand Down
6 changes: 3 additions & 3 deletions docs/modules/ROOT/pages/usage.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ include::partial$attributes.adoc[]

== Starting the server

The server is configured with a YAML file that is passed using the `--config` flag. See xref:configuration:index.adoc[Configuration] for more information about available configuration options.
The server is configured with a YAML file that is passed using the `--config` flag. Values in the configuration file can be overridden from the command-line by the `--set` flag — which can be used multiple times. See xref:configuration:index.adoc[Configuration] for more information about available configuration options.

.Using the binary
[source,sh,subs="attributes"]
----
./{app-name} server --config=/path/to/config.yaml
./{app-name} server --config=/path/to/config.yaml --set=server.httpListenAddr=:3592
----

.Using Docker
Expand All @@ -18,7 +18,7 @@ The server is configured with a YAML file that is passed using the `--config` fl
docker run -i -t -p 3592:3592 \
-v /path/to/conf/dir:/config \
{app-docker-img} \
server --config=/config/conf.yaml
server --config=/config/conf.yaml --set=server.httpListenAddr=:3592
----

== Compiling and testing policies
Expand Down
11 changes: 9 additions & 2 deletions docs/modules/configuration/pages/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ include::ROOT:partial$attributes.adoc[]
[[configuration]]
= Configuration

The Cerbos server is configured with a YAML file. Start the server by passing the configuration file using the `--config` flag.
The Cerbos server is configured with a YAML file. Start the server by passing the configuration file using the `--config` flag. The values defined in the file can be overridden from the command-line by using the `--set` flag. The `--set` flag can be used multiple times. For example, to override `server.httpListenAddr` and `engine.defaultPolicyVersion`, the `--set` flag can be used as follows:


[source,sh,subs="attributes"]
----
./{app-name} server --config=/path/to/config.yaml --set=server.httpListenAddr=:3592 --set=engine.defaultPolicyVersion=staging
----

NOTE: Config values can reference environment variables by enclosing them between `${}`. E.g. `$$${HOME}$$`.

Expand All @@ -17,6 +23,7 @@ server:
grpcListenAddr: ":3593"
metricsEnabled: true # Set to false to disable the /_cerbos/metrics endpoint
logRequestPayloads: false # Set to true to log full request and response payloads. Affects performance.
playgroundEnabled: false # Set to true to enable the playground API.
tls: # Optional
cert: /path/to/certificate
key: /path/to/private_key
Expand All @@ -37,7 +44,7 @@ storage:
driver: "disk" # Valid values are "disk" or "git"
disk: # Only required if "driver" is "disk"
directory: pkg/test/testdata/store
readOnly: true
watchForChanges: false
git: # Only required if the "driver" is "git"
protocol: file # Valid values are "file", "ssh", "https"
url: file://${HOME}/tmp/cerbos/policies
Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ require (
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rjeczalik/notify v0.9.3-0.20201210012515-e2a77dcc14cf
github.com/rs/zerolog v1.20.0 // indirect
github.com/spf13/afero v1.6.0
github.com/spf13/cobra v1.1.3
github.com/stretchr/testify v1.7.0
github.com/tidwall/sjson v1.1.6
github.com/vektra/mockery/v2 v2.6.0
go.elastic.co/ecszap v1.0.0
go.opencensus.io v0.23.0
go.uber.org/automaxprocs v1.4.0
Expand All @@ -56,4 +54,9 @@ require (
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0
google.golang.org/protobuf v1.26.0
gotest.tools/gotestsum v1.6.4
helm.sh/helm/v3 v3.5.4
)

replace github.com/docker/distribution => github.com/docker/distribution v0.0.0-20191216044856-a8371794149d

replace github.com/docker/docker => github.com/docker/docker v17.12.0-ce-rc1.0.20200618181300-9dc6525e6118+incompatible

0 comments on commit df6865c

Please sign in to comment.