Jinja2 template helper filter b64decode (#3242)

# 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>
This commit is contained in:
jorgeav 2023-11-07 07:42:25 +11:00 committed by GitHub
parent eb0f465970
commit 80f5818850
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 23 additions and 0 deletions

View file

@ -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

View file

@ -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 >}}

View file

@ -1,3 +1,4 @@
import base64
import json
import re
@ -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

View file

@ -4,6 +4,7 @@ from jinja2.exceptions import SecurityError
from jinja2.sandbox import SandboxedEnvironment
from .filters import (
b64decode,
datetimeformat,
iso8601_to_time,
json_dumps,
@ -29,3 +30,4 @@ jinja_template_env.filters["regex_replace"] = regex_replace
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

View file

@ -0,0 +1,7 @@
from common.jinja_templater.filters import b64decode
def test_base64_decode():
original = "dGVzdCBzdHJpbmch"
expected = "test string!"
assert b64decode(original) == expected

View file

@ -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' },
],
},
{