775 lines
32 KiB
Rust
775 lines
32 KiB
Rust
//! 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 rmm_domain::inventory_document::DeviceType;
|
|
use serde::{Deserialize, Serialize};
|
|
use serde_json::{Value, json};
|
|
use sqlx::{Postgres, Row, Transaction, postgres::PgRow};
|
|
use std::collections::HashMap;
|
|
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 = hostname_filter(q.q.as_deref())?;
|
|
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 first_page = q.cursor.is_none();
|
|
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;
|
|
let mut principals = Vec::new();
|
|
for customer in tenants {
|
|
if page.last_tenant.is_some_and(|after| customer < after) {
|
|
continue;
|
|
}
|
|
if customer == p.tenant {
|
|
principals.push(p.clone());
|
|
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)?;
|
|
principals.push(current);
|
|
}
|
|
// Read all customers from the same snapshot so a transfer cannot appear
|
|
// under both customers if its cutover commits while listing the fleet.
|
|
let mut tx = s.pool.begin().await?;
|
|
sqlx::query("SET TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY")
|
|
.execute(&mut *tx)
|
|
.await?;
|
|
if first_page {
|
|
page.as_of = sqlx::query_scalar("SELECT clock_timestamp()")
|
|
.fetch_one(&mut *tx)
|
|
.await?;
|
|
}
|
|
for current in principals {
|
|
let customer = current.tenant;
|
|
sqlx::query("SELECT set_config('app.tenant_id',$1,true)")
|
|
.bind(customer.to_string())
|
|
.execute(&mut *tx)
|
|
.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?;
|
|
let hosts = assigned_hosts(&mut tx, customer, page.as_of, &rows).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(), &hosts));
|
|
}
|
|
if truncated {
|
|
break;
|
|
}
|
|
}
|
|
tx.commit().await?;
|
|
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 hosts = assigned_hosts(&mut tx, tenant, now, std::slice::from_ref(&row)).await?;
|
|
let mut item = project(&row, now, true, &hosts);
|
|
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,
|
|
'title', coalesce(nullif(plan_view.payload#>>'{script_revision,name}', ''), nullif(btrim(plan_view.payload->>'reason'), '')),
|
|
'action', convert_from(j.payload, 'UTF8')::jsonb#>>'{action,action}')
|
|
FROM jobs j JOIN approval_requests r USING (tenant_id, operation_id)
|
|
JOIN operation_plans p USING (tenant_id, plan_id)
|
|
CROSS JOIN LATERAL (SELECT convert_from(p.payload, 'UTF8')::jsonb AS payload) AS plan_view
|
|
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 device_type(hardware: &Value) -> Option<DeviceType> {
|
|
DeviceType::deserialize(&hardware["device_type"]).ok()
|
|
}
|
|
|
|
fn inventory_device_type(data: &Value) -> Option<DeviceType> {
|
|
device_type(observed(data, "linux.hardware"))
|
|
.or_else(|| device_type(observed(data, "windows.hardware")))
|
|
}
|
|
|
|
fn hostname_filter(query: Option<&str>) -> Result<&str, Error> {
|
|
let hostname = query.unwrap_or("").trim();
|
|
if hostname.len() > 255 || hostname.chars().any(char::is_control) {
|
|
return Err(Error(StatusCode::BAD_REQUEST, "VALIDATION_ERROR"));
|
|
}
|
|
Ok(hostname)
|
|
}
|
|
|
|
#[derive(Serialize)]
|
|
struct Host {
|
|
asset_id: Uuid,
|
|
agent_id: Uuid,
|
|
hostname: String,
|
|
}
|
|
|
|
async fn hypervisors(
|
|
tx: &mut Transaction<'_, Postgres>,
|
|
tenant: Uuid,
|
|
as_of: DateTime<Utc>,
|
|
ids: Option<&[Uuid]>,
|
|
hostname: &str,
|
|
limit: i64,
|
|
) -> Result<Vec<Host>, Error> {
|
|
let rows: Vec<(Uuid, Uuid, String)> = sqlx::query_as(include_str!("hypervisors.sql"))
|
|
.bind(tenant)
|
|
.bind(as_of)
|
|
.bind(ids)
|
|
.bind(hostname)
|
|
.bind(limit)
|
|
.fetch_all(&mut **tx)
|
|
.await?;
|
|
Ok(rows.into_iter().map(|(asset_id, agent_id, hostname)| Host {
|
|
asset_id, agent_id, hostname,
|
|
}).collect())
|
|
}
|
|
|
|
async fn assigned_hosts(
|
|
tx: &mut Transaction<'_, Postgres>,
|
|
tenant: Uuid,
|
|
as_of: DateTime<Utc>,
|
|
rows: &[PgRow],
|
|
) -> Result<HashMap<Uuid, Host>, Error> {
|
|
let mut ids: Vec<Uuid> = rows.iter().filter_map(|row| row.get("hypervisor_asset_id")).collect();
|
|
ids.sort_unstable();
|
|
ids.dedup();
|
|
if ids.is_empty() {
|
|
return Ok(HashMap::new());
|
|
}
|
|
Ok(hypervisors(tx, tenant, as_of, Some(&ids), "", ids.len() as i64)
|
|
.await?.into_iter().map(|host| (host.asset_id, host)).collect())
|
|
}
|
|
|
|
fn hypervisor_display(data: &Value, assigned: Option<&Host>) -> Value {
|
|
if inventory_device_type(data) != Some(DeviceType::Virtual) {
|
|
return Value::Null;
|
|
}
|
|
if let Some(host) = assigned {
|
|
return json!({"kind":"device","asset_id":host.asset_id,
|
|
"agent_id":host.agent_id,"hostname":host.hostname});
|
|
}
|
|
let linux = observed(data, "linux.hardware");
|
|
let windows = observed(data, "windows.hardware");
|
|
let manufacturer = linux["manufacturer"].as_str()
|
|
.filter(|value| !value.trim().is_empty())
|
|
.or_else(|| linux["readable_dmi"]["sys_vendor"].as_str())
|
|
.or_else(|| windows["manufacturer"].as_str());
|
|
if manufacturer.is_some_and(|name| matches!(name.trim().to_ascii_lowercase().as_str(),
|
|
"hetzner" | "hetzner cloud" | "hetzner online" | "hetzner online gmbh")) {
|
|
return json!({"kind":"placeholder","provider":"hetzner","hostname":"Hetzner Cloud"});
|
|
}
|
|
Value::Null
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct HypervisorFilter {
|
|
q: Option<String>,
|
|
}
|
|
|
|
pub async fn hypervisor_options(
|
|
State(s): State<crate::State>,
|
|
h: HeaderMap,
|
|
Path(tenant): Path<Uuid>,
|
|
Query(query): Query<HypervisorFilter>,
|
|
) -> 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 mut hosts = hypervisors(&mut tx, tenant, Utc::now(), None,
|
|
hostname_filter(query.q.as_deref())?, 51).await?;
|
|
let truncated = hosts.len() > 50;
|
|
hosts.truncate(50);
|
|
tx.commit().await?;
|
|
Ok(Json(json!({"items":hosts,"truncated":truncated})))
|
|
}
|
|
|
|
pub(crate) async fn validate_vm_host_assignment(
|
|
tx: &mut Transaction<'_, Postgres>,
|
|
tenant: Uuid,
|
|
asset: Uuid,
|
|
agent: Uuid,
|
|
host: Option<Uuid>,
|
|
) -> Result<(), Error> {
|
|
let now = Utc::now();
|
|
let row = Selection {
|
|
tenant, asset: Some(asset), agent: Some(agent), view: Some(View::Current),
|
|
status: None, hostname: "", last: Uuid::nil(), as_of: now, limit: 1,
|
|
}.rows(tx).await?.pop().ok_or(Error(StatusCode::NOT_FOUND, "NOT_FOUND"))?;
|
|
let data = row.get::<Option<Value>, _>("data").unwrap_or(Value::Null);
|
|
if inventory_device_type(&data) != Some(DeviceType::Virtual) {
|
|
return Err(Error(StatusCode::CONFLICT, "HYPERVISOR_REQUIRES_VM"));
|
|
}
|
|
if let Some(host) = host {
|
|
if asset == host {
|
|
return Err(Error(StatusCode::CONFLICT, "INVALID_HYPERVISOR"));
|
|
}
|
|
if hypervisors(tx, tenant, now, Some(&[host]), "", 1).await?.is_empty() {
|
|
return Err(Error(StatusCode::NOT_FOUND, "HYPERVISOR_UNAVAILABLE"));
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn os_label(data: &Value) -> Option<String> {
|
|
let linux_hardware = observed(data, "linux.hardware");
|
|
if linux_hardware["proxmox_ve"].as_bool() == Some(true)
|
|
&& device_type(linux_hardware) == Some(DeviceType::Hypervisor)
|
|
{
|
|
return Some("Proxmox VE".to_owned());
|
|
}
|
|
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, hosts: &HashMap<Uuid, Host>) -> 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 linux_memory = observed(&data, "linux.memory");
|
|
let usage = row
|
|
.get::<Option<Value>, _>("system_usage")
|
|
.unwrap_or(Value::Null);
|
|
let usage_at: Option<DateTime<Utc>> = row.get("system_usage_observed_at");
|
|
let inventory_at: Option<DateTime<Utc>> = row.get("observed_at");
|
|
let cpu_observed_at = usage["cpu_usage_observed_at"].as_str();
|
|
let cpu_usage = cpu_observed_at.and_then(|_| usage["cpu_usage_percent"].as_f64());
|
|
let ram_available = usage["ram_available_bytes"].as_u64();
|
|
let ram_total = usage["ram_total_bytes"]
|
|
.as_u64()
|
|
.or_else(|| linux_memory["total_bytes"].as_u64())
|
|
.or_else(|| windows["memory_total_bytes"].as_u64());
|
|
let device_type = inventory_device_type(&data);
|
|
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(),"device_type":device_type,"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()),
|
|
"cpu_usage_percent":cpu_usage,
|
|
"cpu_usage_observed_at":cpu_observed_at,
|
|
"ram_total_bytes":ram_total,
|
|
"ram_available_bytes":ram_available.or_else(|| linux_memory["available_bytes"].as_u64().or_else(||windows["memory_available_bytes"].as_u64())
|
|
.filter(|available| ram_total.is_some_and(|total| *available <= total))),
|
|
"ram_observed_at":if ram_available.is_some() {usage_at} else {inventory_at},
|
|
"last_contact_at":last,"agent_version":row.get::<Option<String>,_>("reported_agent_version"),
|
|
"presence":presence,"observed_at":inventory_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 device_type == Some(DeviceType::Virtual) && row.get::<String, _>("lifecycle") == "current" {
|
|
let assigned_id: Option<Uuid> = row.get("hypervisor_asset_id");
|
|
let assigned = assigned_id.and_then(|id| hosts.get(&id));
|
|
item["hypervisor"] = hypervisor_display(&data, assigned);
|
|
if full {
|
|
item["assigned_hypervisor_asset_id"] = json!(assigned_id);
|
|
}
|
|
}
|
|
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());
|
|
}
|
|
|
|
#[test]
|
|
fn proxmox_os_label_requires_a_valid_hypervisor_observation() {
|
|
let mut snapshot = json!({
|
|
"os": "Debian",
|
|
"observations": [{"scope":"linux.hardware","quality":"valid",
|
|
"data":{"device_type":"hypervisor","proxmox_ve":true}}]
|
|
});
|
|
assert_eq!(os_label(&snapshot).as_deref(), Some("Proxmox VE"));
|
|
snapshot["observations"][0]["data"]["device_type"] = json!("virtual");
|
|
assert_eq!(os_label(&snapshot).as_deref(), Some("Debian"));
|
|
snapshot["observations"][0]["quality"] = json!("error");
|
|
assert_eq!(device_type(observed(&snapshot, "linux.hardware")), None);
|
|
assert_eq!(device_type(&json!({"device_type":"desktop"})), None);
|
|
}
|
|
|
|
#[test]
|
|
fn hetzner_placeholder_requires_a_valid_guest_and_yields_to_a_registered_host() {
|
|
let mut data = json!({"observations":[{"scope":"linux.hardware","quality":"valid",
|
|
"data":{"device_type":"virtual","readable_dmi":{"sys_vendor":" Hetzner "}}}]});
|
|
assert_eq!(hypervisor_display(&data, None)["kind"], "placeholder");
|
|
let host = Host { asset_id: Uuid::new_v4(), agent_id: Uuid::new_v4(), hostname: "pve01".into() };
|
|
assert_eq!(hypervisor_display(&data, Some(&host))["asset_id"], json!(host.asset_id));
|
|
data["observations"][0]["data"]["device_type"] = json!("physical");
|
|
assert!(hypervisor_display(&data, None).is_null());
|
|
data["observations"][0]["data"]["device_type"] = json!("virtual");
|
|
data["observations"][0]["quality"] = json!("error");
|
|
assert!(hypervisor_display(&data, None).is_null());
|
|
data["observations"][0] = json!({"scope":"windows.hardware","quality":"valid",
|
|
"data":{"device_type":"virtual","manufacturer":"HETZNER ONLINE GMBH"}});
|
|
assert_eq!(hypervisor_display(&data, None)["hostname"], "Hetzner Cloud");
|
|
data["observations"][0]["data"]["manufacturer"] = json!("Other company");
|
|
assert!(hypervisor_display(&data, None).is_null());
|
|
}
|
|
|
|
#[tokio::test]
|
|
#[ignore = "requires the disposable RLS database from tests/local_access_database.py"]
|
|
async fn host_links_resolve_outside_the_page_and_stay_inside_the_customer() {
|
|
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();
|
|
let other = Uuid::new_v4();
|
|
let mut ids = [Uuid::new_v4(), Uuid::new_v4()];
|
|
ids.sort_unstable();
|
|
let [vm, host] = ids;
|
|
let foreign_host = Uuid::new_v4();
|
|
for customer in [tenant, other] {
|
|
sqlx::query("INSERT INTO tenants(tenant_id,name) VALUES($1,'Hypervisor test')")
|
|
.bind(customer).execute(&admin).await.unwrap();
|
|
}
|
|
for (customer, id, name, kind) in [
|
|
(tenant, vm, "guest", "virtual"),
|
|
(tenant, host, "hypervisor-1", "hypervisor"),
|
|
(other, foreign_host, "hypervisor-2", "hypervisor"),
|
|
] {
|
|
sqlx::query("INSERT INTO assets(tenant_id,asset_id) VALUES($1,$2)")
|
|
.bind(customer).bind(id).execute(&admin).await.unwrap();
|
|
sqlx::query("INSERT INTO agent_installations(tenant_id,asset_id,agent_id,certificate_sha256,last_contact_at,last_authenticated_at,last_authenticated_certificate_sha256) VALUES($1,$2,$2,$3,now(),now(),$3)")
|
|
.bind(customer).bind(id).bind(rmm_domain::jobs::hash(format!("{customer}:{id}").as_bytes()))
|
|
.execute(&admin).await.unwrap();
|
|
let data = json!({"hostname":name,"os":"Ubuntu","root_available_bytes":10,"root_total_bytes":20,
|
|
"observations":[{"scope":"linux.hardware","quality":"valid",
|
|
"data":{"device_type":kind,"manufacturer":"Hetzner"}}]});
|
|
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(customer).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();
|
|
assert!(validate_vm_host_assignment(&mut tx, tenant, vm, vm, Some(foreign_host)).await.is_err());
|
|
assert!(validate_vm_host_assignment(&mut tx, tenant, vm, vm, Some(vm)).await.is_err());
|
|
validate_vm_host_assignment(&mut tx, tenant, vm, vm, Some(host)).await.unwrap();
|
|
|
|
sqlx::query("UPDATE assets SET hypervisor_asset_id=$2 WHERE asset_id=$1")
|
|
.bind(vm).bind(host).execute(&mut *tx).await.unwrap();
|
|
let q = Selection { tenant, asset: None, agent: None, view: Some(View::Current), status: None,
|
|
hostname: "", last: Uuid::nil(), as_of: Utc::now(), limit: 1 };
|
|
let page = q.rows(&mut tx).await.unwrap();
|
|
assert_eq!(page[0].get::<Uuid,_>("asset_id"), vm);
|
|
let hosts = assigned_hosts(&mut tx, tenant, q.as_of, &page).await.unwrap();
|
|
assert_eq!(project(&page[0], q.as_of, false, &hosts)["hypervisor"]["hostname"], "hypervisor-1");
|
|
let options = hypervisors(&mut tx, tenant, q.as_of, None, "HYPERVISOR", 51).await.unwrap();
|
|
assert_eq!(options.len(), 1);
|
|
assert_eq!(options[0].asset_id, host);
|
|
|
|
sqlx::query("UPDATE assets SET archived_at=now() WHERE asset_id=$1")
|
|
.bind(host).execute(&mut *tx).await.unwrap();
|
|
let hosts = assigned_hosts(&mut tx, tenant, q.as_of, &page).await.unwrap();
|
|
assert_eq!(project(&page[0], q.as_of, false, &hosts)["hypervisor"]["kind"], "placeholder");
|
|
|
|
let result = sqlx::query("UPDATE assets SET hypervisor_asset_id=$2 WHERE asset_id=$1")
|
|
.bind(vm).bind(foreign_host).execute(&mut *tx).await.unwrap_err();
|
|
assert_eq!(result.as_database_error().unwrap().code().as_deref(), Some("23503"));
|
|
tx.rollback().await.unwrap();
|
|
}
|
|
|
|
#[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, &HashMap::new());
|
|
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();
|
|
}
|
|
}
|