rigg 0.17.0

Configuration-as-code CLI for Azure AI Search and Microsoft Foundry
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
//! Credential handling for push operations.
//!
//! Strips volatile/read-only fields before push and injects storage
//! credentials for new data sources and knowledge sources.

use std::io::{self, Write};

use anyhow::Result;
use tracing::info;

use rigg_client::ArmClient;
use rigg_client::auth::AzCliAuth;
use rigg_core::resources::ResourceKind;

use crate::commands::common::{get_read_only_fields, get_volatile_fields};

/// Remove volatile and read-only fields from a resource definition before sending to Azure.
///
/// Recurses through the entire JSON tree, stripping field names at every level.
/// This handles both top-level fields (like `@odata.etag`) and nested read-only
/// fields (like `createdResources` inside `azureBlobParameters`).
///
/// Volatile fields (etag, context, secrets) are always stripped.
/// Read-only fields (knowledgeSources, createdResources, etc.) are kept in local
/// files for documentation but must be stripped before push since Azure rejects them.
pub(super) fn strip_volatile_fields(
    kind: ResourceKind,
    definition: &serde_json::Value,
) -> serde_json::Value {
    let volatile_fields = get_volatile_fields(kind);
    let read_only_fields = get_read_only_fields(kind);
    let all_fields: Vec<&str> = volatile_fields
        .iter()
        .chain(read_only_fields.iter())
        .copied()
        .collect();
    strip_fields_recursive(definition, &all_fields)
}

/// Check if a resource needs credential injection before push.
///
/// Returns true when:
/// - DataSource being created (or recreated) without a `credentials` object
/// - KnowledgeSource being created (or recreated) with `<redacted>` connectionString
pub(super) fn needs_credentials(
    kind: ResourceKind,
    definition: &serde_json::Value,
    exists: bool,
    needs_recreate: bool,
) -> bool {
    let is_new = !exists || needs_recreate;
    if !is_new {
        return false;
    }

    match kind {
        ResourceKind::DataSource => {
            // credentials is a volatile field — stripped during pull, so it's absent on disk.
            // If someone manually added it, respect that.
            definition
                .get("credentials")
                .and_then(|c| c.get("connectionString"))
                .and_then(|s| s.as_str())
                .is_none_or(|s| s.is_empty())
        }
        ResourceKind::KnowledgeSource => {
            // Azure returns "<redacted>" for connectionString in GET responses.
            // Check if it's redacted or missing.
            let conn = definition
                .pointer("/azureBlobParameters/connectionString")
                .and_then(|v| v.as_str());
            matches!(conn, Some("<redacted>") | None)
        }
        _ => false,
    }
}

/// Discover a storage account connection string via ARM.
///
/// Falls back gracefully — returns None on any failure (not logged in,
/// no storage accounts found, etc.).
pub(super) async fn discover_storage_credentials(
    env: &rigg_core::config::ResolvedEnvironment,
    cached: &mut Option<String>,
) -> Option<String> {
    // Return cached value if available
    if let Some(conn) = cached {
        return Some(conn.clone());
    }

    let arm = ArmClient::new().ok()?;

    // Get subscription ID: config first, then az cli
    let subscription_id = env
        .primary_search_service()
        .and_then(|s| s.subscription.clone())
        .or_else(|| {
            AzCliAuth::check_status()
                .ok()
                .and_then(|s| s.subscription_id)
        })?;

    // Get resource group: config first, then ARM discovery
    let search_svc = env.primary_search_service()?;
    let resource_group = if let Some(rg) = search_svc.resource_group.clone() {
        rg
    } else {
        arm.find_resource_group(&subscription_id, &search_svc.name)
            .await
            .ok()?
    };

    let accounts = arm
        .list_storage_accounts(&subscription_id, &resource_group)
        .await
        .ok()?;

    if accounts.is_empty() {
        return None;
    }

    let account_name = if accounts.len() == 1 {
        let name = &accounts[0].name;
        println!();
        info!("Auto-selected storage account: {}", name);
        name.clone()
    } else {
        println!();
        println!(
            "Multiple storage accounts found in resource group '{}':",
            resource_group
        );
        for (i, acct) in accounts.iter().enumerate() {
            println!("  [{}] {}", i + 1, acct);
        }
        print!("Select storage account [1]: ");
        io::stdout().flush().ok()?;

        let mut input = String::new();
        io::stdin().read_line(&mut input).ok()?;
        let input = input.trim();

        let idx = if input.is_empty() {
            0
        } else {
            input.parse::<usize>().ok()?.checked_sub(1)?
        };

        accounts.get(idx)?.name.clone()
    };

    let conn_string = arm
        .get_storage_connection_string(&subscription_id, &resource_group, &account_name)
        .await
        .ok()?;

    *cached = Some(conn_string.clone());
    Some(conn_string)
}

/// Inject credentials into a resource definition for new data sources or knowledge sources.
///
/// Tries ARM-based auto-discovery first, then falls back to prompting the user.
pub(super) async fn inject_credentials(
    kind: ResourceKind,
    definition: &serde_json::Value,
    name: &str,
    env: &rigg_core::config::ResolvedEnvironment,
    cached: &mut Option<String>,
) -> Result<serde_json::Value> {
    // Try auto-discovery first
    let conn_string = match discover_storage_credentials(env, cached).await {
        Some(c) => c,
        None => {
            // Fall back to manual prompt
            println!();
            print!(
                "Enter connection string for {} '{}' (or press Enter to skip): ",
                kind.display_name(),
                name
            );
            io::stdout().flush()?;
            let mut input = String::new();
            io::stdin().read_line(&mut input)?;
            let input = input.trim().to_string();
            if input.is_empty() {
                return Ok(definition.clone());
            }
            input
        }
    };

    let mut def = definition.clone();

    match kind {
        ResourceKind::DataSource => {
            // Inject {"credentials": {"connectionString": "..."}}
            if let Some(obj) = def.as_object_mut() {
                obj.insert(
                    "credentials".to_string(),
                    serde_json::json!({"connectionString": conn_string}),
                );
            }
        }
        ResourceKind::KnowledgeSource => {
            // Replace azureBlobParameters.connectionString
            if let Some(blob_params) = def.get_mut("azureBlobParameters") {
                if let Some(obj) = blob_params.as_object_mut() {
                    obj.insert(
                        "connectionString".to_string(),
                        serde_json::Value::String(conn_string),
                    );
                }
            }
        }
        _ => {}
    }

    Ok(def)
}

fn strip_fields_recursive(value: &serde_json::Value, fields: &[&str]) -> serde_json::Value {
    match value {
        serde_json::Value::Object(map) => {
            let filtered: serde_json::Map<String, serde_json::Value> = map
                .iter()
                .filter(|(k, _)| !fields.contains(&k.as_str()))
                .map(|(k, v)| (k.clone(), strip_fields_recursive(v, fields)))
                .collect();
            serde_json::Value::Object(filtered)
        }
        serde_json::Value::Array(arr) => serde_json::Value::Array(
            arr.iter()
                .map(|v| strip_fields_recursive(v, fields))
                .collect(),
        ),
        _ => value.clone(),
    }
}

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

    #[test]
    fn test_strip_volatile_fields_removes_etag_and_context() {
        let definition = json!({
            "name": "test-index",
            "fields": [],
            "@odata.etag": "W/\"abc\"",
            "@odata.context": "https://svc.search.windows.net/$metadata#indexes/$entity"
        });
        let clean = strip_volatile_fields(ResourceKind::Index, &definition);
        let obj = clean.as_object().unwrap();
        assert!(obj.contains_key("name"));
        assert!(obj.contains_key("fields"));
        assert!(!obj.contains_key("@odata.etag"));
        assert!(!obj.contains_key("@odata.context"));
    }

    #[test]
    fn test_strip_volatile_fields_removes_knowledge_source_top_level() {
        let definition = json!({
            "name": "ks-1",
            "indexName": "my-index",
            "description": "Test",
            "ingestionPermissionOptions": { "someConfig": true }
        });
        let clean = strip_volatile_fields(ResourceKind::KnowledgeSource, &definition);
        let obj = clean.as_object().unwrap();
        assert!(obj.contains_key("name"));
        assert!(obj.contains_key("indexName"));
        assert!(obj.contains_key("description"));
        assert!(!obj.contains_key("ingestionPermissionOptions"));
    }

    #[test]
    fn test_strip_volatile_fields_removes_nested_created_resources() {
        let definition = json!({
            "name": "ks-1",
            "kind": "azureBlob",
            "azureBlobParameters": {
                "containerName": "docs",
                "connectionString": "<redacted>",
                "createdResources": {
                    "datasource": "ds-1",
                    "indexer": "ixer-1",
                    "skillset": "sk-1",
                    "index": "idx-1"
                }
            }
        });
        let clean = strip_volatile_fields(ResourceKind::KnowledgeSource, &definition);
        let blob_params = clean
            .get("azureBlobParameters")
            .unwrap()
            .as_object()
            .unwrap();
        assert!(blob_params.contains_key("containerName"));
        assert!(blob_params.contains_key("connectionString"));
        assert!(
            !blob_params.contains_key("createdResources"),
            "createdResources should be stripped from nested object"
        );
    }

    #[test]
    fn test_strip_volatile_fields_preserves_knowledge_base_knowledge_sources() {
        // knowledgeSources is a normal pushable field — NOT stripped
        let definition = json!({
            "name": "my-kb",
            "description": "Test KB",
            "knowledgeSources": [
                {"name": "ks-1"},
                {"name": "ks-2"}
            ]
        });
        let clean = strip_volatile_fields(ResourceKind::KnowledgeBase, &definition);
        let obj = clean.as_object().unwrap();
        assert!(obj.contains_key("name"));
        assert!(obj.contains_key("description"));
        assert!(
            obj.contains_key("knowledgeSources"),
            "knowledgeSources is pushable and should be preserved"
        );
    }

    #[test]
    fn test_strip_volatile_fields_removes_datasource_credentials() {
        let definition = json!({
            "name": "ds-1",
            "type": "azureblob",
            "credentials": { "connectionString": "secret" }
        });
        let clean = strip_volatile_fields(ResourceKind::DataSource, &definition);
        let obj = clean.as_object().unwrap();
        assert!(obj.contains_key("name"));
        assert!(!obj.contains_key("credentials"));
    }

    #[test]
    fn test_strip_volatile_fields_preserves_non_volatile() {
        let definition = json!({
            "name": "sk-1",
            "skills": [{"name": "skill1"}],
            "description": "My skillset"
        });
        let clean = strip_volatile_fields(ResourceKind::Skillset, &definition);
        assert_eq!(clean, definition);
    }

    #[test]
    fn test_strip_volatile_fields_handles_no_volatile_present() {
        let definition = json!({
            "name": "test",
            "fields": []
        });
        let clean = strip_volatile_fields(ResourceKind::Index, &definition);
        assert_eq!(clean, definition);
    }

    #[test]
    fn test_strip_volatile_fields_removes_indexer_start_time() {
        let definition = json!({
            "name": "my-indexer",
            "dataSourceName": "ds-1",
            "targetIndexName": "idx-1",
            "schedule": {
                "interval": "P1D",
                "startTime": "2026-02-06T22:03:10.254Z"
            }
        });
        let clean = strip_volatile_fields(ResourceKind::Indexer, &definition);
        let schedule = clean.get("schedule").unwrap().as_object().unwrap();
        assert!(schedule.contains_key("interval"));
        assert!(
            !schedule.contains_key("startTime"),
            "startTime should be stripped from indexer schedule"
        );
    }

    // --- Credential injection tests ---

    #[test]
    fn test_needs_credentials_datasource_new_no_creds() {
        let def = json!({"name": "ds-1", "type": "azureblob", "container": {"name": "docs"}});
        assert!(needs_credentials(
            ResourceKind::DataSource,
            &def,
            false,
            false
        ));
    }

    #[test]
    fn test_needs_credentials_datasource_update() {
        let def = json!({"name": "ds-1", "type": "azureblob", "container": {"name": "docs"}});
        // Existing resource — Azure preserves credentials on update
        assert!(!needs_credentials(
            ResourceKind::DataSource,
            &def,
            true,
            false
        ));
    }

    #[test]
    fn test_needs_credentials_datasource_recreate() {
        let def = json!({"name": "ds-1", "type": "azureblob", "container": {"name": "docs"}});
        // Drop-and-recreate needs credentials like a new resource
        assert!(needs_credentials(
            ResourceKind::DataSource,
            &def,
            true,
            true
        ));
    }

    #[test]
    fn test_needs_credentials_datasource_with_creds() {
        let def = json!({
            "name": "ds-1",
            "type": "azureblob",
            "credentials": {"connectionString": "DefaultEndpointsProtocol=https;AccountName=..."},
            "container": {"name": "docs"}
        });
        // Credentials already present — no injection needed
        assert!(!needs_credentials(
            ResourceKind::DataSource,
            &def,
            false,
            false
        ));
    }

    #[test]
    fn test_needs_credentials_ks_redacted() {
        let def = json!({
            "name": "ks-1",
            "azureBlobParameters": {
                "containerName": "docs",
                "connectionString": "<redacted>"
            }
        });
        assert!(needs_credentials(
            ResourceKind::KnowledgeSource,
            &def,
            false,
            false
        ));
    }

    #[test]
    fn test_needs_credentials_ks_missing_connection_string() {
        let def = json!({
            "name": "ks-1",
            "azureBlobParameters": {"containerName": "docs"}
        });
        assert!(needs_credentials(
            ResourceKind::KnowledgeSource,
            &def,
            false,
            false
        ));
    }

    #[test]
    fn test_needs_credentials_ks_real_value() {
        let def = json!({
            "name": "ks-1",
            "azureBlobParameters": {
                "containerName": "docs",
                "connectionString": "DefaultEndpointsProtocol=https;AccountName=test;AccountKey=abc"
            }
        });
        assert!(!needs_credentials(
            ResourceKind::KnowledgeSource,
            &def,
            false,
            false
        ));
    }

    #[test]
    fn test_needs_credentials_ks_update() {
        let def = json!({
            "name": "ks-1",
            "azureBlobParameters": {
                "containerName": "docs",
                "connectionString": "<redacted>"
            }
        });
        // Existing KS update — Azure preserves credentials
        assert!(!needs_credentials(
            ResourceKind::KnowledgeSource,
            &def,
            true,
            false
        ));
    }

    #[test]
    fn test_needs_credentials_index() {
        let def = json!({"name": "idx-1", "fields": []});
        // Indexes don't need credentials
        assert!(!needs_credentials(ResourceKind::Index, &def, false, false));
    }

    fn test_env() -> rigg_core::config::ResolvedEnvironment {
        rigg_core::config::ResolvedEnvironment {
            name: "test".to_string(),
            search: vec![rigg_core::SearchServiceConfig {
                name: "test-search".to_string(),
                label: None,
                subscription: None,
                resource_group: None,
                api_version: "2024-07-01".to_string(),
                preview_api_version: "2025-11-01-preview".to_string(),
            }],
            foundry: vec![],
            sync: rigg_core::config::SyncConfig::default(),
        }
    }

    #[tokio::test]
    async fn test_inject_credentials_datasource() {
        let def = json!({
            "name": "ds-1",
            "type": "azureblob",
            "container": {"name": "docs"}
        });
        let conn = "DefaultEndpointsProtocol=https;AccountName=test;AccountKey=abc";
        let mut cached = Some(conn.to_string());
        let env = test_env();

        let result = inject_credentials(ResourceKind::DataSource, &def, "ds-1", &env, &mut cached)
            .await
            .unwrap();

        assert_eq!(
            result
                .get("credentials")
                .unwrap()
                .get("connectionString")
                .unwrap()
                .as_str()
                .unwrap(),
            conn
        );
        // Original fields preserved
        assert_eq!(result.get("name").unwrap().as_str().unwrap(), "ds-1");
        assert_eq!(result.get("type").unwrap().as_str().unwrap(), "azureblob");
    }

    #[tokio::test]
    async fn test_inject_credentials_ks() {
        let def = json!({
            "name": "ks-1",
            "azureBlobParameters": {
                "containerName": "docs",
                "connectionString": "<redacted>"
            }
        });
        let conn = "DefaultEndpointsProtocol=https;AccountName=test;AccountKey=abc";
        let mut cached = Some(conn.to_string());
        let env = test_env();

        let result = inject_credentials(
            ResourceKind::KnowledgeSource,
            &def,
            "ks-1",
            &env,
            &mut cached,
        )
        .await
        .unwrap();

        assert_eq!(
            result
                .pointer("/azureBlobParameters/connectionString")
                .unwrap()
                .as_str()
                .unwrap(),
            conn
        );
        // Original fields preserved
        assert_eq!(
            result
                .pointer("/azureBlobParameters/containerName")
                .unwrap()
                .as_str()
                .unwrap(),
            "docs"
        );
    }
}