nanodock 0.1.0

Zero-dependency-light Docker/Podman daemon client for container detection, port mapping, and lifecycle control
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//! Docker/Podman API JSON response parsing.
//!
//! Deserialises the `GET /containers/json` payload and maps published
//! ports to [`ContainerInfo`] records.

use std::net::IpAddr;

use serde::Deserialize;

use crate::{ContainerInfo, ContainerPortMap, Protocol};

#[derive(Deserialize)]
struct DockerPort<'a> {
    #[serde(
        rename = "IP",
        alias = "host_ip",
        default,
        deserialize_with = "deserialize_host_ip"
    )]
    host_ip: Option<IpAddr>,
    #[serde(rename = "PublicPort", alias = "host_port")]
    public_port: Option<u16>,
    #[serde(rename = "Type", alias = "protocol")]
    proto: Option<&'a str>,
    #[serde(alias = "range")]
    port_range: Option<u16>,
}

#[derive(Deserialize)]
struct DockerContainer<'a> {
    #[serde(rename = "Id")]
    id: Option<&'a str>,
    #[serde(rename = "Names")]
    names: Option<Vec<&'a str>>,
    #[serde(rename = "Image")]
    image: Option<&'a str>,
    #[serde(rename = "Ports")]
    ports: Option<Vec<DockerPort<'a>>>,
}

/// Parse the JSON response from `GET /containers/json` into a port map.
///
/// Each container may publish multiple ports. The map keys are
/// `(public_ip, public_port, protocol)` tuples.
#[must_use]
pub fn parse_containers_json(json_body: &str) -> ContainerPortMap {
    let mut map = ContainerPortMap::new();

    let Ok(containers) = serde_json::from_str::<Vec<DockerContainer<'_>>>(json_body) else {
        return map;
    };

    populate_port_map(&mut map, &containers);
    map
}

/// Strict variant of [`parse_containers_json`] that propagates JSON
/// deserialization errors instead of silently returning an empty map.
pub fn parse_containers_json_strict(
    json_body: &str,
) -> Result<ContainerPortMap, serde_json::Error> {
    let containers = serde_json::from_str::<Vec<DockerContainer<'_>>>(json_body)?;
    let mut map = ContainerPortMap::new();
    populate_port_map(&mut map, &containers);
    Ok(map)
}

fn populate_port_map(map: &mut ContainerPortMap, containers: &[DockerContainer<'_>]) {
    for container in containers {
        let id = container.id.unwrap_or("").to_string();
        let name = container_display_name(container);
        let image = container.image.unwrap_or("").to_string();
        let info = ContainerInfo { id, name, image };

        let Some(ports) = &container.ports else {
            continue;
        };

        for port in ports {
            let Some(public_port) = port.public_port else {
                continue;
            };
            let Some(proto) = parse_port_protocol(port.proto) else {
                continue;
            };

            let port_count = port.port_range.unwrap_or(1);
            for offset in 0..port_count {
                let Some(mapped_port) = public_port.checked_add(offset) else {
                    break;
                };

                map.insert((port.host_ip, mapped_port, proto), info.clone());
            }
        }
    }
}

fn deserialize_host_ip<'de, D>(deserializer: D) -> Result<Option<IpAddr>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let value = Option::<String>::deserialize(deserializer)?;
    value
        .as_deref()
        .map(str::trim)
        .filter(|ip| !ip.is_empty())
        .map(str::parse)
        .transpose()
        .map(|host_ip| host_ip.filter(|ip: &IpAddr| !ip.is_unspecified()))
        .map_err(serde::de::Error::custom)
}

const fn parse_port_protocol(proto: Option<&str>) -> Option<Protocol> {
    match proto {
        None => Some(Protocol::Tcp),
        Some(value) if value.eq_ignore_ascii_case("tcp") => Some(Protocol::Tcp),
        Some(value) if value.eq_ignore_ascii_case("udp") => Some(Protocol::Udp),
        Some(_) => None,
    }
}

fn container_display_name(container: &DockerContainer<'_>) -> String {
    container
        .names
        .as_ref()
        .and_then(|names| names.iter().copied().find_map(normalize_container_name))
        .or_else(|| {
            container
                .image
                .map(str::trim)
                .filter(|image| !image.is_empty())
                .map(ToOwned::to_owned)
        })
        .or_else(|| {
            container
                .id
                .map(str::trim)
                .filter(|id| !id.is_empty())
                .map(short_container_id)
        })
        .unwrap_or_else(|| "container".to_string())
}

fn normalize_container_name(name: &str) -> Option<String> {
    let normalized = name.trim().trim_start_matches('/');
    (!normalized.is_empty()).then(|| normalized.to_string())
}

/// Truncate a full container ID to its 12-character short form.
///
/// Docker/Podman container IDs are hex-encoded (ASCII-only), so byte
/// length equals character count and a byte slice is safe.
#[must_use]
pub fn short_container_id(id: &str) -> String {
    id.get(..12).unwrap_or(id).to_string()
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::net::{IpAddr, Ipv4Addr};

    use crate::Protocol;

    const SAMPLE_RESPONSE: &str = r#"[
        {
            "Id": "abc123def456",
            "Names": ["/backend-postgres-1"],
            "Image": "postgres:16",
            "Ports": [
                {"PrivatePort": 5432, "PublicPort": 5432, "Type": "tcp"}
            ]
        },
        {
            "Id": "789ghi012jkl",
            "Names": ["/backend-redis-1"],
            "Image": "redis:7-alpine",
            "Ports": [
                {"PrivatePort": 6379, "PublicPort": 6379, "Type": "tcp"}
            ]
        },
        {
            "Names": ["/no-ports"],
            "Image": "busybox",
            "Ports": []
        }
    ]"#;

    fn mapped_container(
        map: &ContainerPortMap,
        host_ip: Option<IpAddr>,
        public_port: u16,
        proto: Protocol,
    ) -> &ContainerInfo {
        map.get(&(host_ip, public_port, proto))
            .expect("expected container port mapping to exist")
    }

    fn assert_container_mapping(
        map: &ContainerPortMap,
        host_ip: Option<IpAddr>,
        public_port: u16,
        proto: Protocol,
        expected_name: &str,
        expected_image: &str,
    ) {
        let info = mapped_container(map, host_ip, public_port, proto);
        assert_eq!(info.name, expected_name);
        assert_eq!(info.image, expected_image);
    }

    #[test]
    fn parse_valid_response() {
        let map = parse_containers_json(SAMPLE_RESPONSE);
        assert_eq!(map.len(), 2);

        assert_container_mapping(
            &map,
            None,
            5432,
            Protocol::Tcp,
            "backend-postgres-1",
            "postgres:16",
        );
        assert_container_mapping(
            &map,
            None,
            6379,
            Protocol::Tcp,
            "backend-redis-1",
            "redis:7-alpine",
        );
    }

    #[test]
    fn parse_empty_array() {
        let map = parse_containers_json("[]");
        assert!(map.is_empty());
    }

    #[test]
    fn parse_invalid_json_returns_empty() {
        let map = parse_containers_json("not json");
        assert!(map.is_empty());
    }

    #[test]
    fn parse_container_without_public_port() {
        let json = r#"[{
            "Names": ["/internal"],
            "Image": "app:latest",
            "Ports": [{"PrivatePort": 8080, "Type": "tcp"}]
        }]"#;
        let map = parse_containers_json(json);
        assert!(
            map.is_empty(),
            "entries without PublicPort should be skipped"
        );
    }

    #[test]
    fn container_name_strips_leading_slash() {
        let json = r#"[{
            "Names": ["/my-container"],
            "Image": "nginx:latest",
            "Ports": [{"PrivatePort": 80, "PublicPort": 80, "Type": "tcp"}]
        }]"#;
        let map = parse_containers_json(json);
        assert_container_mapping(
            &map,
            None,
            80,
            Protocol::Tcp,
            "my-container",
            "nginx:latest",
        );
    }

    #[test]
    fn parse_multiple_ports_same_container() {
        let json = r#"[{
            "Names": ["/multi"],
            "Image": "app:latest",
            "Ports": [
                {"PrivatePort": 80, "PublicPort": 8080, "Type": "tcp"},
                {"PrivatePort": 443, "PublicPort": 8443, "Type": "tcp"}
            ]
        }]"#;
        let map = parse_containers_json(json);
        assert_eq!(map.len(), 2);
        assert!(map.contains_key(&(None, 8080, Protocol::Tcp)));
        assert!(map.contains_key(&(None, 8443, Protocol::Tcp)));
    }

    #[test]
    fn parse_missing_protocol_defaults_to_tcp() {
        let json = r#"[{
            "Names": ["/web"],
            "Image": "nginx:latest",
            "Ports": [{"PrivatePort": 80, "PublicPort": 8080}]
        }]"#;
        let map = parse_containers_json(json);
        assert!(
            map.contains_key(&(None, 8080, Protocol::Tcp)),
            "missing Type should default to TCP"
        );
    }

    #[test]
    fn parse_protocol_matching_is_case_insensitive() {
        let json = r#"[{
            "Names": ["/dns"],
            "Image": "bind9:latest",
            "Ports": [{"PrivatePort": 53, "PublicPort": 5353, "Type": "UDP"}]
        }]"#;
        let map = parse_containers_json(json);

        assert!(
            map.contains_key(&(None, 5353, Protocol::Udp)),
            "protocol parsing should accept uppercase protocol tokens"
        );
    }

    #[test]
    fn parse_unsupported_protocol_is_skipped() {
        let json = r#"[{
            "Names": ["/sigtran"],
            "Image": "telecom:latest",
            "Ports": [{"PrivatePort": 2905, "PublicPort": 2905, "Type": "sctp"}]
        }]"#;
        let map = parse_containers_json(json);

        assert!(
            map.is_empty(),
            "unsupported protocols should not be coerced into TCP bindings"
        );
    }

    #[test]
    fn parse_podman_style_ports_with_empty_host_ip() {
        let json = r#"[{
            "Names": ["ensurily-postgres-dev"],
            "Image": "docker.io/library/postgres:14-alpine",
            "Ports": [{"host_ip": "", "container_port": 5432, "host_port": 5432, "range": 1, "protocol": "tcp"}]
        }]"#;
        let map = parse_containers_json(json);

        assert_container_mapping(
            &map,
            None,
            5432,
            Protocol::Tcp,
            "ensurily-postgres-dev",
            "docker.io/library/postgres:14-alpine",
        );
    }

    #[test]
    fn parse_podman_style_ports_expand_ranges() {
        let json = r#"[{
            "Names": ["ensurily-localstack-dev"],
            "Image": "docker.io/localstack/localstack:latest",
            "Ports": [{"host_ip": "", "container_port": 4510, "host_port": 4510, "range": 3, "protocol": "tcp"}]
        }]"#;
        let map = parse_containers_json(json);

        assert!(map.contains_key(&(None, 4510, Protocol::Tcp)));
        assert!(map.contains_key(&(None, 4511, Protocol::Tcp)));
        assert!(map.contains_key(&(None, 4512, Protocol::Tcp)));
    }

    #[test]
    fn parse_container_with_empty_name() {
        let json = r#"[{
            "Names": [],
            "Image": "app:latest",
            "Ports": [{"PrivatePort": 80, "PublicPort": 80, "Type": "tcp"}]
        }]"#;
        let map = parse_containers_json(json);
        assert_eq!(
            mapped_container(&map, None, 80, Protocol::Tcp).name,
            "app:latest",
            "containers without names should fall back to their image"
        );
    }

    #[test]
    fn parse_container_without_name_or_image_uses_short_id() {
        let json = r#"[{
            "Id": "0123456789abcdef0123456789abcdef",
            "Names": ["/"],
            "Ports": [{"PrivatePort": 80, "PublicPort": 80, "Type": "tcp"}]
        }]"#;
        let map = parse_containers_json(json);
        let info = mapped_container(&map, None, 80, Protocol::Tcp);
        assert_eq!(
            info.name, "0123456789ab",
            "containers without names or images should fall back to a short id"
        );
    }

    #[test]
    fn parse_container_with_explicit_host_ip() {
        let json = r#"[{
            "Names": ["/api"],
            "Image": "node:22",
            "Ports": [{"IP": "127.0.0.1", "PrivatePort": 3000, "PublicPort": 8080, "Type": "tcp"}]
        }]"#;
        let map = parse_containers_json(json);

        assert_container_mapping(
            &map,
            Some(IpAddr::V4(Ipv4Addr::LOCALHOST)),
            8080,
            Protocol::Tcp,
            "api",
            "node:22",
        );
    }

    #[test]
    fn parse_docker_wildcard_host_ip_as_unspecified() {
        let json = r#"[{
            "Names": ["/postgres"],
            "Image": "postgres:16",
            "Ports": [{"IP": "0.0.0.0", "PrivatePort": 5432, "PublicPort": 5432, "Type": "tcp"}]
        }]"#;
        let map = parse_containers_json(json);

        assert_container_mapping(&map, None, 5432, Protocol::Tcp, "postgres", "postgres:16");
        assert!(
            !map.contains_key(&(Some(IpAddr::V4(Ipv4Addr::UNSPECIFIED)), 5432, Protocol::Tcp)),
            "unspecified IPv4 bindings should be normalized to the wildcard key"
        );
    }

    #[test]
    fn parse_ipv6_wildcard_host_ip_as_unspecified() {
        let json = r#"[{
            "Names": ["/dns"],
            "Image": "bind9:latest",
            "Ports": [{"IP": "::", "PrivatePort": 53, "PublicPort": 5353, "Type": "udp"}]
        }]"#;
        let map = parse_containers_json(json);

        assert_container_mapping(&map, None, 5353, Protocol::Udp, "dns", "bind9:latest");
        assert!(
            !map.contains_key(&(
                Some(IpAddr::V6(std::net::Ipv6Addr::UNSPECIFIED)),
                5353,
                Protocol::Udp
            )),
            "unspecified IPv6 bindings should be normalized to the wildcard key"
        );
    }

    #[test]
    fn parse_strict_returns_error_on_invalid_json() {
        let result = parse_containers_json_strict("not json");
        assert!(
            result.is_err(),
            "strict parser must propagate deserialization errors"
        );
    }

    #[test]
    fn parse_strict_succeeds_on_valid_json() {
        let map = parse_containers_json_strict(SAMPLE_RESPONSE)
            .expect("strict parser should succeed on valid JSON");
        assert_eq!(map.len(), 2);
        assert_container_mapping(
            &map,
            None,
            5432,
            Protocol::Tcp,
            "backend-postgres-1",
            "postgres:16",
        );
    }

    #[test]
    fn parse_strict_returns_empty_map_for_empty_array() {
        let map = parse_containers_json_strict("[]")
            .expect("strict parser should succeed on empty array");
        assert!(
            map.is_empty(),
            "empty JSON array should produce an empty map, not an error"
        );
    }
}