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
5 changes: 5 additions & 0 deletions docs/incidents/nt_alerts_channel_alerts_only.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# nt. `nt_alerts_channel_alerts_only`

**What happened:** A team's alert channel, set up for production alarms, also got routine auto-merge status cards: a merge conflict that needed a person's call, and a wait on an approving review the branch's rules ask for. Every sender that wanted "the team's channel" opened the alert channel's settings item (`ALERT_NOTIFY`) itself: the merge gate, security and infrastructure cards, plugin reviews, turned-off webhook endpoints, knowledge sign-ins, and log findings whose own setting was never saved. The merge gate posted once per commit and wording, so each push could post again, and it posted a wait on a person's review, which is a pull request's normal state.

**The guarantee:** Every team-wide message has a kind: alert (a production alarm or incident and its follow-ups, a critical security finding), action (a person has to do something CoderHelm can't) or update (news). Its channels come from `common::notify::route` by that kind, and a test fails if any other file reads the alert channel's item. An alert channel saved before kinds existed takes alerts only; action and updates go to the team's updates channel (`UPDATES_NOTIFY`), or nowhere when there is none, and a team can tick more kinds for either channel in Settings, Alerts. The merge gate never posts a wait on people's reviews to a channel (the PR's status comment says it once), posts a conflict that needs a person only where action messages go, and posts it once per PR and reason (`await_merge::told_sk`). The log analyzer Lambda follows the same rule for new findings (`team_update_channels`).
37 changes: 33 additions & 4 deletions lambda/log-analyzer/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,7 @@ def record_connection_success(team_id, account_id):

REC_NOTIFY_SK = "REC_NOTIFY"
TEAM_CHANNEL_SK = "ALERT_NOTIFY" # the team's alert channel (common::alert_notify)
UPDATES_CHANNEL_SK = "UPDATES_NOTIFY" # the team's updates channel (common::notify::route)
WEBHOOK_HOST_SUFFIXES = (".powerplatform.com", ".logic.azure.com", ".webhook.office.com")
SLACK_PREFIX = "https://hooks.slack.com/services/"
SEVERITY_ORDER = {"critical": 0, "warning": 1, "info": 2}
Expand Down Expand Up @@ -1462,6 +1463,30 @@ def pick_channels(mode, own, team, team_enabled):
return {}


def channel_kinds(sk, item):
"""The kinds of message a team channel takes: its saved `kinds`, or, saved
before kinds existed, alerts only for the alert channel and action and
updates for the updates channel. Matches common::notify::route::kinds."""
saved = item.get("kinds")
if saved is None:
return {"action", "update"} if sk == UPDATES_CHANNEL_SK else {"alert"}
return {k for k in saved if k in ("alert", "action", "update")}


def team_update_channels(team_id, alerts_item, updates_item):
"""{channel: url} a findings update posts to with the team's channels:
the updates channel first, then the alert channel only when it was set to
take updates; never an alert channel that takes alerts only. Matches
common::notify::route::pick for Kind::Update."""
out = {}
for sk, item in ((UPDATES_CHANNEL_SK, updates_item), (TEAM_CHANNEL_SK, alerts_item)):
if not item or not bool(item.get("enabled", False)) or "update" not in channel_kinds(sk, item):
continue
for ch, url in open_channels(team_id, item).items():
out.setdefault(ch, url)
return out


def pick_webhook(mode, own_url, team_url, team_enabled):
"""One Teams URL (kept for callers of the single-channel shape)."""
own = {"teams": own_url} if valid_webhook_url(own_url) else {}
Expand Down Expand Up @@ -1680,13 +1705,17 @@ def notify_new_recommendations(team_id, account_id, recs):
mode = effective_mode(own.get("notify_mode"))
if mode == "off":
return
team = settings_table.get_item(Key={"pk": team_id, "sk": TEAM_CHANNEL_SK}).get("Item") or {}
enabled = bool(team.get("enabled", False))
team = {}
if mode == "team":
alerts = settings_table.get_item(Key={"pk": team_id, "sk": TEAM_CHANNEL_SK}).get("Item") or {}
updates = settings_table.get_item(Key={"pk": team_id, "sk": UPDATES_CHANNEL_SK}).get("Item") or {}
# Findings are updates: never in an alert channel that takes alerts only.
team = team_update_channels(team_id, alerts, updates)
channels = pick_channels(
mode,
open_channels(team_id, own) if mode == "custom" else {},
open_channels(team_id, team) if mode == "team" and enabled else {},
enabled,
team,
bool(team),
)
if channels:
deliver(channels, account_id, recs)
Expand Down
18 changes: 18 additions & 0 deletions lambda/tests/test_log_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,24 @@ def test_only_microsoft_webhook_hosts(self):
self.assertFalse(h.valid_webhook_url("https://evil.com@prod-1.logic.azure.com/x"))
self.assertFalse(h.valid_webhook_url("http://prod-1.logic.azure.com/x"))

def test_findings_never_reach_an_alerts_only_channel(self):
teams = "https://prod-1.westus.logic.azure.com/workflows/t"
other = "https://prod-2.westus.logic.azure.com/workflows/u"
alerts = {"enabled": True, "teams_webhook_url": teams}
# Saved before kinds existed: alerts only.
self.assertEqual(h.channel_kinds(h.TEAM_CHANNEL_SK, alerts), {"alert"})
self.assertEqual(h.team_update_channels("TEAM#a", alerts, {}), {})
# The updates channel takes them.
updates = {"enabled": True, "teams_webhook_url": other}
self.assertEqual(h.channel_kinds(h.UPDATES_CHANNEL_SK, updates), {"action", "update"})
self.assertEqual(h.team_update_channels("TEAM#a", alerts, updates), {"teams": other})
# An alert channel set to take updates does, after the updates channel.
more = dict(alerts, kinds=["alert", "update"])
self.assertEqual(h.team_update_channels("TEAM#a", more, {}), {"teams": teams})
self.assertEqual(h.team_update_channels("TEAM#a", more, updates), {"teams": other})
# Off takes nothing.
self.assertEqual(h.team_update_channels("TEAM#a", more, dict(updates, enabled=False)), {"teams": teams})

def test_channel_choice(self):
team = "https://prod-1.westus.logic.azure.com/workflows/t"
own = "https://prod-2.westus.logic.azure.com/workflows/o"
Expand Down
32 changes: 15 additions & 17 deletions services/common/src/alert_notify.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Chat notifications for alerts.
//!
//! A team sets one alert channel (Teams, Slack or both) that every alert route
//! notifies; a route can instead use its own channels or notify nowhere. Each
//! notifies; a route can instead use its own channels or notify nowhere. The
//! alert channel takes alerts only unless the team chose more kinds
//! (`crate::notify::route`). Each
//! alert posts a message, and when CoderHelm opens a pull request for an alert
//! a follow-up message links it (or, when the run finds that no code change
//! is needed, says why: [`no_change_message`]). Webhooks can't edit a posted message, so the
Expand Down Expand Up @@ -59,10 +61,7 @@ pub async fn notify_target(
.key("sk", AttributeValue::S(sk.to_string()))
.send()
};
let (route, team) = tokio::join!(
get(format!("ALERTTOPIC#{topic_arn}"), "ROUTE"),
get(team_id.to_string(), TEAM_SETTINGS_SK),
);
let route = get(format!("ALERTTOPIC#{topic_arn}"), "ROUTE").await;
let Some(route) = route.ok().and_then(|r| r.item().cloned()) else {
return Channels::default();
};
Expand All @@ -74,32 +73,31 @@ pub async fn notify_target(
{
return Channels::default();
}
let team = team
.ok()
.and_then(|t| t.item().cloned())
.unwrap_or_default();
let mode = route
.get("notify_mode")
.and_then(|v| v.as_s().ok())
.cloned()
.unwrap_or_default();
let enabled = team
.get("enabled")
.and_then(|v| v.as_bool().ok())
.copied()
.unwrap_or(false);
let (route_channels, team_channels) = match mode.as_str() {
MODE_OFF => return Channels::default(),
MODE_CUSTOM => (
store::open_all(cipher, team_id, &route).await,
Channels::default(),
),
_ if enabled => (
// The team's channel for alerts (`notify::route`).
_ => (
Channels::default(),
store::open_all(cipher, team_id, &team).await,
crate::notify::route::team_channels(
dynamo,
cipher,
table,
team_id,
crate::notify::route::Kind::Alert,
)
.await,
),
_ => return Channels::default(),
};
let enabled = !team_channels.is_empty();
pick(&mode, route_channels, team_channels, enabled)
}

Expand Down
Loading
Loading