feat: access console with a permission tree, grouped grants and effective access
Backend: grants accept a list of permissions; PUT /access/grants edits or renews a set atomically; refusals carry a reason; the grant list includes refresh grants per relation and the administrator-only permissions; users keep their last groups and admin flag; the emergency operator cannot hold grants. Frontend: the Access screen shows the grantable permissions as a checkbox tree with dependencies, rows grouped by person or group, orbit and end date, expiry and no-effect states, and what each person can do right now. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -403,6 +403,9 @@ fn recipe(w: &World, row: &Row) -> (String, Option<Value>) {
|
||||
("POST", "/api/v1/access/grants") => {
|
||||
Some(json!({"principal_type": "group", "principal": "matrix-group", "role": "viewer"}))
|
||||
}
|
||||
("PUT", "/api/v1/access/grants") => Some(
|
||||
json!({"principal_type": "group", "principal": "matrix-group", "permissions": ["orbit.read"]}),
|
||||
),
|
||||
("PUT", "/api/v1/access/users/:id") => Some(json!({"active": true})),
|
||||
("PUT", "/api/v1/orbits/:id/environment")
|
||||
| ("PUT", "/api/v1/restore-targets/:id/environment") => {
|
||||
@@ -1722,3 +1725,193 @@ async fn revoking_an_outpost_or_deleting_a_relation_takes_refreshers_off_immedia
|
||||
.unwrap();
|
||||
assert_eq!(b["active"], true);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_set_of_permissions_can_be_granted_edited_and_renewed_all_or_nothing() {
|
||||
let w = world().await;
|
||||
let admin = w.caller("admin", true, &[]);
|
||||
let target = w.caller("target", false, &["qa"]);
|
||||
let uid = target.user_id.clone().unwrap();
|
||||
let grants = "/api/v1/access/grants";
|
||||
let orbit = w.a.orbit;
|
||||
let end = |days: i64| (chrono::Utc::now() + chrono::Duration::days(days)).to_rfc3339();
|
||||
|
||||
// a list of single permissions, in one request; refusals say which rule applied
|
||||
let (status, created) = w
|
||||
.send(&admin, "POST", grants, Some(json!({"principal_type": "user", "principal": uid, "permissions": ["orbit.read", "snapshot.read", "capture.run"], "orbit_id": orbit})))
|
||||
.await;
|
||||
assert_eq!(status, StatusCode::CREATED);
|
||||
assert_eq!(created.as_array().unwrap().len(), 3);
|
||||
let (status, body) = w
|
||||
.send(&admin, "POST", grants, Some(json!({"principal_type": "user", "principal": uid, "permissions": ["orbit.read"], "orbit_id": orbit})))
|
||||
.await;
|
||||
assert_eq!(
|
||||
(status, body["reason"].as_str()),
|
||||
(StatusCode::CONFLICT, Some("already_granted"))
|
||||
);
|
||||
for (what, body, reason) in [
|
||||
(
|
||||
"empty list",
|
||||
json!({"principal_type": "group", "principal": "g", "permissions": []}),
|
||||
"nothing_selected",
|
||||
),
|
||||
(
|
||||
"admin-only permission",
|
||||
json!({"principal_type": "group", "principal": "g", "permissions": ["orbit.manage"]}),
|
||||
"unknown_permission",
|
||||
),
|
||||
(
|
||||
"refresh is per relation",
|
||||
json!({"principal_type": "group", "principal": "g", "permissions": ["refresh.run"]}),
|
||||
"unknown_permission",
|
||||
),
|
||||
(
|
||||
"two ways at once",
|
||||
json!({"principal_type": "group", "principal": "g", "role": "viewer", "permissions": ["orbit.read"]}),
|
||||
"choose_one",
|
||||
),
|
||||
(
|
||||
"replaces belongs to PUT",
|
||||
json!({"principal_type": "group", "principal": "g", "role": "viewer", "replaces": [Uuid::new_v4()]}),
|
||||
"replaces_needs_put",
|
||||
),
|
||||
(
|
||||
"bad end date",
|
||||
json!({"principal_type": "group", "principal": "g", "role": "viewer", "expires_at": "2020-01-01T00:00:00Z"}),
|
||||
"bad_end_date",
|
||||
),
|
||||
(
|
||||
"unknown orbit",
|
||||
json!({"principal_type": "group", "principal": "g", "role": "viewer", "orbit_id": Uuid::new_v4()}),
|
||||
"unknown_orbit",
|
||||
),
|
||||
] {
|
||||
let (status, body) = w.send(&admin, "POST", grants, Some(body)).await;
|
||||
assert!(status.is_client_error(), "{what}");
|
||||
assert_eq!(body["reason"], reason, "{what}");
|
||||
}
|
||||
|
||||
// the emergency operator is an administrator and never holds grants
|
||||
let emergency = w
|
||||
.state
|
||||
.db
|
||||
.list_users()
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.find(|u| u.is_emergency);
|
||||
if let Some(emergency) = emergency {
|
||||
let (status, body) = w
|
||||
.send(
|
||||
&admin,
|
||||
"POST",
|
||||
grants,
|
||||
Some(
|
||||
json!({"principal_type": "user", "principal": emergency.id, "role": "viewer"}),
|
||||
),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(
|
||||
(status, body["reason"].as_str()),
|
||||
(StatusCode::UNPROCESSABLE_ENTITY, Some("emergency_operator"))
|
||||
);
|
||||
}
|
||||
|
||||
// renew: the same set with a new end date replaces the old rows; nothing is ever missing in between
|
||||
let ids: Vec<Value> = created
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|g| g["id"].clone())
|
||||
.collect();
|
||||
let (status, renewed) = w
|
||||
.send(&admin, "PUT", grants, Some(json!({"principal_type": "user", "principal": uid, "permissions": ["orbit.read", "snapshot.read", "capture.run"], "orbit_id": orbit, "expires_at": end(30), "replaces": ids})))
|
||||
.await;
|
||||
assert_eq!(status, StatusCode::OK);
|
||||
assert_eq!(renewed.as_array().unwrap().len(), 3);
|
||||
let stored = w.state.db.list_grants().unwrap();
|
||||
let mine: Vec<_> = stored.iter().filter(|g| g.principal == uid).collect();
|
||||
assert_eq!(mine.len(), 3, "renewing does not duplicate");
|
||||
assert!(mine.iter().all(|g| g.expires_at.is_some()));
|
||||
|
||||
// edit: dropping a permission removes just that one
|
||||
let ids: Vec<Value> = mine.iter().map(|g| json!(g.id)).collect();
|
||||
let (status, _) = w
|
||||
.send(&admin, "PUT", grants, Some(json!({"principal_type": "user", "principal": uid, "permissions": ["orbit.read", "snapshot.read"], "orbit_id": orbit, "replaces": ids})))
|
||||
.await;
|
||||
assert_eq!(status, StatusCode::OK);
|
||||
let after: Vec<String> = w
|
||||
.state
|
||||
.db
|
||||
.list_grants()
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.filter(|g| g.principal == uid)
|
||||
.map(|g| g.permission)
|
||||
.collect();
|
||||
assert_eq!(after.len(), 2);
|
||||
assert!(!after.contains(&"capture.run".to_string()));
|
||||
|
||||
// a grant of someone else cannot be swept into the replacement, and nothing changes when it is refused
|
||||
let other = w
|
||||
.state
|
||||
.db
|
||||
.add_grant(
|
||||
&NewGrant {
|
||||
principal_type: "group",
|
||||
principal: "elsewhere",
|
||||
permission: "orbit.read",
|
||||
orbit: None,
|
||||
expires_at: None,
|
||||
created_by: "test",
|
||||
},
|
||||
now(),
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let before = w.state.db.list_grants().unwrap().len();
|
||||
let (status, body) = w
|
||||
.send(&admin, "PUT", grants, Some(json!({"principal_type": "user", "principal": uid, "permissions": ["orbit.read"], "orbit_id": orbit, "replaces": [other.id]})))
|
||||
.await;
|
||||
assert_eq!(
|
||||
(status, body["reason"].as_str()),
|
||||
(StatusCode::UNPROCESSABLE_ENTITY, Some("grant_mismatch"))
|
||||
);
|
||||
assert_eq!(w.state.db.list_grants().unwrap().len(), before);
|
||||
|
||||
// the listing carries refresh grants and the administrator-only permissions
|
||||
let (status, listing) = w.send(&admin, "GET", grants, None).await;
|
||||
assert_eq!(status, StatusCode::OK);
|
||||
assert!(listing["relation_grants"].is_array());
|
||||
let admin_only: Vec<&str> = listing["admin_only"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.filter_map(Value::as_str)
|
||||
.collect();
|
||||
assert!(
|
||||
admin_only.contains(&"access.manage")
|
||||
&& !admin_only.contains(&"refresh.run")
|
||||
&& !admin_only.contains(&"orbit.read")
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn the_user_list_keeps_what_the_identity_provider_said_at_the_last_sign_in() {
|
||||
let w = world().await;
|
||||
let admin = w.caller("admin", true, &[]);
|
||||
let _qa = w.caller("qa-person", false, &["qa", "developer"]);
|
||||
let (status, users) = w.send(&admin, "GET", "/api/v1/access/users", None).await;
|
||||
assert_eq!(status, StatusCode::OK);
|
||||
let find = |name: &str| {
|
||||
users
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.find(|u| u["display_name"] == name)
|
||||
.cloned()
|
||||
.unwrap()
|
||||
};
|
||||
assert_eq!(find("admin")["admin"], true);
|
||||
assert_eq!(find("qa-person")["admin"], false);
|
||||
assert_eq!(find("qa-person")["groups"], json!(["qa", "developer"]));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user