Debian Codegraph @

perllib/qosient/config/policy.pm

raw
1 package qosient::config::policy;
2
3 # Gargoyle Client Software. Tools to read, analyze and manage Argus data.
4 # Copyright (c) 2017-2018 QoSient, LLC
5 # All Rights Reserved
6 #
7 # THE ACCOMPANYING PROGRAM IS PROPRIETARY SOFTWARE OF QoSIENT, LLC,
8 # AND CANNOT BE USED, DISTRIBUTED, COPIED OR MODIFIED WITHOUT
9 # EXPRESS PERMISSION OF QoSIENT, LLC.
10 #
11 # QOSIENT, LLC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
12 # SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 # AND FITNESS, IN NO EVENT SHALL QOSIENT, LLC BE LIABLE FOR ANY
14 # SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
16 # IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
17 # ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
18 # THIS SOFTWARE.
19 #
20 # This library contains routines for accessing the DHCP SQL tables.
21
22 use Carp;
23 use strict;
24 use warnings;
25 use DBI qw(:sql_types);
26 use Try::Tiny;
27 use Exporter;
28 use vars qw($VERSION @ISA @EXPORT);
29 $VERSION = "1.00";
30 @ISA = qw(Exporter);
31 @EXPORT = qw(
32 policy_opendb
33 policy_closedb
34 policy_find_version
35 policy_find_active
36 policy_remove_older
37 policy_insert_policyset
38 policy_create_configuration_table
39 );
40
41 my $debug = 0;
42 my $dbase = q{policy};
43 my $table_ethers = q{configuration};
44 my $dsn;
45 my $dbuser = 'root';
46 my $password = q{};
47 my %attr = ( PrintError => $debug, RaiseError => 0 );
48 my $errcount = 0;
49
50
51 # Build an array of hash references from SQL query results.
52 # Return a reference to the array.
53 my $_result_list = sub {
54 my $sth = shift(@_);
55 my @arr;
56 my $done = 0;
57 while ( $done == 0 ) {
58 my $href = $sth->fetchrow_hashref;
59 if ( !defined $href ) {
60 $done = 1;
61 next;
62 }
63 push @arr, $href;
64 }
65 return \@arr;
66 };
67
68 sub policy_create_configuration_table {
69 my ($dbh) = @_;
70 my $query = q{CREATE TABLE IF NOT EXISTS configuration (}
71 . q{policy_set VARCHAR(64), uuid VARCHAR(37), }
72 . q{stime DOUBLE(18,6) UNSIGNED, ltime DOUBLE(18,6) UNSIGNED, }
73 . q{version INTEGER UNSIGNED, config TEXT, }
74 . q{PRIMARY KEY(policy_set, version), }
75 . q{KEY stime (stime), }
76 . q{KEY ltime (ltime))};
77
78 if (!$dbh) {
79 carp "database handle is undefined";
80 return;
81 }
82
83 if ( !$dbh->do($query) ) {
84 return;
85 }
86 return 1;
87 }
88
89 sub policy_opendb {
90 $dsn = "DBI:mysql:";
91 my $dbh = DBI->connect( $dsn, $dbuser, $password, \%attr );
92 if ($dbh) {
93 if ( !$dbh->do("CREATE DATABASE IF NOT EXISTS $dbase") ) {
94 policy_closedb($dbh);
95 $dbh = undef;
96 } else {
97 $dbh->do("USE $dbase");
98 policy_create_configuration_table($dbh);
99 }
100 }
101 return $dbh; # undefined on error
102 }
103
104 sub policy_closedb {
105 my ($dbh) = @_;
106
107 if (!$dbh) {
108 carp "database handle is undefined";
109 return;
110 }
111
112 $dbh->disconnect();
113 return 0;
114 }
115
116 #+------------+-----------------------+------+-----+---------+-------+
117 #| Field | Type | Null | Key | Default | Extra |
118 #+------------+-----------------------+------+-----+---------+-------+
119 #| policy_set | varchar(64) | NO | PRI | NULL | |
120 #| uuid | varchar(37) | YES | MUL | NULL | |
121 #| stime | double(18,6) unsigned | YES | MUL | NULL | |
122 #| ltime | double(18,6) unsigned | YES | | NULL | |
123 #| version | int(10) unsigned | NO | PRI | NULL | |
124 #| config | text | YES | | NULL | |
125 #+------------+-----------------------+------+-----+---------+-------+
126
127 # Close the time interval on some previous version of the policy described
128 # by $policy_set. Set the ltime column to the specified (usually current)
129 # time.
130 #
131 # Return the number of rows changed (should be zero or one). Return
132 # undefined on failure.
133 #
134 # $policy_set is a hash reference with keys:
135 # policy_set (the policy-set name)
136 # uuid
137 # stime
138 # ltime
139 # version
140 # config
141 # $oldver is an integer value
142 # $when is the closing time in seconds after the Unix Epoch UTC.
143 sub policy_invalidate_policyset {
144 my ($dbh, $policy_set, $oldver, $when) = @_;
145 my $query =
146 q{UPDATE configuration SET ltime = ? WHERE policy_set = ? }
147 . q{AND version = ?};
148 my @params = ($when, $policy_set->{'policy_set'}, $oldver);
149
150 if (!$dbh) {
151 carp "database handle is undefined";
152 return;
153 }
154
155 my $sth = $dbh->prepare($query);
156 if (!$sth) {
157 return;
158 }
159
160 my $res = $sth->execute(@params);
161 $sth->finish;
162 if ($res && $res > 1) {
163 carp "Too many policy-sets invalidated?";
164 }
165 return $res;
166 }
167
168 # $policy_set is a hash reference with keys:
169 # policy_set (the policy-set name)
170 # uuid
171 # stime
172 # ltime
173 # version
174 # config
175 sub policy_insert_policyset {
176 # TODO: use transaction to roll back after partial failure
177 my ($dbh, $policy_set) = @_;
178 my $query =
179 q{INSERT INTO configuration (policy_set, version, uuid, stime, }
180 . q{config) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE ltime=?};
181 my @params = ($policy_set->{'policy_set'}, $policy_set->{'version'},
182 $policy_set->{'uuid'}, $policy_set->{'stime'},
183 $policy_set->{'config'}, $policy_set->{'ltime'});
184
185 if (!$dbh) {
186 carp "database handle is undefined";
187 return;
188 }
189
190 my $stmt = $dbh->prepare($query);
191 if (!$stmt) {
192 return;
193 }
194
195 my $res = $stmt->execute(@params);
196 $stmt->finish;
197 if (!$res) {
198 return;
199 }
200
201 # Now invalidate the previous version, if any.
202 my $ver = $policy_set->{'version'};
203 if ($ver > 1) {
204 my $num = policy_invalidate_policyset($dbh, $policy_set, $ver - 1,
205 $policy_set->{'stime'});
206 }
207 return 1;
208 }
209
210 # remove policy-sets from the configuration table that are older than
211 # the specified date/time.
212 #
213 # $when is specified in seconds after the Unix Epoch UTC
214 # Important to only remove those with ltime > 0 since newly added rows
215 # may have a zero value for ltime.
216 sub policy_remove_older {
217 my ($dbh, $when) = @_;
218 my $query = q{DELETE FROM configuration WHERE ltime > 0 AND ltime < ?};
219 my @params = ($when);
220
221 if (!$dbh) {
222 carp "database handle is undefined";
223 return;
224 }
225
226 my $stmt = $dbh->prepare($query);
227 if (!$stmt) {
228 return;
229 }
230
231 my $res = $stmt->execute(@params);
232 $stmt->finish;
233 if ($debug) {
234 if (!$res) {
235 print STDERR "Removal of rows from configuration table failed\n";
236 }
237 else {
238 print STDERR "Removed $res rows from the configuration table\n";
239 }
240 }
241 return $res;
242 }
243
244 # Find the policies that were active during a particular period of time
245 # $begin and $end are times specified in seconds after the Unix Epoch UTC.
246 #
247 # Returns a reference to an array of hashrefs with a key for each column of
248 # the configuration SQL table.
249 sub policy_find_active {
250 my ($dbh, $begin, $end) = @_;
251 my $query =
252 # WHERE $begin <= ltime && $end >= stime
253 q{SELECT * FROM configuration WHERE ? <= ltime && ? >= stime};
254 my @params = ($begin, $end);
255
256 if (!$dbh) {
257 carp "database handle is undefined";
258 return;
259 }
260
261 my $stmt = $dbh->prepare($query);
262 if (!$stmt) {
263 return;
264 }
265
266 my $res = $stmt->execute(@params);
267 if (!$res) {
268 $stmt->finish;
269 return;
270 }
271
272 my $aref = $_result_list->($stmt);
273 $stmt->finish;
274 return $aref;
275 }
276
277 # Find the latest version number for a policy-set
278 sub policy_find_version {
279 my ($dbh, $policy_set_name) = @_;
280 my $query = q{SELECT MAX(version) FROM configuration WHERE policy_set = ?};
281 my @params = ($policy_set_name);
282
283 if (!$dbh) {
284 carp "database handle is undefined";
285 return;
286 }
287
288 my $sth = $dbh->prepare($query);
289 if (!$sth) {
290 return;
291 }
292
293 my $res = $sth->execute(@params);
294 if (!$res) {
295 $sth->finish;
296 return;
297 }
298
299 my $aref = $sth->fetchrow_arrayref;
300 if (!$aref) {
301 # No policy-set by this name, start at zero
302 $sth->finish;
303 return 0;
304 }
305
306 $sth->finish;
307 return $aref->[0];
308 }
309
310 1;