Skip to content
This repository was archived by the owner on Oct 12, 2023. It is now read-only.

Commit 879f5fc

Browse files
authored
Merge pull request #12 from keithduncan/add-env-var-support
Add support for reading SSH_AUTH_SOCK environment variable
2 parents bab7984 + b89bf0c commit 879f5fc

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

agent/src/main.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{fs, path::Path};
1+
use std::{env, fs, path::Path};
22
use ssh_agent::agent::Agent;
33
use clap::{App, Arg, SubCommand};
44
use url::Url;
@@ -23,19 +23,18 @@ fn main() {
2323
.subcommand(SubCommand::with_name("list-keys")
2424
.about("List all keys for the caller's IAM identity."))
2525
.subcommand(SubCommand::with_name("daemon")
26-
.about("Run the daemon, bind a UNIX domain socket to provide ssh list and sign operations.")
26+
.about("Run the daemon, bind a UNIX domain socket to provide ssh list and sign operations. Defaults to SSH_AUTH_SOCK if unspecified.")
2727
.arg(Arg::with_name("bind-to")
2828
.long("bind-to")
2929
.short("b")
3030
.takes_value(true)
31-
.value_name("PATH")
32-
.required(true)))
31+
.value_name("PATH")))
3332
.setting(clap::AppSettings::SubcommandRequired)
3433
.get_matches();
3534

3635
// Uses an environment variable rather than an argument so that this can be
3736
// an ECS ValueFrom in an ECS task.
38-
let ssh_agent_backend_url = Url::parse(&std::env::var("IAM_SSH_AGENT_BACKEND_URL").expect("IAM_SSH_AGENT_BACKEND_URL is required")).expect("IAM_SSH_AGENT_BACKEND_URL is a valid url");
37+
let ssh_agent_backend_url = Url::parse(&env::var("IAM_SSH_AGENT_BACKEND_URL").expect("IAM_SSH_AGENT_BACKEND_URL is required")).expect("IAM_SSH_AGENT_BACKEND_URL is a valid url");
3938
let agent = agent::Backend::new(ssh_agent_backend_url);
4039

4140
if let Some(_matches) = matches.subcommand_matches("list-keys") {
@@ -46,8 +45,10 @@ fn main() {
4645
if let Some(matches) = matches.subcommand_matches("daemon") {
4746
// TODO support exec mode and export SSH_AUTH_SOCK
4847

49-
let pipe = matches.value_of("bind-to").expect("bind-to is required");
50-
let pipe = Path::new(pipe);
48+
// Support command line for testing and an environment variable for
49+
// systemd units.
50+
let pipe: String = matches.value_of("bind-to").map(str::to_string).or(env::var("SSH_AUTH_SOCK").ok()).expect("bind-to is required");
51+
let pipe = Path::new(&pipe);
5152

5253
if fs::metadata(&pipe).is_ok() {
5354
if let Ok(_) = fs::remove_file(&pipe){

0 commit comments

Comments
 (0)