forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
809 lines (730 loc) · 21.5 KB
/
api.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
/*
Copyright 2016 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package client
import (
"bufio"
"bytes"
"crypto/x509"
"encoding/pem"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net"
"os"
"os/signal"
"os/user"
"path/filepath"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/auth/native"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/session"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/teleport/lib/web"
log "github.com/Sirupsen/logrus"
"github.com/docker/docker/pkg/term"
"github.com/gravitational/trace"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"golang.org/x/crypto/ssh/terminal"
)
// Config is a client config
type Config struct {
// Login is a teleport user login
Login string
// Remote host to connect
Host string
// Labels represent host Labels
Labels map[string]string
// HostLogin is a user login on a remote host
HostLogin string
// HostPort is a remote host port to connect to
HostPort int
// ProxyHost is a host or IP of the proxy (with optional ":port")
ProxyHost string
// KeyTTL is a time to live for the temporary SSH keypair to remain valid:
KeyTTL time.Duration
// InsecureSkipVerify is an option to skip HTTPS cert check
InsecureSkipVerify bool
}
// ProxyHostPort returns a full host:port address of the proxy or an empty string if no
// proxy is given. If 'forWeb' flag is set, returns HTTPS port, otherwise
// returns SSH port (proxy servers listen on both)
func (c *Config) ProxyHostPort(defaultPort int) string {
if c.ProxySpecified() {
port := fmt.Sprintf("%d", defaultPort)
return net.JoinHostPort(c.ProxyHost, port)
}
return ""
}
// NodeHostPort returns host:port string based on user supplied data
// either if user has set host:port in the connection string,
// or supplied the -p flag. If user has set both, -p flag data is ignored
func (c *Config) NodeHostPort() string {
if strings.Contains(c.Host, ":") {
return c.Host
}
return net.JoinHostPort(c.Host, strconv.Itoa(c.HostPort))
}
// ProxySpecified returns true if proxy has been specified
func (c *Config) ProxySpecified() bool {
return len(c.ProxyHost) > 0
}
// TeleportClient is a wrapper around SSH client with teleport specific
// workflow built in
type TeleportClient struct {
Config
localAgent agent.Agent
authMethods []ssh.AuthMethod
}
// NewClient creates a TeleportClient object and fully configures it
func NewClient(c *Config) (tc *TeleportClient, err error) {
// validate configuration
if c.Login == "" {
c.Login = Username()
log.Infof("no teleport login given. defaulting to %s", c.Login)
}
if c.ProxyHost == "" {
return nil, trace.Wrap(teleport.BadParameter("proxy", "no proxy address specified"))
}
if c.HostLogin == "" {
c.HostLogin = c.Login
log.Infof("no host login given. defaulting to %s", c.HostLogin)
}
if c.KeyTTL == 0 {
c.KeyTTL = defaults.CertDuration
} else if c.KeyTTL > defaults.MaxCertDuration || c.KeyTTL < defaults.MinCertDuration {
return nil, trace.Errorf("invalid requested cert TTL")
}
tc = &TeleportClient{
Config: *c,
authMethods: make([]ssh.AuthMethod, 0),
}
// first, see if we can authenticate with credentials stored in
// a local SSH agent:
if sshAgent := connectToSSHAgent(); sshAgent != nil {
tc.authMethods = append(tc.authMethods, authMethodFromAgent(sshAgent))
}
// then, we can authenticate via a locally stored cert previously
// signed by the CA:
localAgent, err := GetLocalAgent()
if err != nil {
return nil, trace.Wrap(err)
}
if localAgent != nil {
tc.localAgent = localAgent
tc.authMethods = append(tc.authMethods, authMethodFromAgent(localAgent))
} else {
log.Errorf("unable to obtain locally stored credentials")
}
// finally, interactive auth (via password + HTOP 2nd factor):
tc.authMethods = append(tc.authMethods, tc.makeInteractiveAuthMethod())
return tc, nil
}
// getTargetNodes returns a list of node addresses this SSH command needs to
// operate on.
func (tc *TeleportClient) getTargetNodes(proxy *ProxyClient) ([]string, error) {
var (
err error
nodes []services.Server
retval = make([]string, 0)
)
if tc.Labels != nil && len(tc.Labels) > 0 {
nodes, err = proxy.FindServersByLabels(tc.Labels)
if err != nil {
return nil, trace.Wrap(err)
}
for i := 0; i < len(nodes); i++ {
retval = append(retval, nodes[i].Addr)
}
}
if len(nodes) == 0 {
retval = append(retval, net.JoinHostPort(tc.Host, strconv.Itoa(tc.HostPort)))
}
return retval, nil
}
// SSH connects to a node and, if 'command' is specified, executes the command on it,
// otherwise runs interactive shell
func (tc *TeleportClient) SSH(command string) (err error) {
// connect to proxy first:
if !tc.Config.ProxySpecified() {
return trace.Wrap(teleport.BadParameter("server", "proxy server is not specified"))
}
proxyClient, err := tc.ConnectToProxy()
if err != nil {
return trace.Wrap(err)
}
defer proxyClient.Close()
// which nodes are we executing this commands on?
nodeAddrs, err := tc.getTargetNodes(proxyClient)
if err != nil {
return trace.Wrap(err)
}
if len(nodeAddrs) == 0 {
return trace.Wrap(teleport.BadParameter("host", "no target host specified"))
}
// execute non-interactive SSH command:
if len(command) > 0 {
return tc.runCommand(nodeAddrs, proxyClient, command)
}
// more than one node for an interactive shell?
// that can't be!
if len(nodeAddrs) != 1 {
return trace.Errorf("Cannot launch shell on multiple nodes: %v", nodeAddrs)
}
// execute SSH shell on a single node:
nodeClient, err := proxyClient.ConnectToNode(nodeAddrs[0], tc.Config.HostLogin)
if err != nil {
return trace.Wrap(err)
}
defer nodeClient.Close()
return tc.runShell(nodeClient, "")
}
// Join connects to the existing/active SSH session
func (tc *TeleportClient) Join(sessionID session.ID) (err error) {
var notFoundError = &teleport.NotFoundError{Message: "Session not found or it has ended"}
// connect to proxy:
if !tc.Config.ProxySpecified() {
return trace.Wrap(teleport.BadParameter("server", "proxy server is not specified"))
}
proxyClient, err := tc.ConnectToProxy()
if err != nil {
return trace.Wrap(err)
}
defer proxyClient.Close()
// connect to the first site via proxy:
sites, err := proxyClient.GetSites()
if err != nil {
return trace.Wrap(err)
}
if len(sites) == 0 {
return trace.Wrap(notFoundError)
}
site, err := proxyClient.ConnectToSite(sites[0].Name, tc.Config.HostLogin)
if err != nil {
return trace.Wrap(err)
}
// find the session ID on the site:
sessions, err := site.GetSessions()
if err != nil {
return trace.Wrap(err)
}
var session *session.Session
for _, s := range sessions {
if s.ID == sessionID {
session = &s
break
}
}
if session == nil {
return trace.Wrap(notFoundError)
}
// pick the 1st party of the session and use his server ID to connect to
if len(session.Parties) == 0 {
return trace.Wrap(notFoundError)
}
serverID := session.Parties[0].ServerID
// find a server address by its ID
nodes, err := site.GetNodes()
if err != nil {
return trace.Wrap(err)
}
var node *services.Server
for _, n := range nodes {
if n.ID == serverID {
node = &n
break
}
}
if node == nil {
return trace.Wrap(notFoundError)
}
// connect to server:
nc, err := proxyClient.ConnectToNode(node.Addr, tc.Config.HostLogin)
if err != nil {
return trace.Wrap(err)
}
return tc.runShell(nc, session.ID)
}
// SCP securely copies file(s) from one SSH server to another
func (tc *TeleportClient) SCP(args []string, port int, recursive bool) (err error) {
if len(args) < 2 {
return trace.Errorf("Need at least two arguments for scp")
}
first := args[0]
last := args[len(args)-1]
// local copy?
if !isRemoteDest(first) && !isRemoteDest(last) {
return trace.Errorf("Making local copies is not supported")
}
if !tc.Config.ProxySpecified() {
return trace.Wrap(teleport.BadParameter("server", "proxy server is not specified"))
}
proxyClient, err := tc.ConnectToProxy()
if err != nil {
return trace.Wrap(err)
}
defer proxyClient.Close()
// upload:
if isRemoteDest(last) {
host, dest := parseSCPDestination(last)
addr := net.JoinHostPort(host, strconv.Itoa(port))
client, err := proxyClient.ConnectToNode(addr, tc.HostLogin)
if err != nil {
return trace.Wrap(err)
}
// copy everything except the last arg (that's destination)
for _, src := range args[:len(args)-1] {
err = client.Upload(src, dest)
if err != nil {
return trace.Wrap(err)
}
fmt.Printf("uploaded %s\n", src)
}
// download:
} else {
host, src := parseSCPDestination(first)
addr := net.JoinHostPort(host, strconv.Itoa(port))
client, err := proxyClient.ConnectToNode(addr, tc.HostLogin)
if err != nil {
return trace.Wrap(err)
}
// copy everything except the last arg (that's destination)
for _, dest := range args[1:] {
err = client.Download(src, dest, recursive)
if err != nil {
return trace.Wrap(err)
}
fmt.Printf("downloaded %s\n", src)
}
}
return nil
}
func parseSCPDestination(s string) (host, dest string) {
parts := strings.Split(s, ":")
return parts[0], strings.Join(parts[1:], ":")
}
func isRemoteDest(name string) bool {
return strings.IndexRune(name, ':') >= 0
}
// ListNodes returns a list of nodes connected to a proxy
func (tc *TeleportClient) ListNodes() ([]services.Server, error) {
var err error
// userhost is specified? that must be labels
if tc.Host != "" {
tc.Labels, err = ParseLabelSpec(tc.Host)
if err != nil {
return nil, trace.Wrap(err)
}
}
// connect to the proxy and ask it to return a full list of servers
proxyClient, err := tc.ConnectToProxy()
if err != nil {
return nil, trace.Wrap(err)
}
defer proxyClient.Close()
return proxyClient.FindServersByLabels(tc.Labels)
}
// runCommand executes a given bash command on a bunch of remote nodes
func (tc *TeleportClient) runCommand(nodeAddresses []string, proxyClient *ProxyClient, command string) error {
stdoutMutex := &sync.Mutex{}
wg := &sync.WaitGroup{}
for _, address := range nodeAddresses {
wg.Add(1)
go func(address string) {
defer wg.Done()
nodeClient, err := proxyClient.ConnectToNode(address, tc.Config.Login)
if err != nil {
fmt.Println(err)
}
defer nodeClient.Close()
// run the command on one node:
out := bytes.Buffer{}
err = nodeClient.Run(command, &out)
if err != nil {
fmt.Println(err)
}
stdoutMutex.Lock()
defer stdoutMutex.Unlock()
fmt.Printf("Running command on %v:\n", address)
if err != nil {
fmt.Println(err)
} else {
fmt.Printf(out.String())
}
}(address)
}
wg.Wait()
return nil
}
// runShell starts an interactive SSH session/shell
func (tc *TeleportClient) runShell(nodeClient *NodeClient, sessionID session.ID) error {
defer nodeClient.Close()
address := tc.NodeHostPort()
// If typing on the terminal, we do not want the terminal to echo the
// password that is typed (so it doesn't display)
if term.IsTerminal(0) {
state, err := term.SetRawTerminal(0)
if err != nil {
return trace.Wrap(err)
}
defer term.RestoreTerminal(0, state)
}
broadcastClose := utils.NewCloseBroadcaster()
// Catch term signals
exitSignals := make(chan os.Signal, 1)
signal.Notify(exitSignals, syscall.SIGTERM)
go func() {
defer broadcastClose.Close()
<-exitSignals
fmt.Printf("\nConnection to %s closed\n", address)
}()
winSize, err := term.GetWinsize(0)
if err != nil {
return trace.Wrap(err)
}
shell, err := nodeClient.Shell(int(winSize.Width), int(winSize.Height), sessionID)
if err != nil {
return trace.Wrap(err)
}
// Catch Ctrl-C signal
ctrlCSignal := make(chan os.Signal, 1)
signal.Notify(ctrlCSignal, syscall.SIGINT)
go func() {
for {
<-ctrlCSignal
_, err := shell.Write([]byte{3})
if err != nil {
log.Errorf(err.Error())
}
}
}()
// Catch Ctrl-Z signal
ctrlZSignal := make(chan os.Signal, 1)
signal.Notify(ctrlZSignal, syscall.SIGTSTP)
go func() {
for {
<-ctrlZSignal
_, err := shell.Write([]byte{26})
if err != nil {
log.Errorf(err.Error())
}
}
}()
// copy from the remote shell to the local
go func() {
defer broadcastClose.Close()
_, err := io.Copy(os.Stdout, shell)
if err != nil {
log.Errorf(err.Error())
}
fmt.Printf("\nConnection to %s closed from the remote side\n", address)
}()
// copy from the local shell to the remote
go func() {
defer broadcastClose.Close()
buf := make([]byte, 1)
for {
n, err := os.Stdin.Read(buf)
if err != nil {
fmt.Println(trace.Wrap(err))
return
}
if n > 0 {
// catch Ctrl-D
if buf[0] == 4 {
fmt.Printf("\nConnection to %s closed\n", address)
return
}
_, err = shell.Write(buf[:n])
if err != nil {
fmt.Println(trace.Wrap(err))
return
}
}
}
}()
<-broadcastClose.C
return nil
}
// ConnectToProxy dials the proxy server and returns ProxyClient if successful
func (tc *TeleportClient) ConnectToProxy() (*ProxyClient, error) {
proxyAddr := tc.Config.ProxyHostPort(defaults.SSHProxyListenPort)
sshConfig := &ssh.ClientConfig{
User: tc.Config.Login,
HostKeyCallback: CheckHostSignature,
}
if len(tc.authMethods) == 0 {
return nil, trace.Errorf("no authentication methods provided")
}
log.Debugf("connecting to proxy: %v", proxyAddr)
for {
// try to authenticate using every auth method we have:
for _, m := range tc.authMethods {
sshConfig.Auth = []ssh.AuthMethod{m}
proxyClient, err := ssh.Dial("tcp", proxyAddr, sshConfig)
if err != nil {
if utils.IsHandshakeFailedError(err) {
continue
}
return nil, trace.Wrap(err)
}
return &ProxyClient{
Client: proxyClient,
proxyAddress: proxyAddr,
hostKeyCallback: sshConfig.HostKeyCallback,
authMethods: tc.authMethods,
hostLogin: tc.Config.HostLogin,
}, nil
}
// if we get here, it means we failed to authenticate using stored keys
// and we need to ask for the login information
err := tc.Login()
if err != nil {
// we need to communicate directly to user here,
// otherwise user will see endless loop with no explanation
if teleport.IsTrustError(err) {
fmt.Printf("ERROR: refusing to connect to untrusted proxy %v without --insecure flag\n", tc.Config.ProxyHostPort(defaults.HTTPListenPort))
} else {
fmt.Printf("ERROR: %v\n", err)
}
}
}
}
// makeInteractiveAuthMethod creates an 'ssh.AuthMethod' which authenticates
// interactively by asknig a Teleport password + HTOP token
func (tc *TeleportClient) makeInteractiveAuthMethod() ssh.AuthMethod {
callbackFunc := func() (signers []ssh.Signer, err error) {
if err = tc.Login(); err != nil {
log.Error(err)
// we need to communicate directly to user here,
// otherwise user will see endless loop with no explanation
if teleport.IsTrustError(err) {
fmt.Printf("ERROR: refusing to connect to untrusted proxy %v without --insecure flag\n", tc.Config.ProxyHostPort(defaults.HTTPListenPort))
} else {
fmt.Printf("ERROR: %v\n", err)
}
return nil, trace.Wrap(err)
}
return tc.localAgent.Signers()
}
return ssh.PublicKeysCallback(callbackFunc)
}
// Login asks for a password + HOTP token, makes a request to CA via proxy and
// saves the generated credentials into local keystore for future use
func (tc *TeleportClient) Login() error {
password, hotpToken, err := tc.AskPasswordAndHOTP()
if err != nil {
return trace.Wrap(err)
}
// generate a new keypair. the public key will be signed via proxy if our password+HOTP
// are legit
priv, pub, err := native.New().GenerateKeyPair("")
if err != nil {
return trace.Wrap(err)
}
// ask the CA (via proxy) to sign our public key:
response, err := web.SSHAgentLogin(tc.Config.ProxyHostPort(defaults.HTTPListenPort), tc.Config.Login,
password, hotpToken, pub, tc.KeyTTL, tc.InsecureSkipVerify, loopbackPool(tc.Config.ProxyHostPort(defaults.HTTPListenPort)))
if err != nil {
return trace.Wrap(err)
}
// parse the returned&signed key:
pcert, _, _, _, err := ssh.ParseAuthorizedKey(response.Cert)
if err != nil {
return trace.Wrap(err)
}
pk, err := ssh.ParseRawPrivateKey(priv)
if err != nil {
return trace.Wrap(err)
}
// store the newly generated key in the local key store
addedKey := agent.AddedKey{
PrivateKey: pk,
Certificate: pcert.(*ssh.Certificate),
Comment: "",
LifetimeSecs: 0,
ConfirmBeforeUse: false,
}
if err := tc.localAgent.Add(addedKey); err != nil {
return trace.Wrap(err)
}
key := Key{
Priv: priv,
Cert: response.Cert,
Deadline: time.Now().Add(tc.KeyTTL),
}
keyPath := filepath.Join(getKeysDir(), KeyFilePrefix+strconv.FormatInt(rand.Int63n(100), 16)+KeyFileSuffix)
err = saveKey(key, keyPath)
if err != nil {
return trace.Wrap(err)
}
// save the list of CAs we trust to the cache file
err = AddHostSignersToCache(response.HostSigners)
if err != nil {
return trace.Wrap(err)
}
return nil
}
// loopbackPool reads trusted CAs if it finds it in a predefined location
// and will work only if target proxy address is loopback
func loopbackPool(proxyAddr string) *x509.CertPool {
if !utils.IsLoopback(proxyAddr) {
log.Debugf("not using loopback pool for remote proxy addr: %v", proxyAddr)
return nil
}
log.Debugf("attempting to use loopback pool for local proxy addr: %v", proxyAddr)
certPool := x509.NewCertPool()
certPath := filepath.Join(defaults.DataDir, defaults.SelfSignedCertPath)
pemByte, err := ioutil.ReadFile(certPath)
if err != nil {
log.Debugf("could not open any path in: %v", certPath)
return nil
}
for {
var block *pem.Block
block, pemByte = pem.Decode(pemByte)
if block == nil {
break
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
log.Debugf("could not parse cert in: %v, err: %v", certPath, err)
return nil
}
certPool.AddCert(cert)
}
log.Debugf("using local pool for loopback proxy: %v, err: %v", certPath, err)
return certPool
}
// connects to a local SSH agent
func connectToSSHAgent() agent.Agent {
socketPath := os.Getenv("SSH_AUTH_SOCK")
if socketPath == "" {
log.Infof("SSH_AUTH_SOCK is not set. Is local SSH agent running?")
return nil
}
conn, err := net.Dial("unix", socketPath)
if err != nil {
log.Errorf("Failed connecting to local SSH agent via %s", socketPath)
return nil
}
return agent.NewClient(conn)
}
// Username returns the current user's username
func Username() string {
u, err := user.Current()
if err != nil {
utils.FatalError(err)
}
return u.Username
}
// AskPasswordAndHOTP prompts the user to enter the password + HTOP 2nd factor
func (tc *TeleportClient) AskPasswordAndHOTP() (pwd string, token string, err error) {
fmt.Printf("Enter password for %v:\n", tc.Config.Login)
pwd, err = passwordFromConsole()
if err != nil {
fmt.Println(err)
return "", "", trace.Wrap(err)
}
fmt.Printf("Enter your HOTP token:\n")
token, err = lineFromConsole()
if err != nil {
fmt.Println(err)
return "", "", trace.Wrap(err)
}
return pwd, token, nil
}
// passwordFromConsole reads from stdin without echoing typed characters to stdout
func passwordFromConsole() (string, error) {
fd := syscall.Stdin
state, err := terminal.GetState(fd)
// intercept Ctr+C and restore terminal
sigCh := make(chan os.Signal, 1)
closeCh := make(chan int)
if err != nil {
log.Warnf("failed reading terminal state: %v", err)
} else {
signal.Notify(sigCh, syscall.SIGINT)
go func() {
select {
case <-sigCh:
terminal.Restore(fd, state)
os.Exit(1)
case <-closeCh:
}
}()
}
defer func() {
close(closeCh)
}()
bytes, err := terminal.ReadPassword(fd)
return string(bytes), err
}
// lineFromConsole reads a line from stdin
func lineFromConsole() (string, error) {
bytes, _, err := bufio.NewReader(os.Stdin).ReadLine()
return string(bytes), err
}
// parseLabelSpec parses a string like 'name=value,"long name"="quoted value"` into a map like
// { "name" -> "value", "long name" -> "quoted value" }
func ParseLabelSpec(spec string) (map[string]string, error) {
tokens := []string{}
var openQuotes = false
var tokenStart, assignCount int
var specLen = len(spec)
// tokenize the label spec:
for i, ch := range spec {
endOfToken := false
// end of line?
if i+1 == specLen {
i++
endOfToken = true
}
switch ch {
case '"':
openQuotes = !openQuotes
case '=', ',', ';':
if !openQuotes {
endOfToken = true
if ch == '=' {
assignCount += 1
}
}
}
if endOfToken && i > tokenStart {
tokens = append(tokens, strings.TrimSpace(strings.Trim(spec[tokenStart:i], `"`)))
tokenStart = i + 1
}
}
// simple validation of tokenization: must have an even number of tokens (because they're pairs)
// and the number of such pairs must be equal the number of assignments
if len(tokens)%2 != 0 || assignCount != len(tokens)/2 {
return nil, fmt.Errorf("invalid label spec: '%s', should be 'key=value'", spec)
}
// break tokens in pairs and put into a map:
labels := make(map[string]string)
for i := 0; i < len(tokens); i += 2 {
labels[tokens[i]] = tokens[i+1]
}
return labels, nil
}
func authMethodFromAgent(ag agent.Agent) ssh.AuthMethod {
return ssh.PublicKeysCallback(ag.Signers)
}