Plugin.pm untested

filter: Statementstmt 0.0% 0 / 98 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 201 CC 27 SCAR 56.9
CC
27
Cov
0%
parse_manifest
506.0
_as_object
12.0
_check_keys
6.0
CRAP 295.2
1 package Praxo::Plugin;
2  
3 use strict;
4 use warnings;
5 use Exporter 'import';
6 use Scalar::Util qw(looks_like_number);
7 use Praxo::Json ();
8 use Praxo::Errors qw(praxo_error);
9  
10 our @EXPORT_OK = qw(parse_manifest);
11  
12 my @KINDS = qw(
13 tool.provider.v1 model.provider.v1 policy.provider.v1
14 memory.provider.v1 sandbox.provider.v1
15 );
16 my %TOP_KEYS = map { $_ => 1 } qw(
17 schemaVersion id version kind entrypoint protocol capabilities permissions
18 limits isolation health signature sbom
19 );
20 my %ENTRYPOINT_KEYS = map { $_ => 1 } qw(type command module image args dir);
21 my %PERMISSION_KEYS = map { $_ => 1 } qw(filesystem network env);
22 my %LIMIT_KEYS = map { $_ => 1 } qw(memoryMB cpuMillis timeoutMs maxInflight);
23 my %HEALTH_KEYS = map { $_ => 1 } qw(method intervalMs);
24 my %SIGNATURE_KEYS = map { $_ => 1 } qw(algorithm keyId);
25 my %KIND_LOOKUP = map { $_ => 1 } @KINDS;
26  
27 sub parse_manifest {
28 my ($raw) = @_;
29 praxo_error('PLUGIN_INVALID', 'plugin: manifest is empty')
30 unless defined($raw) && $raw =~ /\S/;
31 my $value = eval { Praxo::Json::parse($raw) };
32 praxo_error('PLUGIN_INVALID', "plugin: manifest: $@") if $@;
33 praxo_error('PLUGIN_INVALID', 'plugin: manifest must be an object')
34 unless ref($value) eq 'HASH';
35 _check_keys($value, \%TOP_KEYS, 'manifest');
36  
37 my $schema_version = _as_int($value->{schemaVersion}, 'schemaVersion');
38 praxo_error('PLUGIN_INVALID', "plugin: manifest: schemaVersion = $schema_version, want 1")
39 unless $schema_version == 1;
40 my $id = _as_string($value->{id}, 'id');
41 praxo_error('PLUGIN_INVALID', "plugin: manifest: id $id is not reverse-DNS")
42 unless $id =~ /^(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
43 my $version = _as_string($value->{version}, 'version');
44 praxo_error('PLUGIN_INVALID', "plugin: manifest: version $version is not semver")
45 unless $version =~ /^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/;
46 my $kind = _as_string($value->{kind}, 'kind');
47 praxo_error('PLUGIN_INVALID', "plugin: manifest: kind $kind is not a known contract")
48 unless $KIND_LOOKUP{$kind};
49 my $protocol = _as_string($value->{protocol}, 'protocol');
50 praxo_error('PLUGIN_INVALID', "plugin: manifest: protocol = $protocol, want jsonrpc-2")
51 unless $protocol eq 'jsonrpc-2';
52  
53 my $entrypoint = _as_object($value->{entrypoint}, 'entrypoint');
54 _check_keys($entrypoint, \%ENTRYPOINT_KEYS, 'entrypoint');
55 my $entry_type = _as_string($entrypoint->{type}, 'entrypoint.type');
56 my $required = $entry_type eq 'process' ? 'command'
57 : $entry_type eq 'wasm' ? 'module'
58 : $entry_type eq 'container' ? 'image'
59 : undef;
60 praxo_error('PLUGIN_INVALID', "plugin: manifest: unknown entrypoint type $entry_type")
61 unless defined $required;
62 praxo_error('PLUGIN_INVALID', "plugin: manifest: $entry_type entrypoint requires $required")
63 unless _text_present($entrypoint->{$required});
64  
65 my $isolation = _as_string($value->{isolation}, 'isolation');
66 praxo_error('PLUGIN_INVALID', "plugin: manifest: isolation $isolation does not match entrypoint $entry_type")
67 unless $isolation eq $entry_type;
68  
69 my $permissions = _as_object($value->{permissions}, 'permissions');
70 _check_keys($permissions, \%PERMISSION_KEYS, 'permissions');
71 my $limits = _as_object($value->{limits}, 'limits');
72 _check_keys($limits, \%LIMIT_KEYS, 'limits');
73 for my $key (keys %$limits) {
74 praxo_error('PLUGIN_INVALID', 'plugin: manifest: limits must not be negative')
75 if _as_int($limits->{$key}, "limits.$key") < 0;
76 }
77 my $health = _as_object($value->{health}, 'health');
78 _check_keys($health, \%HEALTH_KEYS, 'health');
79  
80 if (exists $value->{signature}) {
81 my $signature = _as_object($value->{signature}, 'signature');
82 _check_keys($signature, \%SIGNATURE_KEYS, 'signature');
83 praxo_error('PLUGIN_INVALID', 'plugin: manifest: signature requires algorithm and keyId')
84 unless _text_present($signature->{algorithm}) && _text_present($signature->{keyId});
85 }
86  
87 my @capabilities;
88 if (exists $value->{capabilities}) {
89 praxo_error('PLUGIN_INVALID', 'plugin: manifest: capabilities must be an array')
90 unless ref($value->{capabilities}) eq 'ARRAY';
91 for my $item (@{ $value->{capabilities} }) {
92 push @capabilities, _as_string($item, 'capabilities');
93 }
94 }
95 praxo_error('PLUGIN_INVALID', 'plugin: manifest: capability must not be empty')
96 if grep { $_ eq '' } @capabilities;
97 my %seen;
98 for my $capability (@capabilities) {
99 praxo_error('PLUGIN_INVALID', 'plugin: manifest: duplicate capability')
100 if $seen{$capability}++;
101 }
102  
103 return {
104 schemaVersion => $schema_version,
105 id => $id,
106 version => $version,
107 kind => $kind,
108 entrypoint => $entrypoint,
109 protocol => $protocol,
110 capabilities => \@capabilities,
111 permissions => $permissions,
112 limits => $limits,
113 isolation => $isolation,
114 health => $health,
115 };
116 }
117  
118 sub _check_keys {
119 my ($value, $allowed, $where) = @_;
120 for my $key (keys %$value) {
121 praxo_error('PLUGIN_INVALID', "plugin: manifest: unknown field $key in $where")
122 unless $allowed->{$key};
123 }
124 }
125  
126 sub _as_object {
127 my ($value, $where) = @_;
128 return {} unless defined $value;
129 praxo_error('PLUGIN_INVALID', "plugin: manifest: $where must be an object")
130 unless ref($value) eq 'HASH';
131 return $value;
132 }
133  
134 sub _as_string {
135 my ($value, $where) = @_;
136 praxo_error('PLUGIN_INVALID', "plugin: manifest: $where must be a string")
137 unless defined($value) && !ref($value);
138 return "$value";
139 }
140  
141 sub _as_int {
142 my ($value, $where) = @_;
143 praxo_error('PLUGIN_INVALID', "plugin: manifest: $where must be an integer")
144 unless defined($value) && !ref($value) && looks_like_number($value);
145 return int($value);
146 }
147  
148 sub _text_present {
149 my ($value) = @_;
150 return defined($value) && !ref($value) && length("$value");
151 }
152  
153 1;