Skip to content

Commit df5bd4c

Browse files
committed
Quote complex vars correctly.
1 parent 5e55981 commit df5bd4c

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

src/infrablocks/invoke_terraform/terraform/terraform.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ def _format_configuration_value(
118118
elif value is None:
119119
return f'{option_key}="{key}=null"'
120120
else:
121-
return f'{option_key}="{key}={json.dumps(value)}"'
121+
jsonified_value = (
122+
json.dumps(value).replace("\\", "\\\\").replace('"', r"\"")
123+
)
124+
return f'{option_key}="{key}={jsonified_value}"'
122125

123126
def _build_backend_config(
124127
self, backend_config: BackendConfig | None

tests/unit/infrablocks/invoke_terraform/terraform/test_terraform.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,12 @@ def test_apply_executes_with_list_of_string_var(self):
204204
terraform.apply(vars=variables)
205205

206206
executor.execute.assert_called_once_with(
207-
["terraform", "apply", '-var="foo=["ex", "why", "zed"]"'], env=None
207+
[
208+
"terraform",
209+
"apply",
210+
'-var="foo=[\\"ex\\", \\"why\\", \\"zed\\"]"',
211+
],
212+
env=None,
208213
)
209214

210215
def test_apply_executes_with_list_of_integer_var(self):
@@ -259,7 +264,12 @@ def test_apply_executes_with_mapping_of_string_var(self):
259264
terraform.apply(vars=variables)
260265

261266
executor.execute.assert_called_once_with(
262-
["terraform", "apply", '-var="foo={"a": "x", "b": "y"}"'], env=None
267+
[
268+
"terraform",
269+
"apply",
270+
'-var="foo={\\"a\\": \\"x\\", \\"b\\": \\"y\\"}"',
271+
],
272+
env=None,
263273
)
264274

265275
def test_apply_executes_with_mapping_of_integer_var(self):
@@ -270,7 +280,8 @@ def test_apply_executes_with_mapping_of_integer_var(self):
270280
terraform.apply(vars=variables)
271281

272282
executor.execute.assert_called_once_with(
273-
["terraform", "apply", '-var="foo={"a": 1, "b": 2}"'], env=None
283+
["terraform", "apply", '-var="foo={\\"a\\": 1, \\"b\\": 2}"'],
284+
env=None,
274285
)
275286

276287
def test_apply_executes_with_mapping_of_float_var(self):
@@ -281,7 +292,8 @@ def test_apply_executes_with_mapping_of_float_var(self):
281292
terraform.apply(vars=variables)
282293

283294
executor.execute.assert_called_once_with(
284-
["terraform", "apply", '-var="foo={"a": 1.1, "b": 2.2}"'], env=None
295+
["terraform", "apply", '-var="foo={\\"a\\": 1.1, \\"b\\": 2.2}"'],
296+
env=None,
285297
)
286298

287299
def test_apply_executes_with_mapping_of_boolean_var(self):
@@ -292,7 +304,11 @@ def test_apply_executes_with_mapping_of_boolean_var(self):
292304
terraform.apply(vars=variables)
293305

294306
executor.execute.assert_called_once_with(
295-
["terraform", "apply", '-var="foo={"a": true, "b": false}"'],
307+
[
308+
"terraform",
309+
"apply",
310+
'-var="foo={\\"a\\": true, \\"b\\": false}"',
311+
],
296312
env=None,
297313
)
298314

@@ -304,7 +320,11 @@ def test_apply_executes_with_mapping_of_none_var(self):
304320
terraform.apply(vars=variables)
305321

306322
executor.execute.assert_called_once_with(
307-
["terraform", "apply", '-var="foo={"a": true, "b": false}"'],
323+
[
324+
"terraform",
325+
"apply",
326+
'-var="foo={\\"a\\": true, \\"b\\": false}"',
327+
],
308328
env=None,
309329
)
310330

0 commit comments

Comments
 (0)