Skip to content

Commit

Permalink
Merge pull request #3099 from Bravo555/fix/tedge-write-remove-hardcod…
Browse files Browse the repository at this point in the history
…ed-path

fix: tedge write remove hardcoded path
  • Loading branch information
Bravo555 authored Sep 4, 2024
2 parents 323615c + f8e94a5 commit b9467fe
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
2 changes: 2 additions & 0 deletions crates/common/tedge_config/src/sudo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ impl SudoCommandBuilder {
match which::which(self.sudo_program.as_ref()) {
Ok(sudo) => {
let mut c = Command::new(sudo);
// non-interactive
c.arg("-n");
c.arg(program);
c
}
Expand Down
8 changes: 7 additions & 1 deletion crates/core/tedge_write/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use camino::Utf8Path;
use std::process::Command;
use tedge_config::SudoCommandBuilder;

use crate::TEDGE_WRITE_BINARY;

/// Options for copying files using a `tedge-write` process.
#[derive(Debug, PartialEq)]
pub struct CopyOptions<'a> {
Expand Down Expand Up @@ -57,7 +59,11 @@ impl<'a> CopyOptions<'a> {
}

fn command(&self) -> anyhow::Result<Command> {
let mut command = self.sudo.command(crate::TEDGE_WRITE_PATH);
// if tedge-write is in PATH of tedge process, use it, if not, defer PATH lookup to sudo
let tedge_write_binary =
which::which_global(TEDGE_WRITE_BINARY).unwrap_or(TEDGE_WRITE_BINARY.into());

let mut command = self.sudo.command(tedge_write_binary);

let from_reader = std::fs::File::open(self.from)
.with_context(|| format!("could not open file for reading '{}'", self.from))?;
Expand Down
2 changes: 1 addition & 1 deletion crates/core/tedge_write/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
//!
//! https://github.com/thin-edge/thin-edge.io/issues/2456

const TEDGE_WRITE_PATH: &str = "/usr/bin/tedge-write";
const TEDGE_WRITE_BINARY: &str = "tedge-write";

pub mod bin;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ Set Configuration when file exists and tedge run by root
Binary file (Child Device) ${CHILD_SN} ${PARENT_SN}:device:${CHILD_SN} CONFIG1_BINARY /etc/binary-config1.tar.gz ${CURDIR}/binary-config1.tar.gz 664 root:root delete_file_before=${false}
... agent_as_root=${true}

Set Configuration when tedge-write is in another location
[Template] Set Configuration from Device with tedge-write at another location
Text file (Main Device) ${PARENT_SN} ${PARENT_SN} CONFIG1 /etc/config1.json ${CURDIR}/config1-version2.json 664 root:root delete_file_before=${false}
Binary file (Main Device) ${PARENT_SN} ${PARENT_SN} CONFIG1_BINARY /etc/binary-config1.tar.gz ${CURDIR}/binary-config1.tar.gz 664 root:root delete_file_before=${false}
Text file (Child Device) ${CHILD_SN} ${PARENT_SN}:device:${CHILD_SN} CONFIG1 /etc/config1.json ${CURDIR}/config1-version2.json 664 root:root delete_file_before=${false}
Binary file (Child Device) ${CHILD_SN} ${PARENT_SN}:device:${CHILD_SN} CONFIG1_BINARY /etc/binary-config1.tar.gz ${CURDIR}/binary-config1.tar.gz 664 root:root delete_file_before=${false}

Set configuration with broken url
[Template] Set Configuration from URL
Main Device ${PARENT_SN} ${PARENT_SN} CONFIG1 /etc/config1.json invalid://hellö.zip
Expand Down Expand Up @@ -294,6 +301,62 @@ Set Configuration from Device
END
END

Set Configuration from Device with tedge-write at another location
[Documentation]
... Check if config_update still works if `tedge-write` binary is present at another location. For that we need
... to make sure that other location is in $PATH and that this new $PATH is inherited by tedge-agent, so for the
... purposes of the test we change $PATH at the tedge-agent systemd service level. We also add a sudoers entry
... with new path of tedge-write so sudo correcly elevates permissions.
[Arguments]
... ${test_desc}
... ${device}
... ${external_id}
... ${config_type}
... ${device_file}
... ${file}
... ${permission}
... ${ownership}
... ${delete_file_before}=${true}
[Setup]
[Teardown]

Set Device Context ${device}

# Have /opt/tedge/bin in $PATH of tedge-agent
Execute Command mkdir -p /etc/systemd/system/tedge-agent.service.d
Execute Command cmd=echo "[Service]\nEnvironment=\\"PATH=/opt/tedge/bin:$PATH\\"" > /etc/systemd/system/tedge-agent.service.d/10-override-path.conf
Execute Command systemctl daemon-reload
Restart Service tedge-agent

# put tedge-write in /opt/tedge/bin
Execute Command mkdir -p /opt/tedge/bin
Execute Command mv /usr/bin/tedge-write /opt/tedge/bin/
Execute Command
... echo 'tedge ALL \= (ALL) NOPASSWD: /opt/tedge/bin/tedge-write' > /etc/sudoers.d/20-tedge-opt

TRY
Set Configuration from Device
... ${test_desc}
... ${device}
... ${external_id}
... ${config_type}
... ${device_file}
... ${file}
... ${permission}
... ${ownership}
... ${delete_file_before}
FINALLY
# cleanup
Set Device Context ${device}

Execute Command mv /opt/tedge/bin/tedge-write /usr/bin/
Execute Command rm /etc/sudoers.d/20-tedge-opt

Execute Command rm -r /etc/systemd/system/tedge-agent.service.d
Execute Command systemctl daemon-reload
Restart Service tedge-agent
END

Set Configuration from URL
[Arguments] ${test_desc} ${device} ${external_id} ${config_type} ${device_file} ${config_url}

Expand Down

0 comments on commit b9467fe

Please sign in to comment.