| 1 | package App::Netdisco::Web::Plugin::AdminTask::Topology; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use Dancer::Plugin::Ajax; | ||
| 5 | use Dancer::Plugin::DBIC; | ||
| 6 | use Dancer::Plugin::Auth::Extensible; | ||
| 7 | |||
| 8 | use App::Netdisco::Web::Plugin; | ||
| 9 | use App::Netdisco::Util::Device 'get_device'; | ||
| 10 | use App::Netdisco::Util::Port qw/port_acl_check port_acl_by_role_check/; | ||
| 11 | |||
| 12 | use Try::Tiny; | ||
| 13 | use NetAddr::IP::Lite ':lower'; | ||
| 14 | |||
| 15 | # portctl_topology is what lets port_control users at manual topology, and it | ||
| 16 | # must switch the feature off where the work is done, not just hide the | ||
| 17 | # shortcut on device ports: /topology/del clears is_uplink, which would let the | ||
| 18 | # portctl_uplinks guard pass for a real uplink. the page route is registered | ||
| 19 | # once at load, from this setting, so the routes below also check it per | ||
| 20 | # request. | ||
| 21 | register_admin_task({ | ||
| 22 | tag => 'topology', | ||
| 23 | label => 'Manual Device Topology', | ||
| 24 | roles => [ 'admin', (setting('portctl_topology') ? 'port_control' : ()) ], | ||
| 25 | }); | ||
| 26 | |||
| 27 | sub _topology_ok { | ||
| 28 | return 1 if user_has_role('admin'); | ||
| 29 | return (setting('portctl_topology') and user_has_role('port_control')) ? 1 : 0; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub _sanity_ok { | ||
| 33 | my $dev1 = NetAddr::IP::Lite->new(param('dev1')); | ||
| 34 | return 0 unless ($dev1 and $dev1->addr ne '0.0.0.0'); | ||
| 35 | |||
| 36 | my $dev2 = NetAddr::IP::Lite->new(param('dev2')); | ||
| 37 | return 0 unless ($dev2 and $dev2->addr ne '0.0.0.0'); | ||
| 38 | |||
| 39 | return 0 unless param('port1'); | ||
| 40 | return 0 unless param('port2'); | ||
| 41 | |||
| 42 | return 0 if | ||
| 43 | (($dev1->addr eq $dev2->addr) and (param('port1') eq param('port2'))); | ||
| 44 | |||
| 45 | return 1; | ||
| 46 | } | ||
| 47 | |||
| 48 | # These routes write is_uplink and the remote_* columns on a named port, which | ||
| 49 | # is a port change like any other, so they answer to the same role ACL. The | ||
| 50 | # service check is not used: it refuses a port that is already an uplink, which | ||
| 51 | # is what manual topology exists to set. | ||
| 52 | sub _scope_ok { | ||
| 53 | my ($ip, $portname) = @_; | ||
| 54 | return 1 if user_has_role('admin'); | ||
| 55 | |||
| 56 | my $device = get_device($ip); | ||
| 57 | return 0 unless $device and $device->in_storage; | ||
| 58 | my $port = $device->ports->search({ 'me.port' => $portname })->single | ||
| 59 | or return 0; | ||
| 60 | |||
| 61 | return 0 unless port_acl_check($port, $device, logged_in_user); | ||
| 62 | return port_acl_by_role_check($port, $device, logged_in_user) ? 1 : 0; | ||
| 63 | } | ||
| 64 | |||
| 65 | ajax '/ajax/control/admin/topology/add' => require_any_role [qw(admin port_control)] => sub { | ||
| 66 | send_error('Forbidden', 403) unless _topology_ok(); | ||
| 67 | send_error('Bad Request', 400) unless _sanity_ok(); | ||
| 68 | send_error('Forbidden', 403) | ||
| 69 | unless _scope_ok(param('dev1'), param('port1')) | ||
| 70 | and _scope_ok(param('dev2'), param('port2')); | ||
| 71 | |||
| 72 | my $device = schema(vars->{'tenant'})->resultset('Topology') | ||
| 73 | ->create({ | ||
| 74 | dev1 => param('dev1'), | ||
| 75 | port1 => param('port1'), | ||
| 76 | dev2 => param('dev2'), | ||
| 77 | port2 => param('port2'), | ||
| 78 | }); | ||
| 79 | |||
| 80 | # re-set remote device details in affected ports | ||
| 81 | # could fail for bad device or port names | ||
| 82 | try { | ||
| 83 | schema(vars->{'tenant'})->txn_do(sub { | ||
| 84 | # only work on root_ips | ||
| 85 | my $left = get_device(param('dev1')); | ||
| 86 | my $right = get_device(param('dev2')); | ||
| 87 | |||
| 88 | # skip bad entries | ||
| 89 | return unless ($left->in_storage and $right->in_storage); | ||
| 90 | |||
| 91 | $left->ports | ||
| 92 | ->search({port => param('port1')}, {for => 'update'}) | ||
| 93 | ->single() | ||
| 94 | ->update({ | ||
| 95 | remote_ip => param('dev2'), | ||
| 96 | remote_port => param('port2'), | ||
| 97 | remote_type => undef, | ||
| 98 | remote_id => undef, | ||
| 99 | is_uplink => \"true", | ||
| 100 | manual_topo => \"true", | ||
| 101 | }); | ||
| 102 | |||
| 103 | $right->ports | ||
| 104 | ->search({port => param('port2')}, {for => 'update'}) | ||
| 105 | ->single() | ||
| 106 | ->update({ | ||
| 107 | remote_ip => param('dev1'), | ||
| 108 | remote_port => param('port1'), | ||
| 109 | remote_type => undef, | ||
| 110 | remote_id => undef, | ||
| 111 | is_uplink => \"true", | ||
| 112 | manual_topo => \"true", | ||
| 113 | }); | ||
| 114 | }); | ||
| 115 | }; | ||
| 116 | |||
| 117 | return ''; | ||
| 118 | }; | ||
| 119 | |||
| 120 | ajax '/ajax/control/admin/topology/del' => require_any_role [qw(admin port_control)] => sub { | ||
| 121 | send_error('Forbidden', 403) unless _topology_ok(); | ||
| 122 | send_error('Bad Request', 400) unless _sanity_ok(); | ||
| 123 | send_error('Forbidden', 403) | ||
| 124 | unless _scope_ok(param('dev1'), param('port1')) | ||
| 125 | and _scope_ok(param('dev2'), param('port2')); | ||
| 126 | |||
| 127 | my $gone = schema(vars->{'tenant'})->txn_do(sub { | ||
| 128 | return schema(vars->{'tenant'})->resultset('Topology') | ||
| 129 | ->search({ | ||
| 130 | dev1 => param('dev1'), | ||
| 131 | port1 => param('port1'), | ||
| 132 | dev2 => param('dev2'), | ||
| 133 | port2 => param('port2'), | ||
| 134 | })->delete; | ||
| 135 | }); | ||
| 136 | |||
| 137 | # only a deleted manual link may reset its ports: naming the two ends of a | ||
| 138 | # discovered uplink would otherwise clear its is_uplink flag, and with it | ||
| 139 | # the portctl_uplinks guard. DBI reports "no rows" as the true "0E0" | ||
| 140 | return '' unless 0 + ($gone || 0); | ||
| 141 | |||
| 142 | # re-set remote device details in affected ports | ||
| 143 | # could fail for bad device or port names | ||
| 144 | try { | ||
| 145 | schema(vars->{'tenant'})->txn_do(sub { | ||
| 146 | # only work on root_ips | ||
| 147 | my $left = get_device(param('dev1')); | ||
| 148 | my $right = get_device(param('dev2')); | ||
| 149 | |||
| 150 | # skip bad entries | ||
| 151 | return unless ($left->in_storage and $right->in_storage); | ||
| 152 | |||
| 153 | $left->ports | ||
| 154 | ->search({port => param('port1')}, {for => 'update'}) | ||
| 155 | ->single() | ||
| 156 | ->update({ | ||
| 157 | remote_ip => undef, | ||
| 158 | remote_port => undef, | ||
| 159 | remote_type => undef, | ||
| 160 | remote_id => undef, | ||
| 161 | is_uplink => \"false", | ||
| 162 | manual_topo => \"false", | ||
| 163 | }); | ||
| 164 | |||
| 165 | $right->ports | ||
| 166 | ->search({port => param('port2')}, {for => 'update'}) | ||
| 167 | ->single() | ||
| 168 | ->update({ | ||
| 169 | remote_ip => undef, | ||
| 170 | remote_port => undef, | ||
| 171 | remote_type => undef, | ||
| 172 | remote_id => undef, | ||
| 173 | is_uplink => \"false", | ||
| 174 | manual_topo => \"false", | ||
| 175 | }); | ||
| 176 | }); | ||
| 177 | }; | ||
| 178 | |||
| 179 | return ''; | ||
| 180 | }; | ||
| 181 | |||
| 182 | ajax '/ajax/content/admin/topology' => require_any_role [qw(admin port_control)] => sub { | ||
| 183 | send_error('Forbidden', 403) unless _topology_ok(); | ||
| 184 | |||
| 185 | my $set = schema(vars->{'tenant'})->resultset('Topology') | ||
| 186 | ->search({},{order_by => [qw/dev1 dev2 port1/]}); | ||
| 187 | |||
| 188 | content_type('text/html'); | ||
| 189 | template 'ajax/admintask/topology.tt', { | ||
| 190 | results => $set, | ||
| 191 | }, { layout => undef }; | ||
| 192 | }; | ||
| 193 | |||
| 194 | true; |