Skip to content
Open
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
11 changes: 11 additions & 0 deletions Cargo.lock

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

28 changes: 26 additions & 2 deletions crates/cli/lib/commands/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::time::Instant;
use clap::{Args, Subcommand};
use console::style;
use microsandbox::image::Image;
use microsandbox_image::{PullOptions, Registry};

use crate::ui;

Expand Down Expand Up @@ -91,6 +92,8 @@ pub async fn run(args: ImageArgs) -> anyhow::Result<()> {
args.reference,
args.force,
args.quiet,
args.insecure,
args.ca_certs,
microsandbox_image::PullPolicy::Always,
)
.await
Expand All @@ -107,6 +110,8 @@ pub async fn run_pull(args: pull::PullArgs) -> anyhow::Result<()> {
args.reference,
args.force,
args.quiet,
args.insecure,
args.ca_certs,
microsandbox_image::PullPolicy::Always,
)
.await
Expand All @@ -117,6 +122,8 @@ async fn run_pull_inner(
reference: String,
force: bool,
quiet: bool,
insecure: bool,
cli_ca_certs: Option<String>,
pull_policy: microsandbox_image::PullPolicy,
) -> anyhow::Result<()> {
let start = Instant::now();
Expand All @@ -129,9 +136,24 @@ async fn run_pull_inner(
.map_err(|e| anyhow::anyhow!("invalid image reference: {e}"))?;

let auth = global.resolve_registry_auth(image_ref.registry())?;
let registry = microsandbox_image::Registry::with_auth(platform, cache, auth)?;
let mut ca_certs = global.resolve_ca_certs().await?;
if let Some(path) = &cli_ca_certs {
let data = tokio::fs::read(path)
.await
.map_err(|e| anyhow::anyhow!("failed to read CA certs from `{path}`: {e}"))?;
ca_certs.push(data);
}
let mut insecure_registries = global.insecure_registries();
if insecure {
insecure_registries.push(image_ref.registry().to_string());
}
let registry = Registry::builder(platform, cache)
.auth(auth)
.extra_ca_certs(ca_certs)
.add_insecure_registries(insecure_registries)
.build()?;

let options = microsandbox_image::PullOptions {
let options = PullOptions {
pull_policy,
force,
..Default::default()
Expand Down Expand Up @@ -218,6 +240,8 @@ pub(crate) async fn pull_if_missing(reference: &str, quiet: bool) -> anyhow::Res
reference.to_string(),
false,
quiet,
false,
None,
microsandbox_image::PullPolicy::IfMissing,
)
.await
Expand Down
8 changes: 8 additions & 0 deletions crates/cli/lib/commands/pull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ pub struct PullArgs {
/// Suppress progress output.
#[arg(short, long)]
pub quiet: bool,

/// Connect to the registry over plain HTTP instead of HTTPS.
#[arg(long)]
pub insecure: bool,

/// Path to a PEM file containing additional CA root certificates to trust.
#[arg(long, value_name = "PATH")]
pub ca_certs: Option<String>,
}
38 changes: 26 additions & 12 deletions crates/cli/lib/commands/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,17 @@ fn run_login(args: RegistryLoginArgs) -> anyhow::Result<()> {
})?;

let mut config = load_persisted_config_or_default()?;
config.registries.auth.insert(
args.registry.clone(),
RegistryAuthEntry {
username: args.username,
store: Some(RegistryCredentialStore::Keyring),
password_env: None,
secret_name: None,
},
);
config
.registries
.hosts
.entry(args.registry.clone())
.or_default()
.auth = Some(RegistryAuthEntry {
username: args.username,
store: Some(RegistryCredentialStore::Keyring),
password_env: None,
secret_name: None,
});

if let Err(error) = save_persisted_config(&config) {
let restore = match previous_auth {
Expand Down Expand Up @@ -130,7 +132,12 @@ fn run_login(args: RegistryLoginArgs) -> anyhow::Result<()> {
fn run_logout(args: RegistryLogoutArgs) -> anyhow::Result<()> {
let mut config = load_persisted_config_or_default()?;
let previous_auth = get_registry_keyring_auth(&args.registry).ok().flatten();
let had_config_entry = config.registries.auth.remove(&args.registry).is_some();
let had_config_entry = config
.registries
.hosts
.get_mut(&args.registry)
.and_then(|e| e.auth.take())
.is_some();
let had_keyring_entry = previous_auth.is_some();

if !had_config_entry && !had_keyring_entry {
Expand All @@ -156,12 +163,19 @@ fn run_logout(args: RegistryLogoutArgs) -> anyhow::Result<()> {

fn run_list(_args: RegistryListArgs) -> anyhow::Result<()> {
let config = load_persisted_config_or_default()?;
if config.registries.auth.is_empty() {
let auth_entries: Vec<_> = config
.registries
.hosts
.iter()
.filter_map(|(hostname, entry)| entry.auth.as_ref().map(|auth| (hostname, auth)))
.collect();

if auth_entries.is_empty() {
println!("No registries configured.");
return Ok(());
}

let mut entries: Vec<_> = config.registries.auth.iter().collect();
let mut entries: Vec<_> = auth_entries;
entries.sort_by(|(left, _), (right, _)| left.cmp(right));

let mut table = ui::Table::new(&["REGISTRY", "USERNAME", "SOURCE"]);
Expand Down
2 changes: 2 additions & 0 deletions crates/image/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ hex.workspace = true
libc.workspace = true
microsandbox-utils = { version = "0.3.12", path = "../utils" }
oci-client.workspace = true
rustls-pemfile.workspace = true
oci-spec.workspace = true
scopeguard.workspace = true
serde.workspace = true
Expand All @@ -30,5 +31,6 @@ tracing.workspace = true
xattr.workspace = true

[dev-dependencies]
rcgen.workspace = true
tar.workspace = true
tempfile.workspace = true
2 changes: 1 addition & 1 deletion crates/image/lib/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
/// Authentication credentials for OCI registry access.
///
/// Resolution chain (in [`Registry`](crate::Registry)):
/// 1. Explicit [`RegistryAuth`] via [`Registry::with_auth()`](crate::Registry::with_auth)
/// 1. Explicit [`RegistryAuth`] via [`RegistryBuilder::auth()`](crate::RegistryBuilder::auth)
/// 2. OS keyring / credential store (when configured by the caller)
/// 3. Global config `registries.auth` (`store`, `password_env`, or `secret_name`)
/// 4. Docker credential store/config fallback (when enabled by the caller)
Expand Down
4 changes: 4 additions & 0 deletions crates/image/lib/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ pub enum ImageError {
reference: String,
},

/// Invalid PEM certificate data.
#[error("invalid PEM certificate: {0}")]
InvalidCertificate(String),

/// General I/O error.
#[error(transparent)]
Io(#[from] std::io::Error),
Expand Down
2 changes: 1 addition & 1 deletion crates/image/lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ pub use oci_client::Reference;
pub use platform::{Arch, Os, Platform};
pub use progress::{PullProgress, PullProgressHandle, PullProgressSender, progress_channel};
pub use pull::{PullOptions, PullPolicy, PullResult};
pub use registry::Registry;
pub use registry::{Registry, RegistryBuilder};
pub use store::{CachedImageMetadata, CachedLayerMetadata, GlobalCache};
Loading
Loading