-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathssh_keys.go
122 lines (101 loc) · 2.63 KB
/
ssh_keys.go
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
package ssh
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/cli/cli/v2/internal/config"
"github.com/cli/cli/v2/internal/run"
"github.com/cli/safeexec"
)
type Context struct {
configDir string
keygenExe string
}
// NewContextForTests creates a new `ssh.Context` with internal properties set to the
// specified values. It should only be used to inject test-specific setup.
func NewContextForTests(configDir, keygenExe string) Context {
return Context{
configDir,
keygenExe,
}
}
type KeyPair struct {
PublicKeyPath string
PrivateKeyPath string
}
var ErrKeyAlreadyExists = errors.New("SSH key already exists")
func (c *Context) LocalPublicKeys() ([]string, error) {
sshDir, err := c.SshDir()
if err != nil {
return nil, err
}
return filepath.Glob(filepath.Join(sshDir, "*.pub"))
}
func (c *Context) HasKeygen() bool {
_, err := c.findKeygen()
return err == nil
}
func (c *Context) GenerateSSHKey(keyName string, passphrase string) (*KeyPair, error) {
keygenExe, err := c.findKeygen()
if err != nil {
return nil, err
}
sshDir, err := c.SshDir()
if err != nil {
return nil, err
}
err = os.MkdirAll(sshDir, 0700)
if err != nil {
return nil, fmt.Errorf("could not create .ssh directory: %w", err)
}
keyFile := filepath.Join(sshDir, keyName)
keyPair := KeyPair{
PublicKeyPath: keyFile + ".pub",
PrivateKeyPath: keyFile,
}
if _, err := os.Stat(keyFile); err == nil {
// Still return keyPair because the caller might be OK with this - they can check the error with errors.Is(err, ErrKeyAlreadyExists)
return &keyPair, ErrKeyAlreadyExists
}
if err := os.MkdirAll(filepath.Dir(keyFile), 0711); err != nil {
return nil, err
}
keygenCmd := exec.Command(keygenExe, "-t", "ed25519", "-C", "", "-N", passphrase, "-f", keyFile)
err = run.PrepareCmd(keygenCmd).Run()
if err != nil {
return nil, err
}
return &keyPair, nil
}
func (c *Context) SshDir() (string, error) {
if c.configDir != "" {
return c.configDir, nil
}
dir, err := config.HomeDirPath(".ssh")
if err == nil {
c.configDir = dir
}
return dir, err
}
func (c *Context) findKeygen() (string, error) {
if c.keygenExe != "" {
return c.keygenExe, nil
}
keygenExe, err := safeexec.LookPath("ssh-keygen")
if err != nil && runtime.GOOS == "windows" {
// We can try and find ssh-keygen in a Git for Windows install
if gitPath, err := safeexec.LookPath("git"); err == nil {
gitKeygen := filepath.Join(filepath.Dir(gitPath), "..", "usr", "bin", "ssh-keygen.exe")
if _, err = os.Stat(gitKeygen); err == nil {
return gitKeygen, nil
}
}
}
if err == nil {
c.keygenExe = keygenExe
}
return keygenExe, err
}