Skip to content

Commit 785d760

Browse files
authored
fix(tfplan): When attribute isn't found it should raise ProviderError (#267)
Closes #266
1 parent b57fefe commit 785d760

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

src/tirith/providers/terraform_plan/handler.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,15 @@ def provide(provider_inputs, input_data):
112112
for evaluated_output in evaluated_outputs:
113113
outputs.append({"value": evaluated_output, "meta": resource_change, "err": None})
114114

115-
# If we didn't find the attribute in this resource, add a None value so it still gets evaluated
115+
# If we didn't find the attribute in this resource, raise the ProviderError so that the value
116+
# still gets evaluated
116117
if not local_is_found_attribute:
117-
outputs.append({"value": None, "meta": resource_change, "err": None})
118+
outputs.append(
119+
{
120+
"value": ProviderError(severity_value=2),
121+
"err": f"attribute: '{attribute}' is not found",
122+
}
123+
)
118124
else:
119125
outputs.append(
120126
{
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"evaluators": [
3+
{
4+
"id": "eval-id-1",
5+
"description": "",
6+
"provider_args": {
7+
"operation_type": "attribute",
8+
"terraform_resource_attribute": "shouldnt_exist",
9+
"terraform_resource_type": "*"
10+
},
11+
"condition": {
12+
"type": "Equals",
13+
"value": 10,
14+
"error_tolerance": 2
15+
}
16+
}
17+
],
18+
"meta": {
19+
"required_provider": "stackguardian/terraform_plan",
20+
"version": "v1"
21+
},
22+
"eval_expression": "eval-id-1"
23+
}

tests/providers/terraform_plan/test_dot_star_attr.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,24 @@ def test_multiple_resource_tag_check_all_resources_have_tag():
116116
assert result["final_result"] is True
117117
all_passed = all(item.get("passed") is True for item in result["evaluators"][0]["result"])
118118
assert all_passed, "All resource evaluations should pass when all have the required tag"
119+
120+
121+
def test_star_with_not_found_attribute_should_raise_providererror():
122+
input_data = load_json_from_fixtures("input_costcenter_tags.json")
123+
policy = load_json_from_fixtures("policy_star_restype_should_skip.json")
124+
125+
result = start_policy_evaluation_from_dict(policy, input_data)
126+
127+
# The policy tries to access 'shouldnt_exist' attribute on all resources (*)
128+
# Since this attribute doesn't exist, it should return ProviderError
129+
# With error_tolerance=2, errors with severity <= 2 should be skipped
130+
131+
# Check that we have results for multiple resources
132+
assert len(result["evaluators"][0]["result"]) == 3
133+
134+
# All results should have ProviderError values (skipped due to error tolerance)
135+
for item in result["evaluators"][0]["result"]:
136+
# With error_tolerance=2, provider errors should be skipped (passed=None)
137+
assert item.get("passed") is None
138+
# The message should indicate the attribute was not found
139+
assert "shouldnt_exist" in item.get("message", "")

0 commit comments

Comments
 (0)