diff --git a/.github/dependabot.yml b/.github/dependabot.yml index f9711877..501aaa89 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -29,7 +29,13 @@ updates: directories: - /deploy/terraform/modules/audit - /deploy/terraform/modules/datadog + - /deploy/terraform/modules/discovery + - /deploy/terraform/modules/ec2 + - /deploy/terraform/modules/ecs + - /deploy/terraform/modules/foundation - /deploy/terraform/examples/audit + - /deploy/terraform/examples/complete + - /deploy/terraform/examples/kubernetes schedule: interval: weekly commit-message: diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 79fe04fd..2e44c587 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -21,6 +21,14 @@ on: - "!LICENSE" - "!.github/**" - ".github/workflows/rust.yml" + # Files under the paths above that the build or tests read: generated + # docs the tests compare, the CLI's embedded logo, and the release + # workflow scripts/check-versions.sh checks. + - "docs/cli-reference.md" + - "docs/policy-schema/**" + - "docs/guides/audit-ocsf.md" + - "assets/logo-mark.svg" + - ".github/workflows/release.yml" # A draft pull request runs nothing; marking it ready for review starts the run. pull_request: types: [opened, synchronize, reopened, ready_for_review] diff --git a/crates/limen-proxy/src/sessions.rs b/crates/limen-proxy/src/sessions.rs index 9cbeb29a..daea38d3 100644 --- a/crates/limen-proxy/src/sessions.rs +++ b/crates/limen-proxy/src/sessions.rs @@ -551,7 +551,7 @@ pub(crate) async fn open_web_stream( ..Default::default() }; let span = session_span(); - let (session_id, worker, inbound, outbound, _slot, resolved) = establish( + let (session_id, worker, inbound, outbound, slot, resolved) = establish( proxy, user, user_cert_der, @@ -565,6 +565,9 @@ pub(crate) async fn open_web_stream( let proxy = proxy.clone(); let user = user.clone(); tokio::spawn(async move { + // The worker's load slot lasts as long as the stream (a WebSocket can + // run for hours), not just until the stream is handed back. + let _slot = slot; relay( &proxy, relay_io, diff --git a/crates/limen-worker/src/kube/mod.rs b/crates/limen-worker/src/kube/mod.rs index 5ec3ec87..695625e5 100644 --- a/crates/limen-worker/src/kube/mod.rs +++ b/crates/limen-worker/src/kube/mod.rs @@ -15,6 +15,7 @@ use std::path::PathBuf; use clap::Args; use limen_policy::{AccessRequest, Principal}; use limen_session::grant::VerifiedGrant; +use limen_session::snapshot::Restrictions; use time::OffsetDateTime; use tonic::Status; @@ -106,6 +107,30 @@ pub(crate) struct Admitted { pub request_ids: Vec, } +impl Admitted { + /// Why an open stream (a `watch`, `logs -f`, or an upgraded `exec`, + /// `attach`, or `port-forward`) must stop at `now`: the session reached + /// its deadline, an access grant it depends on ended, or the user was + /// locked, logged out, or the session killed. `restrictions` is from the + /// worker's latest snapshot, if it has one. + pub(super) fn stop_reason( + &self, + restrictions: Option<&Restrictions>, + now: OffsetDateTime, + ) -> Option<&'static str> { + if now >= self.expires { + return Some("max_session_ttl reached"); + } + let restrictions = restrictions?; + if !self.request_ids.is_empty() + && restrictions.grant_ended(&self.request_ids, now.unix_timestamp()) + { + return Some("the access grant ended"); + } + restrictions.blocks(&self.username, &self.login_id, &self.session_id) + } +} + /// The worker's own checks for a Kubernetes grant, on its own policy /// snapshot. A proxy that signs a grant the policy does not allow is refused. pub(crate) fn admit( @@ -202,3 +227,91 @@ pub(crate) fn admit( request_ids: effective.request_ids, }) } + +#[cfg(test)] +mod tests { + use limen_session::snapshot::{ActiveGrant, Restrictions}; + use time::{Duration, OffsetDateTime}; + + use super::Admitted; + + fn admitted(now: OffsetDateTime, request_ids: &[&str]) -> Admitted { + Admitted { + session_id: "s-1".into(), + username: "ada@example.com".into(), + login_id: "l-1".into(), + actor_type: "user", + groups: vec![], + resource: "eks-prod".into(), + proxy_id: "p-1".into(), + expires: now + Duration::hours(1), + ci: None, + request_ids: request_ids.iter().map(|&s| s.to_owned()).collect(), + } + } + + fn grant(id: &str, expires: OffsetDateTime) -> ActiveGrant { + ActiveGrant { + request_id: id.into(), + username: "ada@example.com".into(), + role: "eks-writer".into(), + resource: "eks-prod".into(), + expires_at_unix: expires.unix_timestamp(), + } + } + + #[test] + fn a_stream_runs_while_nothing_has_ended() { + let now = OffsetDateTime::now_utc(); + let held = Restrictions { + grants: vec![grant("r-1", now + Duration::minutes(30))], + ..Restrictions::default() + }; + assert_eq!(admitted(now, &["r-1"]).stop_reason(Some(&held), now), None); + assert_eq!(admitted(now, &[]).stop_reason(None, now), None); + } + + #[test] + fn a_stream_stops_at_its_deadline_even_without_a_snapshot() { + let now = OffsetDateTime::now_utc(); + let a = admitted(now, &[]); + assert_eq!( + a.stop_reason(None, a.expires), + Some("max_session_ttl reached") + ); + } + + /// Upgraded streams (`exec`, `attach`, `port-forward`) use this check as + /// well as streaming responses, so a revoked or expired grant ends them. + #[test] + fn a_stream_stops_when_its_grant_is_revoked_or_expires() { + let now = OffsetDateTime::now_utc(); + let a = admitted(now, &["r-1"]); + let revoked = Restrictions::default(); + assert_eq!( + a.stop_reason(Some(&revoked), now), + Some("the access grant ended") + ); + let expired = Restrictions { + grants: vec![grant("r-1", now)], + ..Restrictions::default() + }; + assert_eq!( + a.stop_reason(Some(&expired), now), + Some("the access grant ended") + ); + } + + #[test] + fn a_stream_stops_when_the_user_is_locked() { + let now = OffsetDateTime::now_utc(); + let locked = Restrictions { + locked_users: ["Ada@example.com".to_owned()].into(), + ..Restrictions::default() + }; + assert_eq!( + admitted(now, &[]).stop_reason(Some(&locked), now), + Some("user locked") + ); + } +} diff --git a/crates/limen-worker/src/kube/pump.rs b/crates/limen-worker/src/kube/pump.rs index 08913d70..40cfa203 100644 --- a/crates/limen-worker/src/kube/pump.rs +++ b/crates/limen-worker/src/kube/pump.rs @@ -98,7 +98,6 @@ pub(super) async fn run( info: StreamInfo, client: C, upstream: U, - expires: OffsetDateTime, recording: Option, started: Instant, ) where @@ -163,16 +162,11 @@ pub(super) async fn run( let mut tick = tokio::time::interval(Duration::from_secs(1)); loop { tick.tick().await; - if OffsetDateTime::now_utc() >= expires { - return "max_session_ttl reached"; - } - if let Some(snapshot) = worker.policy.latest() - && let Some(why) = snapshot.restrictions.blocks( - &admitted.username, - &admitted.login_id, - &admitted.session_id, - ) - { + let snapshot = worker.policy.latest(); + if let Some(why) = admitted.stop_reason( + snapshot.as_ref().map(|s| &*s.restrictions), + OffsetDateTime::now_utc(), + ) { return why; } } diff --git a/crates/limen-worker/src/kube/serve.rs b/crates/limen-worker/src/kube/serve.rs index c912681d..e88c05ad 100644 --- a/crates/limen-worker/src/kube/serve.rs +++ b/crates/limen-worker/src/kube/serve.rs @@ -455,7 +455,6 @@ async fn start_stream( let upstream_upgrade = hyper::upgrade::on(&mut *response); let worker = ctx.worker.clone(); let admitted = Arc::clone(admitted); - let expires = admitted.expires; tokio::spawn(async move { let (Ok(client), Ok(upstream)) = tokio::join!(client_upgrade, upstream_upgrade) else { // The stream never ran: close what session.start opened. @@ -476,7 +475,6 @@ async fn start_stream( stream_info, TokioIo::new(client), TokioIo::new(upstream), - expires, recording, started, ) @@ -497,23 +495,11 @@ async fn start_stream( /// Why a response that is still streaming (a `watch`, `logs -f`) must stop: /// the user was locked, the session was killed, or the grant ran out. fn must_stop(ctx: &Ctx) -> Option<&'static str> { - let admitted = &ctx.admitted; - if time::OffsetDateTime::now_utc() >= admitted.expires { - return Some("max_session_ttl reached"); - } - ctx.worker.policy.latest().and_then(|snapshot| { - if !admitted.request_ids.is_empty() - && snapshot.restrictions.grant_ended( - &admitted.request_ids, - time::OffsetDateTime::now_utc().unix_timestamp(), - ) - { - return Some("the access grant ended"); - } - snapshot - .restrictions - .blocks(&admitted.username, &admitted.login_id, &admitted.session_id) - }) + let snapshot = ctx.worker.policy.latest(); + ctx.admitted.stop_reason( + snapshot.as_ref().map(|s| &*s.restrictions), + time::OffsetDateTime::now_utc(), + ) } /// Relay the API server's response body, and cut it off if the user is locked diff --git a/deploy/compose/setup.sh b/deploy/compose/setup.sh index b8656c40..053c8893 100755 --- a/deploy/compose/setup.sh +++ b/deploy/compose/setup.sh @@ -131,7 +131,7 @@ check_prerequisites() { # recreated so they enroll again; the rest of the running stack is kept. start_stack() { stack_setup ${scale_flag[@]+"${scale_flag[@]}"} ${stacks[@]+"${stacks[@]}"} - stack_policy_files "$root" "${stacks[@]}" || fail "could not select policy examples" + stack_policy_files "$root" ${stacks[@]+"${stacks[@]}"} || fail "could not select policy examples" stack_lock locked=true state="$STACK_STATE"