-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add subjectAccessReview test for resource attributes
Signed-off-by: averevki <[email protected]>
- Loading branch information
Showing
4 changed files
with
92 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...ts/singlecluster/authorino/identity/subject_access_review/test_non_resource_attributes.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"""Test kubernetes SubjectAccessReview non-resource attributes authorization by verifying only a | ||
ServiceAccount bound to a ClusterRole is authorized to access a resource""" | ||
|
||
import pytest | ||
|
||
from testsuite.kuadrant.policy.authorization import ValueFrom | ||
from testsuite.kubernetes.cluster_role import ClusterRole, Rule | ||
|
||
pytestmark = [pytest.mark.authorino] | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def authorization(authorization): | ||
"""Add kubernetes subject-access-review authorization with non-resource attributes (omit resource_attributes)""" | ||
authorization.authorization.add_kubernetes( | ||
"subject-access-review-username", ValueFrom("auth.identity.user.username") | ||
) | ||
return authorization | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def cluster_role(request, cluster, blame, module_label): | ||
"""Creates and returns a ClusterRole""" | ||
rules = [Rule(verbs=["get"], nonResourceURLs=["/get"])] | ||
cluster_role = ClusterRole.create_instance(cluster, blame("cr"), rules, labels={"app": module_label}) | ||
request.addfinalizer(cluster_role.delete) | ||
cluster_role.commit() | ||
return cluster_role | ||
|
||
|
||
def test_subject_access_review_non_resource_attributes(client, auth, auth2): | ||
"""Test Kubernetes SubjectAccessReview functionality by setting up authentication and authorization for an endpoint | ||
and querying it with authorized and non-authorized ServiceAccount.""" | ||
response = client.get("/get", auth=auth) | ||
assert response.status_code == 200 | ||
|
||
response = client.get("/get", auth=auth2) | ||
assert response.status_code == 403 |
44 changes: 44 additions & 0 deletions
44
.../tests/singlecluster/authorino/identity/subject_access_review/test_resource_attributes.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""Test kubernetes SubjectAccessReview with resource attributes""" | ||
|
||
import pytest | ||
|
||
from testsuite.kuadrant.policy import CelExpression | ||
from testsuite.kuadrant.policy.authorization import ValueFrom, Value, ResourceAttributes | ||
from testsuite.kubernetes.cluster_role import ClusterRole, Rule | ||
|
||
pytestmark = [pytest.mark.authorino] | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def authorization(authorization): | ||
"""Add kubernetes subject-access-review identity with resource attributes for authpolicy resource""" | ||
authorization.authorization.add_kubernetes( | ||
"subject-access-review-host", | ||
ValueFrom("auth.identity.user.username"), | ||
ResourceAttributes( | ||
resource=Value("authpolicy"), group=Value("kuadrant.io"), verb=CelExpression("request.method.lowerAscii()") | ||
), | ||
) | ||
return authorization | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def cluster_role(request, cluster, blame, module_label): | ||
"""Creates ClusterRole with rules only for accessing authpolicy resource""" | ||
rules = [Rule(verbs=["get"], resources=["authpolicy"], apiGroups=["kuadrant.io"])] | ||
cluster_role = ClusterRole.create_instance(cluster, blame("cr"), rules, labels={"app": module_label}) | ||
request.addfinalizer(cluster_role.delete) | ||
cluster_role.commit() | ||
return cluster_role | ||
|
||
|
||
def test_subject_access_review_resource_attributes(client, auth, auth2): | ||
"""Test if the client is authorized to access the api based on the service account token resource attributes""" | ||
response = client.get("/get", auth=auth) | ||
assert response.status_code == 200 | ||
|
||
response = client.post("/post", auth=auth) | ||
assert response.status_code == 403 | ||
|
||
response = client.get("/get", auth=auth2) | ||
assert response.status_code == 403 |
16 changes: 0 additions & 16 deletions
16
...ests/singlecluster/authorino/identity/subject_access_review/test_subject_access_review.py
This file was deleted.
Oops, something went wrong.