Repository navigation
Expand file tree
/
Copy pathrecord.rs
More file actions
188 lines (161 loc) · 5.59 KB
/
Copy pathrecord.rs
File metadata and controls
188 lines (161 loc) · 5.59 KB
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
use crate::datalayer::error::RecordError;
use std::str::FromStr;
use tlsh2::TlshDefault;
/// Trait for Metric types (like TLSH or numerical IDs) to specify if they natively map to a Radix Tree exact-match key.
pub trait RadixKeyMapping {
fn to_radix_key(&self) -> Option<Vec<u8>> {
None
}
}
impl RadixKeyMapping for TlshDefault {
fn to_radix_key(&self) -> Option<Vec<u8>> {
Some(self.hash().to_vec())
}
}
impl RadixKeyMapping for u32 {
fn to_radix_key(&self) -> Option<Vec<u8>> {
Some(self.to_string().into_bytes())
}
}
/// The primary trait for any entity inserted into the Apotheosis Database.
/// Implement this trait on domain objects to define how they should be indexed
/// by the Radix exact-match tree and the HNSW proximity graph.
pub trait ApotheosisRecord {
type MetricId: Clone + RadixKeyMapping; // The mathematical/hashing space representation used by HNSW. E.g., `tlsh2::TlshDefault` or `u32`.
fn search_id(&self) -> Self::MetricId; // The metric representation (e.g., hash) used for similarity calculation in the HNSW graph.
fn get_attributes(&self) -> Vec<(String, String)> {
// Optional metadata key-value pairs associated with the ApotheosisRecord. These are exported during model visualization (GEXF).
vec![]
}
}
// Simple record types (Nodes with no metadata / Dummy records)
#[derive(Clone, serde::Serialize, serde::Deserialize)]
pub struct SimpleRecord<ID> {
pub id: ID,
pub radix_key: String,
}
impl<ID: Clone + RadixKeyMapping> ApotheosisRecord for SimpleRecord<ID> {
type MetricId = ID;
fn search_id(&self) -> Self::MetricId {
self.id.clone()
}
}
pub type SimpleNumberRecord = SimpleRecord<u32>;
pub type SimpleTlshRecord = SimpleRecord<TlshDefault>;
impl SimpleNumberRecord {
/// Builds a record from an untrusted string (e.g. a line read from a
/// dataset file). Returns `Err` instead of panicking so a malformed entry
/// can be skipped without aborting the whole ingestion.
pub fn create(s: String) -> Result<Self, RecordError> {
let id = s
.parse::<u32>()
.map_err(|source| RecordError::InvalidNumber {
input: s.clone(),
source,
})?;
Ok(Self { id, radix_key: s })
}
}
impl SimpleTlshRecord {
/// Builds a record from an untrusted TLSH hash string. Returns `Err`
/// instead of panicking so a malformed entry can be skipped without
/// aborting the whole ingestion.
pub fn create(s: String) -> Result<Self, RecordError> {
let id = TlshDefault::from_str(&s)
.map_err(|_| RecordError::InvalidTlshHash { input: s.clone() })?;
Ok(Self { id, radix_key: s })
}
}
// TLSH node that accepts any JSON as metadata
#[derive(Clone, serde::Serialize, serde::Deserialize)]
pub struct GenericJsonRecord {
pub id: TlshDefault,
pub radix_key: String,
pub metadata: serde_json::Value,
}
impl ApotheosisRecord for GenericJsonRecord {
type MetricId = TlshDefault;
fn search_id(&self) -> Self::MetricId {
self.id.clone()
}
fn get_attributes(&self) -> Vec<(String, String)> {
if let Some(obj) = self.metadata.as_object() {
obj.iter()
.map(|(k, v)| {
let val_str = match v {
serde_json::Value::String(s) => s.clone(),
_ => v.to_string(),
};
(k.clone(), val_str)
})
.collect()
} else {
vec![("metadata".to_string(), self.metadata.to_string())]
}
}
}
impl GenericJsonRecord {
/// Builds a record from an untrusted TLSH hash string. Returns `Err`
/// instead of panicking so a malformed entry can be skipped without
/// aborting the whole ingestion.
pub fn create(s: String, metadata: serde_json::Value) -> Result<Self, RecordError> {
let id = TlshDefault::from_str(&s)
.map_err(|_| RecordError::InvalidTlshHash { input: s.clone() })?;
Ok(Self {
id,
radix_key: s,
metadata,
})
}
}
// Example/Existing Implementations
pub struct FileRecord {
pub hash: TlshDefault,
pub filename: String,
pub file_path: String,
pub version: String,
pub size: u64,
pub timestamp: u64,
}
#[derive(Clone)]
pub struct WinModuleRecord {
pub hash: TlshDefault,
pub id: u32,
pub file_version: String,
pub internal_filename: String,
pub product: String,
pub company: String,
pub os_id: u32,
}
impl ApotheosisRecord for WinModuleRecord {
type MetricId = TlshDefault;
fn search_id(&self) -> Self::MetricId {
self.hash.clone()
}
fn get_attributes(&self) -> Vec<(String, String)> {
vec![
("id".to_string(), self.id.to_string()),
("file_version".to_string(), self.file_version.clone()),
(
"internal_filename".to_string(),
self.internal_filename.clone(),
),
("product".to_string(), self.product.clone()),
("company".to_string(), self.company.clone()),
("os_id".to_string(), self.os_id.to_string()),
]
}
}
pub struct BinaryRecord {
pub hash: TlshDefault,
pub versions_names: Vec<(String, String)>,
}
impl ApotheosisRecord for BinaryRecord {
type MetricId = TlshDefault;
fn search_id(&self) -> Self::MetricId {
self.hash.clone() // Simplified dummy behavior for tests
}
fn get_attributes(&self) -> Vec<(String, String)> {
vec![("version_names".to_string(), "".to_string())]
}
}