|
| 1 | +use std::time::Duration; |
| 2 | + |
| 3 | +use scannerlib::models::{self, Phase, Scan, Status, Target}; |
| 4 | +use serde::{Deserialize, Serialize}; |
| 5 | + |
| 6 | +use super::test_builder::{Snapshottable, Test, WaitForStatusExt}; |
| 7 | +use crate::container_image_scanner::DockerRegistryV2Mock; |
| 8 | + |
| 9 | +#[derive(Serialize, Deserialize)] |
| 10 | +#[serde(transparent)] |
| 11 | +struct RegistryResults(Vec<models::Result>); |
| 12 | + |
| 13 | +impl Snapshottable for RegistryResults { |
| 14 | + fn prepare(&mut self) { |
| 15 | + let address = lazy_regex::regex!(r"127\.0\.0\.1:\d+"); |
| 16 | + for result in &mut self.0 { |
| 17 | + for value in [&mut result.hostname, &mut result.message] |
| 18 | + .into_iter() |
| 19 | + .flatten() |
| 20 | + { |
| 21 | + *value = address.replace_all(value, "registry").into_owned(); |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +fn registry_scan(id: &str, hosts: Vec<String>) -> Scan { |
| 28 | + Scan { |
| 29 | + scan_id: id.into(), |
| 30 | + target: Target { |
| 31 | + hosts, |
| 32 | + ..Default::default() |
| 33 | + }, |
| 34 | + scan_preferences: vec![("registry_allow_insecure", "true").into()], |
| 35 | + ..Default::default() |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +#[tokio::test] |
| 40 | +async fn registry_errors_are_reported() { |
| 41 | + let t = Test::new("registry_errors") |
| 42 | + .config("cis_registry_errors") |
| 43 | + .await; |
| 44 | + let mut registry = mockito::Server::new_async().await; |
| 45 | + let _auth = registry.mock("GET", "/v2/").with_status(503).create(); |
| 46 | + let mut image = DockerRegistryV2Mock::supported_images().remove(0); |
| 47 | + let blob_registry = |
| 48 | + DockerRegistryV2Mock::serve_images(&[image.clone()], &[200, 200, 200, 200, 200, 200, 503]) |
| 49 | + .await; |
| 50 | + image.registry = blob_registry.address().into(); |
| 51 | + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); |
| 52 | + let unreachable = listener.local_addr().unwrap(); |
| 53 | + drop(listener); |
| 54 | + |
| 55 | + for (id, host) in [ |
| 56 | + ( |
| 57 | + "auth-503", |
| 58 | + format!("oci://{}/repository:latest", registry.host_with_port()), |
| 59 | + ), |
| 60 | + ("blob-503", image.to_string()), |
| 61 | + ("invalid-image", String::new()), |
| 62 | + ("unreachable-catalog", format!("oci://{unreachable}")), |
| 63 | + ( |
| 64 | + "unreachable-repository", |
| 65 | + format!("oci://{unreachable}/repository"), |
| 66 | + ), |
| 67 | + ( |
| 68 | + "unreachable-image", |
| 69 | + format!("oci://{unreachable}/repository:latest"), |
| 70 | + ), |
| 71 | + ] { |
| 72 | + let scan = t |
| 73 | + .create_container_image_scan(registry_scan(id, vec![host])) |
| 74 | + .await; |
| 75 | + scan.start().await; |
| 76 | + scan.wait_for(Phase::Succeeded.with_timeout(Duration::from_secs(10))) |
| 77 | + .await |
| 78 | + .body::<Status>() |
| 79 | + .snapshot("status"); |
| 80 | + scan.get_results() |
| 81 | + .await |
| 82 | + .body::<RegistryResults>() |
| 83 | + .snapshot("results"); |
| 84 | + scan.delete().await; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +#[tokio::test] |
| 89 | +async fn registry_error_does_not_interrupt_other_images() { |
| 90 | + let t = Test::new("mixed_registry_results") |
| 91 | + .config("cis_registry_errors") |
| 92 | + .await; |
| 93 | + let mut registry = mockito::Server::new_async().await; |
| 94 | + let _auth = registry.mock("GET", "/v2/").with_status(503).create(); |
| 95 | + let healthy_registry = DockerRegistryV2Mock::serve_default().await; |
| 96 | + let mut hosts = vec![format!("oci://{}", registry.host_with_port())]; |
| 97 | + hosts.extend( |
| 98 | + DockerRegistryV2Mock::supported_images() |
| 99 | + .into_iter() |
| 100 | + .map(|mut image| { |
| 101 | + image.registry = healthy_registry.address().into(); |
| 102 | + image.to_string() |
| 103 | + }), |
| 104 | + ); |
| 105 | + let scan = t |
| 106 | + .create_container_image_scan(registry_scan("mixed", hosts)) |
| 107 | + .await; |
| 108 | + scan.start().await; |
| 109 | + scan.wait_for(Phase::Succeeded.with_timeout(Duration::from_secs(10))) |
| 110 | + .await |
| 111 | + .body::<Status>() |
| 112 | + .snapshot("status"); |
| 113 | + scan.get_result(0..=0) |
| 114 | + .await |
| 115 | + .body::<RegistryResults>() |
| 116 | + .snapshot("discovery_error"); |
| 117 | + scan.delete().await; |
| 118 | +} |
0 commit comments