Repository navigation
Expand file tree
/
Copy pathconfig_test.go
More file actions
331 lines (294 loc) · 9.1 KB
/
Copy pathconfig_test.go
File metadata and controls
331 lines (294 loc) · 9.1 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright 2024 Block, Inc.
package blip_test
import (
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/cashapp/blip/v2"
)
func TestMain(m *testing.M) {
code := m.Run()
os.Exit(code)
}
// --------------------------------------------------------------------------
func TestDefaultConfig(t *testing.T) {
// The default config should be valid. Would be embarrassing if not.
got := blip.DefaultConfig()
if err := got.Validate(); err != nil {
t.Errorf("default config is not valid, expected it to be valid: %s", err)
}
}
func TestConfigMonitorLoaderStopTimeout(t *testing.T) {
c := blip.ConfigMonitorLoader{}
c.ApplyDefaults(blip.DefaultConfig())
assert.Equal(t, blip.DEFAULT_MONITOR_LOADER_STOP_TIMEOUT, c.StopTimeout)
assert.NoError(t, c.Validate())
for _, v := range []string{"0", "-1s", "3"} {
c.StopTimeout = v
assert.Error(t, c.Validate(), "stop-timeout %q", v)
}
c.StopTimeout = "500ms"
assert.NoError(t, c.Validate())
}
// TestEnvInterpolation verifies that env vars with special characters, most notably $, are properly interpolated
func TestEnvInterpolation(t *testing.T) {
envKey := "blip_test_TestEnvInterpolation"
envVal := "a$1b!@#$%^&*()-+={};\""
defer os.Unsetenv(envKey)
err := os.Setenv(envKey, envVal)
require.Nil(t, err)
cfg := blip.Config{MySQL: blip.ConfigMySQL{Password: fmt.Sprintf("${%s}", envKey)}}
cfg.InterpolateEnvVars()
assert.Equal(t, envVal, cfg.MySQL.Password)
}
// TestEnvInterpolationEmpty verifies that a config like ${FOO:-bar} without FOO set, evaluates to "bar"
func TestEnvInterpolationEmpty(t *testing.T) {
envKey := "blip_test_TestEnvInterpolation"
_ = os.Unsetenv(envKey)
cfg := blip.Config{MySQL: blip.ConfigMySQL{Password: fmt.Sprintf("${%s:-bar}", envKey)}}
cfg.InterpolateEnvVars()
assert.Equal(t, "bar", cfg.MySQL.Password)
}
func TestConfigRedacted(t *testing.T) {
envKey := "blip_test_TestConfigRedacted"
envPassword := "env-monitor-password"
t.Setenv(envKey, envPassword)
cfg := blip.Config{
API: blip.ConfigAPI{Bind: "127.0.0.1:7522"},
MySQL: blip.ConfigMySQL{
Hostname: "defaults.example:3306",
Username: "default-user",
Password: "default-password",
},
Monitors: []blip.ConfigMonitor{
{
MonitorId: "inline-monitor",
Hostname: "inline.example:3306",
Username: "inline-user",
Password: "inline-monitor-password",
},
{
MonitorId: "env-monitor",
Hostname: "env.example:3306",
Username: "env-user",
Password: fmt.Sprintf("${%s}", envKey),
},
},
Plans: blip.ConfigPlans{
Table: "blip.plans",
Monitor: &blip.ConfigMonitor{
MonitorId: "plan-monitor",
Hostname: "plans.example:3306",
Username: "plan-user",
Password: "plan-monitor-password",
},
},
}
cfg.Monitors[1].InterpolateEnvVars()
redacted := cfg.Redacted()
dump := fmt.Sprintf("%#v", redacted)
for _, password := range []string{
"default-password",
"inline-monitor-password",
envPassword,
"plan-monitor-password",
} {
assert.NotContains(t, dump, password)
}
assert.Equal(t, "...", redacted.MySQL.Password)
assert.Equal(t, "...", redacted.Monitors[0].Password)
assert.Equal(t, "...", redacted.Monitors[1].Password)
assert.Equal(t, "...", redacted.Plans.Monitor.Password)
// Redaction retains useful non-secret context for debug logging.
assert.Contains(t, dump, "127.0.0.1:7522")
assert.Contains(t, dump, "inline-monitor")
assert.Contains(t, dump, "inline.example:3306")
assert.Contains(t, dump, "inline-user")
assert.Contains(t, dump, "blip.plans")
// Redaction must not change the live config.
assert.Equal(t, "default-password", cfg.MySQL.Password)
assert.Equal(t, "inline-monitor-password", cfg.Monitors[0].Password)
assert.Equal(t, envPassword, cfg.Monitors[1].Password)
assert.Equal(t, "plan-monitor-password", cfg.Plans.Monitor.Password)
}
func TestConfigMonitorRedactedPreservesPlanMonitorCycles(t *testing.T) {
monitor := blip.ConfigMonitor{
MonitorId: "cyclic-monitor",
Password: "cyclic-password",
}
monitor.Plans.Monitor = &monitor
redacted := monitor.Redacted()
assert.Equal(t, "...", redacted.Password)
require.NotNil(t, redacted.Plans.Monitor)
assert.Equal(t, "...", redacted.Plans.Monitor.Password)
assert.Same(t, redacted.Plans.Monitor, redacted.Plans.Monitor.Plans.Monitor)
assert.Equal(t, "cyclic-password", monitor.Password)
}
func TestConfigRedactedRedactsSinkOptionValues(t *testing.T) {
envKey := "blip_test_TestConfigRedactedRedactsSinkOptionValues"
envToken := "environment-expanded-sink-token"
t.Setenv(envKey, envToken)
cfg := blip.Config{
Sinks: blip.ConfigSinks{
"datadog": {
"api-key-auth": "global-datadog-api-key",
"api-compress": "global-non-secret-option-value",
},
},
Monitors: []blip.ConfigMonitor{
{
MonitorId: "sink-monitor",
Sinks: blip.ConfigSinks{
"signalfx": {
"auth-token": fmt.Sprintf("${%s}", envKey),
},
},
},
},
Plans: blip.ConfigPlans{
Monitor: &blip.ConfigMonitor{
MonitorId: "plan-sink-monitor",
Sinks: blip.ConfigSinks{
"custom-sink": {
"unknown-option": "custom-sink-option-value",
},
},
},
},
}
cfg.InterpolateEnvVars()
cfg.Monitors[0].InterpolateEnvVars()
cfg.Plans.Monitor.InterpolateEnvVars()
redacted := cfg.Redacted()
dump := fmt.Sprintf("%#v", redacted)
for _, value := range []string{
"global-datadog-api-key",
"global-non-secret-option-value",
envToken,
"custom-sink-option-value",
} {
assert.NotContains(t, dump, value)
}
for _, optionName := range []string{
"datadog",
"api-key-auth",
"api-compress",
"signalfx",
"auth-token",
} {
assert.Contains(t, dump, optionName)
}
assert.Equal(t, "...", redacted.Sinks["datadog"]["api-key-auth"])
assert.Equal(t, "...", redacted.Monitors[0].Sinks["signalfx"]["auth-token"])
assert.Equal(t, "...", redacted.Plans.Monitor.Sinks["custom-sink"]["unknown-option"])
// Redaction must not change the live sink maps.
assert.Equal(t, "global-datadog-api-key", cfg.Sinks["datadog"]["api-key-auth"])
assert.Equal(t, envToken, cfg.Monitors[0].Sinks["signalfx"]["auth-token"])
assert.Equal(t, "custom-sink-option-value", cfg.Plans.Monitor.Sinks["custom-sink"]["unknown-option"])
}
func TestConfigHTTPRedacted(t *testing.T) {
envKey := "blip_test_TestConfigHTTPRedacted"
t.Setenv(envKey, "environment-proxy-password")
tests := []struct {
name string
proxy string
want string
}{
{name: "empty"},
{
name: "unauthenticated",
proxy: "http://proxy.example:8080",
want: "http://proxy.example:8080",
},
{
name: "username only",
proxy: "http://proxy-user@proxy.example:8080",
want: "http://proxy-user@proxy.example:8080",
},
{
name: "authenticated",
proxy: "http://proxy-user:proxy-password@proxy.example:8080",
want: "http://proxy-user:xxxxx@proxy.example:8080",
},
{
name: "environment expanded password",
proxy: fmt.Sprintf("http://proxy-user:${%s}@proxy.example:8080", envKey),
want: "http://proxy-user:xxxxx@proxy.example:8080",
},
{
name: "malformed",
proxy: "http://proxy-user:%zz@proxy.example:8080",
want: "...",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := blip.ConfigHTTP{Proxy: tt.proxy}
cfg.InterpolateEnvVars()
original := cfg.Proxy
redacted := cfg.Redacted()
assert.Equal(t, tt.want, redacted.Proxy)
assert.Equal(t, original, cfg.Proxy, "redaction mutated the live HTTP config")
})
}
}
func TestApplyDefaultConfig(t *testing.T) {
// Defaults apply when the value isn't set
df := blip.DefaultConfig()
my := blip.Config{}
my.ApplyDefaults(df)
if my.API.Bind != blip.DEFAULT_API_BIND {
t.Errorf("api.bind=%s, expected %s", my.API.Bind, blip.DEFAULT_API_BIND)
}
// But when a value is set, it overrides the default
my = blip.Config{
API: blip.ConfigAPI{
Bind: ":1234",
},
}
my.ApplyDefaults(df)
if my.API.Bind != ":1234" {
t.Errorf("api.bind=%s, expected :1234", my.API.Bind)
}
}
func TestValidateTimeoutConnect(t *testing.T) {
// timeout-connect must be a non-negative Go duration. Empty is valid because
// defaults might not be applied yet, and zero disables the connect timeout.
tests := []struct {
timeout string
valid bool
}{
{"", true},
{"0", true},
{"0s", true},
{"10s", true},
{"1500ms", true},
{"10", false}, // no unit
{"ten seconds", false},
{"-1s", false},
}
for _, tt := range tests {
t.Run(tt.timeout, func(t *testing.T) {
// Monitor defaults: config.mysql.timeout-connect, validated by Config.Validate
cfg := blip.DefaultConfig()
cfg.MySQL.TimeoutConnect = tt.timeout
err := cfg.Validate()
if tt.valid {
assert.NoError(t, err, "config.mysql")
} else {
assert.ErrorContains(t, err, "invalid config.mysql.timeout-connect", "config.mysql")
}
// Monitor: config.monitor.timeout-connect, validated by ConfigMonitor.Validate
mon := blip.ConfigMonitor{MonitorId: "m1", TimeoutConnect: tt.timeout}
err = mon.Validate()
if tt.valid {
assert.NoError(t, err, "config.monitor")
} else {
assert.ErrorContains(t, err, "invalid config.monitor.timeout-connect", "config.monitor")
assert.ErrorContains(t, err, "m1", "config.monitor")
}
})
}
}