diff options
Diffstat (limited to 'config/policy.go')
-rw-r--r-- | config/policy.go | 39 |
1 files changed, 25 insertions, 14 deletions
diff --git a/config/policy.go b/config/policy.go index f143f1a..ade4e71 100644 --- a/config/policy.go +++ b/config/policy.go @@ -2,19 +2,20 @@ package config import ( "errors" + "encoding/json" "fmt" "io/ioutil" "reflect" "strings" "github.com/utahta/go-openuri" - "gopkg.in/yaml.v2" + "github.com/ghodss/yaml" ) // Policy validates a number of rules against a given configuration. // type Policy struct { - Enforcements []Enforcement `yaml:"enforcements"` + Enforcements []Enforcement `json:"enforcements"` } // Validate checks the given config against all policy enforcements. @@ -23,7 +24,7 @@ func (pol Policy) Validate(config Config) error { validate := newValidator() for _, enforcement := range pol.Enforcements { - cfg, err := ResolveYAMLPath(enforcement.Path, config) + cfg, err := ResolveJSONPath(enforcement.Path, config) if err != nil { // If the path resolved nothing, there's nothing to enforce @@ -52,16 +53,26 @@ func (pol Policy) Validate(config Config) error { // Enforcement represents a policy rule and config path on which to apply it. // type Enforcement struct { - Path string `yaml:"path"` - Rule string `yaml:"rule"` + Path string `json:"path"` + Rule string `json:"rule"` } -// ReadPolicy unmarshals the given YAML bytes into a new Policy struct. +// ReadYAMLPolicy converts YAML input to JSON and returns a new Policy struct. +// +func ReadYAMLPolicy(data []byte) (*Policy, error) { + jsonData, err := yaml.YAMLToJSON(data) + if err != nil { + return nil, err + } + + return ReadPolicy(jsonData) +} + +// ReadPolicy unmarshals the given YAML/json bytes into a new Policy struct. // func ReadPolicy(data []byte) (*Policy, error) { var policy Policy - - err := yaml.Unmarshal(data, &policy) + err := json.Unmarshal(data, &policy) if err != nil { return nil, err @@ -88,13 +99,13 @@ func ReadPolicyFromURI(uri string) (*Policy, error) { return nil, err } - return ReadPolicy(data) + return ReadYAMLPolicy(data) } -// ResolveYAMLPath returns the config value found at the given YAML-ish +// ResolveJSONPath returns the config value found at the given JSON-ish // namespace/path (e.g. "variants.production.runs.as"). // -func ResolveYAMLPath(path string, cfg interface{}) (interface{}, error) { +func ResolveJSONPath(path string, cfg interface{}) (interface{}, error) { parts := strings.SplitN(path, ".", 2) name := parts[0] @@ -107,12 +118,12 @@ func ResolveYAMLPath(path string, cfg interface{}) (interface{}, error) { case reflect.Struct: for i := 0; i < t.NumField(); i++ { if t.Field(i).Anonymous { - if subsubcfg, err := ResolveYAMLPath(path, v.Field(i).Interface()); err == nil { + if subsubcfg, err := ResolveJSONPath(path, v.Field(i).Interface()); err == nil { return subsubcfg, nil } } - if name == resolveYAMLTagName(t.Field(i)) { + if name == resolveJSONTagName(t.Field(i)) { subcfg = v.Field(i).Interface() break } @@ -134,7 +145,7 @@ func ResolveYAMLPath(path string, cfg interface{}) (interface{}, error) { } if len(parts) > 1 { - return ResolveYAMLPath(parts[1], subcfg) + return ResolveJSONPath(parts[1], subcfg) } return subcfg, nil |