File: test_master_key_reader_writer.cc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (940 lines) | stat: -rw-r--r-- 34,825 bytes parent folder | download
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
/*
 Copyright (c) 2018, 2025, Oracle and/or its affiliates.

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License, version 2.0,
 as published by the Free Software Foundation.

 This program is designed to work with certain software (including
 but not limited to OpenSSL) that is licensed under separate terms,
 as designated in a particular file or component or in included license
 documentation.  The authors of MySQL hereby grant you an additional
 permission to link the program and your derivative works with the
 separately licensed software that they have either included with
 the program or referenced in the documentation.

 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 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <fstream>
#include <stdexcept>
#include <system_error>
#include <thread>

#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif

#include <gmock/gmock-matchers.h>
#include <gtest/gtest.h>

#include "config_builder.h"
#include "dim.h"
#include "keyring/keyring_manager.h"
#include "mock_server_testutils.h"
#include "mysql/harness/string_utils.h"  // split_string
#include "mysqlrouter/keyring_info.h"
#include "mysqlrouter/utils.h"  // copy_file
#include "process_manager.h"
#include "random_generator.h"
#include "router_component_test.h"
#include "script_generator.h"
#include "tcp_port_pool.h"

/**
 * @file
 * @brief Component Tests for the master-key-reader and master-key-writer
 */

using namespace std::chrono_literals;

MATCHER_P(FileContentEqual, master_key, "") {
  std::ifstream file(arg);
  std::stringstream file_content;
  file_content << file.rdbuf();
  return file_content.str() == master_key;
}

MATCHER_P(FileContentNotEqual, master_key, "") {
  std::ifstream file(arg);
  std::stringstream file_content;
  file_content << file.rdbuf();
  return file_content.str() != master_key;
}

class MasterKeyReaderWriterTest : public RouterComponentBootstrapTest {
 protected:
  void SetUp() override {
    RouterComponentTest::SetUp();

    mysql_harness::DIM &dim = mysql_harness::DIM::instance();
    // RandomGenerator
    dim.set_RandomGenerator(
        []() {
          static mysql_harness::RandomGenerator rg;
          return &rg;
        },
        [](mysql_harness::RandomGeneratorInterface *) {});
  }

  void write_to_file(const Path &file_path, const std::string &text) {
    std::ofstream master_key_file(file_path.str());
    if (master_key_file.good()) {
      master_key_file << text;
    }
  }

  static std::pair<std::string, std::map<std::string, std::string>>
  metadata_cache_section(uint16_t server_port) {
    return {"metadata_cache:test",
            {{"router_id", "1"},
             {"bootstrap_server_addresses",
              "mysql://localhost:" + std::to_string(server_port)},
             {"user", "mysql_router1_user"},
             {"metadata_cluster", "test"},
             {"ttl", "500"}}};
  }

  static std::string get_metadata_cache_section(unsigned server_port) {
    auto section = metadata_cache_section(server_port);

    return mysql_harness::ConfigBuilder::build_section(section.first,
                                                       section.second);
  }

  static std::pair<std::string, std::map<std::string, std::string>>
  metadata_cache_routing_section(const std::string &role,
                                 const std::string &strategy,
                                 uint16_t router_port) {
    return {"routing:test_default",
            {{"bind_port", std::to_string(router_port)},
             {"destinations", "metadata-cache://test/default?role=" + role},
             {"protocol", "classic"},
             {"routing_strategy", strategy}}};
  }

  static std::string get_metadata_cache_routing_section(
      const std::string &role, const std::string &strategy,
      uint16_t router_port) {
    auto section = metadata_cache_routing_section(role, strategy, router_port);

    return mysql_harness::ConfigBuilder::build_section(section.first,
                                                       section.second) +
           "\n";
  }

  KeyringInfo init_keyring() {
    ScriptGenerator script_generator(ProcessManager::get_origin(),
                                     tmp_dir_.name());

    KeyringInfo keyring_info;
    keyring_info.set_master_key_reader(script_generator.get_reader_script());
    keyring_info.set_master_key_writer(script_generator.get_writer_script());
    keyring_info.set_keyring_file(Path(tmp_dir_.name()).join("keyring").str());

    keyring_info.generate_master_key();
    master_key_ = keyring_info.get_master_key();
    keyring_info.add_router_id_to_env(1);
    keyring_info.write_master_key();
    mysql_harness::init_keyring_with_key(keyring_info.get_keyring_file(),
                                         keyring_info.get_master_key(), true);

    mysql_harness::Keyring *keyring = mysql_harness::get_keyring();
    keyring->store("mysql_router1_user", "password", "root");
    mysql_harness::flush_keyring();
    mysql_harness::reset_keyring();

    return keyring_info;
  }

  std::map<std::string, std::string> get_default_section_map(
      bool assign_fake_reader = false, bool assign_fake_writer = false) {
    ScriptGenerator script_generator(ProcessManager::get_origin(),
                                     tmp_dir_.name());
    std::map<std::string, std::string> default_section = get_DEFAULT_defaults();
    default_section["keyring_path"] =
        Path(tmp_dir_.name()).join("keyring").str();

    if (assign_fake_reader)
      default_section["master_key_reader"] =
          script_generator.get_fake_reader_script();
    else
      default_section["master_key_reader"] =
          script_generator.get_reader_script();

    if (assign_fake_writer)
      default_section["master_key_writer"] =
          script_generator.get_fake_writer_script();
    else
      default_section["master_key_writer"] =
          script_generator.get_writer_script();

    return default_section;
  }

  std::map<std::string, std::string>
  get_incorrect_master_key_default_section_map() {
    ScriptGenerator script_generator(ProcessManager::get_origin(),
                                     tmp_dir_.name());

    auto default_section = get_DEFAULT_defaults();
    default_section["keyring_path"] =
        Path(tmp_dir_.name()).join("keyring").str();
    default_section["master_key_reader"] =
        script_generator.get_reader_incorrect_master_key_script();
    default_section["master_key_writer"] = script_generator.get_writer_script();

    return default_section;
  }

  TempDirectory tmp_dir_;
  TempDirectory bootstrap_dir_;
  std::string master_key_;
};

/**
 * @test
 *       verify that when bootstrap is launched using --master-key-reader and
 *       --master-key-writer options then master key file is not created.
 */
TEST_F(MasterKeyReaderWriterTest,
       NoMasterKeyFileWhenBootstrapPassWithMasterKeyReader) {
  auto server_port = port_pool_.get_next_available();
  auto &server_mock = launch_mysql_server_mock(
      get_data_dir().join("bootstrap_gr.js").str(), server_port, false);
  ASSERT_NO_FATAL_FAILURE(check_port_ready(server_mock, server_port));

  ScriptGenerator script_generator(ProcessManager::get_origin(),
                                   tmp_dir_.name());

  // launch the router in bootstrap mode
  auto &router = launch_router_for_bootstrap({
      "--bootstrap=127.0.0.1:" + std::to_string(server_port),
      "--directory=" + bootstrap_dir_.name(),
      "--force",
      "--master-key-reader=" + script_generator.get_reader_script(),
      "--master-key-writer=" + script_generator.get_writer_script(),
  });

  // check if the bootstrapping was successful
  ASSERT_NO_FATAL_FAILURE(check_exit_code(router, EXIT_SUCCESS, 30000ms));
  EXPECT_TRUE(router.expect_output(
      "MySQL Router configured for the InnoDB Cluster 'mycluster'"))
      << router.get_full_output() << std::endl
      << "server: " << server_mock.get_full_output();

  Path tmp(bootstrap_dir_.name());
  Path master_key_file(tmp.join("mysqlrouter.key").str());

  ASSERT_FALSE(master_key_file.exists());

  Path keyring_file(tmp.join("data").join("keyring").str());
  ASSERT_TRUE(keyring_file.exists());

  Path dir(tmp_dir_.name());
  Path data_file(dir.join("master_key").str());
  ASSERT_TRUE(data_file.exists());
}

/**
 * @test
 *       verify that when bootstrap is launched using --master-key-reader and
 *       --master-key-writer options then generated config file contains
 *       entries for master_key_reader and master_key_writer.
 *       Also, verify that --bootstrap can be specified after --master-key-*
 *       options (all other tests will use it in the beginning).
 */
TEST_F(MasterKeyReaderWriterTest,
       CheckConfigFileWhenBootstrapPassWithMasterKeyReader) {
  auto server_port = port_pool_.get_next_available();
  auto &server_mock = launch_mysql_server_mock(
      get_data_dir().join("bootstrap_gr.js").str(), server_port, false);
  ASSERT_NO_FATAL_FAILURE(check_port_ready(server_mock, server_port));

  ScriptGenerator script_generator(ProcessManager::get_origin(),
                                   tmp_dir_.name());

  // launch the router in bootstrap mode
  auto &router = launch_router_for_bootstrap({
      "--directory=" + bootstrap_dir_.name(),
      "--force",
      "--master-key-reader=" + script_generator.get_reader_script(),
      "--master-key-writer=" + script_generator.get_writer_script(),
      "--bootstrap=127.0.0.1:" + std::to_string(server_port),
  });

  // check if the bootstrapping was successful
  ASSERT_NO_FATAL_FAILURE(check_exit_code(router, EXIT_SUCCESS, 30000ms));
  EXPECT_TRUE(
      router.expect_output("MySQL Router configured for the "
                           "InnoDB Cluster 'mycluster'"))
      << router.get_full_output() << std::endl
      << "server: " << server_mock.get_full_output();

  Path tmp(bootstrap_dir_.name());
  Path config_file(tmp.join("mysqlrouter.conf").str());
  ASSERT_TRUE(config_file.exists());

  // read master-key-reader and master-key-writer
  std::string master_key_reader = "", master_key_writer = "";

  std::ifstream file(config_file.str());
  std::istream_iterator<std::string> beg(file), eof;
  std::vector<std::string> lines(beg, eof);

  for (const auto &line : lines) {
    int index = line.find('=');
    if (line.substr(0, index) == "master_key_reader")
      master_key_reader = line.substr(index + 1);
    else if (line.substr(0, index) == "master_key_writer")
      master_key_writer = line.substr(index + 1);
  }

  ASSERT_THAT(master_key_reader,
              testing::Eq(script_generator.get_reader_script()));
  ASSERT_THAT(master_key_writer,
              testing::Eq(script_generator.get_writer_script()));
}

/**
 * @test
 *       verify that when --master-key-reader option is used, but specified
 * reader cannot be executed, then bootstrap fails and appropriate error
 * message is printed to standard output.
 */
TEST_F(MasterKeyReaderWriterTest, BootstrapFailsWhenCannotRunMasterKeyReader) {
  auto server_port = port_pool_.get_next_available();
  auto &server_mock = launch_mysql_server_mock(
      get_data_dir().join("bootstrap_gr.js").str(), server_port, false);
  ASSERT_NO_FATAL_FAILURE(check_port_ready(server_mock, server_port));

  ScriptGenerator script_generator(ProcessManager::get_origin(),
                                   tmp_dir_.name());

  // launch the router in bootstrap mode
  auto &router = launch_router_for_bootstrap(
      {
          "--bootstrap=127.0.0.1:" + std::to_string(server_port),
          "--directory=" + bootstrap_dir_.name(),
          "--force",
          "--master-key-reader=" + script_generator.get_fake_reader_script(),
          "--master-key-writer=" + script_generator.get_writer_script(),
      },
      EXIT_FAILURE);

  // check if the bootstrapping failed
  ASSERT_NO_FATAL_FAILURE(check_exit_code(router, EXIT_FAILURE));
  EXPECT_TRUE(router.expect_output(
      "Error: Cannot fetch master key file using master key reader"))
      << router.get_full_output() << std::endl
      << "server: " << server_mock.get_full_output();
}

/**
 * @test
 *       verify that when --master-key-writer option is used, but specified
 * master key writer cannot be executed, then bootstrap fails and appropriate
 * error message is printed to standard output.
 */
TEST_F(MasterKeyReaderWriterTest, BootstrapFailsWhenCannotRunMasterKeyWriter) {
  auto server_port = port_pool_.get_next_available();
  auto &server_mock = launch_mysql_server_mock(
      get_data_dir().join("bootstrap_gr.js").str(), server_port, false);
  ASSERT_NO_FATAL_FAILURE(check_port_ready(server_mock, server_port));

  ScriptGenerator script_generator(ProcessManager::get_origin(),
                                   tmp_dir_.name());

  // launch the router in bootstrap mode
  auto &router = launch_router_for_bootstrap(
      {
          "--bootstrap=127.0.0.1:" + std::to_string(server_port),
          "--directory=" + bootstrap_dir_.name(),
          "--force",
          "--master-key-reader=" + script_generator.get_reader_script(),
          "--master-key-writer=" + script_generator.get_fake_writer_script(),
      },
      EXIT_FAILURE);

  // check if the bootstrapping failed
  ASSERT_NO_FATAL_FAILURE(check_exit_code(router, EXIT_FAILURE));
  EXPECT_TRUE(router.expect_output(
      "Error: Cannot write master key file using master key writer"))
      << router.get_full_output() << std::endl
      << "server: " << server_mock.get_full_output();
}

/**
 * @test
 *       verify that if keyring file already exists and --master-key-reader
 * option is used and bootstrap fails, then original keyring file is restored.
 */
TEST_F(MasterKeyReaderWriterTest, KeyringFileRestoredWhenBootstrapFails) {
  mysql_harness::mkdir(Path(tmp_dir_.name()).join("data").str(), 0777);
  // create keyring file
  Path keyring_path(Path(tmp_dir_.name()).join("data").join("keyring").str());

  write_to_file(keyring_path, "keyring file content");

  auto server_port = port_pool_.get_next_available();
  auto &server_mock = launch_mysql_server_mock(
      get_data_dir().join("bootstrap_gr.js").str(), server_port, false);
  ASSERT_NO_FATAL_FAILURE(check_port_ready(server_mock, server_port));

  ScriptGenerator script_generator(ProcessManager::get_origin(),
                                   tmp_dir_.name());

  // launch the router in bootstrap mode
  auto &router = launch_router_for_bootstrap(
      {
          "--bootstrap=127.0.0.1:" + std::to_string(server_port),
          "--directory=" + bootstrap_dir_.name(),
          "--force",
          "--master-key-reader=" + script_generator.get_fake_reader_script(),
          "--master-key-writer=" + script_generator.get_fake_writer_script(),
      },
      EXIT_FAILURE);

  // check if the bootstrapping failed
  ASSERT_NO_FATAL_FAILURE(check_exit_code(router, EXIT_FAILURE));
  ASSERT_THAT(keyring_path.str(), FileContentEqual("keyring file content"));
}

/**
 * @test
 *       verify that if --master-key-reader option is used and bootstrap
 * fails, then original master key is restored.
 */
TEST_F(MasterKeyReaderWriterTest, MasterKeyRestoredWhenBootstrapFails) {
  // create file that is used by master-key-reader and master-key-writer
  Path master_key_path(Path(tmp_dir_.name()).join("master_key").str());
  write_to_file(master_key_path, "");

  unsigned server_port = port_pool_.get_next_available();
  ScriptGenerator script_generator(ProcessManager::get_origin(),
                                   tmp_dir_.name());

  // launch the router in bootstrap mode
  auto &router = launch_router_for_bootstrap(
      {
          "--bootstrap=127.0.0.1:" + std::to_string(server_port),
          "--connect-timeout=1",
          "--directory=" + bootstrap_dir_.name(),
          "--force",
          "--master-key-reader=" + script_generator.get_reader_script(),
          "--master-key-writer=" + script_generator.get_writer_script(),
      },
      EXIT_FAILURE);

  // check if the bootstrapping failed
  ASSERT_NO_FATAL_FAILURE(check_exit_code(router, EXIT_FAILURE));
  ASSERT_THAT(master_key_path.str(), FileContentEqual(""));
}

/*
 * @test
 *       verify that if --master-key-reader option is used and original master
 * key is empty and bootstrap passes, then new master key is stored using
 * --master-key-writer.
 */
TEST_F(MasterKeyReaderWriterTest,
       IsNewMasterKeyIfReaderReturnsEmptyKeyAndBootstrapPass) {
  write_to_file(Path(tmp_dir_.name()).join("master_key"), "");

  auto server_port = port_pool_.get_next_available();
  auto &server_mock = launch_mysql_server_mock(
      get_data_dir().join("bootstrap_gr.js").str(), server_port, false);
  ASSERT_NO_FATAL_FAILURE(check_port_ready(server_mock, server_port));

  ScriptGenerator script_generator(ProcessManager::get_origin(),
                                   tmp_dir_.name());

  // launch the router in bootstrap mode
  auto &router = launch_router_for_bootstrap({
      "--bootstrap=127.0.0.1:" + std::to_string(server_port),
      "--directory=" + bootstrap_dir_.name(),
      "--force",
      "--master-key-reader=" + script_generator.get_reader_script(),
      "--master-key-writer=" + script_generator.get_writer_script(),
  });

  // check if the bootstrapping was successful
  ASSERT_NO_FATAL_FAILURE(check_exit_code(router, EXIT_SUCCESS, 30000ms));
  EXPECT_TRUE(
      router.expect_output("MySQL Router configured for the "
                           "InnoDB Cluster 'mycluster'"))
      << router.get_full_output() << std::endl
      << "server: " << server_mock.get_full_output();

  Path dir(tmp_dir_.name());
  Path data_file(dir.join("master_key").str());
  ASSERT_TRUE(data_file.exists());
  ASSERT_THAT(data_file.str(), FileContentNotEqual(""));
}

/*
 * @test
 *       verify that if master key exists and is not empty and bootstrap pass,
 * then original master key is not overridden.
 */
TEST_F(MasterKeyReaderWriterTest,
       DontWriteMasterKeyAtBootstrapIfMasterKeyAlreadyExists) {
  write_to_file(Path(tmp_dir_.name()).join("master_key"), "master key value");

  auto server_port = port_pool_.get_next_available();
  auto &server_mock = launch_mysql_server_mock(
      get_data_dir().join("bootstrap_gr.js").str(), server_port, false);
  ASSERT_NO_FATAL_FAILURE(check_port_ready(server_mock, server_port));

  ScriptGenerator script_generator(ProcessManager::get_origin(),
                                   tmp_dir_.name());

  // launch the router in bootstrap mode
  auto &router = launch_router_for_bootstrap({
      "--bootstrap=127.0.0.1:" + std::to_string(server_port),
      "--directory=" + bootstrap_dir_.name(),
      "--force",
      "--master-key-reader=" + script_generator.get_reader_script(),
      "--master-key-writer=" + script_generator.get_writer_script(),
  });

  // check if the bootstrapping failed
  ASSERT_NO_FATAL_FAILURE(check_exit_code(router));
  ASSERT_THAT(Path(tmp_dir_.name()).join("master_key").str(),
              FileContentEqual("master key value"));
}

/**
 * @test
 *       verify that when master key returned by master-key-reader is correct,
 *       then launching the router succeeds
 *
 */
TEST_F(MasterKeyReaderWriterTest, ConnectToMetadataServerPass) {
  const auto server_port = port_pool_.get_next_available();
  const auto router_port = port_pool_.get_next_available();

  // launch server
  /*auto &server =*/launch_mysql_server_mock(
      get_data_dir().join("metadata_dynamic_nodes.js").str(), server_port);

  // launch the router with metadata-cache configuration
  TempDirectory conf_dir("conf");

  auto writer = config_writer(conf_dir.name())
                    .section(metadata_cache_section(server_port))
                    .section(metadata_cache_routing_section(
                        "PRIMARY", "round-robin", router_port));

  // set master-key-reader/writer in DEFAULT section
  auto &default_section = writer.sections().at("DEFAULT");

  const auto keyring_info = init_keyring();

  default_section["keyring_path"] = keyring_info.get_keyring_file();
  default_section["master_key_reader"] = keyring_info.get_master_key_reader();
  default_section["master_key_writer"] = keyring_info.get_master_key_writer();

  auto &router =
      router_spawner()
          // ERROR: 1273 Syntax Error at: ROLLBACK ...
          // it will never get READY.
          .wait_for_sync_point(ProcessManager::Spawner::SyncPoint::RUNNING)
          .spawn({"-c", writer.write()});

  EXPECT_TRUE(wait_log_contains(
      router, "Connected with metadata server running on", 10s));
}

/**
 * @test
 *       verify that when master key returned by master-key-reader is correct
 *       and then launching the router succeeds, then master-key is not
 * written to log files.
 *
 */
TEST_F(MasterKeyReaderWriterTest,
       NoMasterKeyInLogsWhenConnectToMetadataServerPass) {
  const auto server_port = port_pool_.get_next_available();
  const auto router_port = port_pool_.get_next_available();

  // launch server
  auto &server = launch_mysql_server_mock(
      get_data_dir().join("metadata_dynamic_nodes.js").str(), server_port);
  ASSERT_NO_FATAL_FAILURE(check_port_ready(server, server_port, 10000ms));

  TempDirectory conf_dir("conf");

  auto writer = config_writer(conf_dir.name())
                    .section(metadata_cache_section(server_port))
                    .section(metadata_cache_routing_section(
                        "PRIMARY", "round-robin", router_port));

  // set master-key-reader/writer in DEFAULT section
  auto &default_section = writer.sections().at("DEFAULT");

  const auto keyring_info = init_keyring();

  default_section["keyring_path"] = keyring_info.get_keyring_file();
  default_section["master_key_reader"] = keyring_info.get_master_key_reader();
  default_section["master_key_writer"] = keyring_info.get_master_key_writer();

  // launch the router with metadata-cache configuration

  auto &router =
      router_spawner()
          // ERROR: 1273 Syntax Error at: ROLLBACK ...
          // it will never get READY.
          .wait_for_sync_point(ProcessManager::Spawner::SyncPoint::RUNNING)
          .spawn({"-c", writer.write()});

  // wait a bit for log to generate.
  std::this_thread::sleep_for(1s);

  auto log_lines =
      mysql_harness::split_string(router.get_logfile_content(), '\n');
  EXPECT_THAT(
      log_lines,
      ::testing::Not(::testing::Contains(::testing::HasSubstr(master_key_))));
}

/**
 * @test
 *       verify that when cannot run master-key-reader in order to read master
 * key then launching the router fails.
 */
TEST_F(MasterKeyReaderWriterTest, CannotLaunchRouterWhenNoMasterKeyReader) {
  auto server_port = port_pool_.get_next_available();
  auto router_port = port_pool_.get_next_available();
  std::string metadata_cache_section = get_metadata_cache_section(server_port);
  std::string routing_section =
      get_metadata_cache_routing_section("PRIMARY", "round-robin", router_port);

  init_keyring();

  // launch second metadata server
  auto &server = launch_mysql_server_mock(
      get_data_dir().join("metadata_dynamic_nodes.js").str(), server_port,
      false);
  ASSERT_NO_FATAL_FAILURE(check_port_ready(server, server_port));

  auto default_section_map = get_default_section_map(true, true);
  // launch the router with metadata-cache configuration
  TempDirectory conf_dir("conf");
  auto &router = launch_router(
      {
          "-c",
          create_config_file(conf_dir.name(),
                             metadata_cache_section + routing_section,
                             &default_section_map),
      },
      EXIT_FAILURE, true, false, -1s);

  ASSERT_NO_FATAL_FAILURE(check_exit_code(router, EXIT_FAILURE));
}

/**
 * @test
 *       verify that when password fetched using --master-key-reader is
 * incorrect, then launching the router fails with appropriate log message.
 */
TEST_F(MasterKeyReaderWriterTest, CannotLaunchRouterWhenMasterKeyIncorrect) {
  auto server_port = port_pool_.get_next_available();
  auto router_port = port_pool_.get_next_available();
  std::string metadata_cache_section = get_metadata_cache_section(server_port);
  std::string routing_section =
      get_metadata_cache_routing_section("PRIMARY", "round-robin", router_port);

  init_keyring();

  // launch second metadata server
  auto &server = launch_mysql_server_mock(
      get_data_dir().join("metadata_dynamic_nodes.js").str(), server_port,
      false);
  ASSERT_NO_FATAL_FAILURE(check_port_ready(server, server_port));

  auto incorrect_master_key_default_section_map =
      get_incorrect_master_key_default_section_map();
  // launch the router with metadata-cache configuration
  TempDirectory conf_dir("conf");
  auto &router = launch_router(
      {"-c", create_config_file(conf_dir.name(),
                                metadata_cache_section + routing_section,
                                &incorrect_master_key_default_section_map)},
      EXIT_FAILURE, true, false, -1s);

  ASSERT_NO_FATAL_FAILURE(check_exit_code(router, EXIT_FAILURE));
}
/*
 * These tests are executed only for STANDALONE layout and are not executed
 * for Windows. Bootstrap for layouts different than STANDALONE use
 * directories to which tests don't have access (see install_layout.cmake).
 */
Path g_origin_path;
#ifndef SKIP_BOOTSTRAP_SYSTEM_DEPLOYMENT_TESTS

class MasterKeyReaderWriterSystemDeploymentTest
    : public MasterKeyReaderWriterTest {
 protected:
  void SetUp() override {
    // this test modifies the origin path so we need to restore it
    ProcessManager::set_origin(g_origin_path);
    RouterComponentTest::SetUp();
    init_tmp_dir();

    set_mysqlrouter_exec(Path(exec_file_));
  }

  void TearDown() override {
    RouterComponentTest::TearDown();
#ifdef __APPLE__
    unlink(library_link_file_.c_str());
#endif
  }

  void write_to_file(const Path &file_path, const std::string &text) {
    std::ofstream master_key_file(file_path.str());
    if (master_key_file.good()) {
      master_key_file << text;
    }
  }

  /*
   * Create temporary directory that represents system deployment
   * layout for mysql bootstrap. A mysql executable is copied to
   * tmp_dir_/stage/bin/ and then an execution permission is assigned to it.
   *
   * After the test is completed, the whole temporary directory is deleted.
   */
  void init_tmp_dir() {
    mysql_harness::mkdir(tmp_dir_.name() + "/stage", 0700);
    mysql_harness::mkdir(tmp_dir_.name() + "/stage/bin", 0700);
    exec_file_ = tmp_dir_.name() + "/stage/bin/mysqlrouter";
    mysqlrouter::copy_file(get_mysqlrouter_exec().str(), exec_file_);
#ifndef _WIN32
    chmod(exec_file_.c_str(), 0700);
#endif

    // on MacOS we need to create symlink to library_output_directory
    // inside our temp dir as mysqlrouter has @loader_path/../lib
    // hardcoded by MYSQL_ADD_EXECUTABLE
#ifdef __APPLE__
    std::string cur_dir_name = g_origin_path.real_path().dirname().str();
    const std::string library_output_dir =
        cur_dir_name + "/library_output_directory";

    library_link_file_ =
        std::string(Path(tmp_dir_.name()).real_path().str() + "/stage/lib");

    if (symlink(library_output_dir.c_str(), library_link_file_.c_str())) {
      throw std::runtime_error(
          "Could not create symbolic link to library_output_directory: " +
          std::to_string(errno));
    }
#endif
    config_file_ = tmp_dir_.name() + "/stage/mysqlrouter.conf";
  }

  auto &run_server_mock() {
    const std::string json_stmts = get_data_dir().join("bootstrap_gr.js").str();
    server_port_ = port_pool_.get_next_available();

    // launch mock server and wait for it to start accepting connections
    auto &server_mock = launch_mysql_server_mock(json_stmts, server_port_);
    return server_mock;
  }

  std::string exec_file_;
  std::string config_file_;
#ifdef __APPLE__
  std::string library_link_file_;
#endif

  uint16_t server_port_;
};

/**
 * @test
 *      Verify if bootstrap with --master-key-reader and --master-key-writer
 *      and with system deployment layout then master key file
 * (stage/mysqlrouter.key) is not generated.
 */
TEST_F(MasterKeyReaderWriterSystemDeploymentTest, BootstrapPass) {
  auto &server_mock = run_server_mock();
  ASSERT_NO_FATAL_FAILURE(check_port_ready(server_mock, server_port_));

  ScriptGenerator script_generator(ProcessManager::get_origin(),
                                   tmp_dir_.name());

  // launch the router in bootstrap mode
  auto &router = launch_router_for_bootstrap({
      "--bootstrap=127.0.0.1:" + std::to_string(server_port_),
      "--master-key-reader=" + script_generator.get_reader_script(),
      "--master-key-writer=" + script_generator.get_writer_script(),
  });

  // check if the bootstrapping was successful
  ASSERT_NO_FATAL_FAILURE(check_exit_code(router, EXIT_SUCCESS));

  EXPECT_TRUE(
      router.expect_output("MySQL Router configured for the "
                           "InnoDB Cluster 'mycluster'"))
      << router.get_full_output() << std::endl
      << "server: " << server_mock.get_full_output();

  Path dir(tmp_dir_.name());
  Path data_file(dir.join("stage").join("mysqlrouter.key").str());
  ASSERT_FALSE(data_file.exists());
}

/**
 * @test
 *       verify if bootstrap with --master-key-reader and with system
 * deployment layout, but specified reader cannot be executed, then bootstrap
 * fails and appropriate error message is printed to standard output.
 */
TEST_F(MasterKeyReaderWriterSystemDeploymentTest,
       BootstrapFailsWhenCannotRunMasterKeyReader) {
  auto &server_mock = run_server_mock();
  ASSERT_NO_FATAL_FAILURE(check_port_ready(server_mock, server_port_));

  ScriptGenerator script_generator(ProcessManager::get_origin(),
                                   tmp_dir_.name());

  // launch the router in bootstrap mode
  auto &router = launch_router_for_bootstrap(
      {
          "--bootstrap=127.0.0.1:" + std::to_string(server_port_),
          "--master-key-reader=" + script_generator.get_fake_reader_script(),
          "--master-key-writer=" + script_generator.get_writer_script(),
      },
      EXIT_FAILURE);

  // check if the bootstrapping failed
  ASSERT_NO_FATAL_FAILURE(check_exit_code(router, EXIT_FAILURE));

  EXPECT_TRUE(router.expect_output(
      "Error: Cannot fetch master key file using master key reader"))
      << router.get_full_output() << std::endl
      << "server: " << server_mock.get_full_output();
}

/**
 * @test
 *       verify if bootstrap with --master-key-writer and system deployment
 * layout, but specified master key writer cannot be executed, then bootstrap
 * fails and appropriate error message is printed to standard output.
 */
TEST_F(MasterKeyReaderWriterSystemDeploymentTest,
       BootstrapFailsWhenCannotRunMasterKeyWriter) {
  auto &server_mock = run_server_mock();
  ASSERT_NO_FATAL_FAILURE(check_port_ready(server_mock, server_port_));

  ScriptGenerator script_generator(ProcessManager::get_origin(),
                                   tmp_dir_.name());

  // launch the router in bootstrap mode
  auto &router = launch_router_for_bootstrap(
      {
          "--bootstrap=127.0.0.1:" + std::to_string(server_port_),
          "--master-key-reader=" + script_generator.get_reader_script(),
          "--master-key-writer=" + script_generator.get_fake_writer_script(),
      },
      EXIT_FAILURE);

  // check if the bootstrapping failed
  ASSERT_NO_FATAL_FAILURE(check_exit_code(router, EXIT_FAILURE));

  EXPECT_TRUE(router.expect_output(
      "Error: Cannot write master key file using master key writer"))
      << router.get_full_output() << std::endl
      << "server: " << server_mock.get_full_output();
}

/**
 * @test
 *       verify that if keyring file already exists and bootstrap with
 * --master-key-reader and system deployment layout and bootstrap fails, then
 * original keyring file is restored.
 */
TEST_F(MasterKeyReaderWriterSystemDeploymentTest,
       KeyringFileRestoredWhenBootstrapFails) {
  mysql_harness::mkdir(Path(tmp_dir_.name()).join("stage").join("data").str(),
                       0777);
  // create keyring file
  Path keyring_path(
      Path(tmp_dir_.name()).join("stage").join("data").join("keyring").str());
  // set original keyring file
  write_to_file(keyring_path, "keyring file content");

  auto &server_mock = run_server_mock();
  ASSERT_NO_FATAL_FAILURE(check_port_ready(server_mock, server_port_));

  ScriptGenerator script_generator(ProcessManager::get_origin(),
                                   tmp_dir_.name());

  // launch the router in bootstrap mode
  auto &router = launch_router_for_bootstrap(
      {
          "--bootstrap=127.0.0.1:" + std::to_string(server_port_),
          "--connect-timeout=1",
          "--master-key-reader=" + script_generator.get_fake_reader_script(),
          "--master-key-writer=" + script_generator.get_fake_writer_script(),
      },
      EXIT_FAILURE);

  // check if the bootstrapping failed
  ASSERT_NO_FATAL_FAILURE(check_exit_code(router, EXIT_FAILURE));

  ASSERT_THAT(keyring_path.str(), FileContentEqual("keyring file content"));
}

/**
 * @test
 *       verify bootstrap with --master-key-reader and system deployment
 * layout and bootstrap fails, then original master key is restored.
 */
TEST_F(MasterKeyReaderWriterSystemDeploymentTest,
       MasterKeyRestoredWhenBootstrapFails) {
  // create file with master key
  Path master_key_path(Path(tmp_dir_.name()).join("master_key").str());
  write_to_file(master_key_path, "");

  unsigned server_port = port_pool_.get_next_available();
  ScriptGenerator script_generator(ProcessManager::get_origin(),
                                   tmp_dir_.name());

  // launch the router in bootstrap mode
  auto &router = launch_router_for_bootstrap(
      {
          "--bootstrap=127.0.0.1:" + std::to_string(server_port),
          "--connect-timeout=1",
          "--master-key-reader=" + script_generator.get_reader_script(),
          "--master-key-writer=" + script_generator.get_writer_script(),
      },
      EXIT_FAILURE);

  // check if the bootstrapping failed
  ASSERT_NO_FATAL_FAILURE(check_exit_code(router, EXIT_FAILURE));

  ASSERT_THAT(master_key_path.str(), FileContentEqual(""));
}

#endif

int main(int argc, char *argv[]) {
  init_windows_sockets();
  g_origin_path = Path(argv[0]).dirname();
  ProcessManager::set_origin(Path(argv[0]).dirname());
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}