auths-cli 0.1.3

Command-line interface for Auths decentralized identity system
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
//! Declarative, headless provisioning for enterprise deployments.
//!
//! Reads a TOML configuration file and reconciles the node's identity state
//! to match. Secrets are handled via environment variable overrides layered
//! automatically by the `config` crate, never passed as CLI arguments.

use crate::factories::storage::build_auths_context;
use crate::ux::format::Output;
use anyhow::{Context, Result, anyhow};
use auths_sdk::signing::PassphraseProvider;
use auths_sdk::storage_layout::install_linearity_hook;
use auths_sdk::workflows::provision::{IdentityConfig, NodeConfig, enforce_identity_state};
use clap::Parser;
use config::{Config, Environment, File};
use std::path::{Path, PathBuf};
use std::sync::Arc;

/// Declarative headless provisioning for enterprise deployments.
///
/// Reads a TOML configuration file and reconciles the node's identity
/// state to match. Environment variables with prefix `AUTHS_` and
/// separator `__` override any TOML values.
///
/// Usage:
/// ```ignore
/// auths provision --config node.toml
/// auths provision --config node.toml --dry-run
/// AUTHS_IDENTITY__KEY_ALIAS=override auths provision --config node.toml
/// ```
#[derive(Parser, Debug, Clone)]
#[command(
    name = "provision",
    about = "Declarative headless provisioning from a TOML config file"
)]
pub struct ProvisionCommand {
    /// Path to the TOML configuration file.
    #[arg(long, value_parser, help = "Path to the TOML config file")]
    pub config: PathBuf,

    /// Validate config and print resolved state without applying changes.
    #[arg(long, help = "Validate and print resolved config without applying")]
    pub dry_run: bool,

    /// Overwrite existing identity if present.
    #[arg(long, help = "Overwrite existing identity")]
    pub force: bool,
}

/// Handle the provision command.
///
/// Args:
/// * `cmd`: The parsed provision command with config path, dry-run, and force flags.
/// * `passphrase_provider`: Provider for key encryption passphrases.
///
/// Usage:
/// ```ignore
/// handle_provision(cmd, Arc::clone(&passphrase_provider))?;
/// ```
pub fn handle_provision(
    cmd: ProvisionCommand,
    passphrase_provider: Arc<dyn PassphraseProvider + Send + Sync>,
) -> Result<()> {
    let out = Output::new();
    let config = load_node_config(&cmd.config)?;

    if cmd.dry_run {
        return display_resolved_state(&config, &out);
    }

    out.print_heading("Auths Provision");
    out.println("================");
    out.newline();

    validate_storage_perimeter(&config.identity, &out)?;
    out.print_info("Initializing identity...");

    let repo_path = Path::new(&config.identity.repo_path);
    let auths_ctx = build_auths_context(
        repo_path,
        &Default::default(),
        Some(passphrase_provider.clone()),
    )?;

    match enforce_identity_state(
        &config,
        cmd.force,
        passphrase_provider.as_ref(),
        auths_ctx.key_storage.as_ref(),
        Arc::clone(&auths_ctx.registry),
        Arc::clone(&auths_ctx.identity_storage),
    )
    .map_err(anyhow::Error::from)?
    {
        None => {
            out.print_success("Identity already exists and matches — no changes needed.");
        }
        Some(result) => {
            out.newline();
            out.print_success("Identity provisioned successfully.");
            out.println(&format!(
                "  {}",
                out.key_value("Identity", &result.controller_did)
            ));
            out.println(&format!(
                "  {}",
                out.key_value("Key name", &result.key_alias)
            ));
        }
    }

    install_system_hooks(&config.identity, &out);
    print_provision_summary(&config, &out);

    Ok(())
}

/// Load and merge TOML config file with environment variable overrides.
///
/// Environment variables use prefix `AUTHS_` with double-underscore separator
/// for nested keys. For example:
/// - `AUTHS_IDENTITY__KEY_ALIAS` overrides `identity.key_alias`
/// - `AUTHS_WITNESS__THRESHOLD` overrides `witness.threshold`
///
/// Args:
/// * `path`: Path to the TOML configuration file.
///
/// Usage:
/// ```ignore
/// let config = load_node_config(Path::new("node.toml"))?;
/// ```
fn load_node_config(path: &Path) -> Result<NodeConfig> {
    let path_str = path
        .to_str()
        .ok_or_else(|| anyhow!("Config path is not valid UTF-8"))?;

    let settings = Config::builder()
        .add_source(File::with_name(path_str))
        .add_source(Environment::with_prefix("AUTHS").separator("__"))
        .build()
        .with_context(|| format!("Failed to load config from {:?}", path))?;

    settings
        .try_deserialize::<NodeConfig>()
        .with_context(|| "Failed to deserialize node config")
}

/// Print the resolved configuration for `--dry-run` inspection.
fn display_resolved_state(config: &NodeConfig, out: &Output) -> Result<()> {
    out.print_heading("Resolved Configuration (dry-run)");
    out.println("=================================");
    out.newline();

    out.println(&format!(
        "  {}",
        out.key_value("key_alias", &config.identity.key_alias)
    ));
    out.println(&format!(
        "  {}",
        out.key_value("repo_path", &config.identity.repo_path)
    ));
    out.println(&format!(
        "  {}",
        out.key_value("preset", &config.identity.preset)
    ));

    if !config.identity.metadata.is_empty() {
        out.newline();
        out.println("  Metadata:");
        for (k, v) in &config.identity.metadata {
            out.println(&format!("    {} = {}", k, v));
        }
    }

    if let Some(ref witness) = config.witness {
        out.newline();
        out.println("  Witness:");
        out.println(&format!(
            "    {}",
            out.key_value(
                "witnesses",
                &witness
                    .witnesses
                    .iter()
                    .map(|w| format!("{} ({})", w.url, w.aid))
                    .collect::<Vec<_>>()
                    .join(", "),
            )
        ));
        out.println(&format!(
            "    {}",
            out.key_value("threshold", &witness.threshold.to_string())
        ));
        out.println(&format!(
            "    {}",
            out.key_value("timeout_ms", &witness.timeout_ms.to_string())
        ));
        out.println(&format!("    {}", out.key_value("policy", &witness.policy)));
    }

    out.newline();
    out.print_success("Config is valid. No changes applied (dry-run).");
    Ok(())
}

/// Ensure the repo directory exists and contains a Git repository.
fn validate_storage_perimeter(identity: &IdentityConfig, out: &Output) -> Result<()> {
    use crate::factories::storage::{ensure_git_repo, open_git_repo};

    let repo_path = Path::new(&identity.repo_path);

    if repo_path.exists() {
        match open_git_repo(repo_path) {
            Ok(_) => {
                out.println(&format!(
                    "  Repository: {} ({})",
                    out.info(&identity.repo_path),
                    out.success("found")
                ));
            }
            Err(_) => {
                out.print_info("Initializing Git repository...");
                ensure_git_repo(repo_path)
                    .with_context(|| format!("Failed to init Git repository at {:?}", repo_path))?;
                out.println(&format!(
                    "  Repository: {} ({})",
                    out.info(&identity.repo_path),
                    out.success("initialized")
                ));
            }
        }
    } else {
        out.print_info("Creating directory and Git repository...");
        ensure_git_repo(repo_path).with_context(|| {
            format!(
                "Failed to create and init Git repository at {:?}",
                repo_path
            )
        })?;
        out.println(&format!(
            "  Repository: {} ({})",
            out.info(&identity.repo_path),
            out.success("created")
        ));
    }

    Ok(())
}

/// Install linearity enforcement hook (best-effort).
fn install_system_hooks(identity: &IdentityConfig, out: &Output) {
    let repo_path = Path::new(&identity.repo_path);
    if let Err(e) = install_linearity_hook(repo_path) {
        out.print_warn(&format!("Could not install linearity hook: {}", e));
    }
}

/// Print a summary of what was provisioned.
fn print_provision_summary(config: &NodeConfig, out: &Output) {
    out.newline();
    out.print_heading("Provision Summary");
    out.println(&format!(
        "  {}",
        out.key_value("Repository", &config.identity.repo_path)
    ));
    out.println(&format!(
        "  {}",
        out.key_value("Key alias", &config.identity.key_alias)
    ));
    out.println(&format!(
        "  {}",
        out.key_value("Preset", &config.identity.preset)
    ));

    if let Some(ref w) = config.witness {
        out.println(&format!(
            "  {}",
            out.key_value(
                "Witnesses",
                &w.witnesses
                    .iter()
                    .map(|e| e.url.clone())
                    .collect::<Vec<_>>()
                    .join(", "),
            )
        ));
        out.println(&format!("  {}", out.key_value("Witness policy", &w.policy)));
    }
}

impl crate::commands::executable::ExecutableCommand for ProvisionCommand {
    fn execute(&self, ctx: &crate::config::CliConfig) -> anyhow::Result<()> {
        handle_provision(self.clone(), ctx.passphrase_provider.clone())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    fn write_test_toml(content: &str) -> NamedTempFile {
        let mut f = tempfile::Builder::new().suffix(".toml").tempfile().unwrap();
        f.write_all(content.as_bytes()).unwrap();
        f
    }

    #[test]
    fn test_load_minimal_config() {
        let toml = r#"
[identity]
key_alias = "test-key"
repo_path = "/tmp/test-auths"
"#;
        let f = write_test_toml(toml);
        let config = load_node_config(f.path()).unwrap();
        assert_eq!(config.identity.key_alias, "test-key");
        assert_eq!(config.identity.repo_path, "/tmp/test-auths");
        assert_eq!(config.identity.preset, "default");
        assert!(config.witness.is_none());
    }

    #[test]
    fn test_load_full_config() {
        let toml = r#"
[identity]
key_alias = "prod-key"
repo_path = "/data/auths"
preset = "radicle"

[identity.metadata]
name = "prod-node-01"
environment = "production"

[witness]
threshold = 2
timeout_ms = 10000
policy = "enforce"

[[witness.witnesses]]
url = "https://witness1.example.com"
aid = "BWitnessOne00000000000000000000000000000000"

[[witness.witnesses]]
url = "https://witness2.example.com"
aid = "BWitnessTwo00000000000000000000000000000000"
"#;
        let f = write_test_toml(toml);
        let config = load_node_config(f.path()).unwrap();
        assert_eq!(config.identity.key_alias, "prod-key");
        assert_eq!(config.identity.preset, "radicle");
        assert_eq!(
            config.identity.metadata.get("name").unwrap(),
            "prod-node-01"
        );
        let w = config.witness.unwrap();
        assert_eq!(w.witnesses.len(), 2);
        assert_eq!(w.threshold, 2);
        assert_eq!(w.timeout_ms, 10000);
        assert_eq!(w.policy, "enforce");
    }

    #[test]
    fn test_load_config_with_defaults() {
        let toml = r#"
[identity]
"#;
        let f = write_test_toml(toml);
        let config = load_node_config(f.path()).unwrap();
        assert_eq!(config.identity.key_alias, "main");
        assert_eq!(config.identity.preset, "default");
    }

    #[test]
    fn test_load_config_missing_file() {
        let result = load_node_config(Path::new("/nonexistent/config.toml"));
        assert!(result.is_err());
    }

    #[test]
    fn test_provision_command_defaults() {
        let cmd = ProvisionCommand {
            config: PathBuf::from("test.toml"),
            dry_run: false,
            force: false,
        };
        assert!(!cmd.dry_run);
        assert!(!cmd.force);
    }

    #[test]
    fn test_witness_policy_parsing() {
        let toml = r#"
[identity]
key_alias = "test"
repo_path = "/tmp/test"

[witness]
urls = ["https://w1.example.com"]
threshold = 1
policy = "warn"
"#;
        let f = write_test_toml(toml);
        let config = load_node_config(f.path()).unwrap();
        let w = config.witness.unwrap();
        assert_eq!(w.policy, "warn");
    }
}