blib/lib/AnyEvent/MCP/Config.pm

filter: Statementstmt 53.0% 209 / 394 Branchbran 42.8% 84 / 196 Conditioncond 30.5% 63 / 206 MC/DCmcdc 13.7% 14 / 102 Subroutinesub 56.8% 29 / 51 Podpod 100% 40 / 40 totaltotal 44.3% 439 / 989 CC 185 SCAR 32.5
CC
185
Cov
45%
http_settings
90.3
http_settings
90.3
serialize_ini
28.4
CRAP 25.8
« blib/lib/AnyEvent/MCP/Extension.pm ↑ Summary bin/mcp-http »
1 =head1 NAME
2  
3 AnyEvent::MCP::Config - named servers and derived MCP credentials
4  
5 =head1 SYNOPSIS
6  
7 use AnyEvent::MCP::Config;
8  
9 my $config = AnyEvent::MCP::Config::global_config;
10 my $token = $config->user_credentials ("agni")->[0][2];
11 my $url = $config->http_url ("agni", undef, path => "/");
12  
13 =head1 DESCRIPTION
14  
15 An MCP server and its clients often need matching credentials. Rather than
16 maintaining a separate password for each server and user, this module can
17 derive them from one private master secret. Users are defined by
18 C<user.NAME = GENERATION> in C<[global]>, C<[program PROGRAM]> or
19 C<[instance NAME]>, in that order of precedence. An absent global
20 C<user.default> is implicitly enabled at generation 1.
21  
22 The shared file format is described in L<AnyEvent::MCP/GLOBAL CONFIGURATION>.
23 Loading this module does not read any files. C<global_config> loads the
24 configuration, ensures a persistent master secret exists and caches the
25 initialized object for the process.
26  
27 A missing secret is generated using C<AnyEvent::MCP::random_bytes> and
28 appended under a file lock. Failure to save the secret is fatal, rather
29 than using a temporary secret that other processes cannot reproduce.
30 A supplied invalid secret is an error. Changing or losing the master
31 secret invalidates all credentials derived from it.
32  
33 Derivation uses C<derive_secret> with the random master key.
34 The application namespace, purpose, program, instance name and username are
35 separately length-prefixed, so distinct contexts derive different credentials.
36 Deriving a password does not create an account or configure the server to
37 accept it: authentication still belongs to the server.
38  
39 =head1 METHODS
40  
41 =over
42  
43 =item $config = AnyEvent::MCP::Config::global_config (file => $file)
44  
45 Return the process-global configuration object, loaded and initialized.
46 The first call accepts the same arguments as C<new>, and ensures that the
47 master secret is persistently stored. Later calls return the same object
48 without rereading or reinitializing it.
49  
50 =item $config = AnyEvent::MCP::Config->new (file => $file)
51  
52 Construct an independent configuration object without reading or writing
53 the file. C<$file> defaults to the global configuration path.
54  
55 =item $config->load
56  
57 Reload the file, replacing the object's sections. A missing file yields an
58 empty configuration; other read and parsing errors are fatal. Sections are
59 available as C<< $config->{section}{$section} >>, each a hash reference of
60 string values. Named servers use C<[instance NAME]>. An optional raw file
61 handle can be supplied when the caller already holds the file lock.
62  
63 =item $config->save
64  
65 Write the loaded sections back as canonical UTF-8 INI text, sorting
66 sections and keys. Comments and original formatting are not retained.
67 The caller must hold C<AnyEvent::MCP::file_lock> through the update.
68 Returns the configuration object.
69  
70 =item $config->ensure_secret
71  
72 Ensure that C<[global]> contains a valid persistent master secret. If
73 missing, lock and reread the file, generate a secret only if still absent,
74 and append it without changing other settings. Fail if it cannot be saved
75 or is invalid. Returns the configuration object.
76  
77 =item $bytes = $config->derive_secret ($length, @info)
78  
79 =item $bytes = AnyEvent::MCP::Config->derive_secret ($length, @info)
80  
81 Derive C<$length> secret bytes from the configuration's master secret and
82 an ordered list of context byte strings C<@info>. The class form uses
83 C<global_config>, loading the default configuration on first use and reusing it
84 thereafter. The object form uses that object's configuration, including
85 one loaded from an explicit file.
86  
87 The same master secret and context always produce the same bytes. Use a
88 distinctive application/purpose string as the first context field, followed
89 by names or other values identifying the result:
90  
91 my $secret = AnyEvent::MCP::Config->derive_secret (
92 32, "my application token v1", AnyEvent::MCP::utf8enc ($name)
93 );
94  
95 The result contains raw bytes; encode it yourself if a printable token is
96 needed. This method does not add the result to a server's accepted tokens.
97  
98 The fixed C<anyevent-mcp> context field is prepended automatically.
99 Context fields are separately length-prefixed, so C<("ab", "c")> and
100 C<("a", "bc")> have different contexts. Encode character strings explicitly,
101 for example with C<AnyEvent::MCP::utf8enc>. Context arguments are left
102 unchanged.
103  
104 C<$length> must be an integer in 0..16320; zero returns an empty string.
105 Shorter results are prefixes of longer results for the same master secret
106 and context. Use distinct context fields for different purposes, rather
107 than relying on different lengths to separate them.
108  
109 Derivation uses HKDF-SHA512 expansion, with each context field prefixed by
110 its four-byte big-endian byte length. Changing or losing the master secret
111 changes every result derived from it.
112  
113 =item $credential = $config->credential ($purpose, $name, $username)
114  
115 Derives a 32-byte credential, returned as unpadded URL-safe Base64.
116 C<$purpose> is C<token>. C<$name> is the instance name, and C<$username>
117 is optional. This is the older unmanaged derivation API; its result is not
118 automatically accepted by a server. Managed credentials are enumerated by
119 C<user_credentials>.
120  
121 Credentials use the context C<("anyevent-mcp", "token", PROGRAM, NAME, USER)>.
122 PROGRAM is the invoked basename, with a trailing C<.pl> or C<.perl> removed.
123 Changing the program or instance name changes the credential.
124 A credential derived with C<$username> is B<not> automatically accepted
125 by the server. This method neither requires a server section nor configures
126 authentication.
127  
128 =item $users = $config->users ($instance, $program)
129  
130 Return effective user definitions keyed by username, each with C<scope>
131 (global, program or instance) and signed C<generation>. Inheritance is
132 global, program, instance; a negative generation disables without fallback.
133  
134 =item $credentials = $config->user_credentials ($instance, $program)
135  
136 Return enabled users as C<[[USERNAME, PRINCIPAL, TOKEN]...]> with default
137 first. The principal is C<global:NAME>, C<program:NAME>, or C<instance:NAME>.
138 HKDF context includes program, instance, scope, user and generation. Rotating
139 a generation changes the token but preserves saved user permissions.
140  
141 =item $user = $config->connect_user ($instance, $program)
142  
143 Return the username allowed to register private Vim, browser WebSocket, or
144 multibroker reverse-MCP connections. C<connect_user> inherits through the
145 usual scopes and defaults to C<default>. Revoking that user disables these
146 connections until its generation is renewed or another user is selected.
147  
148 =item $program = AnyEvent::MCP::Config::program_name ()
149  
150 Returns the invoked program basename, stripping C<.pl> or C<.perl>.
151 An inline Perl invocation uses C<perl>. This identity also selects the
152 C<[program PROGRAM]> configuration section; it is independent of routing aliases.
153  
154 =item $user = AnyEvent::MCP::Config::user_name ($user)
155  
156 Validate a managed username (ASCII letter or underscore, followed by
157 letters, digits, underscores or hyphens). Instance names are validated by
158 L<AnyEvent::MCP/SMALL UTILITIES>.
159  
160 =item @options = AnyEvent::MCP::Config::http_getopt (\$name, \$bind, \$print_url, \@tokens)
161  
162 Return standard C<Getopt::Long> options referring to the caller's scalars.
163  
164 =item $port = $config->server_port ($name)
165  
166 Derive a stable default port in 16384..32767 from the master secret and
167 program basename and service-instance name. Collisions produce ordinary listener bind errors.
168  
169 =item %settings = $config->http_settings ($name, $bind)
170  
171 Resolve HTTP settings from C<[instance NAME]> and an optional command-line
172 C<bind> override. The default is IPv4 loopback and the name-derived port.
173 The bind setting accepts C<ADDRESS:PORT>, C<[IPv6]:PORT>, C<ADDRESS>,
174 and C<:PORT> (which keeps the default loopback address).
175 Hostnames are resolved once when settings are obtained; their original
176 spelling is advertised unless C<url_host> overrides it. The returned options
177 include C<name>, C<host>, C<port> and C<tokens>, plus configured C<path>,
178 C<url_host> and C<url_scheme>. This does not start a listener.
179  
180 =item @rules = $config->auto_rules ($name)
181  
182 Return ordered automatic permission rules from C<[program PROGRAM]> and
183 C<[instance NAME]>. C<auto-rule.SUFFIX = allow|deny TOOL[,ACTION[,CAPABILITY]]>
184 entries are sorted by key. The suffix is an arbitrary ordering label; instance
185 entries override matching program entries, and an empty value disables an
186 inherited entry. Command-line rules take precedence over these defaults.
187  
188 =item $url = $config->http_url ($name, $bind, %options)
189  
190 Format a URL containing the first accepted token, without listening.
191 Port zero cannot be used because the kernel has not yet selected a port.
192 When the token is derived, the master secret must be persistently stored,
193 so the next process derives the same URL. Explicit static tokens and
194 C<auth =E<gt> "none"> do not require that persistence check.
195  
196 =back
197  
198 =head1 INI FUNCTIONS
199  
200 These functions work with Unicode character strings, not octets. Use
201 L<AnyEvent::MCP/file_load> and L<AnyEvent::MCP/file_save> for UTF-8
202 file I/O. Binary values belong in Base64 or hexadecimal form.
203  
204 =over
205  
206 =item $sections = AnyEvent::MCP::Config::parse_ini ($text, $filename)
207  
208 Parse Unicode INI text into C<[[NAME, {KEY =E<gt> VALUE}], ...]>.
209 Sections retain file order, including repeated section names and the
210 initial unnamed section C<"">. Duplicate keys within one section use
211 the final value. C<$filename> is optional, for line-numbered errors.
212  
213 =item $merged = AnyEvent::MCP::Config::merge_sections ($sections, $default)
214  
215 Return a hash mapping section names to key/value hashes, with later
216 sections overriding earlier ones. When C<$default> is given, unnamed
217 sections use that name (C<global> for global configuration and
218 C<account> for IMAP accounts). Use the original ordered list instead
219 for files where repeated sections are meaningful, such as permission rules.
220  
221 =item $text = AnyEvent::MCP::Config::serialize_ini ($sections)
222  
223 Serialize an ordered section list to canonical INI text. Section order
224 is preserved; keys within each section are sorted. Values must be
225 single-line character strings. Comments and whitespace formatting
226 are deliberately not preserved on rewrite.
227  
228 =back
229  
230 =cut
231  
232 package AnyEvent::MCP::Config;
233  
234 102 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
235  
236 102 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
237 102 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
238 102 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
239 102 ▶use Errno qw(ENOENT);
Subroutine BEGIN: called CC=1 SCAR=0.0
240  
241 102 ▶use AnyEvent::Socket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
242 102 ▶use AnyEvent::MCP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
243  
244 102 our @ISA = qw(AnyEvent::MCP::Base);
245  
246 383 ▶sub parse_ini {
Subroutine parse_ini: called CC=6 SCAR=23.5
Pod documented
247 0 ▶ my ($text, $file) = @_;
Subroutine parse_ini: not called CC=6 SCAR=23.5
Pod documented
248 0 ▶ $file //= "INI";
Condition50%2/4
$file //= "INI"
l!l
3794
$file //= "INI"
l!l
00
MC/DC100%1/1
$file //= "INI"
$file
MC/DC0%0/1
$file //= "INI"
$file
Truth table100%2/2
A
A= $file
Aresult
11
00
Truth table0%0/2
A
A= $file
Aresult
11
00
249 0 my @sections = (["", {}]);
250 0 my $line = 0;
251  
252 0 for (split /\n/, $text) {
253 0 ++$line;
254 0 s/\r\z//;
255 0 ▶ next if /\A\s*(?:[#;]|\z)/;
Branch: 2 branches
BranchTrueFalseTotal
if /\A\s*(?:[#;]|\z)/u 332 2426 2758
if /\A\s*(?:[#;]|\z)/u 0 0 0
256 0 ▶ if (/\A\s*\[([^\]\r\n]+)\]\s*\z/) {
Branch: 4 branches
BranchTrueFalseTotal
if (/\A\s*\[([^\]\r\n]+)\]\s*\z/u) { } 641 1785 2426
elsif (/\A\s*([^=\s]+)\s*=\s*(.*?)\s*\z/u) { } 1784 1 1785
if (/\A\s*\[([^\]\r\n]+)\]\s*\z/u) { } 0 0 0
elsif (/\A\s*([^=\s]+)\s*=\s*(.*?)\s*\z/u) { } 0 0 0
257 0 push @sections, [$1, {}];
258 } elsif (/\A\s*([^=\s]+)\s*=\s*(.*?)\s*\z/) {
259 0 $sections[-1][1]{$1} = $2;
260 } else {
261 0 die "$file:$line: invalid INI line\n";
262 }
263 }
264  
265 \@sections
266 0 }
267  
268 121 ▶sub merge_sections {
Subroutine merge_sections: called CC=5 SCAR=21.7
Pod documented
269 0 ▶ my ($sections, $default) = @_;
Subroutine merge_sections: not called CC=5 SCAR=21.7
Pod documented
270 0 my %merged;
271  
272 0 for (@$sections) {
273 0 my ($name, $kv) = @$_;
274 0 ▶ $name = $default if defined $default && $name eq "";
Branch: 2 branches
BranchTrueFalseTotal
if defined $default and $name eq "" 121 228 349
if defined $default and $name eq "" 0 0 0
Condition33%2/6
defined $default and $name eq ""
!ll&&!rl&&r
0228121
defined $default and $name eq ""
!ll&&!rl&&r
000
MC/DC50%1/2
defined $default and $name eq ""
defined $default $name eq ""
MC/DC0%0/2
defined $default and $name eq ""
defined $default $name eq ""
Truth table66%2/3
A and B
A= defined $defaultB= $name eq ""
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $defaultB= $name eq ""
ABresult
0X0
100
111
275 0 ▶ my $values = $merged{$name} ||= {};
Condition50%2/4
$merged{$name} ||= {}
l!l
119230
$merged{$name} ||= {}
l!l
00
MC/DC100%1/1
$merged{$name} ||= {}
$merged{$name}
MC/DC0%0/1
$merged{$name} ||= {}
$merged{$name}
Truth table100%2/2
A
A= $merged{$name}
Aresult
11
00
Truth table0%0/2
A
A= $merged{$name}
Aresult
11
00
276 0 %$values = (%$values, %$kv);
277 }
278  
279 0 \%merged
280 }
281  
282 73 ▶sub serialize_ini {
Subroutine serialize_ini: called CC=9 SCAR=33.5
Pod documented
283 0 ▶ my ($sections) = @_;
Subroutine serialize_ini: not called CC=9 SCAR=33.5
Pod documented
284  
285 join "", map {
286 0 my ($name, $values) = @$_;
287 0 ▶ $name !~ /[\]\r\n]/ or die "Invalid INI section name\n";
Branch: 2 branches
BranchTrueFalseTotal
$name =~ /[\]\r\n]/u and die "Invalid INI section name\n" 0 92 92
$name =~ /[\]\r\n]/u and die "Invalid INI section name\n" 0 0 0
288 (length $name ? "[$name]\n" : "")
289 . (join "", map {
290 0 ▶ length && !/[=\s]/ && !/\A[#;]/
Branch: 4 branches
BranchTrueFalseTotal
length $name ? : 91 1 92
length $_ and not /[=\s]/u and not /\A[#;]/u or die "Invalid INI key\n" 340 1 341
length $name ? : 0 0 0
length $_ and not /[=\s]/u and not /\A[#;]/u or die "Invalid INI key\n" 0 0 0
Condition25%3/12
length $_ and not /[=\s]/u and not /\A[#;]/u
!ll&&!rl&&r
01340
length $_ and not /[=\s]/u
!ll&&!rl&&r
00341
length $_ and not /[=\s]/u and not /\A[#;]/u
!ll&&!rl&&r
000
length $_ and not /[=\s]/u
!ll&&!rl&&r
000
MC/DC33%1/3
length $_ and not /[=\s]/u and not /\A[#;]/u
length $_ not /[=\s]/u not /\A[#;]/u
MC/DC0%0/3
length $_ and not /[=\s]/u and not /\A[#;]/u
length $_ not /[=\s]/u not /\A[#;]/u
Truth table50%2/4
A and not(B) and not(C)
A= length $_B= not /[=\s]/uC= not /\A[#;]/u
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and not(B) and not(C)
A= length $_B= not /[=\s]/uC= not /\A[#;]/u
ABCresult
0XX0
10X0
1100
1111
291 or die "Invalid INI key\n";
292 0 my $value = $values->{$_};
293 0 ▶ defined $value && !ref $value && $value !~ /[\r\n]/
Branch: 2 branches
BranchTrueFalseTotal
defined $value and not ref $value and not $value =~ /[\r\n]/u or die "Invalid INI value for $_\n" 339 1 340
defined $value and not ref $value and not $value =~ /[\r\n]/u or die "Invalid INI value for $_\n" 0 0 0
Condition25%3/12
defined $value and not ref $value and not $value =~ /[\r\n]/u
!ll&&!rl&&r
01339
defined $value and not ref $value
!ll&&!rl&&r
00340
defined $value and not ref $value and not $value =~ /[\r\n]/u
!ll&&!rl&&r
000
defined $value and not ref $value
!ll&&!rl&&r
000
MC/DC33%1/3
defined $value and not ref $value and not $value =~ /[\r\n]/u
defined $value not ref $value not $value =~ /[\r\n]/u
MC/DC0%0/3
defined $value and not ref $value and not $value =~ /[\r\n]/u
defined $value not ref $value not $value =~ /[\r\n]/u
Truth table50%2/4
A and not(B) and not(C)
A= defined $valueB= not ref $valueC= not $value =~ /[\r\n]/u
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and not(B) and not(C)
A= defined $valueB= not ref $valueC= not $value =~ /[\r\n]/u
ABCresult
0XX0
10X0
1100
1111
294 or die "Invalid INI value for $_\n";
295 0 "$_ = $value\n"
296 } sort keys %$values)
297 . "\n"
298 } @$sections
299 }
300  
301 109 ▶sub _file {
Subroutine _file: called CC=3 SCAR=15.7
302 my $dir = $ENV{XDG_CONFIG_HOME}
303 0 ▶ || ($ENV{HOME} || die "HOME is not set\n") . "/.config";
Subroutine _file: not called CC=3 SCAR=15.7
Condition37%3/8
$ENV{'XDG_CONFIG_HOME'} || ($ENV{'HOME'} || die("HOME is not set\n")) . '/.config'
l!l
1036
$ENV{'HOME'} || die("HOME is not set\n")
l!l
60
$ENV{'XDG_CONFIG_HOME'} || ($ENV{'HOME'} || die("HOME is not set\n")) . '/.config'
l!l
00
$ENV{'HOME'} || die("HOME is not set\n")
l!l
00
MC/DC100%1/1
$ENV{'XDG_CONFIG_HOME'} || ($ENV{'HOME'} || die("HOME is not set\n")) . '/.config'
$ENV{'XDG_CONFIG_HOME'}
MC/DC0%0/1
$ENV{'XDG_CONFIG_HOME'} || ($ENV{'HOME'} || die("HOME is not set\n")) . '/.config'
$ENV{'XDG_CONFIG_HOME'}
Truth table100%2/2
A
A= $ENV{'XDG_CONFIG_HOME'}
Aresult
11
00
Truth table0%0/2
A
A= $ENV{'XDG_CONFIG_HOME'}
Aresult
11
00
304  
305 0 "$dir/anyevent-mcp/global.ini"
306 }
307  
308 102 my $global;
309  
310 83 ▶sub global_config {
Subroutine global_config: called CC=2 SCAR=10.9
Pod documented
311 0 ▶ $global //= __PACKAGE__->new (@_)->load->ensure_secret
Subroutine global_config: not called CC=2 SCAR=10.9
Pod documented
Condition33%2/6
$global //= __PACKAGE__->new(@_)->load->ensure_secret
l!l&&r!l&&!r
28550
$global //= __PACKAGE__->new(@_)->load->ensure_secret
l!l&&r!l&&!r
000
MC/DC0%0/2
$global //= __PACKAGE__->new(@_)->load->ensure_secret
$global __PACKAGE__->new(@_)->load->ensure_secret
MC/DC0%0/2
$global //= __PACKAGE__->new(@_)->load->ensure_secret
$global __PACKAGE__->new(@_)->load->ensure_secret
Truth table66%2/3
A //= B
A= $globalB= __PACKAGE__->new(@_)->load->ensure_secret
ABresult
1X1
011
000
Truth table0%0/3
A //= B
A= $globalB= __PACKAGE__->new(@_)->load->ensure_secret
ABresult
1X1
011
000
312 }
313  
314 113 ▶sub new {
Subroutine new: called CC=2 SCAR=9.2
Pod documented
315 0 ▶ my ($class, %kv) = @_;
Subroutine new: not called CC=2 SCAR=9.2
Pod documented
316  
317 0 my $self = $class->SUPER::new (section => {}, %kv);
318 0 ▶ $self->{file} = _file unless exists $self->{file};
Branch: 2 branches
BranchTrueFalseTotal
unless exists $self->{"file"} 53 60 113
unless exists $self->{"file"} 0 0 0
319 0 $self
320 }
321  
322 130 ▶sub load {
Subroutine load: called CC=7 SCAR=28.1
Pod documented
323 0 ▶ my ($self, $fh) = @_;
Subroutine load: not called CC=7 SCAR=28.1
Pod documented
324 0 $self->{section} = {};
325  
326 0 ▶ unless ($fh) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($fh) 101 29 130
unless ($fh) 0 0 0
327 0 ▶ open $fh, "<:raw", $self->{file} or do {
Branch: 2 branches
BranchTrueFalseTotal
open $fh, "<:raw", $self->{"file"} or do { die qq[$self->{"file"}: $!\n] unless $! == 2; return $self } 86 15 101
open $fh, "<:raw", $self->{"file"} or do { die qq[$self->{"file"}: $!\n] unless $! == 2; return $self } 0 0 0
328 0 ▶ die "$self->{file}: $!\n" unless $! == ENOENT;
Branch: 2 branches
BranchTrueFalseTotal
unless $! == 2 0 15 15
unless $! == 2 0 0 0
329 0 return $self
330 };
331 }
332  
333 0 ▶ seek $fh, 0, 0 or die "$self->{file}: $!\n";
Branch: 2 branches
BranchTrueFalseTotal
seek $fh, 0, 0 or die qq[$self->{"file"}: $!\n] 115 0 115
seek $fh, 0, 0 or die qq[$self->{"file"}: $!\n] 0 0 0
334 0 $self->{section} = merge_sections (parse_ini (AnyEvent::MCP::file_load ($self->{file}, $fh), $self->{file}), "global");
335 0 ▶ close $fh or die "$self->{file}: $!\n" unless @_ > 1;
Branch: 2 branches
BranchTrueFalseTotal
unless @_ > 1 86 29 115
unless @_ > 1 0 0 0
Condition30%3/10
@_ > 1 || (close $fh || die("$self->{'file'}: $!\n"))
l!l&&r!l&&!r
29860
close $fh or die qq[$self->{"file"}: $!\n]
l!l
860
@_ > 1 || (close $fh || die("$self->{'file'}: $!\n"))
l!l&&r!l&&!r
000
close $fh or die qq[$self->{"file"}: $!\n]
l!l
00
MC/DC0%0/2
@_ > 1 || (close $fh || die("$self->{'file'}: $!\n"))
@_ > 1 close $fh
MC/DC0%0/2
@_ > 1 || (close $fh || die("$self->{'file'}: $!\n"))
@_ > 1 close $fh
Truth table66%2/3
A || B
A= @_ > 1B= close $fh
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= @_ > 1B= close $fh
ABresult
1X1
011
000
336  
337 0 $self
338 }
339  
340 9 ▶sub save {
Subroutine save: called CC=1 SCAR=1.2
Pod documented
341 0 ▶ my ($self) = @_;
Subroutine save: not called CC=1 SCAR=1.2
Pod documented
342  
343 AnyEvent::MCP::file_save $self->{file},
344 0 serialize_ini [map [$_, $self->{section}{$_}], sort keys %{$self->{section}}];
345 0 $self
346 }
347  
348 76 ▶sub ensure_secret {
Subroutine ensure_secret: called CC=8 SCAR=29.9
Pod documented
349 0 ▶ my ($self) = @_;
Subroutine ensure_secret: not called CC=8 SCAR=29.9
Pod documented
350  
351 0 ▶ unless (exists $self->{section}{global}{secret}) {
Branch: 2 branches
BranchTrueFalseTotal
unless (exists $self->{"section"}{"global"}{"secret"}) 19 57 76
unless (exists $self->{"section"}{"global"}{"secret"}) 0 0 0
352 0 my $lock = AnyEvent::MCP::file_lock $self->{file};
353 0 $self->load ($lock);
354  
355 0 ▶ unless (exists $self->{section}{global}{secret}) {
Branch: 2 branches
BranchTrueFalseTotal
unless (exists $self->{"section"}{"global"}{"secret"}) 18 0 18
unless (exists $self->{"section"}{"global"}{"secret"}) 0 0 0
356 0 my $secret = eval { unpack "H*", AnyEvent::MCP::random_bytes 64 };
357 0 ▶ die "$@Supply a securely generated 128-digit hexadecimal secret in $self->{file}\n"
Branch: 2 branches
BranchTrueFalseTotal
if $@ 1 17 18
if $@ 0 0 0
358 if $@;
359 0 ▶ seek $lock, 0, 2 or die "$self->{file}: $!\n";
Branch: 2 branches
BranchTrueFalseTotal
seek $lock, 0, 2 or die qq[$self->{"file"}: $!\n] 17 0 17
seek $lock, 0, 2 or die qq[$self->{"file"}: $!\n] 0 0 0
360 0 ▶ print {$lock} "\n[global]\nsecret = $secret\n" or die "$self->{file}: $!\n";
Branch: 2 branches
BranchTrueFalseTotal
print {$lock;} "\n[global]\nsecret = $secret\n" or die qq[$self->{"file"}: $!\n] 17 0 17
print {$lock;} "\n[global]\nsecret = $secret\n" or die qq[$self->{"file"}: $!\n] 0 0 0
361 0 $self->{section}{global}{secret} = $secret;
362 }
363  
364 0 ▶ close $lock or die "$self->{file}: $!\n";
Branch: 2 branches
BranchTrueFalseTotal
close $lock or die qq[$self->{"file"}: $!\n] 17 0 17
close $lock or die qq[$self->{"file"}: $!\n] 0 0 0
365 }
366  
367 0 ▶ $self->{section}{global}{secret} =~ /\A[0-9a-fA-F]{128}\z/
Branch: 2 branches
BranchTrueFalseTotal
$self->{"section"}{"global"}{"secret"} =~ /\A[0-9a-fA-F]{128}\z/u or die qq[$self->{"file"}: invalid secret; supply 128 hexadecimal digits\n] 73 1 74
$self->{"section"}{"global"}{"secret"} =~ /\A[0-9a-fA-F]{128}\z/u or die qq[$self->{"file"}: invalid secret; supply 128 hexadecimal digits\n] 0 0 0
368 or die "$self->{file}: invalid secret; supply 128 hexadecimal digits\n";
369 0 $self
370 }
371  
372 145 ▶sub _prk_derive_secret($$@) {
Subroutine _prk_derive_secret: called CC=5 SCAR=21.5
373 0 ▶ my ($prk, $length, @info) = @_;
Subroutine _prk_derive_secret: not called CC=5 SCAR=21.5
374  
375 0 ▶ 64 == length $prk
Branch: 2 branches
BranchTrueFalseTotal
64 == length $prk or die "derive_secret: key length must be 64 octets\n" 145 0 145
64 == length $prk or die "derive_secret: key length must be 64 octets\n" 0 0 0
376 or die "derive_secret: key length must be 64 octets\n";
377 0 ▶ $length =~ /\A[0-9]+\z/ && $length <= 255 * 64
Branch: 2 branches
BranchTrueFalseTotal
$length =~ /\A[0-9]+\z/u and $length <= 16320 or die "derive_secret: length must be an integer in 0..16320\n" 142 3 145
$length =~ /\A[0-9]+\z/u and $length <= 16320 or die "derive_secret: length must be an integer in 0..16320\n" 0 0 0
Condition50%3/6
$length =~ /\A[0-9]+\z/u and $length <= 16320
!ll&&!rl&&r
21142
$length =~ /\A[0-9]+\z/u and $length <= 16320
!ll&&!rl&&r
000
MC/DC100%2/2
$length =~ /\A[0-9]+\z/u and $length <= 16320
$length =~ /\A[0-9]+\z/u $length <= 16320
MC/DC0%0/2
$length =~ /\A[0-9]+\z/u and $length <= 16320
$length =~ /\A[0-9]+\z/u $length <= 16320
Truth table100%3/3
A and B
A= $length =~ /\A[0-9]+\z/uB= $length <= 16320
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $length =~ /\A[0-9]+\z/uB= $length <= 16320
ABresult
0X0
100
111
378 or die "derive_secret: length must be an integer in 0..16320\n";
379  
380 0 my $info = pack "(N/a*)*", "anyevent-mcp", @info;
381 0 my $t = "";
382 0 my $okm = "";
383 0 my $i = 0;
384  
385 0 ▶ while ($length > length $okm) {
Branch: 2 branches
BranchTrueFalseTotal
if ($length > length $okm) 397 142 539
if ($length > length $okm) 0 0 0
386 0 $t = Digest::SHA::hmac_sha512 $t . $info . chr ++$i, $prk;
387 0 $okm .= $t;
388 }
389  
390 0 substr $okm, 0, $length
391 }
392  
393 146 ▶sub derive_secret {
Subroutine derive_secret: called CC=3 SCAR=14.2
Pod documented
394 0 ▶ my ($self, $length, @info) = @_;
Subroutine derive_secret: not called CC=3 SCAR=14.2
Pod documented
395  
396 0 ▶ $self = global_config () unless ref $self;
Branch: 2 branches
BranchTrueFalseTotal
unless ref $self 2 144 146
unless ref $self 0 0 0
397  
398 0 my $secret = $self->{section}{global}{secret};
399 0 ▶ $secret =~ /\A[0-9a-fA-F]{128}\z/
Branch: 2 branches
BranchTrueFalseTotal
$secret =~ /\A[0-9a-fA-F]{128}\z/u or die qq[$self->{"file"}: missing or invalid secret\n] 145 1 146
$secret =~ /\A[0-9a-fA-F]{128}\z/u or die qq[$self->{"file"}: missing or invalid secret\n] 0 0 0
400 or die "$self->{file}: missing or invalid secret\n";
401  
402 0 _prk_derive_secret (pack ("H*", $secret), $length, @info)
403 }
404  
405 352 ▶sub program_name {
Subroutine program_name: called CC=2 SCAR=9.2
Pod documented
406 0 ▶ my $name = File::Basename::basename ($0);
Subroutine program_name: not called CC=2 SCAR=9.2
Pod documented
407 0 $name =~ s/\.(?:pl|perl)\z//;
408 0 ▶ $name eq "-e" ? "perl" : $name
Branch: 2 branches
BranchTrueFalseTotal
$name eq '-e' ? : 25 327 352
$name eq '-e' ? : 0 0 0
409 }
410  
411 51 ▶sub user_name {
Subroutine user_name: called CC=3 SCAR=15.3
Pod documented
412 0 ▶ my ($name) = @_;
Subroutine user_name: not called CC=3 SCAR=15.3
Pod documented
413  
414 0 ▶ defined $name && $name =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/
Branch: 2 branches
BranchTrueFalseTotal
defined $name and $name =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/u or die "Invalid MCP user name\n" 50 1 51
defined $name and $name =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/u or die "Invalid MCP user name\n" 0 0 0
Condition33%2/6
defined $name and $name =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/u
!ll&&!rl&&r
0150
defined $name and $name =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/u
!ll&&!rl&&r
000
MC/DC50%1/2
defined $name and $name =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/u
defined $name $name =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/u
MC/DC0%0/2
defined $name and $name =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/u
defined $name $name =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/u
Truth table66%2/3
A and B
A= defined $nameB= $name =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/u
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $nameB= $name =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/u
ABresult
0X0
100
111
415 or die "Invalid MCP user name\n";
416  
417 0 $name
418 }
419  
420 14 ▶sub credential {
Subroutine credential: called CC=3 SCAR=14.2
Pod documented
421 0 ▶ my ($self, $purpose, $name, $username) = @_;
Subroutine credential: not called CC=3 SCAR=14.2
Pod documented
422  
423 0 ▶ $username = "" unless defined $username;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $username 5 9 14
unless defined $username 0 0 0
424  
425 0 ▶ $purpose eq "token"
Branch: 2 branches
BranchTrueFalseTotal
$purpose eq "token" or die "Unknown credential purpose '${purpose}'\n" 13 1 14
$purpose eq "token" or die "Unknown credential purpose '${purpose}'\n" 0 0 0
426 or die "Unknown credential purpose '$purpose'\n";
427  
428 0 my $credential = MIME::Base64::encode_base64 (
429 $self->derive_secret (
430 32,
431 $purpose,
432 (AnyEvent::MCP::utf8enc program_name),
433 (AnyEvent::MCP::utf8enc $name),
434 (AnyEvent::MCP::utf8enc $username)
435 ), ""
436 );
437 0 $credential =~ y!+/=!-_!d;
438  
439 0 $credential
440 }
441  
442 64 ▶sub users {
Subroutine users: called CC=8 SCAR=31.5
Pod documented
443 0 ▶ my ($self, $name, $program) = @_;
Subroutine users: not called CC=8 SCAR=31.5
Pod documented
444  
445 0 $name = AnyEvent::MCP::instance_name $name;
446 0 ▶ $program //= program_name;
Condition16%1/6
$program //= program_name()
l!l&&r!l&&!r
6400
$program //= program_name()
l!l&&r!l&&!r
000
MC/DC0%0/2
$program //= program_name()
$program program_name()
MC/DC0%0/2
$program //= program_name()
$program program_name()
Truth table33%1/3
A //= B
A= $programB= program_name()
ABresult
1X1
011
000
Truth table0%0/3
A //= B
A= $programB= program_name()
ABresult
1X1
011
000
447 0 my %users = (default => { scope => "global", generation => 1 });
448  
449 0 for my $entry (
450 ["global", $self->{section}{global}],
451 ["program", $self->{section}{"program $program"}],
452 ["instance", $self->{section}{"instance $name"}],
453 ) {
454 0 my ($scope, $section) = @$entry;
455 0 ▶ next unless $section;
Branch: 2 branches
BranchTrueFalseTotal
unless $section 54 138 192
unless $section 0 0 0
456  
457 0 for my $key (grep /\Auser\./, keys %$section) {
458 0 ▶ my ($user) = $key =~ /\Auser\.([A-Za-z_][A-Za-z0-9_-]*)\z/
Branch: 2 branches
BranchTrueFalseTotal
my($user) = $key =~ /\Auser\.([A-Za-z_][A-Za-z0-9_-]*)\z/u or die "Invalid MCP user '${key}'\n" 58 0 58
my($user) = $key =~ /\Auser\.([A-Za-z_][A-Za-z0-9_-]*)\z/u or die "Invalid MCP user '${key}'\n" 0 0 0
459 or die "Invalid MCP user '$key'\n";
460 0 my $generation = $section->{$key};
461 0 ▶ $generation =~ /\A-?[1-9][0-9]*\z/ && length $generation < 18
Branch: 2 branches
BranchTrueFalseTotal
$generation =~ /\A-?[1-9][0-9]*\z/u and length $generation < 18 or die "Invalid MCP user generation for '${key}'\n" 58 0 58
$generation =~ /\A-?[1-9][0-9]*\z/u and length $generation < 18 or die "Invalid MCP user generation for '${key}'\n" 0 0 0
Condition16%1/6
$generation =~ /\A-?[1-9][0-9]*\z/u and length $generation < 18
!ll&&!rl&&r
0058
$generation =~ /\A-?[1-9][0-9]*\z/u and length $generation < 18
!ll&&!rl&&r
000
MC/DC0%0/2
$generation =~ /\A-?[1-9][0-9]*\z/u and length $generation < 18
$generation =~ /\A-?[1-9][0-9]*\z/u length $generation < 18
MC/DC0%0/2
$generation =~ /\A-?[1-9][0-9]*\z/u and length $generation < 18
$generation =~ /\A-?[1-9][0-9]*\z/u length $generation < 18
Truth table33%1/3
A and B
A= $generation =~ /\A-?[1-9][0-9]*\z/uB= length $generation < 18
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $generation =~ /\A-?[1-9][0-9]*\z/uB= length $generation < 18
ABresult
0X0
100
111
462 or die "Invalid MCP user generation for '$key'\n";
463 0 $users{$user} = { scope => $scope, generation => $generation };
464 }
465 }
466  
467 0 \%users
468 }
469  
470 54 ▶sub user_credentials {
Subroutine user_credentials: called CC=5 SCAR=22.7
Pod documented
471 0 ▶ my ($self, $name, $program) = @_;
Subroutine user_credentials: not called CC=5 SCAR=22.7
Pod documented
472  
473 0 $name = AnyEvent::MCP::instance_name $name;
474 0 ▶ $program //= program_name;
Condition33%2/6
$program //= program_name()
l!l&&r!l&&!r
9450
$program //= program_name()
l!l&&r!l&&!r
000
MC/DC0%0/2
$program //= program_name()
$program program_name()
MC/DC0%0/2
$program //= program_name()
$program program_name()
Truth table66%2/3
A //= B
A= $programB= program_name()
ABresult
1X1
011
000
Truth table0%0/3
A //= B
A= $programB= program_name()
ABresult
1X1
011
000
475 0 my $users = $self->users ($name, $program);
476 0 my @credentials;
477  
478 0 ▶ for my $user (sort { ($a ne "default") <=> ($b ne "default") || $a cmp $b } keys %$users) {
Condition16%1/6
($a ne 'default') <=> ($b ne 'default') || $a cmp $b
l!l&&r!l&&!r
2100
($a ne 'default') <=> ($b ne 'default') || $a cmp $b
l!l&&r!l&&!r
000
MC/DC0%0/2
($a ne 'default') <=> ($b ne 'default') || $a cmp $b
($a ne 'default') <=> ($b ne 'default') $a cmp $b
MC/DC0%0/2
($a ne 'default') <=> ($b ne 'default') || $a cmp $b
($a ne 'default') <=> ($b ne 'default') $a cmp $b
Truth table33%1/3
A || B
A= ($a ne 'default') <=> ($b ne 'default')B= $a cmp $b
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= ($a ne 'default') <=> ($b ne 'default')B= $a cmp $b
ABresult
1X1
011
000
479 0 my $info = $users->{$user};
480 0 ▶ next unless $info->{generation} > 0;
Branch: 2 branches
BranchTrueFalseTotal
unless $info->{"generation"} > 0 4 71 75
unless $info->{"generation"} > 0 0 0 0
481 my $token = MIME::Base64::encode_base64 ($self->derive_secret (
482 32, "user-token-v1", AnyEvent::MCP::utf8enc ($program),
483 AnyEvent::MCP::utf8enc ($name), $info->{scope},
484 0 AnyEvent::MCP::utf8enc ($user), "$info->{generation}"
485 ), "");
486 0 $token =~ y!+/=!-_!d;
487 0 push @credentials, [$user, "$info->{scope}:$user", $token];
488 }
489  
490 \@credentials
491 0 }
492  
493 39 ▶sub connect_user {
Subroutine connect_user: called CC=5 SCAR=25.7
Pod documented
494 0 ▶ my ($self, $name, $program) = @_;
Subroutine connect_user: not called CC=5 SCAR=25.7
Pod documented
495  
496 0 $name = AnyEvent::MCP::instance_name $name;
497 0 ▶ $program //= program_name;
Condition33%2/6
$program //= program_name()
l!l&&r!l&&!r
1380
$program //= program_name()
l!l&&r!l&&!r
000
MC/DC0%0/2
$program //= program_name()
$program program_name()
MC/DC0%0/2
$program //= program_name()
$program program_name()
Truth table66%2/3
A //= B
A= $programB= program_name()
ABresult
1X1
011
000
Truth table0%0/3
A //= B
A= $programB= program_name()
ABresult
1X1
011
000
498 my $user = $self->{section}{"instance $name"}{connect_user}
499 // $self->{section}{"program $program"}{connect_user}
500 0 ▶ // $self->{section}{global}{connect_user} // "default";
Condition18%3/16
(($self->{"section"}{"instance $name"}{"connect_user"} // $self->{"section"}{"program $program"}{"connect_user"}) // $self->{"section"}{"global"}{"connect_user"}) // "default"
l!l
039
($self->{"section"}{"instance $name"}{"connect_user"} // $self->{"section"}{"program $program"}{"connect_user"}) // $self->{"section"}{"global"}{"connect_user"}
l!l&&r!l&&!r
0039
$self->{"section"}{"instance $name"}{"connect_user"} // $self->{"section"}{"program $program"}{"connect_user"}
l!l&&r!l&&!r
0039
(($self->{"section"}{"instance $name"}{"connect_user"} // $self->{"section"}{"program $program"}{"connect_user"}) // $self->{"section"}{"global"}{"connect_user"}) // "default"
l!l
00
($self->{"section"}{"instance $name"}{"connect_user"} // $self->{"section"}{"program $program"}{"connect_user"}) // $self->{"section"}{"global"}{"connect_user"}
l!l&&r!l&&!r
000
$self->{"section"}{"instance $name"}{"connect_user"} // $self->{"section"}{"program $program"}{"connect_user"}
l!l&&r!l&&!r
000
MC/DC0%0/3
(($self->{"section"}{"instance $name"}{"connect_user"} // $self->{"section"}{"program $program"}{"connect_user"}) // $self->{"section"}{"global"}{"connect_user"}) // "default"
$self->{"section"}{"instance $name"}{"connect_user"} $self->{"section"}{"program $program"}{"connect_user"} $self->{"section"}{"global"}{"connect_user"}
MC/DC0%0/3
(($self->{"section"}{"instance $name"}{"connect_user"} // $self->{"section"}{"program $program"}{"connect_user"}) // $self->{"section"}{"global"}{"connect_user"}) // "default"
$self->{"section"}{"instance $name"}{"connect_user"} $self->{"section"}{"program $program"}{"connect_user"} $self->{"section"}{"global"}{"connect_user"}
Truth table25%1/4
A // B // C
A= $self->{"section"}{"instance $name"}{"connect_user"}B= $self->{"section"}{"program $program"}{"connect_user"}C= $self->{"section"}{"global"}{"connect_user"}
ABCresult
1XX1
01X1
0011
0000
Truth table0%0/4
A // B // C
A= $self->{"section"}{"instance $name"}{"connect_user"}B= $self->{"section"}{"program $program"}{"connect_user"}C= $self->{"section"}{"global"}{"connect_user"}
ABCresult
1XX1
01X1
0011
0000
501 0 user_name $user
502 }
503  
504 62 ▶sub http_getopt {
Subroutine http_getopt: called CC=1 SCAR=1.2
Pod documented
505 0 ▶ my ($name, $bind, $print_url, $tokens) = @_;
Subroutine http_getopt: not called CC=1 SCAR=1.2
Pod documented
506  
507 0 "name=s" => $name, "bind=s" => $bind, "print-url|print-urls:s" => $print_url, "token=s@" => $tokens
508 }
509  
510 45 ▶sub auto_rules {
Subroutine auto_rules: called CC=7 SCAR=26.4
Pod documented
511 0 ▶ my ($self, $name) = @_;
Subroutine auto_rules: not called CC=7 SCAR=26.4
Pod documented
512 my %settings = (
513 0 ▶ %{$self->{section}{"program " . program_name ()} || {}},
Condition50%2/4
$self->{'section'}{'program ' . program_name()} || {}
l!l
837
$self->{'section'}{'program ' . program_name()} || {}
l!l
00
MC/DC100%1/1
$self->{'section'}{'program ' . program_name()} || {}
$self->{'section'}{'program ' . program_name()}
MC/DC0%0/1
$self->{'section'}{'program ' . program_name()} || {}
$self->{'section'}{'program ' . program_name()}
Truth table100%2/2
A
A= $self->{'section'}{'program ' . program_name()}
Aresult
11
00
Truth table0%0/2
A
A= $self->{'section'}{'program ' . program_name()}
Aresult
11
00
514 0 ▶ %{$self->{section}{"instance " . AnyEvent::MCP::instance_name ($name)} || {}},
Condition50%2/4
$self->{'section'}{'instance ' . AnyEvent::MCP::instance_name($name)} || {}
l!l
1431
$self->{'section'}{'instance ' . AnyEvent::MCP::instance_name($name)} || {}
l!l
00
MC/DC100%1/1
$self->{'section'}{'instance ' . AnyEvent::MCP::instance_name($name)} || {}
$self->{'section'}{'instance ' . AnyEvent::MCP::instance_name($name)}
MC/DC0%0/1
$self->{'section'}{'instance ' . AnyEvent::MCP::instance_name($name)} || {}
$self->{'section'}{'instance ' . AnyEvent::MCP::instance_name($name)}
Truth table100%2/2
A
A= $self->{'section'}{'instance ' . AnyEvent::MCP::instance_name($name)}
Aresult
11
00
Truth table0%0/2
A
A= $self->{'section'}{'instance ' . AnyEvent::MCP::instance_name($name)}
Aresult
11
00
515 );
516 0 my @rules;
517  
518 0 for my $key (sort grep /\Aauto-rule\./, keys %settings) {
519 0 ▶ $key =~ /\Aauto-rule\.(.+)\z/
Branch: 2 branches
BranchTrueFalseTotal
$key =~ /\Aauto-rule\.(.+)\z/u or die "Invalid automatic permission rule key '${key}'\n" 11 0 11
$key =~ /\Aauto-rule\.(.+)\z/u or die "Invalid automatic permission rule key '${key}'\n" 0 0 0
520 or die "Invalid automatic permission rule key '$key'\n";
521 0 my $value = $settings{$key};
522 0 ▶ next unless length $value;
Branch: 2 branches
BranchTrueFalseTotal
unless length $value 2 9 11
unless length $value 0 0 0
523 0 ▶ my ($decision, $spec) = $value =~ /\A(allow|deny)\s+(\S+)\z/
Branch: 2 branches
BranchTrueFalseTotal
() = $value =~ /\A(allow|deny)\s+(\S+)\z/u or die "Invalid configured automatic permission rule '${key}'\n" 8 1 9
() = $value =~ /\A(allow|deny)\s+(\S+)\z/u or die "Invalid configured automatic permission rule '${key}'\n" 0 0 0
524 or die "Invalid configured automatic permission rule '$key'\n";
525 0 push @rules, [$decision, $spec];
526 }
527  
528 @rules
529 0 }
530  
531 46 ▶sub server_port {
Subroutine server_port: called CC=1 SCAR=1.2
Pod documented
532 0 ▶ my ($self, $name) = @_;
Subroutine server_port: not called CC=1 SCAR=1.2
Pod documented
533  
534 0 $name = AnyEvent::MCP::instance_name $name;
535  
536 0 16384 + (
537 (unpack "n", $self->derive_secret (
538 2,
539 "port",
540 (AnyEvent::MCP::utf8enc program_name),
541 (AnyEvent::MCP::utf8enc $name))
542 ) & 16383)
543 }
544  
545 41 ▶sub http_settings {
Subroutine http_settings: called CC=18 SCAR=45.0
Pod documented
546 0 ▶ my ($self, $name, $bind) = @_;
Subroutine http_settings: not called CC=18 SCAR=45.0
Pod documented
547  
548 0 $name = AnyEvent::MCP::instance_name $name;
549 my $settings = {
550 0 %{ $self->{section}{"program " . program_name ()} },
551 0 %{ $self->{section}{"instance $name"} },
552 };
553 0 my $port = $self->server_port ($name);
554 0 ▶ my $configured = $settings->{bind} // "127.0.0.1:$port";
Condition50%2/4
$settings->{"bind"} // "127.0.0.1:$port"
l!l
1625
$settings->{"bind"} // "127.0.0.1:$port"
l!l
00
MC/DC100%1/1
$settings->{"bind"} // "127.0.0.1:$port"
$settings->{"bind"}
MC/DC0%0/1
$settings->{"bind"} // "127.0.0.1:$port"
$settings->{"bind"}
Truth table100%2/2
A
A= $settings->{"bind"}
Aresult
11
00
Truth table0%0/2
A
A= $settings->{"bind"}
Aresult
11
00
555 0 ▶ $configured = "127.0.0.1$configured" if $configured =~ /\A:[0-9]+\z/;
Branch: 2 branches
BranchTrueFalseTotal
if $configured =~ /\A:[0-9]+\z/u 0 41 41
if $configured =~ /\A:[0-9]+\z/u 0 0 0
556 0 my ($configured_host, $configured_port) = AnyEvent::Socket::parse_hostport $configured, $port;
557 0 ▶ defined $configured_host && defined $configured_port
Branch: 2 branches
BranchTrueFalseTotal
defined $configured_host and defined $configured_port and $configured_port =~ /\A[0-9]+\z/u and $configured_port <= 65535 or die "Invalid MCP bind address '${configured}'\n" 41 0 41
defined $configured_host and defined $configured_port and $configured_port =~ /\A[0-9]+\z/u and $configured_port <= 65535 or die "Invalid MCP bind address '${configured}'\n" 0 0 0
Condition16%3/18
defined $configured_host and defined $configured_port and $configured_port =~ /\A[0-9]+\z/u and $configured_port <= 65535
!ll&&!rl&&r
0041
defined $configured_host and defined $configured_port and $configured_port =~ /\A[0-9]+\z/u
!ll&&!rl&&r
0041
defined $configured_host and defined $configured_port
!ll&&!rl&&r
0041
defined $configured_host and defined $configured_port and $configured_port =~ /\A[0-9]+\z/u and $configured_port <= 65535
!ll&&!rl&&r
000
defined $configured_host and defined $configured_port and $configured_port =~ /\A[0-9]+\z/u
!ll&&!rl&&r
000
defined $configured_host and defined $configured_port
!ll&&!rl&&r
000
MC/DC0%0/4
defined $configured_host and defined $configured_port and $configured_port =~ /\A[0-9]+\z/u and $configured_port <= 65535
defined $configured_host defined $configured_port $configured_port =~ /\A[0-9]+\z/u $configured_port <= 65535
MC/DC0%0/4
defined $configured_host and defined $configured_port and $configured_port =~ /\A[0-9]+\z/u and $configured_port <= 65535
defined $configured_host defined $configured_port $configured_port =~ /\A[0-9]+\z/u $configured_port <= 65535
Truth table20%1/5
A and B and C and D
A= defined $configured_hostB= defined $configured_portC= $configured_port =~ /\A[0-9]+\z/uD= $configured_port <= 65535
ABCDresult
0XXX0
10XX0
110X0
11100
11111
Truth table0%0/5
A and B and C and D
A= defined $configured_hostB= defined $configured_portC= $configured_port =~ /\A[0-9]+\z/uD= $configured_port <= 65535
ABCDresult
0XXX0
10XX0
110X0
11100
11111
558 && $configured_port =~ /\A[0-9]+\z/ && $configured_port <= 65535
559 or die "Invalid MCP bind address '$configured'\n";
560  
561 0 ▶ $bind = $configured unless defined $bind;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $bind 19 22 41
unless defined $bind 0 0 0
562 0 ▶ $bind = "127.0.0.1$bind" if $bind =~ /\A:[0-9]+\z/;
Branch: 2 branches
BranchTrueFalseTotal
if $bind =~ /\A:[0-9]+\z/u 5 36 41
if $bind =~ /\A:[0-9]+\z/u 0 0 0
563  
564 0 my ($host, $service) = AnyEvent::Socket::parse_hostport $bind, $configured_port;
565 0 ▶ defined $host && defined $service && $service =~ /\A[0-9]+\z/ && $service <= 65535
Branch: 2 branches
BranchTrueFalseTotal
defined $host and defined $service and $service =~ /\A[0-9]+\z/u and $service <= 65535 or die "Invalid MCP bind address '${bind}'\n" 40 1 41
defined $host and defined $service and $service =~ /\A[0-9]+\z/u and $service <= 65535 or die "Invalid MCP bind address '${bind}'\n" 0 0 0
Condition22%4/18
defined $host and defined $service and $service =~ /\A[0-9]+\z/u and $service <= 65535
!ll&&!rl&&r
0140
defined $host and defined $service and $service =~ /\A[0-9]+\z/u
!ll&&!rl&&r
0041
defined $host and defined $service
!ll&&!rl&&r
0041
defined $host and defined $service and $service =~ /\A[0-9]+\z/u and $service <= 65535
!ll&&!rl&&r
000
defined $host and defined $service and $service =~ /\A[0-9]+\z/u
!ll&&!rl&&r
000
defined $host and defined $service
!ll&&!rl&&r
000
MC/DC25%1/4
defined $host and defined $service and $service =~ /\A[0-9]+\z/u and $service <= 65535
defined $host defined $service $service =~ /\A[0-9]+\z/u $service <= 65535
MC/DC0%0/4
defined $host and defined $service and $service =~ /\A[0-9]+\z/u and $service <= 65535
defined $host defined $service $service =~ /\A[0-9]+\z/u $service <= 65535
Truth table40%2/5
A and B and C and D
A= defined $hostB= defined $serviceC= $service =~ /\A[0-9]+\z/uD= $service <= 65535
ABCDresult
0XXX0
10XX0
110X0
11100
11111
Truth table0%0/5
A and B and C and D
A= defined $hostB= defined $serviceC= $service =~ /\A[0-9]+\z/uD= $service <= 65535
ABCDresult
0XXX0
10XX0
110X0
11100
11111
566 or die "Invalid MCP bind address '$bind'\n";
567  
568 my %kv = (
569 0 ▶ (map { exists $settings->{$_} ? ($_ => $settings->{$_}) : () }
Branch: 2 branches
BranchTrueFalseTotal
exists $settings->{$_} ? : 2 198 200
exists $settings->{$_} ? : 0 0 0
570 qw(path url_host url_scheme tls_cert tls_key)),
571 name => $name,
572 host => $host,
573 port => $service,
574 );
575  
576 0 ▶ unless (AnyEvent::Socket::parse_address $host) {
Branch: 2 branches
BranchTrueFalseTotal
unless (&AnyEvent::Socket::parse_address($host)) 1 39 40
unless (&AnyEvent::Socket::parse_address($host)) 0 0 0
577 0 my $cv = AnyEvent->condvar;
578 0 AnyEvent::Socket::resolve_sockaddr $host, $service, "tcp", 0, 0, $cv;
579 0 my ($resolved) = $cv->recv;
580 0 ▶ $resolved or die "Cannot resolve MCP bind host '$host'\n";
Branch: 2 branches
BranchTrueFalseTotal
$resolved or die "Cannot resolve MCP bind host '${host}'\n" 1 0 1
$resolved or die "Cannot resolve MCP bind host '${host}'\n" 0 0 0
581  
582 0 my (undef, $address) = AnyEvent::Socket::unpack_sockaddr $resolved->[3];
583 0 ▶ $kv{url_host} //= $host;
Condition16%1/6
$kv{"url_host"} //= $host
l!l&&r!l&&!r
010
$kv{"url_host"} //= $host
l!l&&r!l&&!r
000
MC/DC0%0/2
$kv{"url_host"} //= $host
$kv{"url_host"} $host
MC/DC0%0/2
$kv{"url_host"} //= $host
$kv{"url_host"} $host
Truth table33%1/3
A //= B
A= $kv{"url_host"}B= $host
ABresult
1X1
011
000
Truth table0%0/3
A //= B
A= $kv{"url_host"}B= $host
ABresult
1X1
011
000
584 0 $kv{host} = AnyEvent::Socket::format_address $address;
585 }
586  
587 0 ▶ if (exists $settings->{tokens}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $settings->{'tokens'}) { } 6 34 40
if (exists $settings->{'tokens'}) { } 0 0 0
588 0 $kv{tokens} = [split /\s*,\s*/, $settings->{tokens}, -1];
589 0 $kv{connect_tokens} = $kv{tokens};
590 } else {
591 0 my $credentials = $self->user_credentials ($name);
592 0 $kv{tokens} = [map $_->[2], @$credentials];
593 0 $kv{token_users} = { map { $_->[2] => $_->[1] } @$credentials };
594 0 my $connect = $self->connect_user ($name);
595 0 $kv{connect_tokens} = [map $_->[2], grep $_->[0] eq $connect, @$credentials];
596 }
597  
598 0 %kv
599 }
600  
601 13 ▶sub http_url {
Subroutine http_url: called CC=10 SCAR=32.2
Pod documented
602 0 ▶ my ($self, $name, $bind, %options) = @_;
Subroutine http_url: not called CC=10 SCAR=32.2
Pod documented
603  
604 0 $name = AnyEvent::MCP::instance_name $name;
605  
606 # only a derived token depends on the master secret surviving this process.
607 0 ▶ if (($options{auth} || "") ne "none"
Branch: 2 branches
BranchTrueFalseTotal
if (($options{"auth"} || "") ne "none" and not exists $options{"tokens"} and not exists $self->{"section"}{"instance $name"}{"tokens"} and not exists $self->{"section"}{'program ' . program_name()}{"tokens"} and not exists $self->{"section"}{"global"}{"tokens"}) 3 10 13
if (($options{"auth"} || "") ne "none" and not exists $options{"tokens"} and not exists $self->{"section"}{"instance $name"}{"tokens"} and not exists $self->{"section"}{'program ' . program_name()}{"tokens"} and not exists $self->{"section"}{"global"}{"tokens"}) 0 0 0
Condition42%12/28
($options{"auth"} || "") ne "none" and not exists $options{"tokens"} and not exists $self->{"section"}{"instance $name"}{"tokens"} and not exists $self->{"section"}{'program ' . program_name()}{"tokens"} and not exists $self->{"section"}{"global"}{"tokens"}
!ll&&!rl&&r
1003
($options{"auth"} || "") ne "none" and not exists $options{"tokens"} and not exists $self->{"section"}{"instance $name"}{"tokens"} and not exists $self->{"section"}{'program ' . program_name()}{"tokens"}
!ll&&!rl&&r
1003
($options{"auth"} || "") ne "none" and not exists $options{"tokens"} and not exists $self->{"section"}{"instance $name"}{"tokens"}
!ll&&!rl&&r
913
($options{"auth"} || "") ne "none" and not exists $options{"tokens"}
!ll&&!rl&&r
184
$options{'auth'} || ''
l!l
112
($options{"auth"} || "") ne "none" and not exists $options{"tokens"} and not exists $self->{"section"}{"instance $name"}{"tokens"} and not exists $self->{"section"}{'program ' . program_name()}{"tokens"} and not exists $self->{"section"}{"global"}{"tokens"}
!ll&&!rl&&r
000
($options{"auth"} || "") ne "none" and not exists $options{"tokens"} and not exists $self->{"section"}{"instance $name"}{"tokens"} and not exists $self->{"section"}{'program ' . program_name()}{"tokens"}
!ll&&!rl&&r
000
($options{"auth"} || "") ne "none" and not exists $options{"tokens"} and not exists $self->{"section"}{"instance $name"}{"tokens"}
!ll&&!rl&&r
000
($options{"auth"} || "") ne "none" and not exists $options{"tokens"}
!ll&&!rl&&r
000
$options{'auth'} || ''
l!l
00
MC/DC0%0/5
($options{"auth"} || "") ne "none" and not exists $options{"tokens"} and not exists $self->{"section"}{"instance $name"}{"tokens"} and not exists $self->{"section"}{'program ' . program_name()}{"tokens"} and not exists $self->{"section"}{"global"}{"tokens"}
$options{'auth'} not exists $options{"tokens"} not exists $self->{"section"}{"instance $name"}{"tokens"} not exists $self->{"section"}{'program ' . program_name()}{"tokens"} not exists $self->{"section"}{"global"}{"tokens"}
MC/DC0%0/5
($options{"auth"} || "") ne "none" and not exists $options{"tokens"} and not exists $self->{"section"}{"instance $name"}{"tokens"} and not exists $self->{"section"}{'program ' . program_name()}{"tokens"} and not exists $self->{"section"}{"global"}{"tokens"}
$options{'auth'} not exists $options{"tokens"} not exists $self->{"section"}{"instance $name"}{"tokens"} not exists $self->{"section"}{'program ' . program_name()}{"tokens"} not exists $self->{"section"}{"global"}{"tokens"}
Truth table0%0/6
A and not(B) and not(C) and not(D) and not(E)
A= $options{'auth'}B= not exists $options{"tokens"}C= not exists $self->{"section"}{"instance $name"}{"tokens"}D= not exists $self->{"section"}{'program ' . program_name()}{"tokens"}E= not exists $self->{"section"}{"global"}{"tokens"}
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
Truth table0%0/6
A and not(B) and not(C) and not(D) and not(E)
A= $options{'auth'}B= not exists $options{"tokens"}C= not exists $self->{"section"}{"instance $name"}{"tokens"}D= not exists $self->{"section"}{'program ' . program_name()}{"tokens"}E= not exists $self->{"section"}{"global"}{"tokens"}
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
608 && !exists $options{tokens}
609 && !exists $self->{section}{"instance $name"}{tokens}
610 && !exists $self->{section}{"program " . program_name ()}{tokens}
611 && !exists $self->{section}{global}{tokens}) {
612 0 my $stored = (ref $self)->new (file => $self->{file})->load;
613 $stored->{section}{global}{secret} eq $self->{section}{global}{secret}
614 0 ▶ or die "MCP master secret is not persistently stored in $self->{file}\n";
Branch: 2 branches
BranchTrueFalseTotal
$stored->{"section"}{"global"}{"secret"} eq $self->{"section"}{"global"}{"secret"} or die qq[MCP master secret is not persistently stored in $self->{"file"}\n] 2 1 3
$stored->{"section"}{"global"}{"secret"} eq $self->{"section"}{"global"}{"secret"} or die qq[MCP master secret is not persistently stored in $self->{"file"}\n] 0 0 0
615 }
616  
617 0 require AnyEvent::MCP::Server::HTTP;
618 0 my %http = $self->http_settings ($name, $bind);
619 0 ▶ delete $http{tokens} if ($options{auth} || "") eq "none";
Branch: 2 branches
BranchTrueFalseTotal
if ($options{"auth"} || "") eq "none" 1 11 12
if ($options{"auth"} || "") eq "none" 0 0 0
Condition50%2/4
$options{'auth'} || ''
l!l
111
$options{'auth'} || ''
l!l
00
MC/DC100%1/1
$options{'auth'} || ''
$options{'auth'}
MC/DC0%0/1
$options{'auth'} || ''
$options{'auth'}
Truth table100%2/2
A
A= $options{'auth'}
Aresult
11
00
Truth table0%0/2
A
A= $options{'auth'}
Aresult
11
00
620  
621 0 AnyEvent::MCP::Server::HTTP->new (%http, %options)->url
622 }
623  
624  
625 102 1
« blib/lib/AnyEvent/MCP/Extension.pm ↑ Summary bin/mcp-http »