]>
git.proxmox.com Git - pve-common.git/blob - src/PVE/JSONSchema.pm
95529258f19c24535e09dae21dc54471da8b1d37
1 package PVE
:: JSONSchema
;
5 use Storable
; # for dclone
7 use Devel
:: Cycle
- quiet
; # todo: remove?
8 use PVE
:: Tools
qw(split_list $IPV6RE $IPV4RE ) ;
9 use PVE
:: Exception
qw(raise) ;
10 use HTTP
:: Status
qw(:constants) ;
11 use Net
:: IP
qw(:PROC) ;
16 register_standard_option
20 # Note: This class implements something similar to JSON schema, but it is not 100% complete.
21 # see: http://tools.ietf.org/html/draft-zyp-json-schema-02
22 # see: http://json-schema.org/
24 # the code is similar to the javascript parser from http://code.google.com/p/jsonschema/
26 my $standard_options = {};
27 sub register_standard_option
{
28 my ( $name, $schema ) = @_ ;
30 die "standard option ' $name ' already registered \n "
31 if $standard_options ->{ $name };
33 $standard_options ->{ $name } = $schema ;
36 sub get_standard_option
{
37 my ( $name, $base ) = @_ ;
39 my $std = $standard_options ->{ $name };
40 die "no such standard option ' $name ' \n " if ! $std ;
42 my $res = $base || {};
44 foreach my $opt ( keys %$std ) {
45 next if defined ( $res ->{ $opt });
46 $res ->{ $opt } = $std ->{ $opt };
52 register_standard_option
( 'pve-vmid' , {
53 description
=> "The (unique) ID of the VM." ,
54 type
=> 'integer' , format
=> 'pve-vmid' ,
58 register_standard_option
( 'pve-node' , {
59 description
=> "The cluster node name." ,
60 type
=> 'string' , format
=> 'pve-node' ,
63 register_standard_option
( 'pve-node-list' , {
64 description
=> "List of cluster node names." ,
65 type
=> 'string' , format
=> 'pve-node-list' ,
68 register_standard_option
( 'pve-iface' , {
69 description
=> "Network interface name." ,
70 type
=> 'string' , format
=> 'pve-iface' ,
71 minLength
=> 2 , maxLength
=> 20 ,
74 PVE
:: JSONSchema
:: register_standard_option
( 'pve-storage-id' , {
75 description
=> "The storage identifier." ,
76 type
=> 'string' , format
=> 'pve-storage-id' ,
79 PVE
:: JSONSchema
:: register_standard_option
( 'pve-config-digest' , {
80 description
=> 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.' ,
83 maxLength
=> 40 , # sha1 hex digest lenght is 40
86 PVE
:: JSONSchema
:: register_standard_option
( 'extra-args' , {
87 description
=> "Extra arguments as array" ,
89 items
=> { type
=> 'string' },
96 my ( $format, $code ) = @_ ;
98 die "JSON schema format ' $format ' already registered \n "
99 if $format_list ->{ $format };
101 $format_list ->{ $format } = $code ;
106 return $format_list ->{ $format };
109 # register some common type for pve
111 register_format
( 'string' , sub {}); # allow format => 'string-list'
113 register_format
( 'pve-configid' , \
& pve_verify_configid
);
114 sub pve_verify_configid
{
115 my ( $id, $noerr ) = @_ ;
117 if ( $id !~ m/^[a-z][a-z0-9_]+$/i ) {
118 return undef if $noerr ;
119 die "invalid configuration ID ' $id ' \n " ;
124 PVE
:: JSONSchema
:: register_format
( 'pve-storage-id' , \
& parse_storage_id
);
125 sub parse_storage_id
{
126 my ( $storeid, $noerr ) = @_ ;
128 if ( $storeid !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i ) {
129 return undef if $noerr ;
130 die "storage ID ' $storeid ' contains illegal characters \n " ;
136 register_format
( 'pve-vmid' , \
& pve_verify_vmid
);
137 sub pve_verify_vmid
{
138 my ( $vmid, $noerr ) = @_ ;
140 if ( $vmid !~ m/^[1-9][0-9]+$/ ) {
141 return undef if $noerr ;
142 die "value does not look like a valid VM ID \n " ;
147 register_format
( 'pve-node' , \
& pve_verify_node_name
);
148 sub pve_verify_node_name
{
149 my ( $node, $noerr ) = @_ ;
151 if ( $node !~ m/^([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?)$/ ) {
152 return undef if $noerr ;
153 die "value does not look like a valid node name \n " ;
158 register_format
( 'ipv4' , \
& pve_verify_ipv4
);
159 sub pve_verify_ipv4
{
160 my ( $ipv4, $noerr ) = @_ ;
162 if ( $ipv4 !~ m/^(?:$IPV4RE)$/ ) {
163 return undef if $noerr ;
164 die "value does not look like a valid IPv4 address \n " ;
169 register_format
( 'ipv6' , \
& pve_verify_ipv6
);
170 sub pve_verify_ipv6
{
171 my ( $ipv6, $noerr ) = @_ ;
173 if ( $ipv6 !~ m/^(?:$IPV6RE)$/ ) {
174 return undef if $noerr ;
175 die "value does not look like a valid IPv6 address \n " ;
180 register_format
( 'ip' , \
& pve_verify_ip
);
182 my ( $ip, $noerr ) = @_ ;
184 if ( $ip !~ m/^(?:(?:$IPV4RE)|(?:$IPV6RE))$/ ) {
185 return undef if $noerr ;
186 die "value does not look like a valid IP address \n " ;
191 my $ipv4_mask_hash = {
208 '255.255.128.0' => 17 ,
209 '255.255.192.0' => 18 ,
210 '255.255.224.0' => 19 ,
211 '255.255.240.0' => 20 ,
212 '255.255.248.0' => 21 ,
213 '255.255.252.0' => 22 ,
214 '255.255.254.0' => 23 ,
215 '255.255.255.0' => 24 ,
216 '255.255.255.128' => 25 ,
217 '255.255.255.192' => 26 ,
218 '255.255.255.224' => 27 ,
219 '255.255.255.240' => 28 ,
220 '255.255.255.248' => 29 ,
221 '255.255.255.252' => 30
224 register_format
( 'ipv4mask' , \
& pve_verify_ipv4mask
);
225 sub pve_verify_ipv4mask
{
226 my ( $mask, $noerr ) = @_ ;
228 if (! defined ( $ipv4_mask_hash ->{ $mask })) {
229 return undef if $noerr ;
230 die "value does not look like a valid IP netmask \n " ;
235 register_format
( 'CIDRv6' , \
& pve_verify_cidrv6
);
236 sub pve_verify_cidrv6
{
237 my ( $cidr, $noerr ) = @_ ;
239 if ( $cidr =~ m!^(?:$IPV6RE)(?:/(\d+))$! && ( $1 > 7 ) && ( $1 <= 120 )) {
243 return undef if $noerr ;
244 die "value does not look like a valid IPv6 CIDR network \n " ;
247 register_format
( 'CIDRv4' , \
& pve_verify_cidrv4
);
248 sub pve_verify_cidrv4
{
249 my ( $cidr, $noerr ) = @_ ;
251 if ( $cidr =~ m!^(?:$IPV4RE)(?:/(\d+))$! && ( $1 > 7 ) && ( $1 <= 32 )) {
255 return undef if $noerr ;
256 die "value does not look like a valid IPv4 CIDR network \n " ;
259 register_format
( 'CIDR' , \
& pve_verify_cidr
);
260 sub pve_verify_cidr
{
261 my ( $cidr, $noerr ) = @_ ;
263 if (!( pve_verify_cidrv4
( $cidr, 1 ) ||
264 pve_verify_cidrv6
( $cidr, 1 )))
266 return undef if $noerr ;
267 die "value does not look like a valid CIDR network \n " ;
273 register_format
( 'pve-ipv4-config' , \
& pve_verify_ipv4_config
);
274 sub pve_verify_ipv4_config
{
275 my ( $config, $noerr ) = @_ ;
277 return $config if $config =~ /^(?:dhcp|manual)$/ ||
278 pve_verify_cidrv4
( $config, 1 );
279 return undef if $noerr ;
280 die "value does not look like a valid ipv4 network configuration \n " ;
283 register_format
( 'pve-ipv6-config' , \
& pve_verify_ipv6_config
);
284 sub pve_verify_ipv6_config
{
285 my ( $config, $noerr ) = @_ ;
287 return $config if $config =~ /^(?:auto|dhcp|manual)$/ ||
288 pve_verify_cidrv6
( $config, 1 );
289 return undef if $noerr ;
290 die "value does not look like a valid ipv6 network configuration \n " ;
293 register_format
( 'email' , \
& pve_verify_email
);
294 sub pve_verify_email
{
295 my ( $email, $noerr ) = @_ ;
297 # we use same regex as in Utils.js
298 if ( $email !~ /^(\w+)([\-+.][\w]+)*@(\w[\-\w]*\.){1,5}([A-Za-z]){2,63}$/ ) {
299 return undef if $noerr ;
300 die "value does not look like a valid email address \n " ;
305 register_format
( 'dns-name' , \
& pve_verify_dns_name
);
306 sub pve_verify_dns_name
{
307 my ( $name, $noerr ) = @_ ;
309 my $namere = "([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?)" ;
311 if ( $name !~ /^(${namere}\.)*${namere}$/ ) {
312 return undef if $noerr ;
313 die "value does not look like a valid DNS name \n " ;
318 # network interface name
319 register_format
( 'pve-iface' , \
& pve_verify_iface
);
320 sub pve_verify_iface
{
321 my ( $id, $noerr ) = @_ ;
323 if ( $id !~ m/^[a-z][a-z0-9_]{1,20}([:\.]\d+)?$/i ) {
324 return undef if $noerr ;
325 die "invalid network interface name ' $id ' \n " ;
330 # general addresses by name or IP
331 register_format
( 'address' , \
& pve_verify_address
);
332 sub pve_verify_address
{
333 my ( $addr, $noerr ) = @_ ;
335 if (!( pve_verify_ip
( $addr, 1 ) ||
336 pve_verify_dns_name
( $addr, 1 )))
338 return undef if $noerr ;
339 die "value does not look like a valid address: $addr\n " ;
344 register_format
( 'disk-size' , \
& pve_verify_disk_size
);
345 sub pve_verify_disk_size
{
346 my ( $size, $noerr ) = @_ ;
347 if (! defined ( parse_size
( $size ))) {
348 return undef if $noerr ;
349 die "value does not look like a valid disk size: $size\n " ;
354 register_standard_option
( 'spice-proxy' , {
355 description
=> "SPICE proxy server. This can be used by the client to specify the proxy server. All nodes in a cluster runs 'spiceproxy', so it is up to the client to choose one. By default, we return the node where the VM is currently running. As resonable setting is to use same node you use to connect to the API (This is window.location.hostname for the JS GUI)." ,
356 type
=> 'string' , format
=> 'address' ,
359 register_standard_option
( 'remote-viewer-config' , {
360 description
=> "Returned values can be directly passed to the 'remote-viewer' application." ,
361 additionalProperties
=> 1 ,
363 type
=> { type
=> 'string' },
364 password
=> { type
=> 'string' },
365 proxy
=> { type
=> 'string' },
366 host
=> { type
=> 'string' },
367 'tls-port' => { type
=> 'integer' },
371 register_format
( 'pve-startup-order' , \
& pve_verify_startup_order
);
372 sub pve_verify_startup_order
{
373 my ( $value, $noerr ) = @_ ;
375 return $value if pve_parse_startup_order
( $value );
377 return undef if $noerr ;
379 die "unable to parse startup options \n " ;
382 sub pve_parse_startup_order
{
385 return undef if ! $value ;
389 foreach my $p ( split ( /,/ , $value )) {
390 next if $p =~ m/^\s*$/ ;
392 if ( $p =~ m/^(order=)?(\d+)$/ ) {
394 } elsif ( $p =~ m/^up=(\d+)$/ ) {
396 } elsif ( $p =~ m/^down=(\d+)$/ ) {
406 PVE
:: JSONSchema
:: register_standard_option
( 'pve-startup-order' , {
407 description
=> "Startup and shutdown behavior. Order is a non-negative number defining the general startup order. Shutdown in done with reverse ordering. Additionally you can set the 'up' or 'down' delay in seconds, which specifies a delay to wait before the next VM is started or stopped." ,
409 type
=> 'string' , format
=> 'pve-startup-order' ,
410 typetext
=> '[[order=]\d+] [,up=\d+] [,down=\d+] ' ,
414 my ( $format, $value, $path ) = @_ ;
416 return parse_property_string
( $format, $value, $path ) if ref ( $format ) eq 'HASH' ;
417 return if $format eq 'regex' ;
419 if ( $format =~ m/^(.*)-a?list$/ ) {
421 my $code = $format_list ->{ $1 };
423 die "undefined format ' $format ' \n " if ! $code ;
425 # Note: we allow empty lists
426 foreach my $v ( split_list
( $value )) {
430 } elsif ( $format =~ m/^(.*)-opt$/ ) {
432 my $code = $format_list ->{ $1 };
434 die "undefined format ' $format ' \n " if ! $code ;
436 return if ! $value ; # allow empty string
442 my $code = $format_list ->{ $format };
444 die "undefined format ' $format ' \n " if ! $code ;
446 return parse_property_string
( $code, $value, $path ) if ref ( $code ) eq 'HASH' ;
454 return undef if $value !~ m/^(\d+(\.\d+)?)([KMGT])?$/ ;
455 my ( $size, $unit ) = ( $1, $3 );
458 $size = $size * 1024 ;
459 } elsif ( $unit eq 'M' ) {
460 $size = $size * 1024 * 1024 ;
461 } elsif ( $unit eq 'G' ) {
462 $size = $size * 1024 * 1024 * 1024 ;
463 } elsif ( $unit eq 'T' ) {
464 $size = $size * 1024 * 1024 * 1024 * 1024 ;
475 my $kb = int ( $size/1024 );
476 return $size if $kb*1024 != $size ;
478 my $mb = int ( $kb/1024 );
479 return "${kb}K" if $mb*1024 != $kb ;
481 my $gb = int ( $mb/1024 );
482 return "${mb}M" if $gb*1024 != $mb ;
484 my $tb = int ( $gb/1024 );
485 return "${gb}G" if $tb*1024 != $gb ;
490 sub parse_property_string
{
491 my ( $format, $data, $path, $additional_properties ) = @_ ;
493 # In property strings we default to not allowing additional properties
494 $additional_properties = 0 if ! defined ( $additional_properties );
499 foreach my $part ( split ( /,/ , $data )) {
500 next if $part =~ /^\s*$/ ;
502 if ( $part =~ /^([^=]+)=(.+)$/ ) {
503 my ( $k, $v ) = ( $1, $2 );
504 die "duplicate key in comma-separated list property: $k\n " if defined ( $res ->{ $k });
505 my $schema = $format ->{ $k };
506 if ( my $alias = $schema ->{ alias
}) {
508 $schema = $format ->{ $k };
510 die "invalid key in comma-separated list property: $k\n " if ! $schema ;
511 if ( $schema ->{ type
} && $schema ->{ type
} eq 'boolean' ) {
512 $v = 1 if $v =~ m/^(1|on|yes|true)$/i ;
513 $v = 0 if $v =~ m/^(0|off|no|false)$/i ;
516 } elsif ( $part !~ /=/ ) {
517 die "duplicate key in comma-separated list property: $default_key\n " if $default_key ;
518 foreach my $key ( keys %$format ) {
519 if ( $format ->{ $key }->{ default_key
}) {
521 if (! $res ->{ $default_key }) {
522 $res ->{ $default_key } = $part ;
525 die "duplicate key in comma-separated list property: $default_key\n " ;
529 die "missing key in comma-separated list property \n " ;
534 check_object
( $path, $format, $res, $additional_properties, $errors );
535 if ( scalar ( %$errors )) {
536 raise
"format error \n " , errors
=> $errors ;
542 sub print_property_string
{
543 my ( $data, $format, $skip, $path ) = @_ ;
545 if ( ref ( $format ) ne 'HASH' ) {
546 my $schema = $format_list ->{ $format };
547 die "not a valid format: $format " if ! $schema ;
552 check_object
( $path, $format, $data, undef , $errors );
553 if ( scalar ( %$errors )) {
554 raise
"format error" , errors
=> $errors ;
558 my %skipped = map { $_ => 1 } @$skip ;
560 my %required ; # this is a set, all present keys are required regardless of value
561 foreach my $key ( keys %$format ) {
563 if (! $format ->{ $key }->{ optional
} && ! $format ->{ $key }->{ alias
} && ! $skipped { $key }) {
568 if ( $format ->{ $key }->{ default_key
}) {
570 warn "multiple default keys in schema ( $default_key, $key )" ;
580 $text = " $data ->{ $default_key }" ;
587 foreach my $key ( sort keys %$data ) {
588 delete $required { $key };
589 next if $skipped { $key };
590 die "invalid key: $key " if ! $allowed { $key };
592 my $typeformat = $format ->{ $key }->{ format
};
593 my $value = $data ->{ $key };
594 next if ! defined ( $value );
597 if ( $typeformat && $typeformat eq 'disk-size' ) {
598 $text .= " $key =" . format_size
( $value );
600 $text .= " $key = $value " ;
604 if ( my $missing = join ( ',' , keys %required )) {
605 die "missing properties: $missing " ;
612 my ( $errors, $path, $msg ) = @_ ;
614 $path = '_root' if ! $path ;
616 if ( $errors ->{ $path }) {
617 $errors ->{ $path } = join ( ' \n ' , $errors ->{ $path }, $msg );
619 $errors ->{ $path } = $msg ;
626 # see 'man perlretut'
627 return $value =~ /^[+-]?(\d+\.\d+|\d+\.|\.\d+|\d+)([eE][+-]?\d+)?$/ ;
633 return $value =~ m/^[+-]?\d+$/ ;
637 my ( $path, $type, $value, $errors ) = @_ ;
641 if (! defined ( $value )) {
642 return 1 if $type eq 'null' ;
646 if ( my $tt = ref ( $type )) {
647 if ( $tt eq 'ARRAY' ) {
648 foreach my $t ( @$type ) {
650 check_type
( $path, $t, $value, $tmperr );
651 return 1 if ! scalar ( %$tmperr );
653 my $ttext = join ( '|' , @$type );
654 add_error
( $errors, $path, "type check (' $ttext ') failed" );
656 } elsif ( $tt eq 'HASH' ) {
658 check_prop
( $value, $type, $path, $tmperr );
659 return 1 if ! scalar ( %$tmperr );
660 add_error
( $errors, $path, "type check failed" );
663 die "internal error - got reference type ' $tt '" ;
668 return 1 if $type eq 'any' ;
670 if ( $type eq 'null' ) {
671 if ( defined ( $value )) {
672 add_error
( $errors, $path, "type check (' $type ') failed - value is not null" );
678 my $vt = ref ( $value );
680 if ( $type eq 'array' ) {
681 if (! $vt || $vt ne 'ARRAY' ) {
682 add_error
( $errors, $path, "type check (' $type ') failed" );
686 } elsif ( $type eq 'object' ) {
687 if (! $vt || $vt ne 'HASH' ) {
688 add_error
( $errors, $path, "type check (' $type ') failed" );
692 } elsif ( $type eq 'coderef' ) {
693 if (! $vt || $vt ne 'CODE' ) {
694 add_error
( $errors, $path, "type check (' $type ') failed" );
700 add_error
( $errors, $path, "type check (' $type ') failed - got $vt " );
703 if ( $type eq 'string' ) {
704 return 1 ; # nothing to check ?
705 } elsif ( $type eq 'boolean' ) {
706 #if ($value =~ m/^(1|true|yes|on)$/i) {
709 #} elsif ($value =~ m/^(0|false|no|off)$/i) {
710 } elsif ( $value eq '0' ) {
713 add_error
( $errors, $path, "type check (' $type ') failed - got ' $value '" );
716 } elsif ( $type eq 'integer' ) {
717 if (! is_integer
( $value )) {
718 add_error
( $errors, $path, "type check (' $type ') failed - got ' $value '" );
722 } elsif ( $type eq 'number' ) {
723 if (! is_number
( $value )) {
724 add_error
( $errors, $path, "type check (' $type ') failed - got ' $value '" );
729 return 1 ; # no need to verify unknown types
739 my ( $path, $schema, $value, $additional_properties, $errors ) = @_ ;
741 # print "Check Object " . Dumper($value) . "\nSchema: " . Dumper($schema);
743 my $st = ref ( $schema );
744 if (! $st || $st ne 'HASH' ) {
745 add_error
( $errors, $path, "Invalid schema definition." );
749 my $vt = ref ( $value );
750 if (! $vt || $vt ne 'HASH' ) {
751 add_error
( $errors, $path, "an object is required" );
755 foreach my $k ( keys %$schema ) {
756 check_prop
( $value ->{ $k }, $schema ->{ $k }, $path ?
" $path . $k " : $k, $errors );
759 foreach my $k ( keys %$value ) {
761 my $newpath = $path ?
" $path . $k " : $k ;
763 if ( my $subschema = $schema ->{ $k }) {
764 if ( my $requires = $subschema ->{ requires
}) {
765 if ( ref ( $requires )) {
766 #print "TEST: " . Dumper($value) . "\n", Dumper($requires) ;
767 check_prop
( $value, $requires, $path, $errors );
768 } elsif (! defined ( $value ->{ $requires })) {
769 add_error
( $errors, $path ?
" $path . $requires " : $requires,
770 "missing property - ' $newpath ' requiers this property" );
774 next ; # value is already checked above
777 if ( defined ( $additional_properties ) && ! $additional_properties ) {
778 add_error
( $errors, $newpath, "property is not defined in schema " .
779 "and the schema does not allow additional properties" );
782 check_prop
( $value ->{ $k }, $additional_properties, $newpath, $errors )
783 if ref ( $additional_properties );
787 sub check_object_warn
{
788 my ( $path, $schema, $value, $additional_properties ) = @_ ;
790 check_object
( $path, $schema, $value, $additional_properties, $errors );
791 if ( scalar ( %$errors )) {
792 foreach my $k ( keys %$errors ) {
793 warn "parse error: $k : $errors ->{ $k } \n " ;
801 my ( $value, $schema, $path, $errors ) = @_ ;
803 die "internal error - no schema" if ! $schema ;
804 die "internal error" if ! $errors ;
806 #print "check_prop $path\n" if $value;
808 my $st = ref ( $schema );
809 if (! $st || $st ne 'HASH' ) {
810 add_error
( $errors, $path, "Invalid schema definition." );
814 # if it extends another schema, it must pass that schema as well
815 if ( $schema ->{ extends
}) {
816 check_prop
( $value, $schema ->{ extends
}, $path, $errors );
819 if (! defined ( $value )) {
820 return if $schema ->{ type
} && $schema ->{ type
} eq 'null' ;
821 if (! $schema ->{ optional
} && ! $schema ->{ alias
}) {
822 add_error
( $errors, $path, "property is missing and it is not optional" );
827 return if ! check_type
( $path, $schema ->{ type
}, $value, $errors );
829 if ( $schema ->{ disallow
}) {
831 if ( check_type
( $path, $schema ->{ disallow
}, $value, $tmperr )) {
832 add_error
( $errors, $path, "disallowed value was matched" );
837 if ( my $vt = ref ( $value )) {
839 if ( $vt eq 'ARRAY' ) {
840 if ( $schema ->{ items
}) {
841 my $it = ref ( $schema ->{ items
});
842 if ( $it && $it eq 'ARRAY' ) {
843 #die "implement me $path: $vt " . Dumper($schema) ."\n". Dumper($value);
844 die "not implemented" ;
847 foreach my $el ( @$value ) {
848 check_prop
( $el, $schema ->{ items
}, "${path}[ $ind ]" , $errors );
854 } elsif ( $schema ->{ properties
} || $schema ->{ additionalProperties
}) {
855 check_object
( $path, defined ( $schema ->{ properties
}) ?
$schema ->{ properties
} : {},
856 $value, $schema ->{ additionalProperties
}, $errors );
862 if ( my $format = $schema ->{ format
}) {
863 eval { check_format
( $format, $value, $path ); };
865 add_error
( $errors, $path, "invalid format - $@ " );
870 if ( my $pattern = $schema ->{ pattern
}) {
871 if ( $value !~ m/^$pattern$/ ) {
872 add_error
( $errors, $path, "value does not match the regex pattern" );
877 if ( defined ( my $max = $schema ->{ maxLength
})) {
878 if ( length ( $value ) > $max ) {
879 add_error
( $errors, $path, "value may only be $max characters long" );
884 if ( defined ( my $min = $schema ->{ minLength
})) {
885 if ( length ( $value ) < $min ) {
886 add_error
( $errors, $path, "value must be at least $min characters long" );
891 if ( is_number
( $value )) {
892 if ( defined ( my $max = $schema ->{ maximum
})) {
894 add_error
( $errors, $path, "value must have a maximum value of $max " );
899 if ( defined ( my $min = $schema ->{ minimum
})) {
901 add_error
( $errors, $path, "value must have a minimum value of $min " );
907 if ( my $ea = $schema ->{ enum
}) {
910 foreach my $ev ( @$ea ) {
917 add_error
( $errors, $path, "value ' $value ' does not have a value in the enumeration '" .
918 join ( ", " , @$ea ) . "'" );
925 my ( $instance, $schema, $errmsg ) = @_ ;
928 $errmsg = "Parameter verification failed. \n " if ! $errmsg ;
930 # todo: cycle detection is only needed for debugging, I guess
931 # we can disable that in the final release
932 # todo: is there a better/faster way to detect cycles?
934 find_cycle
( $instance, sub { $cycles = 1 });
936 add_error
( $errors, undef , "data structure contains recursive cycles" );
938 check_prop
( $instance, $schema, '' , $errors );
941 if ( scalar ( %$errors )) {
942 raise
$errmsg, code
=> HTTP_BAD_REQUEST
, errors
=> $errors ;
948 my $schema_valid_types = [ "string" , "object" , "coderef" , "array" , "boolean" , "number" , "integer" , "null" , "any" ];
949 my $default_schema_noref = {
950 description
=> "This is the JSON Schema for JSON Schemas." ,
951 type
=> [ "object" ],
952 additionalProperties
=> 0 ,
955 type
=> [ "string" , "array" ],
956 description
=> "This is a type definition value. This can be a simple type, or a union type" ,
961 enum
=> $schema_valid_types,
963 enum
=> $schema_valid_types,
967 description
=> "This indicates that the instance property in the instance object is not required." ,
973 description
=> "This is a definition for the properties of an object value" ,
979 description
=> "When the value is an array, this indicates the schema to use to validate each item in an array" ,
983 additionalProperties
=> {
984 type
=> [ "boolean" , "object" ],
985 description
=> "This provides a default property definition for all properties that are not explicitly defined in an object type definition." ,
992 description
=> "This indicates the minimum value for the instance property when the type of the instance value is a number." ,
997 description
=> "This indicates the maximum value for the instance property when the type of the instance value is a number." ,
1001 description
=> "When the instance value is a string, this indicates minimum length of the string" ,
1008 description
=> "When the instance value is a string, this indicates maximum length of the string." ,
1014 description
=> "A text representation of the type (used to generate documentation)." ,
1019 description
=> "When the instance value is a string, this provides a regular expression that a instance string value should match in order to be valid." ,
1027 description
=> "This provides an enumeration of possible values that are valid for the instance property." ,
1032 description
=> "This provides a description of the purpose the instance property. The value can be a string or it can be an object with properties corresponding to various different instance languages (with an optional default property indicating the default description)." ,
1034 format_description
=> {
1037 description
=> "This provides a shorter (usually just one word) description for a property used to generate descriptions for comma separated list property strings." ,
1042 description
=> "This provides the title of the property" ,
1045 type
=> [ "string" , "object" ],
1047 description
=> "indicates a required property or a schema that must be validated if this property is present" ,
1050 type
=> [ "string" , "object" ],
1052 description
=> "This indicates what format the data is among some predefined formats which may include: \n\n date - a string following the ISO format \n address \n schema - a schema definition object \n person \n page \n html - a string representing HTML" ,
1057 description
=> "Whether this is the default key in a comma separated list property string." ,
1062 description
=> "When a key represents the same property as another it can be an alias to it, causing the parsed datastructure to use the other key to store the current value under." ,
1067 description
=> "This indicates the default for the instance property."
1071 description
=> "Bash completion function. This function should return a list of possible values." ,
1077 description
=> "This attribute may take the same values as the \" type \" attribute, however if the instance matches the type or if this value is an array and the instance matches any type or schema in the array, than this instance is not valid." ,
1082 description
=> "This indicates the schema extends the given schema. All instances of this schema must be valid to by the extended schema also." ,
1085 # this is from hyper schema
1088 description
=> "This defines the link relations of the instance objects" ,
1095 description
=> "This defines the target URL for the relation and can be parameterized using {propertyName} notation. It should be resolved as a URI-reference relative to the URI that was used to retrieve the instance document" ,
1099 description
=> "This is the name of the link relation" ,
1105 description
=> "For submission links, this defines the method that should be used to access the target resource" ,
1115 my $default_schema = Storable
:: dclone
( $default_schema_noref );
1117 $default_schema ->{ properties
}->{ properties
}->{ additionalProperties
} = $default_schema ;
1118 $default_schema ->{ properties
}->{ additionalProperties
}->{ properties
} = $default_schema ->{ properties
};
1120 $default_schema ->{ properties
}->{ items
}->{ properties
} = $default_schema ->{ properties
};
1121 $default_schema ->{ properties
}->{ items
}->{ additionalProperties
} = 0 ;
1123 $default_schema ->{ properties
}->{ disallow
}->{ properties
} = $default_schema ->{ properties
};
1124 $default_schema ->{ properties
}->{ disallow
}->{ additionalProperties
} = 0 ;
1126 $default_schema ->{ properties
}->{ requires
}->{ properties
} = $default_schema ->{ properties
};
1127 $default_schema ->{ properties
}->{ requires
}->{ additionalProperties
} = 0 ;
1129 $default_schema ->{ properties
}->{ extends
}->{ properties
} = $default_schema ->{ properties
};
1130 $default_schema ->{ properties
}->{ extends
}->{ additionalProperties
} = 0 ;
1132 my $method_schema = {
1134 additionalProperties
=> 0 ,
1137 description
=> "This a description of the method" ,
1142 description
=> "This indicates the name of the function to call." ,
1145 additionalProperties
=> 1 ,
1160 description
=> "The HTTP method name." ,
1161 enum
=> [ 'GET' , 'POST' , 'PUT' , 'DELETE' ],
1166 description
=> "Method needs special privileges - only pvedaemon can execute it" ,
1171 description
=> "A parameter name. If specified, all calls to this method are proxied to the host contained in that parameter." ,
1176 description
=> "Required access permissions. By default only 'root' is allowed to access this method." ,
1178 additionalProperties
=> 0 ,
1181 description
=> "Describe access permissions." ,
1185 description
=> "A simply way to allow access for 'all' authenticated users. Value 'world' is used to allow access without credentials." ,
1187 enum
=> [ 'all' , 'world' ],
1191 description
=> "Array of permission checks (prefix notation)." ,
1198 description
=> "Used internally" ,
1202 description
=> "Used internally" ,
1207 description
=> "path for URL matching (uri template)" ,
1209 fragmentDelimiter
=> {
1211 description
=> "A ways to override the default fragment delimiter '/'. This onyl works on a whole sub-class. You can set this to the empty string to match the whole rest of the URI." ,
1216 description
=> "JSON Schema for parameters." ,
1221 description
=> "Used to store page formatter information (set by PVE::RESTHandler->register_page_formatter)." ,
1226 description
=> "JSON Schema for return value." ,
1231 description
=> "method implementaion (code reference)" ,
1236 description
=> "Delegate call to this class (perl class string)." ,
1239 additionalProperties
=> 0 ,
1245 fragmentDelimiter
=> { optional
=> 1 }
1253 sub validate_schema
{
1256 my $errmsg = "internal error - unable to verify schema \n " ;
1257 validate
( $schema, $default_schema, $errmsg );
1260 sub validate_method_info
{
1263 my $errmsg = "internal error - unable to verify method info \n " ;
1264 validate
( $info, $method_schema, $errmsg );
1266 validate_schema
( $info ->{ parameters
}) if $info ->{ parameters
};
1267 validate_schema
( $info ->{ returns
}) if $info ->{ returns
};
1270 # run a self test on load
1271 # make sure we can verify the default schema
1272 validate_schema
( $default_schema_noref );
1273 validate_schema
( $method_schema );
1275 # and now some utility methods (used by pve api)
1276 sub method_get_child_link
{
1279 return undef if ! $info ;
1281 my $schema = $info ->{ returns
};
1282 return undef if ! $schema || ! $schema ->{ type
} || $schema ->{ type
} ne 'array' ;
1284 my $links = $schema ->{ links
};
1285 return undef if ! $links ;
1288 foreach my $lnk ( @$links ) {
1289 if ( $lnk ->{ href
} && $lnk ->{ rel
} && ( $lnk ->{ rel
} eq 'child' )) {
1298 # a way to parse command line parameters, using a
1299 # schema to configure Getopt::Long
1301 my ( $schema, $args, $arg_param, $fixed_param, $pwcallback ) = @_ ;
1303 if (! $schema || ! $schema ->{ properties
}) {
1304 raise
( "too many arguments \n " , code
=> HTTP_BAD_REQUEST
)
1305 if scalar ( @$args ) != 0 ;
1310 if ( $arg_param && ! ref ( $arg_param )) {
1311 my $pd = $schema ->{ properties
}->{ $arg_param };
1312 die "expected list format $pd ->{format}"
1313 if !( $pd && $pd ->{ format
} && $pd ->{ format
} =~ m/-list/ );
1314 $list_param = $arg_param ;
1318 foreach my $prop ( keys %{ $schema ->{ properties
}}) {
1319 my $pd = $schema ->{ properties
}->{ $prop };
1320 next if $list_param && $prop eq $list_param ;
1321 next if defined ( $fixed_param ->{ $prop });
1323 if ( $prop eq 'password' && $pwcallback ) {
1324 # we do not accept plain password on input line, instead
1325 # we turn this into a boolean option and ask for password below
1326 # using $pwcallback() (for security reasons).
1327 push @getopt, " $prop " ;
1328 } elsif ( $pd ->{ type
} eq 'boolean' ) {
1329 push @getopt, " $prop :s" ;
1331 if ( $pd ->{ format
} && $pd ->{ format
} =~ m/-a?list/ ) {
1332 push @getopt, " $prop =s@" ;
1334 push @getopt, " $prop =s" ;
1339 Getopt
:: Long
:: Configure
( 'prefix_pattern=(--|-)' );
1342 raise
( "unable to parse option \n " , code
=> HTTP_BAD_REQUEST
)
1343 if ! Getopt
:: Long
:: GetOptionsFromArray
( $args, $opts, @getopt );
1347 $opts ->{ $list_param } = $args ;
1349 } elsif ( ref ( $arg_param )) {
1350 foreach my $arg_name ( @$arg_param ) {
1351 if ( $opts ->{ 'extra-args' }) {
1352 raise
( "internal error: extra-args must be the last argument \n " , code
=> HTTP_BAD_REQUEST
);
1354 if ( $arg_name eq 'extra-args' ) {
1355 $opts ->{ 'extra-args' } = $args ;
1359 raise
( "not enough arguments \n " , code
=> HTTP_BAD_REQUEST
) if ! @$args ;
1360 $opts ->{ $arg_name } = shift @$args ;
1362 raise
( "too many arguments \n " , code
=> HTTP_BAD_REQUEST
) if @$args ;
1364 raise
( "too many arguments \n " , code
=> HTTP_BAD_REQUEST
)
1365 if scalar ( @$args ) != 0 ;
1369 if ( my $pd = $schema ->{ properties
}->{ password
}) {
1370 if ( $pd ->{ type
} ne 'boolean' && $pwcallback ) {
1371 if ( $opts ->{ password
} || ! $pd ->{ optional
}) {
1372 $opts ->{ password
} = & $pwcallback ();
1377 $opts = PVE
:: Tools
:: decode_utf8_parameters
( $opts );
1379 foreach my $p ( keys %$opts ) {
1380 if ( my $pd = $schema ->{ properties
}->{ $p }) {
1381 if ( $pd ->{ type
} eq 'boolean' ) {
1382 if ( $opts ->{ $p } eq '' ) {
1384 } elsif ( $opts ->{ $p } =~ m/^(1|true|yes|on)$/i ) {
1386 } elsif ( $opts ->{ $p } =~ m/^(0|false|no|off)$/i ) {
1389 raise
( "unable to parse boolean option \n " , code
=> HTTP_BAD_REQUEST
);
1391 } elsif ( $pd ->{ format
}) {
1393 if ( $pd ->{ format
} =~ m/-list/ ) {
1394 # allow --vmid 100 --vmid 101 and --vmid 100,101
1395 # allow --dow mon --dow fri and --dow mon,fri
1396 $opts ->{ $p } = join ( "," , @{ $opts ->{ $p }}) if ref ( $opts ->{ $p }) eq 'ARRAY' ;
1397 } elsif ( $pd ->{ format
} =~ m/-alist/ ) {
1398 # we encode array as \0 separated strings
1399 # Note: CGI.pm also use this encoding
1400 if ( scalar (@{ $opts ->{ $p }}) != 1 ) {
1401 $opts ->{ $p } = join ( "\0" , @{ $opts ->{ $p }});
1403 # st that split_list knows it is \0 terminated
1404 my $v = $opts ->{ $p }->[ 0 ];
1405 $opts ->{ $p } = " $v\0 " ;
1412 foreach my $p ( keys %$fixed_param ) {
1413 $opts ->{ $p } = $fixed_param ->{ $p };
1419 # A way to parse configuration data by giving a json schema
1421 my ( $schema, $filename, $raw ) = @_ ;
1423 # do fast check (avoid validate_schema($schema))
1424 die "got strange schema" if ! $schema ->{ type
} ||
1425 ! $schema ->{ properties
} || $schema ->{ type
} ne 'object' ;
1429 while ( $raw =~ /^\s*(.+?)\s*$/gm ) {
1432 next if $line =~ /^#/ ;
1434 if ( $line =~ m/^(\S+?):\s*(.*)$/ ) {
1437 if ( $schema ->{ properties
}->{ $key } &&
1438 $schema ->{ properties
}->{ $key }->{ type
} eq 'boolean' ) {
1440 $value = 1 if $value =~ m/^(1|on|yes|true)$/i ;
1441 $value = 0 if $value =~ m/^(0|off|no|false)$/i ;
1443 $cfg ->{ $key } = $value ;
1445 warn "ignore config line: $line\n "
1450 check_prop
( $cfg, $schema, '' , $errors );
1452 foreach my $k ( keys %$errors ) {
1453 warn "parse error in ' $filename ' - ' $k ': $errors ->{ $k } \n " ;
1460 # generate simple key/value file
1462 my ( $schema, $filename, $cfg ) = @_ ;
1464 # do fast check (avoid validate_schema($schema))
1465 die "got strange schema" if ! $schema ->{ type
} ||
1466 ! $schema ->{ properties
} || $schema ->{ type
} ne 'object' ;
1468 validate
( $cfg, $schema, "validation error in ' $filename ' \n " );
1472 foreach my $k ( keys %$cfg ) {
1473 $data .= " $k : $cfg ->{ $k } \n " ;