Skip to content

Commit 878af5b

Browse files
shatilbobveznat
authored andcommitted
User-selectable folder for SSH keys ("identity files")
Introduces `--ssh-dir` flag to `get` and `request` subcommands so user may specify in what directory their identity files (also called SSH private keys, like `id_rsa`, `id_dsa`, `id_ecdsa`, etc.) are located. Default folder remains `~/.ssh`. Signed-off-by: Bob Van Zant <bvanzant@brkt.com>
1 parent 5e7d0e9 commit 878af5b

2 files changed

Lines changed: 22 additions & 10 deletions

File tree

get_cert.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

4247
func 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)

request_cert.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,18 @@ func requestCertFlags() []cli.Flag {
7171
Name: "add-key",
7272
Usage: "When set automatically call ssh-add if cert was auto-signed by server",
7373
},
74+
cli.StringFlag{
75+
Name: "ssh-dir",
76+
Value: os.Getenv("HOME") + "/.ssh",
77+
Usage: "Directory where SSH identity files (like 'id_rsa') reside",
78+
},
7479
}
7580
}
7681

7782
func requestCert(c *cli.Context) error {
7883
allConfig := make(map[string]ssh_ca_util.RequesterConfig)
7984
configPath := c.String("config-file")
85+
sshDir := c.String("ssh-dir")
8086
err := ssh_ca_util.LoadConfig(configPath, &allConfig)
8187
if err != nil {
8288
return cli.NewExitError(fmt.Sprintf("Load Config failed: %s", err), 1)
@@ -171,11 +177,11 @@ func requestCert(c *cli.Context) error {
171177
}
172178
fmt.Printf("Cert request id: %s%s\n", requestID, appendage)
173179
if signed && c.BoolT("add-key") {
174-
cert, err := downloadCert(config, requestID)
180+
cert, err := downloadCert(config, requestID, sshDir)
175181
if err != nil {
176182
return cli.NewExitError(fmt.Sprintf("%s", err), 1)
177183
}
178-
err = addCertToAgent(cert)
184+
err = addCertToAgent(cert, sshDir)
179185
if err != nil {
180186
return cli.NewExitError(fmt.Sprintf("%s", err), 1)
181187
}

0 commit comments

Comments
 (0)