Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix top-level when conditions #586

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions testsuite/kuadrant/policy/authorization/auth_policy.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
"""Module containing classes related to AuthPolicy"""

from functools import cached_property
from typing import Dict, TYPE_CHECKING
from typing import Dict

from testsuite.gateway import Referencable
from testsuite.kubernetes import modify
from testsuite.kubernetes.client import KubernetesClient
from testsuite.utils import asdict
from .auth_config import AuthConfig
from .sections import ResponseSection
from .. import Policy
from .. import Policy, CelPredicate
from . import Pattern

if TYPE_CHECKING:
from . import Rule


class AuthPolicy(Policy, AuthConfig):
"""AuthPolicy object, it serves as Kuadrants AuthConfig"""
Expand Down Expand Up @@ -44,7 +41,7 @@ def create_instance(
return cls(model, context=cluster.context)

@modify
def add_rule(self, when: list["Rule"]):
def add_rule(self, when: list[CelPredicate]):
"""Add rule for the skip of entire AuthPolicy"""
self.model.spec.setdefault("when", [])
self.model.spec["when"].extend([asdict(x) for x in when])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import pytest

from testsuite.kuadrant.policy.authorization import Pattern
from testsuite.kuadrant.policy import CelPredicate

pytestmark = [pytest.mark.authorino]


@pytest.fixture(scope="module")
def authorization(authorization, module_label):
"""Add rule to the AuthConfig to skip entire authn/authz with certain request header"""
authorization.add_rule([Pattern("context.request.http.headers.key", "neq", module_label)])
authorization.add_rule([CelPredicate(f"request.headers.key != '{module_label}'")])
return authorization


Expand Down
21 changes: 12 additions & 9 deletions testsuite/tests/singlecluster/authorino/dinosaur/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from openshift_client import OpenShiftPythonException

from testsuite.httpx.auth import HttpxOidcClientAuth
from testsuite.kuadrant.policy import CelPredicate
from testsuite.oidc.keycloak import Keycloak
from testsuite.utils import ContentType
from testsuite.kuadrant.policy.authorization import Pattern, PatternRef, Value, ValueFrom, DenyResponse
Expand Down Expand Up @@ -100,21 +101,18 @@ def _resource_info(org_id, owner):
@pytest.fixture(scope="module")
def authorization(authorization, keycloak, terms_and_conditions, cluster_info, admin_rhsso, resource_info):
"""Creates complex Authorization Config."""
path_fourth_element = 'context.request.http.path.@extract:{"sep":"/","pos":4}'
path_third_element = 'context.request.http.path.@extract:{"sep":"/","pos":3}'
path_fourth_element = 'request.path.@extract:{"sep":"/","pos":4}'
authorization.add_patterns(
{
"api-route": [Pattern("context.request.http.path", "matches", "^/anything/dinosaurs_mgmt/.+")],
"v1-route": [Pattern(path_third_element, "eq", "v1")],
"dinosaurs-route": [Pattern(path_fourth_element, "eq", "dinosaurs")],
"dinosaur-resource-route": [Pattern("context.request.http.path", "matches", "/dinosaurs/[^/]+$")],
"dinosaur-resource-route": [Pattern("request.path", "matches", "/dinosaurs/[^/]+$")],
"create-dinosaur-route": [
Pattern("context.request.http.path", "matches", "/dinosaurs/?$"),
Pattern("context.request.http.method", "eq", "POST"),
Pattern("request.path", "matches", "/dinosaurs/?$"),
Pattern("request.method", "eq", "POST"),
],
"metrics-federate-route": [
Pattern(path_fourth_element, "eq", "dinosaurs"),
Pattern("context.request.http.path", "matches", "/metrics/federate$"),
Pattern("request.path", "matches", "/metrics/federate$"),
],
"service-accounts-route": [Pattern(path_fourth_element, "eq", "service_accounts")],
"supported-instance-types-route": [Pattern(path_fourth_element, "eq", "instance_types")],
Expand All @@ -129,7 +127,12 @@ def authorization(authorization, keycloak, terms_and_conditions, cluster_info, a
"require-org-id": [Pattern("auth.identity.org_id", "neq", "")],
}
)
authorization.add_rule([PatternRef("api-route"), PatternRef("v1-route")])
authorization.add_rule(
[
CelPredicate("request.path.matches('^/anything/dinosaurs_mgmt/.+')"),
CelPredicate("request.path.split('/')[3] == 'v1'"),
]
)

authorization.identity.clear_all()
authorization.identity.add_oidc(
Expand Down
Loading