forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal.go
272 lines (245 loc) · 7.91 KB
/
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
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
/*
Copyright 2015 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 web
import (
"context"
"fmt"
"io"
"net"
"net/http"
"strconv"
"strings"
"github.com/gravitational/teleport/lib/client"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/session"
"github.com/gravitational/teleport/lib/sshutils"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/trace"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
"golang.org/x/net/websocket"
)
// TerminalRequest describes a request to crate a web-based terminal
// to a remote SSH server
type TerminalRequest struct {
// Server describes a server to connect to (serverId|hostname[:port])
Server string `json:"server_id"`
// User is linux username to connect as
Login string `json:"login"`
// Term sets PTY params like width and height
Term session.TerminalParams `json:"term"`
// SessionID is a teleport session ID to join as
SessionID session.ID `json:"sid"`
// Namespace is node namespace
Namespace string `json:"namespace"`
// Proxy server address
ProxyHostPort string `json:"-"`
// Remote cluster name
Cluster string `json:"-"`
// InteractiveCommand is a command to execute
InteractiveCommand []string `json:"-"`
}
// NodeProvider is a provider of nodes for namespace
type NodeProvider interface {
GetNodes(namespace string) ([]services.Server, error)
}
// newTerminal creates a web-based terminal based on WebSockets and returns a new
// TerminalHandler
func NewTerminal(req TerminalRequest, provider NodeProvider, ctx *SessionContext) (*TerminalHandler, error) {
// make sure whatever session is requested is a valid session
_, err := session.ParseID(string(req.SessionID))
if err != nil {
return nil, trace.BadParameter("sid: invalid session id")
}
if req.Server == "" {
return nil, trace.BadParameter("server: missing server")
}
if req.Login == "" {
return nil, trace.BadParameter("login: missing login")
}
if req.Term.W <= 0 || req.Term.H <= 0 {
return nil, trace.BadParameter("term: bad term dimensions")
}
servers, err := provider.GetNodes(req.Namespace)
if err != nil {
return nil, trace.Wrap(err)
}
hostName, hostPort, err := resolveHostPort(req.Server, servers)
if err != nil {
return nil, trace.Wrap(err)
}
return &TerminalHandler{
params: req,
ctx: ctx,
hostName: hostName,
hostPort: hostPort,
}, nil
}
// TerminalHandler connects together an SSH session with a web-based
// terminal via a web socket.
type TerminalHandler struct {
// params describe the terminal configuration
params TerminalRequest
// ctx is a web session context for the currently logged in user
ctx *SessionContext
// ws is the websocket which is connected to stdin/out/err of the terminal shell
ws *websocket.Conn
// hostName we're connected to
hostName string
// hostPort we're connected to
hostPort int
// sshClient is initialized after an SSH connection to a node is established
sshSession *ssh.Session
}
func (t *TerminalHandler) Close() error {
if t.ws != nil {
t.ws.Close()
}
if t.sshSession != nil {
t.sshSession.Close()
}
return nil
}
// resizePTYWindow is called when a brower resizes its window. Now the node
// needs to be notified via SSH
func (t *TerminalHandler) resizePTYWindow(params session.TerminalParams) error {
if t.sshSession == nil {
return nil
}
_, err := t.sshSession.SendRequest(
// send SSH "window resized" SSH request:
sshutils.WindowChangeRequest,
// no response needed
false,
ssh.Marshal(sshutils.WinChangeReqParams{
W: uint32(params.W),
H: uint32(params.H),
}))
if err != nil {
log.Error(err)
}
return trace.Wrap(err)
}
// Run creates a new websocket connection to the SSH server and runs
// the "loop" piping the input/output of the SSH session into the
// js-based terminal.
func (t *TerminalHandler) Run(w http.ResponseWriter, r *http.Request) {
errToTerm := func(err error, w io.Writer) {
fmt.Fprintf(w, "%s\n\r", err.Error())
log.Error(err)
}
webSocketLoop := func(ws *websocket.Conn) {
agent, cert, err := t.ctx.GetAgent()
if err != nil {
log.Warningf("failed to get user credentials: %v", err)
errToTerm(err, ws)
return
}
signers, err := agent.Signers()
if err != nil {
log.Warningf("failed to get user credentials: %v", err)
errToTerm(err, ws)
return
}
tlsConfig, err := t.ctx.ClientTLSConfig()
if err != nil {
log.Warningf("failed to get client TLS config: %v", err)
errToTerm(err, ws)
return
}
// create teleport client:
output := utils.NewWebSockWrapper(ws, utils.WebSocketTextMode)
clientConfig := &client.Config{
SkipLocalAuth: true,
ForwardAgent: true,
Agent: agent,
TLS: tlsConfig,
AuthMethods: []ssh.AuthMethod{ssh.PublicKeys(signers...)},
DefaultPrincipal: cert.ValidPrincipals[0],
HostLogin: t.params.Login,
Username: t.ctx.user,
Namespace: t.params.Namespace,
Stdout: output,
Stderr: output,
Stdin: ws,
SiteName: t.params.Cluster,
ProxyHostPort: t.params.ProxyHostPort,
Host: t.hostName,
HostPort: t.hostPort,
Env: map[string]string{sshutils.SessionEnvVar: string(t.params.SessionID)},
HostKeyCallback: func(string, net.Addr, ssh.PublicKey) error { return nil },
ClientAddr: r.RemoteAddr,
}
if len(t.params.InteractiveCommand) > 0 {
clientConfig.Interactive = true
}
tc, err := client.NewClient(clientConfig)
if err != nil {
log.Warningf("failed to create client: %v", err)
errToTerm(err, ws)
return
}
// this callback will execute when a shell is created, it will give
// us a reference to ssh.Client object
tc.OnShellCreated = func(s *ssh.Session, c *ssh.Client, _ io.ReadWriteCloser) (bool, error) {
t.sshSession = s
t.resizePTYWindow(t.params.Term)
return false, nil
}
if err = tc.SSH(context.TODO(), t.params.InteractiveCommand, false); err != nil {
log.Warningf("failed to SSH: %v", err)
errToTerm(err, ws)
return
}
}
// this is to make sure we close web socket connections once
// sessionContext that owns them expires
t.ctx.AddClosers(t)
defer t.ctx.RemoveCloser(t)
// TODO(klizhentas)
// we instantiate a server explicitly here instead of using
// websocket.HandlerFunc to set empty origin checker
// make sure we check origin when in prod mode
ws := &websocket.Server{Handler: webSocketLoop}
ws.ServeHTTP(w, r)
}
// resolveHostPort parses an input value and attempts to resolve hostname and port of requested server
func resolveHostPort(value string, existingServers []services.Server) (string, int, error) {
var hostName = ""
// if port is 0, it means the client wants us to figure out which port to use
var hostPort = 0
// check if server exists by comparing its UUID or hostname
for i := range existingServers {
node := existingServers[i]
if node.GetName() == value || strings.EqualFold(node.GetHostname(), value) {
hostName = node.GetHostname()
break
}
}
// if server is not found, parse SSH connection string (for joining an unlisted SSH server)
if hostName == "" {
hostName = value
host, port, err := net.SplitHostPort(value)
if err != nil {
hostPort = defaults.SSHDefaultPort
} else {
hostName = host
hostPort, err = strconv.Atoi(port)
if err != nil {
return "", 0, trace.BadParameter("server: invalid port", err)
}
}
}
return hostName, hostPort, nil
}