Skip to content

Commit 0a4a611

Browse files
committed
Clean the parse configuration logic
Signed-off-by: Oluwole Fadeyi <[email protected]>
1 parent dba5d76 commit 0a4a611

File tree

3 files changed

+43
-36
lines changed

3 files changed

+43
-36
lines changed

pkg/agent/config.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func (c *Config) Dump() (string, error) {
127127
return string(d), nil
128128
}
129129

130-
func (c *Config) validate() error {
130+
func (c *Config) validate(isVenafi bool) error {
131131
var result *multierror.Error
132132

133133
// configured for Venafi Cloud
@@ -139,7 +139,7 @@ func (c *Config) validate() error {
139139
if _, err := url.Parse(c.VenafiCloud.UploadPath); err != nil {
140140
result = multierror.Append(result, fmt.Errorf("upload_path is not a valid URL"))
141141
}
142-
} else {
142+
} else if !isVenafi {
143143
if c.OrganizationID == "" {
144144
result = multierror.Append(result, fmt.Errorf("organization_id is required"))
145145
}
@@ -167,7 +167,7 @@ func (c *Config) validate() error {
167167
}
168168

169169
// ParseConfig reads config into a struct used to configure running agents
170-
func ParseConfig(data []byte) (Config, error) {
170+
func ParseConfig(data []byte, isVenafi bool) (Config, error) {
171171
var config Config
172172

173173
err := yaml.Unmarshal(data, &config)
@@ -186,7 +186,8 @@ func ParseConfig(data []byte) (Config, error) {
186186
config.Endpoint.Protocol = "http"
187187
}
188188

189-
if err = config.validate(); err != nil {
189+
err = config.validate(isVenafi)
190+
if err != nil {
190191
return config, err
191192
}
192193

pkg/agent/config_test.go

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestValidConfigLoad(t *testing.T) {
2525
output-path: "/nothome"
2626
`
2727

28-
loadedConfig, err := ParseConfig([]byte(configFileContents))
28+
loadedConfig, err := ParseConfig([]byte(configFileContents), false)
2929
if err != nil {
3030
t.Fatalf("unexpected error: %v", err)
3131
}
@@ -68,7 +68,7 @@ func TestValidConfigWithEndpointLoad(t *testing.T) {
6868
always-fail: false
6969
`
7070

71-
loadedConfig, err := ParseConfig([]byte(configFileContents))
71+
loadedConfig, err := ParseConfig([]byte(configFileContents), false)
7272
if err != nil {
7373
t.Errorf("unexpected error: %v", err)
7474
}
@@ -114,7 +114,7 @@ func TestValidVenafiCloudConfigLoad(t *testing.T) {
114114
upload_path: "/testing/path"
115115
`
116116

117-
loadedConfig, err := ParseConfig([]byte(configFileContents))
117+
loadedConfig, err := ParseConfig([]byte(configFileContents), false)
118118
if err != nil {
119119
t.Fatalf("unexpected error: %v", err)
120120
}
@@ -149,7 +149,7 @@ func TestValidVenafiCloudConfigLoad(t *testing.T) {
149149
func TestInvalidConfigError(t *testing.T) {
150150
configFileContents := `data-gatherers: "things"`
151151

152-
_, parseError := ParseConfig([]byte(configFileContents))
152+
_, parseError := ParseConfig([]byte(configFileContents), false)
153153

154154
expectedError := fmt.Errorf("yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `things` into []agent.DataGatherer")
155155

@@ -159,26 +159,35 @@ func TestInvalidConfigError(t *testing.T) {
159159
}
160160

161161
func TestMissingConfigError(t *testing.T) {
162-
_, parseError := ParseConfig([]byte(""))
163-
164-
if parseError == nil {
165-
t.Fatalf("expected error, got nil")
166-
}
167-
168-
expectedErrorLines := []string{
169-
"2 errors occurred:",
170-
"\t* organization_id is required",
171-
"\t* cluster_id is required",
172-
"\n",
173-
}
174-
175-
expectedError := strings.Join(expectedErrorLines, "\n")
176-
177-
gotError := parseError.Error()
178-
179-
if gotError != expectedError {
180-
t.Errorf("\ngot=\n%v\nwant=\n%s\ndiff=\n%s", gotError, expectedError, diff.Diff(gotError, expectedError))
181-
}
162+
t.Run("fail to parse config if organization_id or cluster_id are missing (venafi-cloud not enabled)", func(t *testing.T) {
163+
_, parseError := ParseConfig([]byte(""), false)
164+
165+
if parseError == nil {
166+
t.Fatalf("expected error, got nil")
167+
}
168+
169+
expectedErrorLines := []string{
170+
"2 errors occurred:",
171+
"\t* organization_id is required",
172+
"\t* cluster_id is required",
173+
"\n",
174+
}
175+
176+
expectedError := strings.Join(expectedErrorLines, "\n")
177+
178+
gotError := parseError.Error()
179+
180+
if gotError != expectedError {
181+
t.Errorf("\ngot=\n%v\nwant=\n%s\ndiff=\n%s", gotError, expectedError, diff.Diff(gotError, expectedError))
182+
}
183+
})
184+
t.Run("successfully parse config if organization_id or cluster_id are missing (venafi-cloud is enabled)", func(t *testing.T) {
185+
_, parseError := ParseConfig([]byte(""), true)
186+
187+
if parseError != nil {
188+
t.Fatalf("unxexpected error, no error should have occured when parsing configuration: %s", parseError)
189+
}
190+
})
182191
}
183192

184193
func TestPartialMissingConfigError(t *testing.T) {
@@ -190,7 +199,7 @@ func TestPartialMissingConfigError(t *testing.T) {
190199
organization_id: "example"
191200
cluster_id: "example-cluster"
192201
data-gatherers:
193-
- kind: dummy`))
202+
- kind: dummy`), false)
194203

195204
if parseError == nil {
196205
t.Fatalf("expected error, got nil")
@@ -218,7 +227,7 @@ func TestInvalidServerError(t *testing.T) {
218227
cluster_id: "my_cluster"
219228
data-gatherers:
220229
- kind: dummy
221-
name: dummy`))
230+
name: dummy`), false)
222231

223232
if parseError == nil {
224233
t.Fatalf("expected error, got nil")
@@ -246,7 +255,7 @@ func TestInvalidDataGathered(t *testing.T) {
246255
path: /api/v1/data
247256
schedule: "* * * * *"
248257
data-gatherers:
249-
- kind: "foo"`))
258+
- kind: "foo"`), false)
250259

251260
if parseError == nil {
252261
t.Fatalf("expected error, got nil")

pkg/agent/run.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,8 @@ func getConfiguration() (Config, client.Client) {
198198
log.Fatalf("Failed to read config file: %s", err)
199199
}
200200

201-
config, err := ParseConfig(b)
202-
switch {
203-
case err != nil && VenafiCloudMode && (config.OrganizationID == "" || config.ClusterID == ""):
204-
// venafi-cloud does not require the OrganizationID or ClusterID, do not error in case they are missing
205-
case err != nil:
201+
config, err := ParseConfig(b, VenafiCloudMode)
202+
if err != nil {
206203
log.Fatalf("Failed to parse config file: %s", err)
207204
}
208205

0 commit comments

Comments
 (0)