Skip to content

Commit

Permalink
Merge #154
Browse files Browse the repository at this point in the history
154: Fix parsing of docker_switches to adhere to shell quoting rules r=maxdymond a=rlupton20

## Why this change?

At present `floki` doesn't handle quoted arguments properly in the `docker_switches` escape hatch. This adds proper shell compliant parsing of these switches.

## Relevant testing

Tested by hand

```
docker_switches:
        - -e FOO="bar this that other"
```

```
$ ./target/x86_64-unknown-linux-musl/debug/floki run -- echo '$FOO'
bar this that other
```

Error case (missing closing quote):

```
docker_switches:
        - -e FOO="bar this that other
```

```
$ ./target/x86_64-unknown-linux-musl/debug/floki run -- echo '$FOO'
23:54:45 [ERROR] A problem occurred: Malformed item in docker_switches: -e FOO="bar this that other
```

## Contributor notes

I don't think this should break anything that wasn't already broken. It would be nice if this were a little more testable, and more generally I'd like to push the validation (and flattening) of this kind of thing further toward the start of the program, so the rest can just be dumb and simple. Not for now, and perhaps only for when a need to simplify this occurs, or someone fancies the cleanup. It's hard to imagine this program getting wildly more complicated!

## Checks

These aren't hard requirements, just guidelines

- [x] New/modified Rust code formatted with `cargo fmt`



Co-authored-by: Richard Lupton <[email protected]>
  • Loading branch information
bors[bot] and rlupton20 authored Feb 4, 2021
2 parents c59a0d8 + 9cb7d02 commit 4beb66e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Status: Available for use

### Fixed
- Don't require a floki config file to be present to run `floki completion`
- Properly parse `docker_switches` in accordance with shell quoting rules

## [0.6.1] - 2020-07-16

Expand Down
7 changes: 7 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ yaml-rust = "0.4.4"
rust-crypto = "^0.2"
simplelog = "0.9"
nix = "0.19"
shlex = "0.1"

[dev-dependencies]
tempdir = "0.3.7"
3 changes: 3 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ pub enum FlokiError {

#[fail(display = "Unable to forward ssh socket - cannot find SSH_AUTH_SOCK in environment")]
NoSshAuthSock {},

#[fail(display = "Malformed item in docker_switches: {}", item)]
MalformedDockerSwitch { item: String },
}

/// Generate a summary string for a process exiting
Expand Down
50 changes: 44 additions & 6 deletions src/interpret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub(crate) fn run_container(
cmd = configure_floki_host_mountdir_env(cmd, &environ.floki_root);
cmd = configure_forward_user(cmd, &config, &environ);
cmd = configure_forward_ssh_agent(cmd, &config, &environ)?;
cmd = configure_docker_switches(cmd, &config);
cmd = configure_docker_switches(cmd, &config)?;
cmd = configure_working_directory(cmd, &environ, &config);
cmd = configure_volumes(cmd, &volumes);

Expand Down Expand Up @@ -104,15 +104,30 @@ fn configure_forward_ssh_agent(
fn configure_docker_switches(
cmd: DockerCommandBuilder,
config: &FlokiConfig,
) -> DockerCommandBuilder {
) -> Result<DockerCommandBuilder, Error> {
let mut cmd = cmd;
for switch in &config.docker_switches {
for s in switch.split_whitespace() {
cmd = cmd.add_docker_switch(s);
for switch in decompose_switches(&config.docker_switches)? {
cmd = cmd.add_docker_switch(switch);
}
Ok(cmd)
}

fn decompose_switches(
specs: &Vec<String>,
) -> Result<Vec<String>, Error> {
let mut flattened = Vec::new();

for spec in specs {
if let Some(switches) = shlex::split(spec) {
for s in switches {
flattened.push(s);
}
} else {
Err(errors::FlokiError::MalformedDockerSwitch { item: spec.into() })?
}
}

cmd
Ok(flattened)
}

fn configure_working_directory(
Expand Down Expand Up @@ -213,4 +228,27 @@ mod test {
== path::PathBuf::from("/guest/workingdir/")
)
}

#[test]
fn test_decompose_switches() -> Result<(), Error> {
let switches = vec!["-e FOO='bar baz'".to_string()];

let want: Vec<String> = vec![
"-e".to_string(),
"FOO=bar baz".to_string(),
];

let got = decompose_switches(&switches)?;

assert_eq!(want, got);

Ok(())
}

#[test]
fn test_decompose_switches_error() {
let switches = vec!["-e FOO='bar baz".to_string()];
let got = decompose_switches(&switches);
assert!(got.is_err());
}
}

0 comments on commit 4beb66e

Please sign in to comment.