bin/anyevent-mcp

filter: Statementstmt 83.5% 56 / 67 Branchbran 66.0% 33 / 50 Conditioncond 64.2% 27 / 42 MC/DCmcdc 30.4% 7 / 23 Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 68.6% 129 / 188 CC 19 SCAR 27.6
CC
19
Cov
73%
change_user
19.7
BEGIN
1.0
BEGIN
1.0
CRAP 15.8
« blib/lib/AnyEvent/MCP/Server/HTTP.pm ↑ Summary blib/lib/AnyEvent/MCP/Client.pm »
1 #!/opt/bin/perl
2  
3 =head1 NAME
4  
5 anyevent-mcp - initialize MCP configuration and manage users and permissions
6  
7 =head1 SYNOPSIS
8  
9 anyevent-mcp init
10 anyevent-mcp manage-rules fs-default
11 anyevent-mcp create-user chatgpt
12 anyevent-mcp revoke-user chatgpt
13 anyevent-mcp edit-rules-file /path/to/rules.ini
14  
15 =head1 DESCRIPTION
16  
17 C<anyevent-mcp> sets up the shared AnyEvent::MCP configuration and manages
18 permission rules saved by its servers. Use it to review permissions you have
19 allowed or denied permanently, remove a rule, or change the order in which
20 rules are checked.
21  
22 C<manage-rules> opens a graphical rule manager; C<edit-rules> opens the
23 rule file in your text editor. Both take a rule profile name. For example,
24 C<anyevent-mcp manage-rules fs-default> manages the default filesystem
25 server's rules. The corresponding C<*-file> commands accept a pathname
26 when an application stores its rules elsewhere.
27  
28 C<init> ensures the private global configuration contains a persistent
29 master secret, from which servers derive their connection credentials.
30 You normally do not need to run it: servers initialize their global
31 configuration automatically. Repeating C<init> leaves an existing secret
32 unchanged.
33  
34 See L<AnyEvent::MCP::Intro> for setting up and connecting to the servers,
35 L<AnyEvent::MCP/GLOBAL CONFIGURATION> for the configuration file format,
36 and L<AnyEvent::MCP::Server::Permissions/RULE FILES> for permission rules.
37  
38 =head1 COMMANDS
39  
40 =over
41  
42 =item init
43  
44 Ensure that the private global configuration and master secret exist.
45 An existing secret is preserved; missing secrets are generated and saved.
46 C<--config FILE> selects a different configuration file for this command.
47  
48 =item manage-rules NAME
49  
50 Open the YAD rule deletion and reordering interface for the named profile.
51 For a filesystem server started with C<--name code>, use profile C<fs-code>.
52  
53 =item edit-rules NAME
54  
55 Open the named profile's rule file in a text editor.
56  
57 =item manage-rules-file $file
58  
59 Open the rule manager for the explicit pathname C<$file>.
60  
61 =item edit-rules-file $file
62  
63 Open C<$file> in a text editor. Use these explicit-file commands for
64 applications whose rule files do not use the named-profile convention.
65  
66 =item create-user USER
67  
68 Enable a new named user; rotate a revoked user at the next generation.
69 Use --program or --instance for a scoped override.
70  
71 =item revoke-user USER
72  
73 Disable a user, preserving the generation for later rotation. In a narrower
74 scope, a negative definition also blocks inherited credentials. Implicit
75 global:default is revocable. Restart affected servers after changes; these
76 commands do not print secrets. Use server --print-url[=USER] for URLs.
77  
78 User commands rewrite the selected INI file in canonical form. Existing
79 settings are retained, but comments and formatting are not preserved.
80  
81 =back
82  
83 Named profiles use
84 C<anyevent-mcp/rules/NAME.ini> in the user configuration directory.
85 Editing uses C<$VISUAL>, C<$EDITOR>, C<sensible-editor> or C<vi>.
86 For server URLs, use each server's C<--print-url> option.
87  
88 =head1 OPTIONS
89  
90 =over
91  
92 =item --config FILE
93  
94 Use FILE instead of default global configuration
95 (C<$XDG_CONFIG_HOME/anyevent-mcp/global.ini>, or
96 C<~/.config/anyevent-mcp/global.ini>). Relevant to C<init> and user
97 management; rule management uses its own profile or pathname.
98  
99 =item --program NAME
100  
101 Select C<[program NAME]> for user management.
102  
103 =item --instance NAME
104  
105 Select C<[instance NAME]> for user management. Cannot combine with --program.
106  
107 =item --help
108  
109 Print command synopsis, available commands and configuration defaults,
110 then exit without running a command.
111  
112 =back
113  
114 =cut
115  
116 19 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
117  
118 19 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
119  
120 19 ▶use AnyEvent::MCP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
121 19 ▶use AnyEvent::MCP::Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
122  
123 2 ▶sub usage {
Subroutine usage: called CC=1 SCAR=0.0
124 2 print <<'EOF';
125 Usage: anyevent-mcp [OPTIONS] COMMAND [ARGUMENTS]
126  
127 manage-rules NAME delete/reorder rules in YAD
128 edit-rules NAME edit rules with VISUAL, EDITOR, sensible-editor, vi
129 manage-rules-file FILE manage an explicit rules file
130 edit-rules-file FILE edit an explicit rules file
131 init create a private configuration and master secret
132 create-user USER enable or rotate a managed user
133 revoke-user USER revoke a managed user
134  
135 Options (before COMMAND):
136 --config FILE use a different global configuration file
137 --program NAME select [program NAME] for user management
138 --instance NAME select [instance NAME] for user management
139 --help show this help
140  
141 User definitions inherit from [global] through [program NAME] to
142 [instance NAME]; narrower scopes override wider ones. Without --program
143 or --instance, user commands modify [global]. Revoked users remain
144 disabled until recreated with a new token generation. The implicit
145 global:default user is enabled initially.
146  
147 Examples:
148 anyevent-mcp create-user chatgpt
149 anyevent-mcp --program mcp-fs create-user backup
150 anyevent-mcp --instance private revoke-user alice
151 anyevent-mcp revoke-user chatgpt
152 mcp-multibroker --print-urls # list all enabled user URLs
153 mcp-multibroker --print-url=chatgpt # show just chatgpt's URL
154  
155 Server changes take effect after restart. The selected connect_user
156 (default: default) authenticates auxiliary browser/Vim/reverse connections.
157  
158 Rule profiles are stored under $XDG_CONFIG_HOME/anyevent-mcp, falling back
159 to ~/.config/anyevent-mcp/rules (e.g. manage-rules fs-default).
160  
161 Configuration defaults to ~/.config/anyevent-mcp/global.ini, or the
162 corresponding location under XDG_CONFIG_HOME. Use --help for this text.
163  
164 A missing master secret is generated and saved automatically when a server
165 loads its configuration. Configuration format: see AnyEvent::MCP.
166 EOF
167 }
168  
169 11 ▶sub change_user {
Subroutine change_user: called CC=19 SCAR=29.8
170 11 my ($file, $section, $user, $command) = @_;
171  
172 11 my $lock = AnyEvent::MCP::file_lock $file;
173 11 my $config = AnyEvent::MCP::Config->new (file => $file)->load ($lock);
174 11 ▶ my $values = $config->{section}{$section} ||= {};
Condition100%2/2
$config->{"section"}{$section} ||= {}
l!l
92
MC/DC100%1/1
$config->{"section"}{$section} ||= {}
$config->{"section"}{$section}
Truth table100%2/2
A
A= $config->{"section"}{$section}
Aresult
11
00
175 11 my $key = "user.$user";
176 11 my $generation = $values->{$key};
177 11 ▶ if (defined $generation) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $generation) 5 6 11
178 5 ▶ $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 $key generation '${generation}'\n" 5 0 5
Condition33%1/3
$generation =~ /\A-?[1-9][0-9]*\z/u and length $generation < 18
!ll&&!rl&&r
005
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
179 or die "Invalid $key generation '$generation'\n";
180 5 $generation = 0 + $generation;
181 }
182  
183 11 ▶ if ($command eq "create-user") {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq 'create-user') { } 8 3 11
184 8 ▶ die "$key already enabled in [$section]\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $generation and $generation > 0 or not defined $generation and $section eq "global" and $user eq "default" 2 6 8
Condition100%12/12
defined $generation and $generation > 0 or not defined $generation and $section eq "global" and $user eq "default"
l!l&&r!l&&!r
116
defined $generation and $generation > 0
!ll&&!rl&&r
431
not defined $generation and $section eq "global" and $user eq "default"
!ll&&!rl&&r
421
not defined $generation and $section eq "global"
!ll&&!rl&&r
313
MC/DC80%4/5
defined $generation and $generation > 0 or not defined $generation and $section eq "global" and $user eq "default"
defined $generation $generation > 0 not defined $generation $section eq "global" $user eq "default"
Truth table55%5/9
A and B or not(C) and D and E
A= defined $generationB= $generation > 0C= not defined $generationD= $section eq "global"E= $user eq "default"
ABCDEresult
11XXX1
0X1111
101111
0X0XX0
0X10X0
0X1100
100XX0
1010X0
101100
185 if defined $generation && $generation > 0 || !defined $generation && $section eq "global" && $user eq "default";
186 6 ▶ $generation = defined $generation ? -$generation + 1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $generation ? : 3 3 6
187 } else {
188 3 ▶ die "$key already revoked in [$section]\n" if defined $generation && $generation < 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $generation and $generation < 0 0 3 3
Condition66%2/3
defined $generation and $generation < 0
!ll&&!rl&&r
210
MC/DC0%0/2
defined $generation and $generation < 0
defined $generation $generation < 0
Truth table66%2/3
A and B
A= defined $generationB= $generation < 0
ABresult
0X0
100
111
189 3 ▶ if (!defined $generation) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $generation) { } 2 1 3
190 2 ▶ ($section ne "global" || $user eq "default")
Branch: 1 branch
BranchTrueFalseTotal
$section ne "global" or $user eq "default" or die "$key is not enabled\n" 2 0 2
Condition66%2/3
$section ne "global" or $user eq "default"
l!l&&r!l&&!r
110
MC/DC0%0/2
$section ne "global" or $user eq "default"
$section ne "global" $user eq "default"
Truth table66%2/3
A or B
A= $section ne "global"B= $user eq "default"
ABresult
1X1
011
000
191 or die "$key is not enabled\n";
192 2 $generation = -1;
193 } else {
194 1 $generation = -$generation;
195 }
196 }
197  
198 9 $values->{$key} = $generation;
199 9 $config->save;
200 9 ▶ close $lock or die "$file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $lock or die "${file}: $!\n" 9 0 9
201 9 ▶ print "$section: $user generation " . abs ($generation)
Branch: 1 branch
BranchTrueFalseTotal
$generation > 0 ? : 6 3 9
202 . ($generation > 0 ? " enabled\n" : " revoked\n");
203 }
204  
205 19 Getopt::Long::Configure ("no_auto_abbrev", "no_ignore_case", "require_order");
206 19 ▶Getopt::Long::GetOptions
Branch: 1 branch
BranchTrueFalseTotal
&Getopt::Long::GetOptions("config=s", \my $file, "program=s", \my $program, "instance=s", \my $instance, "help", \my $help) or exit 2 19 0 19
207 "config=s" => \my $file,
208 "program=s" => \my $program,
209 "instance=s" => \my $instance,
210 "help" => \my $help,
211 or exit 2;
212  
213 19 ▶if ($help) {
Branch: 1 branch
BranchTrueFalseTotal
if ($help) 2 17 19
214 2 usage;
215 2 exit 0;
216 }
217  
218 17 my $command = shift @ARGV;
219 17 ▶unless (defined $command) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $command) 0 17 17
220 0 usage;
221 0 exit 2;
222 }
223  
224 17 @ARGV = map AnyEvent::MCP::utf8dec $_, @ARGV;
225  
226 eval {
227 17 ▶ if ($command =~ /\A(?:manage-rules|edit-rules)(?:-file)?\z/ && @ARGV == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($command =~ /\A(?:manage-rules|edit-rules)(?:-file)?\z/u and @ARGV == 1) { } 0 17 17
elsif ($command eq 'init' and not @ARGV) { } 1 16 17
elsif ($command =~ /\A(?:create-user|revoke-user)\z/u and @ARGV == 1) { } 11 5 16
Condition33%1/3
$command =~ /\A(?:manage-rules|edit-rules)(?:-file)?\z/u and @ARGV == 1
!ll&&!rl&&r
1700
MC/DC0%0/2
$command =~ /\A(?:manage-rules|edit-rules)(?:-file)?\z/u and @ARGV == 1
$command =~ /\A(?:manage-rules|edit-rules)(?:-file)?\z/u @ARGV == 1
Truth table33%1/3
A and B
A= $command =~ /\A(?:manage-rules|edit-rules)(?:-file)?\z/uB= @ARGV == 1
ABresult
0X0
100
111
228 0 my $path = $ARGV[0];
229 0 ▶ if ($command !~ /-file\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($command =~ /-file\z/u) 0 0 0
230 0 ▶ $path =~ /\A[A-Za-z0-9][A-Za-z0-9_.-]*\z/
Branch: 1 branch
BranchTrueFalseTotal
$path =~ /\A[A-Za-z0-9][A-Za-z0-9_.-]*\z/u or die "Invalid permission rule profile name\n" 0 0 0
231 or die "Invalid permission rule profile name\n";
232 0 ▶ my $config_home = $ENV{XDG_CONFIG_HOME} || ($ENV{HOME} || die "HOME is not set\n") . "/.config";
Condition0%0/4
$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/DC0%0/1
$ENV{'XDG_CONFIG_HOME'} || ($ENV{'HOME'} || die("HOME is not set\n")) . '/.config'
$ENV{'XDG_CONFIG_HOME'}
Truth table0%0/2
A
A= $ENV{'XDG_CONFIG_HOME'}
Aresult
11
00
233 0 ▶ $config_home =~ m{\A/} or die "XDG_CONFIG_HOME must be an absolute path\n";
Branch: 1 branch
BranchTrueFalseTotal
$config_home =~ m[\A/]u or die "XDG_CONFIG_HOME must be an absolute path\n" 0 0 0
234 0 $path = "$config_home/anyevent-mcp/rules/$path.ini";
235 }
236 0 ▶ my $action = $command =~ /\Amanage-/ ? "--manage-rules" : "--edit-rules";
Branch: 1 branch
BranchTrueFalseTotal
$command =~ /\Amanage-/u ? : 0 0 0
237 0 exec "anyevent-mcp-askpermission", $action, AnyEvent::MCP::utf8enc $path;
238 0 die "Cannot start permission rule tool: $!\n";
239 ▶ } elsif ($command eq "init" && !@ARGV) {
Condition66%2/3
$command eq "init" and not @ARGV
!ll&&!rl&&r
1601
MC/DC50%1/2
$command eq "init" and not @ARGV
$command eq "init" not @ARGV
Truth table66%2/3
A and not(B)
A= $command eq "init"B= not @ARGV
ABresult
0X0
100
111
240 1 ▶ my $config = AnyEvent::MCP::Config::global_config (defined $file ? (file => $file) : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $file ? : 1 0 1
241 1 print "Initialized $config->{file}\n";
242 ▶ } elsif ($command =~ /\A(?:create-user|revoke-user)\z/ && @ARGV == 1) {
Condition66%2/3
$command =~ /\A(?:create-user|revoke-user)\z/u and @ARGV == 1
!ll&&!rl&&r
5011
MC/DC50%1/2
$command =~ /\A(?:create-user|revoke-user)\z/u and @ARGV == 1
$command =~ /\A(?:create-user|revoke-user)\z/u @ARGV == 1
Truth table66%2/3
A and B
A= $command =~ /\A(?:create-user|revoke-user)\z/uB= @ARGV == 1
ABresult
0X0
100
111
243 11 ▶ !defined $program || !defined $instance or die "--program and --instance are exclusive\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $program and defined $instance and die "--program and --instance are exclusive\n" 0 11 11
Condition66%2/3
defined $program and defined $instance
!ll&&!rl&&r
1010
MC/DC0%0/2
defined $program and defined $instance
defined $program defined $instance
Truth table66%2/3
A and B
A= defined $programB= defined $instance
ABresult
0X0
100
111
244 11 my $user = AnyEvent::MCP::Config::user_name ($ARGV[0]);
245 11 ▶ my $scope = defined $program ? "program " . AnyEvent::MCP::instance_name ($program)
Branch: 2 branches
BranchTrueFalseTotal
defined $program ? : 1 10 11
defined $instance ? : 2 8 10
246 : defined $instance ? "instance " . AnyEvent::MCP::instance_name ($instance)
247 : "global";
248 11 ▶ my $config_file = $file // AnyEvent::MCP::Config::_file ();
Condition33%1/3
$file // AnyEvent::MCP::Config::_file()
l!l&&r!l&&!r
1100
MC/DC0%0/2
$file // AnyEvent::MCP::Config::_file()
$file AnyEvent::MCP::Config::_file()
Truth table33%1/3
A // B
A= $fileB= AnyEvent::MCP::Config::_file()
ABresult
1X1
011
000
249 11 AnyEvent::MCP::Config->new (file => $config_file)->load->ensure_secret;
250 11 change_user $config_file, $scope, $user, $command;
251 } else {
252 5 die "Invalid command or arguments; use --help\n";
253 }
254 10 1
255 17 ▶} or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { if ($command =~ /\A(?:manage-rules|edit-rules)(?:-file)?\z/u and @ARGV == 1) { my $path = $ARGV[0]; unless ($command =~ /-file\z/u) { $path =~ /\A[A-Za-z0-9][A-Za-z0-9_.-]*\z/u or die "Invalid permission rule profile name\n"; my $config_home = $ENV{"XDG_CONFIG_HOME"} || ($ENV{"HOME"} || die("HOME is not set\n")) . '/.config'; $config_home =~ m[\A/]u or die "XDG_CONFIG_HOME must be an absolute path\n"; $path = "$config_home/anyevent-mcp/rules/$path.ini"; } ; my $action = $command =~ /\Amanage-/u ? "--manage-rules" : "--edit-rules"; exec "anyevent-mcp-askpermission", $action, &AnyEvent::MCP::utf8enc($path); die "Cannot start permission rule tool: $!\n"; } elsif ($command eq "init" and not @ARGV) { my $config = AnyEvent::MCP::Config::global_config(defined $file ? ("file", $file) : ()); print qq[Initialized $config->{"file"}\n]; } elsif ($command =~ /\A(?:create-user|revoke-user)\z/u and @ARGV == 1) { defined $program and defined $instance and die "--program and --instance are exclusive\n"; my $user = AnyEvent::MCP::Config::user_name($ARGV[0]); my $scope = defined $program ? 'program ' . AnyEvent::MCP::instance_name($program) : (defined $instance ? 'instance ' . AnyEvent::MCP::instance_name($instance) : "global"); my $config_file = $file // AnyEvent::MCP::Config::_file(); "AnyEvent::MCP::Config"->new("file", $config_file)->load->ensure_secret; change_user($config_file, $scope, $user, $command); } else { die "Invalid command or arguments; use --help\n"; } ; 1 } } or do { print STDERR $@; exit 1 } 10 7 17
256 7 print STDERR $@;
257 7 exit 1;
258 };
259  
« blib/lib/AnyEvent/MCP/Server/HTTP.pm ↑ Summary blib/lib/AnyEvent/MCP/Client.pm »