2024-01-18 08:02:21 +00:00
|
|
|
pub mod cache;
|
2024-01-18 18:16:59 +00:00
|
|
|
pub mod fetch;
|
|
|
|
pub mod name;
|
2024-01-18 08:02:21 +00:00
|
|
|
|
2024-01-17 22:29:05 +00:00
|
|
|
use crate::error::{HyphaError, Result};
|
2024-01-18 08:02:21 +00:00
|
|
|
use crate::image::cache::ImageCache;
|
2024-01-18 18:16:59 +00:00
|
|
|
use crate::image::fetch::RegistryClient;
|
|
|
|
use crate::image::name::ImageName;
|
2024-01-17 22:29:05 +00:00
|
|
|
use backhand::{FilesystemWriter, NodeHeader};
|
2024-01-20 10:41:49 +00:00
|
|
|
use flate2::read::GzDecoder;
|
2024-01-17 22:29:05 +00:00
|
|
|
use log::{debug, trace};
|
2024-01-20 10:41:49 +00:00
|
|
|
use oci_spec::image::{Descriptor, ImageConfiguration, ImageManifest, MediaType};
|
2024-01-17 22:29:05 +00:00
|
|
|
use std::fs;
|
|
|
|
use std::fs::File;
|
2024-01-20 10:41:49 +00:00
|
|
|
use std::io::{copy, BufReader, Seek, SeekFrom, Write};
|
2024-01-17 22:29:05 +00:00
|
|
|
use std::os::unix::fs::{FileTypeExt, MetadataExt, PermissionsExt};
|
2024-01-20 10:41:49 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use tar::Entry;
|
2024-01-17 22:29:05 +00:00
|
|
|
use uuid::Uuid;
|
|
|
|
use walkdir::WalkDir;
|
|
|
|
|
2024-01-18 08:21:03 +00:00
|
|
|
pub const IMAGE_SQUASHFS_VERSION: u64 = 1;
|
|
|
|
|
2024-01-18 08:02:21 +00:00
|
|
|
pub struct ImageInfo {
|
2024-01-18 22:52:35 +00:00
|
|
|
pub image_squashfs: PathBuf,
|
2024-01-18 08:02:21 +00:00
|
|
|
pub manifest: ImageManifest,
|
2024-01-18 08:15:36 +00:00
|
|
|
pub config: ImageConfiguration,
|
2024-01-18 08:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ImageInfo {
|
2024-01-18 08:15:36 +00:00
|
|
|
fn new(
|
|
|
|
squashfs: PathBuf,
|
|
|
|
manifest: ImageManifest,
|
|
|
|
config: ImageConfiguration,
|
|
|
|
) -> Result<ImageInfo> {
|
|
|
|
Ok(ImageInfo {
|
2024-01-18 22:52:35 +00:00
|
|
|
image_squashfs: squashfs,
|
2024-01-18 08:15:36 +00:00
|
|
|
manifest,
|
|
|
|
config,
|
|
|
|
})
|
2024-01-18 08:02:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ImageCompiler<'a> {
|
|
|
|
cache: &'a ImageCache,
|
|
|
|
}
|
2024-01-17 22:29:05 +00:00
|
|
|
|
2024-01-18 08:02:21 +00:00
|
|
|
impl ImageCompiler<'_> {
|
|
|
|
pub fn new(cache: &ImageCache) -> Result<ImageCompiler> {
|
|
|
|
Ok(ImageCompiler { cache })
|
2024-01-17 22:29:05 +00:00
|
|
|
}
|
|
|
|
|
2024-01-18 08:02:21 +00:00
|
|
|
pub fn compile(&self, image: &ImageName) -> Result<ImageInfo> {
|
2024-01-17 22:29:05 +00:00
|
|
|
debug!("ImageCompiler compile image={image}");
|
|
|
|
let mut tmp_dir = std::env::temp_dir().clone();
|
|
|
|
tmp_dir.push(format!("hypha-compile-{}", Uuid::new_v4()));
|
2024-01-20 10:41:49 +00:00
|
|
|
|
2024-01-17 22:29:05 +00:00
|
|
|
let mut image_dir = tmp_dir.clone();
|
|
|
|
image_dir.push("image");
|
|
|
|
fs::create_dir_all(&image_dir)?;
|
2024-01-20 10:41:49 +00:00
|
|
|
|
|
|
|
let mut layer_dir = tmp_dir.clone();
|
|
|
|
layer_dir.push("layer");
|
|
|
|
fs::create_dir_all(&layer_dir)?;
|
|
|
|
|
2024-01-17 22:29:05 +00:00
|
|
|
let mut squash_file = tmp_dir.clone();
|
|
|
|
squash_file.push("image.squashfs");
|
2024-01-20 10:41:49 +00:00
|
|
|
let info = self.download_and_compile(image, &layer_dir, &image_dir, &squash_file)?;
|
|
|
|
fs::remove_dir_all(&tmp_dir)?;
|
2024-01-18 08:02:21 +00:00
|
|
|
Ok(info)
|
2024-01-17 22:29:05 +00:00
|
|
|
}
|
|
|
|
|
2024-01-18 08:02:21 +00:00
|
|
|
fn download_and_compile(
|
|
|
|
&self,
|
|
|
|
image: &ImageName,
|
2024-01-20 10:41:49 +00:00
|
|
|
layer_dir: &Path,
|
2024-01-18 08:02:21 +00:00
|
|
|
image_dir: &PathBuf,
|
|
|
|
squash_file: &PathBuf,
|
|
|
|
) -> Result<ImageInfo> {
|
2024-01-17 22:29:05 +00:00
|
|
|
debug!(
|
2024-01-20 10:41:49 +00:00
|
|
|
"ImageCompiler download manifest image={image}, image_dir={}",
|
2024-01-17 22:29:05 +00:00
|
|
|
image_dir.to_str().unwrap()
|
|
|
|
);
|
2024-01-18 18:16:59 +00:00
|
|
|
let mut client = RegistryClient::new(image.registry_url()?)?;
|
|
|
|
let manifest = client.get_manifest(&image.name, &image.reference)?;
|
2024-01-18 08:02:21 +00:00
|
|
|
let manifest_serialized = serde_json::to_string(&manifest)?;
|
2024-01-18 08:21:03 +00:00
|
|
|
let cache_key = format!(
|
|
|
|
"manifest\n{}squashfs-version\n{}\n",
|
|
|
|
manifest_serialized, IMAGE_SQUASHFS_VERSION
|
|
|
|
);
|
|
|
|
let cache_digest = sha256::digest(cache_key);
|
|
|
|
|
|
|
|
if let Some(cached) = self.cache.recall(&cache_digest)? {
|
2024-01-18 08:02:21 +00:00
|
|
|
return Ok(cached);
|
|
|
|
}
|
2024-01-18 08:15:36 +00:00
|
|
|
|
2024-01-20 10:41:49 +00:00
|
|
|
debug!(
|
|
|
|
"ImageCompiler download config digest={} size={}",
|
|
|
|
manifest.config().digest(),
|
|
|
|
manifest.config().size(),
|
|
|
|
);
|
2024-01-18 18:16:59 +00:00
|
|
|
let config_bytes = client.get_blob(&image.name, manifest.config())?;
|
2024-01-18 08:15:36 +00:00
|
|
|
let config: ImageConfiguration = serde_json::from_slice(&config_bytes)?;
|
|
|
|
|
2024-01-20 10:41:49 +00:00
|
|
|
let mut layers: Vec<PathBuf> = Vec::new();
|
2024-01-17 22:29:05 +00:00
|
|
|
for layer in manifest.layers() {
|
2024-01-20 10:41:49 +00:00
|
|
|
let layer_path = self.download_layer(image, layer, layer_dir, &mut client)?;
|
|
|
|
layers.push(layer_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
for layer in layers {
|
|
|
|
let mut file = File::open(&layer)?;
|
|
|
|
self.process_whiteout_entries(&file, image_dir)?;
|
|
|
|
file.seek(SeekFrom::Start(0))?;
|
|
|
|
self.process_write_entries(&file, image_dir)?;
|
|
|
|
drop(file);
|
|
|
|
fs::remove_file(&layer)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.squash(image_dir, squash_file)?;
|
|
|
|
let info = ImageInfo::new(squash_file.clone(), manifest.clone(), config)?;
|
|
|
|
self.cache.store(&cache_digest, &info)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process_whiteout_entries(&self, file: &File, image_dir: &PathBuf) -> Result<()> {
|
|
|
|
let mut archive = tar::Archive::new(file);
|
|
|
|
for entry in archive.entries()? {
|
|
|
|
let entry = entry?;
|
|
|
|
let dst = self.check_safe_entry(&entry, image_dir)?;
|
|
|
|
let Some(name) = dst.file_name() else {
|
|
|
|
return Err(HyphaError::new("unable to get file name"));
|
|
|
|
};
|
|
|
|
let Some(name) = name.to_str() else {
|
|
|
|
return Err(HyphaError::new("unable to get file name as string"));
|
|
|
|
};
|
|
|
|
if !name.starts_with(".wh.") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let mut dst = dst.clone();
|
|
|
|
dst.pop();
|
|
|
|
|
|
|
|
let opaque = name == ".wh..wh..opq";
|
|
|
|
|
|
|
|
if !opaque {
|
|
|
|
dst.push(name);
|
|
|
|
self.check_safe_path(&dst, image_dir)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if opaque {
|
|
|
|
for entry in fs::read_dir(dst)? {
|
|
|
|
let entry = entry?;
|
|
|
|
let path = entry.path();
|
|
|
|
if path.is_file() {
|
|
|
|
fs::remove_file(&path)?;
|
|
|
|
} else {
|
|
|
|
fs::remove_dir_all(&path)?;
|
2024-01-17 22:29:05 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-20 10:41:49 +00:00
|
|
|
} else if dst.is_file() {
|
|
|
|
fs::remove_file(&dst)?;
|
|
|
|
} else {
|
|
|
|
fs::remove_dir(&dst)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process_write_entries(&self, file: &File, image_dir: &PathBuf) -> Result<()> {
|
|
|
|
let mut archive = tar::Archive::new(file);
|
|
|
|
for entry in archive.entries()? {
|
|
|
|
let mut entry = entry?;
|
|
|
|
let dst = self.check_safe_entry(&entry, image_dir)?;
|
|
|
|
let Some(name) = dst.file_name() else {
|
|
|
|
return Err(HyphaError::new("unable to get file name"));
|
|
|
|
};
|
|
|
|
let Some(name) = name.to_str() else {
|
|
|
|
return Err(HyphaError::new("unable to get file name as string"));
|
|
|
|
};
|
|
|
|
if name.starts_with(".wh.") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
entry.unpack(dst)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_safe_entry(&self, entry: &Entry<&File>, image_dir: &PathBuf) -> Result<PathBuf> {
|
|
|
|
let mut dst = image_dir.clone();
|
|
|
|
dst.push(entry.path()?);
|
|
|
|
self.check_safe_path(&dst, image_dir)?;
|
|
|
|
Ok(dst)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_safe_path(&self, dst: &PathBuf, image_dir: &PathBuf) -> Result<()> {
|
|
|
|
let resolved = path_clean::clean(dst);
|
|
|
|
if !resolved.starts_with(image_dir) {
|
|
|
|
return Err(HyphaError::new("layer attempts to work outside image dir"));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn download_layer(
|
|
|
|
&self,
|
|
|
|
image: &ImageName,
|
|
|
|
layer: &Descriptor,
|
|
|
|
layer_dir: &Path,
|
|
|
|
client: &mut RegistryClient,
|
|
|
|
) -> Result<PathBuf> {
|
|
|
|
debug!(
|
|
|
|
"ImageCompiler download layer digest={} size={}",
|
|
|
|
layer.digest(),
|
|
|
|
layer.size()
|
|
|
|
);
|
|
|
|
let mut layer_path = layer_dir.to_path_buf();
|
|
|
|
layer_path.push(layer.digest());
|
|
|
|
let mut tmp_path = layer_dir.to_path_buf();
|
|
|
|
tmp_path.push(format!("{}.tmp", layer.digest()));
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut file = File::create(&layer_path)?;
|
|
|
|
let size = client.write_blob(&image.name, layer, &mut file)?;
|
|
|
|
if layer.size() as u64 != size {
|
|
|
|
return Err(HyphaError::new(
|
|
|
|
"downloaded layer size differs from size in manifest",
|
|
|
|
));
|
2024-01-17 22:29:05 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-20 10:41:49 +00:00
|
|
|
|
|
|
|
let compressed = match layer.media_type() {
|
|
|
|
MediaType::ImageLayer => false,
|
|
|
|
MediaType::ImageLayerGzip => {
|
|
|
|
let reader = File::open(&layer_path)?;
|
|
|
|
let mut decoder = GzDecoder::new(&reader);
|
|
|
|
let mut writer = File::create(&tmp_path)?;
|
|
|
|
copy(&mut decoder, &mut writer)?;
|
|
|
|
writer.flush()?;
|
|
|
|
true
|
|
|
|
}
|
|
|
|
MediaType::ImageLayerZstd => {
|
|
|
|
let reader = File::open(&layer_path)?;
|
|
|
|
let mut decoder = zstd::Decoder::new(&reader)?;
|
|
|
|
let mut writer = File::create(&tmp_path)?;
|
|
|
|
copy(&mut decoder, &mut writer)?;
|
|
|
|
writer.flush()?;
|
|
|
|
true
|
|
|
|
}
|
|
|
|
_ => return Err(HyphaError::new("found layer with unknown media type")),
|
|
|
|
};
|
|
|
|
|
|
|
|
if compressed {
|
|
|
|
fs::rename(tmp_path, &layer_path)?;
|
|
|
|
}
|
|
|
|
Ok(layer_path)
|
2024-01-17 22:29:05 +00:00
|
|
|
}
|
|
|
|
|
2024-01-18 08:02:21 +00:00
|
|
|
fn squash(&self, image_dir: &PathBuf, squash_file: &PathBuf) -> Result<()> {
|
2024-01-17 22:29:05 +00:00
|
|
|
let mut writer = FilesystemWriter::default();
|
|
|
|
let walk = WalkDir::new(image_dir).follow_links(false);
|
|
|
|
for entry in walk {
|
|
|
|
let entry = entry?;
|
|
|
|
let rel = entry
|
|
|
|
.path()
|
|
|
|
.strip_prefix(image_dir)?
|
|
|
|
.to_str()
|
|
|
|
.ok_or_else(|| HyphaError::new("failed to strip prefix of tmpdir"))?;
|
|
|
|
let rel = format!("/{}", rel);
|
|
|
|
trace!("ImageCompiler squash write {}", rel);
|
|
|
|
let typ = entry.file_type();
|
|
|
|
let metadata = fs::symlink_metadata(entry.path())?;
|
|
|
|
let uid = metadata.uid();
|
|
|
|
let gid = metadata.gid();
|
|
|
|
let mode = metadata.permissions().mode();
|
|
|
|
let mtime = metadata.mtime();
|
|
|
|
|
|
|
|
if rel == "/" {
|
|
|
|
writer.set_root_uid(uid);
|
|
|
|
writer.set_root_gid(gid);
|
|
|
|
writer.set_root_mode(mode as u16);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let header = NodeHeader {
|
|
|
|
permissions: mode as u16,
|
|
|
|
uid,
|
|
|
|
gid,
|
|
|
|
mtime: mtime as u32,
|
|
|
|
};
|
|
|
|
if typ.is_symlink() {
|
|
|
|
let symlink = fs::read_link(entry.path())?;
|
|
|
|
let symlink = symlink
|
|
|
|
.to_str()
|
|
|
|
.ok_or_else(|| HyphaError::new("failed to read symlink"))?;
|
|
|
|
writer.push_symlink(symlink, rel, header)?;
|
|
|
|
} else if typ.is_dir() {
|
|
|
|
writer.push_dir(rel, header)?;
|
|
|
|
} else if typ.is_file() {
|
|
|
|
let reader = BufReader::new(File::open(entry.path())?);
|
|
|
|
writer.push_file(reader, rel, header)?;
|
|
|
|
} else if typ.is_block_device() {
|
|
|
|
let device = metadata.dev();
|
|
|
|
writer.push_block_device(device as u32, rel, header)?;
|
|
|
|
} else if typ.is_char_device() {
|
|
|
|
let device = metadata.dev();
|
|
|
|
writer.push_char_device(device as u32, rel, header)?;
|
|
|
|
} else {
|
|
|
|
return Err(HyphaError::new("invalid file type"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fs::remove_dir_all(image_dir)?;
|
|
|
|
|
|
|
|
let squash_file_path = squash_file
|
|
|
|
.to_str()
|
|
|
|
.ok_or_else(|| HyphaError::new("failed to convert squashfs string"))?;
|
|
|
|
|
|
|
|
let mut out = File::create(squash_file)?;
|
2024-01-18 08:02:21 +00:00
|
|
|
trace!("ImageCompiler squash generate: {}", squash_file_path);
|
2024-01-17 22:29:05 +00:00
|
|
|
writer.write(&mut out)?;
|
2024-01-18 08:02:21 +00:00
|
|
|
Ok(())
|
2024-01-17 22:29:05 +00:00
|
|
|
}
|
|
|
|
}
|