Skip to content

policy: validate policies against provider schemas before the walk - #38877

Draft
DanielMSchmidt wants to merge 2 commits into
mainfrom
policy-validate-provider-schemas
Draft

policy: validate policies against provider schemas before the walk#38877
DanielMSchmidt wants to merge 2 commits into
mainfrom
policy-validate-provider-schemas

Conversation

@DanielMSchmidt

Copy link
Copy Markdown
Contributor

Validate policies against the run's provider schemas before the plan/apply walk, so a policy that references an attribute a provider does not have fails early rather than partway through evaluation.

This is the terraform (host) side of the policy-plugin schema-validation feature. It only affects experimental policy runs (it's a no-op unless a policy client is attached).

What it does

When opts.PolicyClient is set, after loadSchemas builds the run's schemas and before the graph walk, terraform enumerates schemarepo.Schemas, encodes each provider's config / resource / data-source configschema.Block.ImpliedType() as a cty JSON type, and calls the plugin's new ValidateProviderSchemas RPC. The returned diagnostics are appended to the run diagnostics; an error blocks the run before any policy is evaluated against a real resource.

Why before the walk (not at Setup): Setup runs at the command layer and also serves init — before any Context or schemas exist. Schemas only exist inside terraform.Context, so the schema push is a separate post-Setup call. Insertion points:

  • context_plan.go planWalk — loads schemas via c.Schemas (a cache hit after graph build) then validates, before c.walk.
  • context_apply.go ApplyAndEval — reuses the schemas already loaded before the walk.

Changes

  • internal/policy/proto/policy.proto: new ValidateProviderSchemas RPC + ProviderSchema / request / response messages (mirrors the plugin's proto for wire compatibility). Regenerated with make protobuf.
  • internal/policy: ValidateProviderSchemas added to the Client interface, with the client implementation (cty JSON type encoding), host-side request/response types, and the MockClient.
  • internal/terraform: a validateProviderSchemas helper (enumerates schemarepo.Schemas → the request; local names via Module.LocalNameForProvider) wired into planWalk and ApplyAndEval; a plan test covering both the "schemas sent, clean run" and "validation error blocks the run early" paths.

Dependencies

  • Requires the plugin RPC: hashicorp/terraform-policy-plugin#82 (which in turn depends on the shared engine, hashicorp/terraform-policy-core#95).

Target

  • Experimental (AllowExperimentalFeatures); no behavior change for non-policy runs.

@DanielMSchmidt
DanielMSchmidt requested a review from a team as a code owner July 15, 2026 09:47
When a policy client is attached, send the run's provider schemas to the policy
plugin (via the new ValidateProviderSchemas RPC) after schemas load and before
the plan/apply graph walks — so a policy that references an attribute a provider
does not have fails early, rather than partway through evaluation.

The client serialises each provider's config, resource, and data-source object
types as cty JSON type encodings; the plugin validates the loaded policies with
typed-unknown inputs and returns diagnostics, which block the run on an error.
Wired into planWalk (loading schemas first — a cache hit after graph build) and
ApplyAndEval (schemas already in hand). It is a no-op when no policy client is
attached, so it only affects experimental policy runs.

Adds ValidateProviderSchemas to the policy.Client interface and the vendored
proto, with the client implementation, the mock, and a plan test.

Requires the corresponding plugin RPC (hashicorp/terraform-policy-plugin#82).
@DanielMSchmidt
DanielMSchmidt force-pushed the policy-validate-provider-schemas branch from d156d2a to cb882cd Compare July 15, 2026 09:48
Comment thread internal/terraform/context_apply.go
map<string, bytes> resources = 4;

// data_sources maps a data-source type to the cty JSON encoding of its object type.
map<string, bytes> data_sources = 5;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about ephemeral and list types?
With a lot of resources changing sensitive values to read_only inputs, checking against ephemeral resources might also be important (does not not exist in the policy language?)
Even if these are existing holes in the policy language, having support for them on the core side means it all works can work when the holes get filled.

@DanielMSchmidt
DanielMSchmidt marked this pull request as draft July 21, 2026 08:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants