forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh_keys.go
More file actions
119 lines (102 loc) · 2.68 KB
/
ssh_keys.go
File metadata and controls
119 lines (102 loc) · 2.68 KB
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
package shared
import (
"fmt"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/AlecAivazis/survey/v2"
"github.com/cli/cli/internal/config"
"github.com/cli/cli/internal/run"
"github.com/cli/cli/pkg/cmd/ssh-key/add"
"github.com/cli/cli/pkg/prompt"
"github.com/cli/safeexec"
)
type sshContext struct {
configDir string
keygenExe string
}
func (c *sshContext) 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 *sshContext) localPublicKeys() ([]string, error) {
sshDir, err := c.sshDir()
if err != nil {
return nil, err
}
return filepath.Glob(filepath.Join(sshDir, "*.pub"))
}
func (c *sshContext) 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
}
func (c *sshContext) generateSSHKey() (string, error) {
keygenExe, err := c.findKeygen()
if err != nil {
// give up silently if `ssh-keygen` is not available
return "", nil
}
var sshChoice bool
err = prompt.SurveyAskOne(&survey.Confirm{
Message: "Generate a new SSH key to add to your GitHub account?",
Default: true,
}, &sshChoice)
if err != nil {
return "", fmt.Errorf("could not prompt: %w", err)
}
if !sshChoice {
return "", nil
}
sshDir, err := c.sshDir()
if err != nil {
return "", err
}
keyFile := filepath.Join(sshDir, "id_ed25519")
if _, err := os.Stat(keyFile); err == nil {
return "", fmt.Errorf("refusing to overwrite file %s", keyFile)
}
if err := os.MkdirAll(filepath.Dir(keyFile), 0711); err != nil {
return "", err
}
var sshLabel string
var sshPassphrase string
err = prompt.SurveyAskOne(&survey.Password{
Message: "Enter a passphrase for your new SSH key (Optional)",
}, &sshPassphrase)
if err != nil {
return "", fmt.Errorf("could not prompt: %w", err)
}
keygenCmd := exec.Command(keygenExe, "-t", "ed25519", "-C", sshLabel, "-N", sshPassphrase, "-f", keyFile)
return keyFile + ".pub", run.PrepareCmd(keygenCmd).Run()
}
func sshKeyUpload(httpClient *http.Client, hostname, keyFile string) error {
f, err := os.Open(keyFile)
if err != nil {
return err
}
defer f.Close()
return add.SSHKeyUpload(httpClient, hostname, f, "GitHub CLI")
}