blib/lib/AnyEvent/MCP/Config.pm

filter: Statementstmt 100% 200 / 200 Branchbran 81.2% 78 / 96 Conditioncond 66.2% 53 / 80 MC/DCmcdc 28.2% 11 / 39 Subroutinesub 100% 29 / 29 Podpod 100% 15 / 15 totaltotal 84.1% 386 / 459 CC 83 SCAR 21.0
CC
83
Cov
88%
http_settings
19.7
_ensure_secret
11.4
http_url
10.0
CRAP 8.2
« blib/lib/AnyEvent/MCP/Extension.pm ↑ Summary blib/lib/AnyEvent/MCP/Client/JSONL.pm »
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;
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. The first call to C<global>
24 reads the configuration, ensures a master secret exists and caches the object
25 for the process.
26  
27 A missing secret is generated using C<AnyEvent::MCP::random_bytes> and saved
28 when possible. Saving failure produces a warning; the generated secret remains
29 usable in this process, but another process might derive different credentials.
30 A supplied invalid secret is an error. Changing or losing the master secret
31 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 (file => $file)
44  
45 Returns the cached global configuration. The first call accepts the same
46 arguments as C<new>; later calls return the same object without rereading
47 the file. Call this explicitly during program startup.
48  
49 =item $config = AnyEvent::MCP::Config->new (file => $file)
50  
51 Reads C<$file>, or the default file when omitted, and ensures a master
52 secret exists. Unlike C<global>, each call creates an independent object.
53 A missing file is allowed; other read errors are fatal. Sections are available
54 as C<< $config->{section}{$section} >>, each a hash reference of string
55 values. Named servers use C<[instance NAME]>.
56  
57 =item $config = AnyEvent::MCP::Config->init (file => $file)
58  
59 Explicitly creates the configuration file with a random secret, mode 0600.
60 Creates missing parent directories with mode 0700. Fails if the file already
61 exists, so it cannot accidentally replace an existing secret.
62  
63 =item $bytes = $config->derive_secret ($length, @info)
64  
65 =item $bytes = AnyEvent::MCP::Config->derive_secret ($length, @info)
66  
67 Derive C<$length> secret bytes from the configuration's master secret and
68 an ordered list of context byte strings C<@info>. The class form uses
69 C<global>, loading the default configuration on first use and reusing it
70 thereafter. The object form uses that object's configuration, including
71 one loaded from an explicit file.
72  
73 The same master secret and context always produce the same bytes. Use a
74 distinctive application/purpose string as the first context field, followed
75 by names or other values identifying the result:
76  
77 my $secret = AnyEvent::MCP::Config->derive_secret (
78 32, "my application token v1", AnyEvent::MCP::utf8enc ($name)
79 );
80  
81 The result contains raw bytes; encode it yourself if a printable token is
82 needed. This method does not add the result to a server's accepted tokens.
83  
84 The fixed C<anyevent-mcp> context field is prepended automatically.
85 Context fields are separately length-prefixed, so C<("ab", "c")> and
86 C<("a", "bc")> have different contexts. Encode character strings explicitly,
87 for example with C<AnyEvent::MCP::utf8enc>. Context arguments are left
88 unchanged.
89  
90 C<$length> must be an integer in 0..16320; zero returns an empty string.
91 Shorter results are prefixes of longer results for the same master secret
92 and context. Use distinct context fields for different purposes, rather
93 than relying on different lengths to separate them.
94  
95 Derivation uses HKDF-SHA512 expansion, with each context field prefixed by
96 its four-byte big-endian byte length. Changing or losing the master secret
97 changes every result derived from it.
98  
99 =item $credential = $config->credential ($purpose, $name, $username)
100  
101 Derives a 32-byte credential, returned as unpadded URL-safe Base64.
102 C<$purpose> is C<token>. C<$name> is the instance name, and C<$username>
103 is optional. This is the older unmanaged derivation API; its result is not
104 automatically accepted by a server. Managed credentials are enumerated by
105 C<user_credentials>.
106  
107 Credentials use the context C<("anyevent-mcp", "token", PROGRAM, NAME, USER)>.
108 PROGRAM is the invoked basename, with a trailing C<.pl> or C<.perl> removed.
109 Changing the program or instance name changes the credential.
110 A credential derived with C<$username> is B<not> automatically accepted
111 by the server. This method neither requires a server section nor configures
112 authentication.
113  
114 =item $users = $config->users ($instance, $program)
115  
116 Return effective user definitions keyed by username, each with C<scope>
117 (global, program or instance) and signed C<generation>. Inheritance is
118 global, program, instance; a negative generation disables without fallback.
119  
120 =item $credentials = $config->user_credentials ($instance, $program)
121  
122 Return enabled users as C<[[USERNAME, PRINCIPAL, TOKEN]...]> with default
123 first. The principal is C<global:NAME>, C<program:NAME>, or C<instance:NAME>.
124 HKDF context includes program, instance, scope, user and generation. Rotating
125 a generation changes the token but preserves saved user permissions.
126  
127 =item $user = $config->connect_user ($instance, $program)
128  
129 Return the username allowed to register private Vim, browser WebSocket, or
130 multibroker reverse-MCP connections. C<connect_user> inherits through the
131 usual scopes and defaults to C<default>. Revoking that user disables these
132 connections until its generation is renewed or another user is selected.
133  
134 =item $program = AnyEvent::MCP::Config::program_name ()
135  
136 Returns the invoked program basename, stripping C<.pl> or C<.perl>.
137 An inline Perl invocation uses C<perl>. This identity also selects the
138 C<[program PROGRAM]> configuration section; it is independent of routing aliases.
139  
140 =item $name = AnyEvent::MCP::Config::server_name ($name)
141  
142 Validate a service-instance name, or use C<default> if undefined.
143 Names consist of an ASCII letter or underscore followed by letters, digits,
144 underscores or hyphens.
145  
146 =item @options = AnyEvent::MCP::Config::http_getopt (\$name, \$bind, \$print_url, \@tokens)
147  
148 Return standard C<Getopt::Long> options referring to the caller's scalars.
149  
150 =item $port = $config->server_port ($name)
151  
152 Derive a stable default port in 16384..32767 from the master secret and
153 program basename and service-instance name. Collisions produce ordinary listener bind errors.
154  
155 =item %settings = $config->http_settings ($name, $bind)
156  
157 Resolve HTTP settings from C<[instance NAME]> and an optional command-line
158 C<bind> override. The default is IPv4 loopback and the name-derived port.
159 The bind setting accepts C<ADDRESS:PORT>, C<[IPv6]:PORT>, C<ADDRESS>,
160 and C<:PORT> (which keeps the default loopback address).
161 Hostnames are resolved once when settings are obtained; their original
162 spelling is advertised unless C<url_host> overrides it. The returned options
163 include C<name>, C<host>, C<port> and C<tokens>, plus configured C<path>,
164 C<url_host> and C<url_scheme>. This does not start a listener.
165  
166 =item @rules = $config->auto_rules ($name)
167  
168 Return ordered automatic permission rules from C<[program PROGRAM]> and
169 C<[instance NAME]>. C<auto-rule.SUFFIX = allow|deny TOOL[,ACTION[,CAPABILITY]]>
170 entries are sorted by key. The suffix is an arbitrary ordering label; instance
171 entries override matching program entries, and an empty value disables an
172 inherited entry. Command-line rules take precedence over these defaults.
173  
174 =item $url = $config->http_url ($name, $bind, %options)
175  
176 Format a URL containing the first accepted token, without listening.
177 Port zero cannot be used because the kernel has not yet selected a port.
178 When the token is derived, the master secret must be persistently stored,
179 so the next process derives the same URL. Explicit static tokens and
180 C<auth =E<gt> "none"> do not require that persistence check.
181  
182 =back
183  
184 =cut
185  
186 package AnyEvent::MCP::Config;
187  
188 349 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
189  
190 349 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
191 349 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
192 349 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
193 349 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
194 349 ▶use Fcntl qw(O_WRONLY O_RDWR O_CREAT O_EXCL LOCK_EX);
Subroutine BEGIN: called CC=1 SCAR=0.0
195 349 ▶use Errno qw(ENOENT);
Subroutine BEGIN: called CC=1 SCAR=0.0
196  
197 349 ▶use AnyEvent::Socket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
198 349 ▶use AnyEvent::MCP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
199  
200 349 our @ISA = qw(AnyEvent::MCP::Base);
201  
202 532 ▶sub _file {
Subroutine _file: called CC=3 SCAR=11.1
203 my $dir = $ENV{XDG_CONFIG_HOME}
204 532 ▶ || ($ENV{HOME} || die "HOME is not set\n") . "/.config";
Condition75%3/4
$ENV{'XDG_CONFIG_HOME'} || ($ENV{'HOME'} || die("HOME is not set\n")) . '/.config'
l!l
5248
$ENV{'HOME'} || die("HOME is not set\n")
l!l
80
MC/DC100%1/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
205  
206 532 "$dir/anyevent-mcp/global.ini"
207 }
208  
209 349 my $global;
210  
211 222 ▶sub global {
Subroutine global: called CC=2 SCAR=7.1
Pod documented
212 222 my $class = shift;
213  
214 222 ▶ $global ||= $class->new (@_)
Condition66%2/3
$global ||= $class->new(@_)
l!l&&r!l&&!r
771450
MC/DC0%0/2
$global ||= $class->new(@_)
$global $class->new(@_)
Truth table66%2/3
A ||= B
A= $globalB= $class->new(@_)
ABresult
1X1
011
000
215 }
216  
217 200 ▶sub new {
Subroutine new: called CC=6 SCAR=18.0
Pod documented
218 200 my ($class, %kv) = @_;
219  
220 200 my $self = $class->SUPER::new (section => {}, %kv);
221 200 ▶ $self->{file} = _file unless exists $self->{file};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"file"} 144 56 200
222  
223 200 ▶ if (open my $fh, "<:encoding(UTF-8)", $self->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '<:encoding(UTF-8)', $self->{'file'}) { } 183 17 200
224 183 $self->_read ($fh);
225 183 ▶ close $fh or die "$self->{file}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die qq[$self->{"file"}: $!\n] 183 0 183
226 } else {
227 17 ▶ die "$self->{file}: $!\n" unless $! == ENOENT;
Branch: 1 branch
BranchTrueFalseTotal
unless $! == 2 0 17 17
228 }
229  
230 200 $self->_ensure_secret;
231  
232 199 ▶ $self->{section}{global}{secret} =~ /\A[0-9a-fA-F]{128}\z/
Branch: 1 branch
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] 198 1 199
233 or die "$self->{file}: invalid secret; supply 128 hexadecimal digits\n";
234  
235 198 $self
236 }
237  
238 220 ▶sub _read {
Subroutine _read: called CC=1 SCAR=0.0
239 220 my ($self, $fh) = @_;
240  
241 220 local $/;
242 674 ▶ AnyEvent::MCP::parse_ini scalar (<$fh>), sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
243 674 my ($name, $kv) = @_;
244 674 ▶ my $section = $self->{section}{$name || "global"} ||= {};
Condition100%4/4
$self->{"section"}{$name or "global"} ||= {}
l!l
191483
$name || 'global'
l!l
454220
MC/DC100%1/1
$self->{"section"}{$name or "global"} ||= {}
$self->{"section"}{$name or "global"}
MC/DC100%1/1
$name || 'global'
$name
Truth table100%2/2
A
A= $self->{"section"}{$name or "global"}
Aresult
11
00
Truth table100%2/2
A
A= $name
Aresult
11
00
245 674 %$section = (%$section, %$kv);
246 220 }, $self->{file};
247 }
248  
249 200 ▶sub _ensure_secret {
Subroutine _ensure_secret: called CC=11 SCAR=24.3
250 200 my ($self) = @_;
251  
252 200 ▶ return if exists $self->{section}{global}{secret};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"section"}{"global"}{"secret"} 159 41 200
253  
254 41 my $secret = eval { unpack "H*", AnyEvent::MCP::random_bytes 64 };
255 41 ▶ die "$@Supply a securely generated 128-digit hexadecimal secret in $self->{file}\n"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 40 41
256 if $@;
257  
258 40 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { File::Path::mkpath(File::Basename::dirname($self->{"file"}), 0, 448); sysopen my $fh, $self->{"file"}, 66, 384 or die qq[$self->{"file"}: $!\n]; flock $fh, 2 or die qq[$self->{"file"}: $!\n]; binmode $fh, ":encoding(UTF-8)"; my $disk = bless({"file", $self->{"file"}, "section", {}}, ref $self); $disk->_read($fh); unless (exists $disk->{"section"}{"global"}{"secret"}) { seek $fh, 0, 2 or die qq[$self->{"file"}: $!\n]; print {$fh;} "\n[global]\nsecret = $secret\n" or die qq[$self->{"file"}: $!\n]; $disk->{"section"}{"global"}{"secret"} = $secret; } ; close $fh or die qq[$self->{"file"}: $!\n]; $self->{"section"} = $disk->{"section"}; 1 } } or warn "Cannot save MCP master secret; credentials may only be stable for this process: $@" 37 3 40
259 40 File::Path::mkpath (File::Basename::dirname ($self->{file}), 0, 0700);
260 37 ▶ sysopen my $fh, $self->{file}, O_RDWR | O_CREAT, 0600
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $fh, $self->{"file"}, 66, 384 or die qq[$self->{"file"}: $!\n] 37 0 37
261 or die "$self->{file}: $!\n";
262 37 ▶ flock $fh, LOCK_EX or die "$self->{file}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 2 or die qq[$self->{"file"}: $!\n] 37 0 37
263 37 binmode $fh, ":encoding(UTF-8)";
264  
265 37 my $disk = bless { file => $self->{file}, section => {} }, ref $self;
266 37 $disk->_read ($fh);
267  
268 37 ▶ unless (exists $disk->{section}{global}{secret}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $disk->{"section"}{"global"}{"secret"}) 37 0 37
269 37 ▶ seek $fh, 0, 2 or die "$self->{file}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, 0, 2 or die qq[$self->{"file"}: $!\n] 37 0 37
270 37 ▶ print {$fh} "\n[global]\nsecret = $secret\n"
Branch: 1 branch
BranchTrueFalseTotal
print {$fh;} "\n[global]\nsecret = $secret\n" or die qq[$self->{"file"}: $!\n] 37 0 37
271 or die "$self->{file}: $!\n";
272 37 $disk->{section}{global}{secret} = $secret;
273 }
274  
275 37 ▶ close $fh or die "$self->{file}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die qq[$self->{"file"}: $!\n] 37 0 37
276 37 $self->{section} = $disk->{section};
277 37 1
278 } or warn "Cannot save MCP master secret; credentials may only be stable for this process: $@";
279  
280 $self->{section}{global}{secret} = $secret
281 40 ▶ unless exists $self->{section}{global}{secret};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"section"}{"global"}{"secret"} 3 37 40
282 }
283  
284 4 ▶sub init {
Subroutine init: called CC=5 SCAR=16.4
Pod documented
285 4 my ($class, %kv) = @_;
286  
287 4 ▶ my $file = exists $kv{file} ? $kv{file} : _file;
Branch: 1 branch
BranchTrueFalseTotal
exists $kv{'file'} ? : 4 0 4
288  
289 4 my $secret = AnyEvent::MCP::random_bytes 64;
290  
291 4 File::Path::mkpath (File::Basename::dirname ($file), 0, 0700);
292  
293 4 ▶ sysopen my $fh, $file, O_WRONLY | O_CREAT | O_EXCL, 0600
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $fh, $file, 193, 384 or die "${file}: $!\n" 2 2 4
294 or die "$file: $!\n";
295  
296 2 ▶ print {$fh} "[global]\nsecret = ", unpack ("H*", $secret), "\n"
Branch: 1 branch
BranchTrueFalseTotal
print {$fh;} "[global]\nsecret = ", unpack("H*", $secret), "\n" or die "${file}: $!\n" 2 0 2
297 or die "$file: $!\n";
298 2 ▶ close $fh or die "$file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "${file}: $!\n" 2 0 2
299  
300 2 $class->new (file => $file)
301 }
302  
303 233 ▶sub _prk_derive_secret($$@) {
Subroutine _prk_derive_secret: called CC=5 SCAR=16.1
304 233 my ($prk, $length, @info) = @_;
305  
306 233 ▶ 64 == length $prk
Branch: 1 branch
BranchTrueFalseTotal
64 == length $prk or die "derive_secret: key length must be 64 octets\n" 233 0 233
307 or die "derive_secret: key length must be 64 octets\n";
308 233 ▶ $length =~ /\A[0-9]+\z/ && $length <= 255 * 64
Branch: 1 branch
BranchTrueFalseTotal
$length =~ /\A[0-9]+\z/u and $length <= 16320 or die "derive_secret: length must be an integer in 0..16320\n" 230 3 233
Condition100%3/3
$length =~ /\A[0-9]+\z/u and $length <= 16320
!ll&&!rl&&r
21230
MC/DC100%2/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
309 or die "derive_secret: length must be an integer in 0..16320\n";
310  
311 230 my $info = pack "(N/a*)*", "anyevent-mcp", @info;
312 230 my $t = "";
313 230 my $okm = "";
314 230 my $i = 0;
315  
316 230 ▶ while ($length > length $okm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > length $okm) 485 230 715
317 485 $t = Digest::SHA::hmac_sha512 $t . $info . chr ++$i, $prk;
318 485 $okm .= $t;
319 }
320  
321 230 substr $okm, 0, $length
322 }
323  
324 234 ▶sub derive_secret {
Subroutine derive_secret: called CC=3 SCAR=11.0
Pod documented
325 234 my ($self, $length, @info) = @_;
326  
327 234 ▶ $self = $self->global unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 2 232 234
328  
329 234 my $secret = $self->{section}{global}{secret};
330 234 ▶ $secret =~ /\A[0-9a-fA-F]{128}\z/
Branch: 1 branch
BranchTrueFalseTotal
$secret =~ /\A[0-9a-fA-F]{128}\z/u or die qq[$self->{"file"}: missing or invalid secret\n] 233 1 234
331 or die "$self->{file}: missing or invalid secret\n";
332  
333 233 _prk_derive_secret (pack ("H*", $secret), $length, @info)
334 }
335  
336 1098 ▶sub program_name {
Subroutine program_name: called CC=2 SCAR=6.9
Pod documented
337 1098 my $name = File::Basename::basename ($0);
338 1098 $name =~ s/\.(?:pl|perl)\z//;
339 1098 ▶ $name eq "-e" ? "perl" : $name
Branch: 1 branch
BranchTrueFalseTotal
$name eq '-e' ? : 166 932 1098
340 }
341  
342 1255 ▶sub server_name {
Subroutine server_name: called CC=3 SCAR=11.0
Pod documented
343 1255 my ($name) = @_;
344  
345 1255 ▶ $name = "default" unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 239 1016 1255
346  
347 1255 ▶ $name =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/u or die "Invalid MCP server name '${name}'\n" 1254 1 1255
348 or die "Invalid MCP server name '$name'\n";
349  
350 1254 $name
351 }
352  
353 13 ▶sub credential {
Subroutine credential: called CC=3 SCAR=11.0
Pod documented
354 13 my ($self, $purpose, $name, $username) = @_;
355  
356 13 ▶ $username = "" unless defined $username;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $username 5 8 13
357  
358 13 ▶ $purpose eq "token"
Branch: 1 branch
BranchTrueFalseTotal
$purpose eq "token" or die "Unknown credential purpose '${purpose}'\n" 12 1 13
359 or die "Unknown credential purpose '$purpose'\n";
360  
361 12 my $credential = MIME::Base64::encode_base64 (
362 $self->derive_secret (
363 32,
364 $purpose,
365 (AnyEvent::MCP::utf8enc program_name),
366 (AnyEvent::MCP::utf8enc $name),
367 (AnyEvent::MCP::utf8enc $username)
368 ), ""
369 );
370 12 $credential =~ y!+/=!-_!d;
371  
372 12 $credential
373 }
374  
375 104 ▶sub users {
Subroutine users: called CC=8 SCAR=21.8
Pod documented
376 104 my ($self, $name, $program) = @_;
377  
378 104 $name = server_name $name;
379 104 ▶ $program //= program_name;
Condition33%1/3
$program //= program_name()
l!l&&r!l&&!r
10400
MC/DC0%0/2
$program //= program_name()
$program program_name()
Truth table33%1/3
A //= B
A= $programB= program_name()
ABresult
1X1
011
000
380 104 my %users = (default => { scope => "global", generation => 1 });
381  
382 104 for my $entry (
383 ["global", $self->{section}{global}],
384 ["program", $self->{section}{"program $program"}],
385 ["instance", $self->{section}{"instance $name"}],
386 ) {
387 312 my ($scope, $section) = @$entry;
388 312 ▶ next unless $section;
Branch: 1 branch
BranchTrueFalseTotal
unless $section 84 228 312
389  
390 228 for my $key (grep /\Auser\./, keys %$section) {
391 58 ▶ my ($user) = $key =~ /\Auser\.([A-Za-z_][A-Za-z0-9_-]*)\z/
Branch: 1 branch
BranchTrueFalseTotal
my($user) = $key =~ /\Auser\.([A-Za-z_][A-Za-z0-9_-]*)\z/u or die "Invalid MCP user '${key}'\n" 58 0 58
392 or die "Invalid MCP user '$key'\n";
393 58 my $generation = $section->{$key};
394 58 ▶ $generation =~ /\A-?[1-9][0-9]*\z/ && length $generation < 18
Branch: 1 branch
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
Condition33%1/3
$generation =~ /\A-?[1-9][0-9]*\z/u and length $generation < 18
!ll&&!rl&&r
0058
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
395 or die "Invalid MCP user generation for '$key'\n";
396 58 $users{$user} = { scope => $scope, generation => $generation };
397 }
398 }
399  
400 104 \%users
401 }
402  
403 94 ▶sub user_credentials {
Subroutine user_credentials: called CC=5 SCAR=16.2
Pod documented
404 94 my ($self, $name, $program) = @_;
405  
406 94 $name = server_name $name;
407 94 ▶ $program //= program_name;
Condition66%2/3
$program //= program_name()
l!l&&r!l&&!r
9850
MC/DC0%0/2
$program //= program_name()
$program program_name()
Truth table66%2/3
A //= B
A= $programB= program_name()
ABresult
1X1
011
000
408 94 my $users = $self->users ($name, $program);
409 94 my @credentials;
410  
411 94 ▶ for my $user (sort { ($a ne "default") <=> ($b ne "default") || $a cmp $b } keys %$users) {
Condition33%1/3
($a ne 'default') <=> ($b ne 'default') || $a cmp $b
l!l&&r!l&&!r
2100
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
412 115 my $info = $users->{$user};
413 115 ▶ next unless $info->{generation} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"generation"} > 0 4 111 115
414 my $token = MIME::Base64::encode_base64 ($self->derive_secret (
415 32, "user-token-v1", AnyEvent::MCP::utf8enc ($program),
416 AnyEvent::MCP::utf8enc ($name), $info->{scope},
417 111 AnyEvent::MCP::utf8enc ($user), "$info->{generation}"
418 ), "");
419 111 $token =~ y!+/=!-_!d;
420 111 push @credentials, [$user, "$info->{scope}:$user", $token];
421 }
422  
423 \@credentials
424 94 }
425  
426 80 ▶sub connect_user {
Subroutine connect_user: called CC=5 SCAR=18.4
Pod documented
427 80 my ($self, $name, $program) = @_;
428  
429 80 $name = server_name $name;
430 80 ▶ $program //= program_name;
Condition66%2/3
$program //= program_name()
l!l&&r!l&&!r
1790
MC/DC0%0/2
$program //= program_name()
$program program_name()
Truth table66%2/3
A //= B
A= $programB= program_name()
ABresult
1X1
011
000
431 my $user = $self->{section}{"instance $name"}{connect_user}
432 // $self->{section}{"program $program"}{connect_user}
433 80 ▶ // $self->{section}{global}{connect_user} // "default";
Condition37%3/8
(($self->{"section"}{"instance $name"}{"connect_user"} // $self->{"section"}{"program $program"}{"connect_user"}) // $self->{"section"}{"global"}{"connect_user"}) // "default"
l!l
080
($self->{"section"}{"instance $name"}{"connect_user"} // $self->{"section"}{"program $program"}{"connect_user"}) // $self->{"section"}{"global"}{"connect_user"}
l!l&&r!l&&!r
0080
$self->{"section"}{"instance $name"}{"connect_user"} // $self->{"section"}{"program $program"}{"connect_user"}
l!l&&r!l&&!r
0080
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
434 80 server_name $user
435 }
436  
437 298 ▶sub http_getopt {
Subroutine http_getopt: called CC=1 SCAR=0.0
Pod documented
438 298 my ($name, $bind, $print_url, $tokens) = @_;
439  
440 298 "name=s" => $name, "bind=s" => $bind, "print-url|print-urls:s" => $print_url, "token=s@" => $tokens
441 }
442  
443 166 ▶sub auto_rules {
Subroutine auto_rules: called CC=7 SCAR=19.5
Pod documented
444 166 my ($self, $name) = @_;
445 my %settings = (
446 166 ▶ %{$self->{section}{"program " . program_name ()} || {}},
Condition100%2/2
$self->{'section'}{'program ' . program_name()} || {}
l!l
12442
MC/DC100%1/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
447 166 ▶ %{$self->{section}{"instance " . server_name ($name)} || {}},
Condition100%2/2
$self->{'section'}{'instance ' . server_name($name)} || {}
l!l
46120
MC/DC100%1/1
$self->{'section'}{'instance ' . server_name($name)} || {}
$self->{'section'}{'instance ' . server_name($name)}
Truth table100%2/2
A
A= $self->{'section'}{'instance ' . server_name($name)}
Aresult
11
00
448 );
449 166 my @rules;
450  
451 166 for my $key (sort grep /\Aauto-rule\./, keys %settings) {
452 19 ▶ $key =~ /\Aauto-rule\.(.+)\z/
Branch: 1 branch
BranchTrueFalseTotal
$key =~ /\Aauto-rule\.(.+)\z/u or die "Invalid automatic permission rule key '${key}'\n" 19 0 19
453 or die "Invalid automatic permission rule key '$key'\n";
454 19 my $value = $settings{$key};
455 19 ▶ next unless length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless length $value 4 15 19
456 15 ▶ my ($decision, $spec) = $value =~ /\A(allow|deny)\s+(\S+)\z/
Branch: 1 branch
BranchTrueFalseTotal
() = $value =~ /\A(allow|deny)\s+(\S+)\z/u or die "Invalid configured automatic permission rule '${key}'\n" 14 1 15
457 or die "Invalid configured automatic permission rule '$key'\n";
458 14 push @rules, [$decision, $spec];
459 }
460  
461 @rules
462 165 }
463  
464 95 ▶sub server_port {
Subroutine server_port: called CC=1 SCAR=0.0
Pod documented
465 95 my ($self, $name) = @_;
466  
467 95 $name = server_name $name;
468  
469 95 16384 + (
470 (unpack "n", $self->derive_secret (
471 2,
472 "port",
473 (AnyEvent::MCP::utf8enc program_name),
474 (AnyEvent::MCP::utf8enc $name))
475 ) & 16383)
476 }
477  
478 89 ▶sub http_settings {
Subroutine http_settings: called CC=18 SCAR=29.8
Pod documented
479 89 my ($self, $name, $bind) = @_;
480  
481 89 $name = server_name $name;
482 my $settings = {
483 89 %{ $self->{section}{"program " . program_name ()} },
484 89 %{ $self->{section}{"instance $name"} },
485 };
486 89 my $port = $self->server_port ($name);
487 89 ▶ my $configured = $settings->{bind} // "127.0.0.1:$port";
Condition100%2/2
$settings->{"bind"} // "127.0.0.1:$port"
l!l
2960
MC/DC100%1/1
$settings->{"bind"} // "127.0.0.1:$port"
$settings->{"bind"}
Truth table100%2/2
A
A= $settings->{"bind"}
Aresult
11
00
488 89 ▶ $configured = "127.0.0.1$configured" if $configured =~ /\A:[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $configured =~ /\A:[0-9]+\z/u 0 89 89
489 89 my ($configured_host, $configured_port) = AnyEvent::Socket::parse_hostport $configured, $port;
490 89 ▶ defined $configured_host && defined $configured_port
Branch: 1 branch
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" 89 0 89
Condition33%3/9
defined $configured_host and defined $configured_port and $configured_port =~ /\A[0-9]+\z/u and $configured_port <= 65535
!ll&&!rl&&r
0089
defined $configured_host and defined $configured_port and $configured_port =~ /\A[0-9]+\z/u
!ll&&!rl&&r
0089
defined $configured_host and defined $configured_port
!ll&&!rl&&r
0089
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
491 && $configured_port =~ /\A[0-9]+\z/ && $configured_port <= 65535
492 or die "Invalid MCP bind address '$configured'\n";
493  
494 89 ▶ $bind = $configured unless defined $bind;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bind 34 55 89
495 89 ▶ $bind = "127.0.0.1$bind" if $bind =~ /\A:[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $bind =~ /\A:[0-9]+\z/u 24 65 89
496  
497 89 my ($host, $service) = AnyEvent::Socket::parse_hostport $bind, $configured_port;
498 89 ▶ defined $host && defined $service && $service =~ /\A[0-9]+\z/ && $service <= 65535
Branch: 1 branch
BranchTrueFalseTotal
defined $host and defined $service and $service =~ /\A[0-9]+\z/u and $service <= 65535 or die "Invalid MCP bind address '${bind}'\n" 86 3 89
Condition77%7/9
defined $host and defined $service and $service =~ /\A[0-9]+\z/u and $service <= 65535
!ll&&!rl&&r
1286
defined $host and defined $service and $service =~ /\A[0-9]+\z/u
!ll&&!rl&&r
1088
defined $host and defined $service
!ll&&!rl&&r
1088
MC/DC50%2/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 table60%3/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
499 or die "Invalid MCP bind address '$bind'\n";
500  
501 my %kv = (
502 86 ▶ (map { exists $settings->{$_} ? ($_ => $settings->{$_}) : () }
Branch: 1 branch
BranchTrueFalseTotal
exists $settings->{$_} ? : 15 415 430
503 qw(path url_host url_scheme tls_cert tls_key)),
504 name => $name,
505 host => $host,
506 port => $service,
507 );
508  
509 86 ▶ unless (AnyEvent::Socket::parse_address $host) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&AnyEvent::Socket::parse_address($host)) 1 85 86
510 1 my $cv = AnyEvent->condvar;
511 1 AnyEvent::Socket::resolve_sockaddr $host, $service, "tcp", 0, 0, $cv;
512 1 my ($resolved) = $cv->recv;
513 1 ▶ $resolved or die "Cannot resolve MCP bind host '$host'\n";
Branch: 1 branch
BranchTrueFalseTotal
$resolved or die "Cannot resolve MCP bind host '${host}'\n" 1 0 1
514  
515 1 my (undef, $address) = AnyEvent::Socket::unpack_sockaddr $resolved->[3];
516 1 ▶ $kv{url_host} //= $host;
Condition33%1/3
$kv{"url_host"} //= $host
l!l&&r!l&&!r
010
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
517 1 $kv{host} = AnyEvent::Socket::format_address $address;
518 }
519  
520 86 ▶ if (exists $settings->{tokens}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $settings->{'tokens'}) { } 16 70 86
521 16 $kv{tokens} = [split /\s*,\s*/, $settings->{tokens}, -1];
522 16 $kv{connect_tokens} = $kv{tokens};
523 } else {
524 70 my $credentials = $self->user_credentials ($name);
525 70 $kv{tokens} = [map $_->[2], @$credentials];
526 70 $kv{token_users} = { map { $_->[2] => $_->[1] } @$credentials };
527 70 my $connect = $self->connect_user ($name);
528 70 $kv{connect_tokens} = [map $_->[2], grep $_->[0] eq $connect, @$credentials];
529 }
530  
531 86 %kv
532 }
533  
534 32 ▶sub http_url {
Subroutine http_url: called CC=10 SCAR=23.1
Pod documented
535 32 my ($self, $name, $bind, %options) = @_;
536  
537 32 $name = server_name $name;
538  
539 # only a derived token depends on the master secret surviving this process.
540 32 ▶ if (($options{auth} || "") ne "none"
Branch: 1 branch
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 29 32
Condition85%12/14
($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
2903
($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
2903
($options{"auth"} || "") ne "none" and not exists $options{"tokens"} and not exists $self->{"section"}{"instance $name"}{"tokens"}
!ll&&!rl&&r
2363
($options{"auth"} || "") ne "none" and not exists $options{"tokens"}
!ll&&!rl&&r
1229
$options{'auth'} || ''
l!l
131
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
541 && !exists $options{tokens}
542 && !exists $self->{section}{"instance $name"}{tokens}
543 && !exists $self->{section}{"program " . program_name ()}{tokens}
544 && !exists $self->{section}{global}{tokens}) {
545 3 my $stored = (ref $self)->new (file => $self->{file});
546 $stored->{section}{global}{secret} eq $self->{section}{global}{secret}
547 3 ▶ or die "MCP master secret is not persistently stored in $self->{file}\n";
Branch: 1 branch
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
548 }
549  
550 31 require AnyEvent::MCP::Server::HTTP;
551 31 my %http = $self->http_settings ($name, $bind);
552 31 ▶ delete $http{tokens} if ($options{auth} || "") eq "none";
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"auth"} || "") eq "none" 1 30 31
Condition100%2/2
$options{'auth'} || ''
l!l
130
MC/DC100%1/1
$options{'auth'} || ''
$options{'auth'}
Truth table100%2/2
A
A= $options{'auth'}
Aresult
11
00
553  
554 31 AnyEvent::MCP::Server::HTTP->new (%http, %options)->url
555 }
556  
557  
558 349 1
« blib/lib/AnyEvent/MCP/Extension.pm ↑ Summary blib/lib/AnyEvent/MCP/Client/JSONL.pm »