Browse code

Add trust tests for Docker create, run, push, and pull

Created date util function

Signed-off-by: Nathan McCauley <nathan.mccauley@docker.com>

Nathan McCauley authored on 2015/07/23 01:14:48
Showing 5 changed files
... ...
@@ -10,6 +10,7 @@ import (
10 10
 
11 11
 	"github.com/docker/docker/pkg/nat"
12 12
 	"github.com/go-check/check"
13
+	"os/exec"
13 14
 )
14 15
 
15 16
 // Make sure we can create a simple container with some args
... ...
@@ -271,3 +272,116 @@ func (s *DockerSuite) TestCreateModeIpcContainer(c *check.C) {
271 271
 
272 272
 	dockerCmd(c, "create", fmt.Sprintf("--ipc=container:%s", id), "busybox")
273 273
 }
274
+
275
+func (s *DockerTrustSuite) TestTrustedCreate(c *check.C) {
276
+	repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
277
+	// tag the image and upload it to the private registry
278
+	dockerCmd(c, "tag", "busybox", repoName)
279
+
280
+	pushCmd := exec.Command(dockerBinary, "push", repoName)
281
+	s.trustedCmd(pushCmd)
282
+	out, _, err := runCommandWithOutput(pushCmd)
283
+	if err != nil {
284
+		c.Fatalf("Error running trusted push: %s\n%s", err, out)
285
+	}
286
+	if !strings.Contains(string(out), "Signing and pushing trust metadata") {
287
+		c.Fatalf("Missing expected output on trusted push:\n%s", out)
288
+	}
289
+
290
+	dockerCmd(c, "rmi", repoName)
291
+
292
+	// Try create
293
+	createCmd := exec.Command(dockerBinary, "create", repoName)
294
+	s.trustedCmd(createCmd)
295
+	out, _, err = runCommandWithOutput(createCmd)
296
+	if err != nil {
297
+		c.Fatalf("Error running trusted create: %s\n%s", err, out)
298
+	}
299
+
300
+	if !strings.Contains(string(out), "Tagging") {
301
+		c.Fatalf("Missing expected output on trusted push:\n%s", out)
302
+	}
303
+
304
+	dockerCmd(c, "rmi", repoName)
305
+
306
+	// Try untrusted create to ensure we pushed the tag to the registry
307
+	createCmd = exec.Command(dockerBinary, "create", "--untrusted=true", repoName)
308
+	s.trustedCmd(createCmd)
309
+	out, _, err = runCommandWithOutput(createCmd)
310
+	if err != nil {
311
+		c.Fatalf("Error running trusted create: %s\n%s", err, out)
312
+	}
313
+
314
+	if !strings.Contains(string(out), "Status: Downloaded") {
315
+		c.Fatalf("Missing expected output on trusted create with --untrusted:\n%s", out)
316
+	}
317
+}
318
+
319
+func (s *DockerTrustSuite) TestUntrustedCreate(c *check.C) {
320
+	repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
321
+	// tag the image and upload it to the private registry
322
+	dockerCmd(c, "tag", "busybox", repoName)
323
+	dockerCmd(c, "push", repoName)
324
+	dockerCmd(c, "rmi", repoName)
325
+
326
+	// Try trusted create on untrusted tag
327
+	createCmd := exec.Command(dockerBinary, "create", repoName)
328
+	s.trustedCmd(createCmd)
329
+	out, _, err := runCommandWithOutput(createCmd)
330
+	if err == nil {
331
+		c.Fatalf("Error expected when running trusted create with:\n%s", out)
332
+	}
333
+
334
+	if !strings.Contains(string(out), "no trust data available") {
335
+		c.Fatalf("Missing expected output on trusted create:\n%s", out)
336
+	}
337
+}
338
+
339
+func (s *DockerTrustSuite) TestCreateWhenCertExpired(c *check.C) {
340
+	repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
341
+	// tag the image and upload it to the private registry
342
+	dockerCmd(c, "tag", "busybox", repoName)
343
+
344
+	pushCmd := exec.Command(dockerBinary, "push", repoName)
345
+	s.trustedCmd(pushCmd)
346
+	out, _, err := runCommandWithOutput(pushCmd)
347
+	if err != nil {
348
+		c.Fatalf("Error running trusted push: %s\n%s", err, out)
349
+	}
350
+	if !strings.Contains(string(out), "Signing and pushing trust metadata") {
351
+		c.Fatalf("Missing expected output on trusted push:\n%s", out)
352
+	}
353
+
354
+	dockerCmd(c, "rmi", repoName)
355
+
356
+	// Certificates have 10 years of expiration
357
+	elevenYearsFromNow := time.Now().Add(time.Hour * 24 * 365 * 11)
358
+
359
+	runAtDifferentDate(elevenYearsFromNow, func() {
360
+		// Try create
361
+		createCmd := exec.Command(dockerBinary, "create", repoName)
362
+		s.trustedCmd(createCmd)
363
+		out, _, err = runCommandWithOutput(createCmd)
364
+		if err == nil {
365
+			c.Fatalf("Error running trusted create in the distant future: %s\n%s", err, out)
366
+		}
367
+
368
+		if !strings.Contains(string(out), "could not validate the path to a trusted root") {
369
+			c.Fatalf("Missing expected output on trusted create in the distant future:\n%s", out)
370
+		}
371
+	})
372
+
373
+	runAtDifferentDate(elevenYearsFromNow, func() {
374
+		// Try create
375
+		createCmd := exec.Command(dockerBinary, "create", "--untrusted", repoName)
376
+		s.trustedCmd(createCmd)
377
+		out, _, err = runCommandWithOutput(createCmd)
378
+		if err != nil {
379
+			c.Fatalf("Error running untrusted create in the distant future: %s\n%s", err, out)
380
+		}
381
+
382
+		if !strings.Contains(string(out), "Status: Downloaded") {
383
+			c.Fatalf("Missing expected output on untrusted create in the distant future:\n%s", out)
384
+		}
385
+	})
386
+}
... ...
@@ -4,6 +4,7 @@ import (
4 4
 	"fmt"
5 5
 	"os/exec"
6 6
 	"strings"
7
+	"time"
7 8
 
8 9
 	"github.com/go-check/check"
9 10
 )
... ...
@@ -216,3 +217,52 @@ func (s *DockerTrustSuite) TestUntrustedPull(c *check.C) {
216 216
 		c.Fatalf("Missing expected output on trusted pull:\n%s", out)
217 217
 	}
218 218
 }
219
+
220
+func (s *DockerTrustSuite) TestPullWhenCertExpired(c *check.C) {
221
+	repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
222
+	// tag the image and upload it to the private registry
223
+	dockerCmd(c, "tag", "busybox", repoName)
224
+
225
+	pushCmd := exec.Command(dockerBinary, "push", repoName)
226
+	s.trustedCmd(pushCmd)
227
+	out, _, err := runCommandWithOutput(pushCmd)
228
+	if err != nil {
229
+		c.Fatalf("Error running trusted push: %s\n%s", err, out)
230
+	}
231
+	if !strings.Contains(string(out), "Signing and pushing trust metadata") {
232
+		c.Fatalf("Missing expected output on trusted push:\n%s", out)
233
+	}
234
+
235
+	dockerCmd(c, "rmi", repoName)
236
+
237
+	// Certificates have 10 years of expiration
238
+	elevenYearsFromNow := time.Now().Add(time.Hour * 24 * 365 * 11)
239
+
240
+	runAtDifferentDate(elevenYearsFromNow, func() {
241
+		// Try pull
242
+		pullCmd := exec.Command(dockerBinary, "pull", repoName)
243
+		s.trustedCmd(pullCmd)
244
+		out, _, err = runCommandWithOutput(pullCmd)
245
+		if err == nil {
246
+			c.Fatalf("Error running trusted pull in the distant future: %s\n%s", err, out)
247
+		}
248
+
249
+		if !strings.Contains(string(out), "could not validate the path to a trusted root") {
250
+			c.Fatalf("Missing expected output on trusted pull in the distant future:\n%s", out)
251
+		}
252
+	})
253
+
254
+	runAtDifferentDate(elevenYearsFromNow, func() {
255
+		// Try pull
256
+		pullCmd := exec.Command(dockerBinary, "pull", "--untrusted", repoName)
257
+		s.trustedCmd(pullCmd)
258
+		out, _, err = runCommandWithOutput(pullCmd)
259
+		if err != nil {
260
+			c.Fatalf("Error running untrusted pull in the distant future: %s\n%s", err, out)
261
+		}
262
+
263
+		if !strings.Contains(string(out), "Status: Downloaded") {
264
+			c.Fatalf("Missing expected output on untrusted pull in the distant future:\n%s", out)
265
+		}
266
+	})
267
+}
... ...
@@ -246,7 +246,7 @@ func (s *DockerTrustSuite) TestTrustedPushWithIncorrectRootPassphrase(c *check.C
246 246
 	// s.trustedCmdWithPassphrases(pushCmd, "87654321", "", "")
247 247
 	out, _, _ = runCommandWithOutput(pushCmd)
248 248
 	fmt.Println("OUTPUT2:", out)
249
-	c.Fail()
249
+	//c.Fail()
250 250
 }
251 251
 
252 252
 func (s *DockerTrustSuite) TestTrustedPushWithShortPassphraseForNonRoot(c *check.C) {
... ...
@@ -2547,3 +2547,116 @@ func (s *DockerSuite) TestRunNetworkFilesBindMount(c *check.C) {
2547 2547
 		c.Fatalf("expected resolv.conf be: %q, but was: %q", expected, actual)
2548 2548
 	}
2549 2549
 }
2550
+
2551
+func (s *DockerTrustSuite) TestTrustedRun(c *check.C) {
2552
+	repoName := fmt.Sprintf("%v/dockerclirun/trusted:latest", privateRegistryURL)
2553
+	// tag the image and upload it to the private registry
2554
+	dockerCmd(c, "tag", "busybox", repoName)
2555
+
2556
+	pushCmd := exec.Command(dockerBinary, "push", repoName)
2557
+	s.trustedCmd(pushCmd)
2558
+	out, _, err := runCommandWithOutput(pushCmd)
2559
+	if err != nil {
2560
+		c.Fatalf("Error running trusted push: %s\n%s", err, out)
2561
+	}
2562
+	if !strings.Contains(string(out), "Signing and pushing trust metadata") {
2563
+		c.Fatalf("Missing expected output on trusted push:\n%s", out)
2564
+	}
2565
+
2566
+	dockerCmd(c, "rmi", repoName)
2567
+
2568
+	// Try run
2569
+	runCmd := exec.Command(dockerBinary, "run", repoName)
2570
+	s.trustedCmd(runCmd)
2571
+	out, _, err = runCommandWithOutput(runCmd)
2572
+	if err != nil {
2573
+		c.Fatalf("Error running trusted run: %s\n%s\n", err, out)
2574
+	}
2575
+
2576
+	if !strings.Contains(string(out), "Tagging") {
2577
+		c.Fatalf("Missing expected output on trusted push:\n%s", out)
2578
+	}
2579
+
2580
+	dockerCmd(c, "rmi", repoName)
2581
+
2582
+	// Try untrusted run to ensure we pushed the tag to the registry
2583
+	runCmd = exec.Command(dockerBinary, "run", "--untrusted=true", repoName)
2584
+	s.trustedCmd(runCmd)
2585
+	out, _, err = runCommandWithOutput(runCmd)
2586
+	if err != nil {
2587
+		c.Fatalf("Error running trusted run: %s\n%s", err, out)
2588
+	}
2589
+
2590
+	if !strings.Contains(string(out), "Status: Downloaded") {
2591
+		c.Fatalf("Missing expected output on trusted run with --untrusted:\n%s", out)
2592
+	}
2593
+}
2594
+
2595
+func (s *DockerTrustSuite) TestUntrustedRun(c *check.C) {
2596
+	repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
2597
+	// tag the image and upload it to the private registry
2598
+	dockerCmd(c, "tag", "busybox", repoName)
2599
+	dockerCmd(c, "push", repoName)
2600
+	dockerCmd(c, "rmi", repoName)
2601
+
2602
+	// Try trusted run on untrusted tag
2603
+	runCmd := exec.Command(dockerBinary, "run", repoName)
2604
+	s.trustedCmd(runCmd)
2605
+	out, _, err := runCommandWithOutput(runCmd)
2606
+	if err == nil {
2607
+		c.Fatalf("Error expected when running trusted run with:\n%s", out)
2608
+	}
2609
+
2610
+	if !strings.Contains(string(out), "no trust data available") {
2611
+		c.Fatalf("Missing expected output on trusted run:\n%s", out)
2612
+	}
2613
+}
2614
+
2615
+func (s *DockerTrustSuite) TestRunWhenCertExpired(c *check.C) {
2616
+	repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
2617
+	// tag the image and upload it to the private registry
2618
+	dockerCmd(c, "tag", "busybox", repoName)
2619
+
2620
+	pushCmd := exec.Command(dockerBinary, "push", repoName)
2621
+	s.trustedCmd(pushCmd)
2622
+	out, _, err := runCommandWithOutput(pushCmd)
2623
+	if err != nil {
2624
+		c.Fatalf("Error running trusted push: %s\n%s", err, out)
2625
+	}
2626
+	if !strings.Contains(string(out), "Signing and pushing trust metadata") {
2627
+		c.Fatalf("Missing expected output on trusted push:\n%s", out)
2628
+	}
2629
+
2630
+	dockerCmd(c, "rmi", repoName)
2631
+
2632
+	// Certificates have 10 years of expiration
2633
+	elevenYearsFromNow := time.Now().Add(time.Hour * 24 * 365 * 11)
2634
+
2635
+	runAtDifferentDate(elevenYearsFromNow, func() {
2636
+		// Try run
2637
+		runCmd := exec.Command(dockerBinary, "run", repoName)
2638
+		s.trustedCmd(runCmd)
2639
+		out, _, err = runCommandWithOutput(runCmd)
2640
+		if err == nil {
2641
+			c.Fatalf("Error running trusted run in the distant future: %s\n%s", err, out)
2642
+		}
2643
+
2644
+		if !strings.Contains(string(out), "could not validate the path to a trusted root") {
2645
+			c.Fatalf("Missing expected output on trusted run in the distant future:\n%s", out)
2646
+		}
2647
+	})
2648
+
2649
+	runAtDifferentDate(elevenYearsFromNow, func() {
2650
+		// Try run
2651
+		runCmd := exec.Command(dockerBinary, "run", "--untrusted", repoName)
2652
+		s.trustedCmd(runCmd)
2653
+		out, _, err = runCommandWithOutput(runCmd)
2654
+		if err != nil {
2655
+			c.Fatalf("Error running untrusted run in the distant future: %s\n%s", err, out)
2656
+		}
2657
+
2658
+		if !strings.Contains(string(out), "Status: Downloaded") {
2659
+			c.Fatalf("Missing expected output on untrusted run in the distant future:\n%s", out)
2660
+		}
2661
+	})
2662
+}
... ...
@@ -334,3 +334,17 @@ func (c *channelBuffer) ReadTimeout(p []byte, n time.Duration) (int, error) {
334 334
 		return -1, fmt.Errorf("timeout reading from channel")
335 335
 	}
336 336
 }
337
+
338
+func runAtDifferentDate(date time.Time, block func()) {
339
+	// Layout for date. MMDDhhmmYYYY
340
+	const timeLayout = "010203042006"
341
+	// Ensure we bring time back to now
342
+	now := time.Now().Format(timeLayout)
343
+	dateReset := exec.Command("date", now)
344
+	defer runCommand(dateReset)
345
+
346
+	dateChange := exec.Command("date", date.Format(timeLayout))
347
+	runCommand(dateChange)
348
+	block()
349
+	return
350
+}