Skip to content

Commit 5ffba41

Browse files
authored
Merge pull request #22 from aserto-dev/add_policy_root_to_check
Add policyRoot as option for Check middleware
2 parents ca0f842 + 7d64ec2 commit 5ffba41

4 files changed

Lines changed: 18 additions & 8 deletions

File tree

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

Lines changed: 6 additions & 3 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,8 +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 != "":
132-
policy_path = self._aserto_middleware._policy_path_root + "." + policy_path
133+
policy_root = self._options.policyRoot or self._aserto_middleware._policy_path_root
134+
if policy_root:
135+
policy_path = f"{policy_root}.{policy_path}"
133136
return policy_path
134137

135138
return mapper
@@ -179,7 +182,7 @@ async def decorated(*args: Any, **kwargs: Any) -> Response:
179182
identity_provider=self._identity_provider,
180183
policy_instance_name=self._aserto_middleware._policy_instance_name or "",
181184
policy_instance_label=self._aserto_middleware._policy_instance_label or "",
182-
policy_path_root=self._aserto_middleware._policy_path_root,
185+
policy_path_root=self._options.policyRoot or self._aserto_middleware._policy_path_root,
183186
policy_path_resolver=policy_mapper,
184187
resource_context_provider=resource_context,
185188
)

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 & 3 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,8 +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 != "":
132-
policy_path = self._aserto_middleware._policy_path_root + "." + policy_path
133+
policy_root = self._options.policyRoot or self._aserto_middleware._policy_path_root
134+
if policy_root:
135+
policy_path = f"{policy_root}.{policy_path}"
133136
return policy_path
134137

135138
return mapper
@@ -178,7 +181,7 @@ def decorated(*args: Any, **kwargs: Any) -> Response:
178181
identity_provider=self._identity_provider,
179182
policy_instance_name=self._aserto_middleware._policy_instance_name or "",
180183
policy_instance_label=self._aserto_middleware._policy_instance_label or "",
181-
policy_path_root=self._aserto_middleware._policy_path_root,
184+
policy_path_root=self._options.policyRoot or self._aserto_middleware._policy_path_root,
182185
policy_path_resolver=policy_mapper,
183186
resource_context_provider=self._resource_context_provider,
184187
)

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)