build device_auth from communities

This commit is contained in:
Oliver Gorwits
2017-09-18 22:46:35 +01:00
parent 1969291719
commit 559fa4f93f
2 changed files with 68 additions and 1 deletions

View File

@@ -1,6 +1,7 @@
package App::Netdisco::Configuration;
use App::Netdisco::Environment;
use App::Netdisco::Util::SNMP ();
use Dancer ':script';
use Path::Class 'dir';
@@ -110,6 +111,9 @@ foreach my $name (qw/discover_only macsuck_only arpnip_only nbtstat_only/) {
push @{setting($name)}, @{ setting('devices_only') };
}
# fix up device_auth (or create it from old settings)
config->{'device_auth'} = [ App::Netdisco::Util::SNMP::fixup_device_auth() ];
# legacy config item names
config->{'devport_vlan_limit'} =

View File

@@ -6,7 +6,7 @@ use App::Netdisco::Util::Permission ':all';
use base 'Exporter';
our @EXPORT = ();
our @EXPORT_OK = qw/
build_communities snmp_comm_reindex
fixup_device_auth build_communities snmp_comm_reindex
/;
our %EXPORT_TAGS = (all => \@EXPORT_OK);
@@ -23,6 +23,69 @@ subroutines.
=head1 EXPORT_OK
=head2 fixup_device_auth
Rebuilds the C<device_auth> config with missing defaults and other fixups for
config changes over time. Returns a list which can replace C<device_auth>.
=cut
sub fixup_device_auth {
my $seen_tags = {}; # for cleaning community table
my $config = (setting('device_auth') || []);
my @new_stanzas = ();
# new style snmp config
foreach my $stanza (@$config) {
# user tagged
my $tag = '';
if (1 == scalar keys %$stanza) {
$tag = (keys %$stanza)[0];
$stanza = $stanza->{$tag};
# corner case: untagged lone community
if ($tag eq 'community') {
$tag = $stanza;
$stanza = {community => $tag};
}
}
# defaults
$stanza->{tag} ||= $tag;
++$seen_tags->{ $stanza->{tag} };
$stanza->{read} = 1 if !exists $stanza->{read};
$stanza->{no} ||= [];
$stanza->{only} ||= ['any'];
die "error: config: snmpv2 community in device_auth must be single item, not list\n"
if ref $stanza->{community};
die "error: config: stanza in device_auth must have a tag\n"
if not $stanza->{tag} and exists $stanza->{user};
push @new_stanzas, $stanza
}
# FIXME: clean the community table of obsolete tags
#if ($stored_tag and !exists $seen_tags->{ $stored_tag }) {
# eval { $device->community->update({$tag_name => undef}) };
#}
# legacy config (note: read strings tried before write)
push @new_stanzas, map {{
read => 1,
community => $_,
}} @{setting('community') || []};
push @new_stanzas, map {{
write => 1,
community => $_,
}} @{setting('community_rw') || []};
return @new_stanzas;
}
=head2 build_communities( $device, $mode )
Takes a Netdisco L<Device|App::Netdisco::DB::Result::Device> instance and