1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
17 "git.curoverse.com/arvados.git/sdk/go/arvados"
18 "github.com/ghodss/yaml"
19 "github.com/imdario/mergo"
22 type logger interface {
23 Warnf(string, ...interface{})
26 func loadFileOrStdin(path string, stdin io.Reader, log logger) (*arvados.Config, error) {
28 return load(stdin, log, true)
30 return LoadFile(path, log)
34 func LoadFile(path string, log logger) (*arvados.Config, error) {
35 f, err := os.Open(path)
43 func Load(rdr io.Reader, log logger) (*arvados.Config, error) {
44 return load(rdr, log, true)
47 func load(rdr io.Reader, log logger, useDeprecated bool) (*arvados.Config, error) {
48 buf, err := ioutil.ReadAll(rdr)
53 // Load the config into a dummy map to get the cluster ID
54 // keys, discarding the values; then set up defaults for each
55 // cluster ID; then load the real config on top of the
58 Clusters map[string]struct{}
60 err = yaml.Unmarshal(buf, &dummy)
64 if len(dummy.Clusters) == 0 {
65 return nil, errors.New("config does not define any clusters")
68 // We can't merge deep structs here; instead, we unmarshal the
69 // default & loaded config files into generic maps, merge
70 // those, and then json-encode+decode the result into the
71 // config struct type.
72 var merged map[string]interface{}
73 for id := range dummy.Clusters {
74 var src map[string]interface{}
75 err = yaml.Unmarshal(bytes.Replace(DefaultYAML, []byte(" xxxxx:"), []byte(" "+id+":"), -1), &src)
77 return nil, fmt.Errorf("loading defaults for %s: %s", id, err)
79 err = mergo.Merge(&merged, src, mergo.WithOverride)
81 return nil, fmt.Errorf("merging defaults for %s: %s", id, err)
84 var src map[string]interface{}
85 err = yaml.Unmarshal(buf, &src)
87 return nil, fmt.Errorf("loading config data: %s", err)
89 logExtraKeys(log, merged, src, "")
90 removeSampleKeys(merged)
91 err = mergo.Merge(&merged, src, mergo.WithOverride)
93 return nil, fmt.Errorf("merging config data: %s", err)
96 // map[string]interface{} => json => arvados.Config
97 var cfg arvados.Config
101 errEnc = json.NewEncoder(pw).Encode(merged)
104 err = json.NewDecoder(pr).Decode(&cfg)
109 return nil, fmt.Errorf("transcoding config data: %s", err)
113 err = applyDeprecatedConfig(&cfg, buf, log)
119 // Check for known mistakes
120 for id, cc := range cfg.Clusters {
121 err = checkKeyConflict(fmt.Sprintf("Clusters.%s.PostgreSQL.Connection", id), cc.PostgreSQL.Connection)
129 func checkKeyConflict(label string, m map[string]string) error {
130 saw := map[string]bool{}
132 k = strings.ToLower(k)
134 return fmt.Errorf("%s: multiple entries for %q (fix by using same capitalization as default/example file)", label, k)
141 func removeSampleKeys(m map[string]interface{}) {
143 for _, v := range m {
144 if v, _ := v.(map[string]interface{}); v != nil {
150 func logExtraKeys(log logger, expected, supplied map[string]interface{}, prefix string) {
154 allowed := map[string]interface{}{}
155 for k, v := range expected {
156 allowed[strings.ToLower(k)] = v
158 for k, vsupp := range supplied {
159 vexp, ok := allowed[strings.ToLower(k)]
160 if !ok && expected["SAMPLE"] != nil {
161 vexp = expected["SAMPLE"]
163 log.Warnf("deprecated or unknown config entry: %s%s", prefix, k)
166 if vsupp, ok := vsupp.(map[string]interface{}); !ok {
167 // if vsupp is a map but vexp isn't map, this
168 // will be caught elsewhere; see TestBadType.
170 } else if vexp, ok := vexp.(map[string]interface{}); !ok {
171 log.Warnf("unexpected object in config entry: %s%s", prefix, k)
173 logExtraKeys(log, vexp, vsupp, prefix+k+".")