diff --git a/PVE/API2Client.pm b/PVE/API2Client.pm deleted file mode 100755 index a68c766f..00000000 --- a/PVE/API2Client.pm +++ /dev/null @@ -1,200 +0,0 @@ -package PVE::API2Client; - -use strict; -use warnings; -use URI; -use HTTP::Cookies; -use LWP::UserAgent; -use JSON; -use Data::Dumper; # fixme: remove -use HTTP::Request::Common; - -sub get { - my ($self, $path, $param) = @_; - - return $self->call('GET', $path, $param); -} - -sub post { - my ($self, $path, $param) = @_; - - return $self->call('POST', $path, $param); -} - -sub put { - my ($self, $path, $param) = @_; - - return $self->call('PUT', $path, $param); -} - -sub delete { - my ($self, $path, $param) = @_; - - return $self->call('DELETE', $path, $param); -} - -sub update_ticket { - my ($self, $ticket) = @_; - - my $domain = "$self->{host}.local" unless $self->{host} =~ /\./; - $self->{cookie_jar}->set_cookie(0, 'PVEAuthCookie', $ticket, '/', $domain); -} - -sub login { - my ($self) = @_; - - my $uri = URI->new(); - $uri->scheme($self->{protocol}); - $uri->host($self->{host}); - $uri->port($self->{port}); - $uri->path('/api2/json/access/ticket'); - - my $ua = $self->{useragent}; - - my $response = $ua->post($uri, { - username => $self->{username} || 'unknown', - password => $self->{password} || ''}); - - if (!$response->is_success) { - die $response->status_line . "\n"; - } - - my $data = from_json($response->decoded_content, {utf8 => 1, allow_nonref => 1}); - - $self->update_ticket($data->{data}->{ticket}); - $self->{csrftoken} = $data->{data}->{CSRFPreventionToken}; - - return $data; -} - -sub call { - my ($self, $method, $path, $param) = @_; - - #print "wrapper called\n"; - - my $ticket; - - my $ua = $self->{useragent}; - my $cj = $self->{cookie_jar}; - - $cj->scan(sub { - my ($version, $key, $val) = @_; - $ticket = $val if $key eq 'PVEAuthCookie'; - }); - - if (!$ticket && $self->{username} && $self->{password}) { - $self->login(); - } - - my $uri = URI->new(); - $uri->scheme($self->{protocol}); - $uri->host($self->{host}); - $uri->port($self->{port}); - $uri->path($path); - - # print $ua->{cookie_jar}->as_string; - - #print "CALL $method : " . $uri->as_string() . "\n"; - - if ($self->{csrftoken}) { - $self->{useragent}->default_header('CSRFPreventionToken' => $self->{csrftoken}); - } - - my $response; - if ($method eq 'GET') { - $uri->query_form($param); - $response = $ua->request(HTTP::Request::Common::GET($uri)); - } elsif ($method eq 'POST') { - $response = $ua->request(HTTP::Request::Common::POST($uri, Content => $param)); - } elsif ($method eq 'PUT') { - # We use another temporary URI object to format - # the application/x-www-form-urlencoded content. - - my $tmpurl = URI->new('http:'); - $tmpurl->query_form(%$param); - my $content = $tmpurl->query; - - $response = $ua->request(HTTP::Request::Common::PUT($uri, 'Content-Type' => 'application/x-www-form-urlencoded', Content => $content)); - - } elsif ($method eq 'DELETE') { - $response = $ua->request(HTTP::Request::Common::DELETE($uri)); - } else { - die "method $method not implemented\n"; - } - - #print "RESP: " . Dumper($response) . "\n"; - - my $ct = $response->header('Content-Type') || ''; - - if ($response->is_success) { - - die "got unexpected content type" if $ct !~ m|application/json|; - - return from_json($response->decoded_content, {utf8 => 1, allow_nonref => 1}); - - } else { - - my $msg = $response->status_line . "\n"; - eval { - return if $ct !~ m|application/json|; - my $res = from_json($response->decoded_content, {utf8 => 1, allow_nonref => 1}); - if (my $errors = $res->{errors}) { - foreach my $key (keys %$errors) { - my $m = $errors->{$key}; - chomp($m); - $m =~s/\n/ -- /g; - $msg .= " $key: $m\n"; - } - } - }; - die $msg; - - } -} - -sub new { - my ($class, %param) = @_; - - my $default_ca = "/etc/pve/pve-root-ca.pem"; - - my $ssl_default_opts = { verify_hostname => 0 }; - $ssl_default_opts->{SSL_ca_file} = $default_ca - if -f $default_ca; - - my $self = { - ticket => $param{ticket}, - csrftoken => $param{csrftoken}, - username => $param{username}, - password => $param{password}, - host => $param{host} || 'localhost', - port => $param{port}, - protocol => $param{protocol}, - ssl_opts => $param{ssl_opts} || $ssl_default_opts, - timeout => $param{timeout} || 60, - }; - bless $self; - - if (!$self->{port}) { - $self->{port} = $self->{host} eq 'localhost' ? 85 : 8006; - } - if (!$self->{protocol}) { - $self->{protocol} = $self->{host} eq 'localhost' ? 'http' : 'https'; - } - - $self->{cookie_jar} = HTTP::Cookies->new (ignore_discard => 1); - - $self->update_ticket($self->{ticket}) if $self->{ticket}; - - $self->{useragent} = LWP::UserAgent->new( - cookie_jar => $self->{cookie_jar}, - protocols_allowed => [ 'http', 'https'], - ssl_opts => $self->{ssl_opts}, - timeout => $self->{timeout}, - ); - - $self->{useragent}->default_header('Accept-Encoding' => 'gzip'); # allow gzip - - return $self; -} - -1; diff --git a/PVE/Makefile b/PVE/Makefile index 598023a5..3fd71fd1 100644 --- a/PVE/Makefile +++ b/PVE/Makefile @@ -5,7 +5,6 @@ SUBDIRS=API2 VZDump Status CLI Service PERLSOURCE = \ API2.pm \ API2Tools.pm \ - API2Client.pm \ ExtJSIndex.pm \ TouchIndex.pm \ NoVncIndex.pm \ diff --git a/bin/test/Makefile b/bin/test/Makefile index 5007c096..6833adff 100644 --- a/bin/test/Makefile +++ b/bin/test/Makefile @@ -5,14 +5,9 @@ all: check: ./balloontest.pl -SCRIPTS = \ - example1.pl \ - example2.pl - -.PHONY: install -install: ${SCRIPTS} - install -d ${DOCDIR}/examples - install -m 0755 ${SCRIPTS} ${DOCDIR}/examples +.PHONY: install +install: + # do nothing .PHONY: clean clean: diff --git a/bin/test/example1.pl b/bin/test/example1.pl deleted file mode 100755 index 1bce5514..00000000 --- a/bin/test/example1.pl +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/perl -w - - -use strict; -use PVE::API2Client; -use PVE::AccessControl; -use PVE::INotify; - -use Data::Dumper; - -my $hostname = PVE::INotify::read_file("hostname"); - -# normally you use username/password, -# but we can simply create a ticket and CRSF token if we are root -my $ticket = PVE::AccessControl::assemble_ticket('root@pam'); -my $csrftoken = PVE::AccessControl::assemble_csrf_prevention_token('root@pam'); - -my $conn = PVE::API2Client->new( - #username => 'root@pam', - #password => 'yourpassword', - ticket => $ticket, - csrftoken => $csrftoken, - host => $hostname, - ); - -my $res = $conn->get("api2/json/", {}); - -print "TEST: " . Dumper($res); diff --git a/bin/test/example2.pl b/bin/test/example2.pl deleted file mode 100755 index 73682428..00000000 --- a/bin/test/example2.pl +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/perl -w - - -use strict; -use PVE::API2Client; -use PVE::AccessControl; -use PVE::INotify; - -use Data::Dumper; - -my $hostname = PVE::INotify::read_file("hostname"); - -# normally you use username/password, -# but we can simply create a ticket and CRSF token if we are root -my $ticket = PVE::AccessControl::assemble_ticket('root@pam'); -my $csrftoken = PVE::AccessControl::assemble_csrf_prevention_token('root@pam'); - -my $conn = PVE::API2Client->new( - #username => 'root@pam', - #password => 'yourpassword', - ticket => $ticket, - csrftoken => $csrftoken, - host => $hostname, - ); - -my $res = $conn->get("api2/json/access/domains", {}); -print "TEST: " . Dumper($res); diff --git a/bin/test/perftest1.pl b/bin/test/perftest1.pl deleted file mode 100755 index 7c6c470e..00000000 --- a/bin/test/perftest1.pl +++ /dev/null @@ -1,75 +0,0 @@ -#!/usr/bin/perl -w - -use lib '../../'; -use strict; -use Time::HiRes qw( usleep ualarm gettimeofday tv_interval ); -use PVE::API2Client; -use PVE::INotify; - -use Data::Dumper; - -my $hostname = PVE::INotify::read_file("hostname"); - -# normally you use username/password, -# but we can simply create a ticket if we are root -my $ticket = PVE::AccessControl::assemble_ticket('root@pam'); - -my $wcount = 1; -my $qcount = 100; - -sub test_rpc { - my ($host) = @_; - - my $conn = PVE::API2Client->new( - #username => 'root@pam', - #password => 'yourpassword', - ticket => $ticket, - host => $host, - ); - - for (my $i = 0; $i < $qcount; $i++) { - eval { - my $res = $conn->get("api2/json", {}); - }; - - my $err = $@; - - if ($err) { - - print "ERROR: $err\n"; - last; - } - } -} - -sub run_tests { - my ($host) = @_; - - my $workers; - - my $starttime = [gettimeofday]; - - for (my $i = 0; $i < $wcount; $i++) { - if (my $pid = fork ()) { - $workers->{$pid} = 1; - } else { - test_rpc ($host); - exit (0); - } - } - - # wait for children - 1 while (wait > 0); - - my $elapsed = int(tv_interval ($starttime) * 1000); - - my $tpq = $elapsed / ($wcount*$qcount); - - print "$host: $tpq ms per query\n"; -} - -# TODO: Apache is much slower, why? (SSL?) - -run_tests("localhost"); # test 'pvedaemon' - -run_tests($hostname); # test 'apache'