use anyhow::{Result, anyhow};
use shared::{Service, ServiceSource};
#[derive(Debug, Clone, PartialEq)]
pub enum Source {
Database(super::database::DbTarget),
Named(String),
Bind(String),
}
#[derive(Debug, Clone, PartialEq)]
pub struct Part {
pub name: String,
pub source: Source,
}
const BIND_DENYLIST: &[&str] = &[
"/etc/rustploy",
"/var/lib/rustploy",
"/var/run",
"/run",
"/proc",
"/sys",
"/dev",
"/root",
"/boot",
];
pub fn bind_allowed(path: &str) -> bool {
if !path.starts_with('/') || path.split('/').any(|c| c == "..") {
return false;
}
let p = path.trim_end_matches('/');
if p.is_empty() {
return false;
}
!BIND_DENYLIST
.iter()
.any(|d| p == *d || p.starts_with(&format!("{d}/")))
&& !BIND_DENYLIST.iter().any(|d| d.starts_with(&format!("{p}/")))
}
pub fn logical_name(compose_project: &str, volume: &str) -> String {
volume
.strip_prefix(&format!("{compose_project}_"))
.filter(|s| !s.is_empty())
.unwrap_or(volume)
.to_string()
}
fn bind_name(container_path: &str) -> String {
format!("bind:{container_path}")
}
pub fn synthesize(svc: &Service, name: &str) -> Option<Part> {
if let Some(dest) = name.strip_prefix("bind:") {
return svc
.spec
.volumes
.iter()
.find(|v| !v.read_only && v.container_path == dest && v.host_path.starts_with('/'))
.filter(|v| bind_allowed(&v.host_path))
.map(|v| Part {
name: name.to_string(),
source: Source::Bind(v.host_path.clone()),
});
}
if matches!(svc.spec.source, ServiceSource::Compose(_)) {
return Some(Part {
name: name.to_string(),
source: Source::Named(format!("{}_{name}", svc.compose_project_name())),
});
}
svc.spec
.volumes
.iter()
.any(|v| !v.read_only && v.host_path == name)
.then(|| Part {
name: name.to_string(),
source: Source::Named(name.to_string()),
})
}
pub async fn resolve(docker: &crate::engine::DockerClient, svc: &Service) -> Result<Vec<Part>> {
if super::database::engine_of(svc).is_some() {
let t = super::database::target(docker, svc).await?;
return Ok(vec![Part {
name: format!("db:{}", t.db),
source: Source::Database(t),
}]);
}
let mut parts: Vec<Part> = Vec::new();
if matches!(svc.spec.source, ServiceSource::Compose(_)) {
let project = svc.compose_project_name();
for vol in list_project_volumes(&project, &svc.id).await? {
parts.push(Part {
name: logical_name(&project, &vol),
source: Source::Named(vol),
});
}
}
for v in svc.spec.volumes.iter().filter(|v| !v.read_only) {
if v.host_path.starts_with('/') {
if bind_allowed(&v.host_path) {
parts.push(Part {
name: bind_name(&v.container_path),
source: Source::Bind(v.host_path.clone()),
});
}
} else if !v.host_path.is_empty() {
parts.push(Part {
name: v.host_path.clone(),
source: Source::Named(v.host_path.clone()),
});
}
}
parts.sort_by(|a, b| a.name.cmp(&b.name));
parts.dedup_by(|a, b| a.name == b.name);
Ok(parts)
}
pub const SERVICE_LABEL: &str = "rustploy.service_id";
async fn list_project_volumes(project: &str, service_id: &str) -> Result<Vec<String>> {
let mut found = volume_ls(&format!("label=com.docker.compose.project={project}")).await?;
let extra = [
volume_ls(&format!("label={SERVICE_LABEL}={service_id}")).await?,
mounted_by_stack(project).await?,
];
for vol in extra.into_iter().flatten() {
if !found.contains(&vol) {
found.push(vol);
}
}
Ok(found)
}
async fn mounted_by_stack(project: &str) -> Result<Vec<String>> {
let ids = engine_lines(&[
"ps",
"-a",
"-q",
"--filter",
&format!("label=com.docker.compose.project={project}"),
])
.await?;
let mut out = Vec::new();
for id in ids {
let names = engine_lines(&[
"inspect",
"--format",
"{{range .Mounts}}{{if eq .Type \"volume\"}}{{.Name}}\n{{end}}{{end}}",
&id,
])
.await?;
for n in names {
if !out.contains(&n) {
out.push(n);
}
}
}
Ok(out)
}
async fn engine_lines(args: &[&str]) -> Result<Vec<String>> {
let out = crate::engine::cli()
.args(args)
.output()
.await
.map_err(|e| anyhow!("falha ao consultar o motor: {e}"))?;
if !out.status.success() {
return Err(anyhow!(
"consultar o motor falhou: {}",
String::from_utf8_lossy(&out.stderr).trim()
));
}
Ok(String::from_utf8_lossy(&out.stdout)
.lines()
.map(str::trim)
.filter(|l| !l.is_empty())
.map(String::from)
.collect())
}
async fn volume_ls(filter: &str) -> Result<Vec<String>> {
engine_lines(&["volume", "ls", "-q", "--filter", filter]).await
}
pub async fn create_stack_volume(service_id: &str, project: &str, logical: &str) {
let name = format!("{project}_{logical}");
let _ = crate::engine::cli()
.args([
"volume",
"create",
"--label",
&format!("{SERVICE_LABEL}={service_id}"),
"--label",
&format!("com.docker.compose.project={project}"),
"--label",
&format!("com.docker.compose.volume={logical}"),
&name,
])
.output()
.await;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn bind_recusa_o_que_e_do_rustploy_ou_do_sistema() {
assert!(bind_allowed("/srv/uploads"));
assert!(bind_allowed("/var/lib/outra-coisa"));
for ruim in [
"/",
"",
"relativo",
"/srv/../etc",
"/etc/rustploy",
"/etc/rustploy/age.key",
"/var/lib/rustploy/db",
"/var/lib",
"/etc",
"/root/.ssh",
"/proc/1",
] {
assert!(!bind_allowed(ruim), "{ruim} deveria ser recusado");
}
}
#[test]
fn nome_logico_tira_o_prefixo_da_stack() {
assert_eq!(logical_name("rp_abc_db", "rp_abc_db_pgdata"), "pgdata");
assert_eq!(logical_name("rp_abc_db", "outro"), "outro");
assert_eq!(logical_name("rp_abc_db", "rp_abc_db_"), "rp_abc_db_");
}
}
#[cfg(test)]
mod synth_tests {
use super::*;
use shared::{ComposeSource, VolumeMount};
fn svc(source: ServiceSource, volumes: Vec<VolumeMount>) -> Service {
let mut s = crate::features::wizard::app_spec("svc".into(), "p1".into());
s.source = source;
s.volumes = volumes;
Service {
id: "svc_1".into(),
spec: s,
compose_project: Some("minha_stack".into()),
status: shared::ServiceStatus::Stopped,
live_container_id: None,
created_at: chrono::Utc::now(),
updated_at: chrono::Utc::now(),
}
}
fn vol(host: &str, dest: &str, ro: bool) -> VolumeMount {
VolumeMount {
host_path: host.into(),
container_path: dest.into(),
read_only: ro,
}
}
#[test]
fn volume_nomeado_so_se_o_spec_o_declara() {
let s = svc(
ServiceSource::Registry { image: "x".into() },
vec![vol("pgdata", "/data", false), vol("cache", "/c", true)],
);
assert_eq!(
synthesize(&s, "pgdata").unwrap().source,
Source::Named("pgdata".into())
);
assert!(
synthesize(&s, "cache").is_none(),
"somente leitura não entra"
);
assert!(
synthesize(&s, "invento").is_none(),
"nunca inventa um volume"
);
}
#[test]
fn compose_usa_o_prefixo_da_stack() {
let s = svc(ServiceSource::Compose(ComposeSource::default()), vec![]);
assert_eq!(
synthesize(&s, "dados").unwrap().source,
Source::Named("minha_stack_dados".into())
);
}
#[test]
fn bind_so_se_o_spec_monta_e_fora_da_denylist() {
let s = svc(
ServiceSource::Registry { image: "x".into() },
vec![
vol("/srv/uploads", "/app/up", false),
vol("/etc/rustploy", "/x", false),
],
);
assert_eq!(
synthesize(&s, "bind:/app/up").unwrap().source,
Source::Bind("/srv/uploads".into())
);
assert!(synthesize(&s, "bind:/x").is_none(), "denylist");
assert!(synthesize(&s, "bind:/outro").is_none());
}
}