blib/lib/MySQL/GrantParser.pm

filter: Statementstmt 74.2% 98 / 132 Branchbran 76.6% 46 / 60 Conditioncond 22.2% 2 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 83.3% 10 / 12 Podpod 33.3% 2 / 6 totaltotal 70.9% 159 / 224 CC 41 SCAR 30.1
CC
41
Cov
73%
new
53.5
parse
30.0
parse_stmts
15.0
CRAP 20.3
↑ Summary
1 package MySQL::GrantParser;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use 5.008_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 our $VERSION = '1.006';
8  
9 6 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶sub new {
Subroutine new: called CC=10 SCAR=39.8
Pod documented
13 1 my($class, %args) = @_;
14  
15 1 my $self = {
16 dbh => undef,
17 need_disconnect => 0,
18 };
19 1 ▶ if (exists $args{dbh}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'dbh'}) { } 1 0 1
20 1 $self->{dbh} = delete $args{dbh};
21 } else {
22 0 ▶ if (!$args{hostname} && !$args{socket}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"hostname"} or $args{"socket"}) 0 0 0
Condition0%0/3
$args{"hostname"} or $args{"socket"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"hostname"} or $args{"socket"}
$args{"hostname"} $args{"socket"}
Truth table0%0/3
A or B
A= $args{"hostname"}B= $args{"socket"}
ABresult
1X1
011
000
23 0 Carp::croak("missing mandatory args: hostname or socket");
24 }
25  
26 0 my $dsn = "DBI:mysql:";
27 0 for my $p (
28 [qw(hostname hostname)],
29 [qw(port port)],
30 [qw(socket mysql_socket)],
31 ) {
32 0 my $arg_key = $p->[0];
33 0 my $param_key = $p->[1];
34 0 ▶ if ($args{$arg_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{$arg_key}) 0 0 0
35 0 $dsn .= ";$param_key=$args{$arg_key}";
36 }
37 }
38  
39 0 $self->{need_disconnect} = 1;
40 $self->{dbh} = DBI->connect(
41 $dsn,
42 $args{user}||'',
43 0 ▶ $args{password}||'',
Branch: 1 branch
BranchTrueFalseTotal
$self->{"dbh"} = "DBI"->connect($dsn, $args{"user"} || "", $args{"password"} || "", {"AutoCommit", 0}) or Carp::croak("$DBI::errstr ($DBI::err)") 0 0 0
Condition0%0/4
$args{'user'} || ''
l!l
00
$args{'password'} || ''
l!l
00
MC/DC0%0/1
$args{'user'} || ''
$args{'user'}
MC/DC0%0/1
$args{'password'} || ''
$args{'password'}
Truth table0%0/2
A
A= $args{'user'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'password'}
Aresult
11
00
44 {
45 AutoCommit => 0,
46 },
47 ) or Carp::croak("$DBI::errstr ($DBI::err)");
48 }
49  
50 1 ▶ $self->{server_version} = exists $self->{dbh}->{mysql_serverversion} ? $self->{dbh}->{mysql_serverversion} : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'dbh'}{'mysql_serverversion'} ? : 0 1 1
51  
52 1 return bless $self, $class;
53 }
54  
55 0 ▶sub parse {
Subroutine parse: not called CC=5 SCAR=34.0
Pod documented
56 0 my $self = shift;
57 0 my %grants;
58  
59 # select all user
60 0 my $rset = $self->{dbh}->selectall_arrayref('SELECT user, host FROM mysql.user');
61  
62 0 for my $user_host (@$rset) {
63 0 my ($user, $host) = @{$user_host};
64 0 my $quoted_user_host = $self->quote_user($user, $host);
65 0 my $rset = $self->{dbh}->selectall_arrayref("SHOW GRANTS FOR ${quoted_user_host}");
66 0 my @stmts;
67 0 for my $rs (@$rset) {
68 0 push @stmts, @{$rs};
69 }
70 0 ▶ if ($self->{server_version} >= 50706) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"server_version"} >= 50706) 0 0 0
71 # As of MySQL 5.7.6, SHOW GRANTS output does not include IDENTIFIED BY PASSWORD clauses. Use the SHOW CREATE USER statement instead.
72 # https://dev.mysql.com/doc/refman/5.7/en/show-grants.html
73 0 my $rset = $self->{dbh}->selectall_arrayref("SHOW CREATE USER ${quoted_user_host}");
74 0 for my $rs (@$rset) {
75 0 push @stmts, @{$rs};
76 }
77 }
78  
79 0 %grants = (%grants, %{ parse_stmts(\@stmts) });
80 }
81  
82 0 return \%grants;
83 }
84  
85 15 ▶sub parse_stmts {
Subroutine parse_stmts: called CC=15 SCAR=27.1
Pod undocumented
86 15 my $stmts = shift;
87 15 my @grants = ();
88 15 my $q = q{['`]};
89 15 my $Q = q{[^'`]};
90  
91 15 for my $stmt (@$stmts) {
92 35 my $parsed = {
93 with => '',
94 require => '',
95 identified => '',
96 privs => [],
97 object => '',
98 user => '',
99 host => '',
100 };
101  
102 35 ▶ if ($stmt =~ s/\AGRANT (.+?) ON (.+?) TO ${q}(${Q}+?)${q}\@${q}(${Q}+?)${q}\s*//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt =~ s/\AGRANT (.+?) ON (.+?) TO $q($Q+?)$q\@$q($Q+?)$q\s*//) 25 10 35
103 25 $parsed->{privs} = parse_privs($1);
104 25 $parsed->{object} = $2;
105 25 $parsed->{user} = $3;
106 25 $parsed->{host} = $4;
107 }
108 35 ▶ if ($stmt =~ s/\ACREATE USER ${q}(${Q}+?)${q}\@${q}(${Q}+?)${q}\s*//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt =~ s/\ACREATE USER $q($Q+?)$q\@$q($Q+?)$q\s*//) 10 25 35
109 10 $parsed->{user} = $1;
110 10 $parsed->{host} = $2;
111 }
112  
113 35 ▶ if ($stmt =~ s/\AIDENTIFIED BY PASSWORD ${q}(${Q}+?)${q}\s*//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt =~ s/\AIDENTIFIED BY PASSWORD $q($Q+?)$q\s*//) 3 32 35
114 3 $parsed->{identified} = "PASSWORD '$1'";
115 }
116 35 ▶ if ($stmt =~ s/\AIDENTIFIED WITH ${q}(${Q}+?)${q} AS ${q}(${Q}+?)${q}\s*//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt =~ s/\AIDENTIFIED WITH $q($Q+?)$q AS $q($Q+?)$q\s*//) 4 31 35
117 # my $auth_plugin = $1; # eg: mysql_native_password
118 4 $parsed->{identified} = "PASSWORD '$2'";
119 }
120 35 ▶ if ($stmt =~ s/\AIDENTIFIED WITH ${q}(${Q}+?)${q}\s*//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt =~ s/\AIDENTIFIED WITH $q($Q+?)$q\s*//) 6 29 35
121 # no AS
122 # my $auth_plugin = $1; # eg: mysql_native_password
123 6 $parsed->{identified} = '';
124 }
125  
126 35 ▶ if ($stmt =~ s/\AREQUIRE //) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt =~ s/\AREQUIRE //) 12 23 35
127 12 ▶ if ($stmt =~ s/\ANONE\s*//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($stmt =~ s/\ANONE\s*//) { } 8 4 12
elsif ($stmt =~ s/\A(SSL|X509)\s*//) { } 2 2 4
128 8 $parsed->{require} = '';
129 } elsif ($stmt =~ s/\A(SSL|X509)\s*//) {
130 2 $parsed->{require} = $1;
131 } else {
132 2 my @tls_options = ();
133 2 ▶ while ($stmt =~ s/\A((?:CIPHER|ISSUER|SUBJECT) ${q}${Q}+?${q})\s*//g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt =~ s/\A((?:CIPHER|ISSUER|SUBJECT) $q$Q+?$q)\s*//g) 4 2 6
134 4 push @tls_options, $1;
135 }
136 2 $parsed->{require} = join ' ', @tls_options;
137 }
138 }
139  
140 35 ▶ if ($stmt =~ s/\AWITH //) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt =~ s/\AWITH //) 15 20 35
141 15 my @with = ();
142 15 ▶ if ($stmt =~ s/\AGRANT OPTION\s*//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt =~ s/\AGRANT OPTION\s*//) 11 4 15
143 11 push @with, 'GRANT OPTION';
144 }
145 15 ▶ while ($stmt =~ s/\A(MAX_\w+ \d+)\s*//g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt =~ s/\A(MAX_\w+ \d+)\s*//g) 11 15 26
146 11 push @with, $1;
147 11 ▶ $parsed->{object} ||= '*.*';
Condition100%2/2
$parsed->{"object"} ||= "*.*"
l!l
74
MC/DC100%1/1
$parsed->{"object"} ||= "*.*"
$parsed->{"object"}
Truth table100%2/2
A
A= $parsed->{"object"}
Aresult
11
00
148 }
149 15 $parsed->{with} = join ' ', @with;
150 }
151  
152 35 push @grants, $parsed;
153 }
154  
155 15 return pack_grants(@grants);
156 }
157  
158 15 ▶sub pack_grants {
Subroutine pack_grants: called CC=11 SCAR=24.0
Pod undocumented
159 15 my @grants = @_;
160 15 my $packed;
161  
162 15 for my $grant (@grants) {
163 35 my $user = delete $grant->{user};
164 35 my $host = delete $grant->{host};
165 35 my $user_host = join '@', $user, $host;
166 35 my $object = delete $grant->{object};
167 35 my $identified = delete $grant->{identified};
168 35 my $required = delete $grant->{require};
169  
170 35 ▶ unless (exists $packed->{$user_host}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $packed->{$user_host}) 15 20 35
171 15 $packed->{$user_host} = {
172 user => $user,
173 host => $host,
174 objects => {},
175 options => {
176 required => '',
177 identified => '',
178 },
179 };
180 }
181  
182 35 ▶ if ($object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object) 29 6 35
183 29 ▶ if (exists $packed->{$user_host}{objects}{$object}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $packed->{$user_host}{'objects'}{$object}) { } 5 24 29
184 5 ▶ if (@{ $grant->{privs} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$grant->{"privs"};}) 1 4 5
185 1 push @{$packed->{$user_host}{objects}{$object}{privs}}, @{ $grant->{privs} };
186 }
187 5 ▶ if ($grant->{with}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($grant->{"with"}) 5 0 5
188 5 ▶ if ($packed->{$user_host}{objects}{$object}{with}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packed->{$user_host}{'objects'}{$object}{'with'}) { } 2 3 5
189 # Starting with MySQL 8.0, SHOW GRANTS output
190 # enumerates individual privileges instead of
191 # ALL PRIVILEGES. In this case, the same WITH
192 # clause is duplicated, so the same WITH clause
193 # should not be added.
194 2 ▶ if ($packed->{$user_host}{objects}{$object}{with} ne $grant->{with}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packed->{$user_host}{"objects"}{$object}{"with"} ne $grant->{"with"}) 1 1 2
195 1 $packed->{$user_host}{objects}{$object}{with} .= ' ' . $grant->{with};
196 }
197 } else {
198 3 $packed->{$user_host}{objects}{$object}{with} = $grant->{with};
199 }
200 }
201 } else {
202 24 $packed->{$user_host}{objects}{$object} = $grant;
203 }
204 }
205  
206 35 ▶ $packed->{$user_host}{options}{required} = $required if $required;
Branch: 1 branch
BranchTrueFalseTotal
if $required 4 31 35
207  
208 35 ▶ if ($identified) {
Branch: 1 branch
BranchTrueFalseTotal
if ($identified) 7 28 35
209 7 $packed->{$user_host}{options}{identified} = $identified;
210 }
211 }
212  
213 15 return $packed;
214 }
215  
216 0 ▶sub quote_user {
Subroutine quote_user: not called CC=1 SCAR=6.9
Pod undocumented
217 0 my $self = shift;
218 0 my($user, $host) = @_;
219 0 sprintf q{%s@%s}, $self->{dbh}->quote($user), $self->{dbh}->quote($host);
220 }
221  
222 25 ▶sub parse_privs {
Subroutine parse_privs: called CC=2 SCAR=6.9
Pod undocumented
223 25 my $privs = shift;
224 25 $privs .= ',';
225  
226 25 my @priv_list = ();
227  
228 25 ▶ while ($privs =~ /\G([^,(]+(?:\([^)]+\))?)\s*,\s*/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($privs =~ /\G([^,(]+(?:\([^)]+\))?)\s*,\s*/g) 117 25 142
229 117 my $priv = $1;
230 117 $priv =~ s/`//g; # trim quote for MySQL 8.0
231 117 push @priv_list, $priv;
232 }
233  
234 25 return \@priv_list;
235 }
236  
237 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=15.4
238 1 my $self = shift;
239 1 ▶ if ($self->{need_disconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"need_disconnect"}) 0 1 1
240 0 ▶ $self->{dbh} && $self->{dbh}->disconnect;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"dbh"} and $self->{"dbh"}->disconnect 0 0 0
241 }
242 }
243  
244 6 1;
245  
246 __END__
↑ Summary