Skip to content

Commit abe1d0d

Browse files
authored
Set *_content filter context to WORKER (#907)
The idea is that these filters should *never* be used by the main API server. Making them `LOCAL` too would allow that. So rather than doing this, we prefer making them `WORKER` only and allow any filters to run on workers if the user turns on the proper setting in the API server.
1 parent 3e93fe2 commit abe1d0d

3 files changed

Lines changed: 14 additions & 29 deletions

File tree

docs/docs/python-sdk/reference/templating.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,10 @@ These filters are provided by the Infrahub SDK for artifact and file object cont
185185
<!-- vale off -->
186186
| Name | CORE | WORKER | LOCAL |
187187
| ---- | ---- | ------ | ----- |
188-
| `artifact_content` | | | |
189-
| `file_object_content` | | | |
190-
| `file_object_content_by_hfid` | | | |
191-
| `file_object_content_by_id` | | | |
188+
| `artifact_content` | | | |
189+
| `file_object_content` | | | |
190+
| `file_object_content_by_hfid` | | | |
191+
| `file_object_content_by_id` | | | |
192192
| `from_json` | | | |
193193
| `from_yaml` | | | |
194194
<!-- vale on -->

infrahub_sdk/template/filters.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -162,22 +162,10 @@ def trusted(self) -> bool:
162162

163163

164164
INFRAHUB_FILTERS = [
165-
FilterDefinition(
166-
name="artifact_content", allowed_contexts=ExecutionContext.WORKER | ExecutionContext.LOCAL, source="infrahub"
167-
),
168-
FilterDefinition(
169-
name="file_object_content", allowed_contexts=ExecutionContext.WORKER | ExecutionContext.LOCAL, source="infrahub"
170-
),
171-
FilterDefinition(
172-
name="file_object_content_by_hfid",
173-
allowed_contexts=ExecutionContext.WORKER | ExecutionContext.LOCAL,
174-
source="infrahub",
175-
),
176-
FilterDefinition(
177-
name="file_object_content_by_id",
178-
allowed_contexts=ExecutionContext.WORKER | ExecutionContext.LOCAL,
179-
source="infrahub",
180-
),
165+
FilterDefinition(name="artifact_content", allowed_contexts=ExecutionContext.WORKER, source="infrahub"),
166+
FilterDefinition(name="file_object_content", allowed_contexts=ExecutionContext.WORKER, source="infrahub"),
167+
FilterDefinition(name="file_object_content_by_hfid", allowed_contexts=ExecutionContext.WORKER, source="infrahub"),
168+
FilterDefinition(name="file_object_content_by_id", allowed_contexts=ExecutionContext.WORKER, source="infrahub"),
181169
FilterDefinition(name="from_json", allowed_contexts=ExecutionContext.ALL, source="infrahub"),
182170
FilterDefinition(name="from_yaml", allowed_contexts=ExecutionContext.ALL, source="infrahub"),
183171
]

tests/unit/sdk/test_infrahub_filters.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,8 @@ def test_not_trusted_when_local_only(self) -> None:
6262
fd = FilterDefinition(name="safe", allowed_contexts=ExecutionContext.LOCAL, source="jinja2")
6363
assert fd.trusted is False
6464

65-
def test_not_trusted_when_worker_and_local(self) -> None:
66-
fd = FilterDefinition(
67-
name="artifact_content",
68-
allowed_contexts=ExecutionContext.WORKER | ExecutionContext.LOCAL,
69-
source="infrahub",
70-
)
65+
def test_not_trusted_when_worker_only(self) -> None:
66+
fd = FilterDefinition(name="artifact_content", allowed_contexts=ExecutionContext.WORKER, source="infrahub")
7167
assert fd.trusted is False
7268

7369
def test_not_trusted_when_core_only(self) -> None:
@@ -114,10 +110,11 @@ def test_context_local_allows_local_only_filters(self) -> None:
114110
jinja = Jinja2Template(template="{{ data | safe }}")
115111
jinja.validate(context=ExecutionContext.LOCAL)
116112

117-
def test_context_local_allows_artifact_content(self) -> None:
118-
"""LOCAL context allows artifact_content (WORKER | LOCAL)."""
113+
def test_context_local_blocks_artifact_content(self) -> None:
114+
"""LOCAL context blocks artifact_content (WORKER only) — these filters require a worker."""
119115
jinja = Jinja2Template(template="{{ sid | artifact_content }}")
120-
jinja.validate(context=ExecutionContext.LOCAL)
116+
with pytest.raises(JinjaTemplateOperationViolationError):
117+
jinja.validate(context=ExecutionContext.LOCAL)
121118

122119
@pytest.mark.parametrize("context", [ExecutionContext.CORE, ExecutionContext.WORKER])
123120
def test_user_filters_always_allowed(self, context: ExecutionContext) -> None:

0 commit comments

Comments
 (0)