@@ -36,13 +36,19 @@ func getCertFlags() []cli.Flag {
3636 Name : "add-key" ,
3737 Usage : "When set automatically call ssh-add" ,
3838 },
39+ cli.StringFlag {
40+ Name : "ssh-dir" ,
41+ Value : os .Getenv ("HOME" ) + "/.ssh" ,
42+ Usage : "Directory where SSH identity files (like 'id_rsa') reside" ,
43+ },
3944 }
4045}
4146
4247func getCert (c * cli.Context ) error {
4348
4449 configPath := c .String ("config-file" )
4550 environment := c .String ("environment" )
51+ sshDir := c .String ("ssh-dir" )
4652 certRequestID := c .Args ().First ()
4753
4854 allConfig := make (map [string ]ssh_ca_util.RequesterConfig )
@@ -55,25 +61,25 @@ func getCert(c *cli.Context) error {
5561 return cli .NewExitError (fmt .Sprintf ("%s" , err ), 1 )
5662 }
5763 config := wrongTypeConfig .(ssh_ca_util.RequesterConfig )
58- cert , err := downloadCert (config , certRequestID )
64+ cert , err := downloadCert (config , certRequestID , sshDir )
5965 if err != nil {
6066 return cli .NewExitError (fmt .Sprintf ("%s" , err ), 1 )
6167 }
6268 if c .BoolT ("add-key" ) {
63- err = addCertToAgent (cert )
69+ err = addCertToAgent (cert , sshDir )
6470 if err != nil {
6571 return cli .NewExitError (fmt .Sprintf ("%s" , err ), 1 )
6672 }
6773 }
6874 return nil
6975}
7076
71- func addCertToAgent (cert * ssh.Certificate ) error {
77+ func addCertToAgent (cert * ssh.Certificate , sshDir string ) error {
7278 secondsRemaining := int64 (cert .ValidBefore ) - int64 (time .Now ().Unix ())
7379 if secondsRemaining < 1 {
7480 return fmt .Errorf ("This certificate has already expired." )
7581 }
76- pubKeyPath , err := findKeyLocally (cert .Key )
82+ pubKeyPath , err := findKeyLocally (cert .Key , sshDir )
7783 privKeyPath := strings .Replace (pubKeyPath , ".pub" , "" , 1 )
7884 fmt .Printf ("pubkey %s, privkey %s\n " , pubKeyPath , privKeyPath )
7985 cmd := exec .Command ("ssh-add" , "-t" , fmt .Sprintf ("%d" , secondsRemaining ), privKeyPath )
@@ -87,7 +93,7 @@ func addCertToAgent(cert *ssh.Certificate) error {
8793 return nil
8894}
8995
90- func downloadCert (config ssh_ca_util.RequesterConfig , certRequestID string ) (* ssh.Certificate , error ) {
96+ func downloadCert (config ssh_ca_util.RequesterConfig , certRequestID string , sshDir string ) (* ssh.Certificate , error ) {
9197 getResp , err := http .Get (config .SignerUrl + "cert/requests/" + certRequestID )
9298 if err != nil {
9399 return nil , fmt .Errorf ("Didn't get a valid response: %s" , err )
@@ -107,7 +113,7 @@ func downloadCert(config ssh_ca_util.RequesterConfig, certRequestID string) (*ss
107113 }
108114 cert := pubKey .(* ssh.Certificate )
109115
110- pubKeyPath , err := findKeyLocally (cert .Key )
116+ pubKeyPath , err := findKeyLocally (cert .Key , sshDir )
111117
112118 if err != nil {
113119 return nil , err
@@ -121,8 +127,8 @@ func downloadCert(config ssh_ca_util.RequesterConfig, certRequestID string) (*ss
121127 ssh_ca_util .PrintForInspection (* cert )
122128 return cert , nil
123129}
124- func findKeyLocally ( key ssh. PublicKey ) ( string , error ) {
125- sshDir := os . Getenv ( "HOME" ) + "/.ssh"
130+
131+ func findKeyLocally ( key ssh. PublicKey , sshDir string ) ( string , error ) {
126132 dirEntries , err := ioutil .ReadDir (sshDir )
127133 if err != nil {
128134 return "" , fmt .Errorf ("Could not read your .ssh directory %s: %s\n " , sshDir , err )
0 commit comments