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
10 changes: 9 additions & 1 deletion docs/self-host/sign-in.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ With it on:
- **People who sign in with a password** use Cognito's own two-factor codes: Cognito asks for the code at every sign-in.
- **People who sign in with Google or GitHub** don't pass through Cognito's two-factor step, so CoderHelm asks for its own authenticator-app code right after Google or GitHub. Their secret is sealed with the install's KMS key. No sign-in path skips the code.
- **Sessions from before the switch** are checked at their next request: someone already signed in without a code is sent to the setup screen, or asked to sign in again with their code.
- **API keys** are not people and keep working. **Settings, Sign-in** lists them, so you can revoke any you don't want.
- **API keys** are not people and keep working: a key never signs in, so the rule doesn't hold it back. **Settings, Sign-in** and **Settings, API keys** mark each live key made before the rule was turned on, with whether the person who made it has a second factor now (or has left the team). Such a key may have been made from a session without a second factor: check them and revoke any you don't recognize in **Settings, API keys**. Keys made after the rule is on come from sessions that passed it.

### One second factor per person

Email and password, Google and GitHub all lead to the same person in CoderHelm (by email), so each person has **one** second factor, and it is asked for however they sign in, whether the rule is on or not:

- **An authenticator app set up in CoderHelm** (after Google or GitHub): its code is asked for after Google, GitHub and a password alike.
- **Cognito's code on the password account**: only Cognito can check it, so it is asked for at the password sign-in. Signing in with Google or GitHub sends the person back to sign in with their email and password.
- **Neither kind can be set up while the person has one**, from any session, including one held at the setup screen. To change it, turn the current one off first: the app code takes a current code to turn off, and the password account's code can be turned off only from a session that passed it. If someone lost their phone, an admin resets it (below).

**Settings, Sign-in** shows who has set it up. If someone loses their phone, an admin can reset their two-factor sign-in after checking it's really them (a video call, say). The admin writes how they checked; the reset and the reason go in the log under **Settings, Roles**. The person sets it up again at their next sign-in. Only an owner can reset an admin's or another owner's. Nobody can reset their own.

Expand Down
18 changes: 14 additions & 4 deletions infra/cfn/coderhelm-log-reader-update.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Description: >
Brings the existing CoderHelmLogReader role up to date with the read-only
permissions the current CoderHelm release uses. Every permission here is
read-only: CoderHelm can list, describe and query, never change anything.
It only adds: to drop a permission an older release granted, update the
CoderHelmLogReader stack itself with the current role template.
Created from the dashboard's Update link; you can delete older
CoderHelmLogReaderUpdate stacks once this one is in place.

Expand Down Expand Up @@ -42,6 +44,11 @@ Resources:
Action:
- "iam:SimulatePrincipalPolicy"
Resource: !Sub "arn:aws:iam::${AWS::AccountId}:role/${RoleName}"
- Sid: CheckOwnUpdateStack
Effect: Allow
Action:
- "cloudformation:DescribeStacks"
Resource: !Sub "arn:aws:cloudformation:*:${AWS::AccountId}:stack/CoderHelmLogReaderUpdate-*/*"
- Sid: ReadMetrics
Effect: Allow
Action:
Expand All @@ -59,6 +66,10 @@ Resources:
Action:
- "airflow:InvokeRestApi"
Resource: "arn:aws:airflow:*:*:role/*/Viewer"
# A task definition as ECS returns it includes its containers'
# environment variables; CoderHelm keeps only the log
# configuration, start command, image, labels, storage, cpu and
# memory from it, and never stores or shows the environment.
- Sid: DescribeServicesAndTasks
Effect: Allow
Action:
Expand All @@ -69,15 +80,14 @@ Resources:
- "ecs:ListTasks"
- "ecs:DescribeTasks"
Resource: "*"
# Tags, a stack's resource list and image manifests only: no
# function configuration or code, no stack parameters.
- Sid: FindTheCodeBehindALogGroup
Effect: Allow
Action:
- "lambda:GetFunction"
- "lambda:GetFunctionConfiguration"
- "lambda:ListFunctions"
- "lambda:ListTags"
- "logs:ListTagsForResource"
- "cloudformation:DescribeStacks"
- "cloudformation:ListStackResources"
- "ecr:BatchGetImage"
- "ecr:ListTagsForResource"
Resource: "*"
25 changes: 17 additions & 8 deletions infra/cfn/coderhelm-log-reader.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,20 @@ Resources:
Action:
- "iam:SimulatePrincipalPolicy"
Resource: !Sub "arn:aws:iam::${AWS::AccountId}:role/CoderHelmLogReader"
# Whether the dashboard's Update link was used: the status of
# CoderHelm's own update stacks only, no other stack.
- Sid: CheckOwnUpdateStack
Effect: Allow
Action:
- "cloudformation:DescribeStacks"
Resource: !Sub "arn:aws:cloudformation:*:${AWS::AccountId}:stack/CoderHelmLogReaderUpdate-*/*"
# Before a fix for a log error is written: which tasks of the
# service behind a log group are running, since when, and how
# they start. Lists and describes only.
# they start. Lists and describes only. A task definition as
# ECS returns it includes its containers' environment
# variables; CoderHelm keeps only the log configuration, start
# command, image, labels, storage, cpu and memory from it, and
# never stores or shows the environment.
- Sid: DescribeTasksBehindLogErrors
Effect: Allow
Action:
Expand Down Expand Up @@ -127,21 +138,19 @@ Resources:
Version: "2012-10-17"
Statement:
# Which service writes a log group, so a fix goes to the repo its
# code lives in: describes and tags only. No image layers, no
# function code, nothing is changed.
# code lives in: tags, a stack's resource list (names and types)
# and image manifests only. No function configuration (it holds
# environment variables), no function code, no stack
# parameters, no image layers; nothing is changed.
- Sid: DescribeServicesBehindLogGroups
Effect: Allow
Action:
- "ecs:ListClusters"
- "ecs:ListServices"
- "ecs:DescribeServices"
- "ecs:DescribeTaskDefinition"
- "lambda:GetFunction"
- "lambda:GetFunctionConfiguration"
- "lambda:ListFunctions"
- "lambda:ListTags"
- "logs:ListTagsForResource"
- "cloudformation:DescribeStacks"
- "cloudformation:ListStackResources"
- "ecr:BatchGetImage"
- "ecr:ListTagsForResource"
Resource: "*"
Expand Down
24 changes: 20 additions & 4 deletions services/common/src/live/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ pub fn simulated_resource(action: &str, role_arn: &str, region: &str, account: &
format!("arn:aws:airflow:{region}:{account}:role/coderhelm-permission-check/Viewer")
}
"iam:SimulatePrincipalPolicy" => role_arn.to_string(),
// Allowed on CoderHelm's own update stacks only.
"cloudformation:DescribeStacks" => format!(
"arn:aws:cloudformation:{region}:{account}:stack/{}/coderhelm-permission-check",
log_reader::update_stack_name()
),
_ => "*".to_string(),
}
}
Expand Down Expand Up @@ -423,9 +428,10 @@ async fn probes(s: &Session) -> Vec<(&'static str, Option<bool>)> {
.await
.map(|_| ())
.map_err(|e| refused(&e));
let code = client!(aws_sdk_lambda, s, s.region.clone())
.list_functions()
.max_items(1)
// The role's own stack: a refusal means the ability is missing.
let code = client!(aws_sdk_cloudformation, s, s.region.clone())
.list_stack_resources()
.stack_name(log_reader::ROLE_STACK)
.send()
.await
.map(|_| ())
Expand Down Expand Up @@ -607,7 +613,7 @@ mod tests {
.missing
.contains(&"iam:SimulatePrincipalPolicy".to_string()));
assert!(c.missing.contains(&"ecs:DescribeTasks".to_string()));
assert!(c.missing.contains(&"lambda:GetFunction".to_string()));
assert!(c.missing.contains(&"lambda:ListTags".to_string()));
assert!(!c.missing.iter().any(|m| m.starts_with("airflow:")));
assert_eq!(c.missing_abilities(), vec!["check", "ecs", "code"]);
}
Expand Down Expand Up @@ -704,5 +710,15 @@ mod tests {
simulated_resource("ecs:ListTasks", &t.role_arn, "r", "a"),
"*"
);
// The stack read is allowed on CoderHelm's update stacks only.
assert!(simulated_resource(
"cloudformation:DescribeStacks",
&t.role_arn,
"eu-west-1",
"111122223333"
)
.starts_with(
"arn:aws:cloudformation:eu-west-1:111122223333:stack/CoderHelmLogReaderUpdate-"
));
}
}
44 changes: 34 additions & 10 deletions services/common/src/log_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ pub const ABILITIES: &[Ability] = &[
Ability {
key: "check",
name: "Checking its own permissions",
why: "Asks IAM which of these permissions the role has, so this page can say when an update is needed. It only looks at this one role.",
actions: &["iam:SimulatePrincipalPolicy"],
why: "Asks IAM which of these permissions the role has, and whether CoderHelm's own update stack finished, so this page can say when an update is needed. It only looks at this one role and CoderHelm's update stacks.",
actions: &["iam:SimulatePrincipalPolicy", "cloudformation:DescribeStacks"],
},
Ability {
key: "metrics",
Expand All @@ -85,7 +85,7 @@ pub const ABILITIES: &[Ability] = &[
Ability {
key: "ecs",
name: "ECS services and tasks",
why: "Sees which tasks behind a failing log group are running, since when and how they start, as evidence before a fix.",
why: "Sees which tasks behind a failing log group are running, since when and how they start, as evidence before a fix. A task definition comes with its environment variables; CoderHelm keeps only the start command, image, log setting, storage, cpu and memory.",
actions: &[
"ecs:ListClusters",
"ecs:ListServices",
Expand All @@ -98,14 +98,11 @@ pub const ABILITIES: &[Ability] = &[
Ability {
key: "code",
name: "Finding the code behind a log group",
why: "Reads function, image and stack tags to work out which repository a log group's code lives in, so a fix goes to the right repo.",
why: "Reads function, image and log group tags, the names in a stack's resource list and image manifests to work out which repository a log group's code lives in, so a fix goes to the right repo. Never a function's configuration or code, or a stack's parameters.",
actions: &[
"lambda:GetFunction",
"lambda:GetFunctionConfiguration",
"lambda:ListFunctions",
"lambda:ListTags",
"logs:ListTagsForResource",
"cloudformation:DescribeStacks",
"cloudformation:ListStackResources",
"ecr:BatchGetImage",
"ecr:ListTagsForResource",
],
Expand Down Expand Up @@ -362,21 +359,48 @@ mod tests {
"ecs:ListTasks",
"ecs:DescribeTasks",
"ecs:DescribeServices",
"lambda:GetFunction",
"lambda:ListTags",
] {
assert!(all.iter().any(|x| x == a), "{a}");
}
assert!(!all.iter().any(|x| x == "sts:AssumeRole"));
}

/// Nothing that hands over a function's configuration (its environment
/// variables) or code, or a stack's parameters; the one stack read is
/// CoderHelm's own update stack.
#[test]
fn no_function_configuration_code_or_stack_parameters() {
for t in [ROLE_TEMPLATE, UPDATE_TEMPLATE] {
let all = template_actions(t);
for a in [
"lambda:GetFunction",
"lambda:GetFunctionConfiguration",
"lambda:ListFunctions",
"lambda:ListVersionsByFunction",
"cloudformation:GetTemplate",
] {
assert!(!all.iter().any(|x| x == a), "{a}");
}
let at = t.find("\"cloudformation:DescribeStacks\"").unwrap();
let resource = t[at..].lines().nth(1).unwrap();
assert!(
resource.contains("stack/CoderHelmLogReaderUpdate-*/*"),
"{resource}"
);
assert_eq!(t.matches("cloudformation:DescribeStacks").count(), 1);
}
assert!(update_stack_name().starts_with("CoderHelmLogReaderUpdate-"));
}

#[test]
fn missing_actions_follow_the_abilities() {
assert!(missing_actions(&[]).is_empty());
let m = missing_actions(&["ecs", "metrics"]);
assert_eq!(m[0], "cloudwatch:GetMetricData");
assert_eq!(m.len(), 8);
assert!(m.contains(&"ecs:DescribeTasks"));
assert!(!m.contains(&"lambda:GetFunction"));
assert!(!m.contains(&"lambda:ListTags"));
}

#[test]
Expand Down
14 changes: 8 additions & 6 deletions services/common/src/log_repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
//! definition that write to the log group (found by the task's own log
//! configuration, not by its name), their container images and image
//! labels (`org.opencontainers.image.source`), the ECR image's manifest
//! annotations and repository tags, the Lambda function and its tags, the
//! CloudFormation stack behind them (tags and description), and the log
//! group's own tags. A value that points at one of the team's repos (a
//! annotations and repository tags, the Lambda function's tags (never
//! its configuration, which carries its environment, nor its code), the
//! CloudFormation stack behind them (its name, and the functions in its
//! resource list), and the log group's own tags. A value that points at one of the team's repos (a
//! GitHub URL, `owner/name`, or a repo's exact name) is evidence.
//! 3. The team's code: a code search for the identifiers AWS gave (the log
//! group, service, task family, function, image repository and stack
Expand All @@ -30,8 +31,8 @@
//! for another repo is low.
//!
//! A resolution is cached per log group ([`Cached`]) and used again until
//! the evidence it rested on changes (a new task definition revision, new
//! Lambda code, the team's repos), or a week passes.
//! the evidence it rested on changes (a new task definition revision, the
//! log group's tags, the team's repos), or a week passes.

use std::collections::{BTreeMap, BTreeSet};

Expand Down Expand Up @@ -163,7 +164,8 @@ pub struct LambdaFact {
/// Container image, for image functions.
pub image: String,
pub tags: BTreeMap<String, String>,
/// Changes when new code is deployed.
/// Changes when new code is deployed. No longer read (it comes only
/// with the function's configuration); kept for cached resolutions.
pub code_sha: String,
pub description: String,
}
Expand Down
Loading
Loading