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
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
5 changes: 4 additions & 1 deletion crates/limen-proxy/src/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
113 changes: 113 additions & 0 deletions crates/limen-worker/src/kube/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -106,6 +107,30 @@ pub(crate) struct Admitted {
pub request_ids: Vec<String>,
}

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(
Expand Down Expand Up @@ -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")
);
}
}
16 changes: 5 additions & 11 deletions crates/limen-worker/src/kube/pump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ pub(super) async fn run<C, U>(
info: StreamInfo,
client: C,
upstream: U,
expires: OffsetDateTime,
recording: Option<Recorder>,
started: Instant,
) where
Expand Down Expand Up @@ -163,16 +162,11 @@ pub(super) async fn run<C, U>(
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;
}
}
Expand Down
24 changes: 5 additions & 19 deletions crates/limen-worker/src/kube/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -476,7 +475,6 @@ async fn start_stream(
stream_info,
TokioIo::new(client),
TokioIo::new(upstream),
expires,
recording,
started,
)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion deploy/compose/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down