Skip to content

Commit 22b840c

Browse files
author
oanatmaria
committed
Add policyRoot as option for Check middleware
1 parent ca0f842 commit 22b840c

4 files changed

Lines changed: 18 additions & 6 deletions

File tree

packages/flask-aserto/src/flask_aserto/aio/check.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class CheckOptions:
3131
subjType: Optional[str] = ""
3232
subjMapper: Optional[IdentityMapper] = None
3333
policyPath: Optional[str] = ""
34+
policyRoot: Optional[str] = ""
3435
policyPathMapper: Optional[StringMapper] = None
3536

3637

@@ -110,6 +111,7 @@ def _with_overrides(self, **kwargs: Any) -> "CheckMiddleware":
110111
relationName=kwargs.get("relation_name", self._options.relationName),
111112
relationMapper=kwargs.get("relation_mapper", self._options.relationMapper),
112113
policyPath=kwargs.get("policy_path", self._options.policyPath),
114+
policyRoot=kwargs.get("policy_root", self._options.policyRoot),
113115
subjMapper=kwargs.get("identity_provider", self._identity_provider),
114116
objId=kwargs.get("object_id", self._options.objId),
115117
objType=kwargs.get("object_type", self._options.objType),
@@ -128,7 +130,9 @@ async def mapper() -> str:
128130
policy_path = await self._options.policyPathMapper()
129131
if policy_path == "":
130132
policy_path = "check"
131-
if self._aserto_middleware._policy_path_root != "":
133+
if self._options.policyRoot:
134+
policy_path = self._options.policyRoot + "." + policy_path
135+
elif self._aserto_middleware._policy_path_root != "":
132136
policy_path = self._aserto_middleware._policy_path_root + "." + policy_path
133137
return policy_path
134138

@@ -179,7 +183,7 @@ async def decorated(*args: Any, **kwargs: Any) -> Response:
179183
identity_provider=self._identity_provider,
180184
policy_instance_name=self._aserto_middleware._policy_instance_name or "",
181185
policy_instance_label=self._aserto_middleware._policy_instance_label or "",
182-
policy_path_root=self._aserto_middleware._policy_path_root,
186+
policy_path_root=self._options.policyRoot or self._aserto_middleware._policy_path_root,
183187
policy_path_resolver=policy_mapper,
184188
resource_context_provider=resource_context,
185189
)

packages/flask-aserto/src/flask_aserto/aio/middleware.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,14 @@ def check(
203203
subjType: Optional[str] = "",
204204
subjMapper: Optional[IdentityMapper] = None,
205205
policyPath: Optional[str] = "",
206+
policyRoot: Optional[str] = "",
206207
policyPathMapper: Optional[StringMapper] = None,
207208
) -> CheckMiddleware:
208209
opts = CheckOptions(
209210
objId=objId, objType=objType,objIdMapper=objIdMapper,
210211
objMapper=objMapper, relationName=relationName, relationMapper=relationMapper,
211-
subjType=subjType, subjMapper=subjMapper, policyPath=policyPath, policyPathMapper=policyPathMapper)
212+
subjType=subjType, subjMapper=subjMapper, policyRoot=policyRoot,
213+
policyPath=policyPath, policyPathMapper=policyPathMapper)
212214
return CheckMiddleware(options=opts, aserto_middleware=self)
213215

214216
def register_display_state_map(

packages/flask-aserto/src/flask_aserto/check.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class CheckOptions:
3131
subjType: Optional[str] = ""
3232
subjMapper: Optional[IdentityMapper] = None
3333
policyPath: Optional[str] = ""
34+
policyRoot: Optional[str] = ""
3435
policyPathMapper: Optional[StringMapper] = None
3536

3637

@@ -110,6 +111,7 @@ def _with_overrides(self, **kwargs: Any) -> "CheckMiddleware":
110111
relationName=kwargs.get("relation_name", self._options.relationName),
111112
relationMapper=kwargs.get("relation_mapper", self._options.relationMapper),
112113
policyPath=kwargs.get("policy_path", self._options.policyPath),
114+
policyRoot=kwargs.get("policy_root", self._options.policyRoot),
113115
subjMapper=kwargs.get("identity_provider", self._identity_provider),
114116
objId=kwargs.get("object_id", self._options.objId),
115117
objType=kwargs.get("object_type", self._options.objType),
@@ -128,7 +130,9 @@ def mapper() -> str:
128130
policy_path = self._options.policyPathMapper()
129131
if policy_path == "":
130132
policy_path = "check"
131-
if self._aserto_middleware._policy_path_root != "":
133+
if self._options.policyRoot:
134+
policy_path = self._options.policyRoot + "." + policy_path
135+
elif self._aserto_middleware._policy_path_root != "":
132136
policy_path = self._aserto_middleware._policy_path_root + "." + policy_path
133137
return policy_path
134138

@@ -178,7 +182,7 @@ def decorated(*args: Any, **kwargs: Any) -> Response:
178182
identity_provider=self._identity_provider,
179183
policy_instance_name=self._aserto_middleware._policy_instance_name or "",
180184
policy_instance_label=self._aserto_middleware._policy_instance_label or "",
181-
policy_path_root=self._aserto_middleware._policy_path_root,
185+
policy_path_root=self._options.policyRoot or self._aserto_middleware._policy_path_root,
182186
policy_path_resolver=policy_mapper,
183187
resource_context_provider=self._resource_context_provider,
184188
)

packages/flask-aserto/src/flask_aserto/middleware.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,14 @@ def check(
195195
subjType: Optional[str] = "",
196196
subjMapper: Optional[IdentityMapper] = None,
197197
policyPath: Optional[str] = "",
198+
policyRoot: Optional[str] = "",
198199
policyPathMapper: Optional[StringMapper] = None,
199200
) -> CheckMiddleware:
200201
opts = CheckOptions(
201202
objId=objId, objType=objType,objIdMapper=objIdMapper,
202203
objMapper=objMapper, relationName=relationName, relationMapper=relationMapper,
203-
subjType=subjType, subjMapper=subjMapper, policyPath=policyPath, policyPathMapper=policyPathMapper)
204+
subjType=subjType, subjMapper=subjMapper, policyRoot=policyRoot,
205+
policyPath=policyPath, policyPathMapper=policyPathMapper)
204206
return CheckMiddleware(options=opts, aserto_middleware=self)
205207

206208
def register_display_state_map(

0 commit comments

Comments
 (0)