via-cli 0.2.0

Run commands and API requests with 1Password-backed credentials without exposing secrets to your shell
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
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
use std::collections::BTreeMap;
use std::process::Command;

use crate::config::{
    AuthConfig, CommandConfig, Config, OnePasswordCacheMode, ProviderConfig, RestCommandConfig,
    ServiceConfig,
};
use crate::error::ViaError;
use crate::providers::ProviderRegistry;

pub fn run(config: &Config, only_service: Option<&str>) -> Result<(), ViaError> {
    validate_requested_service(config, only_service)?;

    let mut status = DoctorStatus::default();
    let provider_ready = check_providers(config, &mut status);
    let providers = ProviderRegistry::from_config(config)?;

    for (service_name, service) in &config.services {
        if should_check_service(service_name, only_service) {
            check_service(
                service_name,
                service,
                &provider_ready,
                &providers,
                &mut status,
            )?;
        }
    }

    status.into_result()
}

fn validate_requested_service(config: &Config, only_service: Option<&str>) -> Result<(), ViaError> {
    if let Some(service_name) = only_service {
        if !config.services.contains_key(service_name) {
            return Err(ViaError::UnknownService(service_name.to_owned()));
        }
    }

    Ok(())
}

fn should_check_service(service_name: &str, only_service: Option<&str>) -> bool {
    only_service.is_none_or(|only| only == service_name)
}

fn check_service(
    service_name: &str,
    service: &ServiceConfig,
    provider_ready: &BTreeMap<String, bool>,
    providers: &ProviderRegistry,
    status: &mut DoctorStatus,
) -> Result<(), ViaError> {
    println!("service {service_name}: checking");
    let service_provider_ready = provider_ready
        .get(&service.provider)
        .copied()
        .unwrap_or(false);

    check_service_secrets(
        service_name,
        service,
        service_provider_ready,
        providers,
        status,
    )?;
    check_service_commands(
        service_name,
        service,
        service_provider_ready,
        providers,
        status,
    )
}

fn check_service_secrets(
    service_name: &str,
    service: &ServiceConfig,
    service_provider_ready: bool,
    providers: &ProviderRegistry,
    status: &mut DoctorStatus,
) -> Result<(), ViaError> {
    if service.secrets.is_empty() {
        println!("  secrets: none configured");
        return Ok(());
    }

    if !service_provider_ready {
        status.fail();
        println!(
            "  secrets: skipped because provider `{}` is not ready",
            service.provider
        );
        print_agent_guidance(
            "Ask the user to complete secret provider setup, then rerun `via config doctor`.",
        );
        return Ok(());
    }

    let provider = providers.get(&service.provider)?;
    for (secret_name, reference) in &service.secrets {
        match provider.resolve(reference) {
            Ok(_) => println!("  secret {secret_name}: readable by via"),
            Err(error) => {
                status.fail();
                print_secret_failure(service_name, secret_name, &error);
            }
        }
    }

    Ok(())
}

fn check_service_commands(
    service_name: &str,
    service: &ServiceConfig,
    service_provider_ready: bool,
    providers: &ProviderRegistry,
    status: &mut DoctorStatus,
) -> Result<(), ViaError> {
    for (command_name, command) in &service.commands {
        check_service_command(
            service_name,
            command_name,
            service,
            command,
            service_provider_ready,
            providers,
            status,
        )?;
    }

    Ok(())
}

fn check_service_command(
    service_name: &str,
    command_name: &str,
    service: &ServiceConfig,
    command: &CommandConfig,
    service_provider_ready: bool,
    providers: &ProviderRegistry,
    status: &mut DoctorStatus,
) -> Result<(), ViaError> {
    match command {
        CommandConfig::Rest(rest) => {
            println!("  capability {command_name}: rest");
            if service_provider_ready {
                check_rest_auth(service_name, command_name, service, rest, providers, status)?;
            }
        }
        CommandConfig::Delegated(delegated) => {
            check_delegated_command(command_name, &delegated.program, &delegated.check, status);
        }
    }

    Ok(())
}

fn check_delegated_command(
    command_name: &str,
    program: &str,
    check: &[String],
    status: &mut DoctorStatus,
) {
    match check_program(program, check) {
        Ok(()) => println!("  capability {command_name}: delegated {program}"),
        Err(error) => {
            status.fail();
            print_delegated_failure(command_name, program, &error);
        }
    }
}

fn check_rest_auth(
    service_name: &str,
    command_name: &str,
    service: &ServiceConfig,
    rest: &RestCommandConfig,
    providers: &ProviderRegistry,
    status: &mut DoctorStatus,
) -> Result<(), ViaError> {
    let Some(auth @ AuthConfig::GitHubApp { .. }) = &rest.auth else {
        return Ok(());
    };

    let provider = providers.get(&service.provider)?;

    match resolve_github_app_doctor_secrets(service_name, service, provider, auth).and_then(
        |(credential, private_key)| {
            crate::auth::github_app::validate_credential_bundle(
                credential.expose(),
                private_key.as_deref(),
            )
        },
    ) {
        Ok(()) => println!("  auth {command_name}: GitHub App credential bundle valid"),
        Err(error) => {
            status.fail();
            println!("  auth {command_name}: GitHub App credential bundle invalid");
            println!("  reason: {error}");
            print_human_setup(&[
                "Edit the configured 1Password metadata field for this GitHub App credential bundle.",
                "The metadata field must contain valid JSON with `type`, numeric `app_id`, and `installation_id`.",
                "The private key should be a separate 1Password file attachment referenced by the `private_key` auth setting.",
                "If using the legacy single-field bundle, replace raw PEM line breaks with escaped `\\n` newlines inside `private_key`.",
                "Do not paste the real private key into an online validator.",
                &format!(
                    "Rerun `via config doctor {service_name}` after updating the 1Password field."
                ),
            ]);
            print_agent_guidance(
                "Ask the user to fix the GitHub App credential bundle in 1Password; do not ask for the private key value.",
            );
        }
    }

    Ok(())
}

fn resolve_github_app_doctor_secrets(
    service_name: &str,
    service: &ServiceConfig,
    provider: &dyn crate::providers::SecretProvider,
    auth: &AuthConfig,
) -> Result<(crate::secrets::SecretValue, Option<String>), ViaError> {
    let AuthConfig::GitHubApp {
        secret,
        credential,
        private_key,
    } = auth
    else {
        unreachable!("caller only passes github_app auth");
    };

    match (secret, credential, private_key) {
        (Some(secret), None, None) => {
            let credential = resolve_doctor_secret(service_name, service, provider, secret)?;
            Ok((credential, None))
        }
        (None, Some(credential), Some(private_key)) => {
            let credential = resolve_doctor_secret(service_name, service, provider, credential)?;
            let private_key = resolve_doctor_secret(service_name, service, provider, private_key)?;
            Ok((credential, Some(private_key.expose().to_owned())))
        }
        _ => Err(ViaError::InvalidConfig(
            "github_app auth must set either `secret` or both `credential` and `private_key`"
                .to_owned(),
        )),
    }
}

fn resolve_doctor_secret(
    service_name: &str,
    service: &ServiceConfig,
    provider: &dyn crate::providers::SecretProvider,
    secret: &str,
) -> Result<crate::secrets::SecretValue, ViaError> {
    let reference = service
        .secrets
        .get(secret)
        .ok_or_else(|| ViaError::UnknownSecret {
            service: service_name.to_owned(),
            secret: secret.to_owned(),
        })?;
    provider.resolve(reference)
}

#[derive(Default)]
struct DoctorStatus {
    failed: bool,
}

impl DoctorStatus {
    fn fail(&mut self) {
        self.failed = true;
    }

    fn into_result(self) -> Result<(), ViaError> {
        if self.failed {
            Err(ViaError::DoctorFailed)
        } else {
            Ok(())
        }
    }
}

fn check_program(program: &str, check: &[String]) -> Result<(), ViaError> {
    let args = if check.is_empty() {
        vec!["--version".to_owned()]
    } else {
        check.to_owned()
    };

    run_command(program, &args).map(|_| ())
}

fn check_providers(config: &Config, status: &mut DoctorStatus) -> BTreeMap<String, bool> {
    let mut ready = BTreeMap::new();

    for (provider_name, provider) in &config.providers {
        let provider_ready = match provider {
            ProviderConfig::OnePassword {
                account,
                cache,
                cache_ttl_seconds,
            } => check_onepassword_provider(
                provider_name,
                account.as_deref(),
                *cache,
                *cache_ttl_seconds,
                status,
            ),
        };
        ready.insert(provider_name.clone(), provider_ready);
    }

    ready
}

fn check_onepassword_provider(
    provider_name: &str,
    account: Option<&str>,
    cache: OnePasswordCacheMode,
    cache_ttl_seconds: u64,
    status: &mut DoctorStatus,
) -> bool {
    println!("provider {provider_name} (1Password): checking");
    print_onepassword_cache(cache, cache_ttl_seconds);

    if !check_onepassword_cli_installed(status) {
        return false;
    }
    if !check_onepassword_account(account, status) {
        return false;
    }

    check_onepassword_authentication(account, status)
}

fn print_onepassword_cache(cache: OnePasswordCacheMode, cache_ttl_seconds: u64) {
    match cache {
        OnePasswordCacheMode::Daemon => {
            println!("  cache: daemon enabled (ttl {cache_ttl_seconds}s)")
        }
        OnePasswordCacheMode::Off => println!("  cache: off"),
    }
}

fn check_onepassword_cli_installed(status: &mut DoctorStatus) -> bool {
    match run_command("op", &["--version".to_owned()]) {
        Ok(output) => {
            print_onepassword_version(&output.stdout);
            true
        }
        Err(error) => {
            status.fail();
            print_onepassword_cli_failure(&error);
            false
        }
    }
}

fn print_onepassword_version(version: &str) {
    if version.is_empty() {
        println!("  1Password CLI: installed");
    } else {
        println!("  1Password CLI: installed ({version})");
    }
}

fn print_onepassword_cli_failure(error: &ViaError) {
    println!("  1Password CLI: not ready");
    print_error_hint(error);
    print_human_setup(&[
        "Install the 1Password CLI.",
        "macOS/Homebrew: `brew install --cask 1password-cli`.",
        "Windows/winget: `winget install -e --id AgileBits.1Password.CLI`.",
        "Linux: follow the official APT/YUM/Alpine/Nix/manual steps at https://developer.1password.com/docs/cli/get-started/.",
        "Verify the CLI is available with `op --version`.",
        "Install the 1Password desktop app if it is not already installed.",
        "Open and unlock the 1Password desktop app.",
        "Enable the 1Password CLI integration in the desktop app: Settings > Developer > Integrate with 1Password CLI.",
        "Rerun `via config doctor` after setup.",
    ]);
    print_agent_guidance(
        "Ask the user to install the secret provider, run `via login`, then rerun `via config doctor`.",
    );
}

fn check_onepassword_account(account: Option<&str>, status: &mut DoctorStatus) -> bool {
    let Some(account) = account else {
        return true;
    };

    let args = vec!["account".to_owned(), "get".to_owned(), account.to_owned()];
    match run_command("op", &args) {
        Ok(_) => {
            println!("  account {account}: configured");
            true
        }
        Err(error) => {
            status.fail();
            print_onepassword_account_failure(account, &error);
            false
        }
    }
}

fn print_onepassword_account_failure(account: &str, error: &ViaError) {
    println!("  account {account}: not ready");
    print_error_hint(error);
    print_human_setup(&[
        "Add this 1Password account to the desktop app or CLI.",
        "Confirm the provider account in `via.toml` matches a configured account ID or sign-in address.",
        "Rerun `via config doctor` after the account is available.",
    ]);
    print_agent_guidance(
        "Ask the user to fix the configured 1Password account, then rerun `via config doctor`.",
    );
}

fn check_onepassword_authentication(account: Option<&str>, status: &mut DoctorStatus) -> bool {
    let mut args = vec!["whoami".to_owned()];
    if let Some(account) = account {
        args.push("--account".to_owned());
        args.push(account.to_owned());
    }

    match run_command("op", &args) {
        Ok(_) => {
            println!("  authentication: ready");
            true
        }
        Err(error) => {
            status.fail();
            print_onepassword_auth_failure(&error);
            false
        }
    }
}

fn print_onepassword_auth_failure(error: &ViaError) {
    println!("  authentication: not ready");
    print_error_hint(error);
    print_onepassword_auth_setup(error);
    print_agent_guidance("Ask the user to run `via login`, then rerun `via config doctor`.");
}

fn print_onepassword_auth_setup(error: &ViaError) {
    if is_onepassword_not_signed_in(error) {
        print_onepassword_signed_out_setup();
    } else if is_onepassword_account_missing(error) {
        print_onepassword_missing_account_setup();
    } else {
        print_onepassword_desktop_setup();
    }
}

fn print_onepassword_signed_out_setup() {
    print_human_setup(&[
        "The 1Password CLI can see an account, but it is not signed in.",
        "Run `via login` from your terminal and choose the account that contains the configured vault.",
        "Approve the sign-in from the 1Password desktop app if prompted.",
        "Run `via config doctor` to confirm the CLI session is active.",
        "If multiple accounts are visible, set `[providers.onepassword] account = \"<account-id-or-sign-in-address>\"` in the via config.",
        "Rerun `via login` after pinning the account if needed.",
    ]);
}

fn print_onepassword_missing_account_setup() {
    print_human_setup(&[
        "The 1Password CLI is installed, but it cannot find a signed-in account.",
        "Open the 1Password desktop app and confirm the account containing the configured vault is added and unlocked.",
        "Enable the 1Password CLI integration in the desktop app: Settings > Developer > Integrate with 1Password CLI.",
        "Run `op account list` in your terminal to confirm the account is visible to the CLI.",
        "Run `via login` after the account is visible.",
        "If multiple accounts are visible, set `[providers.onepassword] account = \"<account-id-or-sign-in-address>\"` in the via config.",
        "Rerun `via config doctor` after authentication succeeds.",
    ]);
}

fn print_onepassword_desktop_setup() {
    print_human_setup(&[
        "Install the 1Password desktop app if it is not already installed.",
        "macOS/Homebrew: `brew install --cask 1password`.",
        "Windows/winget: `winget install -e --id AgileBits.1Password`.",
        "Linux: follow the official desktop app install steps at https://support.1password.com/install-linux/.",
        "Add your 1Password account to the desktop app.",
        "Open and unlock the 1Password desktop app.",
        "Enable the 1Password CLI integration in the desktop app: Settings > Developer > Integrate with 1Password CLI.",
        "Run `via login` from your terminal.",
        "Rerun `via config doctor` after authentication succeeds.",
    ]);
}

struct CommandOutput {
    stdout: String,
}

fn run_command(program: &str, args: &[String]) -> Result<CommandOutput, ViaError> {
    let output = Command::new(program).args(args).output();
    match output {
        Ok(output) if output.status.success() => Ok(CommandOutput {
            stdout: String::from_utf8_lossy(&output.stdout).trim().to_owned(),
        }),
        Ok(output) => Err(ViaError::ExternalCommandFailed {
            program: program.to_owned(),
            status: output.status.code(),
            stderr: String::from_utf8_lossy(&output.stderr).trim().to_owned(),
        }),
        Err(source) => Err(ViaError::MissingProgram {
            program: program.to_owned(),
            source,
        }),
    }
}

fn print_secret_failure(service_name: &str, secret_name: &str, error: &ViaError) {
    println!("  secret {secret_name}: not readable by via");
    print_secret_error_hint(error);
    print_human_setup(&[
        &format!(
            "Confirm the configured 1Password reference for `{service_name}.{secret_name}` exists."
        ),
        "Confirm your signed-in account has permission to read it.",
        "Update `via.toml` with the correct secret reference if needed.",
        &format!("Rerun `via config doctor {service_name}` after fixing the secret."),
    ]);
    print_agent_guidance(
        "Do not ask for the token value. Ask the user to fix the configured secret reference or 1Password permissions.",
    );
}

fn print_secret_error_hint(error: &ViaError) {
    match error {
        ViaError::MissingProgram { .. } => {
            println!("  reason: secret provider command was not found on PATH");
        }
        ViaError::ExternalCommandFailed { status, .. } => {
            println!("  reason: secret provider could not read the configured reference; status {status:?}");
        }
        _ => println!("  reason: secret provider could not read the configured reference"),
    }
}

fn print_delegated_failure(command_name: &str, program: &str, error: &ViaError) {
    println!("  capability {command_name}: delegated {program} not ready");
    print_error_hint(error);
    print_human_setup(&[
        &format!("Install `{program}` or make sure it is available on PATH."),
        "Run `via config doctor` again after the delegated tool is available.",
    ]);
    print_agent_guidance(
        "Ask the user to install or fix the delegated tool, then rerun `via config doctor`.",
    );
}

fn print_error_hint(error: &ViaError) {
    match error {
        ViaError::MissingProgram { program, .. } => {
            println!("  reason: `{program}` was not found on PATH");
        }
        ViaError::ExternalCommandFailed { status, stderr, .. } => {
            println!("  reason: command exited with status {status:?}");
            if !stderr.is_empty() {
                println!("  detail: {stderr}");
            }
        }
        _ => println!("  reason: {error}"),
    }
}

fn is_onepassword_account_missing(error: &ViaError) -> bool {
    matches!(
        error,
        ViaError::ExternalCommandFailed { stderr, .. }
            if stderr.contains("no account found for filter")
    )
}

fn is_onepassword_not_signed_in(error: &ViaError) -> bool {
    matches!(
        error,
        ViaError::ExternalCommandFailed { stderr, .. }
            if stderr.contains("account is not signed in")
    )
}

fn print_human_setup(steps: &[&str]) {
    println!("  Human setup:");
    for step in steps {
        println!("    - {step}");
    }
}

fn print_agent_guidance(message: &str) {
    println!("  Agent guidance:");
    println!("    - {message}");
}

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

    fn config() -> Config {
        Config::from_toml_str(
            r#"
version = 1

[providers.onepassword]
type = "1password"

[services.github]
provider = "onepassword"

[services.github.secrets]
token = "op://Private/GitHub/token"

[services.github.commands.api]
mode = "rest"
base_url = "https://api.github.com"
"#,
        )
        .unwrap()
    }

    #[test]
    fn rejects_unknown_service_before_provider_checks() {
        let error = run(&config(), Some("missing")).unwrap_err();

        assert!(matches!(error, ViaError::UnknownService(service) if service == "missing"));
    }

    #[test]
    fn check_program_accepts_successful_check_command() {
        check_program("sh", &["-c".to_owned(), "exit 0".to_owned()]).unwrap();
    }

    #[test]
    fn check_program_reports_failed_check_command() {
        let error = check_program("sh", &["-c".to_owned(), "exit 9".to_owned()]).unwrap_err();

        assert!(matches!(
            error,
            ViaError::ExternalCommandFailed {
                program,
                status: Some(9),
                ..
            } if program == "sh"
        ));
    }

    #[test]
    fn run_command_captures_stdout_without_newline() {
        let output = run_command("sh", &["-c".to_owned(), "printf 'ready\\n'".to_owned()]).unwrap();

        assert_eq!(output.stdout, "ready");
    }

    #[test]
    fn detects_onepassword_missing_account_error() {
        let error = ViaError::ExternalCommandFailed {
            program: "op".to_owned(),
            status: Some(1),
            stderr: "[ERROR] no account found for filter".to_owned(),
        };

        assert!(is_onepassword_account_missing(&error));
    }

    #[test]
    fn detects_onepassword_signed_out_error() {
        let error = ViaError::ExternalCommandFailed {
            program: "op".to_owned(),
            status: Some(1),
            stderr: "[ERROR] account is not signed in".to_owned(),
        };

        assert!(is_onepassword_not_signed_in(&error));
    }
}