« blib/lib/App/Netdisco/SSHCollector/Platform/OS10.pm
↑ Summary
blib/lib/App/Netdisco/Web/Plugin/AdminTask/PseudoDevice.pm »
| 1 | package App::Netdisco::Util::ManagedConfig; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | # config processing must be finished before the YAML copy is taken | ||
| 7 | use App::Netdisco::Configuration (); | ||
| 8 | |||
| 9 | use Dancer qw/:syntax :script/; | ||
| 10 | use Dancer::Plugin::DBIC 'schema'; | ||
| 11 | |||
| 12 | use Storable 'dclone'; | ||
| 13 | use Try::Tiny; | ||
| 14 | |||
| 15 | use base 'Exporter'; | ||
| 16 | our @EXPORT_OK = qw/ | ||
| 17 | tenant_key | ||
| 18 | acl_txn_do | ||
| 19 | load_managed_config | ||
| 20 | capture_yaml_settings | ||
| 21 | yaml_setting | ||
| 22 | is_reserved_group_name | ||
| 23 | /; | ||
| 24 | |||
| 25 | # kept here and not in CONFIG, so that dumpconfig and setting() only ever see | ||
| 26 | # what the code actually uses. declared with our so that tests can reset them. | ||
| 27 | our %YAML; # setting name => clean copy of its YAML value | ||
| 28 | our $CAPTURED; # true once %YAML has been taken in this process | ||
| 29 | our %CACHE; # tenant key => { version => Int, overlay => HashRef } | ||
| 30 | |||
| 31 | =head1 NAME | ||
| 32 | |||
| 33 | App::Netdisco::Util::ManagedConfig - ACL Manager data loaded into the config | ||
| 34 | |||
| 35 | =head1 DESCRIPTION | ||
| 36 | |||
| 37 | The C<config_version> table holds one counter per scope. The C<acl> counter | ||
| 38 | goes up by one every time the ACL Manager writes, so a process can tell | ||
| 39 | whether its copy is stale with one cheap read. | ||
| 40 | |||
| 41 | Only writes made through L</"acl_txn_do( $coderef )"> bump it, so after | ||
| 42 | editing the ACL tables by hand restart C<netdisco-web> and | ||
| 43 | C<netdisco-backend>, or run: | ||
| 44 | |||
| 45 | UPDATE config_version SET version = version + 1 WHERE scope = 'acl'; | ||
| 46 | |||
| 47 | =head1 EXPORT_OK | ||
| 48 | |||
| 49 | =head2 tenant_key | ||
| 50 | |||
| 51 | Returns the current tenant, or C<netdisco> for the default tenant. | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | sub tenant_key { return (vars->{'tenant'} || 'netdisco') } | ||
| 56 | |||
| 57 | =head2 acl_txn_do( $coderef ) | ||
| 58 | |||
| 59 | Runs C<$coderef> in a transaction on the current tenant's database, then bumps | ||
| 60 | the C<acl> counter in that same transaction. Returns nothing. If C<$coderef> | ||
| 61 | dies nothing is bumped and the error propagates. | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | sub acl_txn_do { | ||
| 66 | my $code = shift; | ||
| 67 | # the name the routes write through, not tenant_key(): for the default | ||
| 68 | # tenant schema('netdisco') can be another connection than schema(undef), | ||
| 69 | # and the route writes would then fall outside this transaction | ||
| 70 | my $schema = schema(vars->{'tenant'}); | ||
| 71 | $schema->txn_do(sub { $code->(); _bump_config_version($schema) }); | ||
| 72 | return; | ||
| 73 | } | ||
| 74 | |||
| 75 | # Plain DBI rather than a resultset: this read happens on every request, and | ||
| 76 | # DBIC costs about 1 ms against 0.03 ms. Dies if the table or row is missing | ||
| 77 | # (database not yet upgraded); the caller decides what that means. | ||
| 78 | sub _read_config_version { | ||
| 79 | my $tenant_key = shift; | ||
| 80 | return schema($tenant_key)->storage->dbh_do(sub { | ||
| 81 | my ($storage, $dbh, @bind) = @_; | ||
| 82 | my ($version) = $dbh->selectrow_array( | ||
| 83 | 'SELECT version FROM config_version WHERE scope = ?', undef, @bind); | ||
| 84 | return $version; | ||
| 85 | }, 'acl'); | ||
| 86 | } | ||
| 87 | |||
| 88 | sub _bump_config_version { | ||
| 89 | my $schema = shift; | ||
| 90 | $schema->storage->dbh_do(sub { | ||
| 91 | my ($storage, $dbh, @bind) = @_; | ||
| 92 | $dbh->do('UPDATE config_version SET version = version + 1 WHERE scope = ?', | ||
| 93 | undef, @bind); | ||
| 94 | }, 'acl'); | ||
| 95 | return; | ||
| 96 | } | ||
| 97 | |||
| 98 | =head2 load_managed_config | ||
| 99 | |||
| 100 | Brings the current tenant's ACL Manager roles and named groups into | ||
| 101 | C<portctl_by_role> and C<host_groups>, reading the ACL tables only if the | ||
| 102 | counter moved since the last load. The first call in a process takes the clean | ||
| 103 | YAML copy (see L</capture_yaml_settings>). | ||
| 104 | |||
| 105 | A failed database read never makes it die. It can die only while taking the | ||
| 106 | YAML copy, if one of the two settings is a hash holding something | ||
| 107 | L<Storable> can't copy, such as a code reference, which YAML can't express. | ||
| 108 | A value of either setting that isn't a hash at all counts as empty. | ||
| 109 | |||
| 110 | =cut | ||
| 111 | |||
| 112 | sub load_managed_config { | ||
| 113 | capture_yaml_settings() unless $CAPTURED; | ||
| 114 | my $tenant_key = tenant_key(); | ||
| 115 | _refresh($tenant_key); | ||
| 116 | _apply($tenant_key); | ||
| 117 | return; | ||
| 118 | } | ||
| 119 | |||
| 120 | # reads the tenant's counter and, if it moved, rebuilds the tenant's overlay | ||
| 121 | # from its ACL rows. a failed read drops the tenant's cache entry. | ||
| 122 | sub _refresh { | ||
| 123 | my $tenant_key = shift; | ||
| 124 | |||
| 125 | # a failed read (no database, or no config_version row because the schema | ||
| 126 | # is not upgraded yet) counts as an empty ACL Manager, the way get_user_roles | ||
| 127 | # in Web/Auth/Provider/DBIC.pm returns no roles when its query fails. the | ||
| 128 | # failure is not cached, so the next call tries again. | ||
| 129 | try { | ||
| 130 | # the counter is read before the rows: a write that commits between the | ||
| 131 | # two reads then leaves new rows stored under the old counter, which only | ||
| 132 | # costs one more reload. reading the rows first could store old rows under | ||
| 133 | # the new counter, and they would never be reloaded. | ||
| 134 | my $version = _read_config_version($tenant_key); | ||
| 135 | die "no acl row in config_version\n" unless defined $version; | ||
| 136 | |||
| 137 | return if exists $CACHE{$tenant_key} | ||
| 138 | and $CACHE{$tenant_key}->{version} == $version; | ||
| 139 | |||
| 140 | $CACHE{$tenant_key} = { | ||
| 141 | version => $version, | ||
| 142 | overlay => _build_overlay($tenant_key, _read_acl_rows($tenant_key)), | ||
| 143 | }; | ||
| 144 | } | ||
| 145 | catch { | ||
| 146 | delete $CACHE{$tenant_key}; | ||
| 147 | }; | ||
| 148 | |||
| 149 | return; | ||
| 150 | } | ||
| 151 | |||
| 152 | # writes the clean YAML copy merged with the tenant's overlay into CONFIG; ACL | ||
| 153 | # Manager entries replace YAML ones of the same name. with no overlay (a failed | ||
| 154 | # read) this is the YAML alone, so a worker never keeps stale database roles, | ||
| 155 | # or those of the last request's tenant. | ||
| 156 | sub _apply { | ||
| 157 | my $tenant_key = shift; | ||
| 158 | |||
| 159 | my $overlay = exists $CACHE{$tenant_key} | ||
| 160 | ? $CACHE{$tenant_key}->{overlay} | ||
| 161 | : { portctl_by_role => {}, host_groups => {} }; | ||
| 162 | |||
| 163 | # new top-level hashes every time, so a job's override merged into the last | ||
| 164 | # apply never carries into the next one | ||
| 165 | my $yaml_roles = $YAML{'portctl_by_role'} || {}; | ||
| 166 | config->{'portctl_by_role'} = { | ||
| 167 | (map { ($_ => ($yaml_roles->{$_} || '!group:__ANY__')) } keys %$yaml_roles), | ||
| 168 | %{ $overlay->{'portctl_by_role'} }, | ||
| 169 | }; | ||
| 170 | config->{'host_groups'} = { | ||
| 171 | %{ $YAML{'host_groups'} || {} }, | ||
| 172 | %{ $overlay->{'host_groups'} }, | ||
| 173 | }; | ||
| 174 | |||
| 175 | return; | ||
| 176 | } | ||
| 177 | |||
| 178 | =head2 capture_yaml_settings | ||
| 179 | |||
| 180 | Takes the clean copy of the YAML values that the ACL Manager adds to, replacing | ||
| 181 | any copy taken before. A value that isn't a hash is taken as an empty hash. | ||
| 182 | |||
| 183 | L</load_managed_config> and L</"yaml_setting( $name )"> call this themselves the first | ||
| 184 | time either runs in a process, so nothing else needs to. Tests call it again | ||
| 185 | after setting new YAML values. | ||
| 186 | |||
| 187 | It is taken then, not as this module loads, so that site web plugins (which | ||
| 188 | C<Web.pm> loads later) and values a test sets before its first request or job | ||
| 189 | are in the copy. | ||
| 190 | |||
| 191 | =cut | ||
| 192 | |||
| 193 | sub capture_yaml_settings { | ||
| 194 | foreach my $name (qw/portctl_by_role host_groups/) { | ||
| 195 | my $value = setting($name); | ||
| 196 | # every request and job applies this copy, so a mistyped value such as | ||
| 197 | # portctl_by_role: [] must not make all of them die, discovery included | ||
| 198 | $YAML{$name} = ((ref $value eq 'HASH') ? dclone($value) : {}); | ||
| 199 | } | ||
| 200 | $CAPTURED = 1; | ||
| 201 | return; | ||
| 202 | } | ||
| 203 | |||
| 204 | =head2 yaml_setting( $name ) | ||
| 205 | |||
| 206 | Returns the clean YAML copy of C<portctl_by_role> or C<host_groups>, without | ||
| 207 | anything from the ACL Manager. Takes the copy first if this process hasn't yet. | ||
| 208 | |||
| 209 | =cut | ||
| 210 | |||
| 211 | sub yaml_setting { | ||
| 212 | capture_yaml_settings() unless $CAPTURED; | ||
| 213 | return $YAML{ $_[0] }; | ||
| 214 | } | ||
| 215 | |||
| 216 | =head2 is_reserved_group_name( $name ) | ||
| 217 | |||
| 218 | True for a name a host ACL may not take, because it would replace a group | ||
| 219 | Netdisco relies on: the C<__NAME__> groups such as C<__ANY__> that | ||
| 220 | C<share/config.yml> ships, and the C<synthesized_group_> names this module | ||
| 221 | makes for each ACL row. | ||
| 222 | |||
| 223 | =cut | ||
| 224 | |||
| 225 | sub is_reserved_group_name { | ||
| 226 | my $name = shift; | ||
| 227 | return 0 unless defined $name; | ||
| 228 | return (($name =~ m/^__.+__$/) or ($name =~ m/^synthesized_group_/)) ? 1 : 0; | ||
| 229 | } | ||
| 230 | |||
| 231 | # host_host ACLs are not read: nothing uses them as config | ||
| 232 | sub _read_acl_rows { | ||
| 233 | my $tenant_key = shift; | ||
| 234 | return schema($tenant_key)->resultset('AccessControlListName')->search( | ||
| 235 | { 'me.acl_type' => [qw/host host_port/] }, | ||
| 236 | { prefetch => { mappings => [qw/left_acl right_acl/] }, | ||
| 237 | order_by => [qw/me.acl_name mappings.id/] }, | ||
| 238 | )->all; | ||
| 239 | } | ||
| 240 | |||
| 241 | # synthesized group names carry the tenant because ACL ids from different | ||
| 242 | # tenant databases can clash | ||
| 243 | sub _build_overlay { | ||
| 244 | my ($tenant_key, @rows) = @_; | ||
| 245 | my (%roles, %groups); | ||
| 246 | |||
| 247 | foreach my $name (@rows) { | ||
| 248 | my $is_role = ($name->acl_type eq 'host_port'); | ||
| 249 | |||
| 250 | # a host ACL named __ANY__ replaced the shipped group with a reference to | ||
| 251 | # itself (a new ACL's first rule is group:__ANY__), and check_acl never | ||
| 252 | # returned. the ACL Manager refuses these names; this keeps out any saved | ||
| 253 | # before it did. a role of that name touches no group, so it still loads. | ||
| 254 | next if not $is_role and is_reserved_group_name($name->acl_name); | ||
| 255 | |||
| 256 | # an ACL with no rows still replaces a YAML entry of the same name, and | ||
| 257 | # matches nothing | ||
| 258 | if ($is_role) { $roles{ $name->acl_name } = {} } | ||
| 259 | else { $groups{ $name->acl_name } = [] } | ||
| 260 | |||
| 261 | foreach my $map ($name->mappings->all) { | ||
| 262 | my $left = $map->left_acl; | ||
| 263 | my $group = sprintf('synthesized_group_%s_%s', $tenant_key, $left->id); | ||
| 264 | $groups{$group} = $left->rules; | ||
| 265 | |||
| 266 | if ($is_role) { | ||
| 267 | my $right = $map->right_acl; | ||
| 268 | $roles{ $name->acl_name }->{"group:$group"} = ($right ? $right->rules : undef); | ||
| 269 | } | ||
| 270 | else { | ||
| 271 | push @{ $groups{ $name->acl_name } }, "group:$group"; | ||
| 272 | } | ||
| 273 | } | ||
| 274 | } | ||
| 275 | |||
| 276 | return { portctl_by_role => \%roles, host_groups => \%groups }; | ||
| 277 | } | ||
| 278 | |||
| 279 | 1; |