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

**What happened:** On a self-hosted install that runs in the same AWS account as the team's resources, an AWS WAF alarm run called its WAF tools (`waf_sampled_requests`, `waf_log_objects`). AWS logged those calls (`wafv2:ListWebACLs` and the rest) as the worker's own Lambda service role, and refused them. `live::aws::assume` swapped the account's `CoderHelmLogReader` for the install's own role whenever the reader was in the install's account, and the install's own role never gets the WAF reads. The refusal was reported as "CoderHelm's AWS role doesn't allow wafv2:ListWebACLs yet" and recorded as the team role's setup item, so the run waited for it. The wait's re-check took on the reader role itself (`log_findings::session_for`), was allowed, and started the run again. The new run read with the worker's role again, was refused again and waited again: a loop.

**The guarantee:**
- Every read of a team's AWS data is made as the team's role. `live::aws::assume` always takes on the connected role with the team's private ID, the install's own account included. It returns a `TeamSession`, which only `assume` and `assume_exact` can make. The WAF tools, alarm evidence, WAF log objects, CloudWatch metrics, log reads and the permission check accept only a `TeamSession`, so the install's own credentials can't reach them.
- `log_findings::session_for`, which the re-check of a waiting run uses, is the account's connection taken on with `assume`. That is the same target and the same call the run's tools use, so the re-check and the run can't disagree.
- A refusal names the principal AWS refused when it isn't the team's role. It counts as CoderHelm's own failure (an error log), never as the team's setup item.
- A run that a re-check started again never waits again for a read the re-check had read (`role_wait::ending_after`). It records CoderHelm's own failure (an error log and one line on the run) and goes on with what it has.

**Test:** `services/worker/src/passes/incidents/rk_team_reads_as_the_team_role.rs`
2 changes: 1 addition & 1 deletion docs/self-host/integrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Teams connect their own AWS accounts so CoderHelm can read CloudWatch logs, alar
- **Making the role by hand**\
The AWS page lists every action the role needs, grouped by what CoderHelm reads with it, and your install's account id. Create the role named `CoderHelmLogReader`, let your install's account assume it only with the team's External ID, and allow those actions.
- **Reading the install's own account**\
Connect it the same way: the `CoderHelmLogReader` role there, with the team's External ID, proves the team may read it. After that, the install reads its own account with its gateway's and worker's own roles, which the install's stack gives this release's read-only permissions (`infra/lib/own-account-read.ts`, the same list as the update template). Each version update brings any new permission, so the install's own account never shows **Update in AWS**; if a permission is still missing, the page says it comes with the next version update.
Connect it the same way: the `CoderHelmLogReader` role there, with the team's External ID. Every read of the account (logs, metrics, WAF, settings and the rest) is made as that role, exactly as in any other account, so AWS records every read under the team's role and the permission check looks at the same role. When a release adds a read, the account shows **Update in AWS** like any other. Knowledge bases in the install's account are the one exception: the install's own role searches them (see [bedrock.md](bedrock.md)).
- **When a release needs new permissions**\
Each AWS connection shows **Permissions up to date** or **Update needed: N new read-only permissions**. The check runs live: it asks IAM's policy simulator about the role (`iam:SimulatePrincipalPolicy` on that one role), or, for roles made before that, tries the cheapest read of each kind. **Update in AWS** opens a stack (`CoderHelmLogReaderUpdate-<id>`) that adds exactly this release's permissions to the existing role. Sign in to the role's account, review it and press **Create stack**. When you come back, the page checks again by itself. If something went wrong, it names the reason and the fix: the stack failed, it isn't in the role's account, a permissions boundary or an organization policy (SCP) blocks the permissions. Once a day the install checks every account, and each team's Slack or Teams channel hears once per release that needs an update. Until then, features that need a missing permission say so where they are used, skip that step and keep working with the rest.
- **One template for every read**\
Expand Down
87 changes: 67 additions & 20 deletions services/common/src/alarm_evidence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use std::time::Duration;

use chrono::{DateTime, Utc};

use crate::live::aws::Session;
use crate::live::aws::{Session, TeamSession};

/// The heading the evidence goes under in the ticket (and how a later step
/// of the same run knows it is there).
Expand Down Expand Up @@ -582,6 +582,8 @@ pub struct Gathered {
/// The evidence lines and the refusals met while reading them.
#[derive(Default)]
struct Notes {
/// The team's role the reads are made with (`TeamSession::role_arn`).
role: String,
lines: Vec<String>,
refused: Vec<String>,
results: String,
Expand All @@ -590,22 +592,21 @@ struct Notes {
}

impl Notes {
fn on(session: &TeamSession) -> Notes {
Notes {
role: session.role_arn().to_string(),
..Default::default()
}
}

/// A lookup that failed: a refusal names the action and counts toward
/// the install's setup item; anything else says AWS's own words.
fn failed(&mut self, what: &str, action: &str, code: Option<&str>, msg: Option<&str>) {
if refused(code) {
if !self.refused.iter().any(|a| a == action) {
self.refused.push(action.to_string());
}
self.lines.push(format!(
"Couldn't {what}: CoderHelm's AWS role doesn't allow {action} yet."
));
} else {
self.lines.push(format!(
"Couldn't {what}: {}",
clip(msg.or(code).unwrap_or("no answer"))
));
let (line, team_gap) = failure_line(what, action, code, msg, &self.role);
if team_gap && !self.refused.iter().any(|a| a == action) {
self.refused.push(action.to_string());
}
self.lines.push(line);
}

fn done(self, alarm: &WafAlarm) -> Gathered {
Expand All @@ -618,6 +619,48 @@ impl Notes {
}
}

/// Pure: the line for a lookup that failed, and whether it is the team's
/// setup item (the team's role `role_arn` was refused `action`). AWS
/// refusing any other principal is CoderHelm's own failure: the line names
/// the principal, and it is never counted against the team's role.
pub fn failure_line(
what: &str,
action: &str,
code: Option<&str>,
msg: Option<&str>,
role_arn: &str,
) -> (String, bool) {
if !refused(code) {
return (
format!(
"Couldn't {what}: {}",
clip(msg.or(code).unwrap_or("no answer"))
),
false,
);
}
match crate::live::aws::refused_other_principal(msg.unwrap_or(""), role_arn) {
Some(principal) => {
tracing::error!(
action,
principal = %principal,
"CoderHelm's own failure: a team read was made with a role that isn't the team's"
);
(
format!(
"Couldn't {what}: AWS refused {action} to {principal}, which isn't the \
team's role, so this is CoderHelm's own failure, not the team's role."
),
false,
)
}
None => (
format!("Couldn't {what}: CoderHelm's AWS role doesn't allow {action} yet."),
true,
),
}
}

/// Pure: the ticket's section from what was read, with the one line that
/// names the install's setup item when the role was refused a read.
pub fn evidence(alarm: &WafAlarm, mut lines: Vec<String>, refused: Vec<String>) -> Gathered {
Expand Down Expand Up @@ -818,7 +861,7 @@ async fn log_destinations(waf: &aws_sdk_wafv2::Client, arn: &str, n: &mut Notes)
/// The rule's matches in the web ACL's log objects, read straight from its
/// bucket (no Athena); `true` when records naming the rule came back.
async fn read_log_objects(
session: &Session,
session: &TeamSession,
arn: &str,
destination: &str,
alarm: &WafAlarm,
Expand All @@ -842,10 +885,14 @@ async fn read_log_objects(
/// The sampled requests the alarm's rule matched in the last three hours
/// (for an agent's lookup): the lines, and the IAM actions the role was
/// refused (the install's setup item).
pub async fn sampled_requests(session: &Session, alarm: &WafAlarm, now: DateTime<Utc>) -> Gathered {
pub async fn sampled_requests(
session: &TeamSession,
alarm: &WafAlarm,
now: DateTime<Utc>,
) -> Gathered {
let (_, region) = alarm.scope_and_region();
let waf = waf_client(&session_in(session, &region));
let mut n = Notes::default();
let mut n = Notes::on(session);
if let Some(arn) = find_web_acl(&waf, alarm, &mut n).await {
let end = now.timestamp();
read_samples(&waf, &arn, alarm, end - WINDOW_SECS + 60, end, &mut n).await;
Expand All @@ -857,15 +904,15 @@ pub async fn sampled_requests(session: &Session, alarm: &WafAlarm, now: DateTime
/// agent's lookup), read straight from its `aws-waf-logs-*` bucket: the
/// bucket named, else where the web ACL logs.
pub async fn log_objects(
session: &Session,
session: &TeamSession,
alarm: &WafAlarm,
bucket: Option<&str>,
start: i64,
end: i64,
) -> Gathered {
let (_, region) = alarm.scope_and_region();
let waf = waf_client(&session_in(session, &region));
let mut n = Notes::default();
let mut n = Notes::on(session);
let Some(arn) = find_web_acl(&waf, alarm, &mut n).await else {
return n.done(alarm);
};
Expand All @@ -891,10 +938,10 @@ pub async fn log_objects(
/// the result, so a missing permission is named, not silent. The logs held
/// in S3 are read straight from the bucket first; Athena only when that
/// found nothing.
pub async fn gather(session: &Session, alarm: &WafAlarm, now: DateTime<Utc>) -> Gathered {
pub async fn gather(session: &TeamSession, alarm: &WafAlarm, now: DateTime<Utc>) -> Gathered {
let (_, region) = alarm.scope_and_region();
let waf = waf_client(&session_in(session, &region));
let mut n = Notes::default();
let mut n = Notes::on(session);
let Some(arn) = find_web_acl(&waf, alarm, &mut n).await else {
return n.done(alarm);
};
Expand Down
1 change: 1 addition & 0 deletions services/common/src/knowledge/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ pub async fn session(
};
aws::assume(sts, &target, actor)
.await
.map(aws::TeamSession::into_session)
.map_err(|e| Refusal::new("access", e.message, e.fix))
}
}
Expand Down
1 change: 1 addition & 0 deletions services/common/src/knowledge/sources/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ impl Env {
// every bucket the role may read.
crate::live::aws::assume_exact(&a.sts, &target, &a.actor)
.await
.map(crate::live::aws::TeamSession::into_session)
.map_err(|e| ConnectorError::Reconnect(format!("{} {}", e.message, e.fix)))
}
}
Expand Down
Loading
Loading