Skip to main content

omnyssh_core/ssh/services/
postgresql.rs

1//! PostgreSQL service provider.
2//!
3//! Detects PostgreSQL from probe output.
4
5use async_trait::async_trait;
6
7use super::ServiceProvider;
8use crate::event::ServiceKind;
9use crate::ssh::probe::ProbeOutput;
10
11/// PostgreSQL service provider.
12pub struct PostgreSQLProvider;
13
14#[async_trait]
15impl ServiceProvider for PostgreSQLProvider {
16    fn kind(&self) -> ServiceKind {
17        ServiceKind::PostgreSQL
18    }
19
20    fn detect(&self, probe_output: &ProbeOutput) -> bool {
21        // Check for postgresql service in systemd OR port 5432 in listening ports
22        if let Some(services) = probe_output.get_section("SERVICES") {
23            if services.contains("postgresql") {
24                return true;
25            }
26        }
27        if let Some(listen) = probe_output.get_section("LISTEN") {
28            if listen.contains(":5432") || listen.contains("5432") {
29                return true;
30            }
31        }
32        false
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[test]
41    fn test_pg_detect_from_systemd() {
42        let probe = "===OMNYSSH:SERVICES===\npostgresql.service\nsshd.service\n";
43        let parsed = ProbeOutput::parse(probe).expect("should parse");
44        assert!(PostgreSQLProvider.detect(&parsed));
45    }
46
47    #[test]
48    fn test_pg_detect_from_port() {
49        let probe = "===OMNYSSH:LISTEN===\n0.0.0.0:5432\tLISTEN\n";
50        let parsed = ProbeOutput::parse(probe).expect("should parse");
51        assert!(PostgreSQLProvider.detect(&parsed));
52    }
53}