| 1 | # SPDX-FileCopyrightText: SUSE LLC | ||||||||||||||||||||
| 2 | # SPDX-License-Identifier: GPL-2.0-or-later | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | package Cavil::CLI::Client; | ||||||||||||||||||||
| 5 | 7 | ▶ | use Mojo::Base -base, -signatures; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 7 | ▶ | use Mojo::URL; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 7 | ▶ | use Mojo::UserAgent; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 7 | has 'on_wait'; # optional coderef, called ~10x/sec while a request is in flight (drives the spinner) | |||||||||||||||||||
| 11 | 0 | ▶ | has token => sub { die "A Cavil API token is required (run 'cavil-cli config', or set CAVIL_API_KEY)\n" }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 12 | 7 | ▶ | has ua => sub { Mojo::UserAgent->new->connect_timeout(30)->inactivity_timeout(120) }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 0 | ▶ | has url => sub { die "A Cavil URL is required (run 'cavil-cli config', or set CAVIL_URL)\n" }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | # The identity the API token belongs to: a quick way to confirm the url and token are set up right. | ||||||||||||||||||||
| 16 | 14 | ▶ | sub whoami ($self) { | ||||||||||||||||||
|
Subroutine
whoami: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 17 | 14 | return $self->_request('GET', '/api/v1/whoami')->json; | |||||||||||||||||||
| 18 | } | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | # Submit a source archive for a standard legal review. Returns {saved => {...}, duplicate => bool}; the same | ||||||||||||||||||||
| 21 | # archive under the same name is idempotent (duplicate true). A 403 (key may not submit), 400 (checksum | ||||||||||||||||||||
| 22 | # mismatch) and 413 (archive over the server limit) are the user's to fix, so they surface as friendly messages. | ||||||||||||||||||||
| 23 | 8 | ▶ | sub upload ($self, $tarball, $meta) { | ||||||||||||||||||
|
Subroutine
upload: called CC=6 SCAR=19.3
Pod
undocumented
| |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | # ephemeral asks for a one-off report with no lasting side effects (no open review left in the legal backlog), | ||||||||||||||||||||
| 26 | # which is all this client ever wants. Servers without the planned ad-hoc mode ignore it, so it is always sent. | ||||||||||||||||||||
| 27 | my $form = { | ||||||||||||||||||||
| 28 | name => $meta->{name}, | ||||||||||||||||||||
| 29 | priority => $meta->{priority}, | ||||||||||||||||||||
| 30 | checksum => $meta->{checksum}, | ||||||||||||||||||||
| 31 | ephemeral => 1, | ||||||||||||||||||||
| 32 | tarball => {file => $tarball}, | ||||||||||||||||||||
| 33 | 8 | ▶ | (defined $meta->{external_link} ? (external_link => $meta->{external_link}) : ()) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 34 | }; | ||||||||||||||||||||
| 35 | 8 | my $res = $self->_request('POST', '/api/v1/packages/upload', {form => $form, ok_codes => [400, 403, 413]}); | |||||||||||||||||||
| 36 | 7 | ▶ | die "This Cavil key may not submit packages (needs a read-write key with admin access)\n" if $res->code == 403; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | 6 | ▶ | die "The archive is too large for this Cavil instance; trim it with a .cavilignore file or --exclude-path\n" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | if $res->code == 413; | ||||||||||||||||||||
| 39 | 0 | ▶ | die "Upload rejected: @{[$res->json->{error} // 'bad request']}\n" if $res->code == 400; | ||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$res->json->{"error"} // "bad request"
MC/DC0%0/1
$res->json->{"error"} // "bad request"
$res->json->{"error"}
Truth table0%0/2
A
A= $res->json->{"error"}
| |||||||||||||||||||||
| 40 | 5 | return $res->json; | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | # Fetch a report. Returns {ready => 1, data => ...} when it exists, or {ready => 0, stage => ...} while it is | ||||||||||||||||||||
| 44 | # still being built (the endpoint answers 408 with the pipeline stage until the package is analyzed). | ||||||||||||||||||||
| 45 | 6 | ▶ | sub report ($self, $id, $format = 'json') { | ||||||||||||||||||
|
Subroutine
report: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||
| 46 | 6 | my $res = $self->_request('GET', "/api/v1/report/$id.$format", {ok_codes => [408]}); | |||||||||||||||||||
| 47 | 6 | ▶ | return {ready => 1, data => ($format eq 'json' ? $res->json : $res->text)} if $res->code == 200; | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 48 | 1 | return {ready => 0, stage => eval { $res->json->{stage} }}; | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | # Fetch a generated document (spdx or notice), or undef while it is still being generated (408). Bytes are | ||||||||||||||||||||
| 52 | # returned ready to write; the user agent transparently decompresses the server's gzip. | ||||||||||||||||||||
| 53 | 2 | ▶ | sub document ($self, $id, $key) { | ||||||||||||||||||
|
Subroutine
document: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 54 | 2 | my $res = $self->_request('GET', "/api/v1/documents/$id/$key", {ok_codes => [408]}); | |||||||||||||||||||
| 55 | 2 | ▶ | return undef if $res->code == 408; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | 1 | return $res->body; | |||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 30 | ▶ | sub _headers ($self) { | ||||||||||||||||||
|
Subroutine
_headers: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 60 | 30 | return {Authorization => 'Bearer ' . $self->token}; | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 30 | ▶ | sub _request ($self, $method, $path, $options = {}) { | ||||||||||||||||||
|
Subroutine
_request: called CC=10 SCAR=23.1
| |||||||||||||||||||||
| 64 | 30 | my $ua = $self->ua; | |||||||||||||||||||
| 65 | 30 | ▶ | my @body = $options->{json} ? (json => $options->{json}) : $options->{form} ? (form => $options->{form}) : (); | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 66 | 30 | my $tx = $ua->build_tx($method => $self->_url($path) => $self->_headers, @body); | |||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | # A recurring timer on the UA's own loop fires during the blocking request (that loop is what start() runs), | ||||||||||||||||||||
| 69 | # so the caller's spinner keeps moving while we wait on the server. | ||||||||||||||||||||
| 70 | 30 | my $spin = $self->on_wait; | |||||||||||||||||||
| 71 | 30 | ▶ | my $tid = $spin ? $ua->ioloop->recurring(0.1 => $spin) : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | 30 | $tx = $ua->start($tx); | |||||||||||||||||||
| 73 | 30 | ▶ | $ua->ioloop->remove($tid) if defined $tid; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 30 | ▶ | return $tx->result unless my $err = $tx->error; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | # No status code means the request never reached the server: always fatal. An expected status (ok_codes, e.g. | ||||||||||||||||||||
| 78 | # 408 while a report builds) is handed back for the caller to act on; anything else is a friendly die with no | ||||||||||||||||||||
| 79 | # Carp file/line suffix leaking to the user. | ||||||||||||||||||||
| 80 | 6 | ▶ | die "Connection error from Cavil ($method $path): $err->{message}\n" unless $err->{code}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 6 | ▶ | return $tx->result if grep { $_ == $err->{code} } @{$options->{ok_codes} // []}; | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$options->{"ok_codes"} // []
MC/DC100%1/1
$options->{"ok_codes"} // []
$options->{"ok_codes"}
Truth table100%2/2
A
A= $options->{"ok_codes"}
| |||||||||||||||||||||
| 82 | 2 | die "$err->{code} response from Cavil ($method $path): $err->{message}\n"; | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 30 | ▶ | sub _url ($self, $path) { | ||||||||||||||||||
|
Subroutine
_url: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 86 | 30 | return Mojo::URL->new($self->url . $path); | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 7 | 1; | |||||||||||||||||||