-
Notifications
You must be signed in to change notification settings - Fork 225
/
ssh_terminal.go
160 lines (129 loc) · 3.75 KB
/
ssh_terminal.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
package ssh
// TODO - This file was copy and pasted from the cmd package and still needs to be
// updated to take on new conventions.
import (
"bytes"
"context"
"crypto/ed25519"
"fmt"
"io"
"net"
"os"
"time"
"github.com/briandowns/spinner"
"github.com/docker/docker/pkg/ioutils"
"github.com/pkg/errors"
"github.com/superfly/flyctl/agent"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/helpers"
"github.com/superfly/flyctl/ssh"
"github.com/superfly/flyctl/terminal"
)
func spin(in, out string) context.CancelFunc {
ctx, cancel := context.WithCancel(context.Background())
if !helpers.IsTerminal() {
fmt.Fprintln(os.Stderr, in)
return cancel
}
go func() {
s := spinner.New(spinner.CharSets[11], 100*time.Millisecond)
s.Writer = os.Stderr
s.Prefix = in
s.FinalMSG = out
s.Start()
defer s.Stop()
<-ctx.Done()
}()
return cancel
}
type SSHParams struct {
Ctx context.Context
Org api.OrganizationImpl
App string
Dialer agent.Dialer
Cmd string
Stdin io.Reader
Stdout io.WriteCloser
Stderr io.WriteCloser
DisableSpinner bool
}
func RunSSHCommand(ctx context.Context, app *api.AppCompact, dialer agent.Dialer, addr string, cmd string) ([]byte, error) {
var inBuf bytes.Buffer
var errBuf bytes.Buffer
var outBuf bytes.Buffer
stdoutWriter := ioutils.NewWriteCloserWrapper(&outBuf, func() error { return nil })
stderrWriter := ioutils.NewWriteCloserWrapper(&errBuf, func() error { return nil })
inReader := ioutils.NewReadCloserWrapper(&inBuf, func() error { return nil })
err := SSHConnect(&SSHParams{
Ctx: ctx,
Org: app.Organization,
Dialer: dialer,
App: app.Name,
Cmd: cmd,
Stdin: inReader,
Stdout: stdoutWriter,
Stderr: stderrWriter,
DisableSpinner: true,
}, addr)
if err != nil {
return nil, err
}
if len(errBuf.Bytes()) > 0 {
return nil, fmt.Errorf(errBuf.String())
}
return outBuf.Bytes(), nil
}
func SSHConnect(p *SSHParams, addr string) error {
terminal.Debugf("Fetching certificate for %s\n", addr)
cert, pk, err := singleUseSSHCertificate(p.Ctx, p.Org)
if err != nil {
return fmt.Errorf("create ssh certificate: %w (if you haven't created a key for your org yet, try `flyctl ssh establish`)", err)
}
pemkey := ssh.MarshalED25519PrivateKey(pk, "single-use certificate")
terminal.Debugf("Keys for %s configured; connecting...\n", addr)
sshClient := &ssh.Client{
Addr: net.JoinHostPort(addr, "22"),
User: "root",
Dial: p.Dialer.DialContext,
Certificate: cert.Certificate,
PrivateKey: string(pemkey),
}
var endSpin context.CancelFunc
if !p.DisableSpinner {
endSpin = spin(fmt.Sprintf("Connecting to %s...", addr),
fmt.Sprintf("Connecting to %s... complete\n", addr))
defer endSpin()
}
if err := sshClient.Connect(context.Background()); err != nil {
return errors.Wrap(err, "error connecting to SSH server")
}
defer sshClient.Close()
terminal.Debugf("Connection completed.\n", addr)
if !p.DisableSpinner {
endSpin()
}
term := &ssh.Terminal{
Stdin: p.Stdin,
Stdout: p.Stdout,
Stderr: p.Stderr,
Mode: "xterm",
}
if err := sshClient.Shell(context.Background(), term, p.Cmd); err != nil {
return errors.Wrap(err, "ssh shell")
}
return nil
}
func singleUseSSHCertificate(ctx context.Context, org api.OrganizationImpl) (*api.IssuedCertificate, ed25519.PrivateKey, error) {
client := client.FromContext(ctx).API()
hours := 1
pub, priv, err := ed25519.GenerateKey(nil)
if err != nil {
return nil, nil, err
}
icert, err := client.IssueSSHCertificate(ctx, org, []string{"root", "fly"}, nil, &hours, pub)
if err != nil {
return nil, nil, err
}
return icert, priv, nil
}