GlbDNS/Zone.pm

filter: Statementstmt 13.1% 13 / 99 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 44.4% 4 / 9 Podpod 100% 3 / 3 totaltotal 10.1% 20 / 197 CC 41 SCAR 53.4
CC
41
Cov
8%
parse
306.0
geo_fix
272.0
load_configs
72.0
CRAP 207.6
« GlbDNS/Resolver/Base.pm ↑ Summary GlbDNS/Resolver/ShowServer.pm »
1 package GlbDNS::Zone;
2  
3  
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Net::DNS::RR::A;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 =head1 GlbDNS::Zone
11  
12 Parsing zone files with LOC data
13  
14 =head2 load_configs
15  
16 GlbDNS->load_configs($glbdns, $path);
17 GlbDNS->load_configs($glbdns, $file);
18  
19 =cut
20  
21 0 ▶sub load_configs {
Subroutine load_configs: not called CC=8 SCAR=42.8
Pod documented
22 0 my $class = shift;
23 0 my $glbdns = shift;
24 0 my $path = shift;
25 0 ▶ if (-d $path) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $path) { } 0 0 0
elsif (-f $path) { } 0 0 0
26 0 ▶ opendir(DIR, $path) || die "Cannot open directory '$path': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $path or die "Cannot open directory '${path}': $!\n" 0 0 0
27 0 for my $file (readdir(DIR)) {
28 0 ▶ next if (-d $file);
Branch: 1 branch
BranchTrueFalseTotal
if -d $file 0 0 0
29 0 ▶ next if ($file =~/^(\.|#)/);
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /^(\.|#)/ 0 0 0
30 0 ▶ next if ($file =~/~$/);
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /~$/ 0 0 0
31 0 $class->parse($glbdns, "$path/$file");
32 }
33 } elsif (-f $path) {
34 0 $class->parse($glbdns, $path);
35 } else {
36 0 die "Cannot find zone '$path'\n";
37 }
38 0 $class->geo_fix($glbdns);
39 }
40  
41 =head2 parse
42  
43 GlbDNS->parse($glbdns, $file);
44  
45 =cut
46  
47 0 ▶sub parse {
Subroutine parse: not called CC=17 SCAR=57.2
Pod documented
48 0 my $class = shift;
49 0 my $glbdns = shift;
50 0 my $file = shift;
51  
52 0 ▶ open(my $fh, "<", "$file") || die "Cannot open file '$file': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", "$file" or die "Cannot open file '${file}': $!\n" 0 0 0
53 0 my $mtime = @{[stat("$file")]}[9];
54  
55 0 ▶ my $error = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 0 die "$_[0] at $file:$.\n";
57 0 };
58  
59  
60  
61 0 my $base_fqdn;
62 my $base;
63 0 ▶ my $hosts = $glbdns->{hosts} ||= {};
Condition0%0/2
$glbdns->{"hosts"} ||= {}
l!l
00
MC/DC0%0/1
$glbdns->{"hosts"} ||= {}
$glbdns->{"hosts"}
Truth table0%0/2
A
A= $glbdns->{"hosts"}
Aresult
11
00
64 0 ▶ while(my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
65 0 chomp($line);
66 0 ▶ next unless($line);
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 0 0
67 0 ▶ next if($line =~ /^\s+$/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s+$/ 0 0 0
68 0 ▶ next if($line =~ /^;/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^;/ 0 0 0
69  
70 0 ▶ if($line =~/\$ORIGIN\s+([a-zA-Z.\-]+)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /\$ORIGIN\s+([a-zA-Z.\-]+)/) { } 0 0 0
elsif (not $base) { } 0 0 0
71 0 $base_fqdn = $1;
72 0 ($base) = $base_fqdn =~/(.*)\.$/;
73 0 ▶ $error->("'$base_fqdn' needs to be terminated with a . to be a FQDN") unless ($base);
Branch: 1 branch
BranchTrueFalseTotal
unless $base 0 0 0
74 0 next;
75 } elsif (!$base) {
76 0 $error->("No \$ORIGIN domain has been specified, don't know what domain we are working on");
77 }
78  
79 0 my @record = split /\s+/, $line;
80  
81 # if the first record is a DNS entry
82 # then check if it is a FQDN and complete it
83 # or use the default one
84 0 ▶ if ($record[0] !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $record[0] =~ /^\d+$/) { } 0 0 0
85 0 ▶ $record[0] = "$record[0].$base" if($record[0] !~ /\.$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $record[0] =~ /\.$/ 0 0 0
86 } else {
87 0 unshift @record, $base;
88 }
89  
90 # fully qualify CNAMEs
91 0 ▶ if($record[3] eq 'CNAME' && $record[4] !~/\.$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($record[3] eq "CNAME" and not $record[4] =~ /\.$/) 0 0 0
Condition0%0/3
$record[3] eq "CNAME" and not $record[4] =~ /\.$/
!ll&&!rl&&r
000
MC/DC0%0/2
$record[3] eq "CNAME" and not $record[4] =~ /\.$/
$record[3] eq "CNAME" not $record[4] =~ /\.$/
Truth table0%0/3
A and not(B)
A= $record[3] eq "CNAME"B= not $record[4] =~ /\.$/
ABresult
0X0
100
111
92 0 $record[4] .= ".$base";
93 }
94 # fully qualify MX
95 0 ▶ if($record[3] eq 'MX' && $record[5] !~/\.$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($record[3] eq "MX" and not $record[5] =~ /\.$/) 0 0 0
Condition0%0/3
$record[3] eq "MX" and not $record[5] =~ /\.$/
!ll&&!rl&&r
000
MC/DC0%0/2
$record[3] eq "MX" and not $record[5] =~ /\.$/
$record[3] eq "MX" not $record[5] =~ /\.$/
Truth table0%0/3
A and not(B)
A= $record[3] eq "MX"B= not $record[5] =~ /\.$/
ABresult
0X0
100
111
96 0 $record[5] .= ".$base";
97 }
98  
99  
100 0 ▶ my $add_host = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
101 0 my $record = shift;
102 0 ▶ my $host = $hosts->{$record->name} ||= {};
Condition0%0/2
$$hosts{$record->name} ||= {}
l!l
00
MC/DC0%0/1
$$hosts{$record->name} ||= {}
$$hosts{$record->name}
Truth table0%0/2
A
A= $$hosts{$record->name}
Aresult
11
00
103 0 ▶ my $records = $host->{$record->type} ||= [];
Condition0%0/2
$$host{$record->type} ||= []
l!l
00
MC/DC0%0/1
$$host{$record->type} ||= []
$$host{$record->type}
Truth table0%0/2
A
A= $$host{$record->type}
Aresult
11
00
104 0 $host->{__RECORD__} = $record->name;
105 0 $host->{domain} = $host->{__DOMAIN__} = $base;
106 0 push @$records, $record;
107 0 };
108  
109 0 my $rr = Net::DNS::RR->new(join " ", @record);
110  
111 # autocreate PTR records for A records
112 # there can be more than one
113 0 ▶ if ($rr->type eq 'A') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->type eq "A") 0 0 0
114 0 my $address = join(".", reverse( split(/\./, $rr->address)) ) ;
115 0 my $reverse = Net::DNS::RR->new("$address.in-addr.arpa. " . $rr->ttl . " IN PTR " . $rr->name);
116 0 $add_host->($reverse);
117 }
118  
119  
120 0 $add_host->($rr);
121  
122  
123  
124  
125  
126 }
127 0 close($fh);
128 }
129  
130 =head2 geo_fix
131  
132 GlbDNS::Zone->geo_fix($glbdns);
133  
134 =cut
135  
136 0 ▶sub geo_fix {
Subroutine geo_fix: not called CC=16 SCAR=56.1
Pod documented
137 0 my $class = shift;
138 0 my $glbdns = shift;
139 0 my $hosts = $glbdns->{hosts};
140 # now go through and fix up the geolocation ones
141 0 foreach my $host (values %{$hosts}) {
142 0 ▶ if ($host->{CNAME} && @{$host->{CNAME}} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host->{"CNAME"} and @{$host->{"CNAME"};} > 1) 0 0 0
Condition0%0/3
$host->{"CNAME"} and @{$host->{"CNAME"};} > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$host->{"CNAME"} and @{$host->{"CNAME"};} > 1
$host->{"CNAME"} @{$host->{"CNAME"};} > 1
Truth table0%0/3
A and B
A= $host->{"CNAME"}B= @{$host->{"CNAME"};} > 1
ABresult
0X0
100
111
143 # more than one cname is not allowed
144 # so they have to point to geo tagged records
145 # or we abort
146 0 foreach my $cname (@{$host->{CNAME}}) {
147 0 my $target = $hosts->{$cname->cname};
148 0 ▶ die "Need record for " . $cname->cname . "\n" unless $target;
Branch: 1 branch
BranchTrueFalseTotal
unless $target 0 0 0
149 0 ▶ die "Record " . $cname->name . " needs LOC data\n" unless $target->{LOC};
Branch: 1 branch
BranchTrueFalseTotal
unless $target->{"LOC"} 0 0 0
150  
151 0 my ($lat, $lon) = $target->{LOC}[0]->latlon;
152 0 ▶ my $geo = $host->{__GEO__} ||= {};
Condition0%0/2
$host->{"__GEO__"} ||= {}
l!l
00
MC/DC0%0/1
$host->{"__GEO__"} ||= {}
$host->{"__GEO__"}
Truth table0%0/2
A
A= $host->{"__GEO__"}
Aresult
11
00
153  
154 0 ▶ die "Trying to overwrite geo target $target->{__RECORD__}\n" if($geo->{$target->{__RECORD__}});
Branch: 1 branch
BranchTrueFalseTotal
if $$geo{$target->{"__RECORD__"}} 0 0 0
155 0 my $geo_entry = $geo->{$target->{__RECORD__}} = {};
156  
157 0 $geo_entry->{lat} = $lat;
158 0 $geo_entry->{lon} = $lon;
159 0 ▶ $geo_entry->{hosts} = $target->{A} || $target->{CNAME} || die "Need A or CNAME for $target->{__RECORD__}\n";
Condition0%0/5
$target->{'A'} || $target->{'CNAME'} || die("Need A or CNAME for $target->{'__RECORD__'}\n")
l!l
00
$target->{"A"} or $target->{"CNAME"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$target->{'A'} || $target->{'CNAME'} || die("Need A or CNAME for $target->{'__RECORD__'}\n")
$target->{"A"} $target->{"CNAME"}
Truth table0%0/3
A or B
A= $target->{"A"}B= $target->{"CNAME"}
ABresult
1X1
011
000
160 0 ▶ if ($target->{TXT}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target->{"TXT"}) 0 0 0
161 0 foreach my $txt (@{$target->{TXT}}) {
162 0 my @txt = $txt->char_str_list;
163 0 ▶ if($txt[0] eq 'GlbDNS::RADIUS') {
Branch: 2 branches
BranchTrueFalseTotal
if ($txt[0] eq 'GlbDNS::RADIUS') { } 0 0 0
elsif ($txt[0] eq 'GlbDNS::CHECK') { } 0 0 0
164 0 $geo_entry->{radius} = $txt[1];
165 } elsif($txt[0] eq 'GlbDNS::CHECK') {
166 0 foreach my $check_host (@{$geo_entry->{hosts}}) {
167 0 $glbdns->{checks}->{$check_host->address} = {
168 ip => $check_host->address,
169 url => $txt[1],
170 expect => $txt[2],
171 interval => 5,
172 };
173 }
174 }
175 }
176 }
177 0 $geo_entry->{source}->{$host->{__RECORD__}} = $cname;
178 }
179 0 delete($host->{CNAME});
180 }
181 }
182 }
183  
184  
185 7 1;
« GlbDNS/Resolver/Base.pm ↑ Summary GlbDNS/Resolver/ShowServer.pm »