hsheinblatt commented on code in PR #1407:
URL: https://github.com/apache/knox/pull/1407#discussion_r4031035942


##########
.github/workflows/tests/test_token_exchange.py:
##########
@@ -210,6 +276,83 @@ def 
test_same_subject_exchange_succeeds_on_delegation_enabled_topology(self):
         self.assertEqual(body.get("issued_token_type"), ISSUED_TOKEN_TYPE_JWT, 
response.text)
         self.assertEqual(get_token_claim(body["access_token"], "sub"), 
GUEST_USER)
 
+    # ---- KNOX-3466: same-subject requested-audience authorization (KNOX-3461 
code) ----
+    # These exercise delegation.same.subject.requested.audience.enabled and 
therefore fail
+    # until the KNOX-3461 gateway code is present in the image.
+
+    def test_same_subject_requested_audience_authorized_succeeds(self):
+        """Flag on: a requested audience the subject token already carries is 
honored and minted."""
+        subject_token = self._mint_subject_token_with_aud()
+        # Precondition: the subject token actually carries the audience we 
will request.
+        self.assertIn(SUBJECT_AUDIENCE, self._aud_values(subject_token),
+                      "precondition: subject token must carry the requested 
audience in its aud")
+
+        response = self._exchange(
+            self.same_subject_aud_exchange_url, subject_token, 
resources=[SUBJECT_AUDIENCE],
+        )
+        self.assertEqual(response.status_code, 200, response.text)
+        exchanged = response.json().get("access_token")
+        self.assertTrue(exchanged, "exchange did not return an access_token")
+        self.assertEqual(get_token_claim(exchanged, "sub"), GUEST_USER)
+        # The authorized audience is passed through onto the exchanged token.
+        self.assertIn(SUBJECT_AUDIENCE, self._aud_values(exchanged),

Review Comment:
   I'd assert the sets are equal: it's possible if the subject token had 
multiple aud claims to just duplicate all of them, ignore the request, and pass 
this validation.



##########
.github/workflows/tests/test_token_exchange.py:
##########
@@ -210,6 +276,83 @@ def 
test_same_subject_exchange_succeeds_on_delegation_enabled_topology(self):
         self.assertEqual(body.get("issued_token_type"), ISSUED_TOKEN_TYPE_JWT, 
response.text)
         self.assertEqual(get_token_claim(body["access_token"], "sub"), 
GUEST_USER)
 
+    # ---- KNOX-3466: same-subject requested-audience authorization (KNOX-3461 
code) ----
+    # These exercise delegation.same.subject.requested.audience.enabled and 
therefore fail
+    # until the KNOX-3461 gateway code is present in the image.
+
+    def test_same_subject_requested_audience_authorized_succeeds(self):
+        """Flag on: a requested audience the subject token already carries is 
honored and minted."""
+        subject_token = self._mint_subject_token_with_aud()
+        # Precondition: the subject token actually carries the audience we 
will request.
+        self.assertIn(SUBJECT_AUDIENCE, self._aud_values(subject_token),
+                      "precondition: subject token must carry the requested 
audience in its aud")
+
+        response = self._exchange(
+            self.same_subject_aud_exchange_url, subject_token, 
resources=[SUBJECT_AUDIENCE],
+        )
+        self.assertEqual(response.status_code, 200, response.text)
+        exchanged = response.json().get("access_token")
+        self.assertTrue(exchanged, "exchange did not return an access_token")
+        self.assertEqual(get_token_claim(exchanged, "sub"), GUEST_USER)
+        # The authorized audience is passed through onto the exchanged token.
+        self.assertIn(SUBJECT_AUDIENCE, self._aud_values(exchanged),
+                      "authorized requested audience must appear on the 
exchanged token")
+
+    def test_same_subject_requested_audience_unauthorized_rejected(self):
+        """Flag on: a requested audience the subject token does not carry is 
rejected."""
+        subject_token = self._mint_subject_token_with_aud()
+        self.assertNotIn(UNAUTHORIZED_AUDIENCE, 
self._aud_values(subject_token),

Review Comment:
   nit: I'd also assert it has some audience. This is a reasonable 
representative case either way, but arguably the boundary test where the 
subject token has no audiences would also pass this test but be a separate 
case. Actually, that's the below case already, so it would be good to 
disinguish them.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to