-
Notifications
You must be signed in to change notification settings - Fork 64
/
charm.go
436 lines (383 loc) Β· 9.61 KB
/
charm.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package charm
import (
"crypto/rsa"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"path/filepath"
"regexp"
"sync"
"time"
"github.com/meowgorithm/babyenv"
"github.com/mitchellh/go-homedir"
gap "github.com/muesli/go-app-paths"
"github.com/muesli/sasquatch"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
var nameValidator = regexp.MustCompile("^[a-zA-Z0-9]{1,50}$")
// ErrMissingSSHAuth is used when the user is missing SSH credentials.
var ErrMissingSSHAuth = errors.New("missing ssh auth")
// ErrNameTaken is used when a user attempts to set a username and that
// username is already taken.
var ErrNameTaken = errors.New("name already taken")
// ErrNameInvalid is used when a username is invalid.
var ErrNameInvalid = errors.New("invalid name")
// ErrCouldNotUnlinkKey is used when a key can't be deleted.
var ErrCouldNotUnlinkKey = errors.New("could not unlink key")
// ErrAuthFailed indicates an authentication failure. The underlying error is
// wrapped.
type ErrAuthFailed struct {
Err error
}
// Error returns the boxed error string.
func (e ErrAuthFailed) Error() string { return e.Err.Error() }
// Unwrap returns the boxed error.
func (e ErrAuthFailed) Unwrap() error { return e.Err }
// Config contains the Charm client configuration.
type Config struct {
IDHost string `env:"CHARM_ID_HOST" default:"id.charm.sh"`
IDPort int `env:"CHARM_ID_PORT" default:"22"`
BioHost string `env:"CHARM_BIO_HOST" default:"https://bio.charm.sh"`
BioPort int `env:"CHARM_BIO_PORT" default:"443"`
GlowHost string `env:"CHARM_GLOW_HOST" default:"https://glow.charm.sh"`
GlowPort int `env:"CHARM_GLOW_PORT" default:"443"`
JWTKey string `env:"CHARM_JWT_KEY" default:""`
Debug bool `env:"CHARM_DEBUG" default:"false"`
Logfile string `env:"CHARM_LOGFILE" default:""`
}
// Client is the Charm client.
type Client struct {
config *Config
auth *Auth
authLock *sync.Mutex
sshConfig *ssh.ClientConfig
jwtPublicKey *rsa.PublicKey
plainTextEncryptKeys []*EncryptKey
authKeyPaths []string
encryptKeyLock *sync.Mutex
}
// User represents a Charm user.
type User struct {
CharmID string `json:"charm_id"`
Name string `json:"name"`
Email string `json:"email"`
Bio string `json:"bio"`
CreatedAt time.Time `json:"created_at"`
}
// Keys is a server response returned when the user queries for the keys linked
// to her account.
type Keys struct {
ActiveKey int `json:"active_key"`
Keys []Key `json:"keys"`
}
// DataPath returns the Charm data path for the current user. This is where
// Charm keys are stored.
func DataPath() (string, error) {
scope := gap.NewScope(gap.User, "charm")
dataPath, err := scope.DataPath("")
if err != nil {
return "", err
}
return dataPath, nil
}
// ConfigFromEnv loads the configuration from the environment.
func ConfigFromEnv() (*Config, error) {
var cfg Config
if err := babyenv.Parse(&cfg); err != nil {
return nil, err
}
return &cfg, nil
}
// NewClient creates a new Charm client.
func NewClient(cfg *Config) (*Client, error) {
cc := &Client{
config: cfg,
auth: &Auth{},
authLock: &sync.Mutex{},
encryptKeyLock: &sync.Mutex{},
}
jk, err := jwtKey(cfg.JWTKey)
if err != nil {
return nil, err
}
cc.jwtPublicKey = jk
sshKeys, err := findAuthKeys()
if err != nil {
return nil, err
}
if len(sshKeys) == 0 { // We didn't find any keys; give up
return nil, ErrMissingSSHAuth
}
// Try and use the keys we found
var pkam ssh.AuthMethod
for i := 0; i < len(sshKeys); i++ {
pkam, err = publicKeyAuthMethod(sshKeys[i])
if err != nil && i == len(sshKeys)-1 {
return nil, ErrMissingSSHAuth
}
}
cc.authKeyPaths = sshKeys
cc.sshConfig = &ssh.ClientConfig{
User: "charm",
Auth: []ssh.AuthMethod{pkam},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
return cc, nil
}
// JWT returns a JSON web token for the user.
func (cc *Client) JWT() (string, error) {
s, err := cc.sshSession()
if err != nil {
return "", err
}
defer s.Close()
jwt, err := s.Output("jwt")
if err != nil {
return "", err
}
return string(jwt), nil
}
// ID returns the user's ID.
func (cc *Client) ID() (string, error) {
s, err := cc.sshSession()
if err != nil {
return "", err
}
defer s.Close()
id, err := s.Output("id")
if err != nil {
return "", err
}
return string(id), nil
}
// AuthorizedKeys returns the keys linked to a user's account.
func (cc *Client) AuthorizedKeys() (string, error) {
s, err := cc.sshSession()
if err != nil {
return "", err
}
defer s.Close()
keys, err := s.Output("keys")
if err != nil {
return "", err
}
return string(keys), nil
}
// AuthorizedKeysWithMetadata fetches keys linked to a user's account, with metadata.
func (cc *Client) AuthorizedKeysWithMetadata() (*Keys, error) {
s, err := cc.sshSession()
if err != nil {
return nil, err
}
defer s.Close()
b, err := s.Output("api-keys")
if err != nil {
return nil, err
}
var k Keys
err = json.Unmarshal(b, &k)
return &k, err
}
// AuthKeyPaths returns the full file path of the Charm auth SSH keys.
func (cc *Client) AuthKeyPaths() []string {
return cc.authKeyPaths
}
// UnlinkAuthorizedKey removes an authorized key from the user's Charm account.
func (cc *Client) UnlinkAuthorizedKey(key string) error {
s, err := cc.sshSession()
if err != nil {
return err
}
defer s.Close()
k := Key{Key: key}
in, err := s.StdinPipe()
if err != nil {
return err
}
if err := json.NewEncoder(in).Encode(k); err != nil {
return err
}
j, err := json.Marshal(&k)
if err != nil {
return err
}
b, err := s.Output(fmt.Sprintf("api-unlink %s", string(j)))
if err != nil {
return err
}
if len(b) != 0 {
return ErrCouldNotUnlinkKey
}
return nil
}
// SetName sets the account's username.
func (cc *Client) SetName(name string) (*User, error) {
if !ValidateName(name) {
return nil, ErrNameInvalid
}
u := &User{}
u.Name = name
err := cc.AuthedRequest("POST", cc.config.BioHost, cc.config.BioPort, "/v1/bio", u, u)
if err != nil {
return nil, err
}
return u, nil
}
// Bio returns the user's profile.
func (cc *Client) Bio() (*User, error) {
u := &User{}
id, err := cc.ID()
if err != nil {
return nil, err
}
err = cc.AuthedRequest("GET", cc.config.BioHost, cc.config.BioPort, fmt.Sprintf("/v1/id/%s", id), u, u)
if err != nil {
return nil, err
}
if u == nil {
return nil, errors.New("no user data received")
}
return u, nil
}
// ValidateName validates a given name.
func ValidateName(name string) bool {
return nameValidator.MatchString(name)
}
func (cc *Client) sshSession() (*ssh.Session, error) {
c, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", cc.config.IDHost, cc.config.IDPort), cc.sshConfig)
if err != nil {
return nil, err
}
s, err := c.NewSession()
if err != nil {
return nil, err
}
return s, nil
}
func publicKeyAuthMethod(kp string) (ssh.AuthMethod, error) {
keyPath, err := homedir.Expand(kp)
if err != nil {
return nil, err
}
key, err := ioutil.ReadFile(keyPath)
if err != nil {
return nil, err
}
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
return nil, err
}
return ssh.PublicKeys(signer), nil
}
func agentAuthMethod() (ssh.AuthMethod, error) {
socket := os.Getenv("SSH_AUTH_SOCK")
if socket == "" {
// fmt.Println("No SSH_AUTH_SOCK set, not using ssh-agent")
return nil, fmt.Errorf("Missing socket env var")
}
conn, err := net.Dial("unix", socket)
if err != nil {
// fmt.Printf("SSH agent dial error: %s\n", err)
return nil, err
}
agentClient := agent.NewClient(conn)
return ssh.PublicKeysCallback(agentClient.Signers), nil
}
// findSSHKeys looks in a user's ~/.ssh dir for possible SSH keys. If no keys
// are found we return an empty slice.
func findSSHKeys() (pathsToKeys []string, err error) {
path, err := homedir.Expand("~/.ssh")
if err != nil {
return nil, err
}
m, err := filepath.Glob(filepath.Join(path, "id_*"))
if err != nil {
return nil, err
}
if len(m) == 0 {
return nil, nil
}
var found []string
for _, f := range m {
switch filepath.Base(f) {
case "id_dsa":
fallthrough
case "id_rsa":
fallthrough
case "id_ecdsa":
fallthrough
case "id_ed25519":
found = append(found, f)
}
}
return found, nil
}
// findCharmKeys looks in a user's XDG charm-dir for possible auth keys.
// If no keys are found we return an empty slice.
func findAuthKeys() (pathsToKeys []string, err error) {
keyPath, err := DataPath()
if err != nil {
return nil, err
}
m, err := filepath.Glob(filepath.Join(keyPath, "charm_*"))
if err != nil {
return nil, err
}
if len(m) == 0 {
return nil, nil
}
var found []string
for _, f := range m {
switch filepath.Base(f) {
case "charm_rsa":
fallthrough
case "charm_ecdsa":
fallthrough
case "charm_ed25519":
found = append(found, f)
}
}
return found, nil
}
const privateKeySizeLimit = 1 << 24 // 16 MiB
func parsePrivateKey(file string) (interface{}, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
contents, err := ioutil.ReadAll(io.LimitReader(f, privateKeySizeLimit))
if err != nil {
return nil, err
}
if len(contents) == privateKeySizeLimit {
return nil, fmt.Errorf("key size exceeded limit")
}
return ssh.ParseRawPrivateKey(contents)
}
func findSSHSigners() []ssh.Signer {
var r []ssh.Signer
// from agent
signers, err := sasquatch.SSHAgentSigners()
if err == nil {
r = append(r, signers...)
}
files, _ := findSSHKeys()
for _, file := range files {
k, err := parsePrivateKey(file)
if err != nil {
continue
}
signer, err := ssh.NewSignerFromKey(k)
if err != nil {
continue
}
r = append(r, signer)
}
return r
}