|
|
|
@@ -14,6 +14,7 @@ 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)]
|
|
|
|
@@ -121,10 +122,7 @@ pub async fn list(
|
|
|
|
|
};
|
|
|
|
|
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 hostname = hostname_filter(q.q.as_deref())?;
|
|
|
|
|
let binding = json!([tenant, tenants, q.view, q.status, hostname]);
|
|
|
|
|
let scope = format!(
|
|
|
|
|
"device-fleet:{}",
|
|
|
|
@@ -180,6 +178,7 @@ pub async fn list(
|
|
|
|
|
}
|
|
|
|
|
.rows(&mut tx)
|
|
|
|
|
.await?;
|
|
|
|
|
let hosts = assigned_hosts(&mut tx, customer, page.as_of, &rows).await?;
|
|
|
|
|
tx.commit().await?;
|
|
|
|
|
for row in rows {
|
|
|
|
|
if items.len() == capacity {
|
|
|
|
@@ -187,7 +186,7 @@ pub async fn list(
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
last = Some((customer, row.get::<Uuid, _>("agent_id")));
|
|
|
|
|
items.push(project(&row, page.as_of, tenant.is_some()));
|
|
|
|
|
items.push(project(&row, page.as_of, tenant.is_some(), &hosts));
|
|
|
|
|
}
|
|
|
|
|
if truncated {
|
|
|
|
|
break;
|
|
|
|
@@ -236,7 +235,8 @@ pub async fn detail(
|
|
|
|
|
.await?
|
|
|
|
|
.pop()
|
|
|
|
|
.ok_or(Error(StatusCode::NOT_FOUND, "NOT_FOUND"))?;
|
|
|
|
|
let mut item = project(&row, now, true);
|
|
|
|
|
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?
|
|
|
|
@@ -294,6 +294,135 @@ 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)
|
|
|
|
@@ -348,7 +477,7 @@ fn os_label(data: &Value) -> Option<String> {
|
|
|
|
|
Some("Linux".to_owned())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fn project(row: &PgRow, now: DateTime<Utc>, full: bool) -> Value {
|
|
|
|
|
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");
|
|
|
|
@@ -365,8 +494,7 @@ fn project(row: &PgRow, now: DateTime<Utc>, full: bool) -> Value {
|
|
|
|
|
.as_u64()
|
|
|
|
|
.or_else(|| linux_memory["total_bytes"].as_u64())
|
|
|
|
|
.or_else(|| windows["memory_total_bytes"].as_u64());
|
|
|
|
|
let device_type =
|
|
|
|
|
device_type(observed(&data, "linux.hardware")).or_else(|| device_type(windows));
|
|
|
|
|
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())) {
|
|
|
|
@@ -397,6 +525,14 @@ fn project(row: &PgRow, now: DateTime<Utc>, full: bool) -> Value {
|
|
|
|
|
"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");
|
|
|
|
@@ -457,6 +593,87 @@ mod tests {
|
|
|
|
|
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) VALUES($1,$2,$2,$3,now(),now())")
|
|
|
|
|
.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() {
|
|
|
|
@@ -499,7 +716,7 @@ mod tests {
|
|
|
|
|
limit: 1,
|
|
|
|
|
};
|
|
|
|
|
let row = q.rows(&mut tx).await.unwrap().pop().unwrap();
|
|
|
|
|
let summary = project(&row, q.as_of, false);
|
|
|
|
|
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");
|
|
|
|
|