Skip to content

Commit

Permalink
templates: add env & minjson filters
Browse files Browse the repository at this point in the history
 - Escape for systemd envfiles with `env`
 - Minify a JSON block with `minjson`
  • Loading branch information
nathan-muir committed Aug 22, 2018
1 parent 8be7425 commit 88ad55d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion credsmash/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import jinja2.sandbox

import credsmash.api
from .util import read_many, shell_quote, parse_manifest, detect_format, ItemNotFound
from .util import read_many, minjson, envfile_quote, shell_quote, parse_manifest, detect_format, ItemNotFound
from .cli import main

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -206,4 +206,6 @@ def _make_env():
)
env.filters['sh'] = shell_quote
env.filters['jsonify'] = json.dumps
env.filters['minjson'] = minjson
env.filters['env'] = envfile_quote
return env
16 changes: 16 additions & 0 deletions credsmash/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
import re
import os
from collections import OrderedDict

import six
from six.moves import configparser
Expand Down Expand Up @@ -198,3 +199,18 @@ def shell_quote(s):

return "'" + s.replace("'", "'\"'\"'") + "'"


def envfile_quote(s):
"""Return a envfile-escaped version of the string *s*."""
if not s:
return '""'
s = s.replace('\\', '\\\\')
s = s.replace('\n', '\\n\\\n')
s = s.replace('"', '\\"')
return '"' + s + '"'


def minjson(s):
"""A filter to parse & re-minify json"""
o = json.loads(s, object_pairs_hook=OrderedDict)
return json.dumps(o)

0 comments on commit 88ad55d

Please sign in to comment.