Recheck the submitting principal and protect stored log output
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
use super::*;
|
||||
use crate::monitoring::{LogQuery, query};
|
||||
use std::sync::Arc;
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "requires the disposable RLS database from tests/local_access_database.py"]
|
||||
async fn stored_results_require_current_log_rights_on_both_read_paths() {
|
||||
let admin = PgPool::connect(&std::env::var("RMM_TEST_REPAIR_ADMIN_URL").unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
let pool = PgPool::connect(&std::env::var("RMM_TEST_REPAIR_APP_URL").unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
let tenant = Uuid::parse_str(rmm_domain::TENANT_A).unwrap();
|
||||
let foreign = Uuid::parse_str(rmm_domain::TENANT_B).unwrap();
|
||||
let asset = Uuid::new_v4();
|
||||
let actor = format!("local:{}", Uuid::new_v4());
|
||||
let temp = std::env::temp_dir().join(format!("stored-logs-{asset}"));
|
||||
let state = crate::State {
|
||||
pool,
|
||||
signer: None,
|
||||
viewer_a: [0; 32],
|
||||
viewer_b: [1; 32],
|
||||
auth: Arc::new(crate::auth::Auth::from_env("development").await.unwrap()),
|
||||
job_signer: Arc::new(
|
||||
crate::job_signer::JobSigner::load_or_create(&temp.join("key.pem")).unwrap(),
|
||||
),
|
||||
};
|
||||
sqlx::query("INSERT INTO memberships(tenant_id,principal,kind,permissions) VALUES($1,$2,'human',ARRAY['tokens:manage','operations:read','logs:read','logs:read_sensitive'])")
|
||||
.bind(tenant).bind(&actor).execute(&admin).await.unwrap();
|
||||
let mut readers = Vec::new();
|
||||
for scopes in [
|
||||
vec!["operations:read"],
|
||||
vec!["operations:read", "logs:read"],
|
||||
vec!["operations:read", "logs:read", "logs:read_sensitive"],
|
||||
] {
|
||||
let (_, token) = mint(&state.pool, tenant, &actor, &scopes).await;
|
||||
let p = authenticate_pool(&state.pool, &token, Some(tenant))
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(!p.operator);
|
||||
readers.push(p);
|
||||
}
|
||||
|
||||
// Terminal history with inert synthetic bytes, no gateway, outbox or permits.
|
||||
sqlx::query("INSERT INTO assets(tenant_id,asset_id) VALUES($1,$2)")
|
||||
.bind(tenant)
|
||||
.bind(asset)
|
||||
.execute(&admin)
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::query("INSERT INTO agent_installations(tenant_id,agent_id,asset_id,certificate_sha256) VALUES($1,$2,$2,$3)").bind(tenant).bind(asset).bind(hash(asset.as_bytes())).execute(&admin).await.unwrap();
|
||||
let operation = Uuid::new_v4();
|
||||
sqlx::query("INSERT INTO operation_plans(tenant_id,plan_id,principal,payload,plan_hash,expires_at) VALUES($1,$2,$3,'{}','fixture',now())").bind(tenant).bind(operation).bind(&actor).execute(&admin).await.unwrap();
|
||||
sqlx::query("INSERT INTO approval_requests(tenant_id,operation_id,plan_id,principal,idempotency_key,request_hash,approval_state) VALUES($1,$2,$2,$3,'fixture','fixture','approved')").bind(tenant).bind(operation).bind(&actor).execute(&admin).await.unwrap();
|
||||
sqlx::query("INSERT INTO authorizations(tenant_id,authorization_id,operation_id,plan_id,plan_hash,principal,session_hash,approval_origin,testsimulation,policy_version) VALUES($1,$2,$2,$2,'fixture',$3,'fixture','dev_test_browser_simulation',true,1)").bind(tenant).bind(operation).bind(&actor).execute(&admin).await.unwrap();
|
||||
sqlx::query("INSERT INTO operations VALUES($1,$2,$2)")
|
||||
.bind(tenant)
|
||||
.bind(operation)
|
||||
.execute(&admin)
|
||||
.await
|
||||
.unwrap();
|
||||
let until = Utc::now();
|
||||
let since = until - Duration::minutes(5);
|
||||
let query_for = |unit: &str| LogQuery {
|
||||
tenant_id: tenant,
|
||||
asset_id: asset,
|
||||
since: since - Duration::seconds(1),
|
||||
until: until + Duration::seconds(1),
|
||||
unit: unit.into(),
|
||||
max_lines: 10,
|
||||
live: false,
|
||||
idempotency_key: None,
|
||||
};
|
||||
let stored = json!({"exit_code":0,"stdout":[65],"stderr":[66],"evidence":{"fixture":true},"error_code":"fixture"});
|
||||
let restricted = json!({"exit_code":0,"output_restricted":true});
|
||||
let mut secure_job = Uuid::nil();
|
||||
for (unit, classification) in [
|
||||
("ordinary.service", Some(false)),
|
||||
("custom-audit.service", Some(true)),
|
||||
("legacy.service", None),
|
||||
] {
|
||||
let job = Uuid::new_v4();
|
||||
let bytes = serde_json::to_vec(&json!({"action":{"action":"query_logs","unit":unit,"since":since,"until":until,"max_lines":10}})).unwrap();
|
||||
sqlx::query("INSERT INTO jobs(tenant_id,job_id,attempt_id,operation_id,agent_id,asset_id,payload,payload_hash,state,resource,result,log_sensitive) VALUES($1,$2,$2,$3,$4,$4,$5,'fixture','succeeded','device_execution',$6,$7)")
|
||||
.bind(tenant).bind(job).bind(operation).bind(asset).bind(bytes).bind(&stored).bind(classification).execute(&admin).await.unwrap();
|
||||
for (index, p) in readers.iter().enumerate() {
|
||||
let permitted = index == 2 || (index == 1 && classification == Some(false));
|
||||
let detail = crate::jobs::job(&state, p, tenant, job).await.unwrap();
|
||||
assert_eq!(detail["state"], "succeeded");
|
||||
assert_eq!(
|
||||
&detail["result"],
|
||||
if permitted { &stored } else { &restricted }
|
||||
);
|
||||
let logs = query(&state, p, query_for(unit)).await;
|
||||
if permitted {
|
||||
assert_eq!(logs.unwrap()["items"][0]["result"], stored);
|
||||
} else {
|
||||
assert_eq!(logs.unwrap_err().0, StatusCode::FORBIDDEN);
|
||||
}
|
||||
}
|
||||
if classification == Some(true) {
|
||||
secure_job = job;
|
||||
}
|
||||
}
|
||||
// Keep the previously authenticated reader and remove just the issuer's
|
||||
// extra permission. The next read must consult current tenant membership.
|
||||
sqlx::query("UPDATE memberships SET permissions=array_remove(permissions,'logs:read_sensitive') WHERE tenant_id=$1 AND principal=$2")
|
||||
.bind(tenant).bind(&actor).execute(&admin).await.unwrap();
|
||||
let reader = &readers[2];
|
||||
assert_eq!(
|
||||
crate::jobs::job(&state, reader, tenant, secure_job)
|
||||
.await
|
||||
.unwrap()["result"],
|
||||
restricted
|
||||
);
|
||||
assert_eq!(
|
||||
query(&state, reader, query_for("custom-audit.service"))
|
||||
.await
|
||||
.unwrap_err()
|
||||
.0,
|
||||
StatusCode::FORBIDDEN
|
||||
);
|
||||
assert_eq!(
|
||||
crate::jobs::job(&state, reader, foreign, secure_job)
|
||||
.await
|
||||
.unwrap_err()
|
||||
.0,
|
||||
StatusCode::NOT_FOUND
|
||||
);
|
||||
let mut other = query_for("custom-audit.service");
|
||||
other.tenant_id = foreign;
|
||||
assert_eq!(
|
||||
query(&state, reader, other).await.unwrap_err().0,
|
||||
StatusCode::NOT_FOUND
|
||||
);
|
||||
std::fs::remove_dir_all(temp).unwrap();
|
||||
}
|
||||
Reference in New Issue
Block a user