Skip to content

Commit f5e2dea

Browse files
committed
Review the communication protocol.
Based on issue #1 it was found out that different servers use different ways to pass the return status (an HTTP code vs. an application string in the HTTP body). In order to unify the handling, the client relies solely on HTTP codes now. In particular the client doesn't expect any more a string returned by the server (see the --expect option). The basics of the protocol are described in the documentation. The change also introduces the Perl LWP library to handle HTTP messaging and doesn't require any external commands for that. Some command line options become superfluous with the change and removed.
1 parent 0abcc29 commit f5e2dea

3 files changed

Lines changed: 28 additions & 50 deletions

File tree

bin/pakiti-client

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ use File::Temp qw(tempdir);
1717
use FindBin qw($Bin $Script);
1818
use Getopt::Long qw(GetOptions);
1919
use Pod::Usage qw(pod2usage);
20+
use LWP::UserAgent ();
2021

2122
#
2223
# constants
2324
#
2425

2526
use constant COMMANDS => qw(
26-
curl dpkg-query hostname lsb_release openssl pkg rpm svmon uname wget
27+
dpkg-query hostname lsb_release openssl pkg rpm svmon uname
2728
);
2829

2930
use constant PROTOCOL_VERSION => "5";
@@ -373,24 +374,8 @@ sub encrypt_report ($) {
373374

374375
sub send_report ($) {
375376
my($report) = @_;
376-
my($data, @command, $output, $url, @pairs);
377+
my($response, $url, @pairs, $ua);
377378

378-
$TempDir ||= tempdir(CLEANUP => 1);
379-
$data = "$TempDir/data";
380-
write_file($data, $report);
381-
if ($Option{curl}) {
382-
@command = ($Option{curl});
383-
push(@command, qw(-q --silent -X POST));
384-
push(@command, qw(--include --show-error)) if $Option{debug};
385-
push(@command, "--data-binary", "\@" . $data);
386-
} elsif ($Option{wget}) {
387-
@command = ($Option{wget});
388-
push(@command, qw(--tries=1 --output-document=-));
389-
push(@command, qw(--server-response)) if $Option{debug};
390-
push(@command, "--post-file", $data);
391-
} else {
392-
die("$Script: cannot send to $Option{url}: curl/wget not installed\n");
393-
}
394379
# The caller may specify additional information that doesn't describe the
395380
# actual patch state but may be useful for the processing. Options recognized
396381
# as such are sent in the query string.
@@ -402,15 +387,15 @@ sub send_report ($) {
402387

403388
$url = "$Option{url}";
404389
$url = $url . '?' . join('&', @pairs) if @pairs;
405-
## no critic 'InputOutput::ProhibitBacktickOperators'
406-
$output = qx(@command "$url" 2>&1);
407-
if ($output =~ /$Option{expect}$/ and $? == 0) {
408-
print(STDERR "report successfully sent\n") if -t STDERR;
409-
print $output if $output;
410-
return;
411-
}
412-
warn($output) if $output;
413-
die("$Script: failed to send data using $command[0]\n");
390+
391+
$ua = LWP::UserAgent->new;
392+
push @{$ua->requests_redirectable}, 'POST';
393+
394+
$response = $ua->post($url, Content => $report);
395+
$response->is_success or die($response->status_line);
396+
397+
print(STDERR "report successfully sent\n") if -t STDERR;
398+
print $response->decoded_content;
414399
}
415400

416401
#
@@ -459,9 +444,7 @@ sub init () {
459444
$| = 1;
460445
%spec = (
461446
"config" => "|conf=s",
462-
"debug" => "|d",
463447
"encrypt" => "=s",
464-
"expect" => "=s",
465448
"help" => "|h|?",
466449
"host" => "=s",
467450
"input" => "|i=s",
@@ -494,11 +477,8 @@ sub init () {
494477
foreach my $name (COMMANDS()) {
495478
$Option{$name} = which($name) unless defined($Option{$name});
496479
}
497-
$Option{expect} = "OK" unless defined($Option{expect});
498480
die("$Script: option --encrypt requires openssl\n")
499481
if $Option{encrypt} and not $Option{openssl};
500-
die("$Script: option --url requires curl or wget\n")
501-
if $Option{url} and not $Option{curl} and not $Option{wget};
502482
}
503483

504484
#
@@ -611,14 +591,6 @@ with (using bash):
611591
612592
use this configuration file before processing the command line parameters
613593
614-
=item B<--curl> I<PATH>
615-
616-
set the path of the C<curl> command to use
617-
618-
=item B<--debug>, B<-d>
619-
620-
enable debug mode
621-
622594
=item B<--dpkg-query> I<PATH>
623595
624596
set the path of the C<dpkg-query> command to use
@@ -629,11 +601,6 @@ use this certificate to encrypt the report; the value can either be the path
629601
of the file containing the certificate or the certificate itself as multi-line
630602
ASCII armored contents
631603
632-
=item B<--expect> I<STRING>
633-
634-
set the response string to expect from the server in case of success
635-
(default: C<OK>)
636-
637604
=item B<--help>, B<-h>, B<-?>
638605
639606
show some help
@@ -712,10 +679,6 @@ set the path of the C<uname> command to use
712679
713680
send the prepared report to the collecting server at the given URL
714681
715-
=item B<--wget> I<PATH>
716-
717-
set the path of the C<wget> command to use
718-
719682
=back
720683
721684
=head1 CONFIGURATION FILE
@@ -777,6 +740,20 @@ A I<package line> contains the package name, a tab character (0x09), the package
777740
full version, another tab and the package architecture. For C<rpm> based systems,
778741
the full version is in fact I<EPOCH>:I<VERSION>:I<RELEASE>.
779742
743+
=head1 PROTOCOL
744+
745+
The messages are sent using the HTTP protocol and the POST method. The
746+
messages are sent over an TLS-protected channel unless the report is sent as
747+
an encrypted blob, in which case a plain HTTP connection is recommended to
748+
use. Messages containing the encrypted blob are label using the
749+
application/octet-stream mime type. The server uses HTTP codes to signal the
750+
status of delivery. Servers may return human-readable information
751+
in the body of HTTP response, which provides additional details about the
752+
processing.
753+
754+
The client can use query string attributes to pass additional parameters
755+
important for the processing.
756+
780757
=head1 AUTHOR
781758
782759
Lionel Cons L<http://cern.ch/lionel.cons>

debian/control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Homepage: https://github.com/CESNET/pakiti3
99

1010
Package: pakiti-client
1111
Architecture: all
12-
Depends: ${misc:Depends}, curl, openssl, perl
12+
Depends: ${misc:Depends}, openssl, perl, liblwp-protocol-https-perl
1313
Description: Patch Status Monitoring Tool
1414
Pakiti provides a monitoring mechanism to check the patching status of
1515
Linux systems.

pakiti.spec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Source0: %{url}/archive/v%{version}.tar.gz
99
BuildRoot: %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX)
1010
BuildArch: noarch
1111
BuildRequires: perl
12+
Requires: openssl perl perl-LWP-Protocol-https
1213

1314
%description
1415
Pakiti provides a monitoring mechanism to check the patching status of

0 commit comments

Comments
 (0)