-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
access_test.go
1395 lines (1270 loc) · 44.3 KB
/
access_test.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2020-2021 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 db
import (
"context"
"crypto/tls"
"fmt"
"net"
"os"
"sort"
"testing"
"time"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/api/constants"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/lib/auth"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/fixtures"
"github.com/gravitational/teleport/lib/limiter"
"github.com/gravitational/teleport/lib/modules"
"github.com/gravitational/teleport/lib/multiplexer"
"github.com/gravitational/teleport/lib/reversetunnel"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/srv/db/cloud"
"github.com/gravitational/teleport/lib/srv/db/common"
"github.com/gravitational/teleport/lib/srv/db/mongodb"
"github.com/gravitational/teleport/lib/srv/db/mysql"
"github.com/gravitational/teleport/lib/srv/db/postgres"
"github.com/gravitational/teleport/lib/tlsca"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/trace"
"github.com/jackc/pgconn"
"github.com/jonboulle/clockwork"
"github.com/pborman/uuid"
"github.com/siddontang/go-mysql/client"
mysqllib "github.com/siddontang/go-mysql/mysql"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/mongo/driver/wiremessage"
)
func TestMain(m *testing.M) {
utils.InitLoggerForTests()
os.Exit(m.Run())
}
// TestAccessPostgres verifies access scenarios to a Postgres database based
// on the configured RBAC rules.
func TestAccessPostgres(t *testing.T) {
ctx := context.Background()
testCtx := setupTestContext(ctx, t, withSelfHostedPostgres("postgres"))
go testCtx.startHandlingConnections()
tests := []struct {
desc string
user string
role string
allowDbNames []string
allowDbUsers []string
dbName string
dbUser string
err string
}{
{
desc: "has access to all database names and users",
user: "alice",
role: "admin",
allowDbNames: []string{types.Wildcard},
allowDbUsers: []string{types.Wildcard},
dbName: "postgres",
dbUser: "postgres",
},
{
desc: "has access to nothing",
user: "alice",
role: "admin",
allowDbNames: []string{},
allowDbUsers: []string{},
dbName: "postgres",
dbUser: "postgres",
err: "access to db denied",
},
{
desc: "no access to databases",
user: "alice",
role: "admin",
allowDbNames: []string{},
allowDbUsers: []string{types.Wildcard},
dbName: "postgres",
dbUser: "postgres",
err: "access to db denied",
},
{
desc: "no access to users",
user: "alice",
role: "admin",
allowDbNames: []string{types.Wildcard},
allowDbUsers: []string{},
dbName: "postgres",
dbUser: "postgres",
err: "access to db denied",
},
{
desc: "access allowed to specific user/database",
user: "alice",
role: "admin",
allowDbNames: []string{"metrics"},
allowDbUsers: []string{"alice"},
dbName: "metrics",
dbUser: "alice",
},
{
desc: "access denied to specific user/database",
user: "alice",
role: "admin",
allowDbNames: []string{"metrics"},
allowDbUsers: []string{"alice"},
dbName: "postgres",
dbUser: "postgres",
err: "access to db denied",
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
// Create user/role with the requested permissions.
testCtx.createUserAndRole(ctx, t, test.user, test.role, test.allowDbUsers, test.allowDbNames)
// Try to connect to the database as this user.
pgConn, err := testCtx.postgresClient(ctx, test.user, "postgres", test.dbUser, test.dbName)
if test.err != "" {
require.Error(t, err)
require.Contains(t, err.Error(), test.err)
return
}
require.NoError(t, err)
// Execute a query.
result, err := pgConn.Exec(ctx, "select 1").ReadAll()
require.NoError(t, err)
require.Equal(t, []*pgconn.Result{postgres.TestQueryResponse}, result)
// Disconnect.
err = pgConn.Close(ctx)
require.NoError(t, err)
})
}
}
// TestAccessMySQL verifies access scenarios to a MySQL database based
// on the configured RBAC rules.
func TestAccessMySQL(t *testing.T) {
ctx := context.Background()
testCtx := setupTestContext(ctx, t, withSelfHostedMySQL("mysql"))
go testCtx.startHandlingConnections()
tests := []struct {
// desc is the test case description.
desc string
// user is the Teleport local user name the test will use.
user string
// role is the Teleport role name to create and assign to the user.
role string
// allowDbUsers is the role's list of allowed database users.
allowDbUsers []string
// dbUser is the database user to simulate connect as.
dbUser string
// err is the expected test case error.
err string
}{
{
desc: "has access to all database users",
user: "alice",
role: "admin",
allowDbUsers: []string{types.Wildcard},
dbUser: "root",
},
{
desc: "has access to nothing",
user: "alice",
role: "admin",
allowDbUsers: []string{},
dbUser: "root",
err: "access to db denied",
},
{
desc: "access allowed to specific user",
user: "alice",
role: "admin",
allowDbUsers: []string{"alice"},
dbUser: "alice",
},
{
desc: "access denied to specific user",
user: "alice",
role: "admin",
allowDbUsers: []string{"alice"},
dbUser: "root",
err: "access to db denied",
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
// Create user/role with the requested permissions.
testCtx.createUserAndRole(ctx, t, test.user, test.role, test.allowDbUsers, []string{types.Wildcard})
// Try to connect to the database as this user.
mysqlConn, err := testCtx.mysqlClient(test.user, "mysql", test.dbUser)
if test.err != "" {
require.Error(t, err)
require.Contains(t, err.Error(), test.err)
return
}
require.NoError(t, err)
// Execute a query.
result, err := mysqlConn.Execute("select 1")
require.NoError(t, err)
require.Equal(t, mysql.TestQueryResponse, result)
// Disconnect.
err = mysqlConn.Close()
require.NoError(t, err)
})
}
}
// TestMySQLBadHandshake verifies MySQL proxy can gracefully handle truncated
// client handshake messages.
func TestMySQLBadHandshake(t *testing.T) {
ctx := context.Background()
testCtx := setupTestContext(ctx, t, withSelfHostedMySQL("mysql"))
go testCtx.startHandlingConnections()
tests := []struct {
name string
data []byte
}{
{
name: "short user name",
data: []byte{0x8d, 0xae, 0xff, 0x49, 0x0, 0x0, 0x0, 0x1, 0x2d, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x61, 0x6c, 0x69, 0x63, 0x65},
},
{
name: "short db name",
data: []byte{0x8d, 0xae, 0xff, 0x49, 0x0, 0x0, 0x0, 0x1, 0x2d, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x61, 0x6c, 0x69, 0x63, 0x65, 0x0, 0x14, 0xce, 0x7, 0x50, 0x5d, 0x8c, 0xca, 0x17, 0xda, 0x1b, 0x60, 0xea, 0x9d, 0xa9, 0xc4, 0x7d, 0x83, 0x85, 0xa8, 0x7a, 0x96, 0x71, 0x77, 0x65, 0x31, 0x32},
},
{
name: "short plugin name",
data: []byte{0x8d, 0xae, 0xff, 0x49, 0x0, 0x0, 0x0, 0x1, 0x2d, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x61, 0x6c, 0x69, 0x63, 0x65, 0x0, 0x14, 0xce, 0x7, 0x50, 0x5d, 0x8c, 0xca, 0x17, 0xda, 0x1b, 0x60, 0xea, 0x9d, 0xa9, 0xc4, 0x7d, 0x83, 0x85, 0xa8, 0x7a, 0x96, 0x71, 0x77, 0x65, 0x31, 0x32, 0x33, 0x0, 0x6d, 0x79, 0x73, 0x71, 0x6c, 0x5f, 0x6e, 0x61, 0x74, 0x69, 0x76, 0x65, 0x5f, 0x70, 0x61, 0x73, 0x73},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// Connect to MySQL proxy endpoint.
conn, err := net.Dial("tcp", testCtx.mysqlListener.Addr().String())
require.NoError(t, err)
// Read initial handshake message.
bytes := make([]byte, 1024)
_, err = conn.Read(bytes)
require.NoError(t, err)
// Prepend header to the packet data.
packet := append([]byte{
byte(len(test.data)),
byte(len(test.data) >> 8),
byte(len(test.data) >> 16),
0x1,
}, test.data...)
// Write handshake response packet.
_, err = conn.Write(packet)
require.NoError(t, err)
err = conn.Close()
require.NoError(t, err)
})
}
}
// TestAccessMySQLChangeUser verifies that COM_CHANGE_USER command is rejected.
func TestAccessMySQLChangeUser(t *testing.T) {
ctx := context.Background()
testCtx := setupTestContext(ctx, t, withSelfHostedMySQL("mysql"))
go testCtx.startHandlingConnections()
// Create user/role with the requested permissions.
testCtx.createUserAndRole(ctx, t, "alice", "admin", []string{"alice"}, []string{types.Wildcard})
// Connect to the database as this user.
mysqlConn, err := testCtx.mysqlClient("alice", "mysql", "alice")
require.NoError(t, err)
// Send COM_CHANGE_USER command. The driver doesn't support it natively so
// assemble the raw packet and send it which should be enough to test the
// rejection logic.
packet := []byte{
0x05, // Payload length.
0x00, // Payload length cont'd.
0x00, // Payload length cont'd.
0x00, // Sequence number.
mysqllib.COM_CHANGE_USER, // Command type.
'b', // Null-terminated string with new user name.
'o',
'b',
0x00,
// There would've been other fields in "real" packet but these will
// do for the test to detect the command.
}
err = mysqlConn.WritePacket(packet)
require.NoError(t, err)
// Connection should've been closed so any attempt to use it should fail.
_, err = mysqlConn.Execute("select 1")
require.Error(t, err)
}
// TestAccessMySQLServerPacket verifies some edge-cases related to reading
// wire packets sent by the MySQL server.
func TestAccessMySQLServerPacket(t *testing.T) {
ctx := context.Background()
testCtx := setupTestContext(ctx, t, withSelfHostedMySQL("mysql"))
go testCtx.startHandlingConnections()
// Create user/role with access permissions.
testCtx.createUserAndRole(ctx, t, "alice", "admin", []string{"alice"}, []string{types.Wildcard})
// Connect to the database as this user.
mysqlConn, err := testCtx.mysqlClient("alice", "mysql", "alice")
require.NoError(t, err)
// Execute "show tables" command which will make the test server to reply
// in a way that previously would cause our packet parsing logic to fail.
_, err = mysqlConn.Execute("show tables")
require.NoError(t, err)
}
// TestAccessMongoDB verifies access scenarios to a MongoDB database based
// on the configured RBAC rules.
func TestAccessMongoDB(t *testing.T) {
ctx := context.Background()
tests := []struct {
desc string
user string
role string
allowDbNames []string
allowDbUsers []string
dbName string
dbUser string
connectErr string
queryErr string
}{
{
desc: "has access to all database names and users",
user: "alice",
role: "admin",
allowDbNames: []string{types.Wildcard},
allowDbUsers: []string{types.Wildcard},
dbUser: "admin",
dbName: "admin",
connectErr: "",
queryErr: "",
},
{
desc: "has access to nothing",
user: "alice",
role: "admin",
allowDbNames: []string{},
allowDbUsers: []string{},
dbName: "admin",
dbUser: "admin",
connectErr: "access to db denied",
queryErr: "",
},
{
desc: "no access to databases",
user: "alice",
role: "admin",
allowDbNames: []string{""},
allowDbUsers: []string{types.Wildcard},
dbName: "admin",
dbUser: "admin",
connectErr: "access to db denied",
queryErr: "",
},
{
desc: "no access to users",
user: "alice",
role: "admin",
allowDbNames: []string{types.Wildcard},
allowDbUsers: []string{},
dbName: "admin",
dbUser: "admin",
connectErr: "access to db denied",
queryErr: "",
},
{
desc: "access allowed to specific user and database",
user: "alice",
role: "admin",
allowDbNames: []string{"admin"},
allowDbUsers: []string{"alice"},
dbName: "admin",
dbUser: "alice",
connectErr: "",
queryErr: "",
},
{
desc: "access denied to specific user and database",
user: "alice",
role: "admin",
allowDbNames: []string{"admin"},
allowDbUsers: []string{"alice"},
dbName: "metrics",
dbUser: "alice",
connectErr: "",
queryErr: "access to db denied",
},
}
// Each scenario is executed multiple times with different server/client
// options to test things like legacy MongoDB servers and clients that
// use compression.
serverOpts := []struct {
name string
opts []mongodb.TestServerOption
}{
{
name: "new server",
opts: []mongodb.TestServerOption{},
},
{
name: "old server",
opts: []mongodb.TestServerOption{
mongodb.TestServerWireVersion(wiremessage.OpmsgWireVersion - 1),
},
},
}
clientOpts := []struct {
name string
opts *options.ClientOptions
}{
{
name: "client without compression",
opts: options.Client(),
},
{
name: "client with compression",
opts: options.Client().SetCompressors([]string{"zlib"}),
},
}
// Execute each scenario on both modern and legacy Mongo servers
// to make sure legacy messages are also subject to RBAC.
for _, test := range tests {
for _, serverOpt := range serverOpts {
for _, clientOpt := range clientOpts {
t.Run(fmt.Sprintf("%v/%v/%v", serverOpt.name, clientOpt.name, test.desc), func(t *testing.T) {
testCtx := setupTestContext(ctx, t, withSelfHostedMongo("mongo", serverOpt.opts...))
go testCtx.startHandlingConnections()
// Create user/role with the requested permissions.
testCtx.createUserAndRole(ctx, t, test.user, test.role, test.allowDbUsers, test.allowDbNames)
// Try to connect to the database as this user.
client, err := testCtx.mongoClient(ctx, test.user, "mongo", test.dbUser, clientOpt.opts)
defer func() {
if client != nil {
client.Disconnect(ctx)
}
}()
if test.connectErr != "" {
require.Error(t, err)
require.Contains(t, err.Error(), test.connectErr)
return
}
require.NoError(t, err)
// Execute a "find" command. Collection name doesn't matter currently.
_, err = client.Database(test.dbName).Collection("test").Find(ctx, bson.M{})
if test.queryErr != "" {
require.Error(t, err)
require.Contains(t, err.Error(), test.queryErr)
return
}
require.NoError(t, err)
})
}
}
}
}
type testModules struct {
modules.Modules
}
func (m *testModules) Features() modules.Features {
return modules.Features{
DB: false, // Explicitly turn off database access.
}
}
// TestAccessDisabled makes sure database access can be disabled via modules.
func TestAccessDisabled(t *testing.T) {
defaultModules := modules.GetModules()
defer modules.SetModules(defaultModules)
modules.SetModules(&testModules{})
ctx := context.Background()
testCtx := setupTestContext(ctx, t, withSelfHostedPostgres("postgres"))
go testCtx.startHandlingConnections()
userName := "alice"
roleName := "admin"
dbUser := "postgres"
dbName := "postgres"
// Create user/role with the requested permissions.
testCtx.createUserAndRole(ctx, t, userName, roleName, []string{types.Wildcard}, []string{types.Wildcard})
// Try to connect to the database as this user.
_, err := testCtx.postgresClient(ctx, userName, "postgres", dbUser, dbName)
require.Error(t, err)
require.Contains(t, err.Error(), "this Teleport cluster is not licensed for database access")
}
// TestPostgresInjectionDatabase makes sure Postgres connection is not
// susceptible to malicious database name injections.
func TestPostgresInjectionDatabase(t *testing.T) {
ctx := context.Background()
testCtx := setupTestContext(ctx, t, withSelfHostedPostgres("postgres"))
go testCtx.startHandlingConnections()
postgresServer := testCtx.postgres["postgres"].db
// Make sure the role allows wildcard database users and names.
testCtx.createUserAndRole(ctx, t, "alice", "admin", []string{types.Wildcard}, []string{types.Wildcard})
// Connect and make sure connection parameters are as expected.
psql, err := testCtx.postgresClient(ctx, "alice", "postgres", "alice", "test&user=bob")
require.NoError(t, err)
select {
case p := <-postgresServer.ParametersCh():
require.Equal(t, map[string]string{"user": "alice", "database": "test&user=bob"}, p)
case <-time.After(time.Second):
t.Fatal("didn't receive startup message parameters after 1s")
}
err = psql.Close(ctx)
require.NoError(t, err)
}
// TestPostgresInjectionUser makes sure Postgres connection is not
// susceptible to malicious user name injections.
func TestPostgresInjectionUser(t *testing.T) {
ctx := context.Background()
testCtx := setupTestContext(ctx, t, withSelfHostedPostgres("postgres"))
go testCtx.startHandlingConnections()
postgresServer := testCtx.postgres["postgres"].db
// Make sure the role allows wildcard database users and names.
testCtx.createUserAndRole(ctx, t, "alice", "admin", []string{types.Wildcard}, []string{types.Wildcard})
// Construct malicious username that simulates the connection string.
user := fmt.Sprintf("alice@localhost:%v?database=prod&foo=", postgresServer.Port())
// Connect and make sure startup parameters are as expected.
psql, err := testCtx.postgresClient(ctx, "alice", "postgres", user, "test")
require.NoError(t, err)
select {
case p := <-postgresServer.ParametersCh():
require.Equal(t, map[string]string{"user": user, "database": "test"}, p)
case <-time.After(time.Second):
t.Fatal("didn't receive startup message parameters after 1s")
}
err = psql.Close(ctx)
require.NoError(t, err)
}
// TestCompatibilityWithOldAgents verifies that older database agents where
// each database was represented as a DatabaseServer are supported.
//
// DELETE IN 9.0.
func TestCompatibilityWithOldAgents(t *testing.T) {
ctx := context.Background()
testCtx := setupTestContext(ctx, t)
go testCtx.startProxy()
postgresServer, err := postgres.NewTestServer(common.TestServerConfig{
Name: "postgres",
AuthClient: testCtx.authClient,
})
require.NoError(t, err)
go postgresServer.Serve()
t.Cleanup(func() { postgresServer.Close() })
database, err := types.NewDatabaseV3(types.Metadata{
Name: "postgres",
}, types.DatabaseSpecV3{
Protocol: defaults.ProtocolPostgres,
URI: net.JoinHostPort("localhost", postgresServer.Port()),
})
require.NoError(t, err)
databaseServer := testCtx.setupDatabaseServer(ctx, t, agentParams{
Databases: []types.Database{database},
GetServerInfoFn: func(database types.Database) func() (types.Resource, error) {
return func() (types.Resource, error) {
return types.NewDatabaseServerV3(types.Metadata{
Name: database.GetName(),
}, types.DatabaseServerSpecV3{
Protocol: database.GetProtocol(),
URI: database.GetURI(),
HostID: testCtx.hostID,
Hostname: constants.APIDomain,
})
}
},
})
go func() {
for conn := range testCtx.proxyConn {
go databaseServer.HandleConnection(conn)
}
}()
testCtx.createUserAndRole(ctx, t, "alice", "admin", []string{"postgres"}, []string{"postgres"})
// Make sure we can connect successfully.
psql, err := testCtx.postgresClient(ctx, "alice", "postgres", "postgres", "postgres")
require.NoError(t, err)
err = psql.Close(ctx)
require.NoError(t, err)
}
type testContext struct {
hostID string
clusterName string
tlsServer *auth.TestTLSServer
authServer *auth.Server
authClient *auth.Client
proxyServer *ProxyServer
mux *multiplexer.Mux
mysqlListener net.Listener
webListener *multiplexer.WebListener
proxyConn chan net.Conn
fakeRemoteSite *reversetunnel.FakeRemoteSite
server *Server
emitter *testEmitter
hostCA types.CertAuthority
// postgres is a collection of Postgres databases the test uses.
postgres map[string]testPostgres
// mysql is a collection of MySQL databases the test uses.
mysql map[string]testMySQL
// mongo is a collection of MongoDB databases the test uses.
mongo map[string]testMongoDB
// clock to override clock in tests.
clock clockwork.FakeClock
}
// testPostgres represents a single proxied Postgres database.
type testPostgres struct {
// db is the test Postgres database server.
db *postgres.TestServer
// resource is the resource representing this Postgres database.
resource types.Database
}
// testMySQL represents a single proxied MySQL database.
type testMySQL struct {
// db is the test MySQL database server.
db *mysql.TestServer
// resource is the resource representing this MySQL database.
resource types.Database
}
// testMongoDB represents a single proxied MongoDB database.
type testMongoDB struct {
// db is the test MongoDB database server.
db *mongodb.TestServer
// resource is the resource representing this MongoDB database.
resource types.Database
}
// startProxy starts all proxy services required to handle connections.
func (c *testContext) startProxy() {
// Start multiplexer.
go c.mux.Serve()
// Start TLS multiplexer.
go c.webListener.Serve()
// Start database proxy server.
go c.proxyServer.ServePostgres(c.mux.DB())
// Start MySQL proxy server.
go c.proxyServer.ServeMySQL(c.mysqlListener)
// Start database TLS proxy server.
go c.proxyServer.ServeTLS(c.webListener.DB())
}
// startHandlingConnections starts all services required to handle database
// client connections: multiplexer, proxy server Postgres/MySQL listeners
// and the database service agent.
func (c *testContext) startHandlingConnections() {
// Start all proxy services.
c.startProxy()
// Start handling database client connections on the database server.
for conn := range c.proxyConn {
go c.server.HandleConnection(conn)
}
}
// postgresClient connects to test Postgres through database access as a
// specified Teleport user and database account.
func (c *testContext) postgresClient(ctx context.Context, teleportUser, dbService, dbUser, dbName string) (*pgconn.PgConn, error) {
return c.postgresClientWithAddr(ctx, c.mux.DB().Addr().String(), teleportUser, dbService, dbUser, dbName)
}
// postgresClientWithAddr is like postgresClient but allows to override connection address.
func (c *testContext) postgresClientWithAddr(ctx context.Context, address, teleportUser, dbService, dbUser, dbName string) (*pgconn.PgConn, error) {
return postgres.MakeTestClient(ctx, common.TestClientConfig{
AuthClient: c.authClient,
AuthServer: c.authServer,
Address: address,
Cluster: c.clusterName,
Username: teleportUser,
RouteToDatabase: tlsca.RouteToDatabase{
ServiceName: dbService,
Protocol: defaults.ProtocolPostgres,
Username: dbUser,
Database: dbName,
},
})
}
// mysqlClient connects to test MySQL through database access as a specified
// Teleport user and database account.
func (c *testContext) mysqlClient(teleportUser, dbService, dbUser string) (*client.Conn, error) {
return c.mysqlClientWithAddr(c.mysqlListener.Addr().String(), teleportUser, dbService, dbUser)
}
// mysqlClientWithAddr is like mysqlClient but allows to override connection address.
func (c *testContext) mysqlClientWithAddr(address, teleportUser, dbService, dbUser string) (*client.Conn, error) {
return mysql.MakeTestClient(common.TestClientConfig{
AuthClient: c.authClient,
AuthServer: c.authServer,
Address: address,
Cluster: c.clusterName,
Username: teleportUser,
RouteToDatabase: tlsca.RouteToDatabase{
ServiceName: dbService,
Protocol: defaults.ProtocolMySQL,
Username: dbUser,
},
})
}
// mongoClient connects to test MongoDB through database access as a
// specified Teleport user and database account.
func (c *testContext) mongoClient(ctx context.Context, teleportUser, dbService, dbUser string, opts ...*options.ClientOptions) (*mongo.Client, error) {
return c.mongoClientWithAddr(ctx, c.webListener.Addr().String(), teleportUser, dbService, dbUser, opts...)
}
// mongoClientWithAddr is like mongoClient but allows to override connection address.
func (c *testContext) mongoClientWithAddr(ctx context.Context, address, teleportUser, dbService, dbUser string, opts ...*options.ClientOptions) (*mongo.Client, error) {
return mongodb.MakeTestClient(ctx, common.TestClientConfig{
AuthClient: c.authClient,
AuthServer: c.authServer,
Address: address,
Cluster: c.clusterName,
Username: teleportUser,
RouteToDatabase: tlsca.RouteToDatabase{
ServiceName: dbService,
Protocol: defaults.ProtocolMongoDB,
Username: dbUser,
},
}, opts...)
}
// createUserAndRole creates Teleport user and role with specified names
// and allowed database users/names properties.
func (c *testContext) createUserAndRole(ctx context.Context, t *testing.T, userName, roleName string, dbUsers, dbNames []string) (types.User, types.Role) {
user, role, err := auth.CreateUserAndRole(c.tlsServer.Auth(), userName, []string{roleName})
require.NoError(t, err)
role.SetDatabaseUsers(types.Allow, dbUsers)
role.SetDatabaseNames(types.Allow, dbNames)
err = c.tlsServer.Auth().UpsertRole(ctx, role)
require.NoError(t, err)
return user, role
}
// makeTLSConfig returns tls configuration for the test's tls listener.
func (c *testContext) makeTLSConfig(t *testing.T) *tls.Config {
creds, err := utils.GenerateSelfSignedCert([]string{"localhost"})
require.NoError(t, err)
cert, err := tls.X509KeyPair(creds.Cert, creds.PrivateKey)
require.NoError(t, err)
conf := utils.TLSConfig(nil)
conf.Certificates = append(conf.Certificates, cert)
conf.ClientAuth = tls.VerifyClientCertIfGiven
conf.ClientCAs, err = auth.ClientCertPool(c.authServer, c.clusterName)
require.NoError(t, err)
return conf
}
// Close closes all resources associated with the test context.
func (c *testContext) Close() error {
var errors []error
if c.mux != nil {
errors = append(errors, c.mux.Close())
}
if c.mysqlListener != nil {
errors = append(errors, c.mysqlListener.Close())
}
if c.webListener != nil {
errors = append(errors, c.webListener.Close())
}
if c.server != nil {
errors = append(errors, c.server.Close())
}
return trace.NewAggregate(errors...)
}
func setupTestContext(ctx context.Context, t *testing.T, withDatabases ...withDatabaseOption) *testContext {
testCtx := &testContext{
clusterName: "root.example.com",
hostID: uuid.New(),
postgres: make(map[string]testPostgres),
mysql: make(map[string]testMySQL),
mongo: make(map[string]testMongoDB),
clock: clockwork.NewFakeClockAt(time.Now()),
}
t.Cleanup(func() { testCtx.Close() })
// Create and start test auth server.
authServer, err := auth.NewTestAuthServer(auth.TestAuthServerConfig{
Clock: clockwork.NewFakeClockAt(time.Now()),
ClusterName: testCtx.clusterName,
Dir: t.TempDir(),
})
require.NoError(t, err)
testCtx.tlsServer, err = authServer.NewTestTLSServer()
require.NoError(t, err)
testCtx.authServer = testCtx.tlsServer.Auth()
// Create multiplexer.
listener, err := net.Listen("tcp", "localhost:0")
require.NoError(t, err)
testCtx.mux, err = multiplexer.New(multiplexer.Config{
ID: "test",
Listener: listener,
EnableProxyProtocol: true,
})
require.NoError(t, err)
// Setup TLS listener.
testCtx.webListener, err = multiplexer.NewWebListener(multiplexer.WebListenerConfig{
Listener: tls.NewListener(testCtx.mux.TLS(), testCtx.makeTLSConfig(t)),
})
require.NoError(t, err)
// Create MySQL proxy listener.
testCtx.mysqlListener, err = net.Listen("tcp", "localhost:0")
require.NoError(t, err)
// Use sync recording to not involve the uploader.
recConfig, err := authServer.AuthServer.GetSessionRecordingConfig(ctx)
require.NoError(t, err)
recConfig.SetMode(types.RecordAtNodeSync)
err = authServer.AuthServer.SetSessionRecordingConfig(ctx, recConfig)
require.NoError(t, err)
// Auth client for database service.
testCtx.authClient, err = testCtx.tlsServer.NewClient(auth.TestServerID(types.RoleDatabase, testCtx.hostID))
require.NoError(t, err)
testCtx.hostCA, err = testCtx.authClient.GetCertAuthority(types.CertAuthID{Type: types.HostCA, DomainName: testCtx.clusterName}, false)
require.NoError(t, err)
// Auth client, lock watcher and authorizer for database proxy.
proxyAuthClient, err := testCtx.tlsServer.NewClient(auth.TestBuiltin(types.RoleProxy))
require.NoError(t, err)
proxyLockWatcher, err := services.NewLockWatcher(ctx, services.LockWatcherConfig{
ResourceWatcherConfig: services.ResourceWatcherConfig{
Component: teleport.ComponentProxy,
Client: proxyAuthClient,
},
})
require.NoError(t, err)
proxyAuthorizer, err := auth.NewAuthorizer(testCtx.clusterName, proxyAuthClient, proxyLockWatcher)
require.NoError(t, err)
// TLS config for database proxy and database service.
serverIdentity, err := auth.NewServerIdentity(authServer.AuthServer, testCtx.hostID, types.RoleDatabase)
require.NoError(t, err)
tlsConfig, err := serverIdentity.TLSConfig(nil)
require.NoError(t, err)
// Set up database servers used by this test.
var databases []types.Database
for _, withDatabase := range withDatabases {
databases = append(databases, withDatabase(t, ctx, testCtx))
}
// Establish fake reversetunnel b/w database proxy and database service.
testCtx.proxyConn = make(chan net.Conn)
testCtx.fakeRemoteSite = &reversetunnel.FakeRemoteSite{
Name: testCtx.clusterName,
ConnCh: testCtx.proxyConn,
AccessPoint: proxyAuthClient,
}
tunnel := &reversetunnel.FakeServer{
Sites: []reversetunnel.RemoteSite{
testCtx.fakeRemoteSite,
},
}
// Empty config means no limit.
connLimiter, err := limiter.NewLimiter(limiter.Config{})
require.NoError(t, err)
// Create test audit events emitter.
testCtx.emitter = newTestEmitter()
// Create database proxy server.
testCtx.proxyServer, err = NewProxyServer(ctx, ProxyServerConfig{
AuthClient: proxyAuthClient,
AccessPoint: proxyAuthClient,
Authorizer: proxyAuthorizer,
Tunnel: tunnel,
TLSConfig: tlsConfig,
Limiter: connLimiter,
Emitter: testCtx.emitter,
Clock: testCtx.clock,
ServerID: "proxy-server",
Shuffle: func(servers []types.DatabaseServer) []types.DatabaseServer {
// To ensure predictability in tests, sort servers instead of shuffling.
sort.Sort(types.DatabaseServers(servers))
return servers
},
LockWatcher: proxyLockWatcher,
})
require.NoError(t, err)
// Create database service agent.
if len(databases) > 0 {
testCtx.server = testCtx.setupDatabaseServer(ctx, t, agentParams{
Databases: databases,
})
}
return testCtx
}
// agentParams combines parameters for creating database agent servers in tests.
type agentParams struct {
// Databases is a list of statically registered databases.
Databases types.Databases
// HostID is an optional host id.
HostID string
// ResourceMatchers are optional database resource matchers.
ResourceMatchers []services.ResourceMatcher
// GetServerInfoFn overrides heartbeat's server info function.
GetServerInfoFn func(database types.Database) func() (types.Resource, error)
// OnReconcile sets database resource reconciliation callback.
OnReconcile func(types.Databases)
// NoStart indicates server should not be started.
NoStart bool
}
func (p *agentParams) setDefaults(c *testContext) {
if p.HostID == "" {
p.HostID = c.hostID
}
}