Infrastructure code is plain resources by default. An Execution may opt in with terraform_modules; the deployment default is TERRAFORM_MODULES, off. - A module is accepted only from the registry namespaces the provider bundle lists (terraform-google-modules), at one exact version. Any other source is refused before Terraform runs; until now nothing stopped generated code naming any source for terraform init to download. - terraform_module looks up a module's inputs, outputs and submodules from the registry and says when it cannot be used with the pinned provider. - skills/google-cloud-iac-modules: when a module fits, and four modules with examples a test validates against hashicorp/google 8.6.0. - Opted in, the IaC author and validator get the Skill and the lookup. Not opted in, they get neither and a module block is refused in code. - A Change request follows its Baseline. execution.json records the choice. - case-07 runs case 03's request with modules opted into. Azure (aztfm) and AWS (terraform-aws-modules) sources are recorded in the plan for when those Cloud providers are added. No case has run with this. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
206 lines
6.8 KiB
Python
206 lines
6.8 KiB
Python
"""Published Terraform modules are opt-in per Execution (iteration 8).
|
|
|
|
Infrastructure code is plain resources by default. An Execution that opts in is
|
|
given the modules Skill and the module lookup, and only then is a module call from
|
|
an approved source accepted.
|
|
"""
|
|
|
|
import json
|
|
|
|
import pytest
|
|
from langgraph.checkpoint.memory import InMemorySaver
|
|
|
|
from app.adk.fake_runtime import FakeAgentRuntime, sample_submission
|
|
from app.adk.roles import load_roles
|
|
from app.adk.runtime import NoDecider, RoleSpec
|
|
from app.agent import AGENTS_DIR, ExecutionEngine
|
|
from app.nodes.router import Router, Thresholds
|
|
from app.providers import load_provider
|
|
from app.recorder import InMemoryRecorder
|
|
from app.tools.local import TerraformRunner
|
|
from app.tools.modules import MODULE_TOOL, MODULES_OPTED_IN, NOT_OPTED_IN
|
|
from app.workflows.deliverables import DEPENDENCY_GRAPH
|
|
from app.workflows.deliverables import DeliverableKind as K
|
|
|
|
AUTHOR = DEPENDENCY_GRAPH[K.DEPLOYMENT_GUIDANCE].specialist
|
|
CASE = "acme"
|
|
MODULE_CALL = """
|
|
module "network" {
|
|
source = "approved-modules/network/cloud"
|
|
version = "1.2.3"
|
|
}
|
|
"""
|
|
ROLES = {
|
|
"orchestrator": RoleSpec("orchestrator", "Unused.", budget=40),
|
|
**{
|
|
spec.specialist: RoleSpec(
|
|
spec.specialist,
|
|
"Produce.",
|
|
budget=15,
|
|
skills=("base",),
|
|
tools=(MODULE_TOOL,) if spec.specialist == AUTHOR else (),
|
|
modules_skill="modules-skill" if spec.specialist == AUTHOR else None,
|
|
)
|
|
for spec in DEPENDENCY_GRAPH.values()
|
|
},
|
|
}
|
|
|
|
|
|
class Recording(FakeAgentRuntime):
|
|
def __init__(self, turns):
|
|
super().__init__(turns)
|
|
self.seen: dict[str, dict] = {}
|
|
|
|
async def run(self, role, task, tools):
|
|
self.seen[role.name] = {
|
|
"skills": role.skills,
|
|
"role_tools": role.tools,
|
|
"context": task.context.get("terraform_modules"),
|
|
"opted_in": MODULES_OPTED_IN.get(),
|
|
}
|
|
return await super().run(role, task, tools)
|
|
|
|
|
|
def engine(tmp_path, **options) -> tuple[ExecutionEngine, Recording]:
|
|
runtime = Recording(
|
|
{
|
|
spec.specialist: [[sample_submission(kind)]] * 2
|
|
for kind, spec in DEPENDENCY_GRAPH.items()
|
|
}
|
|
)
|
|
built = ExecutionEngine(
|
|
runtime=runtime,
|
|
roles=ROLES,
|
|
recorder=InMemoryRecorder(),
|
|
checkpointer=InMemorySaver(),
|
|
output_root=tmp_path,
|
|
orchestrator=Router(NoDecider(), None, Thresholds()),
|
|
**options,
|
|
)
|
|
return built, runtime
|
|
|
|
|
|
def manifest(tmp_path, execution_id: str) -> dict:
|
|
folder = tmp_path / "deliverables" / "cases" / CASE / "executions" / execution_id
|
|
return json.loads((folder / "execution.json").read_text())
|
|
|
|
|
|
async def test_by_default_code_is_plain_resources(tmp_path):
|
|
built, runtime = engine(tmp_path, modules_available=True)
|
|
|
|
await built.start("Ingest", customer_case=CASE, pre_approved=True, execution_id="a")
|
|
|
|
author = runtime.seen[AUTHOR]
|
|
assert author["skills"] == ("base",)
|
|
assert MODULE_TOOL not in author["role_tools"]
|
|
assert author["context"] is False and author["opted_in"] is False
|
|
assert manifest(tmp_path, "a")["terraform_modules"] is False
|
|
|
|
|
|
async def test_an_execution_that_opts_in_is_given_the_skill_and_the_lookup(tmp_path):
|
|
built, runtime = engine(tmp_path, modules_available=True)
|
|
|
|
await built.start(
|
|
"Ingest",
|
|
customer_case=CASE,
|
|
pre_approved=True,
|
|
execution_id="a",
|
|
terraform_modules=True,
|
|
)
|
|
|
|
author = runtime.seen[AUTHOR]
|
|
assert author["skills"] == ("base", "modules-skill")
|
|
assert MODULE_TOOL in author["role_tools"]
|
|
assert author["context"] is True and author["opted_in"] is True
|
|
# A role that writes no code is given neither.
|
|
assert runtime.seen["requirements-analyst"]["skills"] == ("base",)
|
|
assert manifest(tmp_path, "a")["terraform_modules"] is True
|
|
|
|
|
|
async def test_the_deployment_may_make_modules_the_default(tmp_path):
|
|
built, runtime = engine(tmp_path, modules_available=True, default_modules=True)
|
|
|
|
await built.start("Ingest", customer_case=CASE, pre_approved=True, execution_id="a")
|
|
assert runtime.seen[AUTHOR]["opted_in"] is True
|
|
|
|
await built.start(
|
|
"Ingest",
|
|
customer_case=CASE,
|
|
pre_approved=True,
|
|
execution_id="b",
|
|
terraform_modules=False,
|
|
)
|
|
assert runtime.seen[AUTHOR]["opted_in"] is False
|
|
|
|
|
|
async def test_opting_in_is_refused_when_the_provider_approves_no_sources(tmp_path):
|
|
built, runtime = engine(tmp_path)
|
|
|
|
with pytest.raises(ValueError, match="no approved module sources"):
|
|
await built.start("Ingest", pre_approved=True, terraform_modules=True)
|
|
with pytest.raises(ValueError, match="true or false"):
|
|
await built.start("Ingest", pre_approved=True, terraform_modules="yes")
|
|
assert runtime.seen == {}
|
|
|
|
|
|
async def test_a_change_request_does_as_its_baseline_did(tmp_path):
|
|
built, runtime = engine(tmp_path, modules_available=True)
|
|
await built.start(
|
|
"Ingest",
|
|
customer_case=CASE,
|
|
pre_approved=True,
|
|
execution_id="v1",
|
|
terraform_modules=True,
|
|
)
|
|
runtime.seen.clear()
|
|
|
|
# sample submissions change nothing, so the Change request ends with warnings;
|
|
# what matters is which way it started.
|
|
await built.start(
|
|
"Retention changes.",
|
|
customer_case=CASE,
|
|
pre_approved=True,
|
|
execution_id="v2",
|
|
baseline="v1",
|
|
)
|
|
|
|
assert built is not None
|
|
state = (await built.interaction("v2")).state
|
|
assert state["terraform_modules"] is True
|
|
|
|
|
|
async def test_a_module_call_is_refused_unless_the_execution_opted_in():
|
|
runner = TerraformRunner(
|
|
binary="no-such-binary", module_namespaces=("approved-modules",)
|
|
)
|
|
files = {"terraform/main.tf": MODULE_CALL}
|
|
|
|
token = MODULES_OPTED_IN.set(False)
|
|
try:
|
|
assert (await runner.validate(files)).message == NOT_OPTED_IN
|
|
# Plain resources are untouched by the switch.
|
|
plain = await runner.validate({"terraform/main.tf": "locals {}"})
|
|
assert not plain.message.startswith("Refused")
|
|
finally:
|
|
MODULES_OPTED_IN.reset(token)
|
|
|
|
token = MODULES_OPTED_IN.set(True)
|
|
try:
|
|
opted = await runner.validate(files)
|
|
assert not opted.message.startswith("Refused")
|
|
finally:
|
|
MODULES_OPTED_IN.reset(token)
|
|
|
|
|
|
def test_the_gcp_bundle_names_its_modules_skill_for_the_roles_that_run_terraform():
|
|
bundle = load_provider("gcp")
|
|
roles = load_roles(AGENTS_DIR, bundle)
|
|
|
|
assert bundle.terraform_module_namespaces == ("terraform-google-modules",)
|
|
with_skill = {name for name, role in roles.items() if role.modules_skill}
|
|
assert with_skill == {"iac-author", "validator"}
|
|
# The Skill is not among any role's Skills until an Execution opts in.
|
|
assert all(
|
|
bundle.terraform_modules_skill not in role.skills for role in roles.values()
|
|
)
|