Skip to content

Commit

Permalink
refactor(agent): follow up to PR #889
Browse files Browse the repository at this point in the history
  • Loading branch information
CBenoit committed Jun 21, 2024
1 parent 92f86bf commit 2c2946b
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 26 deletions.
4 changes: 2 additions & 2 deletions crates/devolutions-agent-shared/src/update_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ mod tests {
("latest", VersionSpecification::Latest),
];

for (serialized, deserizlized) in cases {
for (serialized, deserialized) in cases {
let parsed = serde_json::from_str::<VersionSpecification>(&format!("\"{}\"", serialized)).unwrap();
assert_eq!(parsed, *deserizlized);
assert_eq!(parsed, *deserialized);

let reserialized = serde_json::to_string(&parsed).unwrap();
assert_eq!(reserialized, format!("\"{}\"", serialized));
Expand Down
14 changes: 7 additions & 7 deletions devolutions-agent/src/updater/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tokio::{fs::File, io::AsyncWriteExt};
use crate::updater::UpdaterError;

/// Download binary file to memory
pub async fn download_bytes(url: &str) -> Result<Vec<u8>, UpdaterError> {
pub async fn download_binary(url: &str) -> Result<Vec<u8>, UpdaterError> {
info!(%url, "Downloading file from network...");

let body = reqwest::get(url)
Expand All @@ -22,8 +22,8 @@ pub async fn download_bytes(url: &str) -> Result<Vec<u8>, UpdaterError> {
}

/// Download UTF-8 file to memory
pub async fn download_text(url: &str) -> Result<String, UpdaterError> {
let bytes = download_bytes(url).await?;
pub async fn download_utf8(url: &str) -> Result<String, UpdaterError> {
let bytes = download_binary(url).await?;
String::from_utf8(bytes).map_err(|_| UpdaterError::Utf8)
}

Expand All @@ -50,11 +50,11 @@ pub async fn save_to_temp_file(data: &[u8], extension: Option<&str>) -> Result<U

/// Mark file to be removed on next reboot.
pub fn remove_file_on_reboot(file_path: &Utf8Path) -> Result<(), UpdaterError> {
_impl_remove_file_on_reboot(file_path)
remove_file_on_reboot_impl(file_path)
}

#[cfg(windows)]
pub fn _impl_remove_file_on_reboot(file_path: &Utf8Path) -> Result<(), UpdaterError> {
pub fn remove_file_on_reboot_impl(file_path: &Utf8Path) -> Result<(), UpdaterError> {
use windows::core::HSTRING;
use windows::Win32::Storage::FileSystem::{MoveFileExW, MOVEFILE_DELAY_UNTIL_REBOOT};

Expand All @@ -70,7 +70,7 @@ pub fn _impl_remove_file_on_reboot(file_path: &Utf8Path) -> Result<(), UpdaterEr
}

#[cfg(not(windows))]
pub fn _impl_remove_file_on_reboot(_file_path: &Utf8Path) -> Result<(), UpdaterError> {
// NOTE: On UNIX-like platforms /tmp filder is used which is cleared by OS automatically.
pub fn impl_remove_file_on_reboot_impl(_file_path: &Utf8Path) -> Result<(), UpdaterError> {
// NOTE: On UNIX-like platforms /tmp folder is used which is cleared by OS automatically.
Ok(())
}
30 changes: 15 additions & 15 deletions devolutions-agent/src/updater/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use devolutions_gateway_task::{ShutdownSignal, Task};
use crate::config::ConfHandle;

use integrity::validate_artifact_hash;
use io::{download_bytes, download_text, save_to_temp_file};
use io::{download_binary, download_text, save_to_temp_file};
use package::{install_package, validate_package};
use productinfo::DEVOLUTIONS_PRODUCTINFO_URL;
use security::set_file_dacl;
Expand Down Expand Up @@ -83,12 +83,12 @@ impl Task for UpdaterTask {
error!(%err, "Failed to watch update.json file");
}
})
.context("Failed to create file notify debouncer")?;
.context("failed to create file notify debouncer")?;

notify_debouncer
.watcher()
.watch(update_file_path.as_std_path(), RecursiveMode::NonRecursive)
.context("Failed to start update file watcher")?;
.context("failed to start update file watcher")?;

// Trigger initial check during task startup
file_change_notification.notify_waiters();
Expand Down Expand Up @@ -149,19 +149,19 @@ async fn update_product(conf: ConfHandle, product: Product, order: UpdateOrder)
let target_version = order.target_version;
let hash = order.hash;

let package_data = download_bytes(&order.package_url)
let package_data = download_binary(&order.package_url)
.await
.with_context(|| format!("Failed to download package file for `{product}`"))?;
.with_context(|| format!("failed to download package file for `{product}`"))?;

let package_path = save_to_temp_file(&package_data, Some(product.get_package_extension())).await?;

info!(%product, %target_version, %package_path, "Downloaded product Installer");

let ctx = UpdaterCtx { product, conf };

validate_artifact_hash(&ctx, &package_data, &hash).context("Failed to validate package file integrity")?;
validate_artifact_hash(&ctx, &package_data, &hash).context("failed to validate package file integrity")?;

validate_package(&ctx, &package_path).context("Failed to validate package contents")?;
validate_package(&ctx, &package_path).context("failed to validate package contents")?;

if ctx.conf.get_conf().debug.skip_msi_install {
warn!(%product, "DEBUG MODE: Skipping package installation due to debug configuration");
Expand All @@ -180,9 +180,9 @@ async fn update_product(conf: ConfHandle, product: Product, order: UpdateOrder)
async fn read_update_json(update_file_path: &Utf8Path) -> anyhow::Result<UpdateJson> {
let update_json_data = tokio::fs::read(update_file_path)
.await
.context("Failed to read update.json file")?;
.context("failed to read update.json file")?;
let update_json: UpdateJson =
serde_json::from_slice(&update_json_data).context("Failed to parse update.json file")?;
serde_json::from_slice(&update_json_data).context("failed to parse update.json file")?;

Ok(update_json)
}
Expand Down Expand Up @@ -221,15 +221,15 @@ async fn check_for_updates(product: Product, update_json: &UpdateJson) -> anyhow

info!(%product, %target_version, "Ready to update the product");

let product_info_db = download_text(DEVOLUTIONS_PRODUCTINFO_URL)
let product_info_db = download_utf8(DEVOLUTIONS_PRODUCTINFO_URL)
.await
.context("Failed to download productinfo database")?;
.context("failed to download productinfo database")?;

let product_info_db: productinfo::ProductInfoDb = product_info_db.parse()?;

let product_info = product_info_db
.get(product.get_productinfo_id())
.ok_or_else(|| anyhow!("Product `{product}` info not found in remote database"))?;
.ok_or_else(|| anyhow!("product `{product}` info not found in remote database"))?;

let remote_version = product_info.version.parse::<DateVersion>()?;

Expand Down Expand Up @@ -259,11 +259,11 @@ async fn init_update_json() -> anyhow::Result<Utf8PathBuf> {
let update_file_path = get_updater_file_path();

let default_update_json =
serde_json::to_string_pretty(&UpdateJson::default()).context("Failed to serialize default update.json")?;
serde_json::to_string_pretty(&UpdateJson::default()).context("failed to serialize default update.json")?;

fs::write(&update_file_path, default_update_json)
.await
.context("Failed to write default update.json file")?;
.context("failed to write default update.json file")?;

// Set permissions for update.json file:
match set_file_dacl(&update_file_path, security::UPDATE_JSON_DACL) {
Expand All @@ -276,7 +276,7 @@ async fn init_update_json() -> anyhow::Result<Utf8PathBuf> {
.unwrap_or_else(|err| warn!(%err, "Failed to remove update.json file after failed permissions set"));

// Treat as fatal error
return Err(anyhow!(err).context("Failed to set update.json file permissions"));
return Err(anyhow!(err).context("failed to set update.json file permissions"));
}
}

Expand Down
2 changes: 1 addition & 1 deletion devolutions-agent/src/updater/productinfo/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ mod tests {

#[test]
fn test_productinfo_parse() {
let input = include_str!("test_asset_db");
let input = include_str!("../../../test_asset_db");
let db: ProductInfoDb = input.parse().unwrap();

assert_eq!(db.get("Gatewaybin").unwrap().version, "2024.2.1.0");
Expand Down
2 changes: 1 addition & 1 deletion devolutions-agent/src/updater/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub fn reversed_hex_to_uuid(mut hex: &str) -> Result<String, UpdaterError> {

formatted.push('{');

// Hypen pattern is not same as reversing pattern blocks
// Hyphen pattern is not same as reversing pattern blocks.
const HYPEN_PATTERN: &[usize] = &[1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0];

for (pattern, hypen) in UUID_REVERSING_PATTERN.iter().zip(HYPEN_PATTERN) {
Expand Down
File renamed without changes.

0 comments on commit 2c2946b

Please sign in to comment.