mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-23 16:55:48 -06:00
713f961bfe
There are two new CLI options added with this change for the `opa run`
sub command.
`--set`
`--set-file`
These allow for overriding config options on the CLI using `key=value`
options to reference the YAML/JSON config structures. The `--set` value
expects to take in the value you want to set while the `--set-file` value is
a path to a file which will be read for the value of the file. This is primarily
useful for secrets mounted as files on a host.
In addition this adds in some simple environment variable injection so that a
deployer can specify environment variables in the config via ${NAME} syntax.
At the time the config is loaded from file it will inject in the environment vars.
This applies to strings set via the `--set` variable too.
Some things to note:
* This won't work for configs generated by discovery bundles, it is *only* for
configurations loaded by the runtime.
* This is only done at the time of loading the file. Plugins receive modified
copies of the config (post injection), and should never be allowed to re-read
directly from disk.
* There are security implications of setting secrets in env vars. It is
recommended to use the file based secrets approach with `--set-file`
instead of environment variables.
Signed-off-by: Patrick East <east.patrick@gmail.com>
115 lines
3.3 KiB
Go
115 lines
3.3 KiB
Go
// Copyright 2016 The OPA Authors. All rights reserved.
|
|
// Use of this source code is governed by an Apache2
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package runtime
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/ghodss/yaml"
|
|
"github.com/open-policy-agent/opa/internal/strvals"
|
|
)
|
|
|
|
func loadConfig(params Params) ([]byte, error) {
|
|
baseConf := map[string]interface{}{}
|
|
|
|
// User specified config file
|
|
if params.ConfigFile != "" {
|
|
var bytes []byte
|
|
var err error
|
|
bytes, err = ioutil.ReadFile(params.ConfigFile)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
processedConf := subEnvVars(string(bytes))
|
|
|
|
if err := yaml.Unmarshal([]byte(processedConf), &baseConf); err != nil {
|
|
return []byte{}, fmt.Errorf("failed to parse %s: %s", params.ConfigFile, err)
|
|
}
|
|
}
|
|
|
|
overrideConf := map[string]interface{}{}
|
|
|
|
// User specified a config override via --set
|
|
for _, override := range params.ConfigOverrides {
|
|
processedOverride := subEnvVars(override)
|
|
if err := strvals.ParseInto(processedOverride, overrideConf); err != nil {
|
|
return []byte{}, fmt.Errorf("failed parsing --set data: %s", err)
|
|
}
|
|
}
|
|
|
|
// User specified a config override value via --set-file
|
|
for _, override := range params.ConfigOverrideFiles {
|
|
reader := func(rs []rune) (interface{}, error) {
|
|
bytes, err := ioutil.ReadFile(string(rs))
|
|
value := strings.TrimSpace(string(bytes))
|
|
return value, err
|
|
}
|
|
if err := strvals.ParseIntoFile(override, overrideConf, reader); err != nil {
|
|
return []byte{}, fmt.Errorf("failed parsing --set-file data: %s", err)
|
|
}
|
|
}
|
|
|
|
// Merge together base config file and overrides, prefer the overrides
|
|
conf := mergeValues(baseConf, overrideConf)
|
|
|
|
// Take the patched config and marshal back to YAML
|
|
return yaml.Marshal(conf)
|
|
}
|
|
|
|
// regex looking for ${...} notation strings
|
|
var envRegex = regexp.MustCompile(`(?U:\${.*})`)
|
|
|
|
// subEnvVars will look for any environment variables in the passed in string
|
|
// with the syntax of ${VAR_NAME} and replace that string with ENV[VAR_NAME]
|
|
func subEnvVars(s string) string {
|
|
updatedConfig := envRegex.ReplaceAllStringFunc(s, func(s string) string {
|
|
// Trim off the '${' and '}'
|
|
if len(s) <= 3 {
|
|
// This should never happen..
|
|
return ""
|
|
}
|
|
varName := s[2 : len(s)-1]
|
|
|
|
// Lookup the variable in the environment. We play by
|
|
// bash rules.. if its undefined we'll treat it as an
|
|
// empty string instead of raising an error.
|
|
return os.Getenv(varName)
|
|
})
|
|
|
|
return updatedConfig
|
|
}
|
|
|
|
// mergeValues will merge source and destination map, preferring values from the source map
|
|
func mergeValues(dest map[string]interface{}, src map[string]interface{}) map[string]interface{} {
|
|
for k, v := range src {
|
|
// If the key doesn't exist already, then just set the key to that value
|
|
if _, exists := dest[k]; !exists {
|
|
dest[k] = v
|
|
continue
|
|
}
|
|
nextMap, ok := v.(map[string]interface{})
|
|
// If it isn't another map, overwrite the value
|
|
if !ok {
|
|
dest[k] = v
|
|
continue
|
|
}
|
|
// Edge case: If the key exists in the destination, but isn't a map
|
|
destMap, isMap := dest[k].(map[string]interface{})
|
|
// If the source map has a map for this key, prefer it
|
|
if !isMap {
|
|
dest[k] = v
|
|
continue
|
|
}
|
|
// If we got to this point, it is a map in both, so merge them
|
|
dest[k] = mergeValues(destMap, nextMap)
|
|
}
|
|
return dest
|
|
}
|