diff --git a/infrastructure/AGENTS.md b/infrastructure/AGENTS.md index 0be0a4dc..f0b95cf1 100644 --- a/infrastructure/AGENTS.md +++ b/infrastructure/AGENTS.md @@ -11,10 +11,10 @@ Common to all modules: Terraform **1.13.0** (`.terraform-version`, tfenv), state ### `aws/` (state key `aws/terraform.tfstate`) Application infra. Providers: AWS 6.14.1, Infisical. - `dsql.tf` — `aws_dsql_cluster.branch`, the production database (deletion-protected, `prevent_destroy`). IAM-token auth only: `branch-lambda-role` and `branch-ci-migrate` get `dsql:DbConnectAdmin` on its ARN, and `lambda.tf` passes `local.dsql_endpoint` as `DB_HOST`. RDS was deleted after the 2026-10-15 cutover; its final snapshot is `branch-rds-final-20261015`. -- `lambda.tf` — 6 Lambda functions (auth/donors/expenditures/projects/reports/users, Node 20.x, 256MB, 30s), IAM role (CloudWatch Logs + pool-scoped `cognito-idp:AdminDeleteUser`/`AdminGetUser` for the registration-rollback and `DELETE /users/{userId}` paths + `s3:PutObject`/`s3:GetObject` on the reports bucket), deployment S3 bucket. **`lifecycle` ignores `s3_key` only** — code is deployed by CI (`lambda-deploy`), not Terraform. Env: `NODE_ENV`, `DB_*`, `COGNITO_USER_POOL_ID`, `COGNITO_CLIENT_ID`, `REPORTS_BUCKET_NAME`, `NODE_OPTIONS`, `SENTRY_DSN`, `SENTRY_ENVIRONMENT`, `OTEL_EXPORTER_OTLP_ENDPOINT`, `OTEL_EXPORTER_OTLP_HEADERS`. Attaches the Sentry layer (`modules/sentry`); the SDK lives only in that layer, never in the bundles, and `NODE_OPTIONS` is what activates it — drop either and errors stop reporting with no other symptom. `SENTRY_DSN` comes from Infisical `/sentry` (`sentry-dsn`) and `OTEL_EXPORTER_OTLP_HEADERS` from `/grafana`, not the tree. **The `environment` block is authoritative:** any var set by hand in the console and not declared here is deleted on the next apply, which previously took out authentication across all six lambdas. `AWS_REGION` is Lambda-reserved and must stay absent. +- `lambda.tf` — 6 Lambda functions (auth/donors/expenditures/projects/reports/users, Node 20.x, 256MB, 30s), IAM role (CloudWatch Logs + pool-scoped `cognito-idp:AdminDeleteUser`/`AdminGetUser` for the registration-rollback and `DELETE /users/{userId}` paths + `s3:PutObject`/`s3:GetObject` on the reports bucket), deployment S3 bucket. **`lifecycle` ignores `s3_key` only** — code is deployed by CI (`lambda-deploy`), not Terraform. Env: `NODE_ENV`, `DB_*`, `COGNITO_USER_POOL_ID`, `COGNITO_CLIENT_ID`, `REPORTS_BUCKET_NAME`, `SES_FROM_ADDRESS` (sender for the expenditures lambda's approve/deny emails, `mailer.ts`), `NODE_OPTIONS`, `SENTRY_DSN`, `SENTRY_ENVIRONMENT`, `OTEL_EXPORTER_OTLP_ENDPOINT`, `OTEL_EXPORTER_OTLP_HEADERS`. Attaches the Sentry layer (`modules/sentry`); the SDK lives only in that layer, never in the bundles, and `NODE_OPTIONS` is what activates it — drop either and errors stop reporting with no other symptom. `SENTRY_DSN` comes from Infisical `/sentry` (`sentry-dsn`) and `OTEL_EXPORTER_OTLP_HEADERS` from `/grafana`, not the tree. **The `environment` block is authoritative:** any var set by hand in the console and not declared here is deleted on the next apply, which previously took out authentication across all six lambdas. `AWS_REGION` is Lambda-reserved and must stay absent. - `cognito.tf` — user pool (email sign-in, auto-verify, 8-char password policy, `advanced_security_mode = AUDIT`, `mfa_configuration = OPTIONAL` with `software_token_mfa_configuration { enabled = true }`, deletion protection) + public client (1h access/ID tokens, 30d refresh, no secret). Outputs `cognito_user_pool_id` / `cognito_client_id`; the lambdas get these from `lambda.tf` directly, no manual step. Infisical `/aws/cognito/` is still the source for the `COGNITO_*` GitHub Actions secrets used by `lambda-tests.yml` — keep it in sync if the pool is ever recreated. Threat protection is AUDIT rather than ENFORCED because every sign-in is proxied through the auth lambda, so adaptive auth would risk-score one shared ENI address. TOTP MFA is opt-in per user (`POST /auth/mfa-setup`/`/mfa-verify`/`/mfa-disable` in the auth lambda) — never forced at login. Both email bodies live in `aws/templates/*.html.tftpl` and are pulled in with `templatefile()` — edit the HTML there, not in the `.tf`. `invite-email.html.tftpl` feeds `admin_create_user_config` (sent by `POST /users` → AdminCreateUser); `verification-email.html.tftpl` feeds `verification_message_template`, which Cognito uses for **both** the sign-up code and the forgot-password code — keep that copy neutral between them, and keep `default_email_option = "CONFIRM_WITH_CODE"` because the auth lambda confirms with a code, not a link. Each template takes one variable, `app_url`. Their links come from `local.app_url` in `dns.tf`, which is the custom domain once attached and the CloudFront domain before that. `{username}` survives only inside an HTML comment: Cognito rejects an invite template that omits it, but `username_attributes = ["email"]` makes it render the pool-generated UUID, so it must never be shown. Sending is `COGNITO_DEFAULT` until `var.enable_ses_email` flips `email_configuration` to SES — see `email.tf`. - `dns.tf` — Route 53 zone + ACM certificates for `var.app_domain`, and `local.app_url`. The app lives at **`branchaccounting.org`** (frontend, apex) and **`api.branchaccounting.org`** (API Gateway), registered through Route 53 Domains in the client account, so `use_existing_hosted_zone` and `enable_custom_domain` both default to true. Three states: empty `app_domain` creates nothing; `app_domain` set creates the zone and requests certificates; `+ enable_custom_domain` completes validation and attaches the domain to CloudFront and API Gateway. The second step is separate because ACM validates over public DNS and would otherwise block the apply for 45 minutes before failing. **`var.use_existing_hosted_zone` picks where the zone comes from:** false creates `aws_route53_zone.app` (domain registered elsewhere, NS set by hand at the registrar), true adopts the zone via `data.aws_route53_zone.app`. Set it to true when the domain is bought through Route 53 Domains — registration creates a hosted zone automatically and points the domain at it, so creating a second zone here would leave two zones with different nameservers and resolution silently following whichever set the registrar holds. Everything downstream reads `local.zone_id`, never the resource directly. -- `email.tf` — outbound mail identity for `var.app_domain`: SES domain identity, Easy DKIM, a `mail.` custom MAIL FROM, and the Route 53 records that make them resolve (`_amazonses` TXT, three `_domainkey` CNAMEs, MAIL FROM MX + SPF, apex SPF, `_dmarc` TXT at `p=none`). Gated on `var.app_domain` like `dns.tf`, so an empty domain creates nothing. `var.enable_ses_email` is the second gate: it swaps `cognito.tf`'s `email_configuration` from `COGNITO_DEFAULT` to `DEVELOPER` sending as `no-reply@`. **Do not flip it until DKIM reads Verified in SES and the account has SES production access** — in the SES sandbox `DEVELOPER` mode can only mail verified addresses, so every invitation to a new user fails outright. `COGNITO_DEFAULT` sends from AWS's shared `no-reply@verificationemail.com`, which no domain can authenticate and which is capped at 50 messages/day; that is why invitations landed in spam. +- `email.tf` — outbound mail identity for `var.app_domain`: SES domain identity, Easy DKIM, a `mail.` custom MAIL FROM, and the Route 53 records that make them resolve (`_amazonses` TXT, three `_domainkey` CNAMEs, MAIL FROM MX + SPF, apex SPF, `_dmarc` TXT at `p=none`). Gated on `var.app_domain` like `dns.tf`, so an empty domain creates nothing. `var.enable_ses_email` is the second gate: it swaps `cognito.tf`'s `email_configuration` from `COGNITO_DEFAULT` to `DEVELOPER` sending as `no-reply@`. **It defaults to true:** us-east-2 was granted SES production access on 2026-10-17 (case 179201951700548) and DKIM is Verified. Never enable it in an account still in the SES sandbox — `DEVELOPER` mode can only mail verified addresses there, so every invitation to a new user fails outright. `DEVELOPER` mode sends through the `AWSServiceRoleForAmazonCognitoIdp` service-linked role, which Cognito creates on the first apply; no SES identity policy is needed. Mail is transactional only (Cognito invite, verification and password reset, plus the expenditures lambda's approve/deny notice via `SES_FROM_ADDRESS`), so it carries no unsubscribe link; bounces and complaints go to the account-level SES suppression list, and DKIM (`d=`) plus the `mail.` MAIL FROM keep it DMARC-aligned. `COGNITO_DEFAULT` sends from AWS's shared `no-reply@verificationemail.com`, which no domain can authenticate and which is capped at 50 messages/day; that is why invitations landed in spam. - `api_gateway.tf` — REST API, one resource per lambda, method routing, `AWS_PROXY` integration, `prod` stage. - `s3.tf` — private reports bucket + versioned/encrypted lambda-deployments bucket. The reports bucket was public-read until the lambda role gained `s3:GetObject`; reports are now served only through the presigned `GET /reports/{id}/download`, so nothing may reintroduce a public bucket policy. - `frontend_hosting.tf` — static frontend: private S3 bucket + CloudFront (OAC) with an SPA fallback (403/404 → `/index.html`) and an index-rewrite CloudFront Function. The Next.js app is exported (`output: 'export'`) and synced to S3 by the `frontend-deploy` workflow. diff --git a/infrastructure/aws/README.md b/infrastructure/aws/README.md index 9424fb45..80e1e1ff 100644 --- a/infrastructure/aws/README.md +++ b/infrastructure/aws/README.md @@ -118,7 +118,7 @@ | [app\_domain](#input\_app\_domain) | Domain serving the app; apex or subdomain, resolved by a Route 53 zone | `string` | `"branchaccounting.org"` | no | | [dmarc\_report\_email](#input\_dmarc\_report\_email) | Mailbox receiving DMARC aggregate reports; the rua tag is omitted when empty | `string` | `""` | no | | [enable\_custom\_domain](#input\_enable\_custom\_domain) | Attach the custom domain to CloudFront and API Gateway | `bool` | `true` | no | -| [enable\_ses\_email](#input\_enable\_ses\_email) | Send Cognito invitations from SES on var.app\_domain instead of Cognito's shared sender | `bool` | `false` | no | +| [enable\_ses\_email](#input\_enable\_ses\_email) | Send Cognito invitations from SES on var.app\_domain instead of Cognito's shared sender | `bool` | `true` | no | | [infisical\_client\_id](#input\_infisical\_client\_id) | n/a | `string` | n/a | yes | | [infisical\_client\_secret](#input\_infisical\_client\_secret) | n/a | `string` | n/a | yes | | [infisical\_workspace\_id](#input\_infisical\_workspace\_id) | n/a | `string` | `"d1ee8b80-118c-4daf-ae84-31da43261b76"` | no | diff --git a/infrastructure/aws/lambda.tf b/infrastructure/aws/lambda.tf index 8775b016..573858aa 100644 --- a/infrastructure/aws/lambda.tf +++ b/infrastructure/aws/lambda.tf @@ -247,6 +247,9 @@ resource "aws_lambda_function" "functions" { # wipe it. Harmless on the other five functions. REPORTS_BUCKET_NAME = aws_s3_bucket.reports_bucket.id + # Unset, expenditures/mailer.ts falls back to an unverified address and SES rejects it. + SES_FROM_ADDRESS = local.create_dns ? "BRANCH <${local.ses_from_address}>" : "" + # Loads the Sentry layer's handler wrapper before the function starts. # Dropping NODE_OPTIONS silently disables error reporting -- the layer # alone does nothing. diff --git a/infrastructure/aws/variables.tf b/infrastructure/aws/variables.tf index 14420122..9d80a558 100644 --- a/infrastructure/aws/variables.tf +++ b/infrastructure/aws/variables.tf @@ -24,11 +24,11 @@ variable "enable_custom_domain" { default = true } -# Keep false until DKIM is Verified and the account is out of the SES sandbox. +# Requires DKIM Verified and SES production access, else invitations fail. variable "enable_ses_email" { description = "Send Cognito invitations from SES on var.app_domain instead of Cognito's shared sender" type = bool - default = false + default = true } variable "dmarc_report_email" {