Skip to content

Commit

Permalink
Merge pull request #2888 from reubenmiller/fix-remote-access-spawn-child
Browse files Browse the repository at this point in the history
fix(remote-access): spawn process using current process path and config dir
  • Loading branch information
reubenmiller authored May 21, 2024
2 parents 1002ed6 + 4ae402e commit 2b3e3ea
Showing 1 changed file with 4 additions and 25 deletions.
29 changes: 4 additions & 25 deletions plugins/c8y_remote_access_plugin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::fmt::Display;
use std::fmt::Formatter;
use std::path::PathBuf;
use std::process::Stdio;

use camino::Utf8Path;
Expand All @@ -16,7 +15,6 @@ use tedge_utils::file::create_directory_with_user_group;
use tedge_utils::file::create_file_with_user_group;
use tokio::io::AsyncBufReadExt;
use tokio::io::BufReader;
use toml::Table;
use url::Url;

pub use crate::input::C8yRemoteAccessPluginOpt;
Expand Down Expand Up @@ -95,9 +93,12 @@ static SUCCESS_MESSAGE: &str = "CONNECTED";
struct Unreachable<E: std::error::Error + 'static>(#[source] E, &'static str);

async fn spawn_child(command: String, config_dir: &Utf8Path) -> miette::Result<()> {
let exec_path = get_executable_path(config_dir).await?;
let exec_path = std::env::current_exe()
.into_diagnostic()
.with_context(|| "Could not get current process executable")?;

let mut command = tokio::process::Command::new(exec_path)
.args(["--config-dir", config_dir.as_str()])
.arg("--child")
.arg(command)
.stdout(Stdio::piped())
Expand Down Expand Up @@ -211,28 +212,6 @@ fn build_proxy_url(
.context("Creating websocket URL")
}

async fn get_executable_path(config_dir: &Utf8Path) -> miette::Result<PathBuf> {
let operation_path = supported_operation_path(config_dir);

let content = tokio::fs::read_to_string(&operation_path)
.await
.into_diagnostic()
.with_context(|| {
format!("The operation file {operation_path} does not exist or is not readable.")
})?;

let operation: Table = content
.parse()
.into_diagnostic()
.with_context(|| format!("Failed to parse {operation_path} file"))?;

Ok(PathBuf::from(
operation["exec"]["command"]
.as_str()
.ok_or_else(|| miette!("Failed to read command from {operation_path} file"))?,
))
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 2b3e3ea

Please sign in to comment.