1220 lines
49 KiB
Rust
1220 lines
49 KiB
Rust
mod tests {
|
|
use super::*;
|
|
|
|
fn observation(service: Service, addresses: Vec<HostAddress>) -> BTreeMap<String, Observation> {
|
|
let inventory = Inventory {
|
|
agent_version: "0.22.3".into(),
|
|
hostname: "node1".into(),
|
|
public_ip: "10.2.3.4".into(),
|
|
public_ip_source: "route".into(),
|
|
certificate_sha256: String::new(),
|
|
addresses,
|
|
observed_at: "now".into(),
|
|
services: vec![service],
|
|
system: None,
|
|
};
|
|
let mut observed = BTreeMap::new();
|
|
observed.insert(
|
|
"node1".into(),
|
|
Observation {
|
|
agent_id: "node1".into(),
|
|
ok: true,
|
|
checked_at_unix_ms: 1,
|
|
last_ok_unix_ms: Some(1),
|
|
error: None,
|
|
inventory: Some(inventory),
|
|
},
|
|
);
|
|
observed
|
|
}
|
|
|
|
fn service() -> Service {
|
|
Service {
|
|
key: "container:app:8080/tcp".into(),
|
|
technical_name: "app-8080".into(),
|
|
container_id: "abc".into(),
|
|
container_name: "app".into(),
|
|
image: "demo".into(),
|
|
private_port: 8080,
|
|
protocol: "tcp".into(),
|
|
bindings: vec![Binding {
|
|
bind_ip: "0.0.0.0".into(),
|
|
public_port: 18080,
|
|
}],
|
|
public_port: 0,
|
|
bind_ip: String::new(),
|
|
labels: BTreeMap::new(),
|
|
container: None,
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn docker_protocol_flags_default_on_but_can_be_disabled_and_round_trip() {
|
|
let legacy: Assignment = serde_json::from_value(serde_json::json!({
|
|
"service_key": "container:app:8080/tcp", "hostname": "app"
|
|
}))
|
|
.unwrap();
|
|
assert!(legacy.http2_enabled && legacy.http3_enabled);
|
|
let mut configured = legacy;
|
|
configured.http2_enabled = false;
|
|
configured.http3_enabled = false;
|
|
let serialized = serde_json::to_value(&configured).unwrap();
|
|
assert_eq!(serialized["http2_enabled"], false);
|
|
assert_eq!(serialized["http3_enabled"], false);
|
|
let restored: Assignment = serde_json::from_value(serialized).unwrap();
|
|
assert!(!restored.http2_enabled && !restored.http3_enabled);
|
|
}
|
|
|
|
#[test]
|
|
fn docker_frontend_protocols_are_per_publication_and_do_not_change_acl() {
|
|
let mut desired = crate::config::demo_config("127.0.0.1:9000".parse().unwrap());
|
|
desired.vhosts.clear();
|
|
desired.docker_agents.agents.push(Agent {
|
|
id: "node1".into(),
|
|
name: "Node 1".into(),
|
|
endpoint: "http://10.0.0.2:9876".into(),
|
|
token: "012345678901234567890123".into(),
|
|
base_domain: "docker1.lab.local".into(),
|
|
listener_ids: vec!["http".into()],
|
|
acl_profile_id: Some("office".into()),
|
|
assignments: vec![
|
|
Assignment {
|
|
service_key: "container:app:8080/tcp".into(),
|
|
hostname: "app".into(),
|
|
http2_enabled: false,
|
|
http3_enabled: false,
|
|
..Default::default()
|
|
},
|
|
Assignment {
|
|
service_key: "container:second:8080/tcp".into(),
|
|
hostname: "second".into(),
|
|
..Default::default()
|
|
},
|
|
],
|
|
..Default::default()
|
|
});
|
|
let result = effective_config(desired, &observation(service(), vec![])).unwrap();
|
|
assert_eq!(result.vhosts.len(), 2);
|
|
let first = &result.vhosts[0];
|
|
assert!(!first.http2_enabled && !first.http3_enabled);
|
|
assert!(first.websockets);
|
|
assert_eq!(first.acl_profile_id.as_deref(), Some("office"));
|
|
let other = &result.vhosts[1];
|
|
assert!(other.http2_enabled && other.http3_enabled);
|
|
assert_eq!(other.acl_profile_id.as_deref(), Some("office"));
|
|
}
|
|
|
|
#[test]
|
|
fn public_docker_publication_skips_agent_default_acl_profiles() {
|
|
let mut desired = crate::config::demo_config("127.0.0.1:9000".parse().unwrap());
|
|
desired.vhosts.clear();
|
|
desired.docker_agents.agents.push(Agent {
|
|
id: "node1".into(),
|
|
name: "Node 1".into(),
|
|
endpoint: "http://10.0.0.2:9876".into(),
|
|
token: "012345678901234567890123".into(),
|
|
base_domain: "docker1.lab.local".into(),
|
|
listener_ids: vec!["http".into()],
|
|
acl_profile_id: Some("office".into()),
|
|
assignments: vec![
|
|
Assignment {
|
|
service_key: "container:app:8080/tcp".into(),
|
|
hostname: "app".into(),
|
|
inherit_acl_profiles: false,
|
|
..Default::default()
|
|
},
|
|
Assignment {
|
|
service_key: "container:second:8080/tcp".into(),
|
|
hostname: "second".into(),
|
|
..Default::default()
|
|
},
|
|
],
|
|
..Default::default()
|
|
});
|
|
let serialized =
|
|
serde_json::to_value(&desired.docker_agents.agents[0].assignments).unwrap();
|
|
assert_eq!(serialized[0]["inherit_acl_profiles"], false);
|
|
assert!(serialized[1].get("inherit_acl_profiles").is_none());
|
|
let result = effective_config(desired, &observation(service(), vec![])).unwrap();
|
|
let public = &result.vhosts[0];
|
|
assert!(public.acl_profile_id.is_none() && public.acl_profile_ids.is_empty());
|
|
assert_eq!(result.vhosts[1].acl_profile_id.as_deref(), Some("office"));
|
|
}
|
|
|
|
#[test]
|
|
fn generated_vhost_supports_base_domain_custom_domain_aliases_and_selected_ip() {
|
|
let mut desired = crate::config::demo_config("127.0.0.1:9000".parse().unwrap());
|
|
desired.vhosts.clear();
|
|
desired.docker_agents.agents.push(Agent {
|
|
id: "node1".into(),
|
|
name: "Node 1".into(),
|
|
endpoint: "http://10.0.0.2:9876".into(),
|
|
token: "012345678901234567890123".into(),
|
|
tls_fingerprint_sha256: None,
|
|
domains: Vec::new(),
|
|
base_domain: "docker1.lab.local".into(),
|
|
poll_interval_seconds: 30,
|
|
certificate_id: Some("docker-cert".into()),
|
|
certificate_ids: Vec::new(),
|
|
acl_profile_id: None,
|
|
acl_profile_ids: Vec::new(),
|
|
default_target_ip: None,
|
|
listener_ids: vec!["http".into()],
|
|
assignments: vec![Assignment {
|
|
service_key: "container:app:8080/tcp".into(),
|
|
hostname: "app1".into(),
|
|
aliases: vec!["app-alt".into(), "myapp.example.com".into()],
|
|
target_ip: Some("10.2.3.5".parse().unwrap()),
|
|
force_ssl: Some(true),
|
|
websockets: false,
|
|
cache_enabled: true,
|
|
..Default::default()
|
|
}],
|
|
enabled: true,
|
|
label_publish: false,
|
|
});
|
|
desired.docker_agents.agents[0].acl_profile_id = Some("office".into());
|
|
let observed = observation(
|
|
service(),
|
|
vec![
|
|
HostAddress {
|
|
ip: "10.2.3.4".into(),
|
|
source: "interface".into(),
|
|
interface: "eno1".into(),
|
|
scope: "private".into(),
|
|
},
|
|
HostAddress {
|
|
ip: "10.2.3.5".into(),
|
|
source: "interface".into(),
|
|
interface: "eno2".into(),
|
|
scope: "private".into(),
|
|
},
|
|
],
|
|
);
|
|
let effective = effective_config(desired, &observed).unwrap();
|
|
assert_eq!(effective.vhosts.len(), 1);
|
|
assert_eq!(
|
|
effective.vhosts[0].frontend.hosts,
|
|
vec![
|
|
"app1.docker1.lab.local",
|
|
"app-alt.docker1.lab.local",
|
|
"myapp.example.com"
|
|
]
|
|
);
|
|
assert_eq!(effective.vhosts[0].backends[0].url, "http://10.2.3.5:18080");
|
|
assert_eq!(effective.vhosts[0].certificate_id, None);
|
|
assert!(effective.vhosts[0].force_ssl);
|
|
assert!(!effective.vhosts[0].websockets);
|
|
assert!(effective.vhosts[0].cache.enabled.unwrap_or(false));
|
|
assert_eq!(
|
|
effective.vhosts[0].acl_profile_id.as_deref(),
|
|
Some("office")
|
|
);
|
|
assert_eq!(
|
|
effective.vhosts[0].load_balancing.health_check.mode,
|
|
crate::balance::HealthMode::Tcp
|
|
);
|
|
assert!(!effective.vhosts[0].load_balancing.passive_health.enabled);
|
|
}
|
|
|
|
#[test]
|
|
fn docker_alias_count_is_not_artificially_capped() {
|
|
let assignment = Assignment {
|
|
hostname: "app".into(),
|
|
aliases: (0..40).map(|index| format!("alias-{index}")).collect(),
|
|
..Default::default()
|
|
};
|
|
let agent = Agent {
|
|
domains: vec!["docker.example.test".into()],
|
|
..Default::default()
|
|
};
|
|
let hosts = assignment_hosts(&assignment, &agent).unwrap();
|
|
assert_eq!(hosts.len(), 41);
|
|
assert_eq!(hosts[0], "app.docker.example.test");
|
|
assert_eq!(hosts[40], "alias-39.docker.example.test");
|
|
}
|
|
|
|
#[test]
|
|
fn generated_vhost_inherits_global_server_header_policy() {
|
|
let mut desired = crate::config::demo_config("127.0.0.1:9000".parse().unwrap());
|
|
desired.vhosts.clear();
|
|
desired.server_header = crate::settings::ServerHeader {
|
|
mode: crate::settings::ServerMode::Custom,
|
|
value: "RustProxy".into(),
|
|
};
|
|
desired.docker_agents.agents.push(Agent {
|
|
id: "node1".into(),
|
|
name: "Node 1".into(),
|
|
endpoint: "http://10.0.0.2:9876".into(),
|
|
token: "012345678901234567890123".into(),
|
|
base_domain: "docker1.lab.local".into(),
|
|
listener_ids: vec!["http".into()],
|
|
assignments: vec![Assignment {
|
|
service_key: "container:app:8080/tcp".into(),
|
|
hostname: "app".into(),
|
|
..Default::default()
|
|
}],
|
|
..Default::default()
|
|
});
|
|
let observed = observation(
|
|
service(),
|
|
vec![HostAddress {
|
|
ip: "10.2.3.4".into(),
|
|
source: "interface".into(),
|
|
interface: "eno1".into(),
|
|
scope: "private".into(),
|
|
}],
|
|
);
|
|
let effective = effective_config(desired, &observed).unwrap();
|
|
assert!(effective.vhosts[0].server_header.is_none());
|
|
|
|
let policy = crate::config::ValidationPolicy {
|
|
proxy_addr: "127.0.0.1:8080".parse().unwrap(),
|
|
admin_addr: "127.0.0.1:9090".parse().unwrap(),
|
|
tls_addr: None,
|
|
cert_dir: None,
|
|
data_dir: None,
|
|
};
|
|
let snapshot = crate::config::compile(effective, 0, &policy).unwrap();
|
|
let frontend = snapshot
|
|
.frontend(
|
|
"app.docker1.lab.local",
|
|
"127.0.0.1:8080".parse().unwrap(),
|
|
false,
|
|
)
|
|
.unwrap();
|
|
assert!(matches!(
|
|
frontend.server_header.mode,
|
|
crate::settings::ServerMode::Custom
|
|
));
|
|
assert_eq!(frontend.server_header.value, "RustProxy");
|
|
}
|
|
|
|
#[test]
|
|
fn legacy_assignment_defaults_keep_redirect_off_websockets_on_and_cache_off() {
|
|
let assignment: Assignment = serde_json::from_value(serde_json::json!({
|
|
"service_key": "container:app:8080/tcp",
|
|
"hostname": "app"
|
|
}))
|
|
.unwrap();
|
|
assert_eq!(assignment.force_ssl, None);
|
|
assert!(assignment.websockets);
|
|
assert!(assignment.http2_enabled);
|
|
assert!(assignment.http3_enabled);
|
|
assert!(!assignment.cache_enabled);
|
|
assert_eq!(assignment.compression_enabled, None);
|
|
assert_eq!(assignment.compression_zstd, None);
|
|
assert_eq!(assignment.compression_br, None);
|
|
assert_eq!(assignment.compression_gzip, None);
|
|
assert_eq!(assignment.compression_deflate, None);
|
|
assert!(!assignment.passive_health.enabled);
|
|
}
|
|
|
|
#[test]
|
|
fn published_assignment_stays_routable_as_503_when_service_is_missing() {
|
|
let mut desired = crate::config::demo_config("127.0.0.1:9000".parse().unwrap());
|
|
desired.vhosts.clear();
|
|
desired.docker_agents.agents.push(Agent {
|
|
id: "node1".into(),
|
|
name: "Node 1".into(),
|
|
endpoint: "http://10.0.0.2:9876".into(),
|
|
token: "012345678901234567890123".into(),
|
|
base_domain: "docker1.lab.local".into(),
|
|
listener_ids: vec!["http".into()],
|
|
assignments: vec![Assignment {
|
|
service_key: "container:app:8080/tcp".into(),
|
|
hostname: "app".into(),
|
|
compression_enabled: Some(false),
|
|
compression_zstd: Some(false),
|
|
..Default::default()
|
|
}],
|
|
..Default::default()
|
|
});
|
|
let mut observed = observation(service(), vec![]);
|
|
observed
|
|
.get_mut("node1")
|
|
.unwrap()
|
|
.inventory
|
|
.as_mut()
|
|
.unwrap()
|
|
.services
|
|
.clear();
|
|
let effective = effective_config(desired, &observed).unwrap();
|
|
assert_eq!(effective.vhosts.len(), 1);
|
|
assert!(effective.vhosts[0].backends.is_empty());
|
|
assert!(effective.vhosts[0].transient_unavailable);
|
|
assert_eq!(effective.vhosts[0].compression.enabled, Some(false));
|
|
assert_eq!(effective.vhosts[0].compression.zstd, None);
|
|
|
|
let policy = crate::config::ValidationPolicy {
|
|
proxy_addr: "127.0.0.1:8080".parse().unwrap(),
|
|
admin_addr: "127.0.0.1:9090".parse().unwrap(),
|
|
tls_addr: None,
|
|
cert_dir: None,
|
|
data_dir: None,
|
|
};
|
|
let snapshot = crate::config::compile(effective, 0, &policy).unwrap();
|
|
let route = snapshot
|
|
.select_on(
|
|
"app.docker1.lab.local",
|
|
"/",
|
|
"127.0.0.1:8080".parse().unwrap(),
|
|
false,
|
|
)
|
|
.unwrap();
|
|
assert!(route.unavailable);
|
|
assert!(route.pool.is_none());
|
|
assert!(route.backend.is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn docker_cache_settings_and_id_survive_container_disappearance_and_return() {
|
|
let mut desired = crate::config::demo_config("127.0.0.1:9000".parse().unwrap());
|
|
desired.vhosts.clear();
|
|
desired.docker_agents.agents.push(Agent {
|
|
id: "node1".into(),
|
|
name: "Node 1".into(),
|
|
base_domain: "docker1.lab.local".into(),
|
|
listener_ids: vec!["http".into()],
|
|
assignments: vec![Assignment {
|
|
service_key: "container:app:8080/tcp".into(),
|
|
hostname: "app".into(),
|
|
cache_enabled: true,
|
|
cache_default_ttl_seconds: Some(180),
|
|
cache_rules: vec![serde_json::from_value(serde_json::json!({
|
|
"id": "no-admin-cache",
|
|
"conditions": [{"field": "path", "operator": "prefix", "values": ["/admin"]}],
|
|
"actions": [{"type": "pass"}]
|
|
})).unwrap()],
|
|
..Default::default()
|
|
}],
|
|
..Default::default()
|
|
});
|
|
let persisted = serde_json::to_value(&desired.docker_agents.agents[0].assignments[0])
|
|
.unwrap();
|
|
let restored: Assignment = serde_json::from_value(persisted).unwrap();
|
|
assert_eq!(restored.cache_default_ttl_seconds, Some(180));
|
|
assert_eq!(restored.cache_rules.len(), 1);
|
|
|
|
let found = observation(service(), vec![HostAddress {
|
|
ip: "10.2.3.4".into(),
|
|
source: "interface".into(),
|
|
interface: "eth0".into(),
|
|
scope: "private".into(),
|
|
}]);
|
|
let before = effective_config(desired.clone(), &found).unwrap();
|
|
assert!(!before.vhosts[0].transient_unavailable);
|
|
|
|
let mut missing = found.clone();
|
|
missing.get_mut("node1").unwrap().inventory.as_mut().unwrap().services.clear();
|
|
let offline = effective_config(desired.clone(), &missing).unwrap();
|
|
assert_eq!(offline.vhosts.len(), 1);
|
|
assert!(offline.vhosts[0].transient_unavailable);
|
|
assert_eq!(offline.vhosts[0].id, before.vhosts[0].id);
|
|
assert_eq!(offline.vhosts[0].cache.default_ttl_seconds, Some(180));
|
|
assert_eq!(offline.vhosts[0].cache.rules.len(), 1);
|
|
|
|
let returned = effective_config(desired, &found).unwrap();
|
|
assert!(!returned.vhosts[0].transient_unavailable);
|
|
assert_eq!(returned.vhosts[0].id, before.vhosts[0].id);
|
|
assert_eq!(returned.vhosts[0].cache.rules.len(), 1);
|
|
}
|
|
|
|
#[test]
|
|
fn failed_agent_observation_with_last_inventory_keeps_backend_materialized() {
|
|
let mut desired = crate::config::demo_config("127.0.0.1:9000".parse().unwrap());
|
|
desired.vhosts.clear();
|
|
desired.docker_agents.agents.push(Agent {
|
|
id: "node1".into(),
|
|
name: "Node 1".into(),
|
|
endpoint: "http://10.0.0.2:9876".into(),
|
|
token: "012345678901234567890123".into(),
|
|
base_domain: "docker1.lab.local".into(),
|
|
listener_ids: vec!["http".into()],
|
|
assignments: vec![Assignment {
|
|
service_key: "container:app:8080/tcp".into(),
|
|
hostname: "app".into(),
|
|
compression_enabled: Some(true),
|
|
compression_gzip: Some(false),
|
|
compression_br: Some(true),
|
|
..Default::default()
|
|
}],
|
|
..Default::default()
|
|
});
|
|
let mut observed = observation(
|
|
service(),
|
|
vec![HostAddress {
|
|
ip: "10.2.3.4".into(),
|
|
source: "interface".into(),
|
|
interface: "eno1".into(),
|
|
scope: "private".into(),
|
|
}],
|
|
);
|
|
let state = observed.get_mut("node1").unwrap();
|
|
state.ok = false;
|
|
state.error = Some("temporary agent outage".into());
|
|
let effective = effective_config(desired, &observed).unwrap();
|
|
assert_eq!(effective.vhosts.len(), 1);
|
|
assert_eq!(effective.vhosts[0].backends.len(), 1);
|
|
assert!(!effective.vhosts[0].transient_unavailable);
|
|
assert_eq!(effective.vhosts[0].compression.enabled, Some(true));
|
|
assert_eq!(effective.vhosts[0].compression.gzip, None);
|
|
assert_eq!(effective.vhosts[0].compression.br, None);
|
|
}
|
|
|
|
#[test]
|
|
fn disabling_agent_stops_discovery_but_keeps_published_hostname_as_503() {
|
|
let mut desired = crate::config::demo_config("127.0.0.1:9000".parse().unwrap());
|
|
desired.vhosts.clear();
|
|
desired.docker_agents.agents.push(Agent {
|
|
id: "node1".into(),
|
|
name: "Node 1".into(),
|
|
endpoint: "http://10.0.0.2:9876".into(),
|
|
token: "012345678901234567890123".into(),
|
|
base_domain: "docker1.lab.local".into(),
|
|
listener_ids: vec!["http".into()],
|
|
enabled: false,
|
|
assignments: vec![Assignment {
|
|
service_key: "container:app:8080/tcp".into(),
|
|
hostname: "app".into(),
|
|
..Default::default()
|
|
}],
|
|
..Default::default()
|
|
});
|
|
let observed = observation(
|
|
service(),
|
|
vec![HostAddress {
|
|
ip: "10.2.3.4".into(),
|
|
source: "interface".into(),
|
|
interface: "eno1".into(),
|
|
scope: "private".into(),
|
|
}],
|
|
);
|
|
let effective = effective_config(desired, &observed).unwrap();
|
|
assert_eq!(effective.vhosts.len(), 1);
|
|
assert!(effective.vhosts[0].backends.is_empty());
|
|
assert!(effective.vhosts[0].transient_unavailable);
|
|
}
|
|
|
|
#[test]
|
|
fn docker_assignment_propagates_explicit_passive_health_policy() {
|
|
let mut desired = crate::config::demo_config("127.0.0.1:9000".parse().unwrap());
|
|
desired.vhosts.clear();
|
|
desired.docker_agents.agents.push(Agent {
|
|
id: "node1".into(),
|
|
name: "Node 1".into(),
|
|
endpoint: "http://10.0.0.2:9876".into(),
|
|
token: "012345678901234567890123".into(),
|
|
base_domain: "docker1.lab.local".into(),
|
|
listener_ids: vec!["http".into()],
|
|
assignments: vec![Assignment {
|
|
service_key: "container:app:8080/tcp".into(),
|
|
hostname: "app".into(),
|
|
passive_health: crate::balance::PassiveHealth {
|
|
enabled: true,
|
|
count_http_5xx: false,
|
|
count_slow_responses: true,
|
|
fall: Some(4),
|
|
rise: Some(3),
|
|
..Default::default()
|
|
},
|
|
..Default::default()
|
|
}],
|
|
..Default::default()
|
|
});
|
|
let observed = observation(
|
|
service(),
|
|
vec![HostAddress {
|
|
ip: "10.2.3.4".into(),
|
|
source: "interface".into(),
|
|
interface: "eno1".into(),
|
|
scope: "private".into(),
|
|
}],
|
|
);
|
|
let effective = effective_config(desired, &observed).unwrap();
|
|
let passive = &effective.vhosts[0].load_balancing.passive_health;
|
|
assert!(passive.enabled);
|
|
assert!(!passive.count_http_5xx);
|
|
assert!(passive.count_slow_responses);
|
|
assert_eq!(passive.fall, Some(4));
|
|
assert_eq!(passive.rise, Some(3));
|
|
}
|
|
|
|
#[test]
|
|
fn docker_assignment_passive_health_survives_serialization_round_trip() {
|
|
let original = Assignment {
|
|
service_key: "container:app:8080/tcp".into(),
|
|
hostname: "app".into(),
|
|
passive_health: crate::balance::PassiveHealth {
|
|
enabled: true,
|
|
count_connect_errors: false,
|
|
count_timeouts: true,
|
|
count_io_errors: false,
|
|
count_http_5xx: true,
|
|
count_slow_responses: true,
|
|
fall: Some(5),
|
|
rise: Some(4),
|
|
},
|
|
..Default::default()
|
|
};
|
|
let encoded = serde_json::to_value(&original).unwrap();
|
|
let restored: Assignment = serde_json::from_value(encoded).unwrap();
|
|
let passive = restored.passive_health;
|
|
assert!(passive.enabled);
|
|
assert!(!passive.count_connect_errors);
|
|
assert!(passive.count_timeouts);
|
|
assert!(!passive.count_io_errors);
|
|
assert!(passive.count_http_5xx);
|
|
assert!(passive.count_slow_responses);
|
|
assert_eq!(passive.fall, Some(5));
|
|
assert_eq!(passive.rise, Some(4));
|
|
}
|
|
|
|
#[test]
|
|
fn force_ssl_assignment_requires_plain_and_tls_listener_pair() {
|
|
let listeners = crate::settings::Listeners::from_startup(
|
|
"127.0.0.1:8080".parse().unwrap(),
|
|
Some("127.0.0.1:8443".parse().unwrap()),
|
|
);
|
|
let agent = Agent {
|
|
id: "node1".into(),
|
|
name: "Node 1".into(),
|
|
endpoint: "http://10.0.0.2:9876".into(),
|
|
token: "012345678901234567890123".into(),
|
|
base_domain: "docker1.lab.local".into(),
|
|
listener_ids: vec!["http".into(), "https".into()],
|
|
assignments: vec![Assignment {
|
|
service_key: "container:app:8080/tcp".into(),
|
|
hostname: "app".into(),
|
|
force_ssl: Some(true),
|
|
..Default::default()
|
|
}],
|
|
..Default::default()
|
|
};
|
|
let mut valid = Settings {
|
|
agents: vec![agent.clone()],
|
|
..Default::default()
|
|
};
|
|
valid.validate(&listeners).unwrap();
|
|
let mut invalid = Settings {
|
|
agents: vec![Agent {
|
|
listener_ids: vec!["http".into()],
|
|
..agent
|
|
}],
|
|
..Default::default()
|
|
};
|
|
assert!(invalid.validate(&listeners).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn concrete_docker_binding_keeps_its_own_port() {
|
|
let service = Service {
|
|
bindings: vec![
|
|
Binding {
|
|
bind_ip: "10.2.3.4".into(),
|
|
public_port: 18080,
|
|
},
|
|
Binding {
|
|
bind_ip: "10.2.3.5".into(),
|
|
public_port: 28080,
|
|
},
|
|
],
|
|
..service()
|
|
};
|
|
assert_eq!(
|
|
port_for_ip(&service, "10.2.3.4".parse().unwrap()),
|
|
Some(18080)
|
|
);
|
|
assert_eq!(
|
|
port_for_ip(&service, "10.2.3.5".parse().unwrap()),
|
|
Some(28080)
|
|
);
|
|
assert_eq!(port_for_ip(&service, "10.2.3.6".parse().unwrap()), None);
|
|
}
|
|
|
|
#[test]
|
|
fn tls_fingerprint_normalizes_common_copy_paste_format() {
|
|
let raw = "AA:BB:CC:DD:EE:FF:00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF:00:11:22:33:44:55:66:77:88:99";
|
|
let normalized = normalize_fingerprint(raw).unwrap();
|
|
assert_eq!(normalized.len(), 64);
|
|
assert_eq!(normalized, normalized.to_ascii_lowercase());
|
|
assert_eq!(fingerprint_bytes(&normalized).unwrap()[0], 0xaa);
|
|
}
|
|
|
|
#[test]
|
|
fn generated_vhost_id_stays_within_config_limit() {
|
|
let id = generated_id(
|
|
"agent-name-that-is-deliberately-far-longer-than-the-config-id-limit",
|
|
"compose:very-long-project:very-long-service:12345/tcp",
|
|
);
|
|
assert!(
|
|
id.len() <= 64,
|
|
"generated Docker vhost id is too long: {id}"
|
|
);
|
|
assert!(
|
|
crate::config::valid_id(&id),
|
|
"generated Docker vhost id is invalid: {id}"
|
|
);
|
|
}
|
|
#[test]
|
|
fn docker_assignments_inherit_or_override_acl_sets_and_force_tls_only_on_consent() {
|
|
let mut desired = crate::config::demo_config("127.0.0.1:9000".parse().unwrap());
|
|
desired.vhosts.clear();
|
|
desired.docker_agents.agents.push(Agent {
|
|
id: "node1".into(),
|
|
name: "Node 1".into(),
|
|
base_domain: "docker1.lab.local".into(),
|
|
listener_ids: vec!["http".into()],
|
|
certificate_id: Some("selected-cert".into()),
|
|
acl_profile_id: Some("legacy".into()),
|
|
acl_profile_ids: vec!["work".into(), "vpn".into()],
|
|
assignments: vec![Assignment {
|
|
service_key: "container:app:8080/tcp".into(),
|
|
hostname: "app".into(),
|
|
allow_certificate_mismatch: true,
|
|
..Default::default()
|
|
}],
|
|
..Default::default()
|
|
});
|
|
let observed = observation(service(), vec![]);
|
|
let inherited = effective_config(desired.clone(), &observed).unwrap();
|
|
assert_eq!(
|
|
inherited.vhosts[0].acl_profile_id.as_deref(),
|
|
Some("legacy")
|
|
);
|
|
assert_eq!(inherited.vhosts[0].acl_profile_ids, vec!["work", "vpn"]);
|
|
assert!(inherited.vhosts[0].allow_certificate_mismatch);
|
|
desired.docker_agents.agents[0].assignments[0].acl_profile_ids =
|
|
vec!["guest".into(), "branch".into()];
|
|
desired.docker_agents.agents[0].assignments[0].allow_certificate_mismatch = false;
|
|
let overridden = effective_config(desired, &observed).unwrap();
|
|
assert_eq!(overridden.vhosts[0].acl_profile_id, None);
|
|
assert_eq!(
|
|
overridden.vhosts[0].acl_profile_ids,
|
|
vec!["guest", "branch"]
|
|
);
|
|
assert!(!overridden.vhosts[0].allow_certificate_mismatch);
|
|
}
|
|
|
|
#[test]
|
|
fn older_docker_assignment_json_has_safe_defaults() {
|
|
let assignment: Assignment = serde_json::from_value(serde_json::json!({
|
|
"service_key": "container:app:8080/tcp", "hostname": "app", "acl_profile_id": "legacy"
|
|
}))
|
|
.unwrap();
|
|
assert_eq!(assignment.acl_profile_id.as_deref(), Some("legacy"));
|
|
assert!(assignment.acl_profile_ids.is_empty());
|
|
assert!(!assignment.allow_certificate_mismatch);
|
|
}
|
|
|
|
fn stored_certificate(id: &str, names: &[&str]) -> crate::tls::Certificate {
|
|
crate::tls::Certificate {
|
|
issuer: None,
|
|
id: id.into(),
|
|
covered_hosts: names.iter().map(|name| name.to_string()).collect(),
|
|
hosts: Vec::new(),
|
|
cert_file: format!("{id}.pem"),
|
|
key_file: format!("{id}.key"),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn legacy_base_domain_and_certificate_migrate_to_lists() {
|
|
let mut agent: Agent = serde_json::from_value(serde_json::json!({
|
|
"id": "node1", "name": "Node 1", "endpoint": "https://10.0.0.2:9876",
|
|
"token": "012345678901234567890123", "base_domain": "Docker1.Lab.Local.",
|
|
"certificate_id": "wild", "listener_ids": ["http"],
|
|
"assignments": [{"service_key": "container:app:8080/tcp", "hostname": "app"}]
|
|
}))
|
|
.unwrap();
|
|
assert_eq!(agent.domain_list(), vec!["docker1.lab.local"]);
|
|
assert_eq!(agent.certificate_list(), vec!["wild"]);
|
|
let mut settings = Settings {
|
|
agents: vec![agent.clone()],
|
|
..Default::default()
|
|
};
|
|
let listeners = crate::config::demo_config("127.0.0.1:9000".parse().unwrap()).listeners;
|
|
settings.validate(&listeners).unwrap();
|
|
agent = settings.agents.remove(0);
|
|
assert_eq!(agent.domains, vec!["docker1.lab.local"]);
|
|
assert!(agent.base_domain.is_empty());
|
|
assert_eq!(agent.certificate_ids, vec!["wild"]);
|
|
assert_eq!(agent.certificate_id, None);
|
|
assert_eq!(
|
|
agent.assignments[0].domain.as_deref(),
|
|
Some("docker1.lab.local")
|
|
);
|
|
let saved = serde_json::to_value(&agent).unwrap();
|
|
assert!(saved.get("base_domain").is_none());
|
|
assert!(saved.get("certificate_id").is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn assignment_master_domain_must_belong_to_agent() {
|
|
let listeners = crate::config::demo_config("127.0.0.1:9000".parse().unwrap()).listeners;
|
|
let mut settings = Settings {
|
|
agents: vec![Agent {
|
|
id: "node1".into(),
|
|
name: "Node 1".into(),
|
|
endpoint: "https://10.0.0.2:9876".into(),
|
|
token: "012345678901234567890123".into(),
|
|
domains: vec!["example.com".into(), "app.example.net".into()],
|
|
listener_ids: vec!["http".into()],
|
|
assignments: vec![Assignment {
|
|
service_key: "container:app:8080/tcp".into(),
|
|
hostname: "web".into(),
|
|
domain: Some("app.example.net".into()),
|
|
aliases: vec!["www".into(), "web.example.com".into()],
|
|
..Default::default()
|
|
}],
|
|
..Default::default()
|
|
}],
|
|
..Default::default()
|
|
};
|
|
settings.validate(&listeners).unwrap();
|
|
let agent = &settings.agents[0];
|
|
assert_eq!(
|
|
assignment_hosts(&agent.assignments[0], agent).unwrap(),
|
|
vec![
|
|
"web.app.example.net",
|
|
"www.app.example.net",
|
|
"web.example.com"
|
|
]
|
|
);
|
|
settings.agents[0].assignments[0].domain = Some("other.test".into());
|
|
let error = settings.validate(&listeners).unwrap_err().to_string();
|
|
assert!(error.contains("not assigned"), "{error}");
|
|
settings.agents[0].assignments[0].domain = None;
|
|
settings.agents[0].domains.clear();
|
|
assert!(settings.validate(&listeners).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn certificate_list_prefers_matching_certificate_then_generic() {
|
|
let certificates = vec![
|
|
stored_certificate("wild", &["*.example.com", "example.com"]),
|
|
stored_certificate("exact", &["app.example.com"]),
|
|
stored_certificate("other", &["*.example.net"]),
|
|
];
|
|
let all = vec!["wild".to_string(), "exact".to_string(), "other".to_string()];
|
|
let pick = |hosts: &[&str]| {
|
|
let hosts: Vec<String> = hosts.iter().map(|host| host.to_string()).collect();
|
|
select_certificate(&all, &certificates, &hosts)
|
|
};
|
|
assert_eq!(pick(&["app.example.com"]).as_deref(), Some("exact"));
|
|
assert_eq!(
|
|
pick(&["app.example.com", "www.example.com"]).as_deref(),
|
|
Some("wild")
|
|
);
|
|
assert_eq!(pick(&["a.b.example.com"]), None);
|
|
assert_eq!(pick(&["api.example.net"]).as_deref(), Some("other"));
|
|
assert_eq!(pick(&["app.example.com", "api.example.net"]), None);
|
|
|
|
let mut desired = crate::config::demo_config("127.0.0.1:9000".parse().unwrap());
|
|
desired.vhosts.clear();
|
|
desired.tls.certificates = certificates;
|
|
desired.docker_agents.agents.push(Agent {
|
|
id: "node1".into(),
|
|
name: "Node 1".into(),
|
|
domains: vec!["example.com".into(), "example.org".into()],
|
|
certificate_ids: all.clone(),
|
|
listener_ids: vec!["http".into()],
|
|
assignments: vec![
|
|
Assignment {
|
|
service_key: "container:app:8080/tcp".into(),
|
|
hostname: "app".into(),
|
|
..Default::default()
|
|
},
|
|
Assignment {
|
|
service_key: "container:other:8080/tcp".into(),
|
|
hostname: "other".into(),
|
|
domain: Some("example.org".into()),
|
|
..Default::default()
|
|
},
|
|
],
|
|
..Default::default()
|
|
});
|
|
let effective = effective_config(desired, &observation(service(), vec![])).unwrap();
|
|
assert_eq!(effective.vhosts[0].certificate_id.as_deref(), Some("exact"));
|
|
assert_eq!(
|
|
effective.vhosts[1].frontend.hosts,
|
|
vec!["other.example.org"]
|
|
);
|
|
assert_eq!(effective.vhosts[1].certificate_id, None);
|
|
}
|
|
|
|
#[test]
|
|
fn inventory_system_info_is_optional_and_sanitized() {
|
|
let mut inventory: Inventory = serde_json::from_value(serde_json::json!({
|
|
"agent_version": "1.1.0", "hostname": "node1", "observed_at": "now", "services": [],
|
|
"future_field": true,
|
|
"system": {"os": "Debian GNU/Linux 13\u{7}", "load1": -1.0, "cpu_percent": 250.0,
|
|
"cpu_count": 0, "memory_total_bytes": 1024, "runtime": "podman", "future": 1}
|
|
}))
|
|
.unwrap();
|
|
normalize_inventory(&mut inventory);
|
|
validate_inventory(&inventory).unwrap();
|
|
let system = inventory.system.unwrap();
|
|
assert_eq!(system.os, "Debian GNU/Linux 13");
|
|
assert_eq!(system.load1, None);
|
|
assert_eq!(system.cpu_percent, Some(100.0));
|
|
assert_eq!(system.cpu_count, None);
|
|
assert_eq!(system.memory_total_bytes, Some(1024));
|
|
assert_eq!(system.runtime, "podman");
|
|
let legacy: Inventory = serde_json::from_value(serde_json::json!({
|
|
"agent_version": "1.0.1", "hostname": "node1", "observed_at": "now", "services": []
|
|
}))
|
|
.unwrap();
|
|
assert!(legacy.system.is_none());
|
|
}
|
|
|
|
fn labelled(name: &str, port: u16, labels: &[(&str, &str)]) -> Service {
|
|
Service {
|
|
key: format!("container:{name}:{port}/tcp"),
|
|
technical_name: name.into(),
|
|
private_port: port,
|
|
labels: labels
|
|
.iter()
|
|
.map(|(key, value)| (key.to_string(), value.to_string()))
|
|
.collect(),
|
|
..service()
|
|
}
|
|
}
|
|
|
|
fn label_config(
|
|
services: Vec<Service>,
|
|
) -> (crate::config::Config, BTreeMap<String, Observation>) {
|
|
let mut desired = crate::config::demo_config("127.0.0.1:9000".parse().unwrap());
|
|
desired.vhosts.clear();
|
|
desired.docker_agents.agents.push(Agent {
|
|
id: "node1".into(),
|
|
name: "Node 1".into(),
|
|
domains: vec!["lab.example.com".into(), "example.org".into()],
|
|
listener_ids: vec!["http".into()],
|
|
label_publish: true,
|
|
..Default::default()
|
|
});
|
|
let mut observed = observation(service(), vec![]);
|
|
observed
|
|
.get_mut("node1")
|
|
.unwrap()
|
|
.inventory
|
|
.as_mut()
|
|
.unwrap()
|
|
.services = services;
|
|
(desired, observed)
|
|
}
|
|
|
|
#[test]
|
|
fn container_labels_publish_vhosts_inside_agent_domains_only() {
|
|
let (desired, observed) = label_config(vec![
|
|
labelled(
|
|
"web",
|
|
80,
|
|
&[
|
|
("rustproxy.enable", "true"),
|
|
("rustproxy.host", "shop"),
|
|
("rustproxy.domain", "example.org"),
|
|
("rustproxy.aliases", "www,shop.lab.example.com"),
|
|
("rustproxy.scheme", "https"),
|
|
],
|
|
),
|
|
labelled(
|
|
"api",
|
|
8080,
|
|
&[("rustproxy.enable", "yes"), ("rustproxy.port", "9999")],
|
|
),
|
|
labelled(
|
|
"bad",
|
|
81,
|
|
&[
|
|
("rustproxy.enable", "true"),
|
|
("rustproxy.host", "evil.attacker.test"),
|
|
],
|
|
),
|
|
labelled("plain", 82, &[]),
|
|
labelled(
|
|
"dup",
|
|
83,
|
|
&[
|
|
("rustproxy.enable", "true"),
|
|
("rustproxy.host", "shop"),
|
|
("rustproxy.domain", "example.org"),
|
|
],
|
|
),
|
|
]);
|
|
let (publications, issues) = label_publications(&desired, &observed);
|
|
assert_eq!(publications.len(), 1);
|
|
assert_eq!(
|
|
publications[0].hosts,
|
|
vec![
|
|
"shop.example.org",
|
|
"www.example.org",
|
|
"shop.lab.example.com"
|
|
]
|
|
);
|
|
assert!(matches!(
|
|
publications[0].assignment.scheme,
|
|
BackendScheme::Https
|
|
));
|
|
assert_eq!(issues.len(), 2, "{issues:?}");
|
|
assert!(issues.iter().any(|issue| issue.error.contains("outside")));
|
|
assert!(issues
|
|
.iter()
|
|
.any(|issue| issue.error.contains("already used")));
|
|
let effective = effective_config(desired.clone(), &observed).unwrap();
|
|
assert_eq!(effective.vhosts.len(), 1);
|
|
assert_eq!(effective.vhosts[0].frontend.hosts[0], "shop.example.org");
|
|
|
|
let mut disabled = desired.clone();
|
|
disabled.docker_agents.agents[0].label_publish = false;
|
|
assert!(label_publications(&disabled, &observed).0.is_empty());
|
|
|
|
let mut overridden = desired;
|
|
overridden.docker_agents.agents[0]
|
|
.assignments
|
|
.push(Assignment {
|
|
service_key: "container:web:80/tcp".into(),
|
|
hostname: "web".into(),
|
|
enabled: false,
|
|
..Default::default()
|
|
});
|
|
assert!(!label_publications(&overridden, &observed)
|
|
.0
|
|
.iter()
|
|
.any(|item| item.assignment.service_key == "container:web:80/tcp"));
|
|
}
|
|
|
|
#[test]
|
|
fn label_only_publications_reappear_without_becoming_persisted_assignments() {
|
|
let (desired, found) = label_config(vec![labelled(
|
|
"web", 80, &[("rustproxy.enable", "true"), ("rustproxy.host", "web")],
|
|
)]);
|
|
assert!(desired.docker_agents.agents[0].assignments.is_empty());
|
|
let before = effective_config(desired.clone(), &found).unwrap();
|
|
assert_eq!(before.vhosts.len(), 1);
|
|
|
|
let mut missing = found.clone();
|
|
missing.get_mut("node1").unwrap().inventory.as_mut().unwrap().services.clear();
|
|
assert!(effective_config(desired.clone(), &missing).unwrap().vhosts.is_empty());
|
|
|
|
let again = effective_config(desired.clone(), &found).unwrap();
|
|
assert_eq!(again.vhosts.len(), 1);
|
|
assert_eq!(again.vhosts[0].id, before.vhosts[0].id);
|
|
assert!(desired.docker_agents.agents[0].assignments.is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn alerts_cover_offline_resources_missing_and_unhealthy_services() {
|
|
let unhealthy = Service {
|
|
container: Some(ContainerState {
|
|
state: "running".into(),
|
|
health: "unhealthy".into(),
|
|
..Default::default()
|
|
}),
|
|
..labelled("web", 80, &[("rustproxy.enable", "true")])
|
|
};
|
|
let (mut desired, mut observed) = label_config(vec![unhealthy]);
|
|
desired.docker_agents.agents[0]
|
|
.assignments
|
|
.push(Assignment {
|
|
service_key: "container:gone:80/tcp".into(),
|
|
hostname: "gone".into(),
|
|
..Default::default()
|
|
});
|
|
observed
|
|
.get_mut("node1")
|
|
.unwrap()
|
|
.inventory
|
|
.as_mut()
|
|
.unwrap()
|
|
.system = Some(SystemInfo {
|
|
disk_total_bytes: Some(100),
|
|
disk_free_bytes: Some(5),
|
|
memory_total_bytes: Some(100),
|
|
memory_available_bytes: Some(50),
|
|
load5: Some(9.0),
|
|
cpu_count: Some(2),
|
|
..Default::default()
|
|
});
|
|
let kinds: Vec<_> = alerts(&desired, &observed)
|
|
.into_iter()
|
|
.map(|alert| alert.kind)
|
|
.collect();
|
|
assert!(kinds.contains(&"disk_high"));
|
|
assert!(!kinds.contains(&"memory_high"));
|
|
assert!(kinds.contains(&"load_high"));
|
|
assert!(kinds.contains(&"service_missing"));
|
|
assert!(kinds.contains(&"container_unhealthy"));
|
|
|
|
let observation = observed.get_mut("node1").unwrap();
|
|
observation.ok = false;
|
|
observation.error = Some("connection refused".into());
|
|
let offline = alerts(&desired, &observed);
|
|
assert_eq!(offline.len(), 1);
|
|
assert_eq!(offline[0].kind, "agent_offline");
|
|
assert_eq!(offline[0].severity, "error");
|
|
assert_eq!(offline[0].since_unix_ms, Some(1));
|
|
|
|
desired.docker_agents.alerts.enabled = false;
|
|
assert!(alerts(&desired, &observed).is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn alert_settings_are_validated_and_default_for_old_configs() {
|
|
let settings: Settings = serde_json::from_value(serde_json::json!({"agents": []})).unwrap();
|
|
assert_eq!(settings.alerts, AlertSettings::default());
|
|
let listeners = crate::config::demo_config("127.0.0.1:9000".parse().unwrap()).listeners;
|
|
let mut invalid = Settings {
|
|
alerts: AlertSettings {
|
|
disk_percent: 20,
|
|
..Default::default()
|
|
},
|
|
..Default::default()
|
|
};
|
|
assert!(invalid.validate(&listeners).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn inventory_labels_are_limited_to_rustproxy_namespace() {
|
|
let mut inventory: Inventory = serde_json::from_value(serde_json::json!({
|
|
"agent_version": "1.1.0", "hostname": "node1", "observed_at": "now",
|
|
"services": [{"key": "container:web:80/tcp", "technical_name": "web", "container_id": "a",
|
|
"container_name": "web", "image": "nginx", "private_port": 80, "protocol": "tcp",
|
|
"bindings": [{"bind_ip": "", "public_port": 8080}],
|
|
"labels": {"rustproxy.enable": "true", "com.example.secret": "x"},
|
|
"container": {"state": "Running", "health": "healthy", "restart_count": 2, "cpu_percent": -1.0}}]
|
|
}))
|
|
.unwrap();
|
|
normalize_inventory(&mut inventory);
|
|
validate_inventory(&inventory).unwrap();
|
|
let service = &inventory.services[0];
|
|
assert_eq!(service.labels.len(), 1);
|
|
let container = service.container.as_ref().unwrap();
|
|
assert_eq!(container.state, "running");
|
|
assert_eq!(container.restart_count, Some(2));
|
|
assert_eq!(container.cpu_percent, None);
|
|
}
|
|
|
|
#[test]
|
|
fn containers_without_healthcheck_do_not_raise_alerts() {
|
|
let plain = Service {
|
|
container: Some(ContainerState {
|
|
state: "running".into(),
|
|
health: "none".into(),
|
|
..Default::default()
|
|
}),
|
|
..labelled("web", 80, &[("rustproxy.enable", "true")])
|
|
};
|
|
let (desired, observed) = label_config(vec![plain]);
|
|
assert!(alerts(&desired, &observed).is_empty());
|
|
let (desired, mut observed) =
|
|
label_config(vec![labelled("web", 80, &[("rustproxy.enable", "true")])]);
|
|
observed
|
|
.get_mut("node1")
|
|
.unwrap()
|
|
.inventory
|
|
.as_mut()
|
|
.unwrap()
|
|
.services[0]
|
|
.container = Some(ContainerState {
|
|
state: "restarting".into(),
|
|
health: "none".into(),
|
|
..Default::default()
|
|
});
|
|
let found = alerts(&desired, &observed);
|
|
assert_eq!(found.len(), 1);
|
|
assert_eq!(found[0].message.as_deref(), Some("restarting"));
|
|
}
|
|
|
|
fn security_agent(assignment: Assignment) -> Agent {
|
|
Agent {
|
|
id: "node1".into(),
|
|
name: "Node 1".into(),
|
|
endpoint: "http://10.0.0.2:9876".into(),
|
|
token: "012345678901234567890123".into(),
|
|
base_domain: "docker1.lab.local".into(),
|
|
listener_ids: vec!["http".into()],
|
|
assignments: vec![assignment],
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn docker_assignment_carries_diagnostics_methods_rate_limit_and_security_headers() {
|
|
let mut desired = crate::config::demo_config("127.0.0.1:9000".parse().unwrap());
|
|
let assignment: Assignment = serde_json::from_value(serde_json::json!({
|
|
"service_key": "container:app:8080/tcp",
|
|
"hostname": "app",
|
|
"timing_headers": true,
|
|
"backend_header": true,
|
|
"backend_timing_header": true,
|
|
"cloudflare_client_ip": true,
|
|
"method_policy": {"mode": "allow", "methods": ["GET", "HEAD", "POST"]},
|
|
"rate_limit": {"enabled": true, "requests": 50, "window_seconds": 10, "burst": 5,
|
|
"country_access": {"mode": "block", "countries": ["RU"]}},
|
|
"security_headers": {"hsts": {"enabled": true, "max_age_seconds": 600}, "nosniff": true, "frame_options": "deny"}
|
|
}))
|
|
.unwrap();
|
|
desired.docker_agents.agents.push(security_agent(assignment));
|
|
let effective = effective_config(desired, &observation(service(), vec![])).unwrap();
|
|
let host = effective.vhosts.last().unwrap();
|
|
assert!(host.timing_headers && host.backend_header && host.backend_timing_header);
|
|
assert!(host.cloudflare_client_ip);
|
|
assert_eq!(host.method_policy.methods, ["GET", "HEAD", "POST"]);
|
|
assert!(host.rate_limit.enabled);
|
|
assert_eq!(host.rate_limit.requests, 50);
|
|
assert_eq!(host.rate_limit.country_access.countries, ["RU"]);
|
|
assert!(host.security_headers.hsts.enabled && host.security_headers.nosniff);
|
|
assert!(!host.transient_unavailable);
|
|
let plain = serde_json::to_value(Assignment::default()).unwrap();
|
|
for key in ["timing_headers", "method_policy", "rate_limit", "security_headers"] {
|
|
assert!(plain.get(key).is_none(), "{key} must not be stored when unused");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn assignment_from_the_removed_login_option_still_loads_and_drops_it() {
|
|
let assignment: Assignment = serde_json::from_value(serde_json::json!({
|
|
"service_key": "container:app:8080/tcp",
|
|
"hostname": "app",
|
|
"login_vhost_id": "sso"
|
|
}))
|
|
.unwrap();
|
|
assert!(serde_json::to_value(&assignment).unwrap().get("login_vhost_id").is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn docker_assignment_basic_auth_protects_the_generated_vhost() {
|
|
let mut desired = crate::config::demo_config("127.0.0.1:9000".parse().unwrap());
|
|
let hash = crate::auth::hash_sync("Correct-Horse-12345").unwrap();
|
|
let assignment: Assignment = serde_json::from_value(serde_json::json!({
|
|
"service_key": "container:app:8080/tcp",
|
|
"hostname": "app",
|
|
"basic_auth": {"enabled": true, "realm": "Ops", "require_tls": false,
|
|
"users": [{"username": "ops", "password_hash": hash}]}
|
|
}))
|
|
.unwrap();
|
|
desired.docker_agents.agents.push(security_agent(assignment.clone()));
|
|
let effective = effective_config(desired.clone(), &observation(service(), vec![])).unwrap();
|
|
let host = effective.vhosts.last().unwrap();
|
|
assert!(host.basic_auth.enabled && !host.basic_auth.require_tls);
|
|
assert_eq!(host.basic_auth.users[0].username, "ops");
|
|
assert_eq!(
|
|
host.authentication.map(|choice| choice.provider),
|
|
Some(crate::auth_provider::IdentityProvider::Basic)
|
|
);
|
|
let mut settings = desired.docker_agents.clone();
|
|
settings.validate(&desired.listeners).unwrap();
|
|
settings.agents[0].assignments[0].basic_auth.as_mut().unwrap().users.clear();
|
|
let error = settings.validate(&desired.listeners).unwrap_err();
|
|
assert!(format!("{error:#}").contains("basic.users_required"), "{error:#}");
|
|
let mut off = assignment;
|
|
off.basic_auth.as_mut().unwrap().enabled = false;
|
|
desired.docker_agents.agents[0].assignments[0] = off;
|
|
let effective = effective_config(desired, &observation(service(), vec![])).unwrap();
|
|
let host = effective.vhosts.last().unwrap();
|
|
assert!(!host.basic_auth.enabled && host.authentication.is_none());
|
|
}
|
|
}
|