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
use std::convert::TryFrom;

use log::warn;
use serde_json::{json, Value};
use uuid::Uuid;

use crate::error::Error;
use crate::graph_description::IpConnection;
use crate::node::NodeT;

pub enum IpConnectionState {
    Created,
    Existing,
    Terminated,
}

impl From<IpConnectionState> for u32 {
    fn from(p: IpConnectionState) -> u32 {
        match p {
            IpConnectionState::Created => 1,
            IpConnectionState::Terminated => 2,
            IpConnectionState::Existing => 3,
        }
    }
}

impl TryFrom<u32> for IpConnectionState {
    type Error = Error;

    fn try_from(p: u32) -> Result<IpConnectionState, Error> {
        match p {
            1 => Ok(IpConnectionState::Created),
            2 => Ok(IpConnectionState::Terminated),
            3 => Ok(IpConnectionState::Existing),
            _ => Err(Error::InvalidIpConnectionState(p)),
        }
    }
}

impl IpConnection {
    pub fn new(
        src_ip_address: impl Into<String>,
        dst_ip_address: impl Into<String>,
        protocol: impl Into<String>,
        state: IpConnectionState,
        created_timestamp: u64,
        terminated_timestamp: u64,
        last_seen_timestamp: u64,
    ) -> Self {
        let src_ip_address = src_ip_address.into();
        let dst_ip_address = dst_ip_address.into();
        let protocol = protocol.into();

        Self {
            node_key: Uuid::new_v4().to_string(),
            src_ip_address,
            dst_ip_address,
            protocol,
            state: state.into(),
            created_timestamp,
            terminated_timestamp,
            last_seen_timestamp,
        }
    }

    pub fn into_json(self) -> Value {
        let mut j = json!({
            "node_key": self.node_key,
            "dgraph.type": "IpConnection",
            "src_ip_address": self.src_ip_address,
            "dst_ip_address": self.dst_ip_address,
            "protocol": self.protocol,
        });

        if self.created_timestamp != 0 {
            j["created_timestamp"] = self.created_timestamp.into();
        }

        if self.terminated_timestamp != 0 {
            j["terminated_timestamp"] = self.terminated_timestamp.into();
        }

        if self.last_seen_timestamp != 0 {
            j["last_seen_timestamp"] = self.last_seen_timestamp.into();
        }

        j
    }
}

impl NodeT for IpConnection {
    fn get_asset_id(&self) -> Option<&str> {
        None
    }

    fn set_asset_id(&mut self, _asset_id: impl Into<String>) {
        panic!("Can not set asset_id on IpConnection");
    }

    fn get_node_key(&self) -> &str {
        &self.node_key
    }

    fn set_node_key(&mut self, node_key: impl Into<String>) {
        self.node_key = node_key.into();
    }

    fn merge(&mut self, other: &Self) -> bool {
        if self.node_key != other.node_key {
            warn!("Attempted to merge two IpConnection Nodes with differing node_keys");
            return false;
        }

        let mut merged = false;

        if self.created_timestamp == 0 || other.created_timestamp < self.created_timestamp {
            self.created_timestamp = other.created_timestamp;
            merged = true;
        }
        if self.terminated_timestamp == 0 || other.terminated_timestamp > self.terminated_timestamp
        {
            self.terminated_timestamp = other.terminated_timestamp;
            merged = true;
        }
        if self.last_seen_timestamp == 0 || other.last_seen_timestamp > self.last_seen_timestamp {
            self.last_seen_timestamp = other.last_seen_timestamp;
            merged = true;
        }

        merged
    }

    fn merge_into(&mut self, other: Self) -> bool {
        self.merge(&other)
    }
}