Skip to content

Commit

Permalink
fix(terraform): Fix conditional expression evaluation logic with comp…
Browse files Browse the repository at this point in the history
…are (#6160)

* fix(terraform): Fix conditional expression evaluation logic with compare

* filter out None before conversion

---------

Co-authored-by: Steve Vaknin <svaknin@paloaltonetworks.com>
  • Loading branch information
SteveVaknin and Steve Vaknin committed Apr 10, 2024
1 parent 37b5e84 commit 1b0beb2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ def evaluate_conditional_expression(input_str: str) -> str:
groups, start, end = condition
if len(groups) != 3:
return input_str
evaluated_condition = evaluate_terraform(groups[0])
evaluated_condition = evaluate_compare(groups[0])
if type(evaluated_condition) is str:
evaluated_condition = evaluate_terraform(groups[0])
condition_substr = input_str[start:end]
bool_evaluated_condition = convert_to_bool(evaluated_condition)
if bool_evaluated_condition is True:
Expand Down Expand Up @@ -407,12 +409,12 @@ def apply_binary_op(a: Optional[Union[str, int, bool]], b: Optional[Union[str, i
if type_a != type_b:
try:
temp_b = type_a(b) # type:ignore[misc,arg-type]
if isinstance(type_a, bool):
if isinstance(a, bool) and b:
temp_b = bool(convert_to_bool(b))
return operators[operator](a, temp_b) # type:ignore[type-var]
except Exception:
temp_a = type_b(a) # type:ignore[misc,arg-type]
if isinstance(type_b, bool):
if isinstance(b, bool) and a:
temp_a = bool(convert_to_bool(a))
return operators[operator](temp_a, b) # type:ignore[type-var]
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ def test_conditional_expression(self):
input_str = 'blocked == "allowed" ? True : False'
expected = False
self.assertEqual(expected, evaluate_terraform(input_str))

input_str = 'True == "true" ? True : False'
expected = True
self.assertEqual(expected, evaluate_terraform(input_str))

input_str = 'False != "false" ? True : False'
expected = False
self.assertEqual(expected, evaluate_terraform(input_str))

def test_format(self):
input_str = '"format("Hello, %s!", "Ander")"'
Expand Down

0 comments on commit 1b0beb2

Please sign in to comment.