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

krane: write krane binary to a tempfile #405

Merged
merged 2 commits into from
Nov 8, 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
12 changes: 1 addition & 11 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ nix = "0.28"
nonzero_ext = "0.3"
num_cpus = "1"
olpc-cjson = "0.1"
pentacle = "1.1"
rand = { version = "0.8", default-features = false }
regex = "1"
reqwest = { version = "0.11", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion tools/krane/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ publish = false
anyhow.workspace = true
flate2.workspace = true
lazy_static.workspace = true
pentacle.workspace = true
tempfile.workspace = true

[build-dependencies]
flate2.workspace = true
Expand Down
3 changes: 1 addition & 2 deletions tools/krane/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ This crate packages the `krane` utility from [google/go-containerregistry].

The utility is compiled by a build script, the output of which is compressed and stored in the Rust
crate as via `include_bytes!`.
At runtime, `krane-bundle` writes the decompressed binary to a [sealed anonymous file], passing the
At runtime, `krane-bundle` writes the decompressed binary to a temp file, passing the
filepath of that file to any caller.

[google/go-containerregistry]: https://github.com/google/go-containerregistry
[sealed anonymous file]: https://github.com/haha-business/pentacle
33 changes: 16 additions & 17 deletions tools/krane/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use anyhow::{Context, Result};
use anyhow::Result;
use flate2::read::GzDecoder;
use std::fs::File;
use std::os::fd::AsRawFd;
use std::path::{Path, PathBuf};
use std::fs::{File, Permissions};
use std::os::unix::fs::PermissionsExt;
use std::path::PathBuf;

use pentacle::SealOptions;
use tempfile::TempDir;

const COMPRESSED_KRANE_BIN: &[u8] = include_bytes!(env!("KRANE_GZ_PATH"));

Expand All @@ -15,31 +15,30 @@ lazy_static::lazy_static! {
#[derive(Debug)]
pub struct Krane {
// Hold the file in memory to keep the fd open
_sealed_binary: File,
_tmp_dir: TempDir,
path: PathBuf,
}

impl Krane {
fn seal() -> Result<Krane> {
let mut krane_reader = GzDecoder::new(COMPRESSED_KRANE_BIN);
let tmp_dir = TempDir::new()?;
let path = tmp_dir.path().join("krane");

let mut krane_file = File::create(&path)?;
let permissions = Permissions::from_mode(0o755);
krane_file.set_permissions(permissions)?;

let sealed_binary = SealOptions::new()
.close_on_exec(false)
.executable(true)
.copy_and_seal(&mut krane_reader)
.context("Failed to write krane binary to sealed anonymous file")?;
let mut krane_reader = GzDecoder::new(COMPRESSED_KRANE_BIN);

let fd = sealed_binary.as_raw_fd();
let pid = std::process::id();
let path = PathBuf::from(format!("/proc/{pid}/fd/{fd}"));
std::io::copy(&mut krane_reader, &mut krane_file)?;

Ok(Krane {
_sealed_binary: sealed_binary,
_tmp_dir: tmp_dir,
path,
})
}

pub fn path(&self) -> &Path {
pub fn path(&self) -> &PathBuf {
&self.path
}
}
Expand Down
7 changes: 6 additions & 1 deletion tools/oci-cli-wrapper/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ impl CommandLine {
ensure!(
output.status.success(),
error::OperationFailedSnafu {
message: String::from_utf8_lossy(&output.stderr),
message: format!(
"status: {} stderr: {} stdout: {}",
&output.status,
String::from_utf8_lossy(&output.stderr),
String::from_utf8_lossy(&output.stdout)
),
program: self.path.clone(),
args: args.iter().map(|x| x.to_string()).collect::<Vec<_>>()
}
Expand Down
Loading