Skip to content

Commit

Permalink
Merge pull request #248 from azgabur/authpolicyv2
Browse files Browse the repository at this point in the history
Authpolicy v1beta2 reformat
  • Loading branch information
pehala authored Oct 31, 2023
2 parents dbff688 + 95ac311 commit 4a7b314
Show file tree
Hide file tree
Showing 23 changed files with 199 additions and 165 deletions.
42 changes: 6 additions & 36 deletions testsuite/objects/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ def __post_init__(self):
class Credentials:
"""Dataclass for Credentials structure"""

in_location: str
in_location: Literal["authorizationHeader", "customHeader", "queryString", "cookie"]
keySelector: str

def asdict(self):
"""Custom asdict, because I cannot use 'in' as a name"""
return {"in": self.in_location, "keySelector": self.keySelector}
"""Custom asdict because of needing to put location as parent dict key for inner dict"""
if self.in_location == "authorizationHeader":
return {self.in_location: {"prefix": self.keySelector}}
return {self.in_location: {"name": self.keySelector}}


@dataclass
Expand Down Expand Up @@ -116,39 +118,7 @@ class Value(ABCValue):
class ValueFrom(ABCValue):
"""Dataclass for dynamic Value. It contains reference path to existing value in AuthJson."""

authJSON: str

def asdict(self):
"""Override `asdict` function"""
return {"valueFrom": {"authJSON": self.authJSON}}


@dataclass
class Property:
"""Dataclass for static and dynamic values. Property is a Value with name."""

name: str
value: ABCValue

def asdict(self):
"""Override `asdict` function"""
return {"name": self.name, **asdict(self.value)}


@dataclass
class ExtendedProperty(Property):
"""
Dataclass extending Property class adding optional `overwrite` feature
used in extended_properties functionality in Identity section.
"""

overwrite: Optional[bool] = None

def asdict(self):
"""Extend inherited `asdict` function to include new attributes."""
if self.overwrite is not None:
return {**super().asdict(), "overwrite": self.overwrite}
return super().asdict()
selector: str


@dataclass
Expand Down
15 changes: 4 additions & 11 deletions testsuite/openshift/objects/auth_config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from functools import cached_property
from typing import Dict, List, Optional

from testsuite.objects import Rule
from testsuite.objects import Rule, asdict
from testsuite.openshift.client import OpenShiftClient
from testsuite.openshift.objects import OpenShiftObject, modify
from .sections import IdentitySection, MetadataSection, ResponseSection, AuthorizationSection
Expand All @@ -25,7 +25,7 @@ def authorization(self) -> AuthorizationSection:
@cached_property
def identity(self) -> IdentitySection:
"""Gives access to identity settings"""
return IdentitySection(self, "identity")
return IdentitySection(self, "authentication")

@cached_property
def metadata(self) -> MetadataSection:
Expand All @@ -48,7 +48,7 @@ def create_instance(
):
"""Creates base instance"""
model: Dict = {
"apiVersion": "authorino.kuadrant.io/v1beta1",
"apiVersion": "authorino.kuadrant.io/v1beta2",
"kind": "AuthConfig",
"metadata": {"name": name, "namespace": openshift.project, "labels": labels},
"spec": {"hosts": hostnames or [route.hostname]}, # type: ignore
Expand All @@ -71,15 +71,8 @@ def remove_all_hosts(self):
"""Remove all hosts"""
self.model.spec.hosts = []

@modify
def set_deny_with(self, code, value):
"""Set denyWith"""
self.auth_section["denyWith"] = {
"unauthenticated": {"code": code, "headers": [{"name": "Location", "valueFrom": {"authJSON": value}}]}
}

@modify
def add_rule(self, when: list[Rule]):
"""Add rule for the skip of entire AuthConfig"""
self.auth_section.setdefault("when", [])
self.auth_section["when"].extend([vars(x) for x in when])
self.auth_section["when"].extend([asdict(x) for x in when])
13 changes: 11 additions & 2 deletions testsuite/openshift/objects/auth_config/auth_policy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
"""Module containing classes related to Auth Policy"""
from typing import Dict, List

from testsuite.objects import Rule, asdict

from testsuite.openshift.client import OpenShiftClient
from testsuite.openshift.objects import modify
from testsuite.openshift.objects.auth_config import AuthConfig
from testsuite.openshift.objects.gateway_api.route import HTTPRoute

Expand All @@ -23,7 +26,7 @@ def route(self) -> HTTPRoute:

@property
def auth_section(self):
return self.model.spec.setdefault("authScheme", {})
return self.model.spec.setdefault("rules", {})

# pylint: disable=unused-argument
@classmethod
Expand All @@ -37,7 +40,7 @@ def create_instance( # type: ignore
):
"""Creates base instance"""
model: Dict = {
"apiVersion": "kuadrant.io/v1beta1",
"apiVersion": "kuadrant.io/v1beta2",
"kind": "AuthPolicy",
"metadata": {"name": name, "namespace": openshift.project, "labels": labels},
"spec": {
Expand All @@ -55,3 +58,9 @@ def remove_host(self, hostname):

def remove_all_hosts(self):
return self.route.remove_all_hostnames()

@modify
def add_rule(self, when: list[Rule]):
"""Add rule for the skip of entire AuthPolicy"""
self.model.spec.setdefault("when", [])
self.model.spec["when"].extend([asdict(x) for x in when])
Loading

0 comments on commit 4a7b314

Please sign in to comment.