54
54
import org .apache .bookkeeper .conf .ClientConfiguration ;
55
55
import org .apache .bookkeeper .conf .ServerConfiguration ;
56
56
import org .apache .bookkeeper .net .BookieId ;
57
+ import org .apache .bookkeeper .net .BookieSocketAddress ;
57
58
import org .apache .bookkeeper .proto .BookieConnectionPeer ;
58
59
import org .apache .bookkeeper .proto .BookieServer ;
59
60
import org .apache .bookkeeper .proto .ClientConnectionPeer ;
60
61
import org .apache .bookkeeper .proto .TestPerChannelBookieClient ;
62
+ import org .apache .bookkeeper .server .conf .BookieConfiguration ;
61
63
import org .apache .bookkeeper .test .BookKeeperClusterTestCase ;
62
64
import org .apache .bookkeeper .test .TestStatsProvider ;
63
65
import org .apache .bookkeeper .tls .TLSContextFactory .KeyStoreType ;
64
66
import org .apache .bookkeeper .util .IOUtils ;
67
+ import org .apache .bookkeeper .util .PortManager ;
65
68
import org .apache .bookkeeper .util .TestUtils ;
66
69
import org .apache .commons .io .FileUtils ;
70
+ import org .eclipse .jetty .server .Server ;
67
71
import org .junit .After ;
68
72
import org .junit .Assert ;
69
73
import org .junit .Before ;
@@ -498,9 +502,11 @@ public void testConnectToTLSClusterTLSClientWithAuthentication() throws Exceptio
498
502
499
503
/**
500
504
* Verify that a client without tls enabled can connect to a cluster with TLS.
505
+ * In the Default Bookie config, ONLY_SECURE_CLIENTS_ALLOWED is set to false.
506
+ * Therefore, the Bookie allows non-secure client connection.
501
507
*/
502
508
@ Test
503
- public void testConnectToTLSClusterNonTLSClient () throws Exception {
509
+ public void testConnectToTLSClusterNonTLSClient1 () throws Exception {
504
510
ClientConfiguration conf = new ClientConfiguration (baseClientConf );
505
511
conf .setTLSProviderFactoryClass (null );
506
512
try {
@@ -510,6 +516,87 @@ public void testConnectToTLSClusterNonTLSClient() throws Exception {
510
516
}
511
517
}
512
518
519
+ /**
520
+ * Verify that a client without tls enabled can NOT connect to a cluster with TLS,
521
+ * if ONLY_SECURE_CLIENTS_ALLOWED is set in the Bookie config.
522
+ */
523
+ @ Test
524
+ public void testConnectToTLSClusterNonTLSClient2 () throws Exception {
525
+
526
+ // Set client without TLS
527
+ ClientConfiguration conf = new ClientConfiguration (baseClientConf );
528
+ conf .setTLSProviderFactoryClass (null );
529
+ try {
530
+ // Enable the feature to only allow secure clients.
531
+ restartBookies (c -> {
532
+ c .setOnlySecureClientConnectionAllowed (true );
533
+ return c ;
534
+ });
535
+ testClient (conf , numBookies );
536
+ fail ("non tls client should not be able to connect to tls enabled bookies" );
537
+ } catch (BKException .BKNotEnoughBookiesException nnbe ) {
538
+ // Correct response.
539
+ } catch (BKException .BKUnauthorizedAccessException ue ) {
540
+ // Correct response.
541
+ }
542
+ }
543
+
544
+ /**
545
+ * Verify that the Read from the non-Secure Client throws BKSecurityException
546
+ * if ONLY_SECURE_CLIENTS_ALLOWED is set in the Bookie config.
547
+ */
548
+ @ Test
549
+ public void testReadwithNonTLSBookie () throws Exception {
550
+
551
+ /* TestTLS tests for V3 and V2 protocols together
552
+ * We should be able to create a ledger when its v3 and setOnlySecureClientConnectionAllowed(true)
553
+ * We shouldn't be able to do any create / read when its v2 and setOnlySecureClientConnectionAllowed(true)
554
+ * since v2 does not support TLS.
555
+ */
556
+ if (useV2Protocol ) {
557
+ return ;
558
+ }
559
+
560
+ // Enable the feature to only allow secure clients.
561
+ ClientConfiguration conf = new ClientConfiguration (baseClientConf );
562
+ long lid = 0 ;
563
+ try {
564
+ restartBookies (c -> {
565
+ c .setOnlySecureClientConnectionAllowed (true );
566
+ return c ;
567
+ });
568
+ // Create the Ledger
569
+ BookKeeper client = new BookKeeper (conf );
570
+ byte [] passwd = "testPassword" .getBytes ();
571
+ int numEntries = 100 ;
572
+ byte [] testEntry = "testEntry" .getBytes ();
573
+ try (LedgerHandle lh = client .createLedger (numBookies , numBookies , DigestType .CRC32 , passwd );) {
574
+ for (int i = 0 ; i <= numEntries ; i ++) {
575
+ lh .addEntry (testEntry );
576
+ }
577
+ lid = lh .getId ();
578
+ }
579
+ } catch (BKException .BKNotEnoughBookiesException nnbe ) {
580
+ fail ("tls client could not create the ledger" );
581
+ }
582
+ // nonTLS client should not be able to read the ledger
583
+ conf .setTLSProviderFactoryClass (null );
584
+ try {
585
+ BookKeeper client = new BookKeeper (conf );
586
+ byte [] passwd = "testPassword" .getBytes ();
587
+ int numEntries = 100 ;
588
+ byte [] testEntry = "testEntry" .getBytes ();
589
+ try (LedgerHandle lh = client .openLedger (lid , DigestType .CRC32 , passwd );) {
590
+ Enumeration <LedgerEntry > entries = lh .readEntries (0 , numEntries );
591
+ fail ("The Read should've failed with BKSecurityException" );
592
+ }
593
+ } catch (BKException .BKSecurityException se ) {
594
+ // Correct Response.
595
+ } catch (BKException .BKUnauthorizedAccessException ue ) {
596
+ // Correct response.
597
+ }
598
+ }
599
+
513
600
/**
514
601
* Verify that a client will fail to connect to a server if it has asked for TLS, but it is not available.
515
602
*/
@@ -534,7 +621,7 @@ public void testClientWantsTLSNoServersHaveIt() throws Exception {
534
621
* bookies with TLS enabled in the cluster, although few bookies do not have TLS enabled.
535
622
*/
536
623
@ Test
537
- public void testTLSClientButOnlyFewTLSServers () throws Exception {
624
+ public void testTLSClientButOnlyFewTLSServers1 () throws Exception {
538
625
// disable TLS on initial set of bookies
539
626
restartBookies (c -> {
540
627
c .setTLSProviderFactoryClass (null );
0 commit comments