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
16 changes: 16 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ GITDB_SYNC_POLL_SECONDS=60
# NOTIFICATIONS_WEBHOOK_ALLOW_HTTP=false
# NOTIFICATIONS_WEBHOOK_ALLOW_PRIVATE_TARGETS=false

# Optional "Add to Slack" for Slack alert channels: users pick the workspace and channel in Slack
# instead of pasting an Incoming Webhook URL. Create the app from GitOps Settings → Slack (prefilled
# manifest, incoming-webhook scope only). Redirect URL: <ORIGIN>/notifications/slack/callback (https)
# SLACK_CLIENT_ID=
# SLACK_CLIENT_SECRET=
# The /gitops command of the same app (install the app, link users, connect channels from Slack)
# also needs the app's Signing Secret (Basic Information → App Credentials). Slack sends commands
# to <ORIGIN>/api/slack/commands, so the server must be reachable from the internet (locally: a
# tunnel such as cloudflared or ngrok, with ORIGIN set to the tunnel's https URL)
# SLACK_SIGNING_SECRET=
# To try it locally serve the dev server over https (Slack refuses http redirect URLs):
# mkdir -p .certs && openssl req -x509 -newkey rsa:2048 -nodes -days 365 -subj /CN=localhost \
# -addext subjectAltName=DNS:localhost -keyout .certs/localhost-key.pem -out .certs/localhost.pem
# then open https://localhost:5173 and register https://localhost:5173/notifications/slack/callback
# DEV_HTTPS=true

# Optional cluster-wide Google sign-in (OpenID Connect). Shown on the login page when both the
# client ID and secret are set. Redirect URI to register in GCP: <ORIGIN>/auth/sso/google/callback
GOOGLE_SSO_CLIENT_ID=
Expand Down
49 changes: 38 additions & 11 deletions .github/copilot-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ ejecución. SAML es configuración solo.
- Vitest, ESLint y Prettier
- Playwright para tests e2e de RBAC (`e2e/`)

Las variables de entorno se declaran en `src/env.ts` y se documentan en `.env.example`; Google SSO usa las `GOOGLE_SSO_*` descritas arriba.
Las variables de entorno se declaran en `src/env.ts` y se documentan en `.env.example`; Google SSO usa las
`GOOGLE_SSO_*` descritas arriba, "Add to Slack" usa `SLACK_CLIENT_ID` / `SLACK_CLIENT_SECRET` y el comando
`/gitops` también necesita `SLACK_SIGNING_SECRET` (opcionales).

Comandos:

Expand Down Expand Up @@ -121,16 +123,41 @@ y `project:notifications:update` gestionan destinatarios externos y canales. El
verificación y desuscripción. En `/org/<org>/settings/alerts/settings`, administradores configuran dominios
permitidos y canales (`NotificationChannelEntity` en GitDB). Alertas guardan `recipients` (usuarios, roles,
emails externos) y `channelIds` (referencias a canales, no destinatarios individuales). Canales de organización
se usan en alertas de org y proyecto; canales de proyecto solo en sus alertas. URLs de webhook se encriptan
con `GITDB_ENCRYPTION_KEY`. Emails externos: `pending` → `verified` → `unsubscribed` o `blocked`; template
sin campos `sensitive` ni links; `List-Unsubscribe` HMAC (90 días). Canales: un mensaje por canal en paralelo,
fallo de canal nunca afecta email. El tipo `webhook` se firma con HMAC-SHA256 (`X-GitOps-Signature`): secreto
`whsec_…` se muestra una sola vez al crear o rotar (rotación con 24 h de gracia, ambos secretos validan durante
el período); resuelve DNS rechazando IPs privadas/loopback/link-local/CGNAT/metadata (antes de guardar y cada
envío), no sigue redirects, reintentos con backoff (20 fallos consecutivos deshabilitan automáticamente, evento
`notifications.channel.disabled` con detalles del canal y error). Docs en `docs/webhooks/`. Variables:
`NOTIFICATIONS_WEBHOOK_ALLOW_HTTP` y `NOTIFICATIONS_WEBHOOK_ALLOW_PRIVATE_TARGETS` (solo dev/e2e). Rutas
públicas `/notifications/verify` y `/notifications/unsubscribe` aceptan POST. Sin `ORIGIN` no se envía nada.
se usan en alertas de org y proyecto; canales de proyecto solo en sus alertas. Cada canal tiene un idioma
(`locale`: es | en, por defecto en), que determina el idioma de etiquetas y eventos en sus mensajes.
URLs de webhook se encriptan con `GITDB_ENCRYPTION_KEY`. Emails externos: `pending` → `verified` → `unsubscribed`
o `blocked`; template sin campos `sensitive` ni links; `List-Unsubscribe` HMAC (90 días). Canales: un mensaje
por canal en paralelo, fallo de canal nunca afecta email. **Webhook** se firma con HMAC-SHA256
(`X-GitOps-Signature`): secreto `whsec_…` se muestra una sola vez al crear o rotar (24 h gracia, ambos secretos
validan); resuelve DNS rechazando IPs privadas/loopback; no sigue redirects; reintentos con backoff (20 fallos
= auto-deshabilita). Docs en `docs/webhooks/`. **Slack**: Incoming Webhook (solo URLs `https://hooks.slack.com/services/…`,
sin firma), formatea como Block Kit en el idioma del canal (nombre de alerta, resumen, campos, cambios de campos
con before/after, fecha en zona horaria del lector, nota de contexto y botón "Ver en GitOps" si hay link), escapa
caracteres Slack (`&<>`) en datos de usuario/evento. Un canal GitOps por webhook Slack. Los errores 4xx no se reintentan
y se muestran como último error. **Add to Slack** (`SlackConnectService`): cuando `SLACK_CLIENT_ID` / `SLACK_CLIENT_SECRET` están configurados,
los settings de canales muestran "Añadir a Slack"; usuario elige workspace y canal, GitOps crea el canal con webhook.
Admin abre `GitOps Settings → Slack`, crea la app Slack (manifest con scope `incoming-webhook` y redirect
`<ORIGIN>/notifications/slack/callback`, requiere https), copia credenciales al entorno y reinicia. En dev, usar
`DEV_HTTPS=true` con certificados en `.certs/` (ver `.env.example`).

**Comando `/gitops`** (`SlackAppService`, requiere `SLACK_SIGNING_SECRET`): organización instala el app desde la
página de canales (`installSlack` action, scope `commands`), guardando el bot token en `slack_workspaces`
(GitDB, una fila por team, vinculada a organización). Los endpoints `/api/slack/commands` y
`/api/slack/interactions` verifican la firma Slack (5 min window, `verifySlackSignature`) y actúan como el
usuario Slack vinculado (`authProviders` entry `slack` = `<team>:<user>`) con permiso `organization:notifications:create`.
Luego en Slack: (1) `/gitops login` → link firmado a `/notifications/slack/link` para vincular usuario a su
cuenta GitOps (10 min, una vez; `hooks.server.ts` preserva la URL en `gitops_return_to`); (2) `/gitops channel`
pide nombre e idioma → link firmado a `/notifications/slack/connect` que abre selector de canales Slack, crea el
canal en GitOps con webhook y posea test → `/notifications/slack/callback` → `/notifications/slack/done`;
(3) `/gitops status` muestra organización vinculada y canales conectados. Las rutas de Slack bypass CSRF porque
Slack envía requests sin `Origin` header; solo los endpoints firmados (`CSRF_EXEMPT_PATHS`) están excluidos.

Configuración: webhook Slack (URL: `https://hooks.slack.com/services/…`), canales tienen idioma (`locale: es|en`),
URLs de webhook encriptadas, emails externos: `pending` → `verified` o `blocked`; template sin campos `sensitive` ni
links internos; unsubscribe HMAC (90 días). **Webhook** se firma: secreto `whsec_…` (mostrado una sola vez), resuelve
DNS rechazando IPs privadas, 20 fallos consecutivos = auto-deshabilita. Variantes de desarrollo:
`NOTIFICATIONS_WEBHOOK_ALLOW_HTTP`, `NOTIFICATIONS_WEBHOOK_ALLOW_PRIVATE_TARGETS`. Rutas públicas
`/notifications/verify` y `/notifications/unsubscribe` aceptan POST sin sesión. Sin `ORIGIN` no se envía nada.

Al crear una organización (vía bootstrap o `/gitops-settings`), se llama automáticamente a
`roleService.createDefaultOrganizationRoles()`. Al crear un proyecto, se llama a
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ test-results/
playwright-report/
blob-report/
creport-cloud.sh
creport-local.sh
creport-local.sh
# local https certificates (DEV_HTTPS)
.certs/
89 changes: 87 additions & 2 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ The roadmap is in `IDEAS.md`. Do not describe roadmap items as implemented featu
- Vitest, ESLint, and Prettier
- Playwright for RBAC end-to-end tests (`e2e/`)

Environment variables are declared in `src/env.ts` and documented in `.env.example`; Google SSO uses the `GOOGLE_SSO_*` variables described above.
Environment variables are declared in `src/env.ts` and documented in `.env.example`; Google SSO uses the
`GOOGLE_SSO_*` variables described above, "Add to Slack" uses `SLACK_CLIENT_ID` / `SLACK_CLIENT_SECRET`,
and the `/gitops` Slack command also needs `SLACK_SIGNING_SECRET`.

Email notification templates (`src/notifications/`) are read from disk at runtime; the Docker image copies them so emails can be sent from the container.

Expand Down Expand Up @@ -359,7 +361,11 @@ in `docs/adr/0001-event-bus-and-alerts.md`.
all its projects) or a project (only its alerts). `type` is `webhook`, `slack` or `google-chat`;
a type is available only once its transport is registered with `registerChatTransport()`
(`#lib/server/infra/notifications/chat-notification.ts`) in the composition root
(`$modules/notifications/index.ts`); today only `webhook` is, and tests use `FakeChatTransport`.
(`$modules/notifications/index.ts`); today `webhook` and `slack` are, and tests use
`FakeChatTransport`. Providers with incoming webhooks on a fixed host (Slack, Google Chat) only
write a formatter: `createFixedHostChatTransport({ rule, format })`
(`chat-webhook.transport.ts`) does the URL rule, guarded fetch and `postJson`; `truncateText`
(`chat-format.ts`) is shared for provider length limits.
A `ChatTransport` implements `send()` and `validateDestination()` (sync or async), the anti-SSRF
check run before saving and again before every send: `validateWebhookUrl()` for providers with a
fixed host (https, no credentials, no IP literal, default port, allowed host and path),
Expand All @@ -372,6 +378,10 @@ in `docs/adr/0001-event-bus-and-alerts.md`.
leaves the server masked (`https://hooks.slack.com/…/••••1a2b`); changing it means typing it
again. `enabled` flag can disable delivery without deleting the channel; `disabledReason:
'failures'` means the platform disabled it after 20 consecutive failures.
- Channel language: `NotificationChannelEntity.locale` (`es` | `en`, rows without it read as `en`,
`CHANNEL_LOCALES` in `chat-types.ts`), set in the channel forms and the API (`locale`, invalid →
`invalid_locale`). The engine passes `deliver()` a builder `(locale) => ChannelMessage`
(`ChannelMessageSource`) so each channel gets its own language; the test message uses it too.
- Signed channels: webhook is a signed type. The service generates a signing secret on create
(`whsec_` + 32 random bytes, encrypted in `signingSecret`) and returns it in clear **only once**
from `createChannel()` or `POST /api/notification-channels/[id]/rotate-secret` (never readable
Expand All @@ -380,6 +390,81 @@ in `docs/adr/0001-event-bus-and-alerts.md`.
with a stable `deliveryId` per message so receivers can deduplicate retries. The signature is
`X-GitOps-Signature: t=<unix>,v1=<hash>[,v1=<old-hash>]` using `HMAC-SHA256(secret,
"<timestamp>.<raw body>")` on the JSON sent (not encoded).
- Slack (`#lib/server/infra/notifications/slack/`): Incoming Webhook, URL limited to
`https://hooks.slack.com/services/` only (`SLACK_WEBHOOK_RULE`), unsigned. `buildSlackPayload()` turns
the `ChannelMessage` into Block Kit in the channel's language only: header (alert name, `plain_text`), the
summary only when it differs from the event label, a fields section with `location`
(`Project`/`Organization`: name) and the date (`slackDate()`, `<!date^…>`, reader's time zone),
a divider and a `*Changes*` title (`labels.changes`), then one section per change (`*Label*` /
`Before: …` / `New: …`, `labels.empty` for a `null` side,
only the label when both are `null`), remaining field sections of 10, "View in GitOps" button,
and `note` as context. Every `mrkdwn` value goes through `escapeSlackText()`
(`&`, `<`, `>`) so event or user data can't mention or link. Texts are cut to Block Kit limits.
The Slack channel is fixed by the webhook: one GitOps channel per Slack webhook. 4xx responses
(revoked webhook, archived or deleted channel) are not retried and show as the channel's last error.
- Add to Slack (`slackConnectService` in `$modules/notifications`, `#97`): OAuth v2 install
(`SlackConnectService`, `SlackOAuthClient`) with only the `incoming-webhook` scope, so Slack asks for
the workspace and channel and returns its webhook URL, saved as a normal `slack` channel (same transport,
same `SLACK_WEBHOOK_RULE`). Enabled when `SLACK_CLIENT_ID` and `SLACK_CLIENT_SECRET` are both set
(`readSlackAppConfig()`; half set counts as disabled). Picking the Slack card in the create-channel
form shows an "Add to Slack" button (`formaction` to the `connectSlack` action, `channels-page.ts`;
the enhanced submit follows the redirect with `window.location`; Enter keeps creating the manual channel)
next to the manual URL fields, and GitOps admins get a link to `/gitops-settings/slack` while it is not
configured. The action needs `<scope>:notifications:create` and a session, not an API key; it sets
the `gitops_slack_flow` cookie (`signToken`, 10 min, bound to the user, location, locale and a return
path) and redirects to Slack; `/notifications/slack/callback` (fixed: it is the app's redirect URL,
needs the same session) checks the state, exchanges the code (Basic auth), creates the channel through
`createChannel()` (permission checked again) and goes back with `?slack=connected|<error>`
(`notifications.errors.slack_*`). Self-hosted admins create the app from `/gitops-settings/slack`
(prefilled manifest link, redirect URL, status); a hosted GitOps uses the official app's credentials
the same way. Pasting a webhook URL keeps working. A central OAuth broker for self-hosted installations
is not implemented.
- `/gitops` (`slackAppService` in `$modules/notifications`): the same Slack app with the `commands`
scope, enabled when `SLACK_SIGNING_SECRET` is also set (`slackConnectService.commandsEnabled()`).
An organization installs it from its channels page (`installSlack` action, OAuth with only
`commands`, flow `kind: 'install'`): `slack_workspaces` (GitDB) keeps one row per Slack team, linked
to that organization (another organization gets `slack_workspace_taken`), with the bot token
encrypted (`ChannelCipher` purpose `slack-bot-token`; only used to open forms, `views.open`).
`/api/slack/commands` and `/api/slack/interactions` have no session or API key: they verify
`X-Slack-Signature` (`verifySlackSignature`, 5 min window) on the raw body and act as the GitOps
user linked to the Slack user (`authProviders` entry `slack` = `<team>:<user>`, `findByExternalIdentity`),
refusing disabled users and checking `organization:notifications:create` every time.
`/gitops login` answers with a signed 10-minute link to `/notifications/slack/link` (needs a session:
`hooks.server.ts` remembers it in `gitops_return_to` and the password and Google logins come back to
it, `#lib/server/return-to`; the user must see the workspace's organization). `/gitops channel` opens a
modal (name, locale); its submission answers with a signed link to `/notifications/slack/connect`
(no session), which starts Slack's channel picker (flow `kind: 'command'`, bound to the Slack team
and user: the callback refuses another one with `slack_wrong_workspace`). `/notifications/slack/callback`
then accepts requests without a session (only `command` flows can finish without one), creates the
channel through `createChannel()` as the linked user (`runWithActor`), posts a test message and ends
on `/notifications/slack/done`. Replies use the workspace's locale (picked at install).
- CSRF: SvelteKit's origin check is disabled in the framework (`csrf.trustedOrigins: ['*']`) and done
by `csrfGuard` in `hooks.server.ts` with the same rule (`#lib/server/csrf`), because Slack posts
forms without an `Origin` header. Only `CSRF_EXEMPT_PATHS` (the two signed Slack endpoints) skip it;
never add a path there that does not authenticate its requests by other means.

Local development of Slack OAuth flows requires HTTPS (Slack refuses `http://` redirect URLs).
Set `DEV_HTTPS=true` and create a self-signed certificate:
```bash
mkdir -p .certs && openssl req -x509 -newkey rsa:2048 -nodes -days 365 -subj /CN=localhost \
-addext subjectAltName=DNS:localhost -keyout .certs/localhost-key.pem -out .certs/localhost.pem
```
Then open `https://localhost:5173` and register `https://localhost:5173/notifications/slack/callback`
in the test Slack app. See `.env.example` for details.
- `ChannelMessage` (built by `buildAlertChannelMessage(input, locale)`): `fields` leave out what
the message says elsewhere: `occurredAt`, `location` (project or organization, and any field with
that name), and `changedFields` when `changes` (translated label + before/after) exists.
`labels` carries the translated `Date`/`Changes`/`Before`/`New`/`empty`, and `text` only adds the summary
when it differs from the event label, so nothing is said twice in two languages.
`note` is the translated side note (test send, throttled count); the raw `suppressed` count is
still there. `occurredAt` and `location` are always present; `changes` only for update events;
`labels` only when any of `occurredAt`, `location` or `changes` is used by the transport.
- Update events carry `changes?: FieldChange[]` (`{ field, before, after }`, plain text cut to 200
chars by `fieldChange()` in `#lib/events/field-changes.ts`, `null` when empty or not shown):
`project.updated` (modules as the enabled list, organization by name, settings without values)
and `organization.updated`. Never put secrets in them; `vault.secret.updated` has none. `FieldChange`
is defined in `src/lib/events/domain-events.ts` with the type parameter for field names and is
used by channels (Slack shows before/after) and by the update events in payloads.
- Generic webhook (`#lib/server/infra/notifications/webhook/`): `POST` with the versioned
`WebhookPayload` (`version`, `deliveryId`, `test`, `alert`, `event` with the raw non-sensitive
catalog `fields` from `ChannelMessage.event.fields`, `link`), headers `X-GitOps-Event`,
Expand Down
Loading
Loading