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
13 changes: 12 additions & 1 deletion crates/limen-control-plane/src/access/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = 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| {
Expand Down
19 changes: 16 additions & 3 deletions crates/limen-control-plane/src/access/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,13 +405,26 @@ pub async fn list_requests(
.collect()
}

/// Pending requests that have not lapsed.
pub async fn pending(db: &Db) -> Result<Vec<RequestRow>, 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<Vec<RequestRow>, 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()
Expand Down
55 changes: 55 additions & 0 deletions crates/limen-control-plane/tests/access_requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>()
}
};
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 };
Expand Down
27 changes: 25 additions & 2 deletions crates/limen-session/src/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize> = ALL.into_iter().map(index).collect();
assert_eq!(indices, (0..ALL.len()).collect::<Vec<_>>());
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,
Expand Down Expand Up @@ -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]
Expand Down
10 changes: 10 additions & 0 deletions deploy/kubernetes/limen/control-plane.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/guides/policy-tooling.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/`.
6 changes: 5 additions & 1 deletion examples/ci/policy-check.yml
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand Down
Loading