|
| 1 | +# ==== Purpose ==== |
| 2 | +# |
| 3 | +# Retrieve a given replication log file encryption key id. |
| 4 | +# |
| 5 | +# ==== Usage ==== |
| 6 | +# |
| 7 | +# --let $rpl_log_file= <BINARY OR RELAY LOG FILE> |
| 8 | +# [--let $rpl_debug= 0] |
| 9 | +# --source include/rpl_get_log_encryption_key_id.inc |
| 10 | +# |
| 11 | +# Parameters: |
| 12 | +# |
| 13 | +# $rpl_log_file |
| 14 | +# The file to be inspected. |
| 15 | +# |
| 16 | +# $rpl_debug=1 |
| 17 | +# Print extra debugging information. |
| 18 | +# |
| 19 | +# Output variable: |
| 20 | +# |
| 21 | +# $rpl_encryption_key_id will be set with: |
| 22 | +# |
| 23 | +# - Error: Error fetching the info. |
| 24 | +# |
| 25 | +# - None: The file is not encrypted. |
| 26 | +# |
| 27 | +# - MySQLReplicationKey_<UUID>_<SEQNO>: a replication encryption key. |
| 28 | +# |
| 29 | + |
| 30 | +--let $_rgeki_suffix= `SELECT UUID()` |
| 31 | +--let _RPL_RESULT_FILE= $MYSQLTEST_VARDIR/tmp/_rgeki_$_rgeki_suffix |
| 32 | +--let _RPL_LOG_FILE= $rpl_log_file |
| 33 | +--let _RPL_DEBUG= $rpl_debug |
| 34 | +--let $rpl_encryption_key_id=Error |
| 35 | + |
| 36 | +# Write file to make mysql-test-run.pl start up the server again |
| 37 | +# Because mysqltest is such a wonderful language, we use perl instead. |
| 38 | +perl; |
| 39 | + my $log_file= $ENV{'_RPL_LOG_FILE'}; |
| 40 | + my $result_file= $ENV{'_RPL_RESULT_FILE'}; |
| 41 | + if ($ENV{'_RPL_DEBUG'}) |
| 42 | + { |
| 43 | + print "# debug: log_file='$log_file'\n"; |
| 44 | + } |
| 45 | + |
| 46 | + # Open the file in raw mode |
| 47 | + open LFILE, '<:raw', $log_file or die "Error opening $log_file: $!"; |
| 48 | + open RFILE, "> $result_file" or die "Error opening $result_file: $!"; |
| 49 | + |
| 50 | + # Read binlog magic |
| 51 | + my $bytes_read = read LFILE, my $magic, 4; |
| 52 | + die 'Got $bytes_read but expected 4' unless $bytes_read == 4; |
| 53 | + |
| 54 | + if ($ENV{'_RPL_DEBUG'}) |
| 55 | + { |
| 56 | + print "# debug: magic='$magic'\n"; |
| 57 | + } |
| 58 | + |
| 59 | + my $plain_magic = "\xfe\x62\x69\x6e"; |
| 60 | + my $encrypted_magic = "\xfd\x62\x69\x6e"; |
| 61 | + |
| 62 | + if ($magic eq $plain_magic) { |
| 63 | + # Ordinary binary log |
| 64 | + if ($ENV{'_RPL_DEBUG'}) |
| 65 | + { |
| 66 | + print "# debug: ordinary log file\n"; |
| 67 | + } |
| 68 | + print RFILE "--let \$rpl_encryption_key_id=None\n" or die "Error writing to $result_file: $!"; |
| 69 | + } elsif ($magic eq $encrypted_magic) { |
| 70 | + # Encrypted binary log |
| 71 | + if ($ENV{'_RPL_DEBUG'}) |
| 72 | + { |
| 73 | + print "# debug: encrypted log file\n"; |
| 74 | + } |
| 75 | + $bytes_read = read LFILE, my $version, 1; |
| 76 | + die 'Got $bytes_read but expected 1' unless $bytes_read == 1; |
| 77 | + if ($version cmp "\x01") |
| 78 | + { |
| 79 | + die 'Unexpected replication encryption header version'; |
| 80 | + } |
| 81 | + $bytes_read = read LFILE, my $field_type, 1; |
| 82 | + die 'Got $bytes_read but expected 1' unless $bytes_read == 1; |
| 83 | + if ($field_type cmp "\x01") |
| 84 | + { |
| 85 | + die 'Unexpected field type'; |
| 86 | + } |
| 87 | + $bytes_read = read LFILE, my $length, 1; |
| 88 | + die 'Got $bytes_read but expected 1' unless $bytes_read == 1; |
| 89 | + $length = ord($length); |
| 90 | + $bytes_read = read LFILE, my $key_id, $length; |
| 91 | + die 'Got $bytes_read but expected $length' unless $bytes_read == $length; |
| 92 | + print RFILE "--let \$rpl_encryption_key_id=$key_id\n" or die "Error writing to $result_file: $!"; |
| 93 | + } |
| 94 | + else { |
| 95 | + die 'Not a binary log file.'; |
| 96 | + } |
| 97 | + |
| 98 | + close LFILE or die "Error closing $log_file: $!"; |
| 99 | + close RFILE or die "Error closing $result_file: $!"; |
| 100 | + |
| 101 | +EOF |
| 102 | + |
| 103 | +--source $_RPL_RESULT_FILE |
| 104 | +--remove_file $_RPL_RESULT_FILE |
| 105 | +--let _RPL_LOG_FILE= |
| 106 | +--let _RPL_DEBUG= |
| 107 | +--let _RPL_RESULT_FILE= |
0 commit comments