forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh.go
345 lines (293 loc) · 12.3 KB
/
ssh.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
/*
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package conntest
import (
"bytes"
"context"
"fmt"
"io"
"strings"
"time"
"github.com/google/uuid"
"github.com/gravitational/trace"
"golang.org/x/crypto/ssh"
"github.com/gravitational/teleport/api/client/proto"
"github.com/gravitational/teleport/api/constants"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/api/utils/sshutils"
"github.com/gravitational/teleport/lib/auth"
"github.com/gravitational/teleport/lib/client"
"github.com/gravitational/teleport/lib/connectmycomputer"
libsshutils "github.com/gravitational/teleport/lib/sshutils"
)
// SSHConnectionTesterConfig has the necessary fields to create a new SSHConnectionTester.
type SSHConnectionTesterConfig struct {
// UserClient is an auth client that has a User's identity.
// This is the user that is running the SSH Connection Test.
UserClient auth.ClientI
// ProxyHostPort is the proxy to use in the `--proxy` format (host:webPort,sshPort)
ProxyHostPort string
// TLSRoutingEnabled indicates that proxy supports ALPN SNI server where
// all proxy services are exposed on a single TLS listener (Proxy Web Listener).
TLSRoutingEnabled bool
}
// SSHConnectionTester implements the ConnectionTester interface for Testing SSH access
type SSHConnectionTester struct {
cfg SSHConnectionTesterConfig
webProxyAddr string
sshProxyAddr string
}
// NewSSHConnectionTester creates a new SSHConnectionTester
func NewSSHConnectionTester(cfg SSHConnectionTesterConfig) (*SSHConnectionTester, error) {
parsedProxyHostAddr, err := client.ParseProxyHost(cfg.ProxyHostPort)
if err != nil {
return nil, trace.Wrap(err)
}
return &SSHConnectionTester{
cfg: cfg,
webProxyAddr: parsedProxyHostAddr.WebProxyAddr,
sshProxyAddr: parsedProxyHostAddr.SSHProxyAddr,
}, nil
}
// TestConnection tests an SSH Connection to the target Node using
// - the provided client
// - resource name
// - principal / linux user
//
// A new ConnectionDiagnostic is created and used to store the traces as it goes through the checkpoints
// To set up the SSH client, it will generate a new cert and inject the ConnectionDiagnosticID
// - add a trace of whether the SSH Node was reachable
// - SSH Node receives the cert and extracts the ConnectionDiagnostiID
// - the SSH Node will append a trace indicating if the has access (RBAC)
// - the SSH Node will append a trace indicating if the requested principal is valid for the target Node
func (s *SSHConnectionTester) TestConnection(ctx context.Context, req TestConnectionRequest) (types.ConnectionDiagnostic, error) {
if req.ResourceKind != types.KindNode {
return nil, trace.BadParameter("invalid value for ResourceKind, expected %q got %q", types.KindNode, req.ResourceKind)
}
connectionDiagnosticID := uuid.NewString()
connectionDiagnostic, err := types.NewConnectionDiagnosticV1(connectionDiagnosticID, map[string]string{},
types.ConnectionDiagnosticSpecV1{
// We start with a failed state so that we don't need an extra update when returning non-happy paths.
// For the happy path, we do update the Message to Success.
Message: types.DiagnosticMessageFailed,
})
if err != nil {
return nil, trace.Wrap(err)
}
if err := s.cfg.UserClient.CreateConnectionDiagnostic(ctx, connectionDiagnostic); err != nil {
return nil, trace.Wrap(err)
}
key, err := client.GenerateRSAKey()
if err != nil {
return nil, trace.Wrap(err)
}
currentUser, err := s.cfg.UserClient.GetCurrentUser(ctx)
if err != nil {
return nil, trace.Wrap(err)
}
mfaResponse, err := req.MFAResponse.GetOptionalMFAResponseProtoReq()
if err != nil {
return nil, trace.Wrap(err)
}
certs, err := s.cfg.UserClient.GenerateUserCerts(ctx, proto.UserCertsRequest{
PublicKey: key.MarshalSSHPublicKey(),
Username: currentUser.GetName(),
Expires: time.Now().Add(time.Minute).UTC(),
ConnectionDiagnosticID: connectionDiagnosticID,
MFAResponse: mfaResponse,
})
if err != nil {
return nil, trace.Wrap(err)
}
key.Cert = certs.SSH
key.TLSCert = certs.TLS
certAuths, err := s.cfg.UserClient.GetCertAuthorities(ctx, types.HostCA, false /* loadKeys */)
if err != nil {
return nil, trace.Wrap(err)
}
hostkeyCallback, err := hostKeyCallbackFromCAs(certAuths)
if err != nil {
return nil, trace.Wrap(err)
}
key.TrustedCerts = auth.AuthoritiesToTrustedCerts(certAuths)
keyAuthMethod, err := key.AsAuthMethod()
if err != nil {
return nil, trace.Wrap(err)
}
clusterName, err := s.cfg.UserClient.GetClusterName()
if err != nil {
return nil, trace.Wrap(err)
}
clientConfTLS, err := key.TeleportClientTLSConfig(nil, []string{clusterName.GetClusterName()})
if err != nil {
return nil, trace.Wrap(err)
}
key.KeyIndex = client.KeyIndex{
Username: req.SSHPrincipal,
ProxyHost: s.webProxyAddr,
ClusterName: clusterName.GetClusterName(),
}
processStdout := &bytes.Buffer{}
clientConf := client.MakeDefaultConfig()
clientConf.AddKeysToAgent = client.AddKeysToAgentNo
clientConf.AuthMethods = []ssh.AuthMethod{keyAuthMethod}
clientConf.Host = req.ResourceName
clientConf.HostKeyCallback = hostkeyCallback
clientConf.HostLogin = req.SSHPrincipal
clientConf.NonInteractive = true
clientConf.SSHProxyAddr = s.sshProxyAddr
clientConf.Stderr = io.Discard
clientConf.Stdin = &bytes.Buffer{}
clientConf.Stdout = processStdout
clientConf.TLS = clientConfTLS
clientConf.TLSRoutingEnabled = s.cfg.TLSRoutingEnabled
clientConf.Username = currentUser.GetName()
clientConf.WebProxyAddr = s.webProxyAddr
clientConf.SiteName = clusterName.GetClusterName()
tc, err := client.NewClient(clientConf)
if err != nil {
return nil, trace.Wrap(err)
}
ctxWithTimeout, cancelFunc := context.WithTimeout(ctx, req.DialTimeout)
defer cancelFunc()
if err := tc.SSH(ctxWithTimeout, []string{"whoami"}, false); err != nil {
return s.handleErrFromSSH(ctx, connectionDiagnosticID, req.SSHPrincipal, err, processStdout, currentUser, req)
}
connDiag, err := s.cfg.UserClient.AppendDiagnosticTrace(ctx, connectionDiagnosticID, types.NewTraceDiagnosticConnection(
types.ConnectionDiagnosticTrace_NODE_PRINCIPAL,
fmt.Sprintf("%q user exists in target node", req.SSHPrincipal),
nil,
))
if err != nil {
return nil, trace.Wrap(err)
}
connDiag.SetMessage(types.DiagnosticMessageSuccess)
connDiag.SetSuccess(true)
if err := s.cfg.UserClient.UpdateConnectionDiagnostic(ctx, connDiag); err != nil {
return nil, trace.Wrap(err)
}
return connDiag, nil
}
func (s SSHConnectionTester) handleErrFromSSH(ctx context.Context, connectionDiagnosticID string,
sshPrincipal string, sshError error, processStdout *bytes.Buffer, currentUser types.User, req TestConnectionRequest) (types.ConnectionDiagnostic, error) {
isConnectMyComputerNode := req.SSHNodeSetupMethod == SSHNodeSetupMethodConnectMyComputer
if trace.IsConnectionProblem(sshError) {
var statusCommand string
if req.SSHNodeOS == constants.DarwinOS {
statusCommand = "launchctl print 'system/Teleport Service'"
} else {
statusCommand = "systemctl status teleport"
}
message := fmt.Sprintf(`Failed to connect to the Node. Ensure teleport service is running using "%s".`, statusCommand)
if isConnectMyComputerNode {
message = "Failed to connect to the Node. Open the Connect My Computer tab in Teleport Connect and make sure that the agent is running."
}
connDiag, err := s.cfg.UserClient.AppendDiagnosticTrace(ctx, connectionDiagnosticID, types.NewTraceDiagnosticConnection(
types.ConnectionDiagnosticTrace_CONNECTIVITY,
message,
sshError,
))
if err != nil {
return nil, trace.Wrap(err)
}
return connDiag, nil
}
processStdoutString := strings.TrimSpace(processStdout.String())
// If the selected principal does not exist on the node, attempting to connect emits:
// "Failed to launch: user: lookup username <principal>: no such file or directory."
isUsernameLookupFail := strings.HasPrefix(processStdoutString, "Failed to launch: user:")
// Connect My Computer runs the agent as non-root. When attempting to connect as another system
// user that is not the same as the user who runs the agent, the emitted error is "Failed to
// launch: fork/exec <conn.User shell>: operation not permitted."
isForkExecOperationNotPermitted := strings.HasPrefix(processStdoutString, "Failed to launch: fork/exec") &&
strings.Contains(processStdoutString, "operation not permitted")
// "operation not permitted" is handled only for the Connect My Computer case as we assume that
// regular SSH nodes are started as root and are unlikely to run into this error.
isInvalidNodePrincipal := isUsernameLookupFail || (isConnectMyComputerNode && isForkExecOperationNotPermitted)
if isInvalidNodePrincipal {
message := fmt.Sprintf("Invalid user. Please ensure the principal %q is a valid login in the target node. Output from Node: %v",
sshPrincipal, processStdoutString)
if isConnectMyComputerNode {
connectMyComputerRoleName := connectmycomputer.GetRoleNameForUser(currentUser.GetName())
message = "Invalid user."
outputFromAgent := fmt.Sprintf("Output from the Connect My Computer agent: %v", processStdoutString)
retrySetupInstructions := "reload this page, pick Connect My Computer again, then in Teleport Connect " +
"remove the Connect My Computer agent and start Connect My Computer setup again."
var detailedMessage string
if req.SSHPrincipalSelectionMode == SSHPrincipalSelectionModeManual {
detailedMessage = "You probably picked a login which does not match the system user " +
"that is running Teleport Connect. Pick the correct login and try again.\n\n" +
"If the list of logins does not include the correct login for this node, " +
retrySetupInstructions + "\n\n" + outputFromAgent
} else {
detailedMessage = fmt.Sprintf("The role %q includes only the login %q and %q is not a valid principal for this node. ",
connectMyComputerRoleName, sshPrincipal, sshPrincipal) +
"To fix this problem, " + retrySetupInstructions + "\n\n" + outputFromAgent
}
// The wrapping here is done so that the detailed message will be shown under "Show details"
// and not as one of the main points of the connection test.
sshError = trace.Wrap(sshError, detailedMessage)
}
connDiag, err := s.cfg.UserClient.AppendDiagnosticTrace(ctx, connectionDiagnosticID, types.NewTraceDiagnosticConnection(
types.ConnectionDiagnosticTrace_NODE_PRINCIPAL,
message,
sshError,
))
if err != nil {
return nil, trace.Wrap(err)
}
return connDiag, nil
}
// This happens when the principal is not part of the allowed ones.
// A trace was already added by the Node and, here, we just return the diagnostic.
if trace.IsAccessDenied(sshError) {
connDiag, err := s.cfg.UserClient.GetConnectionDiagnostic(ctx, connectionDiagnosticID)
if err != nil {
return nil, trace.Wrap(err)
}
return connDiag, nil
}
connDiag, err := s.cfg.UserClient.AppendDiagnosticTrace(ctx, connectionDiagnosticID, types.NewTraceDiagnosticConnection(
types.ConnectionDiagnosticTrace_UNKNOWN_ERROR,
fmt.Sprintf("Unknown error. %s", processStdoutString),
sshError,
))
if err != nil {
return nil, trace.Wrap(err)
}
return connDiag, nil
}
func hostKeyCallbackFromCAs(certAuths []types.CertAuthority) (ssh.HostKeyCallback, error) {
var certPublicKeys []ssh.PublicKey
for _, ca := range certAuths {
caCheckers, err := libsshutils.GetCheckers(ca)
if err != nil {
return nil, trace.Wrap(err)
}
certPublicKeys = append(certPublicKeys, caCheckers...)
}
hostKeyCallback, err := sshutils.NewHostKeyCallback(sshutils.HostKeyCallbackConfig{
GetHostCheckers: func() ([]ssh.PublicKey, error) {
return certPublicKeys, nil
},
})
if err != nil {
return nil, trace.Wrap(err)
}
return hostKeyCallback, nil
}