/
session.go
374 lines (334 loc) · 12.6 KB
/
session.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
// Copyright 2022 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 ssh
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/gravitational/trace"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
oteltrace "go.opentelemetry.io/otel/trace"
"golang.org/x/crypto/ssh"
"github.com/gravitational/teleport/api/constants"
"github.com/gravitational/teleport/api/observability/tracing"
)
// Session is a wrapper around ssh.Session that adds tracing support
type Session struct {
*ssh.Session
wrapper *clientWrapper
}
// SendRequest sends an out-of-band channel request on the SSH channel
// underlying the session.
func (s *Session) SendRequest(ctx context.Context, name string, wantReply bool, payload []byte) (bool, error) {
config := tracing.NewConfig(s.wrapper.opts)
ctx, span := config.TracerProvider.Tracer(instrumentationName).Start(
ctx,
fmt.Sprintf("ssh.SessionRequest/%s", name),
oteltrace.WithSpanKind(oteltrace.SpanKindClient),
oteltrace.WithAttributes(
attribute.Bool("want_reply", wantReply),
semconv.RPCServiceKey.String("ssh.Session"),
semconv.RPCMethodKey.String("SendRequest"),
semconv.RPCSystemKey.String("ssh"),
),
)
defer span.End()
// no need to wrap payload here, the session's channel wrapper will do it for us
s.wrapper.addContext(ctx, name)
ok, err := s.Session.SendRequest(name, wantReply, payload)
return ok, trace.Wrap(err)
}
// Setenv sets an environment variable that will be applied to any
// command executed by Shell or Run.
func (s *Session) Setenv(ctx context.Context, name, value string) error {
const request = "env"
config := tracing.NewConfig(s.wrapper.opts)
ctx, span := config.TracerProvider.Tracer(instrumentationName).Start(
ctx,
fmt.Sprintf("ssh.Setenv/%s", name),
oteltrace.WithSpanKind(oteltrace.SpanKindClient),
oteltrace.WithAttributes(
semconv.RPCServiceKey.String("ssh.Session"),
semconv.RPCMethodKey.String("SendRequest"),
semconv.RPCSystemKey.String("ssh"),
),
)
defer span.End()
s.wrapper.addContext(ctx, request)
return trace.Wrap(s.Session.Setenv(name, value))
}
// SetEnvs sets environment variables that will be applied to any
// command executed by Shell or Run. If the server does not handle
// [EnvsRequest] requests then the client falls back to sending individual
// "env" requests until all provided environment variables have been set
// or an error was received.
func (s *Session) SetEnvs(ctx context.Context, envs map[string]string) error {
config := tracing.NewConfig(s.wrapper.opts)
ctx, span := config.TracerProvider.Tracer(instrumentationName).Start(
ctx,
"ssh.SetEnvs",
oteltrace.WithSpanKind(oteltrace.SpanKindClient),
oteltrace.WithAttributes(
semconv.RPCServiceKey.String("ssh.Session"),
semconv.RPCMethodKey.String("SendRequest"),
semconv.RPCSystemKey.String("ssh"),
),
)
defer span.End()
if len(envs) == 0 {
return nil
}
// If the server isn't Teleport fallback to individual "env" requests
if !strings.HasPrefix(string(s.wrapper.ServerVersion()), "SSH-2.0-Teleport") {
return trace.Wrap(s.setEnvFallback(ctx, envs))
}
raw, err := json.Marshal(envs)
if err != nil {
return trace.Wrap(err)
}
s.wrapper.addContext(ctx, EnvsRequest)
ok, err := s.Session.SendRequest(EnvsRequest, true, ssh.Marshal(EnvsReq{EnvsJSON: raw}))
if err != nil {
return trace.Wrap(err)
}
// The server does not handle EnvsRequest requests so fall back
// to sending individual requests.
if !ok {
return trace.Wrap(s.setEnvFallback(ctx, envs))
}
return nil
}
// setEnvFallback sends an "env" request for each item in envs.
func (s *Session) setEnvFallback(ctx context.Context, envs map[string]string) error {
for k, v := range envs {
if err := s.Setenv(ctx, k, v); err != nil {
return trace.Wrap(err, "failed to set environment variable %s", k)
}
}
return nil
}
// RequestPty requests the association of a pty with the session on the remote host.
func (s *Session) RequestPty(ctx context.Context, term string, h, w int, termmodes ssh.TerminalModes) error {
const request = "pty-req"
config := tracing.NewConfig(s.wrapper.opts)
tracer := config.TracerProvider.Tracer(instrumentationName)
ctx, span := tracer.Start(
ctx,
fmt.Sprintf("ssh.RequestPty/%s", term),
oteltrace.WithSpanKind(oteltrace.SpanKindClient),
oteltrace.WithAttributes(
semconv.RPCServiceKey.String("ssh.Session"),
semconv.RPCMethodKey.String("SendRequest"),
semconv.RPCSystemKey.String("ssh"),
attribute.Int("width", w),
attribute.Int("height", h),
),
)
defer span.End()
s.wrapper.addContext(ctx, request)
return trace.Wrap(s.Session.RequestPty(term, h, w, termmodes))
}
// RequestSubsystem requests the association of a subsystem with the session on the remote host.
// A subsystem is a predefined command that runs in the background when the ssh session is initiated.
func (s *Session) RequestSubsystem(ctx context.Context, subsystem string) error {
const request = "subsystem"
config := tracing.NewConfig(s.wrapper.opts)
ctx, span := config.TracerProvider.Tracer(instrumentationName).Start(
ctx,
fmt.Sprintf("ssh.RequestSubsystem/%s", subsystem),
oteltrace.WithSpanKind(oteltrace.SpanKindClient),
oteltrace.WithAttributes(
semconv.RPCServiceKey.String("ssh.Session"),
semconv.RPCMethodKey.String("SendRequest"),
semconv.RPCSystemKey.String("ssh"),
),
)
defer span.End()
s.wrapper.addContext(ctx, request)
return trace.Wrap(s.Session.RequestSubsystem(subsystem))
}
// WindowChange informs the remote host about a terminal window dimension change to h rows and w columns.
func (s *Session) WindowChange(ctx context.Context, h, w int) error {
const request = "window-change"
config := tracing.NewConfig(s.wrapper.opts)
ctx, span := config.TracerProvider.Tracer(instrumentationName).Start(
ctx,
"ssh.WindowChange",
oteltrace.WithSpanKind(oteltrace.SpanKindClient),
oteltrace.WithAttributes(
semconv.RPCServiceKey.String("ssh.Session"),
semconv.RPCMethodKey.String("SendRequest"),
semconv.RPCSystemKey.String("ssh"),
attribute.Int("height", h),
attribute.Int("width", w),
),
)
defer span.End()
s.wrapper.addContext(ctx, request)
return trace.Wrap(s.Session.WindowChange(h, w))
}
// Signal sends the given signal to the remote process.
// sig is one of the SIG* constants.
func (s *Session) Signal(ctx context.Context, sig ssh.Signal) error {
const request = "signal"
config := tracing.NewConfig(s.wrapper.opts)
ctx, span := config.TracerProvider.Tracer(instrumentationName).Start(
ctx,
fmt.Sprintf("ssh.Signal/%s", sig),
oteltrace.WithSpanKind(oteltrace.SpanKindClient),
oteltrace.WithAttributes(
semconv.RPCServiceKey.String("ssh.Session"),
semconv.RPCMethodKey.String("SendRequest"),
semconv.RPCSystemKey.String("ssh"),
),
)
defer span.End()
s.wrapper.addContext(ctx, request)
return trace.Wrap(s.Session.Signal(sig))
}
// Start runs cmd on the remote host. Typically, the remote
// server passes cmd to the shell for interpretation.
// A Session only accepts one call to Run, Start or Shell.
func (s *Session) Start(ctx context.Context, cmd string) error {
const request = "exec"
config := tracing.NewConfig(s.wrapper.opts)
ctx, span := config.TracerProvider.Tracer(instrumentationName).Start(
ctx,
fmt.Sprintf("ssh.Start/%s", cmd),
oteltrace.WithSpanKind(oteltrace.SpanKindClient),
oteltrace.WithAttributes(
semconv.RPCServiceKey.String("ssh.Session"),
semconv.RPCMethodKey.String("SendRequest"),
semconv.RPCSystemKey.String("ssh"),
),
)
defer span.End()
s.wrapper.addContext(ctx, request)
return trace.Wrap(s.Session.Start(cmd))
}
// Shell starts a login shell on the remote host. A Session only
// accepts one call to Run, Start, Shell, Output, or CombinedOutput.
func (s *Session) Shell(ctx context.Context) error {
const request = "shell"
config := tracing.NewConfig(s.wrapper.opts)
ctx, span := config.TracerProvider.Tracer(instrumentationName).Start(
ctx,
"ssh.Shell",
oteltrace.WithSpanKind(oteltrace.SpanKindClient),
oteltrace.WithAttributes(
semconv.RPCServiceKey.String("ssh.Session"),
semconv.RPCMethodKey.String("SendRequest"),
semconv.RPCSystemKey.String("ssh"),
),
)
defer span.End()
s.wrapper.addContext(ctx, request)
return trace.Wrap(s.Session.Shell())
}
// Run runs cmd on the remote host. Typically, the remote
// server passes cmd to the shell for interpretation.
// A Session only accepts one call to Run, Start, Shell, Output,
// or CombinedOutput.
//
// The returned error is nil if the command runs, has no problems
// copying stdin, stdout, and stderr, and exits with a zero exit
// status.
//
// If the remote server does not send an exit status, an error of type
// *ExitMissingError is returned. If the command completes
// unsuccessfully or is interrupted by a signal, the error is of type
// *ExitError. Other error types may be returned for I/O problems.
func (s *Session) Run(ctx context.Context, cmd string) error {
const request = "exec"
config := tracing.NewConfig(s.wrapper.opts)
ctx, span := config.TracerProvider.Tracer(instrumentationName).Start(
ctx,
fmt.Sprintf("ssh.Run/%s", cmd),
oteltrace.WithSpanKind(oteltrace.SpanKindClient),
oteltrace.WithAttributes(
semconv.RPCServiceKey.String("ssh.Session"),
semconv.RPCMethodKey.String("SendRequest"),
semconv.RPCSystemKey.String("ssh"),
),
)
defer span.End()
s.wrapper.addContext(ctx, request)
return trace.Wrap(s.Session.Run(cmd))
}
// Output runs cmd on the remote host and returns its standard output.
func (s *Session) Output(ctx context.Context, cmd string) ([]byte, error) {
const request = "exec"
config := tracing.NewConfig(s.wrapper.opts)
ctx, span := config.TracerProvider.Tracer(instrumentationName).Start(
ctx,
fmt.Sprintf("ssh.Output/%s", cmd),
oteltrace.WithSpanKind(oteltrace.SpanKindClient),
oteltrace.WithAttributes(
semconv.RPCServiceKey.String("ssh.Session"),
semconv.RPCMethodKey.String("SendRequest"),
semconv.RPCSystemKey.String("ssh"),
),
)
defer span.End()
s.wrapper.addContext(ctx, request)
output, err := s.Session.Output(cmd)
return output, trace.Wrap(err)
}
// CombinedOutput runs cmd on the remote host and returns its combined
// standard output and standard error.
func (s *Session) CombinedOutput(ctx context.Context, cmd string) ([]byte, error) {
const request = "exec"
config := tracing.NewConfig(s.wrapper.opts)
ctx, span := config.TracerProvider.Tracer(instrumentationName).Start(
ctx,
fmt.Sprintf("ssh.CombinedOutput/%s", cmd),
oteltrace.WithSpanKind(oteltrace.SpanKindClient),
oteltrace.WithAttributes(
semconv.RPCServiceKey.String("ssh.Session"),
semconv.RPCMethodKey.String("SendRequest"),
semconv.RPCSystemKey.String("ssh"),
),
)
defer span.End()
s.wrapper.addContext(ctx, request)
output, err := s.Session.CombinedOutput(cmd)
return output, trace.Wrap(err)
}
// sendFileTransferDecision will send a "file-transfer-decision@goteleport.com" ssh request
func (s *Session) sendFileTransferDecision(ctx context.Context, requestID string, approved bool) error {
req := &FileTransferDecisionReq{
RequestID: requestID,
Approved: approved,
}
_, err := s.SendRequest(ctx, constants.FileTransferDecision, true, ssh.Marshal(req))
return trace.Wrap(err)
}
// ApproveFileTransferRequest sends a "file-transfer-decision@goteleport.com" ssh request
// The ssh request will have the request ID and Approved: true
func (s *Session) ApproveFileTransferRequest(ctx context.Context, requestID string) error {
return trace.Wrap(s.sendFileTransferDecision(ctx, requestID, true))
}
// DenyFileTransferRequest sends a "file-transfer-decision@goteleport.com" ssh request
// The ssh request will have the request ID and Approved: false
func (s *Session) DenyFileTransferRequest(ctx context.Context, requestID string) error {
return trace.Wrap(s.sendFileTransferDecision(ctx, requestID, false))
}
// RequestFileTransfer sends a "file-transfer-request@goteleport.com" ssh request that will create a new file transfer request
// and notify the parties in an ssh session
func (s *Session) RequestFileTransfer(ctx context.Context, req FileTransferReq) error {
_, err := s.SendRequest(ctx, constants.InitiateFileTransfer, true, ssh.Marshal(req))
return trace.Wrap(err)
}