From d631c592f8fb53742b5798a49425dcfeb6797286 Mon Sep 17 00:00:00 2001 From: Dongsu Park Date: Fri, 17 May 2024 11:14:43 +0200 Subject: [PATCH 1/2] split out mount/OVF logic into get_environment As mount/OVF logic should be separate from get_username(), split it out into a new function get_environment(). --- src/main.rs | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index 40ab4014..dd2e26c5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,8 +36,25 @@ fn mount_parse_ovf_env(dev: String) -> Result { Ok(environment) } +fn get_environment() -> Result { + let ovf_devices = media::get_mount_device()?; + let mut environment: Option = None; + + // loop until it finds a correct device. + for dev in ovf_devices { + environment = match mount_parse_ovf_env(dev) { + Ok(env) => Some(env), + Err(_) => continue, + } + } + + environment + .ok_or_else(|| anyhow::anyhow!("Unable to get list of block devices")) +} + fn get_username( instance_metadata: &InstanceMetadata, + environment: &Environment, ) -> Result { if instance_metadata .compute @@ -49,22 +66,8 @@ fn get_username( } else { // password authentication is enabled - // list of CDROM devices that is available with possible filesystems. - let ovf_devices = media::get_mount_device()?; - let mut environment: Option = None; - - // loop until it finds a correct device. - for dev in ovf_devices { - environment = match mount_parse_ovf_env(dev) { - Ok(env) => Some(env), - Err(_) => continue, - } - } - Ok(environment - .ok_or_else(|| { - anyhow::anyhow!("Unable to get list of block devices") - })? + .clone() .provisioning_section .linux_prov_conf_set .username) @@ -103,7 +106,7 @@ async fn provision() -> Result<(), anyhow::Error> { .build()?; let instance_metadata = imds::query(&client).await?; - let username = get_username(&instance_metadata)?; + let username = get_username(&instance_metadata, &get_environment()?)?; let mut file_path = "/home/".to_string(); file_path.push_str(username.as_str()); From a5319efcef2ea8b12ad49edcae313d42a6754627 Mon Sep 17 00:00:00 2001 From: Dongsu Park Date: Thu, 30 May 2024 12:07:20 +0200 Subject: [PATCH 2/2] move mount_parse_ovf_env to libazureinit/media As mount_parse_ovf_env is a helper for media module, it makes sense to move the function to libazureinit/media. To use inspect_err function, stable since 1.76, specify the minimum Rust version of libazureinit to 1.76.0. --- libazureinit/Cargo.toml | 2 ++ libazureinit/src/media.rs | 20 ++++++++++++++++++++ src/main.rs | 23 ++--------------------- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/libazureinit/Cargo.toml b/libazureinit/Cargo.toml index bc3a28fb..3bab5eac 100644 --- a/libazureinit/Cargo.toml +++ b/libazureinit/Cargo.toml @@ -2,6 +2,7 @@ name = "libazureinit" version = "0.1.1" edition = "2021" +rust-version = "1.76" repository = "https://github.com/Azure/azure-init/" homepage = "https://github.com/Azure/azure-init/" license = "MIT" @@ -20,6 +21,7 @@ serde_json = "1.0.96" nix = {version = "0.28.0", features = ["fs", "user"]} libc = "0.2.146" block-utils = "0.11.1" +tracing = "0.1.40" [dev-dependencies] tempfile = "3" diff --git a/libazureinit/src/media.rs b/libazureinit/src/media.rs index ff725ad1..76b6aabe 100644 --- a/libazureinit/src/media.rs +++ b/libazureinit/src/media.rs @@ -12,6 +12,8 @@ use std::process::Command; use serde::Deserialize; use serde_xml_rs::from_str; +use tracing; + use crate::error::Error; #[derive(Debug, Default, Deserialize, PartialEq, Clone)] @@ -187,6 +189,24 @@ pub fn parse_ovf_env(ovf_body: &str) -> Result { } } +// Mount the given device, get OVF environment data, return it. +pub fn mount_parse_ovf_env(dev: String) -> Result { + let mount_media = + Media::new(PathBuf::from(dev), PathBuf::from(PATH_MOUNT_POINT)); + let mounted = mount_media.mount().inspect_err( + |e| tracing::error!(error = ?e, "Failed to mount media."), + )?; + + let ovf_body = mounted.read_ovf_env_to_string()?; + let environment = parse_ovf_env(ovf_body.as_str())?; + + mounted.unmount().inspect_err( + |e| tracing::error!(error = ?e, "Failed to remove media."), + )?; + + Ok(environment) +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/main.rs b/src/main.rs index dd2e26c5..daf985be 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,6 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -use std::path::PathBuf; use std::process::ExitCode; use anyhow::Context; @@ -11,38 +10,20 @@ use libazureinit::imds::InstanceMetadata; use libazureinit::{ error::Error as LibError, goalstate, imds, media, - media::{Environment, Media}, + media::Environment, reqwest::{header, Client}, user, }; const VERSION: &str = env!("CARGO_PKG_VERSION"); -// Mount the given device, get OVF environment data, return it. -fn mount_parse_ovf_env(dev: String) -> Result { - let mount_media = - Media::new(PathBuf::from(dev), PathBuf::from(media::PATH_MOUNT_POINT)); - let mounted = mount_media - .mount() - .with_context(|| "Failed to mount media.")?; - - let ovf_body = mounted.read_ovf_env_to_string()?; - let environment = media::parse_ovf_env(ovf_body.as_str())?; - - mounted - .unmount() - .with_context(|| "Failed to remove media.")?; - - Ok(environment) -} - fn get_environment() -> Result { let ovf_devices = media::get_mount_device()?; let mut environment: Option = None; // loop until it finds a correct device. for dev in ovf_devices { - environment = match mount_parse_ovf_env(dev) { + environment = match media::mount_parse_ovf_env(dev) { Ok(env) => Some(env), Err(_) => continue, }