kibana-object-manager 0.3.3

A Git-inspired CLI tool for managing Kibana saved objects in version control
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
use crate::etl::Transformer;
use kibana_sync::{Result, ResultContext};
use serde_json::{Map, Value};

/// Transformer that unescapes Vega specification fields for readable Git diffs.
///
/// This transformer does NOT parse or validate the Vega spec content. It simply
/// converts escaped newlines (`\n`) to real newlines so the spec can be stored
/// with triple-quote (`"""`) syntax in JSON5 files, making Git diffs readable.
///
/// Comments (`//` and `/* */`) are fully preserved since no parsing occurs.
#[derive(Debug, Clone)]
pub struct VegaSpecUnescaper {}

impl VegaSpecUnescaper {
    /// Create a new VegaSpecUnescaper with default search paths
    pub fn new() -> Self {
        Self {}
    }
}

impl Default for VegaSpecUnescaper {
    fn default() -> Self {
        Self::new()
    }
}

impl Transformer for VegaSpecUnescaper {
    type Input = Value;
    type Output = Value;

    fn transform(&self, mut input: Self::Input) -> Result<Self::Output> {
        unescape_vega_specs(&mut input)?;
        Ok(input)
    }
}

/// Recursively searches for and unescapes Vega specifications in a JSON value
fn unescape_vega_specs(value: &mut Value) -> Result<()> {
    match value {
        Value::Object(obj) => {
            // Check for Kibana saved object structure: attributes.visState
            if let Some(attributes) = obj.get_mut("attributes")
                && let Value::Object(attr_obj) = attributes
                && let Some(vis_state) = attr_obj.get_mut("visState")
                && let Value::Object(vis_state_obj) = vis_state
                && is_vega_visualization(vis_state_obj)
                && let Some(spec_value) = vis_state_obj
                    .get_mut("params")
                    .and_then(|p| p.as_object_mut())
                    .and_then(|params| params.get_mut("spec"))
            {
                unescape_spec_field(spec_value, "Kibana saved object")?;
            }

            // Check if this object is a Vega visualization (direct format)
            if is_vega_visualization(obj)
                && let Some(spec_value) = obj
                    .get_mut("params")
                    .and_then(|p| p.as_object_mut())
                    .and_then(|params| params.get_mut("spec"))
            {
                unescape_spec_field(spec_value, "direct format")?;
            }

            // Check for embedded saved visualizations (dashboard panels)
            if let Some(saved_vis) = obj.get_mut("savedVis")
                && let Value::Object(saved_vis_obj) = saved_vis
                && is_vega_visualization(saved_vis_obj)
                && let Some(spec_value) = saved_vis_obj
                    .get_mut("params")
                    .and_then(|p| p.as_object_mut())
                    .and_then(|params| params.get_mut("spec"))
            {
                unescape_spec_field(spec_value, "embedded")?;
            }

            // Recursively process all child objects and arrays
            for child_value in obj.values_mut() {
                unescape_vega_specs(child_value)?;
            }
        }
        Value::Array(arr) => {
            // Recursively process array elements
            for item in arr {
                unescape_vega_specs(item)?;
            }
        }
        _ => {
            // Primitive values don't need processing
        }
    }

    Ok(())
}

/// Unescape a single spec field value
///
/// This function simply ensures the spec string has real newlines (not escaped `\n`).
/// The json_writer will then use triple-quote syntax to write it, making Git diffs readable.
///
/// NO parsing or validation is performed - this preserves everything including comments.
fn unescape_spec_field(spec_value: &mut Value, context: &str) -> Result<()> {
    if let Some(spec_str) = spec_value.as_str() {
        log::debug!(
            "VegaSpecUnescaper: Found {} Vega spec string (length: {})",
            context,
            spec_str.len()
        );

        // The spec string from Kibana already has real newlines (not escaped).
        // We just need to ensure it stays as a string - the json_writer will
        // use triple-quote syntax for any string containing newlines.
        //
        // If the string contains newlines, it will be written as:
        //   "spec": """
        //   {
        //     // comments preserved
        //     "$schema": "...",
        //     ...
        //   }"""
        //
        // No transformation needed here - the string is already in the right format.
        // We just log that we found it.
        if spec_str.contains('\n') {
            log::debug!(
                "VegaSpecUnescaper: {} Vega spec contains newlines, will use triple-quote syntax",
                context
            );
        }
    }
    Ok(())
}

/// Check if an object represents a Vega or Vega-Lite visualization
fn is_vega_visualization(obj: &Map<String, Value>) -> bool {
    obj.get("type")
        .and_then(|t| t.as_str())
        .map(|type_str| type_str == "vega" || type_str == "vega-lite")
        .unwrap_or(false)
}

/// Transformer that escapes Vega specification fields from objects to JSON strings
#[derive(Debug, Clone)]
pub struct VegaSpecEscaper {}

impl VegaSpecEscaper {
    /// Create a new VegaSpecEscaper with default search paths
    pub fn new() -> Self {
        Self {}
    }
}

impl Default for VegaSpecEscaper {
    fn default() -> Self {
        Self::new()
    }
}

impl Transformer for VegaSpecEscaper {
    type Input = Value;
    type Output = Value;

    fn transform(&self, mut input: Self::Input) -> Result<Self::Output> {
        escape_vega_specs(&mut input)?;
        Ok(input)
    }
}

/// Recursively searches for and escapes Vega specifications from objects to JSON strings
fn escape_vega_specs(value: &mut Value) -> Result<()> {
    match value {
        Value::Object(obj) => {
            // Check for Kibana saved object structure: attributes.visState
            if let Some(attributes) = obj.get_mut("attributes")
                && let Value::Object(attr_obj) = attributes
                && let Some(vis_state) = attr_obj.get_mut("visState")
                && let Value::Object(vis_state_obj) = vis_state
                && is_vega_visualization(vis_state_obj)
                && let Some(spec_value) = vis_state_obj
                    .get_mut("params")
                    .and_then(|p| p.as_object_mut())
                    .and_then(|params| params.get_mut("spec"))
            {
                escape_spec_field(spec_value, "Kibana saved object")?;
            }

            // Check if this object is a Vega visualization (direct format)
            if is_vega_visualization(obj)
                && let Some(spec_value) = obj
                    .get_mut("params")
                    .and_then(|p| p.as_object_mut())
                    .and_then(|params| params.get_mut("spec"))
            {
                escape_spec_field(spec_value, "direct format")?;
            }

            // Check for embedded saved visualizations (dashboard panels)
            if let Some(saved_vis) = obj.get_mut("savedVis")
                && let Value::Object(saved_vis_obj) = saved_vis
                && is_vega_visualization(saved_vis_obj)
                && let Some(spec_value) = saved_vis_obj
                    .get_mut("params")
                    .and_then(|p| p.as_object_mut())
                    .and_then(|params| params.get_mut("spec"))
            {
                escape_spec_field(spec_value, "embedded")?;
            }

            // Recursively process all child objects and arrays
            for child_value in obj.values_mut() {
                escape_vega_specs(child_value)?;
            }
        }
        Value::Array(arr) => {
            // Recursively process array elements
            for item in arr {
                escape_vega_specs(item)?;
            }
        }
        _ => {
            // Primitive values don't need processing
        }
    }

    Ok(())
}

/// Escape a single spec field value from object to JSON string
fn escape_spec_field(spec_value: &mut Value, context: &str) -> Result<()> {
    if !spec_value.is_string() {
        // Only escape if it's not already a string
        match serde_json::to_string(spec_value) {
            Ok(escaped_spec) => {
                *spec_value = Value::String(escaped_spec);
                log::debug!(
                    "VegaSpecEscaper: Successfully escaped {} Vega spec to JSON string",
                    context
                );
            }
            Err(e) => {
                log::warn!(
                    "VegaSpecEscaper: Failed to escape {} Vega spec: {}",
                    context,
                    e
                );
                return Err(e)
                    .with_context(|| format!("Failed to escape Vega spec in {}", context));
            }
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_unescape_vega_visualization() {
        let input = json!({
            "attributes": {
                "visState": {
                    "type": "vega",
                    "params": {
                        "spec": "{\n  \"$schema\": \"https://vega.github.io/schema/vega/v5.json\",\n  \"width\": 400\n}"
                    }
                }
            }
        });

        let unescaper = VegaSpecUnescaper::new();
        let result = unescaper.transform(input.clone()).unwrap();

        // Spec should remain unchanged - we don't parse, just pass through
        let spec = &result["attributes"]["visState"]["params"]["spec"];
        assert!(spec.is_string());
        let spec_str = spec.as_str().unwrap();
        assert!(spec_str.contains("vega.github.io/schema/vega/v5.json"));
        assert!(spec_str.contains("width"));
        assert!(spec_str.contains('\n')); // Has real newlines
    }

    #[test]
    fn test_unescape_vega_lite_visualization() {
        let input = json!({
            "attributes": {
                "visState": {
                    "type": "vega-lite",
                    "params": {
                        "spec": "{\n  \"$schema\": \"https://vega.github.io/schema/vega-lite/v5.json\",\n  \"mark\": \"bar\"\n}"
                    }
                }
            }
        });

        let unescaper = VegaSpecUnescaper::new();
        let result = unescaper.transform(input).unwrap();

        let spec = &result["attributes"]["visState"]["params"]["spec"];
        assert!(spec.is_string());
        let spec_str = spec.as_str().unwrap();
        assert!(spec_str.contains("vega-lite/v5.json"));
        assert!(spec_str.contains("mark"));
    }

    #[test]
    fn test_comments_are_preserved() {
        // This is the key test - comments must survive the round trip
        let spec_with_comments = r#"{
  // This is a line comment
  /* xray tango */
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "mark": "bar"
}"#;

        let input = json!({
            "attributes": {
                "visState": {
                    "type": "vega",
                    "params": {
                        "spec": spec_with_comments
                    }
                }
            }
        });

        let unescaper = VegaSpecUnescaper::new();
        let result = unescaper.transform(input).unwrap();

        let spec = &result["attributes"]["visState"]["params"]["spec"];
        let spec_str = spec.as_str().unwrap();

        // Comments MUST be preserved
        assert!(
            spec_str.contains("// This is a line comment"),
            "Line comment was lost!"
        );
        assert!(
            spec_str.contains("/* xray tango */"),
            "Block comment was lost!"
        );
    }

    #[test]
    fn test_unescape_dashboard_panel_vega() {
        let input = json!({
            "embeddableConfig": {
                "savedVis": {
                    "type": "vega",
                    "params": {
                        "spec": "{\n  \"$schema\": \"https://vega.github.io/schema/vega/v5.json\",\n  \"data\": []\n}"
                    }
                }
            }
        });

        let unescaper = VegaSpecUnescaper::new();
        let result = unescaper.transform(input).unwrap();

        let spec = &result["embeddableConfig"]["savedVis"]["params"]["spec"];
        assert!(spec.is_string());
        let spec_str = spec.as_str().unwrap();
        assert!(spec_str.contains("vega/v5.json"));
    }

    #[test]
    fn test_ignore_non_vega_visualizations() {
        let input = json!({
            "attributes": {
                "visState": {
                    "type": "histogram",
                    "params": {
                        "spec": "{\"bins\":20}"
                    }
                }
            }
        });

        let unescaper = VegaSpecUnescaper::new();
        let result = unescaper.transform(input.clone()).unwrap();

        // Should remain unchanged (non-vega types are not processed)
        assert_eq!(result, input);
    }

    #[test]
    fn test_handle_invalid_json_gracefully() {
        // Even invalid JSON should be preserved as-is (we don't parse)
        let input = json!({
            "attributes": {
                "visState": {
                    "type": "vega",
                    "params": {
                        "spec": "this is not json at all"
                    }
                }
            }
        });

        let unescaper = VegaSpecUnescaper::new();
        let result = unescaper.transform(input.clone()).unwrap();

        // Should remain unchanged
        assert_eq!(result, input);
    }

    #[test]
    fn test_nested_objects_and_arrays() {
        let input = json!({
            "panels": [
                {
                    "embeddableConfig": {
                        "savedVis": {
                            "type": "vega",
                            "params": {
                                "spec": "{\n  \"width\": 300\n}"
                            }
                        }
                    }
                }
            ]
        });

        let unescaper = VegaSpecUnescaper::new();
        let result = unescaper.transform(input).unwrap();

        let spec = &result["panels"][0]["embeddableConfig"]["savedVis"]["params"]["spec"];
        assert!(spec.is_string());
        let spec_str = spec.as_str().unwrap();
        assert!(spec_str.contains("width"));
        assert!(spec_str.contains("300"));
    }

    #[test]
    fn test_escape_vega_visualization() {
        // When spec is an object, VegaSpecEscaper converts it to JSON string
        let input = json!({
            "attributes": {
                "visState": {
                    "type": "vega",
                    "params": {
                        "spec": {
                            "$schema": "https://vega.github.io/schema/vega/v5.json",
                            "width": 400
                        }
                    }
                }
            }
        });

        let escaper = VegaSpecEscaper::new();
        let result = escaper.transform(input).unwrap();

        let spec = &result["attributes"]["visState"]["params"]["spec"];
        assert!(spec.is_string());

        // Parse the escaped JSON to verify it's valid
        let parsed: Value = serde_json::from_str(spec.as_str().unwrap()).unwrap();
        assert_eq!(
            parsed["$schema"],
            "https://vega.github.io/schema/vega/v5.json"
        );
        assert_eq!(parsed["width"], 400);
    }

    #[test]
    fn test_round_trip_preserves_comments() {
        // The most important test: comments survive the full round trip
        let original_spec = r#"{
  // Line comment here
  /* Block comment here */
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "mark": "bar",
  "encoding": {
    // Another comment
    "x": {"field": "category"}
  }
}"#;

        let input = json!({
            "attributes": {
                "visState": {
                    "type": "vega-lite",
                    "params": {
                        "spec": original_spec
                    }
                }
            }
        });

        // Unescape (for storage)
        let unescaper = VegaSpecUnescaper::new();
        let unescaped = unescaper.transform(input).unwrap();

        let spec = &unescaped["attributes"]["visState"]["params"]["spec"];
        let spec_str = spec.as_str().unwrap();

        // ALL comments must be preserved
        assert!(
            spec_str.contains("// Line comment here"),
            "Line comment lost after unescape"
        );
        assert!(
            spec_str.contains("/* Block comment here */"),
            "Block comment lost after unescape"
        );
        assert!(
            spec_str.contains("// Another comment"),
            "Nested comment lost after unescape"
        );

        // Content must also be preserved
        assert!(spec_str.contains("vega-lite/v5.json"));
        assert!(spec_str.contains("\"mark\": \"bar\"") || spec_str.contains("\"mark\":\"bar\""));
    }

    #[test]
    fn test_hjson_features_preserved() {
        // Test that HJSON features like trailing commas are preserved (we don't parse)
        let input = json!({
            "attributes": {
                "visState": {
                    "type": "vega",
                    "params": {
                        "spec": "{\"data\": [1, 2, 3,], \"width\": 400,}"
                    }
                }
            }
        });

        let unescaper = VegaSpecUnescaper::new();
        let result = unescaper.transform(input.clone()).unwrap();

        // Should be unchanged - we don't parse or modify
        assert_eq!(result, input);
    }

    #[test]
    fn test_real_kibana_export_transformation() {
        // Test with real Kibana export format
        let input = json!({
            "attributes": {
                "visState": {
                    "type": "vega-lite",
                    "params": {
                        "spec": "{\n  \"$schema\": \"https://vega.github.io/schema/vega-lite/v5.json\",\n  \"data\": {\n    \"url\": {\n      \"%context%\": true\n    }\n  }\n}"
                    }
                }
            }
        });

        let unescaper = VegaSpecUnescaper::new();
        let result = unescaper.transform(input).unwrap();

        let spec = &result["attributes"]["visState"]["params"]["spec"];
        assert!(spec.is_string());
        let spec_str = spec.as_str().unwrap();
        assert!(spec_str.contains("vega-lite/v5.json"));
        assert!(spec_str.contains("%context%"));
    }
}