implement host metadata (out of date and last seen at)

Implements: https://todo.e3t.cc/~core/trifid/3
This commit is contained in:
core 2023-07-31 12:55:19 -04:00
parent b12df8aafe
commit 631d4d5280
Signed by: core
GPG Key ID: FDBF740DADDCEECF
5 changed files with 465 additions and 499 deletions

899
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -25,7 +25,7 @@ serde_yaml = "0.9.21" # Config / Serialization and deserialization
log = "0.4" # Logging log = "0.4" # Logging
simple_logger = "4" # Logging simple_logger = "4" # Logging
sea-orm = { version = "^0", features = [ "sqlx-postgres", "runtime-actix-rustls", "macros" ]} # Database sea-orm = { version = "0.12", features = [ "sqlx-postgres", "runtime-actix-rustls", "macros" ]} # Database
trifid_api_migration = { version = "0.1.0", path = "trifid_api_migration" } # Database trifid_api_migration = { version = "0.1.0", path = "trifid_api_migration" } # Database
trifid_api_entities = { version = "0.1.0", path = "trifid_api_entities" } # Database trifid_api_entities = { version = "0.1.0", path = "trifid_api_entities" } # Database

View File

@ -24,7 +24,6 @@ use sea_orm::{ConnectOptions, Database, DatabaseConnection};
use std::error::Error; use std::error::Error;
use std::time::Duration; use std::time::Duration;
use actix_cors::Cors; use actix_cors::Cors;
use crate::config::CONFIG; use crate::config::CONFIG;
use crate::error::{APIError, APIErrorsResponse}; use crate::error::{APIError, APIErrorsResponse};
use crate::keystore::keystore_init; use crate::keystore::keystore_init;

View File

@ -13,8 +13,14 @@ use dnapi_rs::message::{
use ed25519_dalek::{Signature, Signer, Verifier, VerifyingKey}; use ed25519_dalek::{Signature, Signer, Verifier, VerifyingKey};
use log::{error, warn}; use log::{error, warn};
use std::clone::Clone; use std::clone::Clone;
use std::time::{SystemTime, UNIX_EPOCH};
use sea_orm::{ActiveModelTrait, EntityTrait};
use trifid_pki::cert::{deserialize_ed25519_public, deserialize_x25519_public}; use trifid_pki::cert::{deserialize_ed25519_public, deserialize_x25519_public};
use trifid_pki::x25519_dalek::PublicKey; use trifid_pki::x25519_dalek::PublicKey;
use trifid_api_entities::entity::host;
use crate::error::APIErrorsResponse;
use sea_orm::{ColumnTrait, QueryFilter, IntoActiveModel};
use sea_orm::ActiveValue::Set;
#[post("/v1/dnclient")] #[post("/v1/dnclient")]
pub async fn dnclient( pub async fn dnclient(
@ -134,6 +140,42 @@ pub async fn dnclient(
} }
}; };
let host_model = match host::Entity::find()
.filter(host::Column::Id.eq(host))
.one(&db.conn)
.await
{
Ok(h) => h,
Err(e) => {
error!("Database error: {}", e);
return HttpResponse::InternalServerError().json(APIErrorsResponse {
errors: vec![crate::error::APIError {
code: "ERR_DB_ERROR".to_string(),
message: "There was an error with the database query. Please try again later."
.to_string(),
path: None,
}],
});
}
};
let host_model = match host_model {
Some(h) => h,
None => {
return HttpResponse::NotFound().json(APIErrorsResponse {
errors: vec![crate::error::APIError {
code: "ERR_NOT_FOUND".to_string(),
message:
"resource not found"
.to_string(),
path: None,
}],
});
}
};
let mut host_am = host_model.into_active_model();
// Do a config build // Do a config build
let info = match collect_info(&db, host, client_keys.dh_pub.as_bytes()).await { let info = match collect_info(&db, host, client_keys.dh_pub.as_bytes()).await {
@ -172,6 +214,24 @@ pub async fn dnclient(
let config_update_avail = current_cfg.map(|u| u.config.clone()) != Some(cfg.clone()) let config_update_avail = current_cfg.map(|u| u.config.clone()) != Some(cfg.clone())
|| req.counter < host_in_ks.current_config as u32; || req.counter < host_in_ks.current_config as u32;
host_am.last_out_of_date = Set(config_update_avail);
host_am.last_seen_at = Set(SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() as i64);
match host_am.update(&db.conn).await {
Ok(_) => (),
Err(e) => {
error!("Database error: {}", e);
return HttpResponse::InternalServerError().json(APIErrorsResponse {
errors: vec![crate::error::APIError {
code: "ERR_DB_ERROR".to_string(),
message: "There was an error with the database query. Please try again later."
.to_string(),
path: None,
}],
});
}
}
return match req_w.message_type.as_str() { return match req_w.message_type.as_str() {
"CheckForUpdate" => { "CheckForUpdate" => {
// value ignored here // value ignored here

View File

@ -11,4 +11,4 @@ repository = "https://git.e3t.cc/~core/trifid"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
sea-orm = { version = "^0" } sea-orm = { version = "0.12" }