diff --git a/crates/limen-control-plane/src/access/mod.rs b/crates/limen-control-plane/src/access/mod.rs index e8f1a476..e9a36291 100644 --- a/crates/limen-control-plane/src/access/mod.rs +++ b/crates/limen-control-plane/src/access/mod.rs @@ -667,7 +667,18 @@ pub async fn list_requests( .map_err(|e| internal("reading groups", e))? .unwrap_or_default(); let policy = state.policy.current(); - let pending = store::pending(&state.db).await; + let roles: Vec = policy + .policy + .requestable_roles() + .map(|r| r.metadata.name.clone()) + .filter(|role| { + policy + .policy + .may_approve(role, who.username, groups.iter().map(String::as_str)) + }) + .collect(); + let pending = store::pending_for_roles(&state.db, &roles, who.username, limit).await; + // Checked again per row, so the SQL filter is never the only guard. pending.map(|rows| { rows.into_iter() .filter(|r| { diff --git a/crates/limen-control-plane/src/access/store.rs b/crates/limen-control-plane/src/access/store.rs index 7a8e2c08..724f4105 100644 --- a/crates/limen-control-plane/src/access/store.rs +++ b/crates/limen-control-plane/src/access/store.rs @@ -405,13 +405,26 @@ pub async fn list_requests( .collect() } -/// Pending requests that have not lapsed. -pub async fn pending(db: &Db) -> Result, sqlx::Error> { +/// Pending requests that have not lapsed, for `roles` only, oldest first, +/// leaving out `approver`'s own. Filtered here rather than after a capped +/// read, so a busy organisation's oldest requests can't hide the rest. +pub async fn pending_for_roles( + db: &Db, + roles: &[String], + approver: &str, + limit: i64, +) -> Result, sqlx::Error> { sqlx::query(concat!( "SELECT ", request_cols!(), - " FROM access_requests WHERE status = 'pending' AND expires_at > now() ORDER BY created_at LIMIT 500" + " FROM access_requests + WHERE status = 'pending' AND expires_at > now() + AND role = ANY($1) AND lower(requester) <> lower($2) + ORDER BY created_at LIMIT $3" )) + .bind(roles) + .bind(approver) + .bind(limit) .fetch_all(db) .await? .iter() diff --git a/crates/limen-control-plane/tests/access_requests.rs b/crates/limen-control-plane/tests/access_requests.rs index d537454d..e36a7226 100644 --- a/crates/limen-control-plane/tests/access_requests.rs +++ b/crates/limen-control-plane/tests/access_requests.rs @@ -434,6 +434,61 @@ async fn a_role_that_needs_two_approvers_gets_two_different_ones() { ); } +/// The approver's queue honours `limit`, oldest first, and leaves out the +/// approver's own requests (filtered in the query, not after a capped read). +#[tokio::test] +async fn the_approval_queue_honours_its_limit_and_skips_your_own() { + let Some(r) = Rig::start().await else { return }; + let (_, both) = + r.h.login(CONTRACTOR, &["developers", "sre-leads"]) + .await + .expect("a lead who also asks"); + let first = r + .request(&r.dev, "web-prod", "INC-1 disk") + .await + .expect("ssh"); + let second = r.request(&r.dev, "orders-prod", "").await.expect("db"); + let own = r + .request(&both, "web-prod", "INC-2 own") + .await + .expect("own"); + let queue = |who: CertificateDer<'static>, limit: u32| { + let access = r.access(); + async move { + let mut access = access; + access + .list_access_requests(forwarded( + ListAccessRequestsRequest { + scope: "approvable".into(), + status: String::new(), + limit, + }, + Some(&who), + )) + .await + .expect("queue") + .into_inner() + .requests + .into_iter() + .map(|q| q.id) + .collect::>() + } + }; + assert_eq!( + queue(r.lead.clone(), 1).await, + std::slice::from_ref(&first.id) + ); + assert_eq!( + queue(r.lead.clone(), 0).await, + [first.id.clone(), second.id.clone(), own.id.clone()] + ); + assert_eq!( + queue(both, 0).await, + [first.id, second.id], + "never your own" + ); +} + #[tokio::test] async fn a_denial_ends_it_and_a_requester_can_withdraw() { let Some(r) = Rig::start().await else { return }; diff --git a/crates/limen-session/src/envelope.rs b/crates/limen-session/src/envelope.rs index ff817f3f..bbf2f4a6 100644 --- a/crates/limen-session/src/envelope.rs +++ b/crates/limen-session/src/envelope.rs @@ -238,14 +238,37 @@ mod tests { use limen_ca::{CaPurpose, CertificateAuthority, ComponentCertRequest, FileSigner}; - const ALL: [Purpose; 5] = [ + const ALL: [Purpose; 6] = [ Purpose::SessionGrant, Purpose::PolicySnapshot, Purpose::AuditManifest, Purpose::WebAppSession, Purpose::EvidencePack, + Purpose::ControlsReport, ]; + /// `ALL` must list every purpose, or the tests below silently skip the + /// one left out. This match has no wildcard, so a new variant stops it + /// compiling until it is added here and to `ALL`. + #[test] + fn all_lists_every_purpose_once() { + fn index(p: Purpose) -> usize { + match p { + Purpose::SessionGrant => 0, + Purpose::PolicySnapshot => 1, + Purpose::AuditManifest => 2, + Purpose::WebAppSession => 3, + Purpose::EvidencePack => 4, + Purpose::ControlsReport => 5, + } + } + let indices: Vec = ALL.into_iter().map(index).collect(); + assert_eq!(indices, (0..ALL.len()).collect::>()); + let labels: std::collections::BTreeSet<&[u8]> = + ALL.into_iter().map(Purpose::label).collect(); + assert_eq!(labels.len(), ALL.len(), "two purposes share a label"); + } + async fn internal_ca() -> CertificateAuthority { CertificateAuthority::create_self_signed( CaPurpose::Internal, @@ -283,7 +306,7 @@ mod tests { } } - /// The control plane signs four purposes with one key. Only the label in + /// The control plane signs five purposes with one key. Only the label in /// the signed message keeps, say, a policy snapshot from being replayed as /// a browser's web-app session, so the kind check can't be what stops it. #[tokio::test] diff --git a/deploy/kubernetes/limen/control-plane.yaml b/deploy/kubernetes/limen/control-plane.yaml index 3940ded3..344c11e2 100644 --- a/deploy/kubernetes/limen/control-plane.yaml +++ b/deploy/kubernetes/limen/control-plane.yaml @@ -41,6 +41,16 @@ spec: topologyKey: topology.kubernetes.io/zone labelSelector: matchLabels: { app.kubernetes.io/name: limen-control-plane } + # The image runs as distroless nonroot (uid 65532). A fresh volume + # (EBS, for one) is owned by root, so without fsGroup the spool can't + # be written and the control plane, whose audit fails closed, won't + # start. Matches the Helm chart's podSecurityContext. + securityContext: + runAsNonRoot: true + runAsUser: 65532 + runAsGroup: 65532 + fsGroup: 65532 + seccompProfile: { type: RuntimeDefault } containers: - name: control-plane image: REPLACE/limen-control-plane:REPLACE diff --git a/docs/guides/policy-tooling.md b/docs/guides/policy-tooling.md index b7fd20b6..f6ac3f57 100644 --- a/docs/guides/policy-tooling.md +++ b/docs/guides/policy-tooling.md @@ -122,4 +122,4 @@ Every call writes a `policy.suggest` audit event (window, policy version, counts ## In CI -`examples/ci/policy-check.yml` is a GitHub Actions workflow that runs the tests, posts the effective-access diff as a pull request comment (`scripts/policy-pr-comment.sh`), and fails when standing access widens without a label. Copy it into your policy repository. +`examples/ci/policy-check.yml` is a GitHub Actions workflow that runs the tests, posts the effective-access diff as a pull request comment (`scripts/policy-pr-comment.sh`), and fails when standing access widens without a label. Copy both files into your policy repository: the workflow into `.github/workflows/`, the script into `scripts/`. diff --git a/examples/ci/policy-check.yml b/examples/ci/policy-check.yml index e5984ea9..8494857e 100644 --- a/examples/ci/policy-check.yml +++ b/examples/ci/policy-check.yml @@ -1,7 +1,8 @@ # Check a policy change in a pull request (ADR-0028). # # Copy this into the repository that holds your Limen policy as -# .github/workflows/policy-check.yml and adjust `policy/` and the install step. +# .github/workflows/policy-check.yml, copy Limen's scripts/policy-pr-comment.sh +# into that repository's scripts/, and adjust `policy/` and the install step. # # What it does: # 1. runs the `policy_test` cases in the policy: a failing case fails the check; @@ -42,6 +43,9 @@ jobs: - name: Comment on the pull request env: GH_TOKEN: ${{ github.token }} + # The workspace root is not a git checkout (both are in + # subdirectories), so `gh` needs the repository named. + GH_REPO: ${{ github.repository }} PR: ${{ github.event.pull_request.number }} run: head/scripts/policy-pr-comment.sh diff.md - name: Standing access widens