Skip to content

Commit

Permalink
Changed syntax of configuration variables #39
Browse files Browse the repository at this point in the history
The proposed syntax ${var} can cause conflicts with shell scripts
which can be included in the files section of the configuration
dictionary. To avoid any ambiguity, the syntax has been reverted to
the default jinja2 syntax.
  • Loading branch information
nemesifier committed Feb 16, 2016
1 parent 7860e16 commit 3c8e1a2
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 15 deletions.
4 changes: 2 additions & 2 deletions docs/source/general/basics.rst
Expand Up @@ -296,8 +296,8 @@ Here's an example from the real world:
"url": "http://controller.examplewifiservice.com",
"interval": "60",
"verify_ssl": "1",
"uuid": "${UUID}",
"key": "${KEY}"
"uuid": "{{ UUID }}",
"key": "{{ KEY }}"
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion docs/source/general/commandline_utility.rst
Expand Up @@ -76,7 +76,7 @@ know what this argument does please read ":ref:`configuration_variables`"), ther
you can reference environment variables inside *configurations* and *templates*::

export HOSTNAME=freedom
netjsonconfig -c '{"general": { "hostname": "${HOSTNAME}" }}' -b openwrt -m render
netjsonconfig -c '{"general": { "hostname": "{{ HOSTNAME }}" }}' -b openwrt -m render

You can also avoid using ``export`` and write everything in a one line command::

Expand Down
2 changes: 0 additions & 2 deletions netjsonconfig/backends/base.py
Expand Up @@ -13,8 +13,6 @@ def __init__(self, *args, **kwargs):
self.filters = {}
self.block_start_string = '{=##'
self.block_end_string = '##=}'
self.variable_start_string = '${'
self.variable_end_string = '}'

def call_binop(self, context, operator, left, right):
raise SecurityError('binary operator {0} not allowed'.format(operator))
Expand Down
16 changes: 8 additions & 8 deletions tests/openwrt/test_context.py
Expand Up @@ -13,8 +13,8 @@ class TestContext(unittest.TestCase, _TabsMixin):
def test_config(self):
config = {
"general": {
"hostname": "${name}",
"description": "${desc}"
"hostname": "{{ name }}",
"description": "{{ desc }}"
}
}
context = {
Expand All @@ -27,8 +27,8 @@ def test_config(self):
self.assertIn("option description 'test.context.desc'", output)

def test_template(self):
config = {"general": {"hostname": "${name}"}}
template = {"general": {"description": "${desc}"}}
config = {"general": {"hostname": "{{ name }}"}}
template = {"general": {"description": "{{ desc }}"}}
context = {
"name": "test-context-name",
"desc": "test.context.desc"
Expand All @@ -39,21 +39,21 @@ def test_template(self):
self.assertIn("option description 'test.context.desc'", output)

def test_sandbox(self):
danger = """${ self.__repr__.__globals__.get('sys').version }"""
danger = """{{ self.__repr__.__globals__.get('sys').version }}"""
config = {"general": {"description": danger}}
o = OpenWrt(config, context={"description": "sandbox"})
with self.assertRaises(SecurityError):
print(o.render())

def test_security_binop(self):
danger = """desc: ${10**10}"""
danger = """desc: {{ 10**10 }}"""
config = {"general": {"description": danger}}
o = OpenWrt(config, context={"description": "sandbox"})
with self.assertRaises(SecurityError):
print(o.render())

def test_security_unop(self):
danger = """desc: ${ -10 }"""
danger = """desc: {{ -10 }}"""
config = {"general": {"description": danger}}
o = OpenWrt(config, context={"description": "sandbox"})
with self.assertRaises(SecurityError):
Expand All @@ -67,7 +67,7 @@ def test_security_block(self):
print(o.render())

def test_security_methods(self):
danger = """${ "{.__getitem__.__globals__[sys].version}".format(self) }"""
danger = """{{ "{.__getitem__.__globals__[sys].version}".format(self) }}"""
config = {"general": {"description": danger}}
o = OpenWrt(config, context={"description": "sandbox"})
with self.assertRaises(SecurityError):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_bin.py
Expand Up @@ -123,8 +123,8 @@ def test_generate_redirection(self):
tar.close()

def test_context(self):
config = json.dumps({'general': {'description': '${DESC}'}})
config = json.dumps({'general': {'description': '{{ DESC }}'}})
command = "export DESC=testdesc; netjsonconfig --config '{0}' -b openwrt -m render".format(config)
output = subprocess.check_output(command, shell=True).decode()
self.assertNotIn('${DESC}', output)
self.assertNotIn('{{ DESC }}', output)
self.assertIn('testdesc', output)

0 comments on commit 3c8e1a2

Please sign in to comment.