Organize device inventory by customer and restrict token access to its audience
This commit is contained in:
@@ -0,0 +1,478 @@
|
||||
//! Visible inventory for the fleet and for a single customer. Each database
|
||||
//! read stays inside an authorized customer's transaction, including pagination.
|
||||
use crate::{
|
||||
jobs::Error,
|
||||
pagination::{Cursor, PageQuery},
|
||||
};
|
||||
use axum::{
|
||||
Json,
|
||||
extract::{Path, Query, State},
|
||||
http::{HeaderMap, HeaderValue, StatusCode},
|
||||
};
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Value, json};
|
||||
use sqlx::{Postgres, Row, Transaction, postgres::PgRow};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Default, Deserialize, Serialize, schemars::JsonSchema, Clone, Copy)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum View {
|
||||
#[default]
|
||||
Current,
|
||||
History,
|
||||
}
|
||||
impl View {
|
||||
fn name(self) -> &'static str {
|
||||
match self {
|
||||
Self::Current => "current",
|
||||
Self::History => "history",
|
||||
}
|
||||
}
|
||||
}
|
||||
#[derive(Deserialize, Serialize, schemars::JsonSchema, Clone, Copy)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum Status {
|
||||
Online,
|
||||
Offline,
|
||||
}
|
||||
impl Status {
|
||||
fn name(self) -> &'static str {
|
||||
match self {
|
||||
Self::Online => "online",
|
||||
Self::Offline => "offline",
|
||||
}
|
||||
}
|
||||
}
|
||||
#[derive(Default, Deserialize, schemars::JsonSchema)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Filters {
|
||||
pub cursor: Option<String>,
|
||||
pub limit: Option<u16>,
|
||||
#[serde(default)]
|
||||
pub view: View,
|
||||
pub status: Option<Status>,
|
||||
pub q: Option<String>,
|
||||
}
|
||||
#[derive(Default, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct DetailQuery {
|
||||
pub agent_id: Option<Uuid>,
|
||||
}
|
||||
|
||||
pub async fn page(
|
||||
State(s): State<crate::State>,
|
||||
h: HeaderMap,
|
||||
Path(tenant): Path<Uuid>,
|
||||
Query(q): Query<Filters>,
|
||||
) -> Result<Json<Value>, Error> {
|
||||
list(&s, &h, Some(tenant), q).await.map(Json)
|
||||
}
|
||||
pub async fn all(
|
||||
State(s): State<crate::State>,
|
||||
h: HeaderMap,
|
||||
Query(q): Query<Filters>,
|
||||
) -> Result<Json<Value>, Error> {
|
||||
list(&s, &h, None, q).await.map(Json)
|
||||
}
|
||||
|
||||
struct Selection<'a> {
|
||||
tenant: Uuid,
|
||||
asset: Option<Uuid>,
|
||||
agent: Option<Uuid>,
|
||||
view: Option<View>,
|
||||
status: Option<Status>,
|
||||
hostname: &'a str,
|
||||
last: Uuid,
|
||||
as_of: DateTime<Utc>,
|
||||
limit: i64,
|
||||
}
|
||||
impl Selection<'_> {
|
||||
async fn rows(&self, tx: &mut Transaction<'_, Postgres>) -> Result<Vec<PgRow>, Error> {
|
||||
Ok(sqlx::query(include_str!("device_list.sql"))
|
||||
.bind(self.tenant)
|
||||
.bind(self.as_of)
|
||||
.bind(self.last)
|
||||
.bind(self.asset)
|
||||
.bind(self.agent)
|
||||
.bind(self.view.map(View::name))
|
||||
.bind(self.status.map(Status::name))
|
||||
.bind(self.hostname)
|
||||
.bind(self.limit)
|
||||
.fetch_all(&mut **tx)
|
||||
.await?)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn list(
|
||||
s: &crate::State,
|
||||
h: &HeaderMap,
|
||||
tenant: Option<Uuid>,
|
||||
q: Filters,
|
||||
) -> Result<Value, Error> {
|
||||
let p = crate::jobs::principal(s, h, false).await?;
|
||||
let mut tenants = match tenant {
|
||||
Some(id) => {
|
||||
crate::jobs::scope(&p, id, false)?;
|
||||
vec![id]
|
||||
}
|
||||
None => p.tenants.clone(),
|
||||
};
|
||||
tenants.sort_unstable();
|
||||
tenants.dedup();
|
||||
let hostname = q.q.as_deref().unwrap_or("").trim();
|
||||
if hostname.len() > 255 || hostname.chars().any(char::is_control) {
|
||||
return Err(Error(StatusCode::BAD_REQUEST, "VALIDATION_ERROR"));
|
||||
}
|
||||
let binding = json!([tenant, tenants, q.view, q.status, hostname]);
|
||||
let scope = format!(
|
||||
"device-fleet:{}",
|
||||
rmm_domain::jobs::hash(binding.to_string().as_bytes())
|
||||
);
|
||||
let mut page = Cursor::parse(
|
||||
s,
|
||||
&p,
|
||||
&scope,
|
||||
PageQuery {
|
||||
cursor: q.cursor,
|
||||
limit: q.limit,
|
||||
},
|
||||
)
|
||||
.map_err(|code| Error(code, crate::auth::error_code(code)))?;
|
||||
let capacity = usize::from(page.limit);
|
||||
let mut items = Vec::new();
|
||||
let mut last = None;
|
||||
let mut truncated = false;
|
||||
let mut authorized = false;
|
||||
for customer in tenants {
|
||||
if page.last_tenant.is_some_and(|after| customer < after) {
|
||||
continue;
|
||||
}
|
||||
let mut headers = h.clone();
|
||||
headers.insert(
|
||||
"x-rmm-tenant",
|
||||
HeaderValue::from_str(&customer.to_string())
|
||||
.map_err(|_| Error(StatusCode::BAD_REQUEST, "VALIDATION_ERROR"))?,
|
||||
);
|
||||
let current = crate::jobs::principal(s, &headers, false).await?;
|
||||
crate::jobs::scope(¤t, customer, false)?;
|
||||
let mut tx = rmm_persistence::tenant_tx(&s.pool, customer).await?;
|
||||
match crate::permissions::require_principal(&mut tx, ¤t, "inventory:read").await {
|
||||
Err(Error(StatusCode::FORBIDDEN, _)) if tenant.is_none() => continue,
|
||||
other => other?,
|
||||
}
|
||||
authorized = true;
|
||||
let rows = Selection {
|
||||
tenant: customer,
|
||||
asset: None,
|
||||
agent: None,
|
||||
view: Some(q.view),
|
||||
status: q.status,
|
||||
hostname,
|
||||
last: if page.last_tenant == Some(customer) {
|
||||
page.last
|
||||
} else {
|
||||
Uuid::nil()
|
||||
},
|
||||
as_of: page.as_of,
|
||||
limit: (capacity + 1 - items.len()) as i64,
|
||||
}
|
||||
.rows(&mut tx)
|
||||
.await?;
|
||||
tx.commit().await?;
|
||||
for row in rows {
|
||||
if items.len() == capacity {
|
||||
truncated = true;
|
||||
break;
|
||||
}
|
||||
last = Some((customer, row.get::<Uuid, _>("agent_id")));
|
||||
items.push(project(&row, page.as_of, tenant.is_some()));
|
||||
}
|
||||
if truncated {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if !authorized {
|
||||
return Err(Error(StatusCode::FORBIDDEN, "FORBIDDEN"));
|
||||
}
|
||||
let next = if let Some((customer, agent)) = last.filter(|_| truncated) {
|
||||
page.last_tenant = Some(customer);
|
||||
Some(
|
||||
page.next(s, agent)
|
||||
.map_err(|code| Error(code, crate::auth::error_code(code)))?,
|
||||
)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
Ok(
|
||||
json!({"items":items,"next_cursor":next,"truncated":truncated,"completeness":if truncated{"partial"}else{"complete"},"as_of":page.as_of}),
|
||||
)
|
||||
}
|
||||
|
||||
pub async fn detail(
|
||||
State(s): State<crate::State>,
|
||||
h: HeaderMap,
|
||||
Path((tenant, asset)): Path<(Uuid, Uuid)>,
|
||||
Query(query): Query<DetailQuery>,
|
||||
) -> Result<Json<Value>, Error> {
|
||||
let p = crate::jobs::principal(&s, &h, false).await?;
|
||||
crate::jobs::scope(&p, tenant, false)?;
|
||||
let mut tx = rmm_persistence::tenant_tx(&s.pool, tenant).await?;
|
||||
crate::permissions::require_principal(&mut tx, &p, "inventory:read").await?;
|
||||
let now = Utc::now();
|
||||
let row = Selection {
|
||||
tenant,
|
||||
asset: Some(asset),
|
||||
agent: query.agent_id,
|
||||
view: None,
|
||||
status: None,
|
||||
hostname: "",
|
||||
last: Uuid::nil(),
|
||||
as_of: now,
|
||||
limit: 1,
|
||||
}
|
||||
.rows(&mut tx)
|
||||
.await?
|
||||
.pop()
|
||||
.ok_or(Error(StatusCode::NOT_FOUND, "NOT_FOUND"))?;
|
||||
let mut item = project(&row, now, true);
|
||||
item["volumes"] = volumes(&item["inventory"]);
|
||||
item["recent_jobs"] = if p.permits_scope("operations:read")
|
||||
&& crate::permissions::allowed(&mut tx, &p.name, "operations:read").await?
|
||||
{
|
||||
sqlx::query_scalar::<_,Value>("SELECT jsonb_build_object('job_id',j.job_id,'operation_id',j.operation_id,'state',j.state,'created_at',r.created_at) FROM jobs j JOIN approval_requests r USING(tenant_id,operation_id) WHERE j.tenant_id=$1 AND j.asset_id=$2 AND j.agent_id=$3 ORDER BY r.created_at DESC,j.job_id LIMIT 20")
|
||||
.bind(tenant).bind(asset).bind(row.get::<Uuid,_>("agent_id")).fetch_all(&mut *tx).await?.into()
|
||||
} else {
|
||||
Value::Null
|
||||
};
|
||||
tx.commit().await?;
|
||||
Ok(Json(item))
|
||||
}
|
||||
|
||||
fn observed<'a>(data: &'a Value, scope: &str) -> &'a Value {
|
||||
data["observations"]
|
||||
.as_array()
|
||||
.and_then(|items| {
|
||||
items
|
||||
.iter()
|
||||
.find(|o| o["scope"] == scope && o["quality"] == "valid")
|
||||
})
|
||||
.map(|o| &o["data"])
|
||||
.unwrap_or(&Value::Null)
|
||||
}
|
||||
fn volumes(data: &Value) -> Value {
|
||||
let mut items = Vec::new();
|
||||
if let Some(rows) = observed(data, "linux.volumes").as_array() {
|
||||
for entry in rows.iter().filter(|entry| entry["quality"] == "valid") {
|
||||
let v = &entry["data"];
|
||||
if v["mount_point"].is_string() {
|
||||
items.push(json!({"name":v["mount_point"],"free_bytes":v["available_bytes"].as_u64(),"total_bytes":v["total_bytes"].as_u64()}));
|
||||
}
|
||||
}
|
||||
}
|
||||
if items.is_empty() && data["root_total_bytes"].is_number() {
|
||||
let name = if observed(data, "windows.volumes").is_object() {
|
||||
"Systemvolume"
|
||||
} else {
|
||||
"/"
|
||||
};
|
||||
items.push(json!({"name":name,"free_bytes":data["root_available_bytes"],"total_bytes":data["root_total_bytes"]}));
|
||||
}
|
||||
items.into()
|
||||
}
|
||||
fn os_label(data: &Value) -> Option<String> {
|
||||
let raw = data["os"].as_str()?;
|
||||
if let Some(pretty) = observed(data, "linux.os")["release"]["PRETTY_NAME"].as_str() {
|
||||
return Some(pretty.to_owned());
|
||||
}
|
||||
if let Some(pretty) = raw.lines().find_map(|l| l.strip_prefix("PRETTY_NAME=")) {
|
||||
return Some(pretty.trim_matches(['"', '\'']).to_owned());
|
||||
}
|
||||
let product = observed(data, "windows.os");
|
||||
if let Some(name) = product["product_name"].as_str() {
|
||||
// Windows 11 can retain a Windows 10 client ProductName for compatibility.
|
||||
let product_name = match (
|
||||
name.strip_prefix("Windows 10 "),
|
||||
product["build"]
|
||||
.as_str()
|
||||
.and_then(|s| s.parse::<u32>().ok()),
|
||||
) {
|
||||
(Some(edition), Some(build))
|
||||
if build >= 22000
|
||||
&& ["Home", "Pro", "Enterprise", "Education", "IoT"]
|
||||
.iter()
|
||||
.any(|prefix| {
|
||||
edition == *prefix
|
||||
|| edition
|
||||
.strip_prefix(prefix)
|
||||
.is_some_and(|suffix| suffix.starts_with(' '))
|
||||
}) =>
|
||||
{
|
||||
format!("Windows 11 {edition}")
|
||||
}
|
||||
_ => name.to_owned(),
|
||||
};
|
||||
return Some(
|
||||
match product["display_version"]
|
||||
.as_str()
|
||||
.filter(|v| !v.is_empty())
|
||||
{
|
||||
Some(version) => format!("{product_name} {version}"),
|
||||
None => product_name,
|
||||
},
|
||||
);
|
||||
}
|
||||
if !raw.contains('\n') {
|
||||
Some(raw.to_owned())
|
||||
} else {
|
||||
Some("Linux".to_owned())
|
||||
}
|
||||
}
|
||||
fn project(row: &PgRow, now: DateTime<Utc>, full: bool) -> Value {
|
||||
let data = row.get::<Option<Value>, _>("data").unwrap_or(Value::Null);
|
||||
let cpu = observed(&data, "linux.cpu");
|
||||
let windows = observed(&data, "windows.hardware");
|
||||
let last = row.get::<Option<DateTime<Utc>>, _>("last_contact_at");
|
||||
let revoked = row.get::<Option<DateTime<Utc>>, _>("revoked_at");
|
||||
let presence = match (revoked, last.map(|t| (now - t).num_seconds())) {
|
||||
(Some(_), _) => "revoked",
|
||||
(_, None) => "never_seen",
|
||||
(_, Some(age)) if age < 90 => "online",
|
||||
(_, Some(age)) if age < 180 => "stale",
|
||||
_ => "unreachable",
|
||||
};
|
||||
let mut item = json!({
|
||||
"tenant_id":row.get::<Uuid,_>("tenant_id"),"customer_name":row.get::<String,_>("customer_name"),
|
||||
"agent_id":row.get::<Uuid,_>("agent_id"),"asset_id":row.get::<Uuid,_>("asset_id"),
|
||||
"status":row.get::<String,_>("status"),"lifecycle":row.get::<String,_>("lifecycle"),
|
||||
"archived_at":row.get::<Option<DateTime<Utc>>,_>("archived_at"),"revoked_at":revoked,
|
||||
"hostname":data["hostname"].as_str(),"os":os_label(&data),
|
||||
"disk_free_bytes":data["root_available_bytes"].as_u64(),"disk_total_bytes":data["root_total_bytes"].as_u64(),
|
||||
"cpu_model":cpu["model"].as_str().or_else(||windows["cpu_model"].as_str()),
|
||||
"cpu_logical_count":cpu["logical_processors"].as_u64().or_else(||windows["logical_processors_visible"].as_u64()),
|
||||
"ram_total_bytes":observed(&data,"linux.memory")["total_bytes"].as_u64().or_else(||windows["memory_total_bytes"].as_u64()),
|
||||
"last_contact_at":last,"agent_version":row.get::<Option<String>,_>("reported_agent_version"),
|
||||
"presence":presence,"observed_at":row.get::<Option<DateTime<Utc>>,_>("observed_at"),
|
||||
"received_at":row.get::<Option<DateTime<Utc>>,_>("received_at"),
|
||||
"source_id":row.get::<Option<String>,_>("source_id"),"quality":if data.is_null(){"unknown"}else{"valid"},
|
||||
"completeness":"partial","health":"unknown"
|
||||
});
|
||||
if full {
|
||||
item["inventory"] = data;
|
||||
item["capabilities"] = row.get("capabilities");
|
||||
item["enrollment_generation"] = row.get::<i64, _>("enrollment_generation").into();
|
||||
item["assignment_version"] = row.get::<i64, _>("assignment_version").into();
|
||||
item["certificate_sha256"] = row.get::<String, _>("certificate_sha256").into();
|
||||
item["last_authenticated_certificate_sha256"] =
|
||||
json!(row.get::<Option<String>, _>("last_authenticated_certificate_sha256"));
|
||||
item["last_authenticated_at"] =
|
||||
json!(row.get::<Option<DateTime<Utc>>, _>("last_authenticated_at"));
|
||||
}
|
||||
item
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn windows_os(name: &str, build: &str) -> String {
|
||||
let old = json!({"os":name,"observations":[{"scope":"windows.os","quality":"valid",
|
||||
"data":{"product_name":name,"build":build,"display_version":"26H2"}}]});
|
||||
os_label(&old).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn os_labels_keep_the_edition_and_handle_existing_snapshots() {
|
||||
assert_eq!(windows_os("Windows 10 Pro", "26300"), "Windows 11 Pro 26H2");
|
||||
assert_eq!(windows_os("Windows 10 Pro", "19045"), "Windows 10 Pro 26H2");
|
||||
assert_eq!(
|
||||
windows_os("Windows Server 2025", "26100"),
|
||||
"Windows Server 2025 26H2"
|
||||
);
|
||||
assert_eq!(
|
||||
windows_os("Windows 10 Pro", "invalid"),
|
||||
"Windows 10 Pro 26H2"
|
||||
);
|
||||
assert_eq!(
|
||||
windows_os("Windows 10 Custom", "26100"),
|
||||
"Windows 10 Custom 26H2"
|
||||
);
|
||||
let linux = json!({"os":"NAME=Example\nPRETTY_NAME=\"Test Linux 1.2\"\nID=linux"});
|
||||
assert_eq!(os_label(&linux).as_deref(), Some("Test Linux 1.2"));
|
||||
assert!(os_label(&Value::Null).is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "requires the disposable RLS database from tests/local_access_database.py"]
|
||||
async fn visibility_is_filtered_before_the_cursor_limit() {
|
||||
let admin = sqlx::PgPool::connect(&std::env::var("RMM_TEST_REPAIR_ADMIN_URL").unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
let app = sqlx::PgPool::connect(&std::env::var("RMM_TEST_REPAIR_APP_URL").unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
let tenant = Uuid::new_v4();
|
||||
sqlx::query("INSERT INTO tenants(tenant_id,name) VALUES($1,'Filter test')")
|
||||
.bind(tenant)
|
||||
.execute(&admin)
|
||||
.await
|
||||
.unwrap();
|
||||
// In this order: archived, revoked, never connected, old unauthenticated
|
||||
// metadata, confirmed offline, confirmed online. Same hostnames are intentional.
|
||||
for n in 1..=6_i32 {
|
||||
let id = Uuid::from_u128(n as u128);
|
||||
sqlx::query("INSERT INTO assets(tenant_id,asset_id,archived_at) VALUES($1,$2,CASE WHEN $3=1 THEN now() END)")
|
||||
.bind(tenant).bind(id).bind(n).execute(&admin).await.unwrap();
|
||||
let cert = hex::encode(openssl::sha::sha256(format!("{tenant}/{id}").as_bytes()));
|
||||
sqlx::query("INSERT INTO agent_installations(tenant_id,asset_id,agent_id,certificate_sha256,revoked_at,last_contact_at,last_authenticated_at,last_authenticated_certificate_sha256) VALUES($1,$2,$2,$3,CASE WHEN $4=2 THEN now() END,CASE WHEN $4=5 THEN now()-interval '30 days' WHEN $4<>3 THEN now() END,CASE WHEN $4 NOT IN (3,4) THEN now() END, CASE WHEN $4 NOT IN (3,4) THEN $3 END)")
|
||||
.bind(tenant).bind(id).bind(cert).bind(n).execute(&admin).await.unwrap();
|
||||
let data = json!({"hostname":"same-host","os":"Ubuntu","root_available_bytes":10,"root_total_bytes":20,
|
||||
"observations":[{"scope":"linux.memory","quality":"valid","data":{"total_bytes":1024}},{"scope":"linux.cpu","quality":"valid","data":{"logical_processors":2,"model":"Test CPU"}}]});
|
||||
sqlx::query("INSERT INTO observations(tenant_id,agent_id,message_id,boot_id,sequence,observed_at,source_id,quality,completeness,snapshot_id,data,payload_sha256) VALUES($1,$2,$3,$4,1,now(),'linux.base','valid','partial',$5,$6,$7)")
|
||||
.bind(tenant).bind(id).bind(Uuid::new_v4()).bind(Uuid::new_v4()).bind(Uuid::new_v4()).bind(data).bind("0".repeat(64)).execute(&admin).await.unwrap();
|
||||
}
|
||||
let mut tx = rmm_persistence::tenant_tx(&app, tenant).await.unwrap();
|
||||
let mut q = Selection {
|
||||
tenant,
|
||||
asset: None,
|
||||
agent: None,
|
||||
view: Some(View::Current),
|
||||
status: None,
|
||||
hostname: "AME-h",
|
||||
last: Uuid::nil(),
|
||||
as_of: Utc::now(),
|
||||
limit: 1,
|
||||
};
|
||||
let row = q.rows(&mut tx).await.unwrap().pop().unwrap();
|
||||
let summary = project(&row, q.as_of, false);
|
||||
assert_eq!(summary["status"], "offline");
|
||||
assert_eq!(summary["ram_total_bytes"], 1024);
|
||||
assert_eq!(summary["cpu_model"], "Test CPU");
|
||||
assert_eq!(summary["disk_free_bytes"], 10);
|
||||
q.last = row.get("agent_id");
|
||||
assert_eq!(
|
||||
q.rows(&mut tx).await.unwrap()[0].get::<String, _>("status"),
|
||||
"online"
|
||||
);
|
||||
q.status = Some(Status::Offline);
|
||||
assert!(q.rows(&mut tx).await.unwrap().is_empty());
|
||||
q.status = None;
|
||||
q.last = Uuid::nil();
|
||||
q.view = Some(View::History);
|
||||
q.limit = 10;
|
||||
let got: Vec<String> = q
|
||||
.rows(&mut tx)
|
||||
.await
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|r| r.get("lifecycle"))
|
||||
.collect();
|
||||
assert_eq!(got, ["archived", "revoked", "never_seen", "unverified"]);
|
||||
q.hostname = "%";
|
||||
assert!(q.rows(&mut tx).await.unwrap().is_empty());
|
||||
q.hostname = "";
|
||||
q.tenant = Uuid::new_v4();
|
||||
assert!(q.rows(&mut tx).await.unwrap().is_empty());
|
||||
tx.rollback().await.unwrap();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user