Skip to content

Commit 715a59d

Browse files
committed
Fix: CIS: Return results upon failed auth to registry.
1 parent c981200 commit 715a59d

24 files changed

Lines changed: 509 additions & 109 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[scanner]
2+
type = 'openvasd'
3+
4+
[notus]
5+
products_path = 'examples/feed/notus/products'
6+
advisories_path = 'examples/feed/notus/advisories'
7+
8+
[container_image_scanner.database]
9+
location = 'in-memory'
10+
11+
[container_image_scanner.image]
12+
batch_size = 1
13+
scanning_retries = 0

‎rust/src/openvasd/api_tests/mod.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use test_builder::{OpenvasdInstance, Snapshottable, Test, WaitForStatusExt};
1515

1616
use crate::container_image_scanner::DockerRegistryV2Mock;
1717

18+
mod registry_errors;
1819
mod test_builder;
1920
mod test_scan;
2021

@@ -491,7 +492,7 @@ async fn container_image_scanner_deadlock() {
491492

492493
scan.start().await;
493494

494-
scan.wait_for(Phase::Failed.with_timeout(Duration::from_secs(30)))
495+
scan.wait_for(Phase::Succeeded.with_timeout(Duration::from_secs(30)))
495496
.await
496497
.body::<Status>()
497498
.snapshot("status");
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
}

‎rust/src/openvasd/api_tests/snapshots/container_image_scanner_deadlock GET __container-image-scanner__scans__fake_id__status_status.snap‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ expression: inner
55
Status(
66
start_time: "[redacted]",
77
end_time: "[redacted]",
8-
status: failed,
8+
status: succeeded,
99
host_info: Some(HostInfo(
1010
all: 1,
1111
excluded: 0,
12-
dead: 1,
12+
dead: 0,
1313
alive: 0,
1414
queued: 0,
15-
finished: 0,
15+
finished: 1,
1616
)),
1717
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
source: src/openvasd/api_tests/test_builder.rs
3+
expression: inner
4+
---
5+
[
6+
Result(
7+
id: 0,
8+
type: error,
9+
ip_address: Some(""),
10+
hostname: Some("registry"),
11+
oid: Some("openvasd/container-image-scanner"),
12+
port: Some(0),
13+
message: Some("Error occurred on registry while operating on authentication (registry:catalog:*)"),
14+
detail: Some(Detail(
15+
name: "",
16+
value: "",
17+
source: Source(
18+
type: "",
19+
name: "",
20+
description: "",
21+
),
22+
)),
23+
),
24+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
source: src/openvasd/api_tests/test_builder.rs
3+
expression: inner
4+
---
5+
Status(
6+
start_time: "[redacted]",
7+
end_time: "[redacted]",
8+
status: succeeded,
9+
host_info: Some(HostInfo(
10+
all: 3,
11+
excluded: 0,
12+
dead: 0,
13+
alive: 2,
14+
queued: 0,
15+
finished: 3,
16+
)),
17+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
source: src/openvasd/api_tests/test_builder.rs
3+
expression: inner
4+
---
5+
[
6+
Result(
7+
id: 0,
8+
type: error,
9+
ip_address: Some(""),
10+
hostname: Some("oci://registry/repository:latest"),
11+
oid: Some("openvasd/container-image-scanner"),
12+
port: Some(0),
13+
message: Some("Error occurred on registry while operating on authentication (repository:repository:pull)"),
14+
detail: Some(Detail(
15+
name: "",
16+
value: "",
17+
source: Source(
18+
type: "",
19+
name: "",
20+
description: "",
21+
),
22+
)),
23+
),
24+
]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
source: src/openvasd/api_tests/test_builder.rs
3+
expression: inner
4+
---
5+
Status(
6+
start_time: "[redacted]",
7+
end_time: "[redacted]",
8+
status: succeeded,
9+
host_info: Some(HostInfo(
10+
all: 1,
11+
excluded: 0,
12+
dead: 0,
13+
alive: 1,
14+
queued: 0,
15+
finished: 1,
16+
)),
17+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
source: src/openvasd/api_tests/test_builder.rs
3+
expression: inner
4+
---
5+
[
6+
Result(
7+
id: 0,
8+
type: error,
9+
ip_address: Some(""),
10+
hostname: Some("oci://registry/nichtsfrei/victim:latest"),
11+
oid: Some("openvasd/container-image-scanner"),
12+
port: Some(0),
13+
message: Some("Error occurred on registry while operating on manifest"),
14+
detail: Some(Detail(
15+
name: "",
16+
value: "",
17+
source: Source(
18+
type: "",
19+
name: "",
20+
description: "",
21+
),
22+
)),
23+
),
24+
]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
source: src/openvasd/api_tests/test_builder.rs
3+
expression: inner
4+
---
5+
Status(
6+
start_time: "[redacted]",
7+
end_time: "[redacted]",
8+
status: succeeded,
9+
host_info: Some(HostInfo(
10+
all: 1,
11+
excluded: 0,
12+
dead: 0,
13+
alive: 1,
14+
queued: 0,
15+
finished: 1,
16+
)),
17+
)

0 commit comments

Comments
 (0)