policy: validate policies against provider schemas before the walk - #38877
Draft
DanielMSchmidt wants to merge 2 commits into
Draft
policy: validate policies against provider schemas before the walk#38877DanielMSchmidt wants to merge 2 commits into
DanielMSchmidt wants to merge 2 commits into
Conversation
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
force-pushed
the
policy-validate-provider-schemas
branch
from
July 15, 2026 09:48
d156d2a to
cb882cd
Compare
jbardin
reviewed
Jul 15, 2026
| 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; |
Member
There was a problem hiding this comment.
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
marked this pull request as draft
July 21, 2026 08:06
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.PolicyClientis set, afterloadSchemasbuilds the run's schemas and before the graph walk, terraform enumeratesschemarepo.Schemas, encodes each provider's config / resource / data-sourceconfigschema.Block.ImpliedType()as a cty JSON type, and calls the plugin's newValidateProviderSchemasRPC. 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):Setupruns at the command layer and also servesinit— before anyContextor schemas exist. Schemas only exist insideterraform.Context, so the schema push is a separate post-Setupcall. Insertion points:context_plan.goplanWalk— loads schemas viac.Schemas(a cache hit after graph build) then validates, beforec.walk.context_apply.goApplyAndEval— reuses theschemasalready loaded before the walk.Changes
internal/policy/proto/policy.proto: newValidateProviderSchemasRPC +ProviderSchema/ request / response messages (mirrors the plugin's proto for wire compatibility). Regenerated withmake protobuf.internal/policy:ValidateProviderSchemasadded to theClientinterface, with the client implementation (cty JSON type encoding), host-side request/response types, and theMockClient.internal/terraform: avalidateProviderSchemashelper (enumeratesschemarepo.Schemas→ the request; local names viaModule.LocalNameForProvider) wired intoplanWalkandApplyAndEval; a plan test covering both the "schemas sent, clean run" and "validation error blocks the run early" paths.Dependencies
Target
AllowExperimentalFeatures); no behavior change for non-policy runs.