@@ -289,12 +289,13 @@ def test_flags_s_and_x_combined(self):
289289 finally :
290290 self ._delete_for_cleanup (ns )
291291
292- def test_consumer_cannot_create_pod_in_other_namespace (self ):
292+ def test_consumer_can_create_deployment_but_not_pod (self ):
293293 """
294- Consumer kubeconfig: verify creating a pod in another namespace is forbidden.
294+ Consumer kubeconfig: verify allowed and disallowed creates:
295+ - create deployment in own namespace succeeds
296+ - create pod in own namespace is forbidden
295297 """
296298 ns = "kubeplus-test-restrict-" + uuid .uuid4 ().hex [:8 ]
297- other_ns = "kubeplus-test-other-" + uuid .uuid4 ().hex [:8 ]
298299 consumer_sa = "test-consumer-restrict"
299300 kubeconfig_path = os .path .join (ROOT , consumer_sa + ".json" )
300301 api_server = self ._current_cluster_server ()
@@ -304,30 +305,37 @@ def test_consumer_cannot_create_pod_in_other_namespace(self):
304305 self .assertEqual (proc .returncode , 0 , proc .stderr )
305306 self ._assert_kubeconfig_valid (cfg , expected_namespace = ns , expected_user_name = consumer_sa )
306307
307- # Create another namespace (as admin)
308- _run_command ("kubectl create namespace " + other_ns + self .kubeconfig_flag )
308+ # Verify create works in own namespace.
309+ own_name = "consumer-own-" + uuid .uuid4 ().hex [:6 ]
310+ own_out , own_err = _run_command (
311+ "kubectl create deployment " + own_name + " --image=nginx -n " + ns
312+ + " --kubeconfig=" + kubeconfig_path
313+ )
314+ own_conn_err = "unable to connect to the server" in own_err .lower () or "i/o timeout" in own_err .lower ()
315+ if own_conn_err :
316+ self .skipTest ("Skipping authz assertion due to transient API connectivity issue: " + own_err .strip ())
317+ self .assertTrue (
318+ "created" in own_out .lower (),
319+ "Consumer should be able to create deployment in own namespace; got out=%r err=%r"
320+ % (own_out , own_err ),
321+ )
309322
310- # Try to create pod in other namespace using consumer kubeconfig (should fail)
323+ # Verify pod create is forbidden for consumer.
324+ pod_name = "consumer-pod-" + uuid .uuid4 ().hex [:6 ]
311325 out , err = _run_command (
312- "kubectl run nginx --image=nginx -n " + other_ns
326+ "kubectl run " + pod_name + " --image=nginx -n " + ns
313327 + " --kubeconfig=" + kubeconfig_path
314328 )
315329 conn_err = "unable to connect to the server" in err .lower () or "i/o timeout" in err .lower ()
316330 if conn_err :
317331 self .skipTest ("Skipping authz assertion due to transient API connectivity issue: " + err .strip ())
318- # Expect Forbidden (authorization denial), not generic errors (DNS, image pull, etc.)
319332 self .assertTrue (
320333 "forbidden" in err .lower (),
321- "Consumer should not be able to create pod in other namespace ; got out=%r err=%r"
334+ "Consumer should not be able to create pods ; got out=%r err=%r"
322335 % (out , err ),
323336 )
324- self .assertIn (
325- other_ns ,
326- err ,
327- "Expected denial to reference the target other namespace; got err=%r" % (err ,),
328- )
329337 finally :
330- _run_command ("kubectl delete namespace " + other_ns + self .kubeconfig_flag + " 2>/dev/null" )
338+ _run_command ("kubectl delete deployment --all -n " + ns + self .kubeconfig_flag + " 2>/dev/null" )
331339 self ._delete_for_cleanup (ns , sa = consumer_sa )
332340
333341
0 commit comments