Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jinja2 template helper filter b64decode #3242

Merged
merged 9 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

- Add `b64decode` Jinja2 template helper filter by @jorgeav ([#3242](https://github.com/grafana/oncall/pull/3242))

## v1.3.53 (2023-11-03)

### Fixed
Expand Down
1 change: 1 addition & 0 deletions docs/sources/jinja2-templating/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,6 @@ Built-in functions:
- `datetimeformat` - converts time from datetime to the given format (`%H:%M / %d-%m-%Y` by default)
- `regex_replace` - performs a regex find and replace
- `regex_match` - performs a regex match, returns `True` or `False`. Usage example: `{{ payload.ruleName | regex_match(".*") }}`
- `b64decode` - performs a base64 string decode. Usage example: `{{ payload.data | b64decode }}`

{{< section >}}
8 changes: 8 additions & 0 deletions engine/common/jinja_templater/filters.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import base64
import json
import re

Expand Down Expand Up @@ -51,3 +52,10 @@ def json_dumps(value):
return json.dumps(value)
except (ValueError, AttributeError, TypeError):
return None


def b64decode(value):
try:
return base64.b64decode(value).decode("utf-8")
except (ValueError, AttributeError, TypeError):
return None
2 changes: 2 additions & 0 deletions engine/common/jinja_templater/jinja_template_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from jinja2.sandbox import SandboxedEnvironment

from .filters import (
b64decode,
datetimeformat,
iso8601_to_time,
json_dumps,
Expand All @@ -29,3 +30,4 @@ def raise_security_exception(name):
jinja_template_env.filters["regex_match"] = regex_match
jinja_template_env.filters["regex_search"] = regex_search
jinja_template_env.filters["json_dumps"] = json_dumps
jinja_template_env.filters["b64decode"] = b64decode
7 changes: 7 additions & 0 deletions engine/common/tests/test_base64decode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from common.jinja_templater.filters import b64decode


def test_base64_decode():
original = "dGVzdCBzdHJpbmch"
expected = "test string!"
assert b64decode(original) == expected
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export const genericTemplateCheatSheet: CheatSheetInterface = {
{ listItemName: 'time(), datetimeformat, iso8601_to_time' },
{ listItemName: 'to_pretty_json' },
{ listItemName: 'regex_replace, regex_match' },
{ listItemName: 'b64decode' },
],
},
{
Expand Down
Loading