Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = "MIT OR Apache-2.0"
name = "omnect-ui"
readme = "README.md"
repository = "git@github.com:omnect/omnect-ui.git"
version = "0.13.3"
version = "0.14.0"
build = "src/build.rs"

[dependencies]
Expand Down Expand Up @@ -50,6 +50,7 @@ rustls = { version = "0.23", default-features = false, features = [
rustls-pemfile = { version = "2.2", default-features = false, features = [
"std",
] }
semver = { version = "1.0", default-features = false }
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_json = { version = "1.0", default-features = false, features = [
"raw_value",
Expand Down
15 changes: 14 additions & 1 deletion src/api.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::common::VERSION_CHECK;
use crate::common::{config_path, validate_password, validate_token_and_claims};
use crate::middleware::TOKEN_EXPIRE_HOURS;
use crate::socket_client::*;
Expand Down Expand Up @@ -113,7 +114,19 @@ impl Api {

pub async fn healthcheck() -> impl Responder {
debug!("healthcheck() called");
HttpResponse::Ok().finish()

let info = VERSION_CHECK.lock().unwrap().clone();

match info {
Some(result) => {
if result.is_below_min {
HttpResponse::InternalServerError().json(result)
} else {
HttpResponse::Ok().json(result)
}
}
None => HttpResponse::Ok().body("No version check performed yet"),
}
Comment on lines 117 to 129
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

healthcheck must be called in another PR during startup and handle the result

}

pub async fn factory_reset(
Expand Down
49 changes: 48 additions & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
use crate::MIN_ODS_VERSION;
use actix_web::body::MessageBody;
use anyhow::{anyhow, bail, Context, Result};
use argon2::{Argon2, PasswordHash, PasswordVerifier};
use base64::{prelude::BASE64_STANDARD, Engine};
use jwt_simple::prelude::{RS256PublicKey, RSAPublicKeyLike};
use reqwest::blocking::get;
use semver::Version;
use serde::{Deserialize, Serialize};
use std::{fs, io::Write, path::Path};
use std::{
fs,
io::Write,
path::Path,
sync::{LazyLock, Mutex},
};

#[derive(Clone, Debug, Serialize)]
pub struct VersionCheckResult {
pub min_version: String,
pub current_version: String,
pub is_below_min: bool,
}

pub static VERSION_CHECK: LazyLock<Mutex<Option<VersionCheckResult>>> =
LazyLock::new(|| Mutex::new(None));

#[derive(Deserialize)]
pub struct RealmInfo {
Expand All @@ -30,6 +47,7 @@ pub struct StatusResponse {
#[derive(Deserialize)]
pub struct SystemInfo {
pub fleet_id: Option<String>,
pub omnect_device_service_version: Option<String>,
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -199,3 +217,32 @@ pub fn create_frontend_config_file(keycloak_url: &str) -> Result<()> {

Ok(())
}

pub async fn check_and_store_ods_version(ods_socket_path: &str) -> Result<()> {
let status_response = get_status(ods_socket_path)
.await
.context("Failed to get status from socket client")?;

let Some(omnect_device_service_version) =
&status_response.system_info.omnect_device_service_version
else {
bail!("failed to get omnect_device_service_version from status response")
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We expect the response of the ODS to contain omnect_device_service_version in any case and error out if it is not set. IMHO we don't set the type as Option then.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


let ods_version = omnect_device_service_version.clone();

// compare to MIN_ODS_VERSION
let min_version = Version::parse(MIN_ODS_VERSION).expect("parse MIN_ODS_VERSION");
let current_version = Version::parse(&ods_version).expect("parse ods_version");
let is_below_min = current_version < min_version;
{
let mut version_check = VERSION_CHECK.lock().unwrap();
*version_check = Some(VersionCheckResult {
min_version: MIN_ODS_VERSION.to_string(),
current_version: ods_version.clone(),
is_below_min,
});
}

Ok(())
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. we should get rid of expect and unwrap
  2. OnceLock would be better as LazyLock. You could move the code into get_or_init function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactored to used OnceLock and removed unnecessary Mutex handling.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ use tokio::{
};
use uuid::Uuid;

pub const MIN_ODS_VERSION: &str = "0.39.0";

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use VersionReq instead, I'd adjust the name so that it is REQ_ODS_VERSION

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

const UPLOAD_LIMIT_BYTES: usize = 250 * 1024 * 1024;
const MEMORY_LIMIT_BYTES: usize = 10 * 1024 * 1024;

Expand Down Expand Up @@ -120,11 +122,16 @@ async fn main() {
.parse::<u64>()
.expect("UI_PORT format");

let ods_socket_path = std::env::var("SOCKET_PATH").expect("env SOCKET_PATH is missing");
common::check_and_store_ods_version(&ods_socket_path)
.await
.expect("failed to check and store ods version");

CryptoProvider::install_default(default_provider()).expect("failed to install crypto provider");

certificate::create_module_certificate(&cert_path!(), &key_path!())
.await
.expect("Failed to create module certificate");
.expect("failed to create module certificate");

let mut tls_certs =
std::io::BufReader::new(std::fs::File::open(cert_path!()).expect("read certs_file"));
Expand Down
Loading