126 lines
3.8 KiB
Rust
126 lines
3.8 KiB
Rust
use std::{env, path::Path};
|
|
|
|
const OPT_IN: &str = "WIREGUARD_ADMIN_TESTCONTAINERS";
|
|
const SELECTED_ENDPOINT: &str = "WIREGUARD_ADMIN_TEST_DAEMON_ENDPOINT";
|
|
const SELECTED_ID: &str = "WIREGUARD_ADMIN_TEST_DAEMON_ID";
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct Selection {
|
|
pub endpoint: String,
|
|
pub daemon_id: String,
|
|
}
|
|
|
|
impl Selection {
|
|
pub fn from_process_environment() -> Result<Self, &'static str> {
|
|
Self::from_values(
|
|
env::var(OPT_IN).ok().as_deref(),
|
|
env::var(SELECTED_ENDPOINT).ok().as_deref(),
|
|
env::var(SELECTED_ID).ok().as_deref(),
|
|
env::var("DOCKER_HOST").ok().as_deref(),
|
|
env::var("TESTCONTAINERS_COMMAND").ok().as_deref(),
|
|
)
|
|
}
|
|
|
|
fn from_values(
|
|
opt_in: Option<&str>,
|
|
endpoint: Option<&str>,
|
|
daemon_id: Option<&str>,
|
|
docker_host: Option<&str>,
|
|
testcontainers_command: Option<&str>,
|
|
) -> Result<Self, &'static str> {
|
|
if opt_in != Some("1") {
|
|
return Err("explicit opt-in is missing");
|
|
}
|
|
if testcontainers_command == Some("keep") {
|
|
return Err("Testcontainers keep mode is forbidden");
|
|
}
|
|
|
|
let endpoint = endpoint.ok_or("selected daemon endpoint is missing")?;
|
|
if !endpoint.starts_with("unix:///")
|
|
|| endpoint.contains(['\n', '\r', '\0'])
|
|
|| !Path::new(endpoint.trim_start_matches("unix://")).is_absolute()
|
|
{
|
|
return Err("only an explicitly selected local Unix socket is supported");
|
|
}
|
|
if docker_host != Some(endpoint) {
|
|
return Err("DOCKER_HOST must exactly match the selected endpoint");
|
|
}
|
|
|
|
let daemon_id = daemon_id.ok_or("selected daemon ID is missing")?;
|
|
if daemon_id.is_empty()
|
|
|| daemon_id.len() > 128
|
|
|| !daemon_id
|
|
.bytes()
|
|
.all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
|
|
{
|
|
return Err("selected daemon ID has invalid syntax");
|
|
}
|
|
|
|
Ok(Self {
|
|
endpoint: endpoint.to_owned(),
|
|
daemon_id: daemon_id.to_owned(),
|
|
})
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::Selection;
|
|
|
|
const ENDPOINT: &str = "unix:///var/run/docker.sock";
|
|
const ID: &str = "synthetic-daemon-id";
|
|
|
|
fn selection(
|
|
opt_in: Option<&str>,
|
|
endpoint: Option<&str>,
|
|
id: Option<&str>,
|
|
docker_host: Option<&str>,
|
|
keep: Option<&str>,
|
|
) -> Result<Selection, &'static str> {
|
|
Selection::from_values(opt_in, endpoint, id, docker_host, keep)
|
|
}
|
|
|
|
#[test]
|
|
fn requires_explicit_opt_in_endpoint_and_matching_current_daemon_identity() {
|
|
assert!(selection(None, Some(ENDPOINT), Some(ID), Some(ENDPOINT), None).is_err());
|
|
assert!(selection(Some("1"), None, Some(ID), Some(ENDPOINT), None).is_err());
|
|
assert!(selection(Some("1"), Some(ENDPOINT), None, Some(ENDPOINT), None).is_err());
|
|
assert!(
|
|
selection(
|
|
Some("1"),
|
|
Some(ENDPOINT),
|
|
Some(ID),
|
|
Some("tcp://remote:2376"),
|
|
None
|
|
)
|
|
.is_err()
|
|
);
|
|
assert!(
|
|
selection(
|
|
Some("1"),
|
|
Some("tcp://remote:2376"),
|
|
Some(ID),
|
|
Some("tcp://remote:2376"),
|
|
None
|
|
)
|
|
.is_err()
|
|
);
|
|
assert!(
|
|
selection(
|
|
Some("1"),
|
|
Some(ENDPOINT),
|
|
Some(ID),
|
|
Some(ENDPOINT),
|
|
Some("keep")
|
|
)
|
|
.is_err()
|
|
);
|
|
assert_eq!(
|
|
selection(Some("1"), Some(ENDPOINT), Some(ID), Some(ENDPOINT), None)
|
|
.expect("valid explicit local daemon")
|
|
.daemon_id,
|
|
ID
|
|
);
|
|
}
|
|
}
|