Skip to content

Commit

Permalink
Add test for authpolicy attached directly to gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
averevki committed Dec 4, 2023
1 parent 9d2b32b commit 40f1976
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 5 deletions.
9 changes: 4 additions & 5 deletions testsuite/openshift/objects/auth_config/auth_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,21 @@ class AuthPolicy(AuthConfig):
def auth_section(self):
return self.model.spec.setdefault("rules", {})

# pylint: disable=unused-argument
@classmethod
def create_instance( # type: ignore
def create_instance(
cls,
openshift: OpenShiftClient,
name,
route: Referencable,
targetRef: Referencable,
labels: Dict[str, str] = None,
):
): # pylint: disable=invalid-name,arguments-renamed
"""Creates base instance"""
model: Dict = {
"apiVersion": "kuadrant.io/v1beta2",
"kind": "AuthPolicy",
"metadata": {"name": name, "namespace": openshift.project, "labels": labels},
"spec": {
"targetRef": route.reference,
"targetRef": targetRef.reference,
},
}

Expand Down
Empty file.
15 changes: 15 additions & 0 deletions testsuite/tests/kuadrant/gateway/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""Conftest for gateway tests"""
import pytest


@pytest.fixture(scope="module", autouse=True)
def gateway_wait_for_ready(gateway):
"""Waits for gateway to be ready"""
gateway.wait_for_ready()


@pytest.fixture(scope="module", autouse=True)
def commit(request, authorization):
"""Only commit authorization component"""
request.addfinalizer(authorization.delete)
authorization.commit()
60 changes: 60 additions & 0 deletions testsuite/tests/kuadrant/gateway/test_authpolicy_to_gateway.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""Test for AuthPolicy attached directly to gateway"""
from time import sleep
import pytest

from testsuite.openshift.objects.auth_config.auth_policy import AuthPolicy
from testsuite.openshift.objects.gateway_api.route import HTTPRoute


@pytest.fixture(scope="module")
def gateway_httproute(request, gateway, wildcard_domain, module_label, blame):
"""HTTPRoute with wildcard domain"""
route = HTTPRoute.create_instance(gateway.openshift, blame("gw-route"), gateway, {"app": module_label})
route.add_hostname(wildcard_domain)
route.set_path_match("/")

request.addfinalizer(route.delete)
route.commit()
return route


@pytest.fixture(scope="module")
def gateway_authorization(request, gateway, authorization_name, openshift, module_label):
"""AuthPolicy attached straight to gateway"""
auth_policy = AuthPolicy.create_instance(
openshift, f"gw-{authorization_name}", gateway, labels={"testRun": module_label}
)
auth_policy.authorization.add_opa_policy("deny-all", "allow { false }")
request.addfinalizer(auth_policy.delete)
return auth_policy


@pytest.fixture(scope="module")
def client_to_deny(gateway, exposer, blame):
"""Hostname that should be denied by AuthPolicy attached directly to gateway"""
hostname = exposer.expose_hostname(blame("not-accepted-hostname"), gateway)
client = hostname.client()
yield client
client.close()


def test_authpolicy_attached_to_gateway(gateway_httproute, gateway_authorization, client, client_to_deny):
# pylint: disable=unused-argument
"""
Test if AuthPolicy denying all requests sent to the undefined domains:
- send request to the hostname defined
- send request to the undefined domain without AuthPolicy attached to gateway
- commit deny-all AuthPolicy attached to gateway
- send request to the undefined domain with deny-all AuthPolicy attached to gateway
"""
response = client.get("/get")
assert response.status_code == 200

response = client_to_deny.get("/get")
assert response.status_code == 500

gateway_authorization.commit()
sleep(5)

response = client_to_deny.get("/get")
assert response.status_code == 403

0 comments on commit 40f1976

Please sign in to comment.