blib/lib/App/Netdisco/Util/DeviceAuth.pm untested

filter: Statementstmt 0.0% 0 / 96 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 61 MC/DCmcdc 0.0% 0 / 35 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 235 CC 18 SCAR 43.8
CC
18
Cov
0%
get_external_credentials
110.0
fixup_device_auth
90.0
BEGIN
2.0
CRAP 80.0
1 package App::Netdisco::Util::DeviceAuth;
2  
3 use Dancer qw/:syntax :script/;
4 use App::Netdisco::Util::DNS 'hostname_from_ip';
5  
6 use Storable 'dclone';
7 use Try::Tiny;
8  
9 use base 'Exporter';
10 our @EXPORT = ();
11 our @EXPORT_OK = qw/
12 fixup_device_auth get_external_credentials
13 /;
14 our %EXPORT_TAGS = (all => \@EXPORT_OK);
15  
16 =head1 NAME
17  
18 App::Netdisco::Util::DeviceAuth
19  
20 =head1 DESCRIPTION
21  
22 Helper functions for device authentication.
23  
24 There are no default exports, however the C<:all> tag will export all
25 subroutines.
26  
27 =head1 EXPORT_OK
28  
29 =head2 fixup_device_auth
30  
31 Rebuilds the C<device_auth> config with missing defaults and other fixups for
32 config changes over time. Returns a list which can replace C<device_auth>.
33  
34 =cut
35  
36 sub fixup_device_auth {
37 my $da = dclone (setting('device_auth') || []);
38 my $sa = dclone (setting('snmp_auth') || []);
39  
40 die "error: both snmp_auth and device_auth are defined!\n"
41 . "move snmp_auth config into device_auth and remove snmp_auth.\n"
42 if scalar @$da and scalar @$sa;
43  
44 my $config = ((scalar @$da) ? $da : $sa);
45 my @new_stanzas = ();
46  
47 # new style snmp config
48 foreach my $stanza (@$config) {
49 # user tagged
50 my $tag = '';
51 if (1 == scalar keys %$stanza) {
52 $tag = (keys %$stanza)[0];
53 $stanza = $stanza->{$tag};
54  
55 # corner case: untagged lone community
56 if ($tag eq 'community') {
57 $tag = $stanza;
58 $stanza = {community => $tag};
59 }
60 }
61  
62 # defaults
63 $stanza->{tag} ||= $tag;
64 $stanza->{read} = 1 if !exists $stanza->{read};
65 $stanza->{no} ||= [];
66 $stanza->{only} ||= ['group:__ANY__'];
67  
68 die "error: config: snmpv2 community in device_auth must be single item, not list\n"
69 if ref $stanza->{community};
70  
71 die "error: config: stanza in device_auth must have a tag\n"
72 if not $stanza->{tag} and exists $stanza->{user};
73  
74 push @new_stanzas, $stanza;
75 }
76  
77 # import legacy sshcollector configuration
78 my @sshcollector = @{ dclone (setting('sshcollector') || []) };
79 foreach my $stanza (@sshcollector) {
80 # defaults
81 $stanza->{driver} = 'cli';
82 $stanza->{read} = 1;
83 $stanza->{no} ||= [];
84  
85 # fixups
86 $stanza->{only} ||= [ scalar delete $stanza->{ip} ||
87 scalar delete $stanza->{hostname} ];
88 $stanza->{username} = scalar delete $stanza->{user};
89  
90 push @new_stanzas, $stanza;
91 }
92  
93 # legacy config
94 # note: read strings tried before write
95 # note: read-write is no longer used for read operations
96  
97 push @new_stanzas, map {{
98 read => 1, write => 0,
99 no => [], only => ['group:__ANY__'],
100 community => $_,
101 }} @{setting('community') || []};
102  
103 push @new_stanzas, map {{
104 write => 1, read => 0,
105 no => [], only => ['group:__ANY__'],
106 community => $_,
107 }} @{setting('community_rw') || []};
108  
109 foreach my $stanza (@new_stanzas) {
110 $stanza->{driver} ||= 'snmp'
111 if exists $stanza->{community}
112 or exists $stanza->{user};
113 }
114  
115 return @new_stanzas;
116 }
117  
118 =head2 get_external_credentials( $device, $mode )
119  
120 Runs a command to gather SNMP credentials or a C<device_auth> stanza.
121  
122 Mode can be C<read> or C<write> and defaults to 'read'.
123  
124 =cut
125  
126 sub get_external_credentials {
127 my ($device, $mode) = @_;
128 my $cmd = (setting('get_credentials') || setting('get_community'));
129 my $ip = $device->ip;
130 my $host = ($device->dns || hostname_from_ip($ip) || $ip);
131 $mode ||= 'read';
132  
133 # the command below reaches a shell, and this name comes from DNS, so fall
134 # back to the address unless it is shaped like a hostname
135 $host = $ip unless $host =~ m/^[A-Za-z0-9](?:[A-Za-z0-9.-]*[A-Za-z0-9])?$/;
136  
137 if (defined $cmd and length $cmd) {
138 # replace variables
139 $cmd =~ s/\%MODE\%/$mode/egi;
140 $cmd =~ s/\%HOST\%/$host/egi;
141 $cmd =~ s/\%IP\%/$ip/egi;
142  
143 my $result = (-r $cmd && ! -x $cmd) ? do { local (@ARGV, $/) = $cmd; <> } : `$cmd`; # BACKTICKS
144 return () unless defined $result and length $result;
145  
146 my @lines = split (m/\n/, $result);
147 foreach my $line (@lines) {
148 if ($line =~ m/^community\s*=\s*(.*)\s*$/i) {
149 if (length $1 and $mode eq 'read') {
150 debug sprintf '[%s] external read credentials added',
151 $device->ip;
152  
153 return map {{
154 read => 1,
155 only => [$device->ip],
156 community => $_,
157 }} split(m/\s*,\s*/,$1);
158 }
159 }
160 elsif ($line =~ m/^setCommunity\s*=\s*(.*)\s*$/i) {
161 if (length $1 and $mode eq 'write') {
162 debug sprintf '[%s] external write credentials added',
163 $device->ip;
164  
165 return map {{
166 write => 1,
167 only => [$device->ip],
168 community => $_,
169 }} split(m/\s*,\s*/,$1);
170 }
171 }
172 else {
173 my $stanza = undef;
174 try {
175 $stanza = from_json( $line );
176 debug sprintf '[%s] external credentials stanza added',
177 $device->ip;
178 }
179 catch {
180 info sprintf '[%s] error! failed to parse external credentials stanza',
181 $device->ip;
182 };
183 return $stanza if ref $stanza;
184 }
185 }
186 }
187  
188 return ();
189 }
190  
191 true;