-
-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathcorrelation.rs
378 lines (328 loc) · 11.9 KB
/
correlation.rs
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
/*
* Parseable Server (C) 2022 - 2024 Parseable, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
use std::collections::{HashMap, HashSet};
use actix_web::{http::header::ContentType, Error};
use chrono::Utc;
use datafusion::error::DataFusionError;
use http::StatusCode;
use itertools::Itertools;
use once_cell::sync::Lazy;
use relative_path::RelativePathBuf;
use serde::{Deserialize, Serialize};
use serde_json::Error as SerdeError;
use tokio::sync::RwLock;
use tracing::error;
use crate::{
handlers::http::{
rbac::RBACError,
users::{CORRELATION_DIR, USERS_ROOT_DIR},
},
option::CONFIG,
query::QUERY_SESSION,
rbac::{map::SessionKey, Users},
storage::ObjectStorageError,
users::filters::FilterQuery,
utils::{get_hash, user_auth_for_query},
};
pub static CORRELATIONS: Lazy<Correlations> = Lazy::new(Correlations::default);
type CorrelationMap = HashMap<CorrelationId, CorrelationConfig>;
#[derive(Debug, Default, derive_more::Deref)]
pub struct Correlations(RwLock<CorrelationMap>);
impl Correlations {
// Load correlations from storage
pub async fn load(&self) -> anyhow::Result<()> {
let store = CONFIG.storage().get_object_store();
let all_correlations = store.get_all_correlations().await.unwrap_or_default();
for correlations_bytes in all_correlations.values().flatten() {
let correlation = match serde_json::from_slice::<CorrelationConfig>(correlations_bytes)
{
Ok(c) => c,
Err(e) => {
error!("Unable to load correlation file : {e}");
continue;
}
};
self.write()
.await
.insert(correlation.id.to_owned(), correlation);
}
Ok(())
}
pub async fn list_correlations(
&self,
session_key: &SessionKey,
) -> Result<Vec<CorrelationConfig>, CorrelationError> {
let mut user_correlations = vec![];
let permissions = Users.get_permissions(session_key);
for correlation in self.read().await.values() {
let tables = &correlation
.table_configs
.iter()
.map(|t| t.table_name.clone())
.collect_vec();
if user_auth_for_query(&permissions, tables).is_ok() {
user_correlations.push(correlation.clone());
}
}
Ok(user_correlations)
}
pub async fn get_correlation(
&self,
correlation_id: &str,
) -> Result<CorrelationConfig, CorrelationError> {
self.read()
.await
.get(correlation_id)
.cloned()
.ok_or_else(|| {
CorrelationError::AnyhowError(anyhow::Error::msg(format!(
"Unable to find correlation with ID- {correlation_id}"
)))
})
}
/// Create correlation associated with the user
pub async fn create(
&self,
mut correlation: CorrelationConfig,
session_key: &SessionKey,
) -> Result<CorrelationConfig, CorrelationError> {
correlation.id = get_hash(Utc::now().timestamp_micros().to_string().as_str());
correlation.validate(session_key).await?;
// Update in storage
let correlation_bytes = serde_json::to_vec(&correlation)?.into();
let path = correlation.path();
CONFIG
.storage()
.get_object_store()
.put_object(&path, correlation_bytes)
.await?;
// Update in memory
self.write()
.await
.insert(correlation.id.to_owned(), correlation.clone());
Ok(correlation)
}
/// Update existing correlation for the user and with the same ID
pub async fn update(
&self,
mut updated_correlation: CorrelationConfig,
session_key: &SessionKey,
) -> Result<CorrelationConfig, CorrelationError> {
// validate whether user has access to this correlation object or not
let correlation = self.get_correlation(&updated_correlation.id).await?;
if correlation.user_id != updated_correlation.user_id {
return Err(CorrelationError::AnyhowError(anyhow::Error::msg(format!(
r#"User "{}" isn't authorized to update correlation with ID - {}"#,
updated_correlation.user_id, correlation.id
))));
}
correlation.validate(session_key).await?;
updated_correlation.update(correlation);
// Update in storage
let correlation_bytes = serde_json::to_vec(&updated_correlation)?.into();
let path = updated_correlation.path();
CONFIG
.storage()
.get_object_store()
.put_object(&path, correlation_bytes)
.await?;
// Update in memory
self.write().await.insert(
updated_correlation.id.to_owned(),
updated_correlation.clone(),
);
Ok(updated_correlation)
}
/// Delete correlation from memory and storage
pub async fn delete(
&self,
correlation_id: &str,
user_id: &str,
) -> Result<(), CorrelationError> {
let correlation = CORRELATIONS.get_correlation(correlation_id).await?;
if correlation.user_id != user_id {
return Err(CorrelationError::AnyhowError(anyhow::Error::msg(format!(
r#"User "{user_id}" isn't authorized to delete correlation with ID - {correlation_id}"#
))));
}
// Delete from memory
self.write().await.remove(&correlation.id);
// Delete from storage
let path = correlation.path();
CONFIG
.storage()
.get_object_store()
.delete_object(&path)
.await?;
Ok(())
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum CorrelationVersion {
#[default]
V1,
}
type CorrelationId = String;
type UserId = String;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CorrelationConfig {
#[serde(default)]
pub version: CorrelationVersion,
pub title: String,
#[serde(default)]
pub id: CorrelationId,
#[serde(default)]
pub user_id: UserId,
pub table_configs: Vec<TableConfig>,
pub join_config: JoinConfig,
pub filter: Option<FilterQuery>,
pub start_time: Option<String>,
pub end_time: Option<String>,
}
impl CorrelationConfig {
pub fn path(&self) -> RelativePathBuf {
RelativePathBuf::from_iter([
USERS_ROOT_DIR,
&self.user_id,
CORRELATION_DIR,
&format!("{}.json", self.id),
])
}
pub fn update(&mut self, update: Self) {
self.title = update.title;
self.table_configs = update.table_configs;
self.join_config = update.join_config;
self.filter = update.filter;
self.start_time = update.start_time;
self.end_time = update.end_time;
}
/// This function will validate the TableConfigs, JoinConfig, and user auth
pub async fn validate(&self, session_key: &SessionKey) -> Result<(), CorrelationError> {
let ctx = &QUERY_SESSION;
let h1: HashSet<&String> = self.table_configs.iter().map(|t| &t.table_name).collect();
let h2: HashSet<&String> = self
.join_config
.join_conditions
.iter()
.map(|j| &j.table_name)
.collect();
// check if table config tables are the same
if h1.len() != 2 {
return Err(CorrelationError::Metadata(
"Must provide config for two unique tables",
));
}
// check that the tables mentioned in join config are
// the same as those in table config
if h1 != h2 {
return Err(CorrelationError::Metadata(
"Must provide same tables for join config and table config",
));
}
// check if user has access to table
let permissions = Users.get_permissions(session_key);
let tables = &self
.table_configs
.iter()
.map(|t| t.table_name.clone())
.collect_vec();
user_auth_for_query(&permissions, tables)?;
// to validate table config, we need to check whether the mentioned fields
// are present in the table or not
for table_config in self.table_configs.iter() {
// table config check
let df = ctx.table(&table_config.table_name).await?;
let mut selected_fields = table_config
.selected_fields
.iter()
.map(|c| c.as_str())
.collect_vec();
// unwrap because we have determined that the tables in table config are the same as those in join config
let condition = self
.join_config
.join_conditions
.iter()
.find(|j| j.table_name == table_config.table_name)
.unwrap();
let join_field = condition.field.as_str();
if !selected_fields.contains(&join_field) {
selected_fields.push(join_field);
}
// if this errors out then the table config is incorrect or join config is incorrect
df.select_columns(selected_fields.as_slice())?;
}
Ok(())
}
}
#[derive(Debug, thiserror::Error)]
pub enum CorrelationError {
#[error("Failed to connect to storage: {0}")]
ObjectStorage(#[from] ObjectStorageError),
#[error("Serde Error: {0}")]
Serde(#[from] SerdeError),
#[error("Cannot perform this operation: {0}")]
Metadata(&'static str),
#[error("User does not exist")]
UserDoesNotExist(#[from] RBACError),
#[error("Error: {0}")]
AnyhowError(#[from] anyhow::Error),
#[error("Unauthorized")]
Unauthorized,
#[error("DataFusion Error: {0}")]
DataFusion(#[from] DataFusionError),
#[error("{0}")]
ActixError(#[from] Error),
}
impl actix_web::ResponseError for CorrelationError {
fn status_code(&self) -> http::StatusCode {
match self {
Self::ObjectStorage(_) => StatusCode::INTERNAL_SERVER_ERROR,
Self::Serde(_) => StatusCode::BAD_REQUEST,
Self::Metadata(_) => StatusCode::BAD_REQUEST,
Self::UserDoesNotExist(_) => StatusCode::NOT_FOUND,
Self::AnyhowError(_) => StatusCode::INTERNAL_SERVER_ERROR,
Self::Unauthorized => StatusCode::BAD_REQUEST,
Self::DataFusion(_) => StatusCode::INTERNAL_SERVER_ERROR,
Self::ActixError(_) => StatusCode::BAD_REQUEST,
}
}
fn error_response(&self) -> actix_web::HttpResponse<actix_web::body::BoxBody> {
actix_web::HttpResponse::build(self.status_code())
.insert_header(ContentType::plaintext())
.body(self.to_string())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TableConfig {
pub selected_fields: Vec<String>,
pub table_name: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct JoinCondition {
pub table_name: String,
pub field: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct JoinConfig {
pub join_conditions: Vec<JoinCondition>,
}