diff --git a/infra/terraform/cloudflare-kv/component.yaml b/infra/terraform/cloudflare-kv/component.yaml new file mode 100644 index 0000000..e3157c7 --- /dev/null +++ b/infra/terraform/cloudflare-kv/component.yaml @@ -0,0 +1,51 @@ +apiVersion: sourceplane.io/v1 +kind: Component + +metadata: + name: cloudflare-kv + description: Provisions the Cloudflare KV namespace the app's rate limits and share links live in (stage and prod) + labels: + domain: infra + scope: app-kv + +spec: + type: terraform + lifecycle: production + owner: platform + domain: infra + + parameters: + orgName: sourceplane + stackName: cloudflare-kv + terraformDir: terraform + terraformVersion: "1.15.3" + # SEC-JOB: the wiring document (KV namespace id/title) is lease-published + # post-apply; app deploys read it back as WIRING_CLOUDFLARE_KV_. + secretOutputs: "WIRING_CLOUDFLARE_KV=wiring" + + # The Cloudflare Terraform provider authenticates via the CLOUDFLARE_API_TOKEN + # env var, resolved by orun from the project-rung brokered secret (fresh per + # run, Deploy Workers scope). Declared at component level because plan-only + # lanes refresh against the live Cloudflare API too, not just apply. + secretEnv: + CLOUDFLARE_API_TOKEN: "secret://ai-artifacts/ai-artifacts/{{ .environment }}/CLOUDFLARE_API_TOKEN" + # Non-secret connection fact (account-id template) — replaces the ambient + # CLOUDFLARE_ACCOUNT_ID / TF_VAR_cloudflare_account_id CI values. + CLOUDFLARE_ACCOUNT_ID: "secret://ai-artifacts/ai-artifacts/{{ .environment }}/CLOUDFLARE_ACCOUNT_ID" + TF_VAR_cloudflare_account_id: "secret://ai-artifacts/ai-artifacts/{{ .environment }}/CLOUDFLARE_ACCOUNT_ID" + + subscribe: + environments: + - name: stage + profile: plan-only + profileRules: + - profile: apply + when: + triggerRef: github-push-main + + - name: prod + profile: plan-only + profileRules: + - profile: apply + when: + triggerRef: github-push-main diff --git a/infra/terraform/cloudflare-kv/terraform/adopt.tf b/infra/terraform/cloudflare-kv/terraform/adopt.tf new file mode 100644 index 0000000..7c1fbba --- /dev/null +++ b/infra/terraform/cloudflare-kv/terraform/adopt.tf @@ -0,0 +1,43 @@ +# --- Self-healing adoption (AWS-free state seeding) --- +# +# When the platform state does not yet track the namespace but Cloudflare +# already has one under this title (a baseline whose state predates the +# platform backend, or a recovered workspace), the create would collide with +# the duplicate-title guard (error 10014). Instead: look the namespace up at +# plan time with the job's brokered credentials and import it. Fresh products +# resolve to id = "" and create normally, and a resource already tracked in +# state short-circuits the lookup — so this block is inert everywhere except +# the one case it exists for. Both lookups run inside the job: process env +# carries the brokered CLOUDFLARE_API_TOKEN/CLOUDFLARE_ACCOUNT_ID and the +# TF_HTTP_* state credentials, so no extra secrets and no AWS access. + +data "external" "adopt_app_kv" { + program = ["bash", "-c", <<-EOT + set -euo pipefail + title="$(jq -r .title)" + state_file="$(mktemp)" + trap 'rm -f "$state_file"' EXIT + code="$(curl -s -o "$state_file" -w '%%{http_code}' \ + -u "$TF_HTTP_USERNAME:$TF_HTTP_PASSWORD" "$TF_HTTP_ADDRESS" || echo 000)" + if [ "$code" = "200" ] && jq -e \ + '[.resources[]? | select(.type == "cloudflare_workers_kv_namespace" and .name == "app_kv")] | length > 0' \ + "$state_file" >/dev/null 2>&1; then + jq -n '{id: ""}' + exit 0 + fi + resp="$(curl -fsS --retry 3 -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ + "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/storage/kv/namespaces?per_page=100")" + printf '%s' "$resp" | jq -c --arg t "$title" \ + '{id: ([.result[] | select(.title == $t) | .id] | first // "")}' + EOT + ] + query = { + title = local.app_kv_title + } +} + +import { + for_each = toset(compact([data.external.adopt_app_kv.result.id])) + to = cloudflare_workers_kv_namespace.app_kv + id = "${var.cloudflare_account_id}/${each.value}" +} diff --git a/infra/terraform/cloudflare-kv/terraform/backend.tf b/infra/terraform/cloudflare-kv/terraform/backend.tf new file mode 100644 index 0000000..ff89f5f --- /dev/null +++ b/infra/terraform/cloudflare-kv/terraform/backend.tf @@ -0,0 +1,5 @@ +# Terraform backend configuration is provided by Orun at runtime +# through environment variables and automatic backend initialization. +# +# S3 backend details are set via CLI args in the Orun runner. +# Local placeholder file for clarity. diff --git a/infra/terraform/cloudflare-kv/terraform/main.tf b/infra/terraform/cloudflare-kv/terraform/main.tf new file mode 100644 index 0000000..2b9200f --- /dev/null +++ b/infra/terraform/cloudflare-kv/terraform/main.tf @@ -0,0 +1,136 @@ +terraform { + required_version = ">= 1.15.0" + + # State lives on the platform (SB1): the runner exports TF_HTTP_* per job, so + # this block stays empty — no S3 bucket, no AWS role, no workspaces. + backend "http" {} + + required_providers { + cloudflare = { + source = "cloudflare/cloudflare" + version = "~> 4.30" + } + external = { + source = "hashicorp/external" + version = "~> 2.3" + } + } +} + +# --- Providers --- + +# Authenticates via the CLOUDFLARE_API_TOKEN env var (provider-native): the +# token is an orun-managed secret resolved into the job env at run time, so it +# never transits Terraform variables. +provider "cloudflare" {} + +# --- Variables (standard Orun parameters) --- + +variable "cloudflare_account_id" { + type = string + sensitive = true + default = "" + description = "Cloudflare account ID (from CLOUDFLARE_ACCOUNT_ID env var)" +} + +variable "orgName" { + type = string + default = "sourceplane" +} + +variable "owner" { + type = string + default = "sourceplane" +} + +variable "repo" { + type = string + default = "ai-artifacts" +} + +variable "namespace" { + type = string + default = "sourceplane" +} + +variable "namespacePrefix" { + type = string + default = "" +} + +variable "lane" { + type = string + default = "verify" +} + +variable "environment" { + type = string + default = "stage" +} + +variable "component" { + type = string + default = "cloudflare-kv" +} + +variable "stackName" { + type = string + default = "cloudflare-kv" +} + +variable "terraformDir" { + type = string + default = "terraform" +} + +variable "terraformVersion" { + type = string + default = "1.15.3" +} + +# --- The KV namespace the app's rate limits and share links live in --- +# +# Bound by apps/web as KV. Keys are the app's (`ratelimit::`, +# `fragment:`); every expiry is per-PUT (`expirationTtl`), owned by the +# Worker — no Terraform-side TTL knob exists for KV namespaces themselves. + +locals { + # Built on var.repo (the product's repository slug) so the title is unique per + # product. Cloudflare KV namespace titles must be unique per account, and two + # products may share an account — without the slug the second create fails + # with "a namespace with this account ID and title already exists" (10014). + # The Worker binds this namespace by ID via the wiring secret, so the title is + # free to change. + app_kv_title = "${var.namespacePrefix}${var.repo}-app-kv-${var.environment}" +} + +resource "cloudflare_workers_kv_namespace" "app_kv" { + account_id = var.cloudflare_account_id + title = local.app_kv_title +} + +# --- Wiring manifest (BF5, via orun secrets) --- +# The consumable outputs are published by the composition's wire-secrets step +# as the WIRING_CLOUDFLARE_KV secret on the project/{{env}} rung; worker +# deploys read it back as WIRING_CLOUDFLARE_KV_ secretEnv (BF6). Same +# document shape the Secrets Manager path carried. + +output "wiring" { + description = "Wiring document for downstream deploy-time binding resolution (pushed to orun secrets)" + value = jsonencode({ + app_kv_id = cloudflare_workers_kv_namespace.app_kv.id + app_kv_title = cloudflare_workers_kv_namespace.app_kv.title + }) +} + +# --- Outputs (non-secret) --- + +output "app_kv_id" { + description = "Cloudflare Workers KV namespace ID the app's rate limits and share links live in" + value = cloudflare_workers_kv_namespace.app_kv.id +} + +output "app_kv_title" { + description = "Cloudflare Workers KV namespace title (human-readable identifier)" + value = cloudflare_workers_kv_namespace.app_kv.title +} diff --git a/infra/terraform/cloudflare-kv/terraform/outputs.tf b/infra/terraform/cloudflare-kv/terraform/outputs.tf new file mode 100644 index 0000000..9653334 --- /dev/null +++ b/infra/terraform/cloudflare-kv/terraform/outputs.tf @@ -0,0 +1,2 @@ +# Output definitions are in main.tf for simplicity. +# This file is a placeholder for consistency with the Orun Terraform pattern. diff --git a/infra/terraform/cloudflare-kv/terraform/variables.tf b/infra/terraform/cloudflare-kv/terraform/variables.tf new file mode 100644 index 0000000..9c92c69 --- /dev/null +++ b/infra/terraform/cloudflare-kv/terraform/variables.tf @@ -0,0 +1,2 @@ +# Variable definitions are in main.tf for simplicity. +# This file is a placeholder for consistency with the Orun Terraform pattern.