-
Notifications
You must be signed in to change notification settings - Fork 321
/
keys.go
238 lines (193 loc) · 5.21 KB
/
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package ssh
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"os"
"path/filepath"
"sync"
"github.com/loft-sh/devpod/pkg/provider"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"golang.org/x/crypto/ssh"
)
var (
DevPodSSHHostKeyFile = "id_devpod_rsa_host"
DevPodSSHPrivateKeyFile = "id_devpod_rsa"
DevPodSSHPublicKeyFile = "id_devpod_rsa.pub"
)
var keyLock sync.Mutex
func rsaKeyGen() (privateKey string, publicKey string, err error) {
privateKeyRaw, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return "", "", errors.Errorf("generate private key: %v", err)
}
return generateKeys(pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(privateKeyRaw),
}, privateKeyRaw)
}
func generateKeys(block pem.Block, cp crypto.Signer) (privateKey string, publicKey string, err error) {
pkBytes := pem.EncodeToMemory(&block)
privateKey = string(pkBytes)
publicKeyRaw := cp.Public()
p, err := ssh.NewPublicKey(publicKeyRaw)
if err != nil {
return "", "", err
}
publicKey = string(ssh.MarshalAuthorizedKey(p))
return privateKey, publicKey, nil
}
func makeHostKey() (string, error) {
privKey, _, err := rsaKeyGen()
if err != nil {
return "", err
}
return privKey, err
}
func makeSSHKeyPair() (string, string, error) {
privKey, pubKey, err := rsaKeyGen()
if err != nil {
return "", "", err
}
return pubKey, privKey, err
}
func GetPrivateKeyRaw(context, workspaceID string) ([]byte, error) {
workspaceDir, err := provider.GetWorkspaceDir(context, workspaceID)
if err != nil {
return nil, err
}
return GetPrivateKeyRawBase(workspaceDir)
}
func GetDevPodKeysDir() string {
dir, err := homedir.Dir()
if err == nil {
tempDir := filepath.Join(dir, ".devpod", "keys")
err = os.MkdirAll(tempDir, 0755)
if err == nil {
return tempDir
}
}
tempDir := os.TempDir()
return filepath.Join(tempDir, "devpod-ssh")
}
func GetDevPodHostKey() (string, error) {
tempDir := GetDevPodKeysDir()
return GetHostKeyBase(tempDir)
}
func GetDevPodPublicKey() (string, error) {
tempDir := GetDevPodKeysDir()
return GetPublicKeyBase(tempDir)
}
func GetDevPodPrivateKeyRaw() ([]byte, error) {
tempDir := GetDevPodKeysDir()
return GetPrivateKeyRawBase(tempDir)
}
func GetHostKey(context, workspaceID string) (string, error) {
workspaceDir, err := provider.GetWorkspaceDir(context, workspaceID)
if err != nil {
return "", err
}
return GetHostKeyBase(workspaceDir)
}
func GetPrivateKeyRawBase(dir string) ([]byte, error) {
keyLock.Lock()
defer keyLock.Unlock()
err := os.MkdirAll(dir, 0755)
if err != nil {
return nil, err
}
// check if key pair exists
privateKeyFile := filepath.Join(dir, DevPodSSHPrivateKeyFile)
publicKeyFile := filepath.Join(dir, DevPodSSHPublicKeyFile)
_, err = os.Stat(privateKeyFile)
if err != nil {
pubKey, privateKey, err := makeSSHKeyPair()
if err != nil {
return nil, errors.Wrap(err, "generate key pair")
}
err = os.WriteFile(publicKeyFile, []byte(pubKey), 0644)
if err != nil {
return nil, errors.Wrap(err, "write public ssh key")
}
err = os.WriteFile(privateKeyFile, []byte(privateKey), 0600)
if err != nil {
return nil, errors.Wrap(err, "write private ssh key")
}
}
// read private key
out, err := os.ReadFile(privateKeyFile)
if err != nil {
return nil, errors.Wrap(err, "read private ssh key")
}
return out, nil
}
func GetHostKeyBase(dir string) (string, error) {
keyLock.Lock()
defer keyLock.Unlock()
err := os.MkdirAll(dir, 0755)
if err != nil {
return "", err
}
// check if key pair exists
hostKeyFile := filepath.Join(dir, DevPodSSHHostKeyFile)
_, err = os.Stat(hostKeyFile)
if err != nil {
privateKey, err := makeHostKey()
if err != nil {
return "", errors.Wrap(err, "generate host key")
}
err = os.WriteFile(hostKeyFile, []byte(privateKey), 0600)
if err != nil {
return "", errors.Wrap(err, "write host key")
}
}
// read public key
out, err := os.ReadFile(hostKeyFile)
if err != nil {
return "", errors.Wrap(err, "read host ssh key")
}
return base64.StdEncoding.EncodeToString(out), nil
}
func GetPublicKeyBase(dir string) (string, error) {
keyLock.Lock()
defer keyLock.Unlock()
err := os.MkdirAll(dir, 0755)
if err != nil {
return "", err
}
// check if key pair exists
privateKeyFile := filepath.Join(dir, DevPodSSHPrivateKeyFile)
publicKeyFile := filepath.Join(dir, DevPodSSHPublicKeyFile)
_, err = os.Stat(privateKeyFile)
if err != nil {
pubKey, privateKey, err := makeSSHKeyPair()
if err != nil {
return "", errors.Wrap(err, "generate key pair")
}
err = os.WriteFile(publicKeyFile, []byte(pubKey), 0644)
if err != nil {
return "", errors.Wrap(err, "write public ssh key")
}
err = os.WriteFile(privateKeyFile, []byte(privateKey), 0600)
if err != nil {
return "", errors.Wrap(err, "write private ssh key")
}
}
// read public key
out, err := os.ReadFile(publicKeyFile)
if err != nil {
return "", errors.Wrap(err, "read public ssh key")
}
return base64.StdEncoding.EncodeToString(out), nil
}
func GetPublicKey(context, workspaceID string) (string, error) {
workspaceDir, err := provider.GetWorkspaceDir(context, workspaceID)
if err != nil {
return "", err
}
return GetPublicKeyBase(workspaceDir)
}