Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for commands with multiple arguments in custom operations #2602

Merged
merged 4 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions crates/common/logged_command/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ repository = { workspace = true }
[dependencies]
log = { workspace = true }
nix = { workspace = true }
shell-words = { workspace = true }
tokio = { workspace = true, features = [
"fs",
"io-util",
Expand All @@ -22,6 +23,7 @@ tokio = { workspace = true, features = [
[dev-dependencies]
anyhow = { workspace = true }
tedge_test_utils = { workspace = true }
tokio = { workspace = true, features = ["time"] }

[lints]
workspace = true
31 changes: 24 additions & 7 deletions crates/common/logged_command/src/logged_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,23 +148,40 @@ impl std::fmt::Display for LoggedCommand {
}

impl LoggedCommand {
pub fn new(program: impl AsRef<OsStr>) -> LoggedCommand {
pub fn new(program: impl AsRef<OsStr>) -> Result<LoggedCommand, std::io::Error> {
Copy link
Member

Choose a reason for hiding this comment

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

Very minor: I think try_new() is a better name as the function now returns a Result.

let command_line = match program.as_ref().to_str() {
None => format!("{:?}", program.as_ref()),
Some(cmd) => cmd.to_string(),
};

let mut command = Command::new(program);
let mut args = shell_words::split(&command_line)
.map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err))?;

let mut command = match args.len() {
0 => {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"command line is empty.",
))
}
1 => Command::new(&args[0]),
_ => {
let mut command = Command::new(args.remove(0));
command.args(&args);
command
}
};

command
.current_dir("/tmp")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped());

LoggedCommand {
Ok(LoggedCommand {
command_line,
command,
}
})
}

pub fn arg(&mut self, arg: impl AsRef<OsStr>) -> &mut LoggedCommand {
Expand Down Expand Up @@ -259,7 +276,7 @@ mod tests {
let mut logger = BufWriter::new(log_file);

// Prepare a command
let mut command = LoggedCommand::new("echo");
let mut command = LoggedCommand::new("echo").unwrap();
command.arg("Hello").arg("World!");

// Execute the command with logging
Expand Down Expand Up @@ -292,7 +309,7 @@ EOF
let mut logger = BufWriter::new(log_file);

// Prepare a command that triggers some content on stderr
let mut command = LoggedCommand::new("ls");
let mut command = LoggedCommand::new("ls").unwrap();
command.arg("dummy-file");

// Execute the command with logging
Expand Down Expand Up @@ -326,7 +343,7 @@ EOF
let mut logger = BufWriter::new(log_file);

// Prepare a command that cannot be executed
let command = LoggedCommand::new("dummy-command");
let command = LoggedCommand::new("dummy-command").unwrap();

// Execute the command with logging
let _ = command.execute(&mut logger).await;
Expand Down
7 changes: 5 additions & 2 deletions crates/core/plugin_sm/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,14 @@ impl ExternalPluginCommand {
maybe_module: Option<&SoftwareModule>,
) -> Result<LoggedCommand, SoftwareError> {
let mut command = if let Some(sudo) = &self.sudo {
let mut command = LoggedCommand::new(sudo);
// Safe unwrap
let mut command = LoggedCommand::new(sudo).unwrap();
command.arg(&self.path);
command
} else {
LoggedCommand::new(&self.path)
LoggedCommand::new(&self.path).map_err(|err| SoftwareError::ParseError {
reason: err.to_string(),
})?
};
command.arg(action);

Expand Down
8 changes: 7 additions & 1 deletion crates/extensions/c8y_mapper_ext/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,13 @@ impl CumulocityConverter {
let command = command.to_owned();
let payload = payload.to_string();

let mut logged = LoggedCommand::new(&command);
let mut logged =
LoggedCommand::new(&command).map_err(|e| CumulocityMapperError::ExecuteFailed {
error_message: e.to_string(),
command: command.to_string(),
operation_name: operation_name.to_string(),
})?;

logged.arg(&payload);

let maybe_child_process =
Expand Down
22 changes: 22 additions & 0 deletions docs/src/operate/c8y/supported_operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,25 @@ The command will be executed with tedge-mapper permission level so most of the s
* `on_message` - The SmartRest template on which the operation will be executed.
* `command` - The command to execute.
* `result_format` - The expected command output format: `"text"` or `"csv"`, `"text"` being the default.

:::info
The `command` parameter accepts command arguments when provided as a one string, e.g.

```toml title="file: /etc/tedge/operations/c8y/c8y_Command"
[exec]
topic = "c8y/s/ds"
on_message = "511"
command = "python /etc/tedge/operations/command.py"
timeout = 10
```

Arguments will be parsed correctly as long as following features are not included in input: operators, variable assignments, tilde expansion, parameter expansion, command substitution, arithmetic expansion and pathname expansion.

In case those unsupported shell features are present, the syntax that introduce them is interpreted literally.

Be aware that SmartREST payload is always added as the last argument. The command presented above will actually lead to following code execution

```bash
python /etc/tedge/operations/command.py $SMART_REST_PAYLOAD
```
:::
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[exec]
topic = "c8y/s/ds"
on_message = "511"
command = "sh /etc/tedge/operations/command_1.sh"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
echo "command 1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
*** Settings ***
Resource ../../../../resources/common.resource
Library Cumulocity
Library ThinEdgeIO

Test Setup Custom Setup
Test Teardown Get Logs

Test Tags theme:c8y theme:operation theme:custom


*** Test Cases ***
Run the custom operation with multiple arguments
${operation}= Cumulocity.Create Operation description=do something fragments={"c8y_Command":{"text":""}}
Operation Should Be SUCCESSFUL ${operation}
Should Be Equal ${operation.to_json()["c8y_Command"]["result"]} command 1\n
reubenmiller marked this conversation as resolved.
Show resolved Hide resolved


*** Keywords ***
Custom Setup
${DEVICE_SN}= Setup
Set Suite Variable $DEVICE_SN
Device Should Exist ${DEVICE_SN}
ThinEdgeIO.Transfer To Device ${CURDIR}/command_1.sh /etc/tedge/operations/
Execute Command chmod a+x /etc/tedge/operations/command_1.sh
ThinEdgeIO.Transfer To Device ${CURDIR}/c8y_Command_1 /etc/tedge/operations/c8y/c8y_Command