kibana-sync 0.1.0

Reusable Kibana sync library for saved objects, spaces, agents, tools, and workflows
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
use crate::client::{ApiCapability, KibanaClient, KibanaVersion};
use crate::etl::{Extractor, Loader};
use crate::kibana::agents::{AgentsExtractor, AgentsLoader};
use crate::kibana::dependencies::{
    Dependency, find_agent_dependencies, find_tool_dependencies, find_workflow_dependencies,
};
use crate::kibana::saved_objects::{
    SavedObjectsExtractor, SavedObjectsLoader, SavedObjectsManifest,
};
use crate::kibana::spaces::{SpacesExtractor, SpacesLoader};
use crate::kibana::tools::{ToolsExtractor, ToolsLoader};
use crate::kibana::workflows::{WorkflowsExtractor, WorkflowsLoader};
use crate::{Error, Result};
use serde_json::Value;
use std::collections::{HashMap, HashSet};

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum UnsupportedApiPolicy {
    Skip,
    Warn,
    Force,
}

#[derive(Clone, Debug)]
pub struct SyncSelection {
    pub spaces: Vec<String>,
    pub saved_objects: Option<SavedObjectsManifest>,
    pub include_spaces: bool,
    pub include_workflows: bool,
    pub include_agents: bool,
    pub include_tools: bool,
}

impl Default for SyncSelection {
    fn default() -> Self {
        Self {
            spaces: vec!["default".to_string()],
            saved_objects: None,
            include_spaces: false,
            include_workflows: false,
            include_agents: false,
            include_tools: false,
        }
    }
}

#[derive(Clone, Debug)]
pub struct SyncOptions {
    pub expand_dependencies: bool,
    pub overwrite: bool,
    pub unsupported_api_policy: UnsupportedApiPolicy,
}

impl Default for SyncOptions {
    fn default() -> Self {
        Self {
            expand_dependencies: true,
            overwrite: true,
            unsupported_api_policy: UnsupportedApiPolicy::Warn,
        }
    }
}

#[derive(Clone, Debug, Default)]
pub struct SpaceBundle {
    pub saved_objects: Vec<Value>,
    pub workflows: Vec<Value>,
    pub agents: Vec<Value>,
    pub tools: Vec<Value>,
}

#[derive(Clone, Debug, Default)]
pub struct SyncBundle {
    pub spaces: Vec<Value>,
    pub by_space: HashMap<String, SpaceBundle>,
}

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct SyncSummary {
    pub spaces_attempted: usize,
    pub spaces_applied: usize,
    pub saved_objects_attempted: usize,
    pub saved_objects_applied: usize,
    pub workflows_attempted: usize,
    pub workflows_applied: usize,
    pub agents_attempted: usize,
    pub agents_applied: usize,
    pub tools_attempted: usize,
    pub tools_applied: usize,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CapabilityPlan {
    pub supported: Vec<ApiCapability>,
    pub unsupported: Vec<ApiCapabilityWarning>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ApiCapabilityWarning {
    pub capability: ApiCapability,
    pub message: String,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DependencyExpansionCapabilities {
    pub agents: bool,
    pub tools: bool,
    pub workflows: bool,
}

impl DependencyExpansionCapabilities {
    pub fn all() -> Self {
        Self {
            agents: true,
            tools: true,
            workflows: true,
        }
    }
}

impl Default for DependencyExpansionCapabilities {
    fn default() -> Self {
        Self::all()
    }
}

pub fn plan_capabilities(
    version: &KibanaVersion,
    capabilities: impl IntoIterator<Item = ApiCapability>,
) -> CapabilityPlan {
    let mut supported = Vec::new();
    let mut unsupported = Vec::new();

    for capability in capabilities {
        if KibanaClient::supports_capability(version, capability) {
            supported.push(capability);
        } else {
            unsupported.push(ApiCapabilityWarning {
                capability,
                message: KibanaClient::unsupported_capability_reason(version, capability),
            });
        }
    }

    CapabilityPlan {
        supported,
        unsupported,
    }
}

pub async fn pull_sync(
    client: &KibanaClient,
    selection: &SyncSelection,
    options: &SyncOptions,
) -> Result<SyncBundle> {
    let mut bundle = SyncBundle::default();
    let include_spaces = selection.include_spaces
        && capability_allowed(
            client,
            ApiCapability::Spaces,
            &options.unsupported_api_policy,
        )
        .await?;
    let include_saved_objects = selection.saved_objects.is_some()
        && capability_allowed(
            client,
            ApiCapability::SavedObjects,
            &options.unsupported_api_policy,
        )
        .await?;
    let include_workflows = selection.include_workflows
        && capability_allowed(
            client,
            ApiCapability::Workflows,
            &options.unsupported_api_policy,
        )
        .await?;
    let include_agents = selection.include_agents
        && capability_allowed(
            client,
            ApiCapability::Agents,
            &options.unsupported_api_policy,
        )
        .await?;
    let include_tools = selection.include_tools
        && capability_allowed(
            client,
            ApiCapability::Tools,
            &options.unsupported_api_policy,
        )
        .await?;

    if include_spaces {
        bundle.spaces = SpacesExtractor::all(client.clone()).extract().await?;
    }

    for space_id in &selection.spaces {
        let space_client = client.space(space_id)?;
        let mut space_bundle = SpaceBundle::default();

        if include_saved_objects && let Some(manifest) = &selection.saved_objects {
            space_bundle.saved_objects =
                SavedObjectsExtractor::new(space_client.clone(), manifest.clone())
                    .extract()
                    .await?;
        }

        if include_workflows {
            space_bundle.workflows = WorkflowsExtractor::new(space_client.clone(), None)
                .search_workflows(None, None)
                .await?;
        }

        if include_agents {
            space_bundle.agents = AgentsExtractor::new(space_client.clone(), None)
                .search_agents(None)
                .await?;
        }

        if include_tools {
            space_bundle.tools = ToolsExtractor::new(space_client, None)
                .search_tools(None)
                .await?;
        }

        if options.expand_dependencies && (include_agents || include_tools || include_workflows) {
            expand_dependencies(
                client,
                space_id,
                &mut space_bundle,
                DependencyExpansionCapabilities {
                    agents: include_agents,
                    tools: include_tools,
                    workflows: include_workflows,
                },
            )
            .await?;
        }

        bundle.by_space.insert(space_id.clone(), space_bundle);
    }

    Ok(bundle)
}

pub async fn push_sync(
    client: &KibanaClient,
    bundle: &SyncBundle,
    options: &SyncOptions,
) -> Result<SyncSummary> {
    let mut summary = SyncSummary::default();
    let include_spaces = !bundle.spaces.is_empty()
        && capability_allowed(
            client,
            ApiCapability::Spaces,
            &options.unsupported_api_policy,
        )
        .await?;
    let include_saved_objects = bundle
        .by_space
        .values()
        .any(|space_bundle| !space_bundle.saved_objects.is_empty())
        && capability_allowed(
            client,
            ApiCapability::SavedObjects,
            &options.unsupported_api_policy,
        )
        .await?;
    let include_workflows = bundle
        .by_space
        .values()
        .any(|space_bundle| !space_bundle.workflows.is_empty())
        && capability_allowed(
            client,
            ApiCapability::Workflows,
            &options.unsupported_api_policy,
        )
        .await?;
    let include_agents = bundle
        .by_space
        .values()
        .any(|space_bundle| !space_bundle.agents.is_empty())
        && capability_allowed(
            client,
            ApiCapability::Agents,
            &options.unsupported_api_policy,
        )
        .await?;
    let include_tools = bundle
        .by_space
        .values()
        .any(|space_bundle| !space_bundle.tools.is_empty())
        && capability_allowed(
            client,
            ApiCapability::Tools,
            &options.unsupported_api_policy,
        )
        .await?;

    if include_spaces {
        summary.spaces_attempted = bundle.spaces.len();
        summary.spaces_applied = SpacesLoader::new(client.clone())
            .with_overwrite(options.overwrite)
            .load(bundle.spaces.clone())
            .await?;
    }

    for (space_id, space_bundle) in &bundle.by_space {
        let space_client = client.space(space_id)?;

        if include_saved_objects {
            summary.saved_objects_attempted += space_bundle.saved_objects.len();
            summary.saved_objects_applied += SavedObjectsLoader::new(space_client.clone())
                .with_overwrite(options.overwrite)
                .load(space_bundle.saved_objects.clone())
                .await?;
        }

        if include_tools {
            summary.tools_attempted += space_bundle.tools.len();
            summary.tools_applied += ToolsLoader::new(space_client.clone())
                .load(space_bundle.tools.clone())
                .await?;
        }

        if include_agents {
            summary.agents_attempted += space_bundle.agents.len();
            summary.agents_applied += AgentsLoader::new(space_client.clone())
                .load(space_bundle.agents.clone())
                .await?;
        }

        if include_workflows {
            summary.workflows_attempted += space_bundle.workflows.len();
            summary.workflows_applied += WorkflowsLoader::new(space_client)
                .load(space_bundle.workflows.clone())
                .await?;
        }
    }

    Ok(summary)
}

async fn capability_allowed(
    client: &KibanaClient,
    capability: ApiCapability,
    policy: &UnsupportedApiPolicy,
) -> Result<bool> {
    if *policy == UnsupportedApiPolicy::Force {
        return Ok(true);
    }

    let version = client.server_version().await?;
    if KibanaClient::supports_capability(&version, capability) {
        return Ok(true);
    }

    let reason = KibanaClient::unsupported_capability_reason(&version, capability);
    match policy {
        UnsupportedApiPolicy::Skip => tracing::debug!("{reason}; skipping"),
        UnsupportedApiPolicy::Warn => tracing::warn!("{reason}; skipping"),
        UnsupportedApiPolicy::Force => {}
    }

    Ok(false)
}

pub async fn expand_dependencies(
    client: &KibanaClient,
    space_id: &str,
    bundle: &mut SpaceBundle,
    capabilities: DependencyExpansionCapabilities,
) -> Result<()> {
    let space_client = client.space(space_id)?;
    let mut existing_agents = ids(&bundle.agents);
    let mut existing_tools = ids(&bundle.tools);
    let mut existing_workflows = ids(&bundle.workflows);
    let mut processed = HashSet::new();
    let mut pending = Vec::new();

    for agent in &bundle.agents {
        pending.extend(find_agent_dependencies(agent));
    }
    for tool in &bundle.tools {
        pending.extend(find_tool_dependencies(tool));
    }
    for workflow in &bundle.workflows {
        pending.extend(find_workflow_dependencies(workflow));
    }

    while let Some(dependency) = pending.pop() {
        if !processed.insert(dependency.clone()) {
            continue;
        }

        match dependency {
            Dependency::Agent(id) if !existing_agents.contains(&id) && capabilities.agents => {
                let fetched =
                    fetch_dependency(&space_client, "api/agent_builder/agents", &id).await?;
                existing_agents.insert(id);
                pending.extend(find_agent_dependencies(&fetched));
                bundle.agents.push(fetched);
            }
            Dependency::Tool(id) if !existing_tools.contains(&id) && capabilities.tools => {
                let fetched =
                    fetch_dependency(&space_client, "api/agent_builder/tools", &id).await?;
                existing_tools.insert(id);
                pending.extend(find_tool_dependencies(&fetched));
                bundle.tools.push(fetched);
            }
            Dependency::Workflow(id)
                if !existing_workflows.contains(&id) && capabilities.workflows =>
            {
                let path = format!("api/workflows/{id}");
                let response = space_client.get_internal(&path).await?;
                if !response.status().is_success() {
                    let status = response.status();
                    let body = response.text().await.unwrap_or_default();
                    return Err(Error::api_response(status, body));
                }
                let fetched = response.json().await?;
                existing_workflows.insert(id);
                pending.extend(find_workflow_dependencies(&fetched));
                bundle.workflows.push(fetched);
            }
            Dependency::Agent(id) if !capabilities.agents => {
                tracing::debug!("skipping dependent agent {id}; agent API is not enabled")
            }
            Dependency::Tool(id) if !capabilities.tools => {
                tracing::debug!("skipping dependent tool {id}; tool API is not enabled")
            }
            Dependency::Workflow(id) if !capabilities.workflows => {
                tracing::debug!("skipping dependent workflow {id}; workflow API is not enabled")
            }
            _ => {}
        }
    }

    Ok(())
}

fn ids(values: &[Value]) -> HashSet<String> {
    values
        .iter()
        .filter_map(|value| value.get("id").and_then(|id| id.as_str()))
        .map(ToOwned::to_owned)
        .collect()
}

async fn fetch_dependency(client: &KibanaClient, prefix: &str, id: &str) -> Result<Value> {
    let path = format!("{prefix}/{id}");
    let response = client.get(&path).await?;
    if !response.status().is_success() {
        let status = response.status();
        let body = response.text().await.unwrap_or_default();
        return Err(Error::api_response(status, body));
    }

    Ok(response.json().await?)
}

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

    #[test]
    fn sync_bundle_is_storage_neutral() {
        let mut bundle = SyncBundle::default();
        bundle.by_space.insert(
            "default".to_string(),
            SpaceBundle {
                saved_objects: vec![json!({"type": "dashboard", "id": "one"})],
                ..SpaceBundle::default()
            },
        );

        assert_eq!(bundle.by_space["default"].saved_objects.len(), 1);
    }

    #[test]
    fn capability_plan_reports_boundaries() {
        let version = crate::parse_kibana_version("9.2.0").unwrap();
        let plan = plan_capabilities(
            &version,
            [
                ApiCapability::Agents,
                ApiCapability::Tools,
                ApiCapability::Workflows,
            ],
        );

        assert!(plan.supported.contains(&ApiCapability::Agents));
        assert!(plan.supported.contains(&ApiCapability::Tools));
        assert_eq!(plan.unsupported[0].capability, ApiCapability::Workflows);
    }
}