| 1 |
# ==== Purpose ==== |
| 2 |
# |
| 3 |
# This script aims at testing read and truncate operations on encrypted binary |
| 4 |
# log files, as well as related error conditions. |
| 5 |
# |
| 6 |
# Part 1: At server startup, truncate incomplete binary log file |
| 7 |
# |
| 8 |
# The test case will make an encrypted binary log file the last one, forcing |
| 9 |
# recovery checks to evaluate it (see file details below). |
| 10 |
# |
| 11 |
# Before allowing a successful recover, failures to access the encrypted file |
| 12 |
# will be simulated playing with the keyring file and using debug |
| 13 |
# instrumentation. The simulations are the same as sections 4.1 and 4.2. |
| 14 |
# |
| 15 |
# After the attempts with simulated failures, the server will be restarted and |
| 16 |
# become operational again. |
| 17 |
# |
| 18 |
# This will ensure that the server is able to read from the encrypted binary |
| 19 |
# log file on binary log recovery and also is able to open the encrypted file |
| 20 |
# for writing (as no key need to be generated) to clear |
| 21 |
# LOG_EVENT_BINLOG_IN_USE_F flag on it and also to truncate it in the end of |
| 22 |
# the last complete transaction. |
| 23 |
# |
| 24 |
# Part 2: Show binlog events/Show binary logs |
| 25 |
# |
| 26 |
# This will ensure that the server is able to decrypt and access an encrypted |
| 27 |
# binary log file from user requests. |
| 28 |
# |
| 29 |
# It will also ensure that the server is able to show which binary log files |
| 30 |
# are encrypted or not. |
| 31 |
# |
| 32 |
# Part 3: Replicate to a slave server |
| 33 |
# |
| 34 |
# This will ensure that the master is able to replicate from an encrypted |
| 35 |
# binary log file to a slave. It will read from the encrypted binary log file |
| 36 |
# but will replicate its content as a plain binary log events stream (without |
| 37 |
# encryption) to the slave. |
| 38 |
# |
| 39 |
# Part 4: Errors |
| 40 |
# |
| 41 |
# Part 4.1: Simulate errors using debug instrumentation |
| 42 |
# |
| 43 |
# 4.1.1 Use an encryption key that cannot decode the file password |
| 44 |
# |
| 45 |
# By corrupting the encryption key using debug instrumentation, the server |
| 46 |
# shall not be able to recognize the binary log data stream as the decrypted |
| 47 |
# content will not match the original binary log data stream. |
| 48 |
# |
| 49 |
# The remaining simulated errors shall throw replication logs encryption |
| 50 |
# specific errors. |
| 51 |
# |
| 52 |
# 4.1.2 Invalid key type returned by keyring. |
| 53 |
# 4.1.3 Invalid key size returned by keyring. |
| 54 |
# 4.1.4 Unable to read the encrypted header version. |
| 55 |
# 4.1.5 Unsupported encrypted header version. |
| 56 |
# 4.1.6 Read out of header boundaries. |
| 57 |
# 4.1.7 Header contains an unsupported field type. |
| 58 |
# 4.1.8 Header is missing the key ID. |
| 59 |
# 4.1.9 Header is missing the encrypted password. |
| 60 |
# 4.1.10 Header is missing the IV. |
| 61 |
# 4.1.11 Incomplete encryption header. |
| 62 |
# |
| 63 |
# Part 4.2: Use a keyring file without the encryption key |
| 64 |
# |
| 65 |
# After replacing the keyring file with an empty keyring file, the server shall |
| 66 |
# throw an error when requested to access the encrypted binary log file. |
| 67 |
# |
| 68 |
# Part 4.3: Uninstall the keyring from the master |
| 69 |
# |
| 70 |
# After uninstalling the keyring_file plug-in, the server shall throw an error |
| 71 |
# when requested to access the encrypted binary log file. |
| 72 |
# |
| 73 |
# Part 5: mysqlbinlog is unable to dump encrypted binary logs |
| 74 |
# |
| 75 |
# Request mysqlbinlog to dump the content of the encrypted binary log file |
| 76 |
# and parse the output asserting that the expected error message was thrown. |
| 77 |
# |
| 78 |
# |
| 79 |
# This test case rely in two previously generated files: |
| 80 |
# |
| 81 |
# encrypted binary log file |
| 82 |
# ------------------------- |
| 83 |
|
| 84 |
# It was encrypted by a server using V1 encryption. |
| 85 |
# |
| 86 |
# The server that generated it was running with GTID_MODE = OFF, executed the |
| 87 |
# following statements that were logged into the file: |
| 88 |
# - CREATE TABLE t1 (c1 INT PRIMARY KEY); |
| 89 |
# - INSERT INTO t1 (c1) VALUES (1, 2, 3); |
| 90 |
# - INSERT INTO t1 (c1) VALUES (4, 5, 6); |
| 91 |
# - DROP TABLE t1; |
| 92 |
# |
| 93 |
# The file was copied before binary log rotation, so it still has FD's |
| 94 |
# LOG_EVENT_BINLOG_IN_USE_F flag set, and it was manually truncated in the |
| 95 |
# middle of the Query event with the "DROP TABLE t1:" statement. |
| 96 |
# |
| 97 |
# Once recovered, the master shall clear the LOG_EVENT_BINLOG_IN_USE_F flag and |
| 98 |
# truncate the file in the end of last complete transaction. |
| 99 |
# |
| 100 |
# keyring original file |
| 101 |
# --------------------- |
| 102 |
# |
| 103 |
# This is the keyring file where the server that created the above mentioned |
| 104 |
# binary log file stored the encryption key that shall be used to decrypt the |
| 105 |
# file password. |
| 106 |
# |
| 107 |
# |
| 108 |
# ==== Related Bugs and Worklogs ==== |
| 109 |
# |
| 110 |
# WL#10957: Binary log encryption at rest |
| 111 |
# |
| 112 |
|
| 113 |
# This test case is binary log format agnostic |
| 114 |
--source include/have_debug.inc |
| 115 |
--source include/have_binlog_format_row.inc |
| 116 |
--let $rpl_skip_start_slave= 1 |
| 117 |
--source include/master-slave.inc |
| 118 |
|
| 119 |
--let $keyring_file= $MYSQL_TMP_DIR/keyring_master |
| 120 |
--let $keyring_original_file= $MYSQL_TEST_DIR/std_data/rpl_nogtid_encryption_keyring_master |
| 121 |
--let $encrypted_binlog_file= $MYSQL_TEST_DIR/std_data/rpl_nogtid_encryption_master-bin.000002 |
| 122 |
--let $MASTER_DATADIR= `select @@datadir` |
| 123 |
# Makes the index file to contain two files (we will replace the second one |
| 124 |
# by a previously generated encrypted binary log file. |
| 125 |
FLUSH LOCAL LOGS; |
| 126 |
--let $binlog_file= query_get_value(SHOW MASTER STATUS, File, 1) |
| 127 |
--let $assert_text= Master status shall point to master-bin.000002 |
| 128 |
--let $assert_cond= "$binlog_file" = "master-bin.000002" |
| 129 |
--source include/assert.inc |
| 130 |
--let $binlog_file_path= $MASTER_DATADIR/$binlog_file |
| 131 |
|
| 132 |
# Stop the master server |
| 133 |
--let $rpl_server_number= 1 |
| 134 |
--source include/rpl_stop_server.inc |
| 135 |
--connection slave |
| 136 |
# Replace the binary log file |
| 137 |
--remove_file $binlog_file_path |
| 138 |
--copy_file $encrypted_binlog_file $binlog_file_path |
| 139 |
|
| 140 |
--echo # Part 1 |
| 141 |
--let $point= 0 |
| 142 |
--let $master_error_log=$MYSQL_TMP_DIR/master.err |
| 143 |
--let $keyring_parameters=$KEYRING_PLUGIN_OPT $KEYRING_PLUGIN_LOAD --keyring_file_data=$keyring_file |
| 144 |
while ($point < 13) |
| 145 |
{ |
| 146 |
--inc $point |
| 147 |
--let $rpl_server_parameters= $keyring_parameters --log-error=$master_error_log |
| 148 |
if ($point == 1) |
| 149 |
{ |
| 150 |
# Using a keyring key without the key to access the file |
| 151 |
--let $rpl_server_parameters= --log-error=$master_error_log |
| 152 |
--let $debug= |
| 153 |
--let $error_message=Cannot get file password for encrypted replication log file |
| 154 |
} |
| 155 |
if ($point == 2) |
| 156 |
{ |
| 157 |
# Using a keyring key without the key to access the file |
| 158 |
--let $debug= |
| 159 |
--let $error_message=Cannot get file password for encrypted replication log file |
| 160 |
} |
| 161 |
if ($point == 3) |
| 162 |
{ |
| 163 |
# Corrupting the encryption key (plain binlog stream magic is not recognized) |
| 164 |
--let $debug=--debug=d,corrupt_replication_encryption_key |
| 165 |
--let $error_message=Binlog has bad magic number |
| 166 |
} |
| 167 |
if ($point == 4) |
| 168 |
{ |
| 169 |
# Corrupting the encryption key type |
| 170 |
--let $debug=--debug=d,corrupt_replication_encryption_key_type |
| 171 |
--let $error_message=Fetched an invalid key from keyring |
| 172 |
} |
| 173 |
if ($point == 5) |
| 174 |
{ |
| 175 |
# Corrupting the encryption key size |
| 176 |
--let $debug=--debug=d,corrupt_replication_encryption_key_size |
| 177 |
--let $error_message=Fetched an invalid key from keyring |
| 178 |
} |
| 179 |
if ($point == 6) |
| 180 |
{ |
| 181 |
# Corrupt the encrypted header version |
| 182 |
--let $debug=--debug=d,corrupt_encrypted_header_version |
| 183 |
--let $error_message=Unable to determine encryption header version |
| 184 |
} |
| 185 |
if ($point == 7) |
| 186 |
{ |
| 187 |
# Force an unsupported encryption header version |
| 188 |
--let $debug=--debug=d,force_encrypted_header_version_2 |
| 189 |
--let $error_message=Unsupported encryption header version |
| 190 |
} |
| 191 |
if ($point == 8) |
| 192 |
{ |
| 193 |
# Force a header with fields over the header size |
| 194 |
--let $debug=--debug=d,corrupt_encryption_header_read_above_header_size |
| 195 |
--let $error_message=Header is corrupted |
| 196 |
} |
| 197 |
if ($point == 9) |
| 198 |
{ |
| 199 |
# Force a header with an unknown field type |
| 200 |
--let $debug=--debug=d,corrupt_encryption_header_unknown_field_type |
| 201 |
--let $error_message=Unknown field type |
| 202 |
} |
| 203 |
if ($point == 10) |
| 204 |
{ |
| 205 |
# Force a header without a key ID |
| 206 |
--let $debug=--debug=d,corrupt_encryption_header_missing_key_id |
| 207 |
--let $error_message=Header is missing the replication encryption key ID |
| 208 |
} |
| 209 |
if ($point == 11) |
| 210 |
{ |
| 211 |
# Force a header without password |
| 212 |
--let $debug=--debug=d,corrupt_encryption_header_missing_password |
| 213 |
--let $error_message=Header is missing the encrypted password |
| 214 |
} |
| 215 |
if ($point == 12) |
| 216 |
{ |
| 217 |
# Force a header without IV |
| 218 |
--let $debug=--debug=d,corrupt_encryption_header_missing_iv |
| 219 |
--let $error_message=Header is missing the IV |
| 220 |
} |
| 221 |
if ($point == 13) |
| 222 |
{ |
| 223 |
# Force an incomplete (< 512 bytes) encryption header |
| 224 |
--let $debug=--debug=d,force_incomplete_encryption_header |
| 225 |
--let $error_message=Header is incomplete |
| 226 |
} |
| 227 |
--echo Try to restart the master with debug instrumentation |
| 228 |
--error 0,1 |
| 229 |
--exec $MYSQLD --defaults-file=$MYSQLTEST_VARDIR/my.cnf --defaults-group-suffix=.1 $rpl_server_parameters $debug |
| 230 |
--let $assert_file= $master_error_log |
| 231 |
--let $assert_text= Server reported: $error_message |
| 232 |
--let $assert_select= $error_message |
| 233 |
--let $assert_count= 1 |
| 234 |
--source include/assert_grep.inc |
| 235 |
--let $assert_text= Server failed to initialize binary log for recovery |
| 236 |
--let $assert_select= Can.t init tc log |
| 237 |
--let $assert_count= 1 |
| 238 |
--source include/assert_grep.inc |
| 239 |
--let $assert_text= Server aborted to start |
| 240 |
--let $assert_select= Server.*Aborting |
| 241 |
--let $assert_count= 1 |
| 242 |
--source include/assert_grep.inc |
| 243 |
--remove_file $master_error_log |
| 244 |
if ($point == 2) |
| 245 |
{ |
| 246 |
# Copy the keyring file |
| 247 |
--remove_file $keyring_file |
| 248 |
--copy_file $keyring_original_file $keyring_file |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
# Restart the server |
| 253 |
--let $rpl_server_number= 1 |
| 254 |
--let $rpl_server_parameters= $keyring_parameters |
| 255 |
--let $rpl_omit_print_server_parameters= 1 |
| 256 |
--source include/rpl_start_server.inc |
| 257 |
--connection master |
| 258 |
|
| 259 |
--echo # Part 2 |
| 260 |
--let $assert_text= 1st binary log is not encrypted |
| 261 |
--let $assert_cond= "[SHOW BINARY LOGS, Encrypted, 1]" = "No" |
| 262 |
--source include/assert.inc |
| 263 |
--let $assert_text= 2nd binary log is encrypted |
| 264 |
--let $assert_cond= "[SHOW BINARY LOGS, Encrypted, 2]" = "Yes" |
| 265 |
--source include/assert.inc |
| 266 |
--let $assert_text= 3rd binary log is not encrypted |
| 267 |
--let $assert_cond= "[SHOW BINARY LOGS, Encrypted, 3]" = "No" |
| 268 |
--source include/assert.inc |
| 269 |
--let $binlog_file= master-bin.000002 |
| 270 |
--let $binlog_start= query_get_value(SHOW BINLOG EVENTS IN '$binlog_file' LIMIT 1, End_log_pos, 1) |
| 271 |
--let $keep_gtid_events= 1 |
| 272 |
--source include/show_binlog_events.inc |
| 273 |
--let $binlog_start = |
| 274 |
|
| 275 |
--echo # Part 3 |
| 276 |
# Start slave threads so it can sync with master |
| 277 |
--source include/rpl_connection_slave.inc |
| 278 |
--source include/start_slave.inc |
| 279 |
--source include/rpl_connection_master.inc |
| 280 |
--source include/sync_slave_sql_with_master.inc |
| 281 |
--let $assert_text= Slave shall have t1 with a 6 rows on it |
| 282 |
--let $assert_cond= [SELECT COUNT(*) AS t1_rows FROM t1, t1_rows, 1] = 6 |
| 283 |
--source include/assert.inc |
| 284 |
# Slave cleanup |
| 285 |
DROP TABLE t1; |
| 286 |
--source include/stop_slave.inc |
| 287 |
|
| 288 |
--echo # Part 4 |
| 289 |
--source include/rpl_connection_master.inc |
| 290 |
|
| 291 |
--echo # Part 4.1 |
| 292 |
--echo # Part 4.1.1 |
| 293 |
--let $debug_point= corrupt_replication_encryption_key |
| 294 |
--source include/add_debug_point.inc |
| 295 |
--error ER_ERROR_WHEN_EXECUTING_COMMAND |
| 296 |
--eval SHOW BINLOG EVENTS IN '$binlog_file' |
| 297 |
--source include/remove_debug_point.inc |
| 298 |
--echo # Part 4.1.2 |
| 299 |
--let $debug_point= corrupt_replication_encryption_key_type |
| 300 |
--source include/add_debug_point.inc |
| 301 |
--error ER_RPL_ENCRYPTION_KEYRING_INVALID_KEY |
| 302 |
--eval SHOW BINLOG EVENTS IN '$binlog_file' |
| 303 |
--source include/remove_debug_point.inc |
| 304 |
--echo # Part 4.1.3 |
| 305 |
--let $debug_point= corrupt_replication_encryption_key_size |
| 306 |
--source include/add_debug_point.inc |
| 307 |
--error ER_RPL_ENCRYPTION_KEYRING_INVALID_KEY |
| 308 |
--eval SHOW BINLOG EVENTS IN '$binlog_file' |
| 309 |
--source include/remove_debug_point.inc |
| 310 |
--echo # Part 4.1.4 |
| 311 |
--let $debug_point= corrupt_encrypted_header_version |
| 312 |
--source include/add_debug_point.inc |
| 313 |
--error ER_RPL_ENCRYPTION_HEADER_ERROR |
| 314 |
--eval SHOW BINLOG EVENTS IN '$binlog_file' |
| 315 |
--source include/remove_debug_point.inc |
| 316 |
--echo # Part 4.1.5 |
| 317 |
--let $debug_point= force_encrypted_header_version_2 |
| 318 |
--source include/add_debug_point.inc |
| 319 |
--error ER_RPL_ENCRYPTION_HEADER_ERROR |
| 320 |
--eval SHOW BINLOG EVENTS IN '$binlog_file' |
| 321 |
--source include/remove_debug_point.inc |
| 322 |
--echo # Part 4.1.6 |
| 323 |
--let $debug_point= corrupt_encryption_header_read_above_header_size |
| 324 |
--source include/add_debug_point.inc |
| 325 |
--error ER_RPL_ENCRYPTION_HEADER_ERROR |
| 326 |
--eval SHOW BINLOG EVENTS IN '$binlog_file' |
| 327 |
--source include/remove_debug_point.inc |
| 328 |
--echo # Part 4.1.7 |
| 329 |
--let $debug_point= corrupt_encryption_header_unknown_field_type |
| 330 |
--source include/add_debug_point.inc |
| 331 |
--error ER_RPL_ENCRYPTION_HEADER_ERROR |
| 332 |
--eval SHOW BINLOG EVENTS IN '$binlog_file' |
| 333 |
--source include/remove_debug_point.inc |
| 334 |
--echo # Part 4.1.8 |
| 335 |
--let $debug_point= corrupt_encryption_header_missing_key_id |
| 336 |
--source include/add_debug_point.inc |
| 337 |
--error ER_RPL_ENCRYPTION_HEADER_ERROR |
| 338 |
--eval SHOW BINLOG EVENTS IN '$binlog_file' |
| 339 |
--source include/remove_debug_point.inc |
| 340 |
--echo # Part 4.1.9 |
| 341 |
--let $debug_point= corrupt_encryption_header_missing_password |
| 342 |
--source include/add_debug_point.inc |
| 343 |
--error ER_RPL_ENCRYPTION_HEADER_ERROR |
| 344 |
--eval SHOW BINLOG EVENTS IN '$binlog_file' |
| 345 |
--source include/remove_debug_point.inc |
| 346 |
--echo # Part 4.1.10 |
| 347 |
--let $debug_point= corrupt_encryption_header_missing_iv |
| 348 |
--source include/add_debug_point.inc |
| 349 |
--error ER_RPL_ENCRYPTION_HEADER_ERROR |
| 350 |
--eval SHOW BINLOG EVENTS IN '$binlog_file' |
| 351 |
--source include/remove_debug_point.inc |
| 352 |
--echo # Part 4.1.11 |
| 353 |
--let $debug_point= force_incomplete_encryption_header |
| 354 |
--source include/add_debug_point.inc |
| 355 |
--error ER_RPL_ENCRYPTION_HEADER_ERROR |
| 356 |
--eval SHOW BINLOG EVENTS IN '$binlog_file' |
| 357 |
--source include/remove_debug_point.inc |
| 358 |
|
| 359 |
--echo # Part 4.2 |
| 360 |
UNINSTALL PLUGIN keyring_file; |
| 361 |
# Stop the master server |
| 362 |
--let $rpl_server_number= 1 |
| 363 |
--source include/rpl_stop_server.inc |
| 364 |
# Remove the keyring file to create a new empty one |
| 365 |
--remove_file $keyring_file |
| 366 |
# Restart the server |
| 367 |
--let $rpl_server_number= 1 |
| 368 |
--let $rpl_server_parameters= $KEYRING_PLUGIN_OPT $KEYRING_PLUGIN_LOAD --keyring_file_data=$keyring_file |
| 369 |
--source include/rpl_start_server.inc |
| 370 |
--error ER_RPL_ENCRYPTION_KEY_NOT_FOUND |
| 371 |
--eval SHOW BINLOG EVENTS IN '$binlog_file' |
| 372 |
|
| 373 |
--echo # Part 4.3 |
| 374 |
UNINSTALL PLUGIN keyring_file; |
| 375 |
--error ER_RPL_ENCRYPTION_FAILED_TO_FETCH_KEY |
| 376 |
--eval SHOW BINLOG EVENTS IN '$binlog_file' |
| 377 |
|
| 378 |
--echo # Part 5 |
| 379 |
--let $output_file= $MYSQLTEST_VARDIR/tmp/mysqlbinlog.log |
| 380 |
--let $error_file= $MYSQLTEST_VARDIR/tmp/mysqlbinlog.err |
| 381 |
--error 1 |
| 382 |
--exec $MYSQL_BINLOG -F $binlog_file_path > $output_file 2> $error_file |
| 383 |
--let $assert_text= mysqlbinlog reported it does not support reading encrypted log files |
| 384 |
--let $assert_file= $error_file |
| 385 |
--let $assert_count= 1 |
| 386 |
--let $assert_select= Reading encrypted log files directly is not supported |
| 387 |
--source include/assert_grep.inc |
| 388 |
--remove_file $output_file |
| 389 |
--remove_file $error_file |
| 390 |
|
| 391 |
--let $binlog_file = |
| 392 |
--let $binlog_start = |
| 393 |
|
| 394 |
# Cleanup |
| 395 |
--remove_file $keyring_file |
| 396 |
--source include/rpl_connection_slave.inc |
| 397 |
--source include/start_slave.inc |
| 398 |
--source include/rpl_end.inc |