9
9
"os/exec"
10
10
"path/filepath"
11
11
"runtime"
12
+ "strings"
13
+ "syscall"
12
14
"testing"
13
15
14
16
"github.com/docker/docker/api/types"
@@ -165,6 +167,8 @@ func TestDaemonProxy(t *testing.T) {
165
167
}))
166
168
defer proxyServer .Close ()
167
169
170
+ const userPass = "myuser:mypassword@"
171
+
168
172
// Configure proxy through env-vars
169
173
t .Run ("environment variables" , func (t * testing.T ) {
170
174
defer env .Patch (t , "HTTP_PROXY" , proxyServer .URL )()
@@ -195,10 +199,10 @@ func TestDaemonProxy(t *testing.T) {
195
199
196
200
// Configure proxy through command-line flags
197
201
t .Run ("command-line options" , func (t * testing.T ) {
198
- defer env .Patch (t , "HTTP_PROXY" , "http://from-env-http.invalid" )()
199
- defer env .Patch (t , "http_proxy" , "http://from-env-http.invalid" )()
200
- defer env .Patch (t , "HTTPS_PROXY" , "https://from-env-https.invalid" )()
201
- defer env .Patch (t , "https_proxy" , "https://from-env-http .invalid" )()
202
+ defer env .Patch (t , "HTTP_PROXY" , "http://" + userPass + " from-env-http.invalid" )()
203
+ defer env .Patch (t , "http_proxy" , "http://" + userPass + " from-env-http.invalid" )()
204
+ defer env .Patch (t , "HTTPS_PROXY" , "https://" + userPass + "myuser:mypassword@ from-env-https.invalid" )()
205
+ defer env .Patch (t , "https_proxy" , "https://" + userPass + "myuser:mypassword@ from-env-https .invalid" )()
202
206
defer env .Patch (t , "NO_PROXY" , "ignore.invalid" )()
203
207
defer env .Patch (t , "no_proxy" , "ignore.invalid" )()
204
208
@@ -210,6 +214,7 @@ func TestDaemonProxy(t *testing.T) {
210
214
assert .Assert (t , is .Contains (string (logs ), "overriding existing proxy variable with value from configuration" ))
211
215
for _ , v := range []string {"http_proxy" , "HTTP_PROXY" , "https_proxy" , "HTTPS_PROXY" , "no_proxy" , "NO_PROXY" } {
212
216
assert .Assert (t , is .Contains (string (logs ), "name=" + v ))
217
+ assert .Assert (t , ! strings .Contains (string (logs ), userPass ), "logs should not contain the non-sanitized proxy URL: %s" , string (logs ))
213
218
}
214
219
215
220
c := d .NewClientT (t )
@@ -235,10 +240,10 @@ func TestDaemonProxy(t *testing.T) {
235
240
236
241
// Configure proxy through configuration file
237
242
t .Run ("configuration file" , func (t * testing.T ) {
238
- defer env .Patch (t , "HTTP_PROXY" , "http://from-env-http.invalid" )()
239
- defer env .Patch (t , "http_proxy" , "http://from-env-http.invalid" )()
240
- defer env .Patch (t , "HTTPS_PROXY" , "https://from-env-https.invalid" )()
241
- defer env .Patch (t , "https_proxy" , "https://from-env-http .invalid" )()
243
+ defer env .Patch (t , "HTTP_PROXY" , "http://" + userPass + " from-env-http.invalid" )()
244
+ defer env .Patch (t , "http_proxy" , "http://" + userPass + " from-env-http.invalid" )()
245
+ defer env .Patch (t , "HTTPS_PROXY" , "https://" + userPass + "myuser:mypassword@ from-env-https.invalid" )()
246
+ defer env .Patch (t , "https_proxy" , "https://" + userPass + "myuser:mypassword@ from-env-https .invalid" )()
242
247
defer env .Patch (t , "NO_PROXY" , "ignore.invalid" )()
243
248
defer env .Patch (t , "no_proxy" , "ignore.invalid" )()
244
249
@@ -258,6 +263,7 @@ func TestDaemonProxy(t *testing.T) {
258
263
assert .Assert (t , is .Contains (string (logs ), "overriding existing proxy variable with value from configuration" ))
259
264
for _ , v := range []string {"http_proxy" , "HTTP_PROXY" , "https_proxy" , "HTTPS_PROXY" , "no_proxy" , "NO_PROXY" } {
260
265
assert .Assert (t , is .Contains (string (logs ), "name=" + v ))
266
+ assert .Assert (t , ! strings .Contains (string (logs ), userPass ), "logs should not contain the non-sanitized proxy URL: %s" , string (logs ))
261
267
}
262
268
263
269
_ , err = c .ImagePull (ctx , "example.org:5002/some/image:latest" , types.ImagePullOptions {})
@@ -280,7 +286,8 @@ func TestDaemonProxy(t *testing.T) {
280
286
// Conflicting options (passed both through command-line options and config file)
281
287
t .Run ("conflicting options" , func (t * testing.T ) {
282
288
const (
283
- proxyRawURL = "https://myuser:[email protected] "
289
+ proxyRawURL = "https://" + userPass + "example.org"
290
+ proxyURL = "https://xxxxx:[email protected] "
284
291
)
285
292
286
293
d := daemon .New (t )
@@ -295,8 +302,38 @@ func TestDaemonProxy(t *testing.T) {
295
302
assert .NilError (t , err )
296
303
expected := fmt .Sprintf (
297
304
`the following directives are specified both as a flag and in the configuration file: http-proxy: (from flag: %[1]s, from file: %[1]s), https-proxy: (from flag: %[1]s, from file: %[1]s), no-proxy: (from flag: example.com, from file: example.com)` ,
298
- proxyRawURL ,
305
+ proxyURL ,
299
306
)
300
307
assert .Assert (t , is .Contains (string (logs ), expected ))
301
308
})
309
+
310
+ // Make sure values are sanitized when reloading the daemon-config
311
+ t .Run ("reload sanitized" , func (t * testing.T ) {
312
+ const (
313
+ proxyRawURL = "https://" + userPass + "example.org"
314
+ proxyURL = "https://xxxxx:[email protected] "
315
+ )
316
+
317
+ d := daemon .New (t )
318
+ d .Start (t , "--http-proxy" , proxyRawURL , "--https-proxy" , proxyRawURL , "--no-proxy" , "example.com" )
319
+ defer d .Stop (t )
320
+ err := d .Signal (syscall .SIGHUP )
321
+ assert .NilError (t , err )
322
+
323
+ logs , err := d .ReadLogFile ()
324
+ assert .NilError (t , err )
325
+
326
+ // FIXME: there appears to ba a race condition, which causes ReadLogFile
327
+ // to not contain the full logs after signaling the daemon to reload,
328
+ // causing the test to fail here. As a workaround, check if we
329
+ // received the "reloaded" message after signaling, and only then
330
+ // check that it's sanitized properly. For more details on this
331
+ // issue, see https://github.com/moby/moby/pull/42835/files#r713120315
332
+ if ! strings .Contains (string (logs ), "Reloaded configuration:" ) {
333
+ t .Skip ("Skipping test, because we did not find 'Reloaded configuration' in the logs" )
334
+ }
335
+
336
+ assert .Assert (t , is .Contains (string (logs ), proxyURL ))
337
+ assert .Assert (t , ! strings .Contains (string (logs ), userPass ), "logs should not contain the non-sanitized proxy URL: %s" , string (logs ))
338
+ })
302
339
}
0 commit comments