Skip to content

Commit 44a7ccc

Browse files
authored
Make builtin Jinja2 filters allowed on workers (#943)
1 parent e7c4ee5 commit 44a7ccc

3 files changed

Lines changed: 50 additions & 40 deletions

File tree

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,59 +36,59 @@ The following filters are [shipped with Jinja2](https://jinja.palletsprojects.co
3636
| Name | CORE | WORKER | LOCAL |
3737
| ---- | ---- | ------ | ----- |
3838
| abs | | | |
39-
| attr | | | |
40-
| batch | | | |
39+
| attr | | | |
40+
| batch | | | |
4141
| capitalize | | | |
4242
| center | | | |
4343
| count | | | |
4444
| d | | | |
4545
| default | | | |
46-
| dictsort | | | |
46+
| dictsort | | | |
4747
| e | | | |
4848
| escape | | | |
4949
| filesizeformat | | | |
5050
| first | | | |
5151
| float | | | |
5252
| forceescape | | | |
5353
| format | | | |
54-
| groupby | | | |
54+
| groupby | | | |
5555
| indent | | | |
5656
| int | | | |
57-
| items | | | |
57+
| items | | | |
5858
| join | | | |
5959
| last | | | |
6060
| length | | | |
6161
| list | | | |
6262
| lower | | | |
63-
| map | | | |
63+
| map | | | |
6464
| max | | | |
6565
| min | | | |
66-
| pprint | | | |
67-
| random | | | |
68-
| reject | | | |
69-
| rejectattr | | | |
66+
| pprint | | | |
67+
| random | | | |
68+
| reject | | | |
69+
| rejectattr | | | |
7070
| replace | | | |
7171
| reverse | | | |
7272
| round | | | |
73-
| safe | | | |
74-
| select | | | |
75-
| selectattr | | | |
73+
| safe | | | |
74+
| select | | | |
75+
| selectattr | | | |
7676
| slice | | | |
77-
| sort | | | |
77+
| sort | | | |
7878
| string | | | |
7979
| striptags | | | |
8080
| sum | | | |
8181
| title | | | |
82-
| tojson | | | |
82+
| tojson | | | |
8383
| trim | | | |
8484
| truncate | | | |
85-
| unique | | | |
85+
| unique | | | |
8686
| upper | | | |
8787
| urlencode | | | |
88-
| urlize | | | |
88+
| urlize | | | |
8989
| wordcount | | | |
9090
| wordwrap | | | |
91-
| xmlattr | | | |
91+
| xmlattr | | | |
9292
<!-- vale on -->
9393

9494
## Netutils filters

infrahub_sdk/template/filters.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,59 +23,69 @@ def trusted(self) -> bool:
2323

2424
BUILTIN_FILTERS = [
2525
FilterDefinition(name="abs", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
26-
FilterDefinition(name="attr", allowed_contexts=ExecutionContext.LOCAL, source="jinja2"),
27-
FilterDefinition(name="batch", allowed_contexts=ExecutionContext.LOCAL, source="jinja2"),
26+
FilterDefinition(name="attr", allowed_contexts=ExecutionContext.WORKER | ExecutionContext.LOCAL, source="jinja2"),
27+
FilterDefinition(name="batch", allowed_contexts=ExecutionContext.WORKER | ExecutionContext.LOCAL, source="jinja2"),
2828
FilterDefinition(name="capitalize", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
2929
FilterDefinition(name="center", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
3030
FilterDefinition(name="count", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
3131
FilterDefinition(name="d", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
3232
FilterDefinition(name="default", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
33-
FilterDefinition(name="dictsort", allowed_contexts=ExecutionContext.LOCAL, source="jinja2"),
33+
FilterDefinition(
34+
name="dictsort", allowed_contexts=ExecutionContext.WORKER | ExecutionContext.LOCAL, source="jinja2"
35+
),
3436
FilterDefinition(name="e", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
3537
FilterDefinition(name="escape", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
3638
FilterDefinition(name="filesizeformat", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
3739
FilterDefinition(name="first", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
3840
FilterDefinition(name="float", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
3941
FilterDefinition(name="forceescape", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
4042
FilterDefinition(name="format", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
41-
FilterDefinition(name="groupby", allowed_contexts=ExecutionContext.LOCAL, source="jinja2"),
43+
FilterDefinition(
44+
name="groupby", allowed_contexts=ExecutionContext.WORKER | ExecutionContext.LOCAL, source="jinja2"
45+
),
4246
FilterDefinition(name="indent", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
4347
FilterDefinition(name="int", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
44-
FilterDefinition(name="items", allowed_contexts=ExecutionContext.LOCAL, source="jinja2"),
48+
FilterDefinition(name="items", allowed_contexts=ExecutionContext.WORKER | ExecutionContext.LOCAL, source="jinja2"),
4549
FilterDefinition(name="join", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
4650
FilterDefinition(name="last", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
4751
FilterDefinition(name="length", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
4852
FilterDefinition(name="list", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
4953
FilterDefinition(name="lower", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
50-
FilterDefinition(name="map", allowed_contexts=ExecutionContext.LOCAL, source="jinja2"),
54+
FilterDefinition(name="map", allowed_contexts=ExecutionContext.WORKER | ExecutionContext.LOCAL, source="jinja2"),
5155
FilterDefinition(name="max", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
5256
FilterDefinition(name="min", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
53-
FilterDefinition(name="pprint", allowed_contexts=ExecutionContext.LOCAL, source="jinja2"),
54-
FilterDefinition(name="random", allowed_contexts=ExecutionContext.LOCAL, source="jinja2"),
55-
FilterDefinition(name="reject", allowed_contexts=ExecutionContext.LOCAL, source="jinja2"),
56-
FilterDefinition(name="rejectattr", allowed_contexts=ExecutionContext.LOCAL, source="jinja2"),
57+
FilterDefinition(name="pprint", allowed_contexts=ExecutionContext.WORKER | ExecutionContext.LOCAL, source="jinja2"),
58+
FilterDefinition(name="random", allowed_contexts=ExecutionContext.WORKER | ExecutionContext.LOCAL, source="jinja2"),
59+
FilterDefinition(name="reject", allowed_contexts=ExecutionContext.WORKER | ExecutionContext.LOCAL, source="jinja2"),
60+
FilterDefinition(
61+
name="rejectattr", allowed_contexts=ExecutionContext.WORKER | ExecutionContext.LOCAL, source="jinja2"
62+
),
5763
FilterDefinition(name="replace", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
5864
FilterDefinition(name="reverse", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
5965
FilterDefinition(name="round", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
60-
FilterDefinition(name="safe", allowed_contexts=ExecutionContext.LOCAL, source="jinja2"),
61-
FilterDefinition(name="select", allowed_contexts=ExecutionContext.LOCAL, source="jinja2"),
62-
FilterDefinition(name="selectattr", allowed_contexts=ExecutionContext.LOCAL, source="jinja2"),
66+
FilterDefinition(name="safe", allowed_contexts=ExecutionContext.WORKER | ExecutionContext.LOCAL, source="jinja2"),
67+
FilterDefinition(name="select", allowed_contexts=ExecutionContext.WORKER | ExecutionContext.LOCAL, source="jinja2"),
68+
FilterDefinition(
69+
name="selectattr", allowed_contexts=ExecutionContext.WORKER | ExecutionContext.LOCAL, source="jinja2"
70+
),
6371
FilterDefinition(name="slice", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
64-
FilterDefinition(name="sort", allowed_contexts=ExecutionContext.LOCAL, source="jinja2"),
72+
FilterDefinition(name="sort", allowed_contexts=ExecutionContext.WORKER | ExecutionContext.LOCAL, source="jinja2"),
6573
FilterDefinition(name="string", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
6674
FilterDefinition(name="striptags", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
6775
FilterDefinition(name="sum", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
6876
FilterDefinition(name="title", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
69-
FilterDefinition(name="tojson", allowed_contexts=ExecutionContext.LOCAL, source="jinja2"),
77+
FilterDefinition(name="tojson", allowed_contexts=ExecutionContext.WORKER | ExecutionContext.LOCAL, source="jinja2"),
7078
FilterDefinition(name="trim", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
7179
FilterDefinition(name="truncate", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
72-
FilterDefinition(name="unique", allowed_contexts=ExecutionContext.LOCAL, source="jinja2"),
80+
FilterDefinition(name="unique", allowed_contexts=ExecutionContext.WORKER | ExecutionContext.LOCAL, source="jinja2"),
7381
FilterDefinition(name="upper", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
7482
FilterDefinition(name="urlencode", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
75-
FilterDefinition(name="urlize", allowed_contexts=ExecutionContext.LOCAL, source="jinja2"),
83+
FilterDefinition(name="urlize", allowed_contexts=ExecutionContext.WORKER | ExecutionContext.LOCAL, source="jinja2"),
7684
FilterDefinition(name="wordcount", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
7785
FilterDefinition(name="wordwrap", allowed_contexts=ExecutionContext.ALL, source="jinja2"),
78-
FilterDefinition(name="xmlattr", allowed_contexts=ExecutionContext.LOCAL, source="jinja2"),
86+
FilterDefinition(
87+
name="xmlattr", allowed_contexts=ExecutionContext.WORKER | ExecutionContext.LOCAL, source="jinja2"
88+
),
7989
]
8090

8191

tests/unit/sdk/test_infrahub_filters.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,14 @@ def test_context_worker_allows_artifact_content(self) -> None:
100100
jinja.validate(context=ExecutionContext.WORKER)
101101

102102
def test_context_worker_blocks_local_only_filters(self) -> None:
103-
"""WORKER context should still block LOCAL-only filters like 'safe'."""
104-
jinja = Jinja2Template(template="{{ data | safe }}")
103+
"""WORKER context should still block LOCAL-only filters like 'fqdn_to_ip'."""
104+
jinja = Jinja2Template(template="{{ data | fqdn_to_ip }}")
105105
with pytest.raises(JinjaTemplateOperationViolationError) as exc:
106106
jinja.validate(context=ExecutionContext.WORKER)
107-
assert exc.value.message == "The 'safe' filter isn't allowed to be used"
107+
assert exc.value.message == "The 'fqdn_to_ip' filter isn't allowed to be used"
108108

109109
def test_context_local_allows_local_only_filters(self) -> None:
110-
jinja = Jinja2Template(template="{{ data | safe }}")
110+
jinja = Jinja2Template(template="{{ data | fqdn_to_ip }}")
111111
jinja.validate(context=ExecutionContext.LOCAL)
112112

113113
def test_context_local_blocks_artifact_content(self) -> None:

0 commit comments

Comments
 (0)