Browse code

Merge pull request #20509 from estesp/cleanup-authz-test

Clean up authz integration-cli test

Antonio Murdaca authored on 2016/02/20 20:10:29
Showing 2 changed files
... ...
@@ -30,6 +30,10 @@ const (
30 30
 	containerListAPI    = "/containers/json"
31 31
 )
32 32
 
33
+var (
34
+	alwaysAllowed = []string{"/_ping", "/info"}
35
+)
36
+
33 37
 func init() {
34 38
 	check.Suite(&DockerAuthzSuite{
35 39
 		ds: &DockerSuite{},
... ...
@@ -74,12 +78,6 @@ func (s *DockerAuthzSuite) SetUpSuite(c *check.C) {
74 74
 	})
75 75
 
76 76
 	mux.HandleFunc("/AuthZPlugin.AuthZReq", func(w http.ResponseWriter, r *http.Request) {
77
-		if s.ctrl.reqRes.Err != "" {
78
-			w.WriteHeader(http.StatusInternalServerError)
79
-		}
80
-		b, err := json.Marshal(s.ctrl.reqRes)
81
-		c.Assert(err, check.IsNil)
82
-		w.Write(b)
83 77
 		defer r.Body.Close()
84 78
 		body, err := ioutil.ReadAll(r.Body)
85 79
 		c.Assert(err, check.IsNil)
... ...
@@ -96,16 +94,20 @@ func (s *DockerAuthzSuite) SetUpSuite(c *check.C) {
96 96
 		}
97 97
 
98 98
 		s.ctrl.requestsURIs = append(s.ctrl.requestsURIs, authReq.RequestURI)
99
-	})
100 99
 
101
-	mux.HandleFunc("/AuthZPlugin.AuthZRes", func(w http.ResponseWriter, r *http.Request) {
102
-		if s.ctrl.resRes.Err != "" {
100
+		reqRes := s.ctrl.reqRes
101
+		if isAllowed(authReq.RequestURI) {
102
+			reqRes = authorization.Response{Allow: true}
103
+		}
104
+		if reqRes.Err != "" {
103 105
 			w.WriteHeader(http.StatusInternalServerError)
104 106
 		}
105
-		b, err := json.Marshal(s.ctrl.resRes)
107
+		b, err := json.Marshal(reqRes)
106 108
 		c.Assert(err, check.IsNil)
107 109
 		w.Write(b)
110
+	})
108 111
 
112
+	mux.HandleFunc("/AuthZPlugin.AuthZRes", func(w http.ResponseWriter, r *http.Request) {
109 113
 		defer r.Body.Close()
110 114
 		body, err := ioutil.ReadAll(r.Body)
111 115
 		c.Assert(err, check.IsNil)
... ...
@@ -120,6 +122,16 @@ func (s *DockerAuthzSuite) SetUpSuite(c *check.C) {
120 120
 		if strings.HasSuffix(authReq.RequestURI, containerListAPI) {
121 121
 			s.ctrl.psResponseCnt++
122 122
 		}
123
+		resRes := s.ctrl.resRes
124
+		if isAllowed(authReq.RequestURI) {
125
+			resRes = authorization.Response{Allow: true}
126
+		}
127
+		if resRes.Err != "" {
128
+			w.WriteHeader(http.StatusInternalServerError)
129
+		}
130
+		b, err := json.Marshal(resRes)
131
+		c.Assert(err, check.IsNil)
132
+		w.Write(b)
123 133
 	})
124 134
 
125 135
 	err := os.MkdirAll("/etc/docker/plugins", 0755)
... ...
@@ -130,6 +142,16 @@ func (s *DockerAuthzSuite) SetUpSuite(c *check.C) {
130 130
 	c.Assert(err, checker.IsNil)
131 131
 }
132 132
 
133
+// check for always allowed endpoints to not inhibit test framework functions
134
+func isAllowed(reqURI string) bool {
135
+	for _, endpoint := range alwaysAllowed {
136
+		if strings.HasSuffix(reqURI, endpoint) {
137
+			return true
138
+		}
139
+	}
140
+	return false
141
+}
142
+
133 143
 // assertAuthHeaders validates authentication headers are removed
134 144
 func assertAuthHeaders(c *check.C, headers map[string]string) error {
135 145
 	for k := range headers {
... ...
@@ -171,13 +193,10 @@ func (s *DockerAuthzSuite) TearDownSuite(c *check.C) {
171 171
 func (s *DockerAuthzSuite) TestAuthZPluginAllowRequest(c *check.C) {
172 172
 	// start the daemon and load busybox, --net=none build fails otherwise
173 173
 	// cause it needs to pull busybox
174
-	c.Assert(s.d.StartWithBusybox(), check.IsNil)
175
-	// restart the daemon and enable the plugin, otherwise busybox loading
176
-	// is blocked by the plugin itself
177
-	c.Assert(s.d.Restart("--authorization-plugin="+testAuthZPlugin), check.IsNil)
178
-
174
+	c.Assert(s.d.Start("--authorization-plugin="+testAuthZPlugin), check.IsNil)
179 175
 	s.ctrl.reqRes.Allow = true
180 176
 	s.ctrl.resRes.Allow = true
177
+	c.Assert(s.d.LoadBusybox(), check.IsNil)
181 178
 
182 179
 	// Ensure command successful
183 180
 	out, err := s.d.Cmd("run", "-d", "busybox", "top")
... ...
@@ -234,12 +253,10 @@ func (s *DockerAuthzSuite) TestAuthZPluginAllowEventStream(c *check.C) {
234 234
 	testRequires(c, DaemonIsLinux)
235 235
 
236 236
 	// start the daemon and load busybox to avoid pulling busybox from Docker Hub
237
-	c.Assert(s.d.StartWithBusybox(), check.IsNil)
238
-	// restart the daemon and enable the authorization plugin, otherwise busybox loading
239
-	// is blocked by the plugin itself
240
-	c.Assert(s.d.Restart("--authorization-plugin="+testAuthZPlugin), check.IsNil)
237
+	c.Assert(s.d.Start("--authorization-plugin="+testAuthZPlugin), check.IsNil)
241 238
 	s.ctrl.reqRes.Allow = true
242 239
 	s.ctrl.resRes.Allow = true
240
+	c.Assert(s.d.LoadBusybox(), check.IsNil)
243 241
 
244 242
 	startTime := strconv.FormatInt(daemonTime(c).Unix(), 10)
245 243
 	// Add another command to to enable event pipelining
... ...
@@ -321,24 +321,7 @@ func (d *Daemon) StartWithBusybox(arg ...string) error {
321 321
 	if err := d.Start(arg...); err != nil {
322 322
 		return err
323 323
 	}
324
-	bb := filepath.Join(d.folder, "busybox.tar")
325
-	if _, err := os.Stat(bb); err != nil {
326
-		if !os.IsNotExist(err) {
327
-			return fmt.Errorf("unexpected error on busybox.tar stat: %v", err)
328
-		}
329
-		// saving busybox image from main daemon
330
-		if err := exec.Command(dockerBinary, "save", "--output", bb, "busybox:latest").Run(); err != nil {
331
-			return fmt.Errorf("could not save busybox image: %v", err)
332
-		}
333
-	}
334
-	// loading busybox image to this daemon
335
-	if out, err := d.Cmd("load", "--input", bb); err != nil {
336
-		return fmt.Errorf("could not load busybox image: %s", out)
337
-	}
338
-	if err := os.Remove(bb); err != nil {
339
-		d.c.Logf("could not remove %s: %v", bb, err)
340
-	}
341
-	return nil
324
+	return d.LoadBusybox()
342 325
 }
343 326
 
344 327
 // Stop will send a SIGINT every second and wait for the daemon to stop.
... ...
@@ -413,6 +396,28 @@ func (d *Daemon) Restart(arg ...string) error {
413 413
 	return d.Start(arg...)
414 414
 }
415 415
 
416
+// LoadBusybox will load the stored busybox into a newly started daemon
417
+func (d *Daemon) LoadBusybox() error {
418
+	bb := filepath.Join(d.folder, "busybox.tar")
419
+	if _, err := os.Stat(bb); err != nil {
420
+		if !os.IsNotExist(err) {
421
+			return fmt.Errorf("unexpected error on busybox.tar stat: %v", err)
422
+		}
423
+		// saving busybox image from main daemon
424
+		if err := exec.Command(dockerBinary, "save", "--output", bb, "busybox:latest").Run(); err != nil {
425
+			return fmt.Errorf("could not save busybox image: %v", err)
426
+		}
427
+	}
428
+	// loading busybox image to this daemon
429
+	if out, err := d.Cmd("load", "--input", bb); err != nil {
430
+		return fmt.Errorf("could not load busybox image: %s", out)
431
+	}
432
+	if err := os.Remove(bb); err != nil {
433
+		d.c.Logf("could not remove %s: %v", bb, err)
434
+	}
435
+	return nil
436
+}
437
+
416 438
 func (d *Daemon) queryRootDir() (string, error) {
417 439
 	// update daemon root by asking /info endpoint (to support user
418 440
 	// namespaced daemon with root remapped uid.gid directory)