SafeBrowsing2/MySQL.pm untested

filter: Statementstmt 0.0% 0 / 106 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 181 CC 15 SCAR 41.7
CC
15
Cov
0%
init
156.0
add_chunks_a
12.0
add_chunks_s
6.0
CRAP 64.9
1 package Net::Google::SafeBrowsing2::MySQL;
2  
3 use strict;
4 use warnings;
5  
6 use base 'Net::Google::SafeBrowsing2::DBI';
7  
8 use Carp;
9 use DBI;
10 use List::Util qw(first);
11  
12  
13 our $VERSION = '1.0';
14  
15  
16 =head1 NAME
17  
18 Net::Google::SafeBrowsing2::MySQL - MySQL as back-end storage for the Google Safe Browsing v2 database
19  
20 =head1 SYNOPSIS
21  
22 use Net::Google::SafeBrowsing2::MySQL;
23  
24 my $storage = Net::Google::SafeBrowsing2::MySQL->new(host => '127.0.0.1', database => 'GoogleSafeBrowsingv2');
25 ...
26 $storage->close();
27  
28 =head1 DESCRIPTION
29  
30 This is an implementation of L<Net::Google::SafeBrowsing2::Storage> using MySQL.
31  
32 =cut
33  
34  
35 =head1 CONSTRUCTOR
36  
37 =over 4
38  
39 =back
40  
41 =head2 new()
42  
43 Create a Net::Google::SafeBrowsing2::MySQL object
44  
45 my $storage = Net::Google::SafeBrowsing2::MySQL->new(
46 host => '127.0.0.1',
47 database => 'GoogleSafeBrowsingv2',
48 username => 'foo',
49 password => 'bar'
50 );
51  
52 Arguments
53  
54 =over 4
55  
56 =item host
57  
58 Required. MySQL host name
59  
60 =item database
61  
62 Required. MySQL database name to connect to.
63  
64 =item username
65  
66 Required. MySQL username.
67  
68 =item password
69  
70 Required. MySQL password.
71  
72 =item port
73  
74 Optional. MySQL port number to connect to.
75  
76 =item keep_all
77  
78 Optional. Set to 1 to keep old information (such as expiring full hashes) in the database. 0 (delete) by default.
79  
80 =back
81  
82  
83 =cut
84  
85 sub new {
86 my ($class, %args) = @_;
87  
88 my $self = { # default arguments
89 host => '127.0.0.1',
90 database => 'GoogleSafeBrowsingv2',
91 port => 3306,
92 keep_all => 0,
93  
94 %args,
95 };
96  
97 bless $self, $class or croak "Can't bless $class: $!";
98  
99  
100 $self->init();
101  
102 return $self;
103 }
104  
105 =head1 PUBLIC FUNCTIONS
106  
107 =over 4
108  
109 See L<Net::Google::SafeBrowsing2::Storage> for a complete list of public functions.
110  
111 =back
112  
113 =head2 close()
114  
115 Cleanup old full hashes, and close the connection to the database.
116  
117 $storage->close();
118  
119  
120 =cut
121  
122 sub init {
123 my ($self, %args) = @_;
124  
125 $self->{dbh} = DBI->connect("DBI:mysql:database=" . $self->{database} . ";host=" . $self->{host} . ";port=" . $self->{port}, $self->{username}, $self->{password}, {'RaiseError' => 1});
126  
127 my @tables = $self->{dbh}->tables;
128  
129 if (! defined first { $_ =~ '`updates`' } @tables) {
130 $self->create_table_updates();
131 }
132 if (! defined first { $_ =~ '`a_chunks`' } @tables) {
133 $self->create_table_a_chunks();
134 }
135 if (! defined first { $_ =~ '`s_chunks`' } @tables) {
136 $self->create_table_s_chunks();
137 }
138 if (! defined first { $_ =~ '`full_hashes`' } @tables) {
139 $self->create_table_full_hashes();
140 }
141 if (! defined first { $_ =~ '`full_hashes_errors`' } @tables) {
142 $self->create_table_full_hashes_errors();
143 }
144 if (! defined first { $_ =~ '`mac_keys`' } @tables) {
145 $self->create_table_mac_keys();
146 }
147  
148 # Remove indexes previously created
149 $self->{dbh}->do("ALTER TABLE `a_chunks` DROP INDEX `a_chunks_hostkey`") if ($self->{dbh}->do("SHOW INDEX FROM `a_chunks`WHERE KEY_NAME = 'a_chunks_hostkey'") > 0);
150 $self->{dbh}->do("ALTER TABLE `a_chunks` DROP INDEX `a_chunks_num_list`") if ($self->{dbh}->do("SHOW INDEX FROM `a_chunks`WHERE KEY_NAME = 'a_chunks_num_list'") > 0);
151  
152 $self->{dbh}->do("ALTER TABLE `s_chunks` DROP INDEX `s_chunks_hostkey`") if ($self->{dbh}->do("SHOW INDEX FROM `s_chunks`WHERE KEY_NAME = 's_chunks_hostkey'") > 0);
153 $self->{dbh}->do("ALTER TABLE `s_chunks` DROP INDEX `s_chunks_num`") if ($self->{dbh}->do("SHOW INDEX FROM `s_chunks`WHERE KEY_NAME = 's_chunks_num'") > 0);
154 $self->{dbh}->do("ALTER TABLE `s_chunks` DROP INDEX `s_chunks_num_list`") if ($self->{dbh}->do("SHOW INDEX FROM `s_chunks`WHERE KEY_NAME = 's_chunks_num_list'") > 0);
155 }
156  
157  
158 sub create_table_updates {
159 my ($self, %args) = @_;
160  
161 my $schema = qq{
162 CREATE TABLE updates (
163 last INT NOT NULL DEFAULT '0',
164 wait INT NOT NULL DEFAULT '0',
165 errors INT NOT NULL DEFAULT '1800',
166 list VARCHAR( 50 ) NOT NULL
167 );
168 }; # Need to handle errors
169  
170 $self->{dbh}->do($schema);
171 }
172  
173 sub create_table_a_chunks {
174 my ($self, %args) = @_;
175  
176 my $schema = qq{
177 CREATE TABLE a_chunks (
178 hostkey VARBINARY( 8 ),
179 prefix VARBINARY( 8 ),
180 num INT NOT NULL,
181 list VARCHAR( 50 ) NOT NULL
182 );
183 };
184  
185 $self->{dbh}->do($schema);
186  
187 my $index = qq{
188 CREATE INDEX a_chunks_hostkey ON a_chunks (
189 hostkey
190 );
191 };
192 # $self->{dbh}->do($index);
193  
194 $index = qq{
195 CREATE INDEX a_chunks_num_list ON a_chunks (
196 num,
197 list
198 );
199 };
200 # $self->{dbh}->do($index);
201  
202 $index = qq{
203 CREATE UNIQUE INDEX a_chunks_unique ON a_chunks (
204 hostkey,
205 prefix,
206 num,
207 list
208 );
209 };
210 $self->{dbh}->do($index);
211 }
212  
213 sub create_table_s_chunks {
214 my ($self, %args) = @_;
215  
216 my $schema = qq{
217 CREATE TABLE s_chunks (
218 hostkey VARBINARY( 8 ),
219 prefix VARBINARY( 8 ),
220 num INT NOT NULL,
221 add_num INT DEFAULT 0,
222 list VARCHAR( 50 ) NOT NULL
223 );
224 };
225  
226 $self->{dbh}->do($schema);
227  
228 my $index = qq{
229 CREATE INDEX s_chunks_hostkey ON s_chunks (
230 hostkey
231 );
232 };
233 # $self->{dbh}->do($index);
234  
235 $index = qq{
236 CREATE INDEX s_chunks_num ON s_chunks (
237 num
238 );
239 };
240 # $self->{dbh}->do($index);
241  
242 $index = qq{
243 CREATE INDEX s_chunks_num_list ON s_chunks (
244 num,
245 list
246 );
247 };
248 # $self->{dbh}->do($index);
249  
250 $index = qq{
251 CREATE UNIQUE INDEX s_chunks_unique ON s_chunks (
252 hostkey,
253 prefix,
254 num,
255 add_num,
256 list
257 );
258 };
259 $self->{dbh}->do($index);
260 }
261  
262 sub create_table_full_hashes {
263 my ($self, %args) = @_;
264  
265 my $schema = qq{
266 CREATE TABLE full_hashes (
267 id INT AUTO_INCREMENT PRIMARY KEY,
268 num INT,
269 hash VARBINARY( 32 ),
270 list VARCHAR( 50 ),
271 timestamp INT Default '0'
272 );
273 };
274  
275 $self->{dbh}->do($schema);
276  
277 my $index = qq{
278 CREATE UNIQUE INDEX hash ON full_hashes (
279 num,
280 hash,
281 list
282 );
283 };
284 $self->{dbh}->do($index);
285 }
286  
287 sub create_table_full_hashes_errors {
288 my ($self, %args) = @_;
289  
290 my $schema = qq{
291 CREATE TABLE full_hashes_errors (
292 id INT AUTO_INCREMENT PRIMARY KEY,
293 errors INT Default '0',
294 prefix VARBINARY( 8 ),
295 timestamp INT Default '0'
296 );
297 };
298  
299 $self->{dbh}->do($schema);
300 }
301  
302 sub create_table_mac_keys{
303 my ($self, %args) = @_;
304  
305 my $schema = qq{
306 CREATE TABLE mac_keys (
307 client_key VARCHAR( 50 ) Default '',
308 wrapped_key VARCHAR( 50 ) Default ''
309 );
310 };
311  
312 $self->{dbh}->do($schema);
313 }
314  
315  
316 sub add_chunks_s {
317 my ($self, %args) = @_;
318 my $chunknum = $args{chunknum} || 0;
319 my $chunks = $args{chunks} || [];
320 my $list = $args{'list'} || '';
321  
322 my $add = $self->{dbh}->prepare('INSERT IGNORE INTO s_chunks (hostkey, prefix, num, add_num, list) VALUES (?, ?, ?, ?, ?)');
323 $self->{dbh}->{AutoCommit} = 0;
324  
325 foreach my $chunk (@$chunks) {
326 $add->execute( $chunk->{host}, $chunk->{prefix}, $chunknum, $chunk->{add_chunknum}, $list );
327 }
328  
329 if (scalar @$chunks == 0) { # keep empty chunks
330 $add->execute( '', '', $chunknum, '', $list );
331 }
332  
333 $self->{dbh}->commit;
334 $self->{dbh}->{AutoCommit} = 1;
335 }
336  
337 sub add_chunks_a {
338 my ($self, %args) = @_;
339 my $chunknum = $args{chunknum} || 0;
340 my $chunks = $args{chunks} || [];
341 my $list = $args{'list'} || '';
342  
343 my $add = $self->{dbh}->prepare('INSERT IGNORE INTO a_chunks (hostkey, prefix, num, list) VALUES (?, ?, ?, ?)');
344 $self->{dbh}->{AutoCommit} = 0;
345  
346 foreach my $chunk (@$chunks) {
347 # 32-byte prefix seen at chunk 69961
348 # If this becomes more of a problem, the schema will have to be adjusted.
349 if (length($chunk->{prefix}) > 8) {
350 $chunk->{prefix} = substr $chunk->{prefix}, 0, 4;
351 }
352  
353 $add->execute( $chunk->{host}, $chunk->{prefix}, $chunknum, $list );
354 }
355  
356 if (scalar @$chunks == 0) { # keep empty chunks
357 $add->execute( '', '', $chunknum, $list );
358 }
359  
360 $self->{dbh}->commit;
361 $self->{dbh}->{AutoCommit} = 1;
362 }
363  
364 =head1 CHANGELOG
365  
366 =over 4
367  
368 =item 0.6
369  
370 Remove some indexes to speed up INSERTs.
371  
372 =item 0.5
373  
374 Keep empty sub chunks. Shorten prefixes greater than 8 bytes (workaround to keep schema tight)
375  
376 =item 0.4
377  
378 Add option keep_all to keep expired full hashes. Useful for debugging.
379  
380 =item 0.3
381  
382 Use more efficient add_chunk_a and add_chunk_s functions.
383 Change data type for prefixes from VARCHAR to VARBINARY.
384  
385  
386 =back
387  
388 =head1 SEE ALSO
389  
390 See L<Net::Google::SafeBrowsing2> for handling Google Safe Browsing v2.
391  
392 See L<Net::Google::SafeBrowsing2::Storage> for the list of public functions.
393  
394 See L<Net::Google::SafeBrowsing2::Sqlite> for a back-end using Sqlite.
395  
396 Google Safe Browsing v2 API: L<http://code.google.com/apis/safebrowsing/developers_guide_v2.html>
397  
398  
399 =head1 AUTHOR
400  
401 Julien Sobrier, E<lt>jsobrier@zscaler.comE<gt> or E<lt>julien@sobrier.netE<gt>
402  
403 =head1 COPYRIGHT AND LICENSE
404  
405 Copyright (C) 2011 by Julien Sobrier
406  
407 This library is free software; you can redistribute it and/or modify
408 it under the same terms as Perl itself, either Perl version 5.8.8 or,
409 at your option, any later version of Perl 5 you may have available.
410  
411  
412 =cut
413  
414 1;