use std::collections::{BTreeMap, BTreeSet};
use std::ops::RangeInclusive;
use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};
use super::claim::{self, Claim, Mode};
use crate::devsbd::brokerfile::{self, BrokerSpec};
pub const PORTS: RangeInclusive<u16> = 29000..=29999;
pub type Found = Vec<(String, Claim)>;
const PHANTOM_RANDOM: usize = 24;
const BASE62: &[u8] = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct InstanceClaim {
pub kind: String,
pub feature: String,
pub version: String,
pub claim: String,
pub phantom: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub listeners: Vec<Listener>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub files: Vec<File>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub git_hosts: Vec<String>,
}
pub fn git_hosts(claims: &[InstanceClaim]) -> Vec<String> {
claims.iter().flat_map(|c| c.git_hosts.iter().cloned()).collect()
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Listener {
pub port: u16,
pub upstream: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Intercept {
pub port: u16,
pub hosts: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub git: Option<ProxyPort>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub lfs: Option<u16>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub proxy_env: Vec<(String, String)>,
pub bundle: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub installed: Option<String>,
}
impl Intercept {
pub fn bound_hosts(&self) -> Vec<String> {
let git = self.git.iter().flat_map(|g| g.hosts.iter());
self.hosts.iter().chain(git).cloned().collect()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ProxyPort {
pub port: u16,
pub hosts: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Planned {
pub claims: Vec<InstanceClaim>,
pub proxy: Option<ProxyPort>,
pub git: Option<ProxyPort>,
pub lfs: Option<u16>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct File {
pub path: String,
pub owner: String,
pub content: String,
}
pub fn by_phantom<'a>(claims: &'a [InstanceClaim], phantom: &str) -> Option<&'a InstanceClaim> {
claims.iter().find(|c| c.phantom == phantom)
}
pub fn by_port(claims: &[InstanceClaim], port: u16) -> Option<(&InstanceClaim, &str)> {
claims
.iter()
.find_map(|c| c.listeners.iter().find(|l| l.port == port).map(|l| (c, l.upstream.as_str())))
}
pub fn ports<'a>(claims: &'a [InstanceClaim], intercept: Option<&'a Intercept>) -> impl Iterator<Item = u16> + 'a {
let proxies =
intercept.into_iter().flat_map(|i| std::iter::once(i.port).chain(i.git.as_ref().map(|g| g.port)).chain(i.lfs));
claims.iter().flat_map(|c| c.listeners.iter().map(|l| l.port)).chain(proxies)
}
pub fn forward_refusal(claims: &[InstanceClaim], intercept: Option<&Intercept>, key: &str, port: u16) -> Option<String> {
if intercept.is_some_and(|i| i.port == port) {
return Some(format!(
"port {port} of `{key}` is its credential broker proxy port, which only the sandbox itself may reach"
));
}
if intercept.and_then(|i| i.git.as_ref()).is_some_and(|g| g.port == port) {
return Some(format!(
"port {port} of `{key}` is git's credential broker proxy port, which only the sandbox itself may reach"
));
}
if intercept.and_then(|i| i.lfs) == Some(port) {
return Some(format!(
"port {port} of `{key}` is its credential broker LFS listener, which only the sandbox itself may reach"
));
}
let (claim, _) = by_port(claims, port)?;
Some(format!(
"port {port} of `{key}` is its `{}` credential broker listener, which only the sandbox itself may reach",
claim.kind
))
}
pub fn broker_spec(claims: &[InstanceClaim], intercept: Option<&Intercept>) -> Option<BrokerSpec> {
if claims.is_empty() {
return None;
}
let lfs = intercept.and_then(|i| i.lfs).map(|port| brokerfile::Listener { port, mode: brokerfile::Mode::Lfs });
let listeners = claims
.iter()
.flat_map(|c| &c.listeners)
.map(|l| brokerfile::Listener {
port: l.port,
mode: brokerfile::Mode::BaseUrl { upstream: l.upstream.clone() },
})
.chain(lfs)
.collect();
let files = claims
.iter()
.flat_map(|c| c.files.iter().map(move |f| (c, f)))
.map(|(c, f)| brokerfile::PhantomFile {
owner: f.owner.clone(),
path: f.path.clone(),
phantom: c.phantom.clone(),
content: f.content.clone(),
})
.collect();
let (proxy_listeners, proxy_env) = match intercept {
Some(i) => {
let shared = brokerfile::ProxyListener { port: i.port, hosts: i.hosts.clone() };
let git = i.git.iter().map(|g| brokerfile::ProxyListener { port: g.port, hosts: g.hosts.clone() });
(std::iter::once(shared).chain(git).collect(), i.proxy_env.clone())
}
None => (Vec::new(), Vec::new()),
};
let credentials = claims
.iter()
.flat_map(|c| c.git_hosts.iter().map(move |h| brokerfile::Credential { host: h.clone(), phantom: c.phantom.clone() }))
.collect();
Some(BrokerSpec { listeners, proxy_listeners, files, proxy_env, credentials })
}
pub fn select(found: Found) -> Result<Found> {
let mut seen: Vec<(String, String)> = Vec::new();
let mut intercepted: BTreeMap<String, String> = BTreeMap::new();
for (feature, claim) in &found {
if let Some((_, first)) = seen.iter().find(|(kind, _)| *kind == claim.kind) {
bail!(
"features `{first}` and `{feature}` both claim the `{}` credential: one feature per sandbox may",
claim.kind
);
}
seen.push((claim.kind.clone(), feature.clone()));
if claim.pinned.mode != Mode::Intercept {
continue;
}
for u in &claim.pinned.upstreams {
if let Some(first) = intercepted.insert(u.host.clone(), feature.clone()) {
bail!(
"features `{first}` and `{feature}` both intercept `{}`: one intercept claim per upstream in a sandbox",
u.host
);
}
}
}
Ok(found)
}
fn phantom_head(claim: &Claim) -> String {
match &claim.wiring.phantom.prefix {
Some(prefix) => prefix.clone(),
None => format!("dsbx_{}_", claim.kind),
}
}
pub fn mint_phantom(claim: &Claim) -> Result<String> {
Ok(format!("{}{}", phantom_head(claim), random_base62(PHANTOM_RANDOM)?))
}
pub fn mint_incarnation() -> Result<String> {
random_base62(PHANTOM_RANDOM)
}
pub(crate) fn random_base62(len: usize) -> Result<String> {
let mut out = String::with_capacity(len);
let mut buf = [0u8; 64];
while out.len() < len {
getrandom::fill(&mut buf).map_err(|e| anyhow::anyhow!("cannot read the OS random source: {e}"))?;
for b in buf.iter().filter(|&&b| b < 248) {
if out.len() == len {
break;
}
out.push(BASE62[(b % 62) as usize] as char);
}
}
Ok(out)
}
pub fn plan(
selected: &[(String, Claim)],
prior: &[InstanceClaim],
prior_port: Option<u16>,
prior_git_port: Option<u16>,
prior_lfs_port: Option<u16>,
mut mint: impl FnMut(&Claim) -> Result<String>,
) -> Result<Planned> {
let mut taken: BTreeSet<u16> = BTreeSet::new();
let mut out = Vec::new();
for (feature, claim) in selected {
let before = prior.iter().find(|p| p.kind == claim.kind);
let phantom = match before.filter(|p| p.phantom.starts_with(&phantom_head(claim))) {
Some(p) => p.phantom.clone(),
None => mint(claim)?,
};
let listened = if claim.pinned.mode == Mode::BaseUrl { claim.pinned.upstreams.as_slice() } else { &[] };
let listeners = listened
.iter()
.map(|u| {
let kept = before.and_then(|p| p.listeners.iter().find(|l| l.upstream == u.host)).map(|l| l.port);
let port = kept.filter(|p| taken.insert(*p)).unwrap_or(0);
Listener { port, upstream: u.host.clone() }
})
.collect();
out.push(InstanceClaim {
kind: claim.kind.clone(),
feature: feature.clone(),
version: claim.version(),
claim: claim.canonical(),
phantom,
listeners,
files: Vec::new(),
git_hosts: claim.wiring.git.as_ref().map(|g| g.hosts.clone()).unwrap_or_default(),
});
}
let intercepting: Vec<&Claim> =
selected.iter().map(|(_, c)| c).filter(|c| c.pinned.mode == Mode::Intercept).collect();
let git_of = |c: &Claim| c.wiring.git.as_ref().map(|g| g.hosts.clone()).unwrap_or_default();
let hosts: BTreeSet<String> = intercepting
.iter()
.flat_map(|c| {
let git = git_of(c);
c.pinned.upstreams.iter().filter(move |u| !git.contains(&u.host)).map(|u| u.host.clone())
})
.collect();
let git_hosts: BTreeSet<String> = intercepting.iter().flat_map(|c| git_of(c)).collect();
let kept_port = prior_port.filter(|p| !intercepting.is_empty() && PORTS.contains(p) && taken.insert(*p));
let kept_git = prior_git_port.filter(|p| !git_hosts.is_empty() && PORTS.contains(p) && taken.insert(*p));
let lfs_wanted = intercepting.iter().any(|c| c.pinned.lfs.is_some());
let kept_lfs = prior_lfs_port.filter(|p| lfs_wanted && PORTS.contains(p) && taken.insert(*p));
let mut free = || -> Result<u16> {
let port = PORTS.clone().find(|p| !taken.contains(p)).context("out of credential broker ports")?;
taken.insert(port);
Ok(port)
};
for l in out.iter_mut().flat_map(|c| c.listeners.iter_mut()).filter(|l| l.port == 0) {
l.port = free()?;
}
let proxy = match (intercepting.is_empty(), kept_port) {
(true, _) => None,
(false, Some(port)) => Some(ProxyPort { port, hosts: hosts.into_iter().collect() }),
(false, None) => Some(ProxyPort { port: free()?, hosts: hosts.into_iter().collect() }),
};
let git = match (git_hosts.is_empty(), kept_git) {
(true, _) => None,
(false, Some(port)) => Some(ProxyPort { port, hosts: git_hosts.into_iter().collect() }),
(false, None) => Some(ProxyPort { port: free()?, hosts: git_hosts.into_iter().collect() }),
};
let lfs = match (lfs_wanted, kept_lfs) {
(false, _) => None,
(true, Some(port)) => Some(port),
(true, None) => Some(free()?),
};
Ok(Planned { claims: out, proxy, git, lfs })
}
pub const PROXY_VARS: [&str; 8] =
["HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", "ALL_PROXY", "all_proxy", "NO_PROXY", "no_proxy"];
pub const CA_VARS: [&str; 4] = ["NODE_EXTRA_CA_CERTS", "DENO_CERT", "REQUESTS_CA_BUNDLE", "AWS_CA_BUNDLE"];
const LOOPBACK: [&str; 3] = ["localhost", "127.0.0.1", "::1"];
pub fn original_proxy_env<'a>(layers: impl IntoIterator<Item = (&'a String, &'a String)>) -> Vec<(String, String)> {
let mut last: BTreeMap<&str, &str> = BTreeMap::new();
for (k, v) in layers {
if let Some(var) = PROXY_VARS.iter().find(|var| **var == k.as_str()) {
last.insert(var, v);
}
}
PROXY_VARS.iter().filter_map(|var| last.get(var).map(|v| (var.to_string(), v.to_string()))).collect()
}
pub fn no_proxy(original: &[(String, String)], bound: &[String], aliases: &[String]) -> String {
let theirs = original
.iter()
.filter(|(k, _)| k == "NO_PROXY" || k == "no_proxy")
.flat_map(|(_, v)| v.split(','))
.map(str::trim);
let mut out: Vec<&str> = Vec::new();
for entry in LOOPBACK.into_iter().chain(aliases.iter().map(String::as_str)).chain(theirs) {
if !entry.is_empty() && !out.contains(&entry) && !bound.iter().any(|h| exempts(entry, h)) {
out.push(entry);
}
}
out.join(",")
}
fn exempts(entry: &str, host: &str) -> bool {
let entry = entry.to_ascii_lowercase();
let name = match entry.rsplit_once(':') {
Some((name, port)) if !port.is_empty() && port.bytes().all(|b| b.is_ascii_digit()) => name,
_ => entry.as_str(),
};
let name = name.trim_start_matches('*').trim_start_matches('.').trim_end_matches('.');
name.is_empty() || host == name || host.strip_suffix(name).is_some_and(|rest| rest.ends_with('.'))
}
pub fn intercept_env(intercept: &Intercept, aliases: &[String]) -> Vec<(String, String)> {
let proxy = format!("http://127.0.0.1:{}", intercept.port);
let no_proxy = no_proxy(&intercept.proxy_env, &intercept.hosts, aliases);
let mut out = vec![
("HTTPS_PROXY".to_string(), proxy.clone()),
("https_proxy".to_string(), proxy),
("NO_PROXY".to_string(), no_proxy.clone()),
("no_proxy".to_string(), no_proxy),
("NODE_USE_ENV_PROXY".to_string(), "1".to_string()),
];
out.extend(CA_VARS.iter().map(|v| (v.to_string(), intercept.bundle.clone())));
out
}
pub fn proxy_overrides<'a>(keys: impl IntoIterator<Item = &'a String>) -> Vec<&'a str> {
let routed = ["HTTPS_PROXY", "https_proxy", "NO_PROXY", "no_proxy"];
keys.into_iter().map(String::as_str).filter(|k| routed.contains(k)).collect()
}
pub fn base_url(claim: &Claim, record: &InstanceClaim) -> Option<String> {
let wanted = &claim.wiring.base_url.as_ref()?.upstream;
let l = record.listeners.iter().find(|l| l.upstream == *wanted)?;
Some(format!("http://127.0.0.1:{}", l.port))
}
pub fn env(claim: &Claim, record: &InstanceClaim) -> Vec<(String, String)> {
let mut out: Vec<(String, String)> =
claim.wiring.phantom.env.iter().map(|name| (name.clone(), record.phantom.clone())).collect();
if let (Some(b), Some(url)) = (&claim.wiring.base_url, base_url(claim, record)) {
out.push((b.env.clone(), url));
}
out
}
pub fn render_files(
claim: &Claim,
record: &InstanceClaim,
home: &str,
owner: &str,
targets: &[String],
) -> (Vec<File>, Vec<String>) {
let base_url = base_url(claim, record).unwrap_or_default();
let mut files = Vec::new();
let mut warnings = Vec::new();
for f in &claim.wiring.phantom.files {
let path = format!("{}/{}", home.trim_end_matches('/'), f.path);
if let Some(target) = targets.iter().find(|t| covers(t, &path)) {
let env = match claim.wiring.phantom.env.as_slice() {
[] => String::new(),
vars => format!(
"; {} still holds the phantom, so a tool reading it first goes through the broker anyway",
vars.iter().map(|v| format!("`{v}`")).collect::<Vec<_>>().join(", ")
),
};
warnings.push(format!(
"feature `{}`: `{path}` is under mount `{target}`, which keeps its own: the `{}` phantom file is \
skipped{env}",
record.feature, claim.kind
));
continue;
}
let content = f.template.replace(claim::BASE_URL, &base_url).replace(claim::PHANTOM, &record.phantom);
files.push(File { path, owner: owner.to_string(), content });
}
(files, warnings)
}
fn covers(target: &str, path: &str) -> bool {
let t = target.trim_end_matches('/');
path == t || path.strip_prefix(t).is_some_and(|rest| rest.starts_with('/'))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::credentials::home::tests::{sentry, sentry_cli};
fn intercept(kind: &str) -> Claim {
intercept_of(kind, serde_json::json!(["api.github.com"]), None)
}
fn intercept_of(kind: &str, upstreams: serde_json::Value, git: Option<serde_json::Value>) -> Claim {
let mut v = serde_json::json!({
"upstreams": upstreams,
"mode": "intercept",
"inject": [{ "header": "Authorization", "format": "token {token}" }],
"phantom": { "env": [format!("{}_TOKEN", kind.to_uppercase())] },
"login": [{ "method": "paste" }],
});
if let Some(git) = git {
v["git"] = git;
}
claim::parse(kind, &v).unwrap()
}
fn vars(pairs: &[(&str, &str)]) -> Vec<(String, String)> {
pairs.iter().map(|(k, v)| (k.to_string(), v.to_string())).collect()
}
fn counter() -> impl FnMut(&Claim) -> Result<String> {
let mut n = 0;
move |c| {
n += 1;
Ok(format!("{}{n}", phantom_head(c)))
}
}
#[test]
fn select_refuses_a_second_claim_and_a_shared_intercepted_upstream() {
let found = vec![("./a".to_string(), sentry_cli()), ("./b".to_string(), sentry_cli())];
let err = select(found).unwrap_err().to_string();
assert!(err.contains("features `./a` and `./b` both claim the `sentry` credential"), "{err}");
let kept = select(vec![("./s".into(), sentry_cli()), ("./gh".into(), intercept("github"))]).unwrap();
assert_eq!(kept.iter().map(|(f, _)| f.as_str()).collect::<Vec<_>>(), ["./s", "./gh"]);
let other = intercept_of("ghe", serde_json::json!(["example.com", "api.github.com"]), None);
let err = select(vec![("./gh".into(), intercept("github")), ("./ghe".into(), other)]).unwrap_err().to_string();
assert!(err.contains("features `./gh` and `./ghe` both intercept `api.github.com`"), "{err}");
let base = sentry_cli();
let shared = intercept_of("x", serde_json::json!([base.pinned.upstreams[0].host.clone()]), None);
select(vec![("./s".into(), base), ("./x".into(), shared)]).unwrap();
}
#[test]
fn plan_allocates_ports_and_rebuild_keeps_phantoms_and_ports() {
let selected = vec![("./s".to_string(), sentry_cli())];
let planned = plan(&selected, &[], None, None, None, counter()).unwrap();
assert_eq!(planned.proxy, None);
let first = planned.claims;
let c = &first[0];
assert_eq!((c.kind.as_str(), c.phantom.as_str()), ("sentry", "sntrys_1"));
assert_eq!(c.version, sentry_cli().version());
let got: Vec<(u16, &str)> = c.listeners.iter().map(|l| (l.port, l.upstream.as_str())).collect();
assert_eq!(got, [(29000, "sentry.io"), (29001, "us.sentry.io"), (29002, "de.sentry.io")]);
let again = plan(&selected, &first, None, None, None, |_| panic!("kept")).unwrap().claims;
assert_eq!(again, first);
let mut prior = first.clone();
prior[0].listeners.retain(|l| l.upstream != "us.sentry.io");
let next = plan(&selected, &prior, None, None, None, counter()).unwrap().claims;
let got: Vec<(u16, &str)> = next[0].listeners.iter().map(|l| (l.port, l.upstream.as_str())).collect();
assert_eq!(got, [(29000, "sentry.io"), (29001, "us.sentry.io"), (29002, "de.sentry.io")]);
assert_eq!(next[0].phantom, "sntrys_1");
let mut odd = first.clone();
odd[0].phantom = "other_x".into();
assert_eq!(plan(&selected, &odd, None, None, None, counter()).unwrap().claims[0].phantom, "sntrys_1");
}
#[test]
fn intercept_claims_share_one_proxy_port_kept_across_rebuilds() {
let gh = intercept_of(
"github",
serde_json::json!(["api.github.com", "uploads.github.com", "github.com"]),
Some(serde_json::json!({ "hosts": ["github.com"] })),
);
let other = intercept_of("x", serde_json::json!(["api.x.io"]), None);
let selected = vec![("./s".to_string(), sentry_cli()), ("./gh".to_string(), gh.clone()), ("./x".to_string(), other)];
let first = plan(&selected, &[], None, None, None, counter()).unwrap();
assert!(first.claims[1].listeners.is_empty() && first.claims[2].listeners.is_empty());
assert_eq!(git_hosts(&first.claims), ["github.com"]);
assert!(first.claims[0].git_hosts.is_empty());
assert_eq!(
first.proxy,
Some(ProxyPort {
port: 29003,
hosts: vec!["api.github.com".into(), "api.x.io".into(), "uploads.github.com".into()]
})
);
assert_eq!(first.git, Some(ProxyPort { port: 29004, hosts: vec!["github.com".into()] }));
let mut prior = first.claims.clone();
prior[0].listeners.retain(|l| l.upstream != "de.sentry.io");
let next = plan(&selected, &prior, Some(29002), Some(29004), None, counter()).unwrap();
assert_eq!(next.proxy.as_ref().map(|p| p.port), Some(29002));
assert_eq!(next.git.as_ref().map(|p| p.port), Some(29004));
let added = next.claims[0].listeners.iter().find(|l| l.upstream == "de.sentry.io").unwrap();
assert_eq!(added.port, 29003);
let again = plan(&selected, &first.claims, Some(29003), Some(29004), None, counter()).unwrap();
assert_eq!((again.proxy, again.git), (first.proxy.clone(), first.git.clone()));
let upgraded = plan(&selected, &first.claims, Some(29003), None, None, counter()).unwrap();
assert_eq!(upgraded.git.map(|g| g.port), Some(29004));
let moved = plan(&selected, &first.claims, Some(29003), Some(80), None, counter()).unwrap();
assert_eq!(moved.git.map(|g| g.port), Some(29004));
let git_only = intercept_of("g", serde_json::json!(["github.com"]), Some(serde_json::json!({ "hosts": ["github.com"] })));
let planned = plan(&[("./g".into(), git_only)], &[], Some(29000), Some(29001), None, counter()).unwrap();
assert_eq!(planned.proxy, Some(ProxyPort { port: 29000, hosts: vec![] }));
assert_eq!(planned.git, Some(ProxyPort { port: 29001, hosts: vec!["github.com".into()] }));
assert_eq!(plan(&selected[2..], &[], None, Some(29001), None, counter()).unwrap().git, None);
}
#[test]
fn original_proxy_env_takes_each_variables_last_layer() {
let image = vars(&[("HTTPS_PROXY", "http://img:1"), ("PATH", "/bin"), ("no_proxy", "corp"), ("HTTPS_PROXY", "http://img:2")]);
let config: BTreeMap<String, String> =
[("HTTP_PROXY".to_string(), "http://cfg:3".to_string()), ("https_proxy".into(), "http://cfg:4".into())].into();
let layers = image.iter().map(|(k, v)| (k, v)).chain(config.iter());
assert_eq!(
original_proxy_env(layers),
vars(&[("HTTPS_PROXY", "http://img:2"), ("https_proxy", "http://cfg:4"), ("HTTP_PROXY", "http://cfg:3"), ("no_proxy", "corp")])
);
assert!(original_proxy_env(std::iter::empty()).is_empty());
}
#[test]
fn no_proxy_is_loopback_aliases_and_theirs_minus_bound_hosts() {
let bound = vec!["api.github.com".to_string(), "uploads.github.com".to_string()];
let original = vars(&[
("HTTPS_PROXY", "http://corp:3128"),
("NO_PROXY", "corp.lan, .github.com,db,api.github.com:443"),
("no_proxy", "*,10.0.0.0/8,localhost,github.com,xapi.github.com,*.GitHub.com"),
]);
assert_eq!(
no_proxy(&original, &bound, &["db".into(), "cache".into()]),
"localhost,127.0.0.1,::1,db,cache,corp.lan,10.0.0.0/8,xapi.github.com"
);
assert_eq!(no_proxy(&[], &bound, &[]), "localhost,127.0.0.1,::1");
assert_eq!(no_proxy(&[], &["localhost".into()], &["api".into()]), "127.0.0.1,::1,api");
}
#[test]
fn intercept_env_routes_https_and_names_the_bundle() {
let i = Intercept {
port: 29004,
hosts: vec!["api.github.com".into()],
git: None,
lfs: None,
proxy_env: vars(&[("https_proxy", "http://corp:3128"), ("NO_PROXY", "corp")]),
bundle: "/etc/pki/tls/certs/ca-bundle.crt".into(),
installed: None,
};
let env = intercept_env(&i, &["db".into()]);
let get = |k: &str| env.iter().find(|(n, _)| n == k).map(|(_, v)| v.as_str());
assert_eq!(get("HTTPS_PROXY"), Some("http://127.0.0.1:29004"));
assert_eq!(get("https_proxy"), Some("http://127.0.0.1:29004"));
assert_eq!(get("NO_PROXY"), Some("localhost,127.0.0.1,::1,db,corp"));
assert_eq!(get("no_proxy"), get("NO_PROXY"));
assert_eq!(get("NODE_USE_ENV_PROXY"), Some("1"));
for var in CA_VARS {
assert_eq!(get(var), Some("/etc/pki/tls/certs/ca-bundle.crt"), "{var}");
}
assert!(get("SSL_CERT_FILE").is_none() && get("HTTP_PROXY").is_none());
let keys = ["HTTPS_PROXY".to_string(), "FOO".into(), "no_proxy".into(), "HTTP_PROXY".into()];
assert_eq!(proxy_overrides(&keys), ["HTTPS_PROXY", "no_proxy"]);
}
#[test]
fn minted_values_are_long_random_and_plain() {
let p = mint_phantom(&sentry_cli()).unwrap();
let rest = p.strip_prefix("sntrys_").unwrap();
assert_eq!(rest.len(), PHANTOM_RANDOM);
assert!(rest.bytes().all(|b| b.is_ascii_alphanumeric()), "{p}");
assert_ne!(p, mint_phantom(&sentry_cli()).unwrap());
let gh = mint_phantom(&intercept("github")).unwrap();
assert!(gh.starts_with("dsbx_github_"), "{gh}");
assert_eq!(mint_incarnation().unwrap().len(), PHANTOM_RANDOM);
}
#[test]
fn env_and_files_render_and_mount_targets_skip() {
let claim = sentry_cli();
let rec = plan(&[("./s".into(), claim.clone())], &[], None, None, None, counter()).unwrap().claims.remove(0);
assert_eq!(
env(&claim, &rec),
[
("SENTRY_AUTH_TOKEN".to_string(), "sntrys_1".to_string()),
("SENTRY_URL".to_string(), "http://127.0.0.1:29000".to_string())
]
);
let (files, warnings) = render_files(&claim, &rec, "/home/dev/", "dev", &["/workspaces/x".into()]);
assert!(warnings.is_empty());
assert_eq!(
files,
[File {
path: "/home/dev/.sentryclirc".into(),
owner: "dev".into(),
content: "[defaults]\nurl = http://127.0.0.1:29000\n\n[auth]\ntoken = sntrys_1\n".into(),
}]
);
for target in ["/home/dev", "/home/dev/", "/", "/home/dev/.sentryclirc"] {
let (files, warnings) = render_files(&claim, &rec, "/home/dev", "dev", &[target.into()]);
assert!(files.is_empty(), "{target}");
assert!(warnings[0].contains("phantom file is skipped"), "{warnings:?}");
}
let (files, _) = render_files(&claim, &rec, "/home/dev", "dev", &["/home/devx".into()]);
assert_eq!(files.len(), 1);
let claim = sentry();
let planned = plan(&[("./s".into(), claim.clone())], &[], None, None, None, counter()).unwrap();
let rec = &planned.claims[0];
assert!(rec.listeners.is_empty());
assert_eq!(env(&claim, rec), [("SENTRY_AUTH_TOKEN".to_string(), "dsbx_sentry_1".to_string())]);
assert!(render_files(&claim, rec, "/home/dev", "dev", &[]).0.is_empty());
let hosts = planned.proxy.unwrap().hosts;
assert_eq!(hosts, ["de.sentry.io", "sentry.io", "us.sentry.io"]);
}
#[cfg(unix)]
#[test]
fn gh_hosts_yml_renders_unless_mounted() {
let claim = crate::credentials::home::tests::github();
let rec = plan(&[("./gh".into(), claim.clone())], &[], None, None, None, counter()).unwrap().claims.remove(0);
assert_eq!(env(&claim, &rec), [("GH_TOKEN".to_string(), "dsbx_github_1".to_string())]);
let (files, warnings) = render_files(&claim, &rec, "/home/dev", "dev", &["/workspaces/x".into()]);
assert!(warnings.is_empty());
assert_eq!(
files,
[File {
path: "/home/dev/.config/gh/hosts.yml".into(),
owner: "dev".into(),
content: "github.com:\n users:\n x-access-token:\n oauth_token: dsbx_github_1\n \
oauth_token: dsbx_github_1\n user: x-access-token\n git_protocol: https\n"
.into(),
}]
);
let (files, warnings) = render_files(&claim, &rec, "/home/dev", "dev", &["/home/dev/.config/gh".into()]);
assert!(files.is_empty());
assert_eq!(
warnings,
["feature `./gh`: `/home/dev/.config/gh/hosts.yml` is under mount `/home/dev/.config/gh`, which keeps its \
own: the `github` phantom file is skipped; `GH_TOKEN` still holds the phantom, so a tool reading it \
first goes through the broker anyway"]
);
}
#[test]
fn lookups_and_broker_spec() {
let selected = vec![("./s".to_string(), sentry_cli()), ("./gh".to_string(), intercept("github"))];
let planned = plan(&selected, &[], None, None, None, counter()).unwrap();
let mut rec = planned.claims;
let (files, _) = render_files(&sentry_cli(), &rec[0], "/home/dev", "dev", &[]);
rec[0].files = files;
assert_eq!(by_phantom(&rec, "sntrys_1").map(|c| c.kind.as_str()), Some("sentry"));
assert!(by_phantom(&rec, "sntrys_2").is_none());
assert_eq!(by_port(&rec, 29002).map(|(_, u)| u), Some("de.sentry.io"));
let shared = planned.proxy.unwrap();
let i = Intercept {
port: shared.port,
hosts: shared.hosts,
git: None,
lfs: None,
proxy_env: vars(&[("HTTPS_PROXY", "http://corp:3128")]),
bundle: "/b".into(),
installed: None,
};
assert_eq!(ports(&rec, None).collect::<Vec<_>>(), [29000, 29001, 29002]);
assert_eq!(ports(&rec, Some(&i)).collect::<Vec<_>>(), [29000, 29001, 29002, 29003]);
assert!(forward_refusal(&rec, None, "web", 29001).unwrap().contains("`sentry` credential broker listener"));
assert!(forward_refusal(&rec, Some(&i), "web", 29003).unwrap().contains("credential broker proxy port"));
assert!(forward_refusal(&rec, None, "web", 29003).is_none());
assert!(forward_refusal(&rec, Some(&i), "web", 3000).is_none());
assert_eq!(broker_spec(&[], None), None);
let spec = broker_spec(&rec, None).unwrap();
let text = brokerfile::serialize(&spec);
assert_eq!(brokerfile::parse(&text), Ok(spec.clone()));
assert!(text.starts_with("listen 29000 base-url sentry.io\n"), "{text}");
assert_eq!(spec.files[0].phantom, "sntrys_1");
assert_eq!(spec.files[0].path, "/home/dev/.sentryclirc");
let text = brokerfile::serialize(&broker_spec(&rec, Some(&i)).unwrap());
assert!(text.contains("\nproxy-listen 29003 api.github.com\n"), "{text}");
assert!(text.ends_with("\nproxy HTTPS_PROXY=http://corp:3128\n"), "{text}");
assert!(brokerfile::parse(&text).is_ok());
assert!(!text.contains("credential "), "no git hosts: {text}");
}
#[test]
fn git_port_is_a_broker_port_with_the_phantom_for_the_helper() {
let gh = intercept_of(
"github",
serde_json::json!(["api.github.com", "github.com"]),
Some(serde_json::json!({ "hosts": ["github.com"] })),
);
let planned = plan(&[("./gh".into(), gh)], &[], None, None, None, counter()).unwrap();
let (shared, git) = (planned.proxy.unwrap(), planned.git.unwrap());
let i = Intercept {
port: shared.port,
hosts: shared.hosts,
git: Some(git),
lfs: None,
proxy_env: Vec::new(),
bundle: "/b".into(),
installed: None,
};
assert_eq!(i.bound_hosts(), ["api.github.com", "github.com"]);
assert_eq!(ports(&planned.claims, Some(&i)).collect::<Vec<_>>(), [29000, 29001]);
assert!(forward_refusal(&planned.claims, Some(&i), "web", 29001).unwrap().contains("git's credential broker proxy port"));
let text = brokerfile::serialize(&broker_spec(&planned.claims, Some(&i)).unwrap());
assert_eq!(
text,
"proxy-listen 29000 api.github.com\nproxy-listen 29001 github.com\ncredential github.com dsbx_github_1\n"
);
assert!(brokerfile::parse(&text).is_ok());
}
#[test]
fn an_lfs_claim_gets_a_listener_kept_across_rebuilds() {
let mut gh = intercept_of(
"github",
serde_json::json!(["api.github.com", "github.com"]),
Some(serde_json::json!({ "hosts": ["github.com"] })),
);
gh.pinned.lfs = Some(claim::Lfs { hosts: vec!["github.com".into()], upload: "git.push".into() });
let selected = [("./gh".to_string(), gh.clone())];
let planned = plan(&selected, &[], None, None, None, counter()).unwrap();
let ports_of = |p: &Planned| (p.proxy.as_ref().map(|p| p.port), p.git.as_ref().map(|g| g.port), p.lfs);
assert_eq!(ports_of(&planned), (Some(29000), Some(29001), Some(29002)));
let again = plan(&selected, &planned.claims, Some(29001), Some(29000), Some(29002), counter()).unwrap();
assert_eq!(ports_of(&again), (Some(29001), Some(29000), Some(29002)));
assert_eq!(plan(&selected, &planned.claims, Some(29000), Some(29001), Some(80), counter()).unwrap().lfs, Some(29002));
gh.pinned.lfs = None;
assert_eq!(plan(&[("./gh".into(), gh)], &[], None, None, Some(29002), counter()).unwrap().lfs, None);
let (shared, git) = (planned.proxy.unwrap(), planned.git.unwrap());
let i = Intercept {
port: shared.port,
hosts: shared.hosts,
git: Some(git),
lfs: planned.lfs,
proxy_env: Vec::new(),
bundle: "/b".into(),
installed: None,
};
assert_eq!(ports(&planned.claims, Some(&i)).collect::<Vec<_>>(), [29000, 29001, 29002]);
assert!(forward_refusal(&planned.claims, Some(&i), "web", 29002).unwrap().contains("credential broker LFS listener"));
let text = brokerfile::serialize(&broker_spec(&planned.claims, Some(&i)).unwrap());
assert!(text.starts_with("listen 29002 lfs\nproxy-listen 29000 api.github.com\n"), "{text}");
assert_eq!(brokerfile::parse(&text), Ok(broker_spec(&planned.claims, Some(&i)).unwrap()));
}
#[test]
fn records_round_trip_through_toml() {
let mut rec = plan(&[("./s".into(), sentry_cli())], &[], None, None, None, counter()).unwrap().claims;
rec[0].files = render_files(&sentry_cli(), &rec[0], "/home/dev", "dev", &[]).0;
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Wrap {
claims: Vec<InstanceClaim>,
intercept: Option<Intercept>,
}
let intercept = Intercept {
port: 29003,
hosts: vec!["api.github.com".into()],
git: Some(ProxyPort { port: 29004, hosts: vec!["github.com".into()] }),
lfs: None,
proxy_env: vars(&[("HTTPS_PROXY", "http://corp:3128"), ("NO_PROXY", "")]),
bundle: "/etc/ssl/certs/ca-certificates.crt".into(),
installed: Some("ab".repeat(32)),
};
let w = Wrap { claims: rec, intercept: Some(intercept) };
let text = toml::to_string(&w).unwrap();
assert_eq!(toml::from_str::<Wrap>(&text).unwrap(), w);
}
}