use super::verification::{parse_signature, require_signature};
use super::{CliCommand, Invocation, chart_path, flags, oci_host, registry_auth};
use crate::command::{ChartVerification, Pull, Pulled, Push, RegistryArtifact, Verify};
use crate::error::{Error, Result};
#[cfg(test)]
mod tests;
fn artifacts(command: &'static str, stdout: &str, verb: &str) -> Result<Vec<RegistryArtifact>> {
let mut lines = stdout.lines();
let mut found = Vec::new();
while let Some(line) = lines.next() {
let Some(reference) = line.strip_prefix(verb) else {
continue;
};
let digest = lines
.next()
.and_then(|next| next.strip_prefix("Digest: "))
.ok_or_else(|| Error::Parse {
command,
message: format!("no \"Digest: \" line after {line:?}"),
})?;
found.push(RegistryArtifact {
reference: reference.to_owned(),
digest: digest.to_owned(),
});
}
Ok(found)
}
impl CliCommand for Pull {
fn build(&self, inv: &mut Invocation) -> Result<()> {
inv.arg("pull");
inv.arg(self.chart.as_os_str());
chart_path(inv, &self.chart, &self.chart_path)?;
flags! { inv, self;
switch("--devel", devel);
switch("--untar", untar);
option("--untardir", untardir);
option("--destination", destination);
switch("--prov", prov);
}
Ok(())
}
fn parse(&self, stdout: String) -> Result<Pulled> {
let artifacts = artifacts("helm pull", &stdout, "Pulled: ")?;
let verification = parse_signature("helm pull", &stdout, "Chart Hash Verified: ")?.map(
|(signed_by, fingerprint, hash)| ChartVerification {
signed_by,
fingerprint,
hash,
},
);
let warnings = stdout
.lines()
.filter(|line| line.starts_with("WARNING:"))
.map(str::to_owned)
.collect();
Ok(Pulled {
artifacts,
verification,
warnings,
})
}
}
impl CliCommand for Push {
fn build(&self, inv: &mut Invocation) -> Result<()> {
let mut username = self.username.as_deref();
inv.arg("push");
inv.arg(&self.chart);
inv.arg(&self.remote);
if let Some(password) = &self.password {
let user = username.take().ok_or_else(|| {
Error::InvalidCommand("a registry password needs a username".to_owned())
})?;
registry_auth(inv, &[oci_host(&self.remote)], user, password)?;
}
inv.option("--username", username);
flags! { inv, self;
option("--cert-file", cert_file);
option("--key-file", key_file);
option("--ca-file", ca_file);
switch("--insecure-skip-tls-verify", insecure_skip_tls_verify);
switch("--plain-http", plain_http);
}
Ok(())
}
fn parse(&self, stdout: String) -> Result<RegistryArtifact> {
artifacts("helm push", &stdout, "Pushed: ")?
.into_iter()
.next()
.ok_or_else(|| Error::Parse {
command: "helm push",
message: format!("no \"Pushed: \" line in {stdout:?}"),
})
}
}
impl CliCommand for Verify {
fn build(&self, inv: &mut Invocation) -> Result<()> {
inv.arg("verify");
inv.arg(&self.path);
flags! { inv, self;
option("--keyring", keyring);
}
Ok(())
}
fn parse(&self, stdout: String) -> Result<ChartVerification> {
let (signed_by, fingerprint, hash) =
require_signature("helm verify", &stdout, "Chart Hash Verified: ")?;
Ok(ChartVerification {
signed_by,
fingerprint,
hash,
})
}
}