Files
acdl/terraform/platform/main.tf
T
Jon Chery e74a8c2f5d feat(P42): stub implementation — SoD, HITL gates, attestation matrix, Wiz, Kyverno
---ci---
project: acdl
phase: 42
milestone: v1.9
status: execute
---/ci---

Phase 42 — stub-implementation (REQ-107..111, D-084):

route_halt_artifact (REQ-107):
- core/separation_of_duties.py: real SNS publish (ACDL_SOD_HALT_TOPIC_ARN)
  + outbox fallback (SEPARATION_OF_DUTIES_VIOLATION event via
  outbox_writer) + stderr emission. No silent print-only stub.
- terraform/platform/main.tf: aws_sns_topic.acdl-sod-halt + output.

HITL attestation gates (REQ-108):
- core/hitl_gates.py: attest(contract_id, env, approver, evidence,
  outbox_client) records approver_qa/approver_prod/approver_dr to
  outbox, runs SoD check on prod, invokes attestation matrix, returns
  (ok, reason). Dev skips (autonomous). approver_from_env() reads
  GITHUB_ACTOR/GITEA_ACTOR.
- scripts/run_platform.sh: Step 7b HITL gate before apply for qa/prod/dr.

8-concern attestation matrix (REQ-109, D-084):
- core/attestation_matrix.py: check(env, evidence) runs the 8 concerns
  from hitl_matrix_design.md §10.4. Offline-testable (contract_nfrs,
  schema_validity, policy_pass) run for real. Operator-supplied accept
  signed artifacts validated for freshness (FRESHNESS_DAYS table) +
  schema. Signature skip when ACDL_ATTESTATION_SIGNING_KEY_ID unset
  (D-089). Fail loud if missing/expired for prod/dr.

Wiz real client (REQ-110):
- adapters/wiz/wiz_adapter.py: WizClient (GraphQL API, Bearer auth,
  pagination via pageInfo.hasNextPage + endCursor). fetch_and_adapt
  translates issues → PolicyCheckResult; graceful degrade when
  WIZ_API_TOKEN/WIZ_API_URL unset.

Kyverno fleshed out (REQ-111):
- adapters/kyverno/kyverno_adapter.py: full PolicyReport →
  PolicyCheckResult mapping (pass/fail/skip/warn + severity + skip-with-
  reason + resource ref construction from kind/name/namespace).
  adapt_inactive() emits KYVERNO_INACTIVE_TF_STACK guard. --kube-version
  stub parsed for future GitOps.

Tests: +47 (test_route_halt_artifact.py, test_hitl_gates.py,
test_attestation_matrix.py, test_wiz_adapter_real_client.py, expanded
test_kyverno_adapter.py). Existing wiz_adapter tests updated for the
real client's control.name ruleId. 493 passed; run_ci.sh green;
run_platform.sh --check-only green.
2026-07-23 04:40:44 +00:00

241 lines
6.9 KiB
Terraform

# ACDL platform infrastructure — contract ingestion Lambda + DynamoDB (D-051)
#
# Deploys:
# - DynamoDB table acdl-contracts (PK consumerRepo, SK contractId#submittedAt, SSE via CMK, PITR)
# - KMS customer-managed key for DynamoDB + SSM (shared CMK)
# - Lambda function acdl-contract-ingestor (Python 3.12, handler contract_ingestor.lambda_handler)
# - Lambda Function URL (IAM auth — consumers invoke via SigV4)
# - Secrets Manager secret acdl/github-token (stores the Lambda's GitHub PAT for issue creation)
# - IAM execution role for the Lambda (DynamoDB write + Secrets Manager read + KMS decrypt)
#
# State: terraform/platform/terraform.tfstate (separate from spike/ and microservice/)
terraform {
required_version = ">= 1.9, < 1.10"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
backend "s3" {
bucket = "acdl-tfstate-581513795199-us-east-1"
key = "platform/terraform.tfstate"
region = "us-east-1"
}
}
provider "aws" {
region = "us-east-1"
}
# KMS customer-managed key for DynamoDB SSE + SSM Parameter Store encryption
resource "aws_kms_key" "acdl_platform" {
description = "ACDL platform KMS key (DynamoDB SSE + SSM + Secrets Manager)"
enable_key_rotation = true
deletion_window_in_days = 30
}
resource "aws_kms_alias" "acdl_platform" {
name = "alias/acdl-platform"
target_key_id = aws_kms_key.acdl_platform.key_id
}
# DynamoDB table for contract ingestion
resource "aws_dynamodb_table" "acdl_contracts" {
name = "acdl-contracts"
billing_mode = "PAY_PER_REQUEST"
hash_key = "consumerRepo"
range_key = "contractId#submittedAt"
attribute {
name = "consumerRepo"
type = "S"
}
attribute {
name = "contractId#submittedAt"
type = "S"
}
point_in_time_recovery {
enabled = true
}
server_side_encryption {
enabled = true
kms_key_arn = aws_kms_key.acdl_platform.arn
}
tags = {
acdl:owner = "acdl"
acdl:contract = "platform"
acdl:environment = "prod"
acdl:cost-center = "acdl-default"
}
}
# Secrets Manager secret for the Lambda's GitHub token (issue creation)
resource "aws_secretsmanager_secret" "github_token" {
name = "acdl/github-token"
description = "GitHub PAT for the platform Lambda to create issues on the platform repo (D-055)."
kms_key_id = aws_kms_key.acdl_platform.arn
tags = {
acdl:owner = "acdl"
acdl:contract = "platform"
acdl:environment = "prod"
acdl:cost-center = "acdl-default"
}
}
# IAM execution role for the Lambda
resource "aws_iam_role" "lambda_exec" {
name = "acdl-contract-ingestor-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = { Service = "lambda.amazonaws.com" }
}]
})
}
resource "aws_iam_role_policy" "lambda_permissions" {
name = "acdl-contract-ingestor-policy"
role = aws_iam_role.lambda_exec.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = ["dynamodb:PutItem", "dynamodb:GetItem", "dynamodb:Query", "dynamodb:UpdateItem"]
Resource = aws_dynamodb_table.acdl_contracts.arn
},
{
Effect = "Allow"
Action = ["dynamodb:GetItem", "dynamodb:Query"]
Resource = aws_dynamodb_table.acdl_change_requests.arn
},
{
Effect = "Allow"
Action = ["secretsmanager:GetSecretValue"]
Resource = aws_secretsmanager_secret.github_token.arn
},
{
Effect = "Allow"
Action = ["kms:Decrypt"]
Resource = aws_kms_key.acdl_platform.arn
},
{
Effect = "Allow"
Action = ["logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents"]
Resource = "arn:aws:logs:*:*:*"
}
]
})
}
# Lambda function
resource "aws_lambda_function" "contract_ingestor" {
function_name = "acdl-contract-ingestor"
handler = "contract_ingestor.lambda_handler"
runtime = "python3.12"
role = aws_iam_role.lambda_exec.arn
filename = "contract_ingestor.zip"
source_code_hash = filebase64sha256("contract_ingestor.zip")
environment {
variables = {
CONTRACTS_TABLE = aws_dynamodb_table.acdl_contracts.name
GITHUB_TOKEN_SECRET_ID = aws_secretsmanager_secret.github_token.name
PLATFORM_REPO = "acdl/acdl"
}
}
tags = {
acdl:owner = "acdl"
acdl:contract = "platform"
acdl:environment = "prod"
acdl:cost-center = "acdl-default"
}
}
# Lambda Function URL (IAM auth — consumers invoke via SigV4)
resource "aws_lambda_function_url" "contract_ingestor" {
function_name = aws_lambda_function.contract_ingestor.function_name
authorization_type = "AWS_IAM"
}
# P1-6: Render the consumer invoke policy with the live AWS account ID.
# The JSON template (consumer_invoke_policy.json) uses ${account_id} and
# ${region} placeholders. Terraform renders them at apply time using the
# caller's live account ID — no hardcoded placeholder account IDs.
data "aws_caller_identity" "current" {}
data "aws_region" "current" {}
locals {
invoke_policy_template = file("${path.module}/consumer_invoke_policy.json")
rendered_invoke_policy = replace(
replace(local.invoke_policy_template, "${account_id}", data.aws_caller_identity.current.account_id),
"${region}", data.aws_region.current.name
)
}
output "consumer_invoke_policy_rendered" {
value = local.rendered_invoke_policy
description = "The consumer invoke policy JSON with the live account ID rendered. Distribute this to consumer accounts during onboarding."
}
# REQ-93: DynamoDB table for change requests (CMDB for decommission validation)
resource "aws_dynamodb_table" "acdl_change_requests" {
name = "acdl-change-requests"
billing_mode = "PAY_PER_REQUEST"
hash_key = "changeRequestId"
range_key = "submittedAt"
attribute {
name = "changeRequestId"
type = "S"
}
attribute {
name = "submittedAt"
type = "S"
}
point_in_time_recovery {
enabled = true
}
server_side_encryption {
enabled = true
kms_key_arn = aws_kms_key.acdl_platform.arn
}
tags = {
acdl:owner = "acdl"
acdl:contract = "platform"
acdl:environment = "prod"
acdl:cost-center = "acdl-default"
}
}
# REQ-107: SNS topic for separation-of-duties halt artifacts.
# route_halt_artifact publishes here when ACDL_SOD_HALT_TOPIC_ARN is set.
resource "aws_sns_topic" "acdl_sod_halt" {
name = "acdl-sod-halt"
kms_master_key_id = aws_kms_key.acdl_platform.id
tags = {
acdl:owner = "acdl"
acdl:contract = "platform"
acdl:environment = "prod"
acdl:cost-center = "acdl-default"
}
}
output "acdl_sod_halt_topic_arn" {
value = aws_sns_topic.acdl_sod_halt.arn
}