SafeBrowsing2/Postgres.pm untested

filter: Statementstmt 0.0% 0 / 73 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 128 CC 11 SCAR 31.3
CC
11
Cov
0%
init
56.0
new
12.0
add_chunks_a
12.0
CRAP 22.9
1 package Net::Google::SafeBrowsing2::Postgres;
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 our $VERSION = '0.02';
13  
14 =head1 NAME
15  
16 Net::Google::SafeBrowsing2::Postgres - Postgres as back-end storage for the
17 Google Safe Browsing v2 database
18  
19 =head1 SYNOPSIS
20  
21 use Net::Google::SafeBrowsing2::Postgres;
22  
23 my $storage = Net::Google::SafeBrowsing2::Postgres->new(
24 host => '127.0.0.1',
25 database => 'google_safe_browsing',
26 username => $username,
27 password => $password,
28 );
29
30 # ...
31  
32 $storage->close();
33  
34 =head1 DESCRIPTION
35  
36 This is an implementation of L<Net::Google::SafeBrowsing2::Storage> using
37 Postgres.
38  
39 =head1 CONSTRUCTOR
40  
41 =over 4
42  
43 =back
44  
45 =head2 new()
46  
47 Create a Net::Google::SafeBrowsing2::Postgres object
48  
49 my $storage = Net::Google::SafeBrowsing2::Postgres->new(
50 host => '127.0.0.1',
51 database => 'google_safe_browsing',
52 username => $username,
53 password => $password,
54 );
55  
56 Arguments
57  
58 =over 4
59  
60 =item host
61  
62 Specifies Postgres host name. Defaults to 127.0.0.1.
63  
64 =item database
65  
66 Specifies Postgres database name. Defaults to "google_safe_browsing".
67  
68 =item username
69  
70 Specifies the username for the Postgres connection. Required.
71  
72 =item password
73  
74 Specifies the password for the Postgres connection. Required.
75  
76 =item keep_all
77  
78 Optional. Set to 1 to keep old information (such as expiring full hashes)
79 in the database. 0 (delete) by default.
80  
81 =back
82  
83 =cut
84  
85 sub new {
86 my ($class, %args) = @_;
87  
88 # Default arguments
89 my $self = {
90 host => '127.0.0.1',
91 database => 'google_safe_browsing',
92 keep_all => 0,
93  
94 %args,
95 };
96  
97 if (!$self->{username}) {
98 croak "username required";
99 }
100  
101 if (!$self->{password}) {
102 croak "password required";
103 }
104  
105 bless $self, $class;
106  
107 $self->init();
108  
109 return $self;
110 }
111  
112 =head1 PUBLIC FUNCTIONS
113  
114 =over 4
115  
116 See L<Net::Google::SafeBrowsing2::Storage> for a complete list of public functions.
117  
118 =back
119  
120 =head2 close()
121  
122 Cleanup old full hashes, and close the connection to the database.
123  
124 $storage->close();
125  
126 =cut
127  
128 sub init {
129 my ($self, %args) = @_;
130  
131 $self->{dbh} = DBI->connect(
132 "DBI:Pg:dbname=" . $self->{database} . ";host=" . $self->{host},
133 $self->{username},
134 $self->{password},
135 {
136 RaiseError => 1,
137 },
138 );
139  
140 my @tables = $self->{dbh}->tables;
141  
142 # Postgres reports normal tables (compared to internal ones) with a
143 # prefix of "public."
144 if (! defined first { /public\.updates/ } @tables) {
145 $self->create_table_updates();
146 }
147 if (! defined first { /public\.a_chunks/ } @tables) {
148 $self->create_table_a_chunks();
149 }
150 if (! defined first { /public\.s_chunks/ } @tables) {
151 $self->create_table_s_chunks();
152 }
153 if (! defined first { /public\.full_hashes/ } @tables) {
154 $self->create_table_full_hashes();
155 }
156 if (! defined first { /public\.full_hashes_errors/ } @tables) {
157 $self->create_table_full_hashes_errors();
158 }
159 if (! defined first { /public\.mac_keys/ } @tables) {
160 $self->create_table_mac_keys();
161 }
162 }
163  
164 # Overridden because Postgres uses SERIAL instead of AUTO_INCREMENT.
165 sub create_table_full_hashes {
166 my ($self, %args) = @_;
167  
168 my $schema = qq{
169 CREATE TABLE full_hashes (
170 id SERIAL PRIMARY KEY,
171 num INT,
172 hash VARCHAR( 32 ),
173 list VARCHAR( 50 ),
174 timestamp INT Default '0'
175 );
176 };
177  
178 $self->{dbh}->do($schema);
179  
180 my $index = qq{
181 CREATE UNIQUE INDEX hash ON full_hashes (
182 num,
183 hash,
184 list
185 );
186 };
187 $self->{dbh}->do($index);
188 }
189  
190 # Overridden because Postgres uses SERIAL instead of AUTO_INCREMENT.
191 sub create_table_full_hashes_errors {
192 my ($self, %args) = @_;
193  
194 my $schema = qq{
195 CREATE TABLE full_hashes_errors (
196 id SERIAL PRIMARY KEY,
197 errors INT Default '0',
198 prefix VARCHAR( 8 ),
199 timestamp INT Default '0'
200 );
201 };
202  
203 $self->{dbh}->do($schema);
204 }
205  
206  
207 sub add_chunks_a {
208 my ($self, %args) = @_;
209 my $chunknum = $args{chunknum} || 0;
210 my $chunks = $args{chunks} || [];
211 my $list = $args{'list'} || '';
212  
213 $self->{add_chunks_a_ins_sth} ||= $self->{dbh}->prepare('INSERT INTO a_chunks (hostkey, prefix, num, list) VALUES (?, ?, ?, ?)');
214 $self->{add_chunks_a_del_sth} ||= $self->{dbh}->prepare('DELETE FROM a_chunks WHERE hostkey = ? AND prefix = ? AND num = ? AND list = ?');
215  
216 my $add = $self->{add_chunks_a_ins_sth};
217 my $del = $self->{add_chunks_a_del_sth};
218  
219 foreach my $chunk (@$chunks) {
220 # Crude workaround for longer prefixes. Although Google state that the
221 # length varies, the overwhelming majority are 4 bytes. However, I have
222 # seen a 32 byte one (chunk 69961).
223 #
224 # If this becomes more of a problem, the schema will have to be adjusted.
225 if (length($chunk->{prefix}) > 8) {
226 $chunk->{prefix} = substr $chunk->{prefix}, 0, 4;
227 }
228  
229 $del->execute( $chunk->{host}, $chunk->{prefix}, $chunknum, $list );
230 $add->execute( $chunk->{host}, $chunk->{prefix}, $chunknum, $list );
231 }
232  
233 if (scalar @$chunks == 0) { # keep empty chunks
234 $del->execute( '', '', $chunknum, $list );
235 $add->execute( '', '', $chunknum, $list );
236 }
237 }
238  
239 =head1 SEE ALSO
240  
241 See L<Net::Google::SafeBrowsing2> for handling Google Safe Browsing v2.
242  
243 =head1 COPYRIGHT AND LICENSE
244  
245 Copyright 2012 Nick Johnston, nickjohnstonsky@gmail.com. Based on
246 C<Net::Google::SafeBrowsing2> by Julien Sobrier.
247  
248 This library is free software; you can redistribute it and/or modify
249 it under the same terms as Perl itself, either Perl version 5.8.8 or,
250 at your option, any later version of Perl 5 you may have available.
251  
252 =cut
253  
254 1;