rivven-cdc 0.0.2

Change Data Capture for Rivven - PostgreSQL, MySQL, MariaDB
Documentation
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
//! Table and column filtering for CDC events
//!
//! Provides production-grade filtering capabilities:
//! - Include/exclude tables by pattern (glob syntax)
//! - Include/exclude columns per table
//! - Regex-based matching
//! - Column masking for sensitive data (PII redaction)
//!
//! # Example
//!
//! ```rust
//! use rivven_cdc::CdcFilterConfig;
//!
//! let config = CdcFilterConfig {
//!     include_tables: vec!["public.*".to_string()],
//!     exclude_tables: vec!["*.audit_log".to_string()],
//!     mask_columns: vec!["password".to_string(), "ssn".to_string()],
//!     ..Default::default()
//! };
//! ```

use crate::common::CdcEvent;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};

/// Filter configuration for CDC tables and columns
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CdcFilterConfig {
    /// Tables to include (supports glob patterns like "public.*")
    #[serde(default)]
    pub include_tables: Vec<String>,

    /// Tables to exclude (evaluated after includes)
    #[serde(default)]
    pub exclude_tables: Vec<String>,

    /// Per-table column configuration
    #[serde(default)]
    pub table_columns: HashMap<String, TableColumnConfig>,

    /// Global column excludes (applied to all tables)
    #[serde(default)]
    pub global_exclude_columns: Vec<String>,

    /// Columns to mask (show as "***REDACTED***")
    #[serde(default)]
    pub mask_columns: Vec<String>,
}

/// Column configuration for a specific table
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TableColumnConfig {
    /// Columns to include (if empty, include all)
    #[serde(default)]
    pub include: Vec<String>,

    /// Columns to exclude
    #[serde(default)]
    pub exclude: Vec<String>,

    /// Columns to mask with "***REDACTED***"
    #[serde(default)]
    pub mask: Vec<String>,
}

impl Default for CdcFilterConfig {
    fn default() -> Self {
        Self {
            include_tables: vec!["*".to_string()], // Include all by default
            exclude_tables: vec![],
            table_columns: HashMap::new(),
            global_exclude_columns: vec![],
            mask_columns: vec![],
        }
    }
}

/// Compiled filter for efficient runtime evaluation
pub struct CdcFilter {
    config: CdcFilterConfig,
    include_patterns: Vec<Regex>,
    exclude_patterns: Vec<Regex>,
    global_exclude_set: HashSet<String>,
    mask_set: HashSet<String>,
}

impl CdcFilter {
    /// Create a new filter from configuration
    pub fn new(config: CdcFilterConfig) -> Result<Self, regex::Error> {
        let include_patterns = config
            .include_tables
            .iter()
            .map(|p| Self::glob_to_regex(p))
            .collect::<Result<Vec<_>, _>>()?;

        let exclude_patterns = config
            .exclude_tables
            .iter()
            .map(|p| Self::glob_to_regex(p))
            .collect::<Result<Vec<_>, _>>()?;

        let global_exclude_set: HashSet<String> = config
            .global_exclude_columns
            .iter()
            .map(|s| s.to_lowercase())
            .collect();

        let mask_set: HashSet<String> = config
            .mask_columns
            .iter()
            .map(|s| s.to_lowercase())
            .collect();

        Ok(Self {
            config,
            include_patterns,
            exclude_patterns,
            global_exclude_set,
            mask_set,
        })
    }

    /// Convert a glob pattern to regex
    fn glob_to_regex(pattern: &str) -> Result<Regex, regex::Error> {
        let escaped = regex::escape(pattern);
        let regex_pattern = escaped.replace(r"\*", ".*").replace(r"\?", ".");
        Regex::new(&format!("^{}$", regex_pattern))
    }

    /// Check if a table should be included
    pub fn should_include_table(&self, schema: &str, table: &str) -> bool {
        let full_name = format!("{}.{}", schema, table);

        // Check excludes first (higher priority)
        for pattern in &self.exclude_patterns {
            if pattern.is_match(&full_name) || pattern.is_match(table) {
                return false;
            }
        }

        // Check includes
        for pattern in &self.include_patterns {
            if pattern.is_match(&full_name) || pattern.is_match(table) {
                return true;
            }
        }

        // Default: exclude if no patterns match
        self.include_patterns.is_empty()
    }

    /// Filter and transform a CDC event
    ///
    /// Returns true if the event should be processed, false if filtered out.
    /// Modifies the event in place to remove/mask columns.
    pub fn filter_event(&self, event: &mut CdcEvent) -> bool {
        // Table filter
        if !self.should_include_table(&event.schema, &event.table) {
            return false;
        }

        // Column filtering
        let table_key = format!("{}.{}", event.schema, event.table);
        let table_config = self.config.table_columns.get(&table_key);

        // Filter 'before' payload
        if let Some(ref mut before) = event.before {
            self.filter_json(before, table_config);
        }

        // Filter 'after' payload
        if let Some(ref mut after) = event.after {
            self.filter_json(after, table_config);
        }

        true
    }

    /// Filter and mask columns in a JSON object
    fn filter_json(&self, value: &mut serde_json::Value, table_config: Option<&TableColumnConfig>) {
        if let serde_json::Value::Object(map) = value {
            // Collect columns to remove
            let mut to_remove = Vec::new();
            let mut to_mask = Vec::new();

            for key in map.keys() {
                let key_lower = key.to_lowercase();

                // Global excludes
                if self.global_exclude_set.contains(&key_lower) {
                    to_remove.push(key.clone());
                    continue;
                }

                // Global masks
                if self.mask_set.contains(&key_lower) {
                    to_mask.push(key.clone());
                    continue;
                }

                // Table-specific config
                if let Some(tc) = table_config {
                    // Check table excludes
                    if tc.exclude.iter().any(|e| e.to_lowercase() == key_lower) {
                        to_remove.push(key.clone());
                        continue;
                    }

                    // Check table includes (if specified, only include those)
                    if !tc.include.is_empty()
                        && !tc.include.iter().any(|i| i.to_lowercase() == key_lower)
                    {
                        to_remove.push(key.clone());
                        continue;
                    }

                    // Check table masks
                    if tc.mask.iter().any(|m| m.to_lowercase() == key_lower) {
                        to_mask.push(key.clone());
                    }
                }
            }

            // Remove excluded columns
            for key in to_remove {
                map.remove(&key);
            }

            // Mask sensitive columns
            for key in to_mask {
                if let Some(v) = map.get_mut(&key) {
                    *v = serde_json::Value::String("***REDACTED***".to_string());
                }
            }
        }
    }

    /// Get the filter configuration
    pub fn config(&self) -> &CdcFilterConfig {
        &self.config
    }
}

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

    #[test]
    fn test_table_include_all() {
        let config = CdcFilterConfig::default();
        let filter = CdcFilter::new(config).unwrap();

        assert!(filter.should_include_table("public", "users"));
        assert!(filter.should_include_table("schema", "orders"));
    }

    #[test]
    fn test_table_include_pattern() {
        let config = CdcFilterConfig {
            include_tables: vec!["public.*".to_string()],
            ..Default::default()
        };
        let filter = CdcFilter::new(config).unwrap();

        assert!(filter.should_include_table("public", "users"));
        assert!(filter.should_include_table("public", "orders"));
        assert!(!filter.should_include_table("private", "secrets"));
    }

    #[test]
    fn test_table_exclude() {
        let config = CdcFilterConfig {
            include_tables: vec!["*".to_string()],
            exclude_tables: vec!["*.audit_log".to_string(), "private.*".to_string()],
            ..Default::default()
        };
        let filter = CdcFilter::new(config).unwrap();

        assert!(filter.should_include_table("public", "users"));
        assert!(!filter.should_include_table("public", "audit_log"));
        assert!(!filter.should_include_table("private", "secrets"));
    }

    #[test]
    fn test_column_filtering() {
        let config = CdcFilterConfig {
            global_exclude_columns: vec!["password".to_string(), "ssn".to_string()],
            ..Default::default()
        };
        let filter = CdcFilter::new(config).unwrap();

        let mut event = CdcEvent {
            source_type: "postgres".into(),
            database: "mydb".into(),
            schema: "public".into(),
            table: "users".into(),
            op: CdcOp::Insert,
            before: None,
            after: Some(serde_json::json!({
                "id": 1,
                "name": "Alice",
                "password": "secret123",
                "ssn": "123-45-6789"
            })),
            timestamp: 0,
            transaction: None,
        };

        assert!(filter.filter_event(&mut event));

        let after = event.after.as_ref().unwrap();
        assert!(after.get("id").is_some());
        assert!(after.get("name").is_some());
        assert!(after.get("password").is_none());
        assert!(after.get("ssn").is_none());
    }

    #[test]
    fn test_column_masking() {
        let config = CdcFilterConfig {
            mask_columns: vec!["email".to_string(), "phone".to_string()],
            ..Default::default()
        };
        let filter = CdcFilter::new(config).unwrap();

        let mut event = CdcEvent {
            source_type: "postgres".into(),
            database: "mydb".into(),
            schema: "public".into(),
            table: "users".into(),
            op: CdcOp::Insert,
            before: None,
            after: Some(serde_json::json!({
                "id": 1,
                "name": "Alice",
                "email": "alice@example.com",
                "phone": "+1-555-1234"
            })),
            timestamp: 0,
            transaction: None,
        };

        assert!(filter.filter_event(&mut event));

        let after = event.after.as_ref().unwrap();
        assert_eq!(after.get("email").unwrap(), "***REDACTED***");
        assert_eq!(after.get("phone").unwrap(), "***REDACTED***");
        assert_eq!(after.get("name").unwrap(), "Alice");
    }

    #[test]
    fn test_table_specific_columns() {
        let mut table_columns = HashMap::new();
        table_columns.insert(
            "public.users".to_string(),
            TableColumnConfig {
                include: vec!["id".to_string(), "name".to_string()],
                exclude: vec![],
                mask: vec![],
            },
        );

        let config = CdcFilterConfig {
            table_columns,
            ..Default::default()
        };
        let filter = CdcFilter::new(config).unwrap();

        let mut event = CdcEvent {
            source_type: "postgres".into(),
            database: "mydb".into(),
            schema: "public".into(),
            table: "users".into(),
            op: CdcOp::Insert,
            before: None,
            after: Some(serde_json::json!({
                "id": 1,
                "name": "Alice",
                "email": "alice@example.com",
                "created_at": "2024-01-01"
            })),
            timestamp: 0,
            transaction: None,
        };

        assert!(filter.filter_event(&mut event));

        let after = event.after.as_ref().unwrap();
        assert!(after.get("id").is_some());
        assert!(after.get("name").is_some());
        assert!(after.get("email").is_none()); // Not in include list
        assert!(after.get("created_at").is_none()); // Not in include list
    }

    #[test]
    fn test_filter_rejects_excluded_table() {
        let config = CdcFilterConfig {
            exclude_tables: vec!["audit_log".to_string()],
            ..Default::default()
        };
        let filter = CdcFilter::new(config).unwrap();

        let mut event = CdcEvent {
            source_type: "postgres".into(),
            database: "mydb".into(),
            schema: "public".into(),
            table: "audit_log".into(),
            op: CdcOp::Insert,
            before: None,
            after: Some(serde_json::json!({"id": 1})),
            timestamp: 0,
            transaction: None,
        };

        assert!(!filter.filter_event(&mut event));
    }
}