]>
Commit | Line | Data |
---|---|---|
1 | package PVE::RESTHandler; | |
2 | ||
3 | use strict; | |
4 | no strict 'refs'; # our autoload requires this | |
5 | use warnings; | |
6 | use PVE::SafeSyslog; | |
7 | use PVE::Exception qw(raise raise_param_exc); | |
8 | use PVE::JSONSchema; | |
9 | use PVE::PodParser; | |
10 | use HTTP::Status qw(:constants :is status_message); | |
11 | use Text::Wrap; | |
12 | use Storable qw(dclone); | |
13 | ||
14 | my $method_registry = {}; | |
15 | my $method_by_name = {}; | |
16 | my $method_path_lookup = {}; | |
17 | ||
18 | our $AUTOLOAD; # it's a package global | |
19 | ||
20 | sub api_clone_schema { | |
21 | my ($schema) = @_; | |
22 | ||
23 | my $res = {}; | |
24 | my $ref = ref($schema); | |
25 | die "not a HASH reference" if !($ref && $ref eq 'HASH'); | |
26 | ||
27 | foreach my $k (keys %$schema) { | |
28 | my $d = $schema->{$k}; | |
29 | if ($k ne 'properties') { | |
30 | $res->{$k} = ref($d) ? dclone($d) : $d; | |
31 | next; | |
32 | } | |
33 | # convert indexed parameters like -net\d+ to -net[n] | |
34 | foreach my $p (keys %$d) { | |
35 | my $pd = $d->{$p}; | |
36 | if ($p =~ m/^([a-z]+)(\d+)$/) { | |
37 | if ($2 == 0) { | |
38 | $p = "$1\[n\]"; | |
39 | } else { | |
40 | next; | |
41 | } | |
42 | } | |
43 | $res->{$k}->{$p} = ref($pd) ? dclone($pd) : $pd; | |
44 | } | |
45 | } | |
46 | ||
47 | return $res; | |
48 | } | |
49 | ||
50 | sub api_dump_full { | |
51 | my ($tree, $index, $class, $prefix) = @_; | |
52 | ||
53 | $prefix = '' if !$prefix; | |
54 | ||
55 | my $ma = $method_registry->{$class}; | |
56 | ||
57 | foreach my $info (@$ma) { | |
58 | ||
59 | my $path = "$prefix/$info->{path}"; | |
60 | $path =~ s/\/+$//; | |
61 | ||
62 | if ($info->{subclass}) { | |
63 | api_dump_full($tree, $index, $info->{subclass}, $path); | |
64 | } else { | |
65 | next if !$path; | |
66 | ||
67 | # check if method is unique | |
68 | my $realpath = $path; | |
69 | $realpath =~ s/\{[^\}]+\}/\{\}/g; | |
70 | my $fullpath = "$info->{method} $realpath"; | |
71 | die "duplicate path '$realpath'" if $index->{$fullpath}; | |
72 | $index->{$fullpath} = $info; | |
73 | ||
74 | # insert into tree | |
75 | my $treedir = $tree; | |
76 | my $res; | |
77 | my $sp = ''; | |
78 | foreach my $dir (split('/', $path)) { | |
79 | next if !$dir; | |
80 | $sp .= "/$dir"; | |
81 | $res = (grep { $_->{text} eq $dir } @$treedir)[0]; | |
82 | if ($res) { | |
83 | $res->{children} = [] if !$res->{children}; | |
84 | $treedir = $res->{children}; | |
85 | } else { | |
86 | $res = { | |
87 | path => $sp, | |
88 | text => $dir, | |
89 | children => [], | |
90 | }; | |
91 | push @$treedir, $res; | |
92 | $treedir = $res->{children}; | |
93 | } | |
94 | } | |
95 | ||
96 | if ($res) { | |
97 | my $data = {}; | |
98 | foreach my $k (keys %$info) { | |
99 | next if $k eq 'code' || $k eq "match_name" || $k eq "match_re" || | |
100 | $k eq "path"; | |
101 | ||
102 | my $d = $info->{$k}; | |
103 | ||
104 | if ($k eq 'parameters') { | |
105 | $data->{$k} = api_clone_schema($d); | |
106 | } else { | |
107 | ||
108 | $data->{$k} = ref($d) ? dclone($d) : $d; | |
109 | } | |
110 | } | |
111 | $res->{info}->{$info->{method}} = $data; | |
112 | }; | |
113 | } | |
114 | } | |
115 | }; | |
116 | ||
117 | sub api_dump_cleanup_tree { | |
118 | my ($tree) = @_; | |
119 | ||
120 | foreach my $rec (@$tree) { | |
121 | delete $rec->{children} if $rec->{children} && !scalar(@{$rec->{children}}); | |
122 | if ($rec->{children}) { | |
123 | $rec->{leaf} = 0; | |
124 | api_dump_cleanup_tree($rec->{children}); | |
125 | } else { | |
126 | $rec->{leaf} = 1; | |
127 | } | |
128 | } | |
129 | ||
130 | } | |
131 | ||
132 | sub api_dump { | |
133 | my ($class, $prefix) = @_; | |
134 | ||
135 | my $tree = []; | |
136 | ||
137 | my $index = {}; | |
138 | api_dump_full($tree, $index, $class); | |
139 | api_dump_cleanup_tree($tree); | |
140 | return $tree; | |
141 | }; | |
142 | ||
143 | sub validate_method_schemas { | |
144 | ||
145 | foreach my $class (keys %$method_registry) { | |
146 | my $ma = $method_registry->{$class}; | |
147 | ||
148 | foreach my $info (@$ma) { | |
149 | PVE::JSONSchema::validate_method_info($info); | |
150 | } | |
151 | } | |
152 | } | |
153 | ||
154 | sub register_method { | |
155 | my ($self, $info) = @_; | |
156 | ||
157 | my $match_re = []; | |
158 | my $match_name = []; | |
159 | ||
160 | my $errprefix; | |
161 | ||
162 | my $method; | |
163 | if ($info->{subclass}) { | |
164 | $errprefix = "register subclass $info->{subclass} at ${self}/$info->{path} -"; | |
165 | $method = 'SUBCLASS'; | |
166 | } else { | |
167 | $errprefix = "register method ${self}/$info->{path} -"; | |
168 | $info->{method} = 'GET' if !$info->{method}; | |
169 | $method = $info->{method}; | |
170 | } | |
171 | ||
172 | $method_path_lookup->{$self} = {} if !defined($method_path_lookup->{$self}); | |
173 | my $path_lookup = $method_path_lookup->{$self}; | |
174 | ||
175 | die "$errprefix no path" if !defined($info->{path}); | |
176 | ||
177 | foreach my $comp (split(/\/+/, $info->{path})) { | |
178 | die "$errprefix path compoment has zero length\n" if $comp eq ''; | |
179 | my ($name, $regex); | |
180 | if ($comp =~ m/^\{(\w+)(:(.*))?\}$/) { | |
181 | $name = $1; | |
182 | $regex = $3 ? $3 : '\S+'; | |
183 | push @$match_re, $regex; | |
184 | push @$match_name, $name; | |
185 | } else { | |
186 | $name = $comp; | |
187 | push @$match_re, $name; | |
188 | push @$match_name, undef; | |
189 | } | |
190 | ||
191 | if ($regex) { | |
192 | $path_lookup->{regex} = {} if !defined($path_lookup->{regex}); | |
193 | ||
194 | my $old_name = $path_lookup->{regex}->{match_name}; | |
195 | die "$errprefix found changed regex match name\n" | |
196 | if defined($old_name) && ($old_name ne $name); | |
197 | my $old_re = $path_lookup->{regex}->{match_re}; | |
198 | die "$errprefix found changed regex\n" | |
199 | if defined($old_re) && ($old_re ne $regex); | |
200 | $path_lookup->{regex}->{match_name} = $name; | |
201 | $path_lookup->{regex}->{match_re} = $regex; | |
202 | ||
203 | die "$errprefix path match error - regex and fixed items\n" | |
204 | if defined($path_lookup->{folders}); | |
205 | ||
206 | $path_lookup = $path_lookup->{regex}; | |
207 | ||
208 | } else { | |
209 | $path_lookup->{folders}->{$name} = {} if !defined($path_lookup->{folders}->{$name}); | |
210 | ||
211 | die "$errprefix path match error - regex and fixed items\n" | |
212 | if defined($path_lookup->{regex}); | |
213 | ||
214 | $path_lookup = $path_lookup->{folders}->{$name}; | |
215 | } | |
216 | } | |
217 | ||
218 | die "$errprefix duplicate method definition\n" | |
219 | if defined($path_lookup->{$method}); | |
220 | ||
221 | $path_lookup->{$method} = $info; | |
222 | ||
223 | $info->{match_re} = $match_re; | |
224 | $info->{match_name} = $match_name; | |
225 | ||
226 | $method_by_name->{$self} = {} if !defined($method_by_name->{$self}); | |
227 | ||
228 | if ($info->{name}) { | |
229 | die "$errprefix method name already defined\n" | |
230 | if defined($method_by_name->{$self}->{$info->{name}}); | |
231 | ||
232 | $method_by_name->{$self}->{$info->{name}} = $info; | |
233 | } | |
234 | ||
235 | push @{$method_registry->{$self}}, $info; | |
236 | } | |
237 | ||
238 | sub register_page_formatter { | |
239 | my ($self, %config) = @_; | |
240 | ||
241 | my $format = $config{format} || | |
242 | die "missing format"; | |
243 | ||
244 | my $path = $config{path} || | |
245 | die "missing path"; | |
246 | ||
247 | my $method = $config{method} || | |
248 | die "missing method"; | |
249 | ||
250 | my $code = $config{code} || | |
251 | die "missing formatter code"; | |
252 | ||
253 | my $uri_param = {}; | |
254 | my ($handler, $info) = $self->find_handler($method, $path, $uri_param); | |
255 | die "unabe to find handler for '$method: $path'" if !($handler && $info); | |
256 | ||
257 | die "duplicate formatter for '$method: $path'" | |
258 | if $info->{formatter} && $info->{formatter}->{$format}; | |
259 | ||
260 | $info->{formatter}->{$format} = $code; | |
261 | } | |
262 | ||
263 | sub AUTOLOAD { | |
264 | my ($this) = @_; | |
265 | ||
266 | # also see "man perldiag" | |
267 | ||
268 | my $sub = $AUTOLOAD; | |
269 | (my $method = $sub) =~ s/.*:://; | |
270 | ||
271 | $method =~ s/.*:://; | |
272 | ||
273 | my $info = $this->map_method_by_name($method); | |
274 | ||
275 | *{$sub} = sub { | |
276 | my $self = shift; | |
277 | return $self->handle($info, @_); | |
278 | }; | |
279 | goto &$AUTOLOAD; | |
280 | } | |
281 | ||
282 | sub method_attributes { | |
283 | my ($self) = @_; | |
284 | ||
285 | return $method_registry->{$self}; | |
286 | } | |
287 | ||
288 | sub map_method_by_name { | |
289 | my ($self, $name) = @_; | |
290 | ||
291 | my $info = $method_by_name->{$self}->{$name}; | |
292 | die "no such method '${self}::$name'\n" if !$info; | |
293 | ||
294 | return $info; | |
295 | } | |
296 | ||
297 | sub map_path_to_methods { | |
298 | my ($class, $stack, $uri_param) = @_; | |
299 | ||
300 | my $path_lookup = $method_path_lookup->{$class}; | |
301 | ||
302 | while (defined(my $comp = shift @$stack)) { | |
303 | return undef if !$path_lookup; # not registerd? | |
304 | if ($path_lookup->{regex}) { | |
305 | my $name = $path_lookup->{regex}->{match_name}; | |
306 | my $regex = $path_lookup->{regex}->{match_re}; | |
307 | ||
308 | return undef if $comp !~ m/^($regex)$/; | |
309 | $uri_param->{$name} = $1; | |
310 | $path_lookup = $path_lookup->{regex}; | |
311 | } elsif ($path_lookup->{folders}) { | |
312 | $path_lookup = $path_lookup->{folders}->{$comp}; | |
313 | } else { | |
314 | die "internal error"; | |
315 | } | |
316 | ||
317 | return undef if !$path_lookup; | |
318 | ||
319 | if (my $info = $path_lookup->{SUBCLASS}) { | |
320 | $class = $info->{subclass}; | |
321 | ||
322 | my $fd = $info->{fragmentDelimiter}; | |
323 | ||
324 | if (defined($fd)) { | |
325 | # we only support the empty string '' (match whole URI) | |
326 | die "unsupported fragmentDelimiter '$fd'" | |
327 | if $fd ne ''; | |
328 | ||
329 | $stack = [ join ('/', @$stack) ] if scalar(@$stack) > 1; | |
330 | } | |
331 | $path_lookup = $method_path_lookup->{$class}; | |
332 | } | |
333 | } | |
334 | ||
335 | return undef if !$path_lookup; | |
336 | ||
337 | return ($class, $path_lookup); | |
338 | } | |
339 | ||
340 | sub find_handler { | |
341 | my ($class, $method, $path, $uri_param) = @_; | |
342 | ||
343 | my $stack = [ grep { length($_) > 0 } split('\/+' , $path)]; # skip empty fragments | |
344 | ||
345 | my ($handler_class, $path_info); | |
346 | eval { | |
347 | ($handler_class, $path_info) = $class->map_path_to_methods($stack, $uri_param); | |
348 | }; | |
349 | my $err = $@; | |
350 | syslog('err', $err) if $err; | |
351 | ||
352 | return undef if !($handler_class && $path_info); | |
353 | ||
354 | my $method_info = $path_info->{$method}; | |
355 | ||
356 | return undef if !$method_info; | |
357 | ||
358 | return ($handler_class, $method_info); | |
359 | } | |
360 | ||
361 | sub handle { | |
362 | my ($self, $info, $param) = @_; | |
363 | ||
364 | my $func = $info->{code}; | |
365 | ||
366 | if (!($info->{name} && $func)) { | |
367 | raise("Method lookup failed ('$info->{name}')\n", | |
368 | code => HTTP_INTERNAL_SERVER_ERROR); | |
369 | } | |
370 | ||
371 | if (my $schema = $info->{parameters}) { | |
372 | # warn "validate ". Dumper($param}) . "\n" . Dumper($schema); | |
373 | PVE::JSONSchema::validate($param, $schema); | |
374 | # untaint data (already validated) | |
375 | while (my ($key, $val) = each %$param) { | |
376 | ($param->{$key}) = $val =~ /^(.*)$/s; | |
377 | } | |
378 | } | |
379 | ||
380 | my $result = &$func($param); | |
381 | ||
382 | # todo: this is only to be safe - disable? | |
383 | if (my $schema = $info->{returns}) { | |
384 | PVE::JSONSchema::validate($result, $schema, "Result verification vailed\n"); | |
385 | } | |
386 | ||
387 | return $result; | |
388 | } | |
389 | ||
390 | # generate usage information for command line tools | |
391 | # | |
392 | # $name ... the name of the method | |
393 | # $prefix ... usually something like "$exename $cmd" ('pvesm add') | |
394 | # $arg_param ... list of parameters we want to get as ordered arguments | |
395 | # on the command line (or single parameter name for lists) | |
396 | # $fixed_param ... do not generate and info about those parameters | |
397 | # $format: | |
398 | # 'long' ... default (list all options) | |
399 | # 'short' ... command line only (one line) | |
400 | # 'full' ... also include description | |
401 | # $hidepw ... hide password option (use this if you provide a read passwork callback) | |
402 | sub usage_str { | |
403 | my ($self, $name, $prefix, $arg_param, $fixed_param, $format, $hidepw) = @_; | |
404 | ||
405 | $format = 'long' if !$format; | |
406 | ||
407 | my $info = $self->map_method_by_name($name); | |
408 | my $schema = $info->{parameters}; | |
409 | my $prop = $schema->{properties}; | |
410 | ||
411 | my $out = ''; | |
412 | ||
413 | my $arg_hash = {}; | |
414 | ||
415 | my $args = ''; | |
416 | ||
417 | $arg_param = [ $arg_param ] if $arg_param && !ref($arg_param); | |
418 | ||
419 | foreach my $p (@$arg_param) { | |
420 | next if !$prop->{$p}; # just to be sure | |
421 | my $pd = $prop->{$p}; | |
422 | ||
423 | $arg_hash->{$p} = 1; | |
424 | $args .= " " if $args; | |
425 | if ($pd->{format} && $pd->{format} =~ m/-list/) { | |
426 | $args .= "{<$p>}"; | |
427 | } else { | |
428 | $args .= $pd->{optional} ? "[<$p>]" : "<$p>"; | |
429 | } | |
430 | } | |
431 | ||
432 | my $get_prop_descr = sub { | |
433 | my ($k, $display_name) = @_; | |
434 | ||
435 | my $phash = $prop->{$k}; | |
436 | ||
437 | my $res = ''; | |
438 | ||
439 | my $descr = $phash->{description} || "no description available"; | |
440 | chomp $descr; | |
441 | ||
442 | my $type = PVE::PodParser::schema_get_type_text($phash); | |
443 | ||
444 | if ($hidepw && $k eq 'password') { | |
445 | $type = ''; | |
446 | } | |
447 | ||
448 | my $defaulttxt = ''; | |
449 | if (defined(my $dv = $phash->{default})) { | |
450 | $defaulttxt = " (default=$dv)"; | |
451 | } | |
452 | my $tmp = sprintf " %-10s %s$defaulttxt\n", $display_name, "$type"; | |
453 | my $indend = " "; | |
454 | ||
455 | $res .= Text::Wrap::wrap('', $indend, ($tmp)); | |
456 | $res .= "\n", | |
457 | $res .= Text::Wrap::wrap($indend, $indend, ($descr)) . "\n\n"; | |
458 | ||
459 | if (my $req = $phash->{requires}) { | |
460 | my $tmp = "Requires option(s): "; | |
461 | $tmp .= ref($req) ? join(', ', @$req) : $req; | |
462 | $res .= Text::Wrap::wrap($indend, $indend, ($tmp)). "\n\n"; | |
463 | } | |
464 | ||
465 | return $res; | |
466 | }; | |
467 | ||
468 | my $argdescr = ''; | |
469 | foreach my $k (@$arg_param) { | |
470 | next if defined($fixed_param->{$k}); # just to be sure | |
471 | next if !$prop->{$k}; # just to be sure | |
472 | $argdescr .= &$get_prop_descr($k, "<$k>"); | |
473 | } | |
474 | ||
475 | my $idx_param = {}; # -vlan\d+ -scsi\d+ | |
476 | ||
477 | my $opts = ''; | |
478 | foreach my $k (sort keys %$prop) { | |
479 | next if $arg_hash->{$k}; | |
480 | next if defined($fixed_param->{$k}); | |
481 | ||
482 | my $type = $prop->{$k}->{type} || 'string'; | |
483 | ||
484 | next if $hidepw && ($k eq 'password') && !$prop->{$k}->{optional}; | |
485 | ||
486 | my $base = $k; | |
487 | if ($k =~ m/^([a-z]+)(\d+)$/) { | |
488 | my $name = $1; | |
489 | next if $idx_param->{$name}; | |
490 | $idx_param->{$name} = 1; | |
491 | $base = "${name}[n]"; | |
492 | } | |
493 | ||
494 | $opts .= &$get_prop_descr($k, "-$base"); | |
495 | ||
496 | if (!$prop->{$k}->{optional}) { | |
497 | $args .= " " if $args; | |
498 | $args .= "-$base <$type>" | |
499 | } | |
500 | } | |
501 | ||
502 | $out .= "USAGE: " if $format ne 'short'; | |
503 | ||
504 | $out .= "$prefix $args"; | |
505 | ||
506 | $out .= $opts ? " [OPTIONS]\n" : "\n"; | |
507 | ||
508 | return $out if $format eq 'short'; | |
509 | ||
510 | if ($info->{description} && $format eq 'full') { | |
511 | my $desc = Text::Wrap::wrap(' ', ' ', ($info->{description})); | |
512 | $out .= "\n$desc\n\n"; | |
513 | } | |
514 | ||
515 | $out .= $argdescr if $argdescr; | |
516 | ||
517 | $out .= $opts if $opts; | |
518 | ||
519 | return $out; | |
520 | } | |
521 | ||
522 | sub cli_handler { | |
523 | my ($self, $prefix, $name, $args, $arg_param, $fixed_param, $pwcallback) = @_; | |
524 | ||
525 | my $info = $self->map_method_by_name($name); | |
526 | ||
527 | my $res; | |
528 | eval { | |
529 | my $param = PVE::JSONSchema::get_options($info->{parameters}, $args, $arg_param, $fixed_param, $pwcallback); | |
530 | $res = $self->handle($info, $param); | |
531 | }; | |
532 | if (my $err = $@) { | |
533 | my $ec = ref($err); | |
534 | ||
535 | die $err if !$ec || $ec ne "PVE::Exception" || !$err->is_param_exc(); | |
536 | ||
537 | $err->{usage} = $self->usage_str($name, $prefix, $arg_param, $fixed_param, 'short', $pwcallback); | |
538 | ||
539 | die $err; | |
540 | } | |
541 | ||
542 | return $res; | |
543 | } | |
544 | ||
545 | # utility methods | |
546 | # note: this modifies the original hash by adding the id property | |
547 | sub hash_to_array { | |
548 | my ($hash, $idprop) = @_; | |
549 | ||
550 | my $res = []; | |
551 | return $res if !$hash; | |
552 | ||
553 | foreach my $k (keys %$hash) { | |
554 | $hash->{$k}->{$idprop} = $k; | |
555 | push @$res, $hash->{$k}; | |
556 | } | |
557 | ||
558 | return $res; | |
559 | } | |
560 | ||
561 | 1; |