so uh didnt know that,

This commit is contained in:
lda
2026-05-03 05:03:40 +07:00 Verified
parent 695c64cf8e
commit 155458791e
22 changed files with 302 additions and 50 deletions
@@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "DELETE FROM agent_devices WHERE agent_id = ?1 AND device_key = ?2",
"describe": {
"columns": [],
"parameters": {
"Right": 2
},
"nullable": []
},
"hash": "09db61a070a351a6255e21d5949285ccb19fca7f8f29a6c18dddac5495257a45"
}
@@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "DELETE FROM agent_device_hostnames WHERE agent_id = ?1 AND device_key = ?2",
"describe": {
"columns": [],
"parameters": {
"Right": 2
},
"nullable": []
},
"hash": "2cd47bbce9b9ae2ebf39f31b12722bb8d19ef6d90ac634731932a4cefbde03b7"
}
@@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "INSERT INTO agent_device_macs (agent_id, device_key, mac) VALUES (?1, ?2, ?3)",
"describe": {
"columns": [],
"parameters": {
"Right": 3
},
"nullable": []
},
"hash": "47a9d2f464cc766b5bc83f8ef05cb34b2c0f3d5c1a7770b13f70b60deb76bbbf"
}
@@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "DELETE FROM agent_device_macs WHERE agent_id = ?1 AND device_key = ?2",
"describe": {
"columns": [],
"parameters": {
"Right": 2
},
"nullable": []
},
"hash": "4f4329b0a47a00a3b881a1bc5cf8bb9855351714ff827ccff917b3e623191504"
}
@@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "DELETE FROM agent_device_facts WHERE agent_id = ?1 AND device_key = ?2",
"describe": {
"columns": [],
"parameters": {
"Right": 2
},
"nullable": []
},
"hash": "55340d50a29e0a2d324ed946b6fa6503fbd4ea0674bb7ec24e3426c5b0e5832f"
}
@@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "INSERT INTO agent_device_hostnames (agent_id, device_key, hostname) VALUES (?1, ?2, ?3)",
"describe": {
"columns": [],
"parameters": {
"Right": 3
},
"nullable": []
},
"hash": "652cd104ad380746235ab932b00decfce78be6756f39f4ff3787a0cac9df7554"
}
@@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "INSERT INTO agent_device_ips (agent_id, device_key, ip) VALUES (?1, ?2, ?3)",
"describe": {
"columns": [],
"parameters": {
"Right": 3
},
"nullable": []
},
"hash": "97aef0a2b276a9ba64840f10b7fb15de5ecd12cb3aa709043a9e03da193e6a5b"
}
@@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "INSERT INTO agent_devices (agent_id, device_key, presence, display_name, first_seen_unix, last_seen_unix)\n VALUES (?1, ?2, ?3, ?4, ?5, ?6)\n ON CONFLICT (agent_id, device_key) DO UPDATE SET\n presence = excluded.presence,\n last_seen_unix = excluded.last_seen_unix",
"describe": {
"columns": [],
"parameters": {
"Right": 6
},
"nullable": []
},
"hash": "990e14f1dd4093b75780e0177bba88c04379776f539f4d2823bee16aec5990f3"
}
@@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "DELETE FROM agent_device_ips WHERE agent_id = ?1 AND device_key = ?2",
"describe": {
"columns": [],
"parameters": {
"Right": 2
},
"nullable": []
},
"hash": "d442949dce7a4b9a8af97dc34fc34608029564740ad327c525c7f1466bc18a11"
}
@@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "INSERT INTO agent_device_facts (agent_id, device_key, fact_json) VALUES (?1, ?2, ?3)",
"describe": {
"columns": [],
"parameters": {
"Right": 3
},
"nullable": []
},
"hash": "dd591c18060a1b6432eecacb1d1b6fb1fc518a4a7c134c11b60d42dc3c1b179d"
}
@@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "DELETE FROM agent_devices WHERE agent_id = ?1 AND device_key = ?2",
"describe": {
"columns": [],
"parameters": {
"Right": 2
},
"nullable": []
},
"hash": "09db61a070a351a6255e21d5949285ccb19fca7f8f29a6c18dddac5495257a45"
}
@@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "DELETE FROM agent_device_hostnames WHERE agent_id = ?1 AND device_key = ?2",
"describe": {
"columns": [],
"parameters": {
"Right": 2
},
"nullable": []
},
"hash": "2cd47bbce9b9ae2ebf39f31b12722bb8d19ef6d90ac634731932a4cefbde03b7"
}
@@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "INSERT INTO agent_device_macs (agent_id, device_key, mac) VALUES (?1, ?2, ?3)",
"describe": {
"columns": [],
"parameters": {
"Right": 3
},
"nullable": []
},
"hash": "47a9d2f464cc766b5bc83f8ef05cb34b2c0f3d5c1a7770b13f70b60deb76bbbf"
}
@@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "DELETE FROM agent_device_macs WHERE agent_id = ?1 AND device_key = ?2",
"describe": {
"columns": [],
"parameters": {
"Right": 2
},
"nullable": []
},
"hash": "4f4329b0a47a00a3b881a1bc5cf8bb9855351714ff827ccff917b3e623191504"
}
@@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "DELETE FROM agent_device_facts WHERE agent_id = ?1 AND device_key = ?2",
"describe": {
"columns": [],
"parameters": {
"Right": 2
},
"nullable": []
},
"hash": "55340d50a29e0a2d324ed946b6fa6503fbd4ea0674bb7ec24e3426c5b0e5832f"
}
@@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "INSERT INTO agent_device_hostnames (agent_id, device_key, hostname) VALUES (?1, ?2, ?3)",
"describe": {
"columns": [],
"parameters": {
"Right": 3
},
"nullable": []
},
"hash": "652cd104ad380746235ab932b00decfce78be6756f39f4ff3787a0cac9df7554"
}
@@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "INSERT INTO agent_device_ips (agent_id, device_key, ip) VALUES (?1, ?2, ?3)",
"describe": {
"columns": [],
"parameters": {
"Right": 3
},
"nullable": []
},
"hash": "97aef0a2b276a9ba64840f10b7fb15de5ecd12cb3aa709043a9e03da193e6a5b"
}
@@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "INSERT INTO agent_devices (agent_id, device_key, presence, display_name, first_seen_unix, last_seen_unix)\n VALUES (?1, ?2, ?3, ?4, ?5, ?6)\n ON CONFLICT (agent_id, device_key) DO UPDATE SET\n presence = excluded.presence,\n last_seen_unix = excluded.last_seen_unix",
"describe": {
"columns": [],
"parameters": {
"Right": 6
},
"nullable": []
},
"hash": "990e14f1dd4093b75780e0177bba88c04379776f539f4d2823bee16aec5990f3"
}
@@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "DELETE FROM agent_device_ips WHERE agent_id = ?1 AND device_key = ?2",
"describe": {
"columns": [],
"parameters": {
"Right": 2
},
"nullable": []
},
"hash": "d442949dce7a4b9a8af97dc34fc34608029564740ad327c525c7f1466bc18a11"
}
@@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "INSERT INTO agent_device_facts (agent_id, device_key, fact_json) VALUES (?1, ?2, ?3)",
"describe": {
"columns": [],
"parameters": {
"Right": 3
},
"nullable": []
},
"hash": "dd591c18060a1b6432eecacb1d1b6fb1fc518a4a7c134c11b60d42dc3c1b179d"
}
@@ -11,9 +11,13 @@ use wakey_core::parse::mac;
#[derive(Debug, Default, Deserialize)] #[derive(Debug, Default, Deserialize)]
pub struct ListFleetDevicesQuery { pub struct ListFleetDevicesQuery {
pub query: Option<String>, pub query: Option<String>,
/// "all" or Presence variants
pub presence: Option<String>, pub presence: Option<String>,
/// "known", "unknown", "all"
pub known: Option<String>, pub known: Option<String>,
/// "all" or specific agent id
pub agent_id: Option<String>, pub agent_id: Option<String>,
/// "operator" or "all"
pub visibility: Option<String>, pub visibility: Option<String>,
pub limit: Option<usize>, pub limit: Option<usize>,
} }
@@ -42,101 +42,107 @@ impl Store {
let presence = presence_to_str(device.presence); let presence = presence_to_str(device.presence);
let display_name: Option<String> = None; let display_name: Option<String> = None;
sqlx::query( sqlx::query!(
"INSERT INTO agent_devices (agent_id, device_key, presence, display_name, first_seen_unix, last_seen_unix) "INSERT INTO agent_devices (agent_id, device_key, presence, display_name, first_seen_unix, last_seen_unix)
VALUES (?1, ?2, ?3, ?4, ?5, ?6) VALUES (?1, ?2, ?3, ?4, ?5, ?6)
ON CONFLICT (agent_id, device_key) DO UPDATE SET ON CONFLICT (agent_id, device_key) DO UPDATE SET
presence = excluded.presence, presence = excluded.presence,
last_seen_unix = excluded.last_seen_unix", last_seen_unix = excluded.last_seen_unix",
agent_id,
device_key,
presence,
display_name,
snapshot_time_i64,
snapshot_time_i64
) )
.bind(agent_id)
.bind(&device_key)
.bind(presence)
.bind(&display_name)
.bind(snapshot_time_i64)
.bind(snapshot_time_i64)
.execute(&mut *tx) .execute(&mut *tx)
.await .await
.context("failed upserting agent device")?; .context("failed upserting agent device")?;
// Replace child rows. // Replace child rows.
sqlx::query("DELETE FROM agent_device_macs WHERE agent_id = ?1 AND device_key = ?2") sqlx::query!(
.bind(agent_id) "DELETE FROM agent_device_macs WHERE agent_id = ?1 AND device_key = ?2",
.bind(&device_key) agent_id,
.execute(&mut *tx) device_key
.await )
.context("failed deleting device macs")?; .execute(&mut *tx)
.await
.context("failed deleting device macs")?;
for mac in &device.macs { for mac in &device.macs {
let mac_str = mac.to_string().to_ascii_lowercase(); let mac_str = mac.to_string().to_ascii_lowercase();
sqlx::query( sqlx::query!(
"INSERT INTO agent_device_macs (agent_id, device_key, mac) VALUES (?1, ?2, ?3)", "INSERT INTO agent_device_macs (agent_id, device_key, mac) VALUES (?1, ?2, ?3)",
agent_id,
device_key,
mac_str
) )
.bind(agent_id)
.bind(&device_key)
.bind(&mac_str)
.execute(&mut *tx) .execute(&mut *tx)
.await .await
.context("failed inserting device mac")?; .context("failed inserting device mac")?;
} }
sqlx::query("DELETE FROM agent_device_ips WHERE agent_id = ?1 AND device_key = ?2") sqlx::query!(
.bind(agent_id) "DELETE FROM agent_device_ips WHERE agent_id = ?1 AND device_key = ?2",
.bind(&device_key) agent_id,
.execute(&mut *tx) device_key
.await )
.context("failed deleting device ips")?; .execute(&mut *tx)
.await
.context("failed deleting device ips")?;
for ip in &device.ips { for ip in &device.ips {
let ip_str = ip.to_string(); let ip_str = ip.to_string();
sqlx::query( sqlx::query!(
"INSERT INTO agent_device_ips (agent_id, device_key, ip) VALUES (?1, ?2, ?3)", "INSERT INTO agent_device_ips (agent_id, device_key, ip) VALUES (?1, ?2, ?3)",
agent_id,
device_key,
ip_str
) )
.bind(agent_id)
.bind(&device_key)
.bind(&ip_str)
.execute(&mut *tx) .execute(&mut *tx)
.await .await
.context("failed inserting device ip")?; .context("failed inserting device ip")?;
} }
sqlx::query( sqlx::query!(
"DELETE FROM agent_device_hostnames WHERE agent_id = ?1 AND device_key = ?2", "DELETE FROM agent_device_hostnames WHERE agent_id = ?1 AND device_key = ?2",
agent_id,
device_key
) )
.bind(agent_id)
.bind(&device_key)
.execute(&mut *tx) .execute(&mut *tx)
.await .await
.context("failed deleting device hostnames")?; .context("failed deleting device hostnames")?;
for hostname in &device.names { for hostname in &device.names {
sqlx::query( sqlx::query!(
"INSERT INTO agent_device_hostnames (agent_id, device_key, hostname) VALUES (?1, ?2, ?3)", "INSERT INTO agent_device_hostnames (agent_id, device_key, hostname) VALUES (?1, ?2, ?3)",
agent_id,
device_key,
hostname
) )
.bind(agent_id)
.bind(&device_key)
.bind(hostname)
.execute(&mut *tx) .execute(&mut *tx)
.await .await
.context("failed inserting device hostname")?; .context("failed inserting device hostname")?;
} }
sqlx::query("DELETE FROM agent_device_facts WHERE agent_id = ?1 AND device_key = ?2") sqlx::query!(
.bind(agent_id) "DELETE FROM agent_device_facts WHERE agent_id = ?1 AND device_key = ?2",
.bind(&device_key) agent_id,
.execute(&mut *tx) device_key
.await )
.context("failed deleting device facts")?; .execute(&mut *tx)
.await
.context("failed deleting device facts")?;
for observation in &device.observations { for observation in &device.observations {
let fact_json = let fact_json =
serde_json::to_string(observation).context("failed serializing fact")?; serde_json::to_string(observation).context("failed serializing fact")?;
sqlx::query( sqlx::query!(
"INSERT INTO agent_device_facts (agent_id, device_key, fact_json) VALUES (?1, ?2, ?3)", "INSERT INTO agent_device_facts (agent_id, device_key, fact_json) VALUES (?1, ?2, ?3)",
agent_id,
device_key,
fact_json
) )
.bind(agent_id)
.bind(&device_key)
.bind(&fact_json)
.execute(&mut *tx) .execute(&mut *tx)
.await .await
.context("failed inserting device fact")?; .context("failed inserting device fact")?;
@@ -145,12 +151,14 @@ impl Store {
for old_key in existing_keys { for old_key in existing_keys {
if !incoming_keys.contains(&old_key) { if !incoming_keys.contains(&old_key) {
sqlx::query("DELETE FROM agent_devices WHERE agent_id = ?1 AND device_key = ?2") sqlx::query!(
.bind(agent_id) "DELETE FROM agent_devices WHERE agent_id = ?1 AND device_key = ?2",
.bind(&old_key) agent_id,
.execute(&mut *tx) old_key
.await )
.context("failed pruning old agent device")?; .execute(&mut *tx)
.await
.context("failed pruning old agent device")?;
} }
} }