Update the archive step to actually generate tarballs when necessary

This commit is contained in:
R Tyler Croy 2020-10-28 15:35:05 -07:00
parent c7d290d057
commit 99b32b86bd
3 changed files with 69 additions and 7 deletions

View File

@ -34,15 +34,24 @@ fn artifact_matches(pattern: &str) -> Vec<PathBuf> {
/**
* This function will create a tarball based on the given paths
*/
fn create_tarball(output: &str, paths: &Vec<PathBuf>) -> std::io::Result<()> {
let tar_gz = File::create(output)?;
fn create_tarball(output: &str, paths: &Vec<PathBuf>) -> std::io::Result<PathBuf> {
let output = format!("{}.tar.gz", output);
let path = Path::new(&output);
let tar_gz = File::create(&output)?;
let enc = GzEncoder::new(tar_gz, Compression::default());
let mut tar = tar::Builder::new(enc);
for path in paths.iter() {
tar.append_path(path).expect(&format!("Failed to add {:#?} to the tarball", path));
if path.is_dir() {
tar.append_dir_all(".", path).expect(&format!("Failed to add {:#?} to the tarball", path));
}
else {
tar.append_path(path).expect(&format!("Failed to add {:#?} to the tarball", path));
}
}
tar.finish()?;
Ok(())
Ok(path.to_path_buf())
}
/**
@ -99,7 +108,10 @@ fn main() -> std::io::Result<()> {
1 => {
// no tarball, unless it's a directory
let file = &artifacts[0];
let name = file.as_path().file_name().expect("Failed to determine the file name for the archive");
let name = match invoke.parameters.name {
None => file.as_path().file_name().expect("Failed to determine the file name for the archive").to_string_lossy().into_owned(),
Some(name) => name,
};
// No archiving /etc/passwd you silly goose
if ! is_child_path(&file) {
@ -107,7 +119,7 @@ fn main() -> std::io::Result<()> {
}
if file.is_dir() {
create_tarball(&name.to_string_lossy(), &artifacts);
create_tarball(&name, &artifacts);
}
else {
archive(file);
@ -124,7 +136,7 @@ fn main() -> std::io::Result<()> {
// TODO handle
},
Ok(file) => {
//archive(file);
archive(&file);
}
}
},

View File

@ -30,4 +30,16 @@ EOF
assertFalse "step should fail when invoked with an absolute path" "archive-step $INVOCATION_FILE"
}
test_fail_with_path_traversal() {
cat > $INVOCATION_FILE<<EOF
---
configuration:
ipc: unix:///dev/null
parameters:
artifacts: ../../../
EOF
assertFalse "step should fail when invoked with an absolute path" "archive-step $INVOCATION_FILE"
}
. $(dirname $0)/../../../contrib/shunit2/shunit2

View File

@ -0,0 +1,38 @@
#!/bin/sh
INVOCATION_FILE=tmp_archivetest_invocation_file.yml
TAR_NAME=tmp_archivetest_tar
oneTimeTearDown() {
rm -f $INVOCATION_FILE
rm -f "${TAR_NAME}.tar.gz"
}
test_fail_with_file() {
cat > $INVOCATION_FILE<<EOF
---
configuration:
ipc: unix:///dev/null
parameters:
artifacts: Cargo.toml
EOF
assertTrue "step should do nothing with a single file" "archive-step $INVOCATION_FILE"
}
test_fail_with_dir() {
cat > $INVOCATION_FILE<<EOF
---
configuration:
ipc: unix:///dev/null
parameters:
artifacts: $(dirname $0)
name: ${TAR_NAME}
EOF
assertTrue "step should create tarball with a directory" "archive-step $INVOCATION_FILE"
assertTrue "file name ${TAR_NAME}.tar.gz not found" "test -f ${TAR_NAME}.tar.gz"
}
. $(dirname $0)/../../../contrib/shunit2/shunit2