Skip to content

Commit

Permalink
Jinja2 template helper filter b64decode (#3242)
Browse files Browse the repository at this point in the history
# What this PR does
Add an additional jinja2 template helper filter to decode base64-encoded
strings.

An example of an incoming integration payload that would benefit from
this filter is GCP's pubsub message:

https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessage 

## Which issue(s) this PR fixes

## Checklist

- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [ ] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)

---------

Co-authored-by: Jorge Vargas <jorge.vargas@homeprotech.com>
Co-authored-by: Joey Orlando <joey.orlando@grafana.com>
Co-authored-by: Joey Orlando <joseph.t.orlando@gmail.com>
  • Loading branch information
4 people authored Nov 6, 2023
1 parent eb0f465 commit 80f5818
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 0 deletions.
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

0 comments on commit 80f5818

Please sign in to comment.