arcula 2.0.3

Arcula - MongoDB database synchronization tool
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
use std::io::{self, Read};

use anyhow::{anyhow, Context, Result};
use clap::Subcommand;
use colored::Colorize;
use inquire::{Confirm, Password, PasswordDisplayMode};
use serde::Serialize;

use crate::config::{Environment, EnvironmentKind, MongoConfig};
use crate::connections::{self, ConnectionInfo, ConnectionMigrationReport, ConnectionPolicyPatch};
use crate::output;
use crate::utils::mongodb;

#[derive(Debug, Subcommand)]
pub enum ConnectionCommand {
    /// Add or update a named MongoDB connection in secure storage
    Add {
        /// Connection name used by --from/--to, e.g. dev or prod
        name: String,

        /// Environment kind for safety rules
        #[arg(long, value_enum, default_value_t = EnvironmentKind::Other)]
        kind: EnvironmentKind,

        /// Mark this connection as protected even if it is not prod
        #[arg(long)]
        protected: bool,

        /// MongoDB URI. Prefer prompt or --uri-stdin to avoid shell history.
        #[arg(long, value_name = "URI", conflicts_with = "uri_stdin")]
        uri: Option<String>,

        /// Read the MongoDB URI from stdin
        #[arg(long, conflicts_with = "uri")]
        uri_stdin: bool,

        /// Update an existing connection
        #[arg(long)]
        force: bool,
    },

    /// List configured connections without revealing raw URIs
    List {
        /// Include masked URI values; may access secure storage for each connection
        #[arg(long)]
        show_uri: bool,
    },

    /// Show one connection without revealing the raw URI
    Show {
        /// Connection name
        name: String,
    },

    /// Test that a connection can reach MongoDB
    Test {
        /// Connection name
        name: String,
    },

    /// Update per-connection safety policy flags
    Policy {
        /// Connection name
        name: String,

        /// Whether this connection may be used as sync source
        #[arg(long)]
        allow_as_source: Option<bool>,

        /// Whether this connection may be used as sync target
        #[arg(long)]
        allow_as_target: Option<bool>,

        /// Whether agents may execute plans targeting this connection without human approval
        #[arg(long)]
        allow_agent_apply: Option<bool>,

        /// Whether sync plans targeting this connection require OS-backed human approval
        #[arg(long)]
        human_approval_required: Option<bool>,

        /// Whether destructive syncs targeting this connection require a backup
        #[arg(long)]
        destructive_requires_backup: Option<bool>,

        /// Whether target backups must be verified before destructive sync continues
        #[arg(long)]
        backup_verification_required: Option<bool>,
    },

    /// Remove a connection from metadata and secure storage
    Remove {
        /// Connection name
        name: String,

        /// Do not prompt for confirmation
        #[arg(long)]
        yes: bool,
    },

    /// Import MONGO_<ENV>_URI entries from the loaded environment into secure storage
    ImportEnv {
        /// Update existing stored connections
        #[arg(long)]
        force: bool,
    },

    /// Migrate old per-connection secure-storage entries into the single connection vault
    MigrateVault,
}

#[derive(Serialize)]
struct ConnectionTestResult {
    connection: ConnectionInfo,
    database_count: usize,
    databases: Vec<String>,
}

pub async fn execute(command: ConnectionCommand) -> Result<()> {
    match command {
        ConnectionCommand::Add {
            name,
            kind,
            protected,
            uri,
            uri_stdin,
            force,
        } => add_connection(&name, kind, protected, uri, uri_stdin, force),
        ConnectionCommand::List { show_uri } => list_connections(show_uri),
        ConnectionCommand::Show { name } => show_connection(&name),
        ConnectionCommand::Test { name } => test_connection(&name).await,
        ConnectionCommand::Policy {
            name,
            allow_as_source,
            allow_as_target,
            allow_agent_apply,
            human_approval_required,
            destructive_requires_backup,
            backup_verification_required,
        } => update_policy(
            &name,
            ConnectionPolicyPatch {
                allow_as_source,
                allow_as_target,
                allow_agent_apply,
                human_approval_required,
                destructive_requires_backup,
                backup_verification_required,
            },
        ),
        ConnectionCommand::Remove { name, yes } => remove_connection(&name, yes),
        ConnectionCommand::ImportEnv { force } => import_env_connections(force),
        ConnectionCommand::MigrateVault => migrate_vault(),
    }
}

fn add_connection(
    name: &str,
    kind: EnvironmentKind,
    protected: bool,
    uri: Option<String>,
    uri_stdin: bool,
    force: bool,
) -> Result<()> {
    let uri = read_uri(uri, uri_stdin)?;
    let info = connections::upsert_connection(name, uri.trim(), kind, protected, force)?;

    if output::is_json() {
        output::print_json_success("connection", &info);
        return Ok(());
    }

    println!(
        "{} {} ({}, protected: {})",
        "Saved connection:".green().bold(),
        info.name.bold(),
        info.kind,
        info.protected
    );
    if let Some(uri_masked) = info.uri_masked {
        println!("{} {}", "URI:".yellow(), uri_masked);
    }
    println!(
        "{} {}",
        "Metadata:".yellow(),
        connections::config_path().display()
    );

    Ok(())
}

fn list_connections(show_uri: bool) -> Result<()> {
    let infos = connections::list_infos(show_uri)?;

    if output::is_json() {
        output::print_json_success("connections", &infos);
        return Ok(());
    }

    if infos.is_empty() {
        println!("{}", "No stored connections configured.".yellow());
        println!("Add one with: arcula connection add dev --kind dev");
        return Ok(());
    }

    println!("\n{}", "Stored MongoDB Connections:".bold().underline());
    for info in infos {
        println!("\n{} {}", "Connection:".green().bold(), info.name.bold());
        println!("{} {}", "Kind:".yellow(), info.kind);
        println!("{} {}", "Protected:".yellow(), info.protected);
        println!("{} {}", "Secret ref:".yellow(), info.secret_ref);
        println!(
            "{} source={}, target={}, agent_apply={}, human_approval={}, destructive_backup={}, backup_verify={}",
            "Policy:".yellow(),
            info.policy.allow_as_source,
            info.policy.allow_as_target,
            info.policy.allow_agent_apply,
            info.policy.human_approval_required,
            info.policy.destructive_requires_backup,
            info.policy.backup_verification_required
        );
        if let Some(uri_masked) = info.uri_masked {
            println!("{} {}", "URI:".yellow(), uri_masked);
        }
    }
    println!();

    Ok(())
}

fn show_connection(name: &str) -> Result<()> {
    let Some(info) = connections::get_info(name, true)? else {
        return Err(anyhow!("Connection '{}' does not exist", name));
    };

    if output::is_json() {
        output::print_json_success("connection", &info);
        return Ok(());
    }

    println!("\n{} {}", "Connection:".green().bold(), info.name.bold());
    println!("{} {}", "Kind:".yellow(), info.kind);
    println!("{} {}", "Protected:".yellow(), info.protected);
    println!("{} {}", "Secret ref:".yellow(), info.secret_ref);
    println!(
        "{} source={}, target={}, agent_apply={}, human_approval={}, destructive_backup={}, backup_verify={}",
        "Policy:".yellow(),
        info.policy.allow_as_source,
        info.policy.allow_as_target,
        info.policy.allow_agent_apply,
        info.policy.human_approval_required,
        info.policy.destructive_requires_backup,
        info.policy.backup_verification_required
    );
    if let Some(uri_masked) = info.uri_masked {
        println!("{} {}", "URI:".yellow(), uri_masked);
    }
    println!();

    Ok(())
}

fn update_policy(name: &str, patch: ConnectionPolicyPatch) -> Result<()> {
    let info = connections::update_policy(name, patch)?;

    if output::is_json() {
        output::print_json_success("connection", &info);
        return Ok(());
    }

    println!(
        "{} {}",
        "Updated connection policy:".green().bold(),
        info.name.bold()
    );
    println!(
        "{} source={}, target={}, agent_apply={}, human_approval={}, destructive_backup={}, backup_verify={}",
        "Policy:".yellow(),
        info.policy.allow_as_source,
        info.policy.allow_as_target,
        info.policy.allow_agent_apply,
        info.policy.human_approval_required,
        info.policy.destructive_requires_backup,
        info.policy.backup_verification_required
    );
    if info.protected || info.kind.is_prod() {
        println!(
            "{} Protected/prod safety floor forces agent_apply=false, human_approval=true, destructive_backup=true, backup_verify=true.",
            "Note:".yellow().bold()
        );
    }
    Ok(())
}

async fn test_connection(name: &str) -> Result<()> {
    let normalized = connections::normalize_name(name)?;
    let Some(uri) = connections::get_uri(&normalized)? else {
        return Err(anyhow!("Connection '{}' does not exist", normalized));
    };
    let Some(metadata) = connections::find_metadata(&normalized)? else {
        return Err(anyhow!("Connection '{}' does not exist", normalized));
    };

    let config = MongoConfig {
        connection_string: uri,
        environment: Environment::new(&normalized),
    };
    let databases = mongodb::list_databases(&config).await?;
    let filtered_databases: Vec<String> = databases
        .into_iter()
        .filter(|db| !matches!(db.as_str(), "admin" | "local" | "config"))
        .collect();
    let info = ConnectionInfo {
        name: metadata.name,
        kind: metadata.kind,
        protected: metadata.protected,
        secret_ref: metadata.secret_ref,
        policy: metadata.policy,
        uri_masked: Some(mongodb::mask_connection_string(&config.connection_string)),
    };
    let result = ConnectionTestResult {
        connection: info,
        database_count: filtered_databases.len(),
        databases: filtered_databases,
    };

    if output::is_json() {
        output::print_json_success("connection_test", &result);
        return Ok(());
    }

    println!(
        "{} {} reachable ({} user databases)",
        "Connection test passed:".green().bold(),
        result.connection.name.bold(),
        result.database_count
    );
    for db in &result.databases {
        println!("  - {db}");
    }

    Ok(())
}

fn remove_connection(name: &str, yes: bool) -> Result<()> {
    if !yes {
        if output::is_json() {
            return Err(anyhow!(
                "Refusing to prompt in JSON output. Pass --yes to remove the connection."
            ));
        }

        let normalized = connections::normalize_name(name)?;
        let proceed = Confirm::new(&format!("Remove stored connection '{}' ?", normalized))
            .with_default(false)
            .prompt()?;
        if !proceed {
            println!("Operation cancelled.");
            return Ok(());
        }
    }

    let info = connections::remove_connection(name)?;

    if output::is_json() {
        output::print_json_success("connection_removed", &info);
        return Ok(());
    }

    println!(
        "{} {}",
        "Removed connection:".green().bold(),
        info.name.bold()
    );
    Ok(())
}

fn migrate_vault() -> Result<()> {
    let report = connections::migrate_legacy_connections_to_vault()?;

    if output::is_json() {
        output::print_json_success("connection_vault_migration", &report);
        return Ok(());
    }

    print_migration_report(&report);
    Ok(())
}

fn print_migration_report(report: &ConnectionMigrationReport) {
    println!(
        "{} {} connection(s)",
        "Migrated to connection vault:".green().bold(),
        report.migrated.len()
    );
    for info in &report.migrated {
        println!("  - {}", info.name);
    }

    if !report.already_in_vault.is_empty() {
        println!(
            "{} {} connection(s)",
            "Already in vault:".yellow().bold(),
            report.already_in_vault.len()
        );
        for name in &report.already_in_vault {
            println!("  - {name}");
        }
    }

    if !report.missing_legacy_secret.is_empty() {
        println!(
            "{} {} connection(s)",
            "Missing legacy secret:".yellow().bold(),
            report.missing_legacy_secret.len()
        );
        for name in &report.missing_legacy_secret {
            println!("  - {name}");
        }
        println!(
            "{} Re-add missing connections with `arcula connection add <name> --kind <kind>` or import from .env with `arcula --env connection import-env --force`.",
            "Note:".yellow().bold()
        );
    }
}

fn import_env_connections(force: bool) -> Result<()> {
    let infos = connections::import_env_connections(force)?;

    if output::is_json() {
        output::print_json_success("connections_imported", &infos);
        return Ok(());
    }

    if infos.is_empty() {
        println!(
            "{}",
            "No new MONGO_<ENV>_URI entries were imported.".yellow()
        );
        if !force {
            println!("Existing stored connections were skipped. Pass --force to update them.");
        }
        return Ok(());
    }

    println!(
        "{} {} connection(s)",
        "Imported".green().bold(),
        infos.len()
    );
    for info in infos {
        println!(
            "  - {} ({}, protected: {})",
            info.name, info.kind, info.protected
        );
    }

    Ok(())
}

fn read_uri(uri: Option<String>, uri_stdin: bool) -> Result<String> {
    if let Some(uri) = uri {
        return Ok(uri);
    }

    if uri_stdin {
        let mut buffer = String::new();
        io::stdin()
            .read_to_string(&mut buffer)
            .context("Failed to read MongoDB URI from stdin")?;
        return Ok(buffer.trim().to_string());
    }

    if output::is_json() {
        return Err(anyhow!(
            "MongoDB URI is required in JSON output. Use --uri or --uri-stdin."
        ));
    }

    let uri = Password::new("MongoDB URI:")
        .without_confirmation()
        .with_display_mode(PasswordDisplayMode::Hidden)
        .with_display_toggle_enabled()
        .prompt()?;

    Ok(uri)
}