mirror of
https://github.com/percona/percona-toolkit.git
synced 2025-09-09 18:30:16 +00:00
add 5.7 compatibility for pt-show-grants
This commit is contained in:
@@ -1727,6 +1727,7 @@ sub _d {
|
||||
# End Daemon package
|
||||
# ###########################################################################
|
||||
|
||||
|
||||
# ###########################################################################
|
||||
# This is a combination of modules and programs in one -- a runnable module.
|
||||
# http://www.perl.com/pub/a/2006/07/13/lightning-articles.html?page=last
|
||||
@@ -1826,6 +1827,7 @@ sub main {
|
||||
{ AutoCommit => 1, });
|
||||
|
||||
my ( $version, $ts ) = $dbh->selectrow_array("SELECT VERSION(), NOW()");
|
||||
|
||||
print join("\n",
|
||||
"-- Grants dumped by pt-show-grants",
|
||||
"-- Dumped from server " . ($dbh->{mysql_hostinfo} || '')
|
||||
@@ -1856,6 +1858,27 @@ sub main {
|
||||
PTDEBUG && _d('Checking user', $user_host);
|
||||
}
|
||||
|
||||
# If MySQL 5.7+ then we need to use SHOW CREATE USER
|
||||
my @create_user;
|
||||
if ( compare_versions($version, '5.7.6') >= 0 ) {
|
||||
eval {
|
||||
@create_user = @{ $dbh->selectcol_arrayref("SHOW CREATE USER $user_host") };
|
||||
};
|
||||
if ( $EVAL_ERROR ) {
|
||||
PTDEBUG && _d($EVAL_ERROR);
|
||||
$exit_status = 1;
|
||||
}
|
||||
PTDEBUG && _d('CreateUser:', Dumper(\@create_user));
|
||||
# make this replication safe converting the CREATE USER into
|
||||
# CREATE USER IF NOT EXISTS and then doing an ALTER USER
|
||||
my $create = $create_user[0];
|
||||
my $alter = $create;
|
||||
$create =~ s{CREATE USER}{CREATE USER IF NOT EXISTS};
|
||||
$create =~ s{ IDENTIFIED .*}{};
|
||||
$alter =~ s{CREATE USER}{ALTER USER};
|
||||
@create_user = ( $create, $alter );
|
||||
PTDEBUG && _d('AdjustedCreateUser:', Dumper(\@create_user));
|
||||
}
|
||||
my @grants;
|
||||
eval {
|
||||
@grants = @{ $dbh->selectcol_arrayref("SHOW GRANTS FOR $user_host") };
|
||||
@@ -1902,6 +1925,9 @@ sub main {
|
||||
@grants = sort {
|
||||
$b =~ m/IDENTIFIED BY/ <=> $a =~ m/IDENTIFIED BY/ || $a cmp $b
|
||||
} @grants;
|
||||
|
||||
# Add @create_user if there
|
||||
@grants = ( @create_user, @grants ) if scalar @create_user > 0;
|
||||
PTDEBUG && _d('Grants sorted:', Dumper(\@grants));
|
||||
|
||||
# Print REVOKE statements.
|
||||
@@ -1929,7 +1955,7 @@ sub main {
|
||||
}
|
||||
|
||||
@result;
|
||||
} @grants;
|
||||
} grep { /\bgrant\b/i } @grants;
|
||||
|
||||
print join(
|
||||
"\n",
|
||||
@@ -2007,6 +2033,37 @@ sub split_grants {
|
||||
return @grants;
|
||||
}
|
||||
|
||||
sub compare_versions {
|
||||
my ($v1, $v2) = @_;
|
||||
|
||||
# Remove all but numbers and dots.
|
||||
# Assume simple 1.2.3 style
|
||||
$v1 =~ s/[^\d\.]//;
|
||||
$v2 =~ s/[^\d\.]//;
|
||||
|
||||
my @a = ( $v1 =~ /(\d+)\.?/g );
|
||||
my @b = ( $v2 =~ /(\d+)\.?/g );
|
||||
foreach my $n1 (@a) {
|
||||
$n1 += 0; #convert to number
|
||||
if (!@b) {
|
||||
# b ran out of digits, a is larger
|
||||
return 1;
|
||||
}
|
||||
my $n2 = shift @b;
|
||||
$n2 += 0; # convert to number
|
||||
if ($n1 == $n2) {
|
||||
# still tied?, fetch next
|
||||
next;
|
||||
}
|
||||
else {
|
||||
# difference! return result
|
||||
return $n1 <=> $n2;
|
||||
}
|
||||
}
|
||||
# b still has digits? it's larger, else it's a tie
|
||||
return @b ? -1 : 0;
|
||||
}
|
||||
|
||||
sub _d {
|
||||
my ($package, undef, $line) = caller 0;
|
||||
@_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; }
|
||||
|
2421
bin/pt-show-grants.old
Executable file
2421
bin/pt-show-grants.old
Executable file
File diff suppressed because it is too large
Load Diff
@@ -49,29 +49,64 @@ $modes->restore_original_modes;
|
||||
$output = output(
|
||||
sub { pt_show_grants::main('-F', $cnf, qw(--only bob --no-header)); }
|
||||
);
|
||||
|
||||
my $expected_57 = <<'END_OUTPUT_1';
|
||||
-- Grants for 'bob'@'%'
|
||||
CREATE USER IF NOT EXISTS 'bob'@'%';
|
||||
ALTER USER 'bob'@'%' IDENTIFIED WITH 'mysql_native_password' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;
|
||||
GRANT USAGE ON *.* TO 'bob'@'%';
|
||||
-- Grants for 'bob'@'192.168.1.1'
|
||||
CREATE USER IF NOT EXISTS 'bob'@'192.168.1.1';
|
||||
ALTER USER 'bob'@'192.168.1.1' IDENTIFIED WITH 'mysql_native_password' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;
|
||||
GRANT USAGE ON *.* TO 'bob'@'192.168.1.1';
|
||||
-- Grants for 'bob'@'localhost'
|
||||
CREATE USER IF NOT EXISTS 'bob'@'localhost';
|
||||
ALTER USER 'bob'@'localhost' IDENTIFIED WITH 'mysql_native_password' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;
|
||||
GRANT USAGE ON *.* TO 'bob'@'localhost';
|
||||
END_OUTPUT_1
|
||||
|
||||
my $expected_56 = <<'END_OUTPUT_2';
|
||||
-- Grants for 'bob'@'%'
|
||||
GRANT USAGE ON *.* TO 'bob'@'%';
|
||||
-- Grants for 'bob'@'192.168.1.1'
|
||||
GRANT USAGE ON *.* TO 'bob'@'192.168.1.1';
|
||||
-- Grants for 'bob'@'localhost'
|
||||
GRANT USAGE ON *.* TO 'bob'@'localhost';
|
||||
END_OUTPUT_2
|
||||
|
||||
my $expected = $sandbox_version < '5.7' ? $expected_56 : $expected_57;
|
||||
|
||||
is(
|
||||
$output,
|
||||
"-- Grants for 'bob'\@'%'
|
||||
GRANT USAGE ON *.* TO 'bob'\@'%';
|
||||
-- Grants for 'bob'\@'192.168.1.1'
|
||||
GRANT USAGE ON *.* TO 'bob'\@'192.168.1.1';
|
||||
-- Grants for 'bob'\@'localhost'
|
||||
GRANT USAGE ON *.* TO 'bob'\@'localhost';
|
||||
",
|
||||
$expected,
|
||||
'--only user gets grants for user on all hosts (issue 551)'
|
||||
);
|
||||
|
||||
$output = output(
|
||||
sub { pt_show_grants::main('-F', $cnf, qw(--only bob@192.168.1.1 --no-header)); }
|
||||
);
|
||||
|
||||
$expected_57 = <<'END_OUTPUT_3';
|
||||
-- Grants for 'bob'@'192.168.1.1'
|
||||
CREATE USER IF NOT EXISTS 'bob'@'192.168.1.1';
|
||||
ALTER USER 'bob'@'192.168.1.1' IDENTIFIED WITH 'mysql_native_password' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;
|
||||
GRANT USAGE ON *.* TO 'bob'@'192.168.1.1';
|
||||
END_OUTPUT_3
|
||||
|
||||
$expected_56 = <<'END_OUTPUT_4';
|
||||
-- Grants for 'bob'@'192.168.1.1'
|
||||
GRANT USAGE ON *.* TO 'bob'@'192.168.1.1';
|
||||
END_OUTPUT_4
|
||||
|
||||
$expected = $sandbox_version < '5.7' ? $expected_56 : $expected_57;
|
||||
|
||||
is(
|
||||
$output,
|
||||
"-- Grants for 'bob'\@'192.168.1.1'
|
||||
GRANT USAGE ON *.* TO 'bob'\@'192.168.1.1';
|
||||
",
|
||||
$expected,
|
||||
'--only user@host'
|
||||
);
|
||||
|
||||
|
||||
diag(`/tmp/12345/use -u root -e "DROP USER 'bob'\@'%'"`);
|
||||
diag(`/tmp/12345/use -u root -e "DROP USER 'bob'\@'localhost'"`);
|
||||
diag(`/tmp/12345/use -u root -e "DROP USER 'bob'\@'192.168.1.1'"`);
|
||||
|
@@ -102,49 +102,56 @@ $modes->del('NO_AUTO_CREATE_USER');
|
||||
diag(`/tmp/12345/use -u root -e "GRANT SELECT(DateCreated, PckPrice, PaymentStat, SANumber) ON test.t TO 'sally'\@'%'"`);
|
||||
diag(`/tmp/12345/use -u root -e "GRANT SELECT(city_id), INSERT(city) ON sakila.city TO 'sally'\@'%'"`);
|
||||
$modes->restore_original_modes();
|
||||
|
||||
my $postfix = $sandbox_version < '5.7' ? '' : '-57';
|
||||
|
||||
ok(
|
||||
no_diff(
|
||||
sub { pt_show_grants::main('-F', $cnf, qw(--only sally --no-header)) },
|
||||
"t/pt-show-grants/samples/column-grants.txt",
|
||||
"t/pt-show-grants/samples/column-grants$postfix.txt",
|
||||
stderr => 1,
|
||||
),
|
||||
"Column-level grants (bug 866075)"
|
||||
);
|
||||
|
||||
|
||||
ok(
|
||||
no_diff(
|
||||
sub { pt_show_grants::main('-F', $cnf, qw(--only sally --no-header),
|
||||
qw(--separate)) },
|
||||
"t/pt-show-grants/samples/column-grants-separate.txt",
|
||||
"t/pt-show-grants/samples/column-grants-separate$postfix.txt",
|
||||
stderr => 1,
|
||||
),
|
||||
"Column-level grants --separate (bug 866075)"
|
||||
);
|
||||
|
||||
|
||||
ok(
|
||||
no_diff(
|
||||
sub { pt_show_grants::main('-F', $cnf, qw(--only sally --no-header),
|
||||
qw(--separate --revoke)) },
|
||||
"t/pt-show-grants/samples/column-grants-separate-revoke.txt",
|
||||
"t/pt-show-grants/samples/column-grants-separate-revoke$postfix.txt",
|
||||
stderr => 1,
|
||||
),
|
||||
"Column-level grants --separate --revoke (bug 866075)"
|
||||
);
|
||||
|
||||
|
||||
diag(`/tmp/12345/use -u root -e "GRANT SELECT ON sakila.city TO 'sally'\@'%'"`);
|
||||
|
||||
ok(
|
||||
no_diff(
|
||||
sub { pt_show_grants::main('-F', $cnf, qw(--only sally --no-header)) },
|
||||
"t/pt-show-grants/samples/column-grants-combined.txt",
|
||||
"t/pt-show-grants/samples/column-grants-combined$postfix.txt",
|
||||
stderr => 1,
|
||||
keep_output => 1,
|
||||
),
|
||||
"Column-level grants combined with table-level grants on the same table (bug 866075)"
|
||||
);
|
||||
|
||||
|
||||
diag(`/tmp/12345/use -u root -e "DROP USER 'sally'\@'%'"`);
|
||||
|
||||
DONE:
|
||||
# #############################################################################
|
||||
# Done.
|
||||
# #############################################################################
|
||||
|
@@ -63,7 +63,7 @@ like(
|
||||
$output,
|
||||
qr/REVOKE USAGE ON \*\.\* FROM ''\@'';/,
|
||||
'Prints revoke for anonymous user (issue 445)'
|
||||
);
|
||||
) or diag($output);
|
||||
|
||||
diag(`/tmp/12345/use -u root -e "DROP USER ''\@''"`);
|
||||
$output = `/tmp/12345/use -e "SELECT user FROM mysql.user WHERE user = ''"`;
|
||||
|
6
t/pt-show-grants/samples/column-grants-57.txt
Normal file
6
t/pt-show-grants/samples/column-grants-57.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
-- Grants for 'sally'@'%'
|
||||
CREATE USER IF NOT EXISTS 'sally'@'%';
|
||||
ALTER USER 'sally'@'%' IDENTIFIED WITH 'mysql_native_password' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;
|
||||
GRANT INSERT (city), SELECT (city_id) ON `sakila`.`city` TO 'sally'@'%';
|
||||
GRANT SELECT (SANumber, DateCreated, PaymentStat, PckPrice) ON `test`.`t` TO 'sally'@'%';
|
||||
GRANT USAGE ON *.* TO 'sally'@'%';
|
6
t/pt-show-grants/samples/column-grants-combined-57.txt
Normal file
6
t/pt-show-grants/samples/column-grants-combined-57.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
-- Grants for 'sally'@'%'
|
||||
CREATE USER IF NOT EXISTS 'sally'@'%';
|
||||
ALTER USER 'sally'@'%' IDENTIFIED WITH 'mysql_native_password' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;
|
||||
GRANT INSERT (city), SELECT, SELECT (city_id) ON `sakila`.`city` TO 'sally'@'%';
|
||||
GRANT SELECT (SANumber, DateCreated, PaymentStat, PckPrice) ON `test`.`t` TO 'sally'@'%';
|
||||
GRANT USAGE ON *.* TO 'sally'@'%';
|
7
t/pt-show-grants/samples/column-grants-separate-57.txt
Normal file
7
t/pt-show-grants/samples/column-grants-separate-57.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
-- Grants for 'sally'@'%'
|
||||
CREATE USER IF NOT EXISTS 'sally'@'%';
|
||||
ALTER USER 'sally'@'%' IDENTIFIED WITH 'mysql_native_password' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;
|
||||
GRANT INSERT (city) ON `sakila`.`city` TO 'sally'@'%';
|
||||
GRANT SELECT (SANumber, DateCreated, PaymentStat, PckPrice) ON `test`.`t` TO 'sally'@'%';
|
||||
GRANT SELECT (city_id) ON `sakila`.`city` TO 'sally'@'%';
|
||||
GRANT USAGE ON *.* TO 'sally'@'%';
|
@@ -0,0 +1,12 @@
|
||||
-- Revoke statements for 'sally'@'%'
|
||||
REVOKE INSERT (city) ON `sakila`.`city` FROM 'sally'@'%';
|
||||
REVOKE SELECT (SANumber, DateCreated, PaymentStat, PckPrice) ON `test`.`t` FROM 'sally'@'%';
|
||||
REVOKE SELECT (city_id) ON `sakila`.`city` FROM 'sally'@'%';
|
||||
REVOKE USAGE ON *.* FROM 'sally'@'%';
|
||||
-- Grants for 'sally'@'%'
|
||||
CREATE USER IF NOT EXISTS 'sally'@'%';
|
||||
ALTER USER 'sally'@'%' IDENTIFIED WITH 'mysql_native_password' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;
|
||||
GRANT INSERT (city) ON `sakila`.`city` TO 'sally'@'%';
|
||||
GRANT SELECT (SANumber, DateCreated, PaymentStat, PckPrice) ON `test`.`t` TO 'sally'@'%';
|
||||
GRANT SELECT (city_id) ON `sakila`.`city` TO 'sally'@'%';
|
||||
GRANT USAGE ON *.* TO 'sally'@'%';
|
Reference in New Issue
Block a user