forked from gravitational/teleport
/
database.go
428 lines (363 loc) · 14.2 KB
/
database.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
/*
* 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 (
"context"
"fmt"
"net"
"strconv"
"strings"
"time"
"github.com/google/uuid"
"github.com/gravitational/trace"
apiclient "github.com/gravitational/teleport/api/client"
"github.com/gravitational/teleport/api/client/proto"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/lib/client"
"github.com/gravitational/teleport/lib/client/conntest/database"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/services"
alpn "github.com/gravitational/teleport/lib/srv/alpnproxy/common"
"github.com/gravitational/teleport/lib/srv/db/common/role"
)
// databasePinger describes the required methods to test a Database Connection.
type databasePinger interface {
// Ping tests the connection to the Database with a simple request.
Ping(ctx context.Context, params database.PingParams) error
// IsConnectionRefusedError returns whether the error is referring to a connection refused.
IsConnectionRefusedError(error) bool
// IsInvalidDatabaseUserError returns whether the error is referring to an invalid (non-existent) user.
IsInvalidDatabaseUserError(error) bool
// IsInvalidDatabaseNameError returns whether the error is referring to an invalid (non-existent) database name.
IsInvalidDatabaseNameError(error) bool
}
// ClientDatabaseConnectionTester contains the required auth.ClientI methods to test a Database Connection
type ClientDatabaseConnectionTester interface {
client.ALPNAuthClient
services.ConnectionsDiagnostic
apiclient.GetResourcesClient
}
// DatabaseConnectionTesterConfig defines the config fields for DatabaseConnectionTester.
type DatabaseConnectionTesterConfig struct {
// UserClient is an auth client that has a User's identity.
UserClient ClientDatabaseConnectionTester
// PublicProxyAddr is public address of the proxy
PublicProxyAddr 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
}
// DatabaseConnectionTester implements the ConnectionTester interface for Testing Database access.
type DatabaseConnectionTester struct {
cfg DatabaseConnectionTesterConfig
}
// NewDatabaseConnectionTester returns a new DatabaseConnectionTester
func NewDatabaseConnectionTester(cfg DatabaseConnectionTesterConfig) (*DatabaseConnectionTester, error) {
return &DatabaseConnectionTester{
cfg: cfg,
}, nil
}
// TestConnection tests the access to a database using:
// - auth Client using the User access
// - the resource name
// - database user and database name to connect to
//
// A new ConnectionDiagnostic is created and used to store the traces as it goes through the checkpoints
// To connect to the Database, we will create a cert-key pair and setup a Database client back to Teleport Proxy.
// The following checkpoints are reported:
// - database server for the requested database exists / the user's roles can access it
// - the user can use the requested database user and database name (per their roles)
// - the database is acessible and accepting connections from the database server
// - the database has the database user and database name that was requested
func (s *DatabaseConnectionTester) TestConnection(ctx context.Context, req TestConnectionRequest) (types.ConnectionDiagnostic, error) {
if req.ResourceKind != types.KindDatabase {
return nil, trace.BadParameter("invalid value for ResourceKind, expected %q got %q", types.KindDatabase, 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 to set it to each return statement once an error is returned.
// if the test reaches the end, we force the test to be a success by calling
// connectionDiagnostic.SetMessage(types.DiagnosticMessageSuccess)
// connectionDiagnostic.SetSuccess(true)
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)
}
databaseServers, err := s.getDatabaseServers(ctx, req.ResourceName)
if err != nil {
return nil, trace.Wrap(err)
}
if len(databaseServers) == 0 {
connDiag, err := s.appendDiagnosticTrace(ctx,
connectionDiagnosticID,
types.ConnectionDiagnosticTrace_RBAC_DATABASE,
"Database not found. "+
"Ensure your role grants access by adding it to the 'db_labels' property. "+
"This can also happen when you don't have a Database Agent proxying the database - "+
"you can fix that by adding the database labels to the 'db_service.resources.labels' in 'teleport.yaml' file of the database agent.",
trace.NotFound("%s not found", req.ResourceName),
)
if err != nil {
return nil, trace.Wrap(err)
}
return connDiag, nil
}
databaseServer := databaseServers[0]
routeToDatabase := proto.RouteToDatabase{
ServiceName: databaseServer.GetName(),
Protocol: databaseServer.GetDatabase().GetProtocol(),
Username: req.DatabaseUser,
Database: req.DatabaseName,
}
databasePinger, err := getDatabaseConnTester(routeToDatabase.Protocol)
if err != nil {
return nil, trace.Wrap(err)
}
if err := checkDatabaseLogin(routeToDatabase.Protocol, req.DatabaseUser, req.DatabaseName); err != nil {
return nil, trace.Wrap(err)
}
if _, err := s.appendDiagnosticTrace(ctx,
connectionDiagnosticID,
types.ConnectionDiagnosticTrace_RBAC_DATABASE,
"A Database Agent is available to proxy the connection to the Database.",
nil,
); err != nil {
return nil, trace.Wrap(err)
}
listener, err := s.runALPNTunnel(ctx, req, routeToDatabase, connectionDiagnosticID)
if err != nil {
return nil, trace.Wrap(err)
}
defer listener.Close()
ping, err := newPing(listener.Addr().String(), req.DatabaseUser, req.DatabaseName)
if err != nil {
return nil, trace.Wrap(err)
}
if pingErr := databasePinger.Ping(ctx, ping); pingErr != nil {
connDiag, err := s.handlePingError(ctx, connectionDiagnosticID, pingErr, databasePinger)
return connDiag, trace.Wrap(err)
}
return s.handlePingSuccess(ctx, connectionDiagnosticID)
}
func (s *DatabaseConnectionTester) runALPNTunnel(ctx context.Context, req TestConnectionRequest, routeToDatabase proto.RouteToDatabase, connectionDiagnosticID string) (net.Listener, error) {
list, err := net.Listen("tcp", "localhost:0")
if err != nil {
return nil, trace.Wrap(err)
}
alpnProtocol, err := alpn.ToALPNProtocol(routeToDatabase.Protocol)
if err != nil {
return nil, trace.Wrap(err)
}
mfaResponse, err := req.MFAResponse.GetOptionalMFAResponseProtoReq()
if err != nil {
return nil, trace.Wrap(err)
}
err = client.RunALPNAuthTunnel(ctx, client.ALPNAuthTunnelConfig{
AuthClient: s.cfg.UserClient,
Listener: list,
Protocol: alpnProtocol,
Expires: time.Now().Add(time.Minute).UTC(),
PublicProxyAddr: s.cfg.PublicProxyAddr,
ConnectionDiagnosticID: connectionDiagnosticID,
RouteToDatabase: routeToDatabase,
InsecureSkipVerify: req.InsecureSkipVerify,
MFAResponse: mfaResponse,
})
if err != nil {
return nil, trace.Wrap(err)
}
return list, nil
}
func (s *DatabaseConnectionTester) getDatabaseServers(ctx context.Context, databaseName string) ([]types.DatabaseServer, error) {
// Lookup the Database Server that's proxying the requested Database.
page, err := apiclient.GetResourcePage[types.DatabaseServer](ctx, s.cfg.UserClient, &proto.ListResourcesRequest{
PredicateExpression: fmt.Sprintf(`name == "%s"`, databaseName),
ResourceType: types.KindDatabaseServer,
Limit: defaults.MaxIterationLimit,
})
return page.Resources, trace.Wrap(err)
}
func checkDatabaseLogin(protocol, databaseUser, databaseName string) error {
needUser := role.RequireDatabaseUserMatcher(protocol)
needDatabase := role.RequireDatabaseNameMatcher(protocol)
if needUser && databaseUser == "" {
return trace.BadParameter("missing required parameter Database User")
}
if needDatabase && databaseName == "" {
return trace.BadParameter("missing required parameter Database Name")
}
return nil
}
func newPing(alpnProxyAddr, databaseUser, databaseName string) (database.PingParams, error) {
proxyHost, proxyPortStr, err := net.SplitHostPort(alpnProxyAddr)
if err != nil {
return database.PingParams{}, trace.Wrap(err)
}
proxyPort, err := strconv.Atoi(proxyPortStr)
if err != nil {
return database.PingParams{}, trace.Wrap(err)
}
return database.PingParams{
Host: proxyHost,
Port: proxyPort,
Username: databaseUser,
DatabaseName: databaseName,
}, nil
}
func (s DatabaseConnectionTester) handlePingSuccess(ctx context.Context, connectionDiagnosticID string) (types.ConnectionDiagnostic, error) {
if _, err := s.appendDiagnosticTrace(ctx, connectionDiagnosticID,
types.ConnectionDiagnosticTrace_CONNECTIVITY,
"Database is accessible from the Database Agent.",
nil,
); err != nil {
return nil, trace.Wrap(err)
}
if _, err := s.appendDiagnosticTrace(ctx, connectionDiagnosticID,
types.ConnectionDiagnosticTrace_RBAC_DATABASE_LOGIN,
"Access to Database User and Database Name granted.",
nil,
); err != nil {
return nil, trace.Wrap(err)
}
if _, err := s.appendDiagnosticTrace(ctx, connectionDiagnosticID,
types.ConnectionDiagnosticTrace_DATABASE_DB_USER,
"Database User exists in the Database.",
nil,
); err != nil {
return nil, trace.Wrap(err)
}
connDiag, err := s.appendDiagnosticTrace(ctx, connectionDiagnosticID,
types.ConnectionDiagnosticTrace_DATABASE_DB_NAME,
"Database Name exists in the Database.",
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 errorFromDatabaseService(pingErr error) bool {
// If the requested DB User/Name can't be used per RBAC checks, the Database Agent returns an error which gets here.
if strings.Contains(pingErr.Error(), "access to db denied. User does not have permissions. Confirm database user and name.") {
return true
}
// When there's an error when trying to use RDS IAM auth.
if strings.Contains(pingErr.Error(), "FATAL: PAM authentication failed for user") &&
strings.Contains(pingErr.Error(), "IAM policy") {
return true
}
return false
}
func (s DatabaseConnectionTester) handlePingError(ctx context.Context, connectionDiagnosticID string, pingErr error, databasePinger databasePinger) (types.ConnectionDiagnostic, error) {
// The Database Agent (lib/srv/db/server.go) might add an trace in some cases.
// Here, it must be ignored to prevent multiple failed traces.
if errorFromDatabaseService(pingErr) {
connDiag, err := s.cfg.UserClient.GetConnectionDiagnostic(ctx, connectionDiagnosticID)
if err != nil {
return nil, trace.Wrap(err)
}
return connDiag, nil
}
if databasePinger.IsConnectionRefusedError(pingErr) {
connDiag, err := s.appendDiagnosticTrace(ctx,
connectionDiagnosticID,
types.ConnectionDiagnosticTrace_CONNECTIVITY,
"There was a connection problem between the Database Agent and the Database. "+
"Ensure the Database is running and accessible from the Database Agent.",
pingErr,
)
if err != nil {
return nil, trace.Wrap(err)
}
return connDiag, nil
}
// Requested DB User is allowed per RBAC rules, but those entities don't exist in the Database itself.
if databasePinger.IsInvalidDatabaseUserError(pingErr) {
connDiag, err := s.appendDiagnosticTrace(ctx,
connectionDiagnosticID,
types.ConnectionDiagnosticTrace_DATABASE_DB_USER,
"The Database rejected the provided Database User. Ensure that the database user exists.",
pingErr,
)
if err != nil {
return nil, trace.Wrap(err)
}
return connDiag, nil
}
// Requested DB Name is allowed per RBAC rules, but those entities don't exist in the Database itself.
if databasePinger.IsInvalidDatabaseNameError(pingErr) {
connDiag, err := s.appendDiagnosticTrace(ctx,
connectionDiagnosticID,
types.ConnectionDiagnosticTrace_DATABASE_DB_NAME,
"The Database rejected the provided Database Name. Ensure that the database name exists.",
pingErr,
)
if err != nil {
return nil, trace.Wrap(err)
}
return connDiag, nil
}
connDiag, err := s.appendDiagnosticTrace(ctx,
connectionDiagnosticID,
types.ConnectionDiagnosticTrace_UNKNOWN_ERROR,
fmt.Sprintf("Unknown error. %v", pingErr),
pingErr,
)
if err != nil {
return nil, trace.Wrap(err)
}
return connDiag, nil
}
func (s DatabaseConnectionTester) appendDiagnosticTrace(ctx context.Context, connectionDiagnosticID string, traceType types.ConnectionDiagnosticTrace_TraceType, message string, err error) (types.ConnectionDiagnostic, error) {
connDiag, err := s.cfg.UserClient.AppendDiagnosticTrace(
ctx,
connectionDiagnosticID,
types.NewTraceDiagnosticConnection(
traceType,
message,
err,
))
if err != nil {
return nil, trace.Wrap(err)
}
return connDiag, nil
}
func getDatabaseConnTester(protocol string) (databasePinger, error) {
switch protocol {
case defaults.ProtocolPostgres:
return &database.PostgresPinger{}, nil
case defaults.ProtocolMySQL:
return &database.MySQLPinger{}, nil
case defaults.ProtocolSQLServer:
return &database.SQLServerPinger{}, nil
}
return nil, trace.NotImplemented("database protocol %q is not supported yet for testing connection", protocol)
}