/
config.go
112 lines (86 loc) · 2.96 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package config
import (
"encoding/json"
"regexp"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
"bosh-softlayer-cpi/logger"
"bosh-softlayer-cpi/registry"
boslconfig "bosh-softlayer-cpi/softlayer/config"
)
type Config struct {
Cloud Cloud `json:"cloud"`
}
type Cloud struct {
Plugin string `json:"plugin"`
Properties CPIProperties `json:"properties"`
}
type CPIProperties struct {
SoftLayer boslconfig.Config
Agent registry.AgentOptions
Registry registry.ClientOptions
}
func NewConfigFromPath(configFile string, fs boshsys.FileSystem, logger logger.Logger) (Config, error) {
var config Config
if configFile == "" {
return config, bosherr.Errorf("Must provide a config file")
}
logger.Debug("File System", "Reading file %s", configFile)
bytes, err := fs.ReadFileWithOpts(configFile, boshsys.ReadOpts{Quiet: true})
if err != nil {
return config, bosherr.WrapErrorf(err, "Reading config file '%s'", configFile)
}
logger.DebugWithDetails("File System", "Read content", string(redact(bytes)))
if err = json.Unmarshal(bytes, &config); err != nil {
return config, bosherr.WrapError(err, "Unmarshalling config contents")
}
if err = config.Validate(); err != nil {
return config, bosherr.WrapError(err, "Validating config")
}
return config, nil
}
func NewConfigFromString(configString string) (Config, error) {
var config Config
var err error
if configString == "" {
return config, bosherr.Errorf("Must provide a config")
}
if err = json.Unmarshal([]byte(configString), &config); err != nil {
return config, bosherr.WrapError(err, "Unmarshalling config contents")
}
if err = config.Validate(); err != nil {
return config, bosherr.WrapError(err, "Validating config")
}
return config, nil
}
func (c Config) Validate() error {
if c.Cloud.Plugin != "softlayer" {
return bosherr.Errorf("Unsupported cloud plugin type %q", c.Cloud.Plugin)
}
if err := c.Cloud.Properties.SoftLayer.Validate(); err != nil {
return bosherr.WrapError(err, "Validating SoftLayer configuration")
}
if err := c.Cloud.Properties.Agent.Validate(); err != nil {
return bosherr.WrapError(err, "Validating agent configuration")
}
//if err := c.Cloud.Properties.Registry.Validate(); err != nil {
// return bosherr.WrapError(err, "Validating registry configuration")
//}
return nil
}
func redact(bs []byte) []byte {
s := string(bs)
hiddenStr1 := "\"password\":\"<redact>\"" // replacement string
r1 := regexp.MustCompile(`"password":"[^"]*"`) // original string
s1 := r1.ReplaceAllString(s, hiddenStr1)
hiddenStr2 := "\"api_key\":\"<redact>\""
r2 := regexp.MustCompile(`"api_key":"[^"]*"`)
s2 := r2.ReplaceAllString(s1, hiddenStr2)
hiddenStr3 := "//registry:<redact>"
r3 := regexp.MustCompile(`//registry:[^@]*`)
s3 := r3.ReplaceAllString(s2, hiddenStr3)
hiddenStr4 := "//nats:<redact>"
r4 := regexp.MustCompile(`//nats:[^@]*`)
s4 := r4.ReplaceAllString(s3, hiddenStr4)
return []byte(s4)
}