Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions infra/terraform/cloudflare-kv/component.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
apiVersion: sourceplane.io/v1
kind: Component

metadata:
name: cloudflare-kv
description: Provisions the Cloudflare KV namespace the site's content store and its enrichment 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_<ENV>.
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://agent-visibility/agent-visibility/{{ .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://agent-visibility/agent-visibility/{{ .environment }}/CLOUDFLARE_ACCOUNT_ID"
TF_VAR_cloudflare_account_id: "secret://agent-visibility/agent-visibility/{{ .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
43 changes: 43 additions & 0 deletions infra/terraform/cloudflare-kv/terraform/adopt.tf
Original file line number Diff line number Diff line change
@@ -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}"
}
5 changes: 5 additions & 0 deletions infra/terraform/cloudflare-kv/terraform/backend.tf
Original file line number Diff line number Diff line change
@@ -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.
140 changes: 140 additions & 0 deletions infra/terraform/cloudflare-kv/terraform/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
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 = "agent-visibility"
}

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 site's content store lives in ---
#
# Bound by apps/web as VISIBILITY_CACHE: upstream's store (src/lib/store.ts).
# Keys are the Worker's — `resources:raw` (absent until its owner posts a
# page, the Worker serving upstream's sample pages until then; then the
# samples and every posted page) and `resources:enriched` (their Workers AI
# enrichment, put with `expirationTtl`, 3600 s or 60 s when degraded). Every
# expiry is per-PUT, 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_<ENV> 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 site's content store lives 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
}
2 changes: 2 additions & 0 deletions infra/terraform/cloudflare-kv/terraform/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Output definitions are in main.tf for simplicity.
# This file is a placeholder for consistency with the Orun Terraform pattern.
2 changes: 2 additions & 0 deletions infra/terraform/cloudflare-kv/terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Variable definitions are in main.tf for simplicity.
# This file is a placeholder for consistency with the Orun Terraform pattern.
Loading