harborshield 0.1.0

A Rust port of Whalewall, to automate management of firewall rules for Docker containers
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
use petgraph::algo::{is_cyclic_directed, toposort};
use petgraph::graph::{DiGraph, NodeIndex};
use std::collections::HashMap;
use tracing::warn;

/// Docker Compose label constants
pub const COMPOSE_PROJECT_LABEL: &str = "com.docker.compose.project";
pub const COMPOSE_SERVICE_LABEL: &str = "com.docker.compose.service";
pub const COMPOSE_CONTAINER_NUMBER_LABEL: &str = "com.docker.compose.container-number";
pub const COMPOSE_DEPENDS_ON_LABEL: &str = "com.docker.compose.depends_on";
pub const COMPOSE_VERSION_LABEL: &str = "com.docker.compose.version";
pub const COMPOSE_ONEOFF_LABEL: &str = "com.docker.compose.oneoff";
pub const COMPOSE_CONFIG_HASH_LABEL: &str = "com.docker.compose.config-hash";
pub const COMPOSE_PROJECT_CONFIG_FILES_LABEL: &str = "com.docker.compose.project.config_files";
pub const COMPOSE_PROJECT_WORKING_DIR_LABEL: &str = "com.docker.compose.project.working_dir";

/// Information extracted from Docker Compose labels
#[derive(Debug, Clone, Default)]
pub struct ComposeInfo {
    pub project: Option<String>,
    pub service: Option<String>,
    pub container_number: Option<u32>,
    pub depends_on: Vec<String>,
    pub version: Option<String>,
    pub is_oneoff: bool,
}

impl ComposeInfo {
    /// Extract compose information from container labels
    pub fn from_labels(labels: &HashMap<String, String>) -> Self {
        let mut info = Self::default();

        // Extract basic compose information
        info.project = labels.get(COMPOSE_PROJECT_LABEL).cloned();
        info.service = labels.get(COMPOSE_SERVICE_LABEL).cloned();

        // Parse container number
        if let Some(num_str) = labels.get(COMPOSE_CONTAINER_NUMBER_LABEL) {
            info.container_number = num_str.parse().ok();
        }

        // Parse dependencies
        if let Some(deps_str) = labels.get(COMPOSE_DEPENDS_ON_LABEL) {
            info.depends_on = deps_str
                .split(',')
                .map(|s| s.trim().to_string())
                .filter(|s| !s.is_empty())
                .collect();
        }

        // Extract version
        info.version = labels.get(COMPOSE_VERSION_LABEL).cloned();

        // Check if it's a one-off container
        info.is_oneoff = labels
            .get(COMPOSE_ONEOFF_LABEL)
            .map(|v| v == "True" || v == "true")
            .unwrap_or(false);

        info
    }

    /// Generate aliases based on compose information
    pub fn generate_aliases(&self) -> Vec<String> {
        let mut aliases = Vec::new();

        if let Some(service) = &self.service {
            // Add the service name as an alias
            aliases.push(service.clone());

            // Add project-qualified service name if project is specified
            if let Some(project) = &self.project {
                aliases.push(format!("{}.{}", service, project));
                aliases.push(format!("{}_{}", project, service));
            }

            // Add numbered service name if container number is specified and > 1
            if let Some(num) = self.container_number {
                if num > 1 {
                    aliases.push(format!("{}_{}", service, num));

                    if let Some(project) = &self.project {
                        aliases.push(format!("{}_{}_{}", project, service, num));
                    }
                }
            }
        }

        aliases
    }

    /// Check if this container has dependencies
    pub fn has_dependencies(&self) -> bool {
        !self.depends_on.is_empty()
    }

    /// Get a display name for the container
    pub fn display_name(&self) -> String {
        match (&self.project, &self.service, self.container_number) {
            (Some(project), Some(service), Some(num)) if num > 1 => {
                format!("{}_{}_{}", project, service, num)
            }
            (Some(project), Some(service), _) => {
                format!("{}_{}", project, service)
            }
            (None, Some(service), Some(num)) if num > 1 => {
                format!("{}_{}", service, num)
            }
            (None, Some(service), _) => service.clone(),
            _ => String::new(),
        }
    }
}

/// Sort containers by their Docker Compose dependencies using topological sort
pub fn sort_by_dependencies(containers: &mut [bollard::models::ContainerSummary]) {
    // Build a graph of container dependencies
    let mut graph = DiGraph::<String, ()>::new();
    let mut service_to_node: HashMap<String, NodeIndex> = HashMap::new();
    let mut node_to_index: HashMap<NodeIndex, usize> = HashMap::new();

    // First pass: add all containers as nodes
    for (index, container) in containers.iter().enumerate() {
        let labels = container.labels.as_ref().cloned().unwrap_or_default();
        let info = ComposeInfo::from_labels(&labels);

        if let Some(service) = &info.service {
            let node = graph.add_node(service.clone());
            service_to_node.insert(service.clone(), node);
            node_to_index.insert(node, index);
        }
    }

    // Second pass: add edges for dependencies
    for container in containers.iter() {
        let labels = container.labels.as_ref().cloned().unwrap_or_default();
        let info = ComposeInfo::from_labels(&labels);

        if let Some(service) = &info.service {
            if let Some(&from_node) = service_to_node.get(service) {
                for dep in &info.depends_on {
                    if let Some(&to_node) = service_to_node.get(dep) {
                        // Edge direction: from dependent to dependency
                        // This ensures dependencies come before dependents in topological sort
                        graph.add_edge(from_node, to_node, ());
                    } else {
                        warn!(
                            "Container '{}' depends on '{}', but '{}' was not found in the container list",
                            service, dep, dep
                        );
                    }
                }
            }
        }
    }

    // Check for circular dependencies
    if is_cyclic_directed(&graph) {
        warn!(
            "Circular dependencies detected in Docker Compose containers. Falling back to simple sort."
        );
        // Fall back to the simple sort
        sort_by_dependencies_simple(containers);
        return;
    }

    // Perform topological sort
    match toposort(&graph, None) {
        Ok(sorted_nodes) => {
            // Create a new order based on the topological sort
            let mut sorted_containers = Vec::with_capacity(containers.len());
            let mut processed_indices = std::collections::HashSet::new();

            // Add containers in topologically sorted order (reversed because dependencies should come first)
            for node in sorted_nodes.into_iter().rev() {
                if let Some(&index) = node_to_index.get(&node) {
                    sorted_containers.push(containers[index].clone());
                    processed_indices.insert(index);
                }
            }

            // Add any containers that weren't in the graph (e.g., containers without compose labels)
            for (index, container) in containers.iter().enumerate() {
                if !processed_indices.contains(&index) {
                    sorted_containers.push(container.clone());
                }
            }

            // Replace the original vector with the sorted one
            containers.clone_from_slice(&sorted_containers);
        }
        Err(_) => {
            warn!("Failed to perform topological sort. This should not happen after cycle check.");
            sort_by_dependencies_simple(containers);
        }
    }
}

/// Simple sort implementation (original behavior)
fn sort_by_dependencies_simple(containers: &mut [bollard::models::ContainerSummary]) {
    containers.sort_by(|a, b| {
        let a_labels = a.labels.as_ref().cloned().unwrap_or_default();
        let b_labels = b.labels.as_ref().cloned().unwrap_or_default();

        let a_info = ComposeInfo::from_labels(&a_labels);
        let b_info = ComposeInfo::from_labels(&b_labels);

        match (a_info.has_dependencies(), b_info.has_dependencies()) {
            (false, true) => std::cmp::Ordering::Less, // No deps come first
            (true, false) => std::cmp::Ordering::Greater, // Has deps come later
            _ => std::cmp::Ordering::Equal,
        }
    });
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_compose_info_from_labels() {
        let mut labels = HashMap::new();
        labels.insert(COMPOSE_PROJECT_LABEL.to_string(), "myproject".to_string());
        labels.insert(COMPOSE_SERVICE_LABEL.to_string(), "web".to_string());
        labels.insert(COMPOSE_CONTAINER_NUMBER_LABEL.to_string(), "2".to_string());
        labels.insert(
            COMPOSE_DEPENDS_ON_LABEL.to_string(),
            "db, cache".to_string(),
        );
        labels.insert(COMPOSE_VERSION_LABEL.to_string(), "2.4".to_string());
        labels.insert(COMPOSE_ONEOFF_LABEL.to_string(), "False".to_string());

        let info = ComposeInfo::from_labels(&labels);

        assert_eq!(info.project, Some("myproject".to_string()));
        assert_eq!(info.service, Some("web".to_string()));
        assert_eq!(info.container_number, Some(2));
        assert_eq!(info.depends_on, vec!["db", "cache"]);
        assert_eq!(info.version, Some("2.4".to_string()));
        assert!(!info.is_oneoff);
    }

    #[test]
    fn test_generate_aliases() {
        let info = ComposeInfo {
            project: Some("myproject".to_string()),
            service: Some("web".to_string()),
            container_number: Some(2),
            ..Default::default()
        };

        let aliases = info.generate_aliases();
        assert!(aliases.contains(&"web".to_string()));
        assert!(aliases.contains(&"web.myproject".to_string()));
        assert!(aliases.contains(&"myproject_web".to_string()));
        assert!(aliases.contains(&"web_2".to_string()));
        assert!(aliases.contains(&"myproject_web_2".to_string()));
    }

    #[test]
    fn test_generate_aliases_single_container() {
        let info = ComposeInfo {
            project: Some("myproject".to_string()),
            service: Some("db".to_string()),
            container_number: Some(1),
            ..Default::default()
        };

        let aliases = info.generate_aliases();
        assert!(aliases.contains(&"db".to_string()));
        assert!(aliases.contains(&"db.myproject".to_string()));
        assert!(aliases.contains(&"myproject_db".to_string()));
        // Should not contain numbered aliases for container_number = 1
        assert!(!aliases.contains(&"db_1".to_string()));
    }

    #[test]
    fn test_display_name() {
        let info1 = ComposeInfo {
            project: Some("myproject".to_string()),
            service: Some("web".to_string()),
            container_number: Some(2),
            ..Default::default()
        };
        assert_eq!(info1.display_name(), "myproject_web_2");

        let info2 = ComposeInfo {
            project: Some("myproject".to_string()),
            service: Some("db".to_string()),
            container_number: Some(1),
            ..Default::default()
        };
        assert_eq!(info2.display_name(), "myproject_db");

        let info3 = ComposeInfo {
            service: Some("redis".to_string()),
            ..Default::default()
        };
        assert_eq!(info3.display_name(), "redis");
    }

    #[test]
    fn test_sort_by_dependencies() {
        use bollard::models::ContainerSummary;

        let mut containers = vec![
            ContainerSummary {
                id: Some("web".to_string()),
                labels: Some(HashMap::from([
                    (COMPOSE_SERVICE_LABEL.to_string(), "web".to_string()),
                    (COMPOSE_DEPENDS_ON_LABEL.to_string(), "db".to_string()),
                ])),
                ..Default::default()
            },
            ContainerSummary {
                id: Some("cache".to_string()),
                labels: Some(HashMap::from([(
                    COMPOSE_SERVICE_LABEL.to_string(),
                    "cache".to_string(),
                )])),
                ..Default::default()
            },
            ContainerSummary {
                id: Some("db".to_string()),
                labels: Some(HashMap::from([(
                    COMPOSE_SERVICE_LABEL.to_string(),
                    "db".to_string(),
                )])),
                ..Default::default()
            },
        ];

        sort_by_dependencies(&mut containers);

        // Containers without dependencies should come first
        // db should come before web since web depends on db
        let ids: Vec<&str> = containers
            .iter()
            .filter_map(|c| c.id.as_ref().map(|s| s.as_str()))
            .collect();

        // cache and db have no dependencies, so they should come first
        // web depends on db, so it should come last
        assert!(
            ids.iter().position(|&id| id == "db").unwrap()
                < ids.iter().position(|&id| id == "web").unwrap()
        );
    }

    #[test]
    fn test_sort_by_dependencies_chain() {
        use bollard::models::ContainerSummary;

        // Test a->b->c dependency chain
        let mut containers = vec![
            ContainerSummary {
                id: Some("c".to_string()),
                labels: Some(HashMap::from([(
                    COMPOSE_SERVICE_LABEL.to_string(),
                    "c".to_string(),
                )])),
                ..Default::default()
            },
            ContainerSummary {
                id: Some("a".to_string()),
                labels: Some(HashMap::from([
                    (COMPOSE_SERVICE_LABEL.to_string(), "a".to_string()),
                    (COMPOSE_DEPENDS_ON_LABEL.to_string(), "b".to_string()),
                ])),
                ..Default::default()
            },
            ContainerSummary {
                id: Some("b".to_string()),
                labels: Some(HashMap::from([
                    (COMPOSE_SERVICE_LABEL.to_string(), "b".to_string()),
                    (COMPOSE_DEPENDS_ON_LABEL.to_string(), "c".to_string()),
                ])),
                ..Default::default()
            },
        ];

        sort_by_dependencies(&mut containers);

        let ids: Vec<&str> = containers
            .iter()
            .filter_map(|c| c.id.as_ref().map(|s| s.as_str()))
            .collect();

        // Order should be c, b, a
        assert_eq!(ids, vec!["c", "b", "a"]);
    }

    #[test]
    fn test_sort_by_dependencies_circular() {
        use bollard::models::ContainerSummary;

        // Test circular dependency: a->b->c->a
        let mut containers = vec![
            ContainerSummary {
                id: Some("a".to_string()),
                labels: Some(HashMap::from([
                    (COMPOSE_SERVICE_LABEL.to_string(), "a".to_string()),
                    (COMPOSE_DEPENDS_ON_LABEL.to_string(), "b".to_string()),
                ])),
                ..Default::default()
            },
            ContainerSummary {
                id: Some("b".to_string()),
                labels: Some(HashMap::from([
                    (COMPOSE_SERVICE_LABEL.to_string(), "b".to_string()),
                    (COMPOSE_DEPENDS_ON_LABEL.to_string(), "c".to_string()),
                ])),
                ..Default::default()
            },
            ContainerSummary {
                id: Some("c".to_string()),
                labels: Some(HashMap::from([
                    (COMPOSE_SERVICE_LABEL.to_string(), "c".to_string()),
                    (COMPOSE_DEPENDS_ON_LABEL.to_string(), "a".to_string()),
                ])),
                ..Default::default()
            },
        ];

        // Should fall back to simple sort without panicking
        sort_by_dependencies(&mut containers);

        // All containers have dependencies, so order might not change much
        // The important thing is that it doesn't panic
        assert_eq!(containers.len(), 3);
    }

    #[test]
    fn test_sort_by_dependencies_missing_dependency() {
        use bollard::models::ContainerSummary;

        // Test when a container depends on a non-existent service
        let mut containers = vec![
            ContainerSummary {
                id: Some("web".to_string()),
                labels: Some(HashMap::from([
                    (COMPOSE_SERVICE_LABEL.to_string(), "web".to_string()),
                    (
                        COMPOSE_DEPENDS_ON_LABEL.to_string(),
                        "db,nonexistent".to_string(),
                    ),
                ])),
                ..Default::default()
            },
            ContainerSummary {
                id: Some("db".to_string()),
                labels: Some(HashMap::from([(
                    COMPOSE_SERVICE_LABEL.to_string(),
                    "db".to_string(),
                )])),
                ..Default::default()
            },
        ];

        sort_by_dependencies(&mut containers);

        let ids: Vec<&str> = containers
            .iter()
            .filter_map(|c| c.id.as_ref().map(|s| s.as_str()))
            .collect();

        // db should still come before web
        assert!(
            ids.iter().position(|&id| id == "db").unwrap()
                < ids.iter().position(|&id| id == "web").unwrap()
        );
    }

    #[test]
    fn test_sort_by_dependencies_complex() {
        use bollard::models::ContainerSummary;

        // Test complex dependency graph
        // frontend -> api -> (db, cache)
        // worker -> (api, queue)
        // queue has no dependencies
        let mut containers = vec![
            ContainerSummary {
                id: Some("worker".to_string()),
                labels: Some(HashMap::from([
                    (COMPOSE_SERVICE_LABEL.to_string(), "worker".to_string()),
                    (
                        COMPOSE_DEPENDS_ON_LABEL.to_string(),
                        "api,queue".to_string(),
                    ),
                ])),
                ..Default::default()
            },
            ContainerSummary {
                id: Some("frontend".to_string()),
                labels: Some(HashMap::from([
                    (COMPOSE_SERVICE_LABEL.to_string(), "frontend".to_string()),
                    (COMPOSE_DEPENDS_ON_LABEL.to_string(), "api".to_string()),
                ])),
                ..Default::default()
            },
            ContainerSummary {
                id: Some("api".to_string()),
                labels: Some(HashMap::from([
                    (COMPOSE_SERVICE_LABEL.to_string(), "api".to_string()),
                    (COMPOSE_DEPENDS_ON_LABEL.to_string(), "db,cache".to_string()),
                ])),
                ..Default::default()
            },
            ContainerSummary {
                id: Some("db".to_string()),
                labels: Some(HashMap::from([(
                    COMPOSE_SERVICE_LABEL.to_string(),
                    "db".to_string(),
                )])),
                ..Default::default()
            },
            ContainerSummary {
                id: Some("cache".to_string()),
                labels: Some(HashMap::from([(
                    COMPOSE_SERVICE_LABEL.to_string(),
                    "cache".to_string(),
                )])),
                ..Default::default()
            },
            ContainerSummary {
                id: Some("queue".to_string()),
                labels: Some(HashMap::from([(
                    COMPOSE_SERVICE_LABEL.to_string(),
                    "queue".to_string(),
                )])),
                ..Default::default()
            },
        ];

        sort_by_dependencies(&mut containers);

        let ids: Vec<&str> = containers
            .iter()
            .filter_map(|c| c.id.as_ref().map(|s| s.as_str()))
            .collect();

        // Check key ordering constraints
        let pos = |id| ids.iter().position(|&x| x == id).unwrap();

        // db and cache should come before api
        assert!(pos("db") < pos("api"));
        assert!(pos("cache") < pos("api"));

        // api should come before frontend and worker
        assert!(pos("api") < pos("frontend"));
        assert!(pos("api") < pos("worker"));

        // queue should come before worker
        assert!(pos("queue") < pos("worker"));
    }
}