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
1 change: 1 addition & 0 deletions hooks-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ rotate without a restart.
| `-public-base-url` | `PUBLIC_BASE_URL` | | public base URL of a developer's bb server, used in the Slack set-up replies; no default |
| `-slack-team-id` | `SLACK_TEAM_ID` | | the Slack app's workspace id; discovered from the bot token when empty |
| `-slack-app-id` | `SLACK_APP_ID` | | the Slack app's id; discovered from the bot token when empty |
| `-slack-alert-channels` | `SLACK_ALERT_CHANNELS` | | comma-separated Slack channel ids (C…/G…) the alert-channel history and reply endpoints may use; empty disables both |
| `-slack-api` | | `https://slack.com/api` | Slack Web API root |
| `-v` | | `false` | debug logging |
| | `SENTRY_DSN` | | report the service's own errors to Sentry; unset disables reporting |
Expand Down
8 changes: 8 additions & 0 deletions hooks-service/cmd/hooks-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func run() error {
publicBase = flag.String("public-base-url", os.Getenv("PUBLIC_BASE_URL"), "the public base URL of a developer's bb server, used in the Slack set-up replies; no default, the deployment sets it")
slackTeam = flag.String("slack-team-id", os.Getenv("SLACK_TEAM_ID"), "the team app's workspace id; discovered from the bot token when empty")
slackApp = flag.String("slack-app-id", os.Getenv("SLACK_APP_ID"), "the team app's id; discovered from the bot token when empty")
slackAlert = flag.String("slack-alert-channels", os.Getenv("SLACK_ALERT_CHANNELS"), "comma-separated Slack channel ids (C…/G…) the alert-channel history and reply endpoints may use; empty disables both")
slackAPI = flag.String("slack-api", hooks.SlackAPI, "Slack Web API root")
verbose = flag.Bool("v", false, "debug logging")
)
Expand Down Expand Up @@ -121,6 +122,12 @@ func run() error {
// bot token posts as the app, and the OAuth client secret is used only
// here to exchange a developer's "Sign in with Slack" code. None of the
// three ever reaches a pod.
var alertChannels []string
for _, id := range strings.Split(*slackAlert, ",") {
if id = strings.TrimSpace(id); id != "" {
alertChannels = append(alertChannels, id)
}
}
slack := &hooks.Slack{
SigningSecret: []byte(strings.TrimSpace(os.Getenv("SLACK_SIGNING_SECRET"))),
SigningSecretFile: *slackSec,
Expand All @@ -133,6 +140,7 @@ func run() error {
PublicBaseURL: strings.TrimSpace(*publicBase),
TeamID: strings.TrimSpace(*slackTeam),
AppID: strings.TrimSpace(*slackApp),
AlertChannels: alertChannels,
API: *slackAPI,
HTTP: &http.Client{Timeout: 10 * time.Second},
}
Expand Down
96 changes: 96 additions & 0 deletions hooks-service/docs/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,102 @@ The response is the file's bytes with its `Content-Type` and no caching.
502 `{"error":"slack-refused"}` and 503 `{"error":"slack-unavailable"}`
are as for the other Slack calls.

### Alert-channel history and reply

Alertmanager posts a Kubernetes Job-failure alert into a channel with its
own Slack receiver, so bb holds no session for that message and the
ordinary reply path cannot reach it. Two additive, cluster-only endpoints
let a developer's server find the alert's original message and post exactly
one threaded reply under it, and nothing else. Both are on the cluster-only
listener (8081), beside the other `/v1/slack` routes, and both are disabled
unless the deployment sets an allow-list:

`-slack-alert-channels` (or `SLACK_ALERT_CHANNELS`) is a comma-separated
list of channel ids (`C…` public or `G…` private). When it is empty (the
default) both endpoints answer 404 `{"error":"not-enabled"}` and make no
Slack call. Only a channel on the list is readable or replyable; the bot
must also be a member of it.

#### `GET /v1/slack/alert-channel/history`

```
GET /v1/slack/alert-channel/history?connection=c_7f3a...&channel=C0123&limit=20&oldest=1700000000.000000
```

- The connection must have a current Slack identity, exactly as for the
ephemeral endpoint: 403 `{"error":"not-identified"}` without one, 404
`{"error":"unknown-connection"}` for an id the service does not know.
- `channel` is required and must be on the allow-list; anything else is 403
`{"error":"channel-not-allowed"}` before Slack is called.
- `limit` is optional, 1 to 50, default 20. `oldest` is optional and, when
set, is passed to Slack as a timestamp (a number of seconds, Slack's `ts`
form); a non-numeric `oldest` or an out-of-range `limit` is 400.
- It calls `conversations.history` with the bot token, one call, no
pagination, and returns the messages, newest first as Slack returns them,
trimmed to what an alert match needs:
`ts`, `thread_ts`, `user`, `bot_id`, `text`, and each attachment's
`title`, `text`, `fallback` and `fields`. Nothing else Slack sent is
returned, and Slack's error text is never passed back.
```json
{"messages": [{"ts": "1700000000.000100", "thread_ts": "1700000000.000100", "bot_id": "B0123", "text": "Firing: KubeJobFailed", "attachments": [{"title": "KubeJobFailed", "text": "Job backup failed", "fallback": "Firing", "fields": [{"title": "job", "value": "backup", "short": true}]}]}]}
```
- Every returned `ts` is remembered as **matched** for that connection and
channel, in memory, for the same 24-hour window as a routed session and
bounded in size. The reply endpoint accepts only those.
- The read needs the bot scopes `channels:history` and `groups:history`; a
token without them answers 503 `{"error":"missing-scope"}`. The bot must
be a member of the channel, or Slack answers `not_in_channel`, logged and
answered 502 `{"error":"slack-refused"}`.

#### `POST /v1/slack/alert-channel/reply`

```
POST /v1/slack/alert-channel/reply
{"connection": "c_7f3a...", "channel": "C0123", "parent_ts": "1700000000.000100",
"request_id": "a1b2c3", "text": "On it. Done by Alice's bb, as Alice."}
```

- The same identity and allow-list checks apply (403 `not-identified`, 404
`unknown-connection`, 403 `channel-not-allowed`).
- `parent_ts` must be a `ts` a history read returned for the **same
connection and channel** and that has not lapsed; anything else is 403
`{"error":"parent-not-matched"}` and makes no Slack call. A match made by
another connection, or for another channel, does not count.
- `text` is required and posted verbatim; a body over 32 KiB is 413 and a
missing or malformed field is 400. It is text only: no Block Kit blocks
and no `reply_broadcast`.
- The only write is one `chat.postMessage` with `thread_ts` set to
`parent_ts`. One reply is allowed per `(connection, channel, parent_ts)`:
a repeat with the same `request_id` returns 200
`{"ok":true,"duplicate":true}` without posting, and a repeat with a
different `request_id` is 409 `{"error":"already-replied"}`. While the
first post is still in flight a repeat gets 409 `{"error":"in-flight"}`
and should be retried; a post Slack refused releases the claim, so a
retry with the same `request_id` can post.

The error cases for both endpoints:

| Status | Error | When |
|---|---|---|
| 404 | `not-enabled` | the allow-list flag is empty |
| 404 | `unknown-connection` | the connection id is unknown |
| 403 | `not-identified` | the connection has no current Slack identity |
| 403 | `channel-not-allowed` | the channel is not on the allow-list |
| 403 | `parent-not-matched` | reply only: `parent_ts` was not matched for this connection and channel |
| 409 | `already-replied` | reply only: another `request_id` already posted |
| 409 | `in-flight` | reply only: the first post for this parent is still in flight; retry |
| 400 | (plain text) | a missing or malformed field, a bad `limit` or `oldest` |
| 413 | (plain text) | a reply body over 32 KiB |
| 503 | `missing-scope` | the bot token lacks `channels:history`/`groups:history` |
| 503 | `slack-unavailable` | Slack could not be reached |
| 502 | `slack-refused` | Slack refused the call (e.g. `not_in_channel`) |

The match and reply state is in memory only, like the session and ephemeral
state. A hooks restart loses both: the consumer re-reads history, which is
safe, but it also forgets that a reply was posted, so a retry across a
restart could post twice. That is an accepted limit; the consumer keeps its
own reply-once record.

### Credentials and setup

The service reads three keys from the one `hooks-service` Secret, mounted
Expand Down
2 changes: 2 additions & 0 deletions hooks-service/hooks/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ func (s *Server) Subscriber() http.Handler {
mux.HandleFunc("POST /v1/slack/bot/join", s.Slack.handleJoin)
mux.HandleFunc("POST /v1/slack/conversations", s.Slack.handleConversations)
mux.HandleFunc("POST /v1/slack/ephemeral", s.Slack.handleEphemeral)
mux.HandleFunc("GET /v1/slack/alert-channel/history", s.Slack.handleAlertHistory)
mux.HandleFunc("POST /v1/slack/alert-channel/reply", s.Slack.handleAlertReply)
mux.HandleFunc("POST /v1/slack/sessions/{session}/message", s.Slack.handleReply)
mux.HandleFunc("GET /v1/slack/sessions/{session}/files/{id}", s.Slack.handleFile)
mux.HandleFunc("POST /v1/slack/sessions/{session}/status", s.Slack.handleStatus)
Expand Down
Loading