diff --git a/documentation/docs/tirith-providers/terraform-plan.md b/documentation/docs/tirith-providers/terraform-plan.md index d214b87b..b3f0ceed 100644 --- a/documentation/docs/tirith-providers/terraform-plan.md +++ b/documentation/docs/tirith-providers/terraform-plan.md @@ -33,6 +33,7 @@ Most operations read the `resource_changes` array of the plan. If the plan conta |---|---| | `attribute` | Get an attribute's planned value for every instance of a resource type | | `action` | Get the plan actions (`create`, `update`, `delete`, ...) for a resource type | +| `provider_name` | Get the provider that manages each instance of a resource type | | `count` | Count the changed instances of a resource type | | `direct_dependencies` | Get the resource types listed in a resource's `depends_on` | | `direct_references` | Get or check references between resources | @@ -133,6 +134,46 @@ Example (adapted from a test fixture; fails when a virtual network would be dele --- +## `provider_name` + +Returns the provider that manages every instance of a resource type, read from `provider_name` in the plan, for example `registry.terraform.io/hashicorp/aws`. Every resource change carries it, including resources that use a provider with no `provider` block. + +| Parameter | Required | Description | +|---|---|---| +| `terraform_resource_type` | yes | Resource type to match, or `*` for every type. | +| `exclude_resource_types` | no (default `[]`) | List of resource types to skip. Only applied when `terraform_resource_type` is `*`. | + +**Returns:** one string per matching resource instance. + +**On a miss:** no resource of the requested type — severity 1. + +Example (fails when an AWS, Google Cloud or Azure resource uses a provider outside the `hashicorp` namespace, such as `registry.terraform.io/acme/aws`; other providers such as `random` pass): + +```json +{ + "meta": { + "version": "v1", + "required_provider": "stackguardian/terraform_plan" + }, + "evaluators": [ + { + "id": "official_cloud_providers", + "provider_args": { + "operation_type": "provider_name", + "terraform_resource_type": "*" + }, + "condition": { + "type": "RegexMatch", + "value": "^(registry\\.(terraform\\.io|opentofu\\.org)/hashicorp/(aws|google|google-beta|azurerm|azuread)|(?!.*/(aws|google|google-beta|azurerm|azuread)$).*)$" + } + } + ], + "eval_expression": "official_cloud_providers" +} +``` + +--- + ## `count` Counts the instances of a resource type in `resource_changes`. diff --git a/src/tirith/providers/terraform_plan/handler.py b/src/tirith/providers/terraform_plan/handler.py index d702a3f1..8a2e7e16 100644 --- a/src/tirith/providers/terraform_plan/handler.py +++ b/src/tirith/providers/terraform_plan/handler.py @@ -266,6 +266,32 @@ def provide(provider_inputs, input_data): } ) return outputs + # - Get the provider that manages each instance of a resource + # - resource_changes.*.provider_name + elif input_type == "provider_name": + resource_type = provider_inputs["terraform_resource_type"] + is_resource_type_found = False + for resource_change in resource_changes: + if resource_type == "*" and resource_change["type"] in exclude_resource_types: + continue + if resource_type in (resource_change["type"], "*"): + is_resource_type_found = True + outputs.append( + { + "value": resource_change.get("provider_name"), + "meta": resource_change, + "err": None, + "context": _resource_context(input_type, resource_change, attribute="provider_name"), + } + ) + if not is_resource_type_found: + outputs.append( + { + "value": ProviderError(severity_value=1), + "err": f"resource_type: '{resource_type}' is not found", + } + ) + return outputs # CASE 3 # - Get count of a particular resource # - resource_changes.*.index diff --git a/tests/providers/terraform_plan/test_provider_name.py b/tests/providers/terraform_plan/test_provider_name.py new file mode 100644 index 00000000..d1a9d887 --- /dev/null +++ b/tests/providers/terraform_plan/test_provider_name.py @@ -0,0 +1,109 @@ +from tirith.core.core import start_policy_evaluation_from_dict +from tirith.providers.common import ProviderError +from tirith.providers.terraform_plan import handler + +OFFICIAL_CLOUD_PROVIDERS = ( + r"^(registry\.(terraform\.io|opentofu\.org)/hashicorp/(aws|google|google-beta|azurerm|azuread)" + r"|(?!.*/(aws|google|google-beta|azurerm|azuread)$).*)$" +) + + +def _plan(*resources): + return { + "resource_changes": [ + { + "address": f"{resource_type}.this", + "type": resource_type, + "provider_name": provider_name, + "change": {"actions": ["create"], "after": {}}, + } + for resource_type, provider_name in resources + ] + } + + +def _official_providers_policy(): + return { + "meta": {"version": "v1", "required_provider": "stackguardian/terraform_plan"}, + "evaluators": [ + { + "id": "official_cloud_providers", + "provider_args": {"operation_type": "provider_name", "terraform_resource_type": "*"}, + "condition": {"type": "RegexMatch", "value": OFFICIAL_CLOUD_PROVIDERS}, + } + ], + "eval_expression": "official_cloud_providers", + } + + +def test_returns_the_provider_of_every_matching_resource(): + plan = _plan( + ("aws_s3_bucket", "registry.terraform.io/hashicorp/aws"), + ("random_id", "registry.terraform.io/hashicorp/random"), + ) + + result = handler.provide({"operation_type": "provider_name", "terraform_resource_type": "*"}, plan) + + assert [r["value"] for r in result] == [ + "registry.terraform.io/hashicorp/aws", + "registry.terraform.io/hashicorp/random", + ] + assert result[0]["context"]["resource_address"] == "aws_s3_bucket.this" + + +def test_filters_by_resource_type(): + plan = _plan( + ("aws_s3_bucket", "registry.terraform.io/hashicorp/aws"), + ("random_id", "registry.terraform.io/hashicorp/random"), + ) + + result = handler.provide({"operation_type": "provider_name", "terraform_resource_type": "random_id"}, plan) + + assert [r["value"] for r in result] == ["registry.terraform.io/hashicorp/random"] + + +def test_skips_excluded_resource_types_on_wildcard(): + plan = _plan( + ("aws_s3_bucket", "registry.terraform.io/hashicorp/aws"), + ("random_id", "registry.terraform.io/hashicorp/random"), + ) + + result = handler.provide( + {"operation_type": "provider_name", "terraform_resource_type": "*", "exclude_resource_types": ["random_id"]}, + plan, + ) + + assert [r["value"] for r in result] == ["registry.terraform.io/hashicorp/aws"] + + +def test_missing_resource_type_is_severity_1(): + plan = _plan(("aws_s3_bucket", "registry.terraform.io/hashicorp/aws")) + + result = handler.provide({"operation_type": "provider_name", "terraform_resource_type": "google_bucket"}, plan) + + assert isinstance(result[0]["value"], ProviderError) + assert result[0]["value"].severity_value == 1 + + +def test_official_and_non_cloud_providers_pass_a_regex_policy(): + plan = _plan( + ("aws_s3_bucket", "registry.terraform.io/hashicorp/aws"), + ("google_storage_bucket", "registry.opentofu.org/hashicorp/google"), + ("random_id", "registry.terraform.io/hashicorp/random"), + ("kubernetes_namespace", "registry.terraform.io/hashicorp/kubernetes"), + ) + + result = start_policy_evaluation_from_dict(_official_providers_policy(), plan) + + assert result["final_result"] is True + + +def test_lookalike_cloud_provider_fails_a_regex_policy(): + plan = _plan( + ("aws_s3_bucket", "registry.terraform.io/hashicorp/aws"), + ("aws_instance", "registry.terraform.io/acme/aws"), + ) + + result = start_policy_evaluation_from_dict(_official_providers_policy(), plan) + + assert result["final_result"] is False