| 1 | package App::Netdisco::Util::Configuration; | ||
| 2 | |||
| 3 | use Dancer qw/:syntax :script/; | ||
| 4 | |||
| 5 | use Hash::Merge::Simple; | ||
| 6 | use MIME::Base64 'decode_base64'; | ||
| 7 | use Try::Tiny; | ||
| 8 | |||
| 9 | use base 'Exporter'; | ||
| 10 | our @EXPORT = (); | ||
| 11 | our @EXPORT_OK = qw/ | ||
| 12 | parse_params_to_config | ||
| 13 | split_params_to_config | ||
| 14 | snmp_charset_maps | ||
| 15 | /; | ||
| 16 | our %EXPORT_TAGS = (all => \@EXPORT_OK); | ||
| 17 | |||
| 18 | =head1 CONFIGURATION OVERRIDE | ||
| 19 | |||
| 20 | Users can override or add to Netdisco configuration from the command line, | ||
| 21 | or in a job specification in a configured schedule or API-submitted job. | ||
| 22 | This in turn overrides the NETDISCO_WITH_CONFIGURATION environment variable. | ||
| 23 | |||
| 24 | Configuration is provided to a Job in either the C<port> or C<subaction> | ||
| 25 | (C<extra>) slots. There is a way to provide configuration when these | ||
| 26 | slots are also used for job parameters. | ||
| 27 | |||
| 28 | Configuration can be provided directly as JSON, as simple "k1=v1,k2=v2" | ||
| 29 | format, or, when job parameters are needed, in a JSON dictionary slot | ||
| 30 | "C<with>" alongside the job parameter in a dictionary slot "C<value>". | ||
| 31 | |||
| 32 | When configuration override is provided to BOTH the C<port> and C<subaction> | ||
| 33 | (C<extra>) slots the BEHAVIOUR IS UNDEFINED. Best to avoid doing that. | ||
| 34 | |||
| 35 | Note that earlier behaviour of providing a bare string as a C<device_auth> | ||
| 36 | tag hint is now unsupported and C<device_auth_tag_hint> setting can be | ||
| 37 | used instead. | ||
| 38 | |||
| 39 | Also note that this implementation precludes providing a dictionary as | ||
| 40 | the "extra" configuration to any action (as it will be interpreted and | ||
| 41 | consumed as configuration setting overrides). Lists and strings remain | ||
| 42 | supported. | ||
| 43 | |||
| 44 | For example: | ||
| 45 | |||
| 46 | =over 4 | ||
| 47 | |||
| 48 | =item * C<undef> (changed to empty string if subaction) | ||
| 49 | |||
| 50 | =item * C<unchanged> | ||
| 51 | |||
| 52 | =item * C<"unchanged"> | ||
| 53 | |||
| 54 | =item * C<'"unchanged"'> | ||
| 55 | |||
| 56 | =item * C<[{"mac": "string", "port": "string"}]> | ||
| 57 | |||
| 58 | =item * C<{"mac": "string", "port": "string"}> (unsupported) | ||
| 59 | |||
| 60 | =item * C<{"value": [{"mac": "string", "port": "string"}]}> | ||
| 61 | |||
| 62 | =item * C<{"value": {"mac": "string", "port": "string"}}> | ||
| 63 | |||
| 64 | =item * C<{"value": '{"mac": "string", "port": "string"}'}> | ||
| 65 | |||
| 66 | =item * C<{"value": "unchanged", "with": {"snmptimeout": 5000000}}> | ||
| 67 | |||
| 68 | =item * C<{"value": "unchanged", "with": '{"snmptimeout": 5000000}'}> | ||
| 69 | |||
| 70 | =item * C<{"value": "unchanged", "with": 'snmptimeout=5000000'}> | ||
| 71 | |||
| 72 | =item * C<{"snmptimeout": 5000000}> | ||
| 73 | |||
| 74 | =item * C<'{"snmptimeout": 5000000}'> | ||
| 75 | |||
| 76 | =item * C<snmptimeout=5000000> | ||
| 77 | |||
| 78 | =item * C<{"value": "unchanged", "with": "FAILS"}> (unsupported) | ||
| 79 | |||
| 80 | =item * C<{"value": "unchanged", "with": ["FAILS"]}> (unsupported) | ||
| 81 | |||
| 82 | =back | ||
| 83 | |||
| 84 | =head1 snmp_charset_maps( $raw ) | ||
| 85 | |||
| 86 | Returns the C<snmp_charset> setting as a list of maps from ACL to a list of | ||
| 87 | charset names, from any of the forms it may be written in: a charset name, a | ||
| 88 | map whose values are a name or a list, or a list of those maps. Undef, | ||
| 89 | references and empty names are dropped. Returns C<< [ { 'group:__ANY__' => [] } ] >> | ||
| 90 | when nothing is left. | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | sub snmp_charset_maps { | ||
| 95 | my $raw = shift; | ||
| 96 | my @maps = (ref [] eq ref $raw) ? @$raw : ($raw); | ||
| 97 | my @normalized; | ||
| 98 | |||
| 99 | foreach my $map (@maps) { | ||
| 100 | $map = { 'group:__ANY__' => $map } if defined $map and q{} eq ref $map; | ||
| 101 | next unless ref {} eq ref $map; | ||
| 102 | push @normalized, { map { ($_ => _charset_list($map->{$_})) } keys %$map }; | ||
| 103 | } | ||
| 104 | |||
| 105 | return scalar @normalized ? \@normalized : [ { 'group:__ANY__' => [] } ]; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub _charset_list { | ||
| 109 | my ($value) = @_; | ||
| 110 | my @names = (ref [] eq ref $value) ? @$value : ($value); | ||
| 111 | return [ grep { defined and q{} eq ref $_ and length } @names ]; | ||
| 112 | } | ||
| 113 | |||
| 114 | =head1 parse_params_to_config | ||
| 115 | |||
| 116 | Takes a defined value, works out what has been provided. If there is | ||
| 117 | configuration to override it applies that. If there is a residual value | ||
| 118 | to return, it returns that, otherwise returns undef. | ||
| 119 | |||
| 120 | =cut | ||
| 121 | |||
| 122 | sub parse_params_to_config { | ||
| 123 | my ($actual_value, $overrides) = split_params_to_config(shift); | ||
| 124 | merge_into_configuration($overrides) if $overrides; | ||
| 125 | return $actual_value; | ||
| 126 | } | ||
| 127 | |||
| 128 | =head1 split_params_to_config | ||
| 129 | |||
| 130 | Works out what has been provided as C<parse_params_to_config> does, but | ||
| 131 | applies nothing. Returns the residual value and a hashref of the | ||
| 132 | configuration overrides, or undef when there are none. | ||
| 133 | |||
| 134 | =cut | ||
| 135 | |||
| 136 | sub split_params_to_config { | ||
| 137 | my $orig_value = shift; | ||
| 138 | return (undef, undef) unless defined $orig_value; | ||
| 139 | |||
| 140 | # value via "schedule:" deployment.yml would already be a Perl struct | ||
| 141 | my $struct = (ref $orig_value ne q{} or $orig_value =~ m/^[e.0-9]+$/) | ||
| 142 | ? $orig_value | ||
| 143 | : try { from_json($orig_value) }; | ||
| 144 | # reminder: from_json of a "" string returns the string, | ||
| 145 | # unquoted string throws error, and unquoted number returns a number. | ||
| 146 | # so struct could still be a string, or struct reference, or undef on parse error | ||
| 147 | my $came_from_json = (((defined $struct) | ||
| 148 | and (ref $orig_value eq q{}) and ($struct ne $orig_value)) ? true : false); | ||
| 149 | |||
| 150 | # case when value is a struct but not config (hashref), just leave it alone | ||
| 151 | if ((ref $struct ne q{}) and (ref $struct ne ref {})) { | ||
| 152 | return ($orig_value, undef); | ||
| 153 | } | ||
| 154 | |||
| 155 | # case when value is an empty string | ||
| 156 | if (($orig_value eq q{}) or (defined $struct and $struct eq q{})) { | ||
| 157 | return (q{}, undef); | ||
| 158 | } | ||
| 159 | |||
| 160 | # finally, we have either a lengthy string or a struct | ||
| 161 | my $value = ((defined $struct) | ||
| 162 | ? $struct | ||
| 163 | : $orig_value); | ||
| 164 | |||
| 165 | # if a lengthy string, it could be k=v config | ||
| 166 | if (ref $value eq q{}) { | ||
| 167 | if ($value =~ m/^(?:(?:[^=,]+)=(?:[^=,]+))(?:,(?:[^=,]+)=(?:[^=,]+))*$/) { | ||
| 168 | $value = parse_config_string_to_dict($value); | ||
| 169 | |||
| 170 | } | ||
| 171 | else { | ||
| 172 | # try to decode base64 | ||
| 173 | my $decoded = try { from_json(decode_base64($value)) }; # might explode | ||
| 174 | if (defined $decoded and ref {} eq ref $decoded) { | ||
| 175 | return split_params_to_config($decoded); | ||
| 176 | } | ||
| 177 | # some other use of subaction (file ref, log comment, etc) | ||
| 178 | else { | ||
| 179 | return ($value, undef); | ||
| 180 | } | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | # now value is a hashref, look for with/value setup | ||
| 185 | my $actual_value = undef; | ||
| 186 | |||
| 187 | if (exists $value->{value}) { | ||
| 188 | # if JSON was thawed from the value, refreeze it | ||
| 189 | my $inner = delete $value->{value}; | ||
| 190 | $actual_value = (((ref $inner ne q{}) and $came_from_json) | ||
| 191 | ? to_json($inner) : $inner); | ||
| 192 | } | ||
| 193 | |||
| 194 | $value = $value->{'with'} if exists $value->{'with'}; | ||
| 195 | # we can recurse to decode a stringified JSON 'with' | ||
| 196 | my $overrides = ((ref $value eq ref {}) | ||
| 197 | ? $value : (split_params_to_config($value))[1]); | ||
| 198 | |||
| 199 | return ($actual_value, | ||
| 200 | ((ref $overrides eq ref {} and scalar keys %$overrides) ? $overrides : undef)); | ||
| 201 | } | ||
| 202 | |||
| 203 | sub parse_config_string_to_dict { | ||
| 204 | my $extra = shift; | ||
| 205 | return {} unless | ||
| 206 | $extra and (ref $extra eq q{}) and $extra =~ m/=/; | ||
| 207 | |||
| 208 | # must be key1=val1,key2=val2 | ||
| 209 | my $dict = {}; | ||
| 210 | my @kvs = split m/,/, $extra; | ||
| 211 | foreach my $kv (@kvs) { | ||
| 212 | next unless $kv; | ||
| 213 | die "bad syntax for subaction, missing =\n" unless $kv =~ m/=/; | ||
| 214 | my ($k, $v) = split m/=/, $kv, 2; | ||
| 215 | $dict->{$k} = $v; | ||
| 216 | } | ||
| 217 | |||
| 218 | return $dict; | ||
| 219 | } | ||
| 220 | |||
| 221 | sub merge_into_configuration { | ||
| 222 | my $newconfig = shift; | ||
| 223 | die "bad configuration format\n" unless ref $newconfig eq ref {}; | ||
| 224 | my $SETTINGS = config(); | ||
| 225 | $SETTINGS = Hash::Merge::Simple::merge( $SETTINGS, $newconfig ); | ||
| 226 | set($_ => $SETTINGS->{$_}) for keys %$newconfig; | ||
| 227 | } | ||
| 228 | |||
| 229 | true; |