From 99423567553461bceebb521cb14ac33688107bbc Mon Sep 17 00:00:00 2001 From: Jorge Vargas Date: Thu, 2 Nov 2023 09:16:10 +1100 Subject: [PATCH 1/6] add jinja template filter base64decode --- docs/sources/jinja2-templating/_index.md | 1 + engine/common/jinja_templater/filters.py | 8 +++++++- engine/common/jinja_templater/jinja_template_env.py | 2 ++ engine/common/tests/test_base64decode.py | 7 +++++++ .../src/components/CheatSheet/CheatSheet.config.ts | 1 + 5 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 engine/common/tests/test_base64decode.py diff --git a/docs/sources/jinja2-templating/_index.md b/docs/sources/jinja2-templating/_index.md index 6fb52e3ddd..887689f70c 100644 --- a/docs/sources/jinja2-templating/_index.md +++ b/docs/sources/jinja2-templating/_index.md @@ -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 >}} diff --git a/engine/common/jinja_templater/filters.py b/engine/common/jinja_templater/filters.py index 88b4797de6..82e38f0902 100644 --- a/engine/common/jinja_templater/filters.py +++ b/engine/common/jinja_templater/filters.py @@ -1,6 +1,6 @@ +import base64 import json import re - from django.utils.dateparse import parse_datetime @@ -51,3 +51,9 @@ 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 diff --git a/engine/common/jinja_templater/jinja_template_env.py b/engine/common/jinja_templater/jinja_template_env.py index f4d2d65a3e..21c227c790 100644 --- a/engine/common/jinja_templater/jinja_template_env.py +++ b/engine/common/jinja_templater/jinja_template_env.py @@ -4,6 +4,7 @@ from jinja2.sandbox import SandboxedEnvironment from .filters import ( + b64decode, datetimeformat, iso8601_to_time, json_dumps, @@ -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 diff --git a/engine/common/tests/test_base64decode.py b/engine/common/tests/test_base64decode.py new file mode 100644 index 0000000000..c6f5dbb4c9 --- /dev/null +++ b/engine/common/tests/test_base64decode.py @@ -0,0 +1,7 @@ +from common.jinja_templater.filters import b64decode + + +def test_base64_encode(): + original = "dGVzdCBlbmNvZGUgc3RyaW5n" + expected = "test encode string" + assert b64decode(original) == expected diff --git a/grafana-plugin/src/components/CheatSheet/CheatSheet.config.ts b/grafana-plugin/src/components/CheatSheet/CheatSheet.config.ts index 3f0e600d5d..9d233f799b 100644 --- a/grafana-plugin/src/components/CheatSheet/CheatSheet.config.ts +++ b/grafana-plugin/src/components/CheatSheet/CheatSheet.config.ts @@ -83,6 +83,7 @@ export const genericTemplateCheatSheet: CheatSheetInterface = { { listItemName: 'time(), datetimeformat, iso8601_to_time' }, { listItemName: 'to_pretty_json' }, { listItemName: 'regex_replace, regex_match' }, + { listItemName: 'base64decode' }, ], }, { From 0e69e87da3ed85afe4e80107f2c8f278e002eb52 Mon Sep 17 00:00:00 2001 From: jorgeav <54142549+jorgeav@users.noreply.github.com> Date: Thu, 2 Nov 2023 09:59:21 +1100 Subject: [PATCH 2/6] style linting --- engine/common/jinja_templater/filters.py | 1 + 1 file changed, 1 insertion(+) diff --git a/engine/common/jinja_templater/filters.py b/engine/common/jinja_templater/filters.py index 82e38f0902..6e9226a0b1 100644 --- a/engine/common/jinja_templater/filters.py +++ b/engine/common/jinja_templater/filters.py @@ -1,6 +1,7 @@ import base64 import json import re + from django.utils.dateparse import parse_datetime From 23694b4cfa84c11a7a195e83960999101d0343af Mon Sep 17 00:00:00 2001 From: jorgeav <54142549+jorgeav@users.noreply.github.com> Date: Thu, 2 Nov 2023 10:07:03 +1100 Subject: [PATCH 3/6] corrected test name --- engine/common/tests/test_base64decode.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/engine/common/tests/test_base64decode.py b/engine/common/tests/test_base64decode.py index c6f5dbb4c9..59fbf666ab 100644 --- a/engine/common/tests/test_base64decode.py +++ b/engine/common/tests/test_base64decode.py @@ -1,7 +1,7 @@ from common.jinja_templater.filters import b64decode -def test_base64_encode(): - original = "dGVzdCBlbmNvZGUgc3RyaW5n" - expected = "test encode string" +def test_base64_decode(): + original = "dGVzdCBzdHJpbmch" + expected = "test string!" assert b64decode(original) == expected From 8b8b6c7f5cfda89f72edef7f29181963839d1494 Mon Sep 17 00:00:00 2001 From: jorgeav <54142549+jorgeav@users.noreply.github.com> Date: Thu, 2 Nov 2023 13:02:49 +1100 Subject: [PATCH 4/6] corrected cheatsheet variable name --- grafana-plugin/src/components/CheatSheet/CheatSheet.config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grafana-plugin/src/components/CheatSheet/CheatSheet.config.ts b/grafana-plugin/src/components/CheatSheet/CheatSheet.config.ts index 9d233f799b..506522aea2 100644 --- a/grafana-plugin/src/components/CheatSheet/CheatSheet.config.ts +++ b/grafana-plugin/src/components/CheatSheet/CheatSheet.config.ts @@ -83,7 +83,7 @@ export const genericTemplateCheatSheet: CheatSheetInterface = { { listItemName: 'time(), datetimeformat, iso8601_to_time' }, { listItemName: 'to_pretty_json' }, { listItemName: 'regex_replace, regex_match' }, - { listItemName: 'base64decode' }, + { listItemName: 'b64decode' }, ], }, { From 6692f9f8c4095fc7ebb373a3b5168b7ef9eb21e7 Mon Sep 17 00:00:00 2001 From: Joey Orlando Date: Thu, 2 Nov 2023 16:14:23 -0400 Subject: [PATCH 5/6] update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc8f47fb29..59d210c099 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) + ### Fixed - Fix db migration for mobile app @Ferril ([#3260](https://github.com/grafana/oncall/pull/3260)) From 77aec96a3d6201c60b8881191bca14203d3d621f Mon Sep 17 00:00:00 2001 From: jorgeav <54142549+jorgeav@users.noreply.github.com> Date: Sat, 4 Nov 2023 10:27:15 +1100 Subject: [PATCH 6/6] corrected style linting --- engine/common/jinja_templater/filters.py | 1 + 1 file changed, 1 insertion(+) diff --git a/engine/common/jinja_templater/filters.py b/engine/common/jinja_templater/filters.py index 6e9226a0b1..59618eb5be 100644 --- a/engine/common/jinja_templater/filters.py +++ b/engine/common/jinja_templater/filters.py @@ -53,6 +53,7 @@ def json_dumps(value): except (ValueError, AttributeError, TypeError): return None + def b64decode(value): try: return base64.b64decode(value).decode("utf-8")