Debian Codegraph @

mysql-test/suite/rpl/t/rpl_encryption.test

raw
1 # ==== Purpose ====
2 #
3 # This test case will test some basic aspects of binlog_encryption option and
4 # how to fix possible issues.
5 #
6 # Part 1: Cannot enable the option without keyring.
7 #
8 # The server shall abort to start when it is unable to initialize the binary
9 # log encryption.
10 #
11 # Part 2: First time enable.
12 #
13 # The master enables the option at startup.
14 #
15 # The slave enables the option in a client session after installing the
16 # keyring.
17 #
18 # Verify binary log rotation, master key ID and encryption of new binary log
19 # files on master and slave servers.
20 #
21 # Verify that 'ALTER INSTANCE ROTATE BINLOG MASTER KEY' rotates
22 # the maskter key, which is used to encrypt new created relay
23 # logs and re-encrypt previous encrypted relay logs.
24 #
25 # Part 3: Start the slave to replicate from the master.
26 #
27 # Ensure that the slave can replicate content from the master.
28 #
29 # Part 4: Uninstall keyring on slave before stopping replication threads.
30 #
31 # Makes the slave to fail to read existing relay log files by uninstalling
32 # the keyring plugin.
33 #
34 # The slave shall be able to work again after a CHANGE MASTER that discards
35 # the existing relay log files.
36 #
37 # Part 5: Verify that 'ALTER INSTANCE ROTATE BINLOG MASTER KEY' rotates
38 # the maskter key, which is used to encrypt new created binary
39 # logs and re-encrypt previous encrypted binary logs.
40 #
41 # Verify that we can not rotate binlog master key when
42 # 'binlog-encryption' is off.
43 #
44 # ==== Related Bugs and Worklogs ====
45 #
46 # WL#10957: Binary log encryption at rest
47 # BUG#28628991: ASSERTION `!IS_SET()' FAILED.
48 # BUG#28616149: WRONG ERR MESSAGE IF SLAVE IS NOT ABLE TO READ RELAYLOG AS KEYRING UNINSTALLED
49 #
50
51 # This test case is binary log format agnostic
52 --source include/have_binlog_format_row.inc
53 --let $rpl_skip_start_slave= 1
54 --source include/master-slave.inc
55 --source include/have_debug.inc
56
57 # Suppression of error messages
58 CALL mtr.add_suppression('Failed to store key');
59 CALL mtr.add_suppression('Failed to initialize binlog encryption');
60 CALL mtr.add_suppression('Unable to recover binlog encryption master key');
61 CALL mtr.add_suppression('Aborting');
62 CALL mtr.add_suppression('Cannot get file password for encrypted replication log file');
63 CALL mtr.add_suppression('Could not parse relay log event entry');
64 CALL mtr.add_suppression('Failed to fetch key from keyring');
65
66 # Setup test case variables
67 --let $keyring_master= $MYSQL_TMP_DIR/keyring_master
68 --let $keyring_slave= $MYSQL_TMP_DIR/keyring_slave
69 --let $keyring_original_file= $MYSQL_TEST_DIR/std_data/rpl_nogtid_encryption_keyring_master
70 --let $encrypted_binlog_file= $MYSQL_TEST_DIR/std_data/rpl_nogtid_encryption_master-bin.000002
71 --let $MASTER_DATADIR= `select @@datadir`
72 --source include/rpl_connection_slave.inc
73 --let $SLAVE_DATADIR= `select @@datadir`
74 --source include/rpl_connection_master.inc
75
76
77 --echo # Part 1
78 --let $before_master_file=query_get_value(SHOW MASTER STATUS, File, 1)
79
80 # Enabling the option without plug-in
81 --error ER_RPL_ENCRYPTION_MASTER_KEY_RECOVERY_FAILED
82 SET @@GLOBAL.binlog_encryption = ON;
83
84 --let $assert_text=binlog_encryption option shall be OFF
85 --let $option_value = `SELECT variable_value FROM performance_schema.global_variables WHERE variable_name = "binlog_encryption"`
86 --let $assert_cond= "$option_value" = "OFF"
87 --source include/assert.inc
88
89 --let $after_master_file=query_get_value(SHOW MASTER STATUS, File, 1)
90 --let $assert_text=Binary log was not rotated
91 --let $assert_cond= "$before_master_file" = "$after_master_file"
92 --source include/assert.inc
93 --let $rpl_log_file=$MASTER_DATADIR$after_master_file
94 --source include/rpl_get_log_encryption_key_id.inc
95 --let $assert_text=Binary log is not encrypted
96 --let $assert_cond= "$rpl_encryption_key_id" = "None"
97 --source include/assert.inc
98 --let $assert_text= 1st binary log is not encrypted on master
99 --let $assert_cond= "[SHOW BINARY LOGS, Encrypted, 1]" = "No"
100 --source include/assert.inc
101
102 # Stop the master server
103 --let $rpl_server_number= 1
104 --source include/rpl_stop_server.inc
105 # The master will not be able to process some MTR include requests
106 --connection slave
107
108 # Try to restart the master server setting the option, but it should fail
109 --echo Starting the server with "--binlog_encryption=ON" but no keyring installed
110 --let $rpl_server_parameters= --binlog_encryption=ON --log-error=$MYSQL_TMP_DIR/master.err
111 --error 0,1
112 --exec $MYSQLD --defaults-file=$MYSQLTEST_VARDIR/my.cnf --defaults-group-suffix=.$rpl_server_number $rpl_server_parameters
113 --let $assert_file= $MYSQL_TMP_DIR/master.err
114 --let $assert_text= Server failed to initialize binlog encryption
115 --let $assert_select= Failed to initialize binlog encryption
116 --let $assert_count= 1
117 --source include/assert_grep.inc
118 --let $assert_text= Server aborted to start
119 --let $assert_select= Server.*Aborting
120 --let $assert_count= 1
121 --source include/assert_grep.inc
122 --remove_file $MYSQL_TMP_DIR/master.err
123
124 --echo # Part 2
125 # Restart the master loading the keyring
126 --echo Starting the master with "--binlog_encryption=ON" and keyring installed
127 --let $rpl_server_number= 1
128 --let $rpl_server_parameters= $KEYRING_PLUGIN_OPT $KEYRING_PLUGIN_LOAD --keyring_file_data=$keyring_master --binlog_encryption=ON
129 --let $rpl_server_error= 0
130 --let $rpl_omit_print_server_parameters= 1
131 --source include/rpl_start_server.inc
132 --connection master
133
134 --let $assert_text=binlog_encryption option shall be ON
135 --let $option_value = `SELECT variable_value FROM performance_schema.global_variables WHERE variable_name = "binlog_encryption"`
136 --let $assert_cond= "$option_value" = "ON"
137 --source include/assert.inc
138
139 --let $assert_text= 2nd binary log is encrypted on master
140 --let $assert_cond= "[SHOW BINARY LOGS, Encrypted, 2]" = "Yes"
141 --source include/assert.inc
142
143 --let $after_master_file=query_get_value(SHOW MASTER STATUS, File, 1)
144 --let $rpl_log_file=$MASTER_DATADIR$after_master_file
145 --source include/rpl_get_log_encryption_key_id.inc
146 --let $assert_text=Binary log is encrypted using 1st master key
147 --let $assert_cond= RIGHT("$rpl_encryption_key_id", 2) = "_1"
148 --source include/assert.inc
149
150 --let $assert_text=Binary log rotated as expected at master startup
151 --let $assert_cond= RIGHT("$after_master_file", 6) = "000002"
152 --source include/assert.inc
153
154 # Next server startup will rely on this set persist
155 SET PERSIST binlog_encryption = ON;
156
157 --echo Restart the master with keyring installed
158 --let $rpl_server_number= 1
159 --let $rpl_server_parameters= $KEYRING_PLUGIN_OPT $KEYRING_PLUGIN_LOAD --keyring_file_data=$keyring_master
160 --let $rpl_server_error= 0
161 --let $rpl_omit_print_server_parameters= 1
162 --source include/rpl_restart_server.inc
163
164 --let $assert_text=binlog_encryption option shall be ON
165 --let $option_value = `SELECT variable_value FROM performance_schema.global_variables WHERE variable_name = "binlog_encryption"`
166 --let $assert_cond= "$option_value" = "ON"
167 --source include/assert.inc
168
169 --let $assert_text= 3rd binary log is encrypted on master
170 --let $assert_cond= "[SHOW BINARY LOGS, Encrypted, 3]" = "Yes"
171 --source include/assert.inc
172
173 --let $after_master_file=query_get_value(SHOW MASTER STATUS, File, 1)
174 --let $rpl_log_file=$MASTER_DATADIR$after_master_file
175 --source include/rpl_get_log_encryption_key_id.inc
176 --let $assert_text=Binary log is encrypted using 1st master key
177 --let $assert_cond= RIGHT("$rpl_encryption_key_id", 2) = "_1"
178 --source include/assert.inc
179
180 --let $assert_text=Binary log rotated as expected at master startup
181 --let $assert_cond= RIGHT("$after_master_file", 6) = "000003"
182 --source include/assert.inc
183
184 # Restart the slave loading the keyring
185 --source include/rpl_connection_slave.inc
186 --let $assert_text= 1st binary log is not encrypted on slave
187 --let $assert_cond= "[SHOW BINARY LOGS, Encrypted, 1]" = "No"
188 --source include/assert.inc
189
190 --let $relay_log_base=`SELECT @@GLOBAL.relay_log`
191 --let $relay_log_suffix=.000001
192 --let $rpl_log_file=$SLAVE_DATADIR$relay_log_base$relay_log_suffix
193 --source include/rpl_get_log_encryption_key_id.inc
194 --let $assert_text=Relay log is not encrypted for $relay_log_base$relay_log_suffix
195 --let $assert_cond= "$rpl_encryption_key_id" = "None"
196 --source include/assert.inc
197
198 --echo Restart the slave with keyring installed
199 --let $rpl_server_number= 2
200 --let $rpl_server_parameters= $KEYRING_PLUGIN_OPT $KEYRING_PLUGIN_LOAD --keyring_file_data=$keyring_slave --skip-replica-start
201 --let $rpl_server_error= 0
202 --let $rpl_omit_print_server_parameters= 1
203 --source include/rpl_restart_server.inc
204
205 --let $assert_text=binlog_encryption option shall be OFF
206 --let $option_value = `SELECT variable_value FROM performance_schema.global_variables WHERE variable_name = "binlog_encryption"`
207 --let $assert_cond= "$option_value" = "OFF"
208 --source include/assert.inc
209
210 --let $assert_text= 2nd binary log is not encrypted on slave
211 --let $assert_cond= "[SHOW BINARY LOGS, Encrypted, 2]" = "No"
212 --source include/assert.inc
213
214 --let $relay_log_suffix=.000002
215 --let $rpl_log_file=$SLAVE_DATADIR$relay_log_base$relay_log_suffix
216 --source include/rpl_get_log_encryption_key_id.inc
217 --let $assert_text=Relay log is not encrypted for $relay_log_base$relay_log_suffix
218 --let $assert_cond= "$rpl_encryption_key_id" = "None"
219 --source include/assert.inc
220
221 SET GLOBAL binlog_encryption = ON;
222
223 --let $assert_text=binlog_encryption option shall be ON
224 --let $option_value = `SELECT variable_value FROM performance_schema.global_variables WHERE variable_name = "binlog_encryption"`
225 --let $assert_cond= "$option_value" = "ON"
226 --source include/assert.inc
227
228 --let $assert_text= 3rd binary log is encrypted on slave
229 --let $assert_cond= "[SHOW BINARY LOGS, Encrypted, 3]" = "Yes"
230 --source include/assert.inc
231
232 --let $after_master_file=query_get_value(SHOW MASTER STATUS, File, 1)
233 --let $assert_text=Binary log rotated as expected at server startup
234 --let $assert_cond= RIGHT("$after_master_file", 6) = "000003"
235 --source include/assert.inc
236 --let $rpl_log_file=$SLAVE_DATADIR$after_master_file
237 --source include/rpl_get_log_encryption_key_id.inc
238 --let $assert_text=New binary log is encrypted using 1st master key
239 --let $assert_cond= RIGHT("$rpl_encryption_key_id", 2) = "_1"
240 --source include/assert.inc
241 --let $relay_log_suffix=.000004
242 --let $rpl_log_file=$SLAVE_DATADIR$relay_log_base$relay_log_suffix
243 --source include/rpl_get_log_encryption_key_id.inc
244 --let $assert_text=Relay log rotated as expected at server startup
245 --let $assert_cond= "$rpl_encryption_key_id" = "Error: unable to open the file"
246 --source include/assert.inc
247 --let $relay_log_suffix=.000003
248 --let $rpl_log_file=$SLAVE_DATADIR$relay_log_base$relay_log_suffix
249 --source include/rpl_get_log_encryption_key_id.inc
250 --let $assert_text=Relay log is encrypted for $relay_log_base$relay_log_suffix
251 --let $assert_cond= RIGHT("$rpl_encryption_key_id", 2) = "_1"
252 --source include/assert.inc
253
254 # Verify that 'ALTER INSTANCE ROTATE BINLOG MASTER KEY' rotates
255 # the maskter key, which is used to encrypt new created relay
256 # logs and re-encrypt previous encrypted relay logs.
257 --let $debug_point=verify_unusable_encryption_keys_are_purged
258 --source include/add_debug_point.inc
259 ALTER INSTANCE ROTATE BINLOG MASTER KEY;
260 --source include/remove_debug_point.inc
261
262 --let $relay_log_suffix=.000003
263 --let $rpl_log_file=$SLAVE_DATADIR$relay_log_base$relay_log_suffix
264 --source include/rpl_get_log_encryption_key_id.inc
265 --let $assert_text=The third relay log is re-encrypted using second master key
266 --let $assert_cond= RIGHT("$rpl_encryption_key_id", 2) = "_2"
267 --source include/assert.inc
268
269 --let $relay_log_suffix=.000004
270 --let $rpl_log_file=$SLAVE_DATADIR$relay_log_base$relay_log_suffix
271 --source include/rpl_get_log_encryption_key_id.inc
272 --let $assert_text=The after relay log is encrypted using second master key
273 --let $assert_cond= RIGHT("$rpl_encryption_key_id", 2) = "_2"
274 --source include/assert.inc
275
276 --echo # Part 3
277 --source include/rpl_connection_master.inc
278 # Slave can replicate from encrypted binary log file
279 CREATE TABLE t1 (c1 INT PRIMARY KEY);
280 INSERT INTO t1 (c1) VALUES (1), (2), (3);
281 INSERT INTO t1 (c1) VALUES (4), (5), (6);
282 --source include/rpl_connection_slave.inc
283 --source include/start_slave.inc
284 --source include/rpl_connection_master.inc
285 --source include/sync_slave_sql_with_master.inc
286 --let $gtid_mode= `SELECT @@GLOBAL.gtid_mode`
287 --let $AUTO_POSITION=1
288 if ($gtid_mode == 'OFF') {
289 --let $recover_master_file=$_saved_file
290 --let $recover_master_pos=$_saved_pos
291 --let $AUTO_POSITION=0
292 }
293
294 --echo # Part 4
295 --source include/rpl_connection_slave.inc
296 # Force keyring removal
297 UNINSTALL PLUGIN keyring_file;
298 --remove_file $keyring_slave
299 --source include/rpl_connection_master.inc
300 --source include/sync_slave_sql_with_master.inc
301
302 # Make SQL thread to read next relay log file
303 FLUSH LOGS;
304 # The binary log shall rotate and is still encrypted
305 --let $assert_text= 5th binary log is encrypted on slave
306 --let $assert_cond= "[SHOW BINARY LOGS, Encrypted, 5]" = "Yes"
307 --source include/assert.inc
308 --let $encrypted_master_file=query_get_value(SHOW MASTER STATUS, File, 1)
309 --let $assert_text=Binary log rotated as expected by the FLUSH LOGS
310 --let $assert_cond=RIGHT("$encrypted_master_file", 6) = "000005"
311 --source include/assert.inc
312 --let $rpl_log_file=$SLAVE_DATADIR$encrypted_master_file
313 --source include/rpl_get_log_encryption_key_id.inc
314 --let $assert_text=New binary log is still encrypted
315 --let $assert_cond=RIGHT("$rpl_encryption_key_id", 2) = "_2"
316 --source include/assert.inc
317 --let $slave_sql_errno= convert_error(ER_REPLICA_RELAY_LOG_READ_FAILURE)
318 --source include/wait_for_slave_sql_error.inc
319 --source include/stop_slave_io.inc
320
321 RESET SLAVE ALL;
322 # A new relay log file is always created for the default channel
323 --let $encrypted_relay_file=`SELECT CONCAT(@@GLOBAL.relay_log,'.000001')`
324 --let $rpl_log_file=$SLAVE_DATADIR$encrypted_relay_file
325 --source include/rpl_get_log_encryption_key_id.inc
326 --let $assert_text=New relay log is still encrypted
327 --let $assert_cond=RIGHT("$rpl_encryption_key_id", 2) = "_2"
328 --source include/assert.inc
329
330 # The server is still able to encrypt new relay log files
331 --replace_result $MASTER_MYPORT MASTER_MYPORT
332 --eval CHANGE REPLICATION SOURCE TO SOURCE_HOST='127.0.0.1', SOURCE_PORT=$MASTER_MYPORT, SOURCE_USER='root'
333 --let $encrypted_relay_file=`SELECT CONCAT(@@GLOBAL.relay_log,'.000001')`
334 --let $rpl_log_file=$SLAVE_DATADIR$encrypted_relay_file
335 --source include/rpl_get_log_encryption_key_id.inc
336 --let $assert_text=New relay log is still encrypted
337 --let $assert_cond=RIGHT("$rpl_encryption_key_id", 2) = "_2"
338 --source include/assert.inc
339
340 # SQL thread is still not able to read encrypted relay log files
341 START SLAVE SQL_THREAD;
342 --let $slave_sql_errno= convert_error(ER_REPLICA_FATAL_ERROR)
343 --source include/wait_for_slave_sql_error.inc
344
345 # Disable the option to not rely on keyring anymore
346 SET GLOBAL binlog_encryption = OFF;
347 # The binary log shall rotate and no more be encrypted
348 --let $assert_text= 6th binary log is not encrypted on slave
349 --let $assert_cond= "[SHOW BINARY LOGS, Encrypted, 6]" = "No"
350 --source include/assert.inc
351 --let $disabled_master_file=query_get_value(SHOW MASTER STATUS, File, 1)
352 --let $assert_text=Binary log rotated as expected when disabling the option
353 --let $assert_cond=RIGHT("$disabled_master_file", 6) = "000006"
354 --source include/assert.inc
355 --let $rpl_log_file=$SLAVE_DATADIR$disabled_master_file
356 --source include/rpl_get_log_encryption_key_id.inc
357 --let $assert_text=New binary log is not encrypted
358 --let $assert_cond="$rpl_encryption_key_id" = "None"
359 --source include/assert.inc
360 RESET SLAVE ALL;
361 # A new relay log file is always created for the default channel
362 --let $encrypted_relay_file=`SELECT CONCAT(@@GLOBAL.relay_log,'.000001')`
363 --let $rpl_log_file=$SLAVE_DATADIR$encrypted_relay_file
364 --source include/rpl_get_log_encryption_key_id.inc
365 --let $assert_text=New relay log is not encrypted
366 --let $assert_cond="$rpl_encryption_key_id" = "None"
367 --source include/assert.inc
368 # Now, both replication threads shall work fine again
369 --replace_result $MASTER_MYPORT MASTER_MYPORT
370 --eval CHANGE REPLICATION SOURCE TO SOURCE_HOST='127.0.0.1', SOURCE_PORT=$MASTER_MYPORT, SOURCE_USER='root'
371 --let $encrypted_relay_file=`SELECT CONCAT(@@GLOBAL.relay_log,'.000001')`
372 --let $rpl_log_file=$SLAVE_DATADIR$encrypted_relay_file
373 --source include/rpl_get_log_encryption_key_id.inc
374 --let $assert_text=New relay log is not encrypted
375 --let $assert_cond="$rpl_encryption_key_id" = "None"
376 --source include/assert.inc
377 if ($AUTO_POSITION) {
378 --disable_query_log
379 CHANGE REPLICATION SOURCE TO SOURCE_AUTO_POSITION=1;
380 --enable_query_log
381 }
382 if (!$AUTO_POSITION) {
383 --disable_query_log
384 --eval CHANGE REPLICATION SOURCE TO SOURCE_LOG_FILE='$recover_master_file', SOURCE_LOG_POS=$recover_master_pos
385 --enable_query_log
386 }
387 --source include/start_slave.inc
388
389
390 --source include/rpl_connection_master.inc
391
392 --let $third_binary_log=query_get_value(SHOW MASTER STATUS, File, 1)
393 --echo third_binary_log: $third_binary_log
394 --let $rpl_log_file=$MASTER_DATADIR$third_binary_log
395 --source include/rpl_get_log_encryption_key_id.inc
396 --let $assert_text=the third binary log is encrypted using first master key
397 --let $assert_cond= RIGHT("$rpl_encryption_key_id", 2) = "_1"
398 --source include/assert.inc
399
400 --echo # Part 5
401 --let $debug_point=verify_unusable_encryption_keys_are_purged
402 --source include/add_debug_point.inc
403 ALTER INSTANCE ROTATE BINLOG MASTER KEY;
404 --source include/remove_debug_point.inc
405 --let $assert_text=the fourth binary log is encrypted on master
406 --let $assert_cond= "[SHOW BINARY LOGS, Encrypted, 4]" = "Yes"
407 --source include/assert.inc
408
409 --let $rpl_log_file=$MASTER_DATADIR$third_binary_log
410 --source include/rpl_get_log_encryption_key_id.inc
411 --let $assert_text=the third binary log is re-encrypted using second master key
412 --let $assert_cond= RIGHT("$rpl_encryption_key_id", 2) = "_2"
413 --source include/assert.inc
414
415 --let $fourth_binary_log=query_get_value(SHOW MASTER STATUS, File, 1)
416 --let $rpl_log_file=$MASTER_DATADIR$fourth_binary_log
417 --source include/rpl_get_log_encryption_key_id.inc
418 --let $assert_text=the fourth binary log is encrypted using second master key
419 --let $assert_cond= RIGHT("$rpl_encryption_key_id", 2) = "_2"
420 --source include/assert.inc
421
422 SET PERSIST binlog_encryption = OFF;
423 --let $assert_text= 5th binary log is not encrypted on master
424 --let $assert_cond= "[SHOW BINARY LOGS, Encrypted, 5]" = "No"
425 --source include/assert.inc
426
427 # Verify that we can not rotate binlog master key when
428 # 'binlog-encryption' is off.
429 --error ER_RPL_ENCRYPTION_CANNOT_ROTATE_BINLOG_MASTER_KEY
430 ALTER INSTANCE ROTATE BINLOG MASTER KEY;
431
432 # Cleanup
433 RESET PERSIST binlog_encryption;
434 UNINSTALL PLUGIN keyring_file;
435 --remove_file $keyring_master
436 DROP TABLE t1;
437 --source include/sync_slave_sql_with_master.inc
438 --source include/stop_slave.inc
439 --let $rpl_server_number= 1
440 --let $rpl_server_parameters=
441 --source include/rpl_restart_server.inc
442 --let $rpl_server_number= 2
443 --let $rpl_server_parameters=
444 --source include/rpl_restart_server.inc
445 --source include/start_slave.inc
446 --source include/rpl_end.inc