lib/OpenSearch/PPLQuery/Connection.pm

filter: Statementstmt 100% 169 / 169 Branchbran 96.3% 131 / 136 Conditioncond 94.2% 65 / 69 MC/DCmcdc 89.7% 35 / 39 Subroutinesub 100% 19 / 19 Podpod 0.0% 0 / 14 totaltotal 93.9% 419 / 446 CC 96 SCAR 31.7
CC
96
Cov
98%
direct_connection
42.0
parse_query_header
28.0
validate_header_connection
6.0
CRAP 23.7
↑ Summary lib/OpenSearch/PPLQuery.pm »
1 package OpenSearch::PPLQuery::Connection;
2 27 $OpenSearch::PPLQuery::Connection::VERSION = '1.1.0';
3 # Internal connection-header parser and source merger for bin/pplquery. The
4 # functions intentionally retain source-specific behavior: query headers can
5 # select a connection, while --connection-file is an explicitly selected,
6 # standalone connection source.
7 #
8 # resolve_connection is the entry point. It reads the query and its metadata,
9 # merges every connection source, and is the one sequence both bin/pplquery
10 # and the tests run, so the order errors are reported in cannot drift.
11  
12 27 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 27 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 27 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 27 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 27 ▶use OpenSearch::PPLQuery ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 27 my $SEPARATOR_HELP = "Add a line containing nothing, or only spaces and tabs, between the last header field and the query.\n";
20 27 my $LINE_ENDING_HELP = "Save the file with LF or CRLF line endings; a lone CR is not a line ending here.\n";
21  
22 146 ▶sub resolve_connection {
Subroutine resolve_connection: called CC=5 SCAR=16.1
Pod undocumented
23 146 my ($cli, $connection_file, $source, $stdin) = @_;
24 146 my $selected = defined $connection_file;
25 146 ▶ my $header = $selected ? read_connection_file($connection_file) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$selected ? : 31 115 146
26 135 my ($query, $query_header) = read_query($source, $stdin, $selected);
27 77 ▶ if ($query !~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($query =~ /\S/u) 3 74 77
28 3 ▶ die 'Query is empty: ' . ($source eq '-' ? 'standard input' : "query file $source") . " has no PPL text.\n"
Branch: 1 branch
BranchTrueFalseTotal
$source eq '-' ? : 3 0 3
29 . "A connection header and its separator line are metadata, not query text.\n";
30 }
31 74 ▶ return ($query, direct_connection($cli, $selected ? $header : $query_header, $selected));
Branch: 1 branch
BranchTrueFalseTotal
$selected ? : 19 55 74
32 }
33  
34 135 ▶sub read_query {
Subroutine read_query: called CC=3 SCAR=11.0
Pod undocumented
35 135 my ($path, $stdin, $strip_lax_header) = @_;
36 135 my ($query, $directory);
37 135 ▶ if ($path eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq '-') { } 129 6 135
38 129 $query = read_utf8_handle($stdin, 'standard input');
39 128 $directory = File::Spec->rel2abs('.');
40 } else {
41 6 $query = read_utf8_file($path, "query file $path");
42 5 $directory = File::Spec->rel2abs(dirname($path));
43 }
44 133 ▶ return (strip_query_header_lax($query), undef) if $strip_lax_header;
Branch: 1 branch
BranchTrueFalseTotal
if $strip_lax_header 20 113 133
45 113 return parse_query_header($query, $directory);
46 }
47  
48 31 ▶sub read_connection_file {
Subroutine read_connection_file: called CC=3 SCAR=11.0
Pod undocumented
49 31 my ($path) = @_;
50 31 my $text = read_utf8_file($path, "connection file $path");
51 # Normalizing the end of the file covers every way it can terminate, so
52 # the parser below only ever sees a header followed by a blank line. A
53 # bare CR that matters is still rejected on the sentinel or field line.
54 29 $text =~ s/[ \t\r\n]+\z//;
55 29 $text .= "\n\n";
56 29 my ($body, $header) = parse_query_header($text, File::Spec->rel2abs(dirname($path)), 1);
57 21 ▶ if ($body =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /\S/u) 1 20 21
58 1 my ($first) = $body =~ /^[ \t]*(\S.*?)[ \t]*$/m;
59 1 ▶ die "Connection file $path must contain only one connection header and whitespace, but text follows its separator line: "
Condition50%1/2
$first // ""
l!l
10
MC/DC0%0/1
$first // ""
$first
Truth table50%1/2
A
A= $first
Aresult
11
00
60 . OpenSearch::PPLQuery::terminal_text($first // '') . "\nA connection file holds no query. Move the query to its own file.\n";
61 }
62 20 return $header;
63 }
64  
65 52 ▶sub read_utf8_file {
Subroutine read_utf8_file: called CC=3 SCAR=11.0
Pod undocumented
66 52 my ($path, $label) = @_;
67 52 ▶ open my $handle, '<:raw', $path or die "Cannot open $label: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $handle, "<:raw", $path or die "Cannot open ${label}: $!\n" 48 4 52
68 48 my $text = read_utf8_handle($handle, $label);
69 45 ▶ close $handle or die "Cannot close $label: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $handle or die "Cannot close ${label}: $!\n" 45 0 45
70 45 return $text;
71 }
72  
73 177 ▶sub read_utf8_handle {
Subroutine read_utf8_handle: called CC=1 SCAR=0.0
Pod undocumented
74 177 my ($handle, $label) = @_;
75 177 return OpenSearch::PPLQuery::decode_utf8(read_octets_handle($handle, $label), $label);
76 }
77  
78 177 ▶sub read_octets_handle {
Subroutine read_octets_handle: called CC=4 SCAR=15.0
Pod undocumented
79 177 my ($handle, $label) = @_;
80 177 my $octets = do { local $/; <$handle> };
81 177 ▶ die "Cannot read $label: $!\n" if !defined($octets) && $!;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $octets and $! 0 177 177
Condition33%1/3
not defined $octets and $!
!ll&&!rl&&r
17700
MC/DC0%0/2
not defined $octets and $!
not defined $octets $!
Truth table33%1/3
not(A) and B
A= not defined $octetsB= $!
ABresult
0X0
100
111
82 177 ▶ return $octets // '';
Condition50%1/2
$octets // ""
l!l
1770
MC/DC0%0/1
$octets // ""
$octets
Truth table50%1/2
A
A= $octets
Aresult
11
00
83 }
84  
85 142 ▶sub parse_query_header {
Subroutine parse_query_header: called CC=28 SCAR=33.3
Pod undocumented
86 142 my ($query, $directory, $required) = @_;
87 142 ▶ if ($query =~ /\A\/\/ pplquery[ \t]*\r(?!\n)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query =~ m[\A// pplquery[ \t]*\r(?!\n)]u) 2 140 142
88 2 die "The pplquery header uses an unsupported bare CR line ending on line 1\n$LINE_ENDING_HELP";
89 }
90 140 ▶ if ($query !~ /\A\/\/ pplquery[ \t]*\r?\n/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($query =~ m[\A// pplquery[ \t]*\r?\n]u) 23 117 140
91 23 ▶ die "The pplquery header requires a blank separator line, but the input ends at the sentinel.\n$SEPARATOR_HELP"
Branch: 1 branch
BranchTrueFalseTotal
if $query =~ m[\A// pplquery[ \t]*\z]u 0 23 23
92 if $query =~ /\A\/\/ pplquery[ \t]*\z/;
93 23 ▶ die "Connection file must begin with a complete pplquery header sentinel.\nLine 1 must be exactly '// pplquery'.\n" if $required;
Branch: 1 branch
BranchTrueFalseTotal
if $required 1 22 23
94 22 reject_misplaced_sentinel($query);
95 19 return ($query, undef);
96 }
97  
98 117 my $offset = $+[0];
99 117 my $number = 1;
100 117 my @field_lines;
101 117 while (1) {
102 330 my $newline = index($query, "\n", $offset);
103 330 $number++;
104 # $number has already advanced to the line that does not exist, so the
105 # last line actually read is the one worth naming.
106 330 ▶ die 'The pplquery header requires a blank separator line, but the input ended at line ' . ($number - 1)
Branch: 1 branch
BranchTrueFalseTotal
if $newline < 0 2 328 330
107 . " while still inside the header.\n$SEPARATOR_HELP"
108 if $newline < 0;
109 328 my $line = substr($query, $offset, $newline - $offset);
110 328 $offset = $newline + 1;
111 328 $line =~ s/\r\z//;
112 328 ▶ die "The pplquery header uses an unsupported bare CR line ending on line $number\n$LINE_ENDING_HELP" if $line =~ /\r/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\r/u 0 328 328
113 328 ▶ last if $line =~ /\A[ \t]*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A[ \t]*\z/u 115 213 328
114 213 push @field_lines, [$number, $line];
115 }
116  
117 115 my @supported = qw(url user password-environment password-file ca-file tls-verify timeout);
118 115 my %supported = map { $_ => 1 } @supported;
119 115 my %header;
120 115 for my $field_line (@field_lines) {
121 209 my ($line_number, $line) = @$field_line;
122 209 my $shown = 'line ' . $line_number . ': ' . OpenSearch::PPLQuery::terminal_text($line);
123 209 ▶ if ($line !~ /\A\/\/ ([a-z][a-z0-9-]*):(.*)\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ m[\A// ([a-z][a-z0-9-]*):(.*)\z]su) 11 198 209
124 11 ▶ if ($line =~ /\A\/\/ ([^:]*):/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[\A// ([^:]*):]su) 4 7 11
125 4 ▶ die "Header field key must contain only ASCII characters, on $shown\n" if $1 =~ /[^\x00-\x7f]/;
Branch: 1 branch
BranchTrueFalseTotal
if $1 =~ /[^\x00-\x7f]/u 1 3 4
126 3 ▶ die "Header field key must be lowercase, on $shown\n" if $1 =~ /[A-Z]/;
Branch: 1 branch
BranchTrueFalseTotal
if $1 =~ /[A-Z]/u 1 2 3
127 }
128 9 die "Malformed pplquery header field on $shown\nEvery header line must be written exactly '// key: value', with one space after the slashes and no space before the colon.\n";
129 }
130 198 my ($key, $value) = ($1, $2);
131 198 ▶ die "Unknown pplquery header field '$key' on $shown\nSupported fields are " . join(', ', @supported) . ".\n" if !$supported{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $supported{$key} 6 192 198
132 192 ▶ die "Duplicate pplquery header field '$key' on $shown\nIt was already given earlier in this header; keep only one.\n" if exists $header{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $header{$key} 2 190 192
133 190 $value =~ s/\A[ \t]+//;
134 190 $value =~ s/[ \t]+\z//;
135 190 ▶ die "Header field '$key' must not be empty, on $shown\nGive it a value, or remove the line to fall back to the next connection source.\n" if $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "" 8 182 190
136 182 ▶ die "Header field 'password-environment' must name PPLQUERY_[A-Z][A-Z0-9_]*, not '"
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "password-environment" and not $value =~ /\APPLQUERY_[A-Z][A-Z0-9_]*\z/u 5 177 182
Condition100%3/3
$key eq "password-environment" and not $value =~ /\APPLQUERY_[A-Z][A-Z0-9_]*\z/u
!ll&&!rl&&r
160175
MC/DC100%2/2
$key eq "password-environment" and not $value =~ /\APPLQUERY_[A-Z][A-Z0-9_]*\z/u
$key eq "password-environment" not $value =~ /\APPLQUERY_[A-Z][A-Z0-9_]*\z/u
Truth table100%3/3
A and not(B)
A= $key eq "password-environment"B= not $value =~ /\APPLQUERY_[A-Z][A-Z0-9_]*\z/u
ABresult
0X0
100
111
137 . OpenSearch::PPLQuery::terminal_text($value) . "', on $shown\nIt names the environment variable holding the password, never the password itself.\n"
138 if $key eq 'password-environment' && $value !~ /\APPLQUERY_[A-Z][A-Z0-9_]*\z/;
139 177 ▶ die "Header field 'tls-verify' must be exactly 'true' or 'false', not '"
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "tls-verify" and $value ne "true" and $value ne "false" 6 171 177
Condition100%6/6
$key eq "tls-verify" and $value ne "true" and $value ne "false"
!ll&&!rl&&r
16476
$key eq "tls-verify" and $value ne "true"
!ll&&!rl&&r
156813
MC/DC100%3/3
$key eq "tls-verify" and $value ne "true" and $value ne "false"
$key eq "tls-verify" $value ne "true" $value ne "false"
Truth table100%4/4
A and B and C
A= $key eq "tls-verify"B= $value ne "true"C= $value ne "false"
ABCresult
0XX0
10X0
1100
1111
140 . OpenSearch::PPLQuery::terminal_text($value) . "', on $shown\n"
141 if $key eq 'tls-verify' && $value ne 'true' && $value ne 'false';
142 171 ▶ OpenSearch::PPLQuery::validate_timeout($value, "Header field 'timeout' on $shown") if $key eq 'timeout';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "timeout" 19 152 171
143 163 ▶ $header{$key} = $key eq 'timeout' ? 0 + $value : $value;
Branch: 1 branch
BranchTrueFalseTotal
$key eq 'timeout' ? : 11 152 163
144 }
145 die "Header fields 'password-environment' and 'password-file' are mutually exclusive; specify only one\n"
146 69 ▶ if exists($header{'password-environment'}) && exists($header{'password-file'});
Branch: 1 branch
BranchTrueFalseTotal
if exists $header{"password-environment"} and exists $header{"password-file"} 1 68 69
Condition100%3/3
exists $header{"password-environment"} and exists $header{"password-file"}
!ll&&!rl&&r
52161
MC/DC100%2/2
exists $header{"password-environment"} and exists $header{"password-file"}
exists $header{"password-environment"} exists $header{"password-file"}
Truth table100%3/3
A and B
A= exists $header{"password-environment"}B= exists $header{"password-file"}
ABresult
0X0
100
111
147  
148 68 for my $key (qw(password-file ca-file)) {
149 136 ▶ next if !exists($header{$key}) || File::Spec->file_name_is_absolute($header{$key});
Branch: 1 branch
BranchTrueFalseTotal
if not exists $header{$key} or "File::Spec"->file_name_is_absolute($header{$key}) 123 13 136
Condition100%3/3
not exists $header{$key} or "File::Spec"->file_name_is_absolute($header{$key})
l!l&&r!l&&!r
1131013
MC/DC100%2/2
not exists $header{$key} or "File::Spec"->file_name_is_absolute($header{$key})
not exists $header{$key} "File::Spec"->file_name_is_absolute($header{$key})
Truth table100%3/3
not(A) or B
A= not exists $header{$key}B= "File::Spec"->file_name_is_absolute($header{$key})
ABresult
1X1
011
000
150 13 $header{$key} = File::Spec->rel2abs($header{$key}, $directory);
151 }
152 68 validate_header_connection(\%header);
153  
154 60 my $remainder = substr($query, $offset);
155 60 reject_misplaced_sentinel($remainder, 1);
156 59 return ($remainder, \%header);
157 }
158  
159 20 ▶sub strip_query_header_lax {
Subroutine strip_query_header_lax: called CC=2 SCAR=6.9
Pod undocumented
160 20 my ($query) = @_;
161 20 ▶ return $query if $query !~ /\A\/\/ pplquery[ \t]*\r?\n.*?^[ \t]*\r?\n/ms;
Branch: 1 branch
BranchTrueFalseTotal
unless $query =~ m[\A// pplquery[ \t]*\r?\n.*?^[ \t]*\r?\n]msu 16 4 20
162 4 return substr($query, $+[0]);
163 }
164  
165 # One validation of the header as a whole. A header that omits url is checked
166 # against a placeholder https URL, so its other fields are still validated
167 # individually; the password fields are not client options and take no part.
168 68 ▶sub validate_header_connection {
Subroutine validate_header_connection: called CC=6 SCAR=17.9
Pod undocumented
169 68 my ($header) = @_;
170 OpenSearch::PPLQuery::validate_options({
171 url => $header->{url} // 'https://header-validation.invalid', timeout => 1,
172 (exists($header->{user}) ? (user => $header->{user}, password => 'validation') : ()),
173 (exists($header->{'ca-file'}) ? (ca_file => $header->{'ca-file'}) : ()),
174 68 ▶ (exists($header->{'tls-verify'}) && $header->{'tls-verify'} eq 'false' ? (insecure => 1) : ()),
Branch: 3 branches
BranchTrueFalseTotal
exists $header->{'user'} ? : 26 42 68
exists $header->{'ca-file'} ? : 13 55 68
exists $header->{'tls-verify'} && $header->{'tls-verify'} eq 'false' ? : 7 61 68
Condition100%5/5
$header->{"url"} // "https://header-validation.invalid"
l!l
5315
exists $header->{"tls-verify"} and $header->{"tls-verify"} eq "false"
!ll&&!rl&&r
5387
MC/DC100%1/1
$header->{"url"} // "https://header-validation.invalid"
$header->{"url"}
MC/DC100%2/2
exists $header->{"tls-verify"} and $header->{"tls-verify"} eq "false"
exists $header->{"tls-verify"} $header->{"tls-verify"} eq "false"
Truth table100%2/2
A
A= $header->{"url"}
Aresult
11
00
Truth table100%3/3
A and B
A= exists $header->{"tls-verify"}B= $header->{"tls-verify"} eq "false"
ABresult
0X0
100
111
175 });
176 }
177  
178 82 ▶sub reject_misplaced_sentinel {
Subroutine reject_misplaced_sentinel: called CC=4 SCAR=13.9
Pod undocumented
179 82 my ($query, $after_header) = @_;
180 82 ▶ die "The pplquery sentinel cannot have leading whitespace and must appear on line 1\n"
Branch: 1 branch
BranchTrueFalseTotal
if $query =~ m[(?:\A|\n)[ \t]+// pplquery[ \t]*(?=\r?(?:\n|\z))]u 1 81 82
181 . "Move '// pplquery' to the very start of the input, with nothing before it.\n"
182 if $query =~ /(?:\A|\n)[ \t]+\/\/ pplquery[ \t]*(?=\r?(?:\n|\z))/;
183 81 ▶ if ($query =~ /(?:\A|\n)\/\/ pplquery[ \t]*(?=\r?(?:\n|\z))/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query =~ m[(?:\A|\n)// pplquery[ \t]*(?=\r?(?:\n|\z))]u) 3 78 81
184 3 ▶ die $after_header
Branch: 1 branch
BranchTrueFalseTotal
$after_header ? : 1 2 3
185 ? "A repeated pplquery sentinel is not allowed later in the query\n"
186 . "One connection header is permitted, at the top of the input.\n"
187 : "The pplquery sentinel must appear on line 1\n"
188 . "A '// pplquery' line anywhere else is refused so that connection metadata cannot hide further down a query.\n";
189 }
190 }
191  
192 120 ▶sub direct_connection {
Subroutine direct_connection: called CC=42 SCAR=37.4
Pod undocumented
193 120 my ($cli, $header, $connection_file_selected) = @_;
194 120 ▶ $header //= {};
Condition100%2/2
$header //= {}
l!l
5664
MC/DC100%1/1
$header //= {}
$header
Truth table100%2/2
A
A= $header
Aresult
11
00
195 120 ▶ my $header_url_selected = !$connection_file_selected && !defined($cli->{url}) && exists($header->{url});
Condition100%6/6
!($connection_file_selected || defined $cli->{'url'}) && exists $header->{'url'}
!ll&&!rl&&r
415920
$connection_file_selected || defined $cli->{'url'}
l!l&&r!l&&!r
192279
MC/DC100%3/3
!($connection_file_selected || defined $cli->{'url'}) && exists $header->{'url'}
$connection_file_selected defined $cli->{'url'} exists $header->{'url'}
Truth table100%4/4
not(A || B) && C
A= $connection_file_selectedB= defined $cli->{'url'}C= exists $header->{'url'}
ABCresult
1XX0
01X0
0000
0011
196 my $url = defined($cli->{url}) ? $cli->{url}
197 : exists($header->{url}) ? $header->{url}
198 120 ▶ : environment_nonempty('PPLQUERY_URL');
Branch: 2 branches
BranchTrueFalseTotal
defined $cli->{'url'} ? : 23 97 120
exists $header->{'url'} ? : 36 61 97
199 my $user = defined($cli->{user}) ? $cli->{user}
200 : exists($header->{user}) ? $header->{user}
201 119 ▶ : !$header_url_selected ? environment_nonempty('PPLQUERY_USER')
Branch: 3 branches
BranchTrueFalseTotal
defined $cli->{'user'} ? : 4 115 119
exists $header->{'user'} ? : 21 94 115
!$header_url_selected ? : 88 6 94
202 : undef;
203 # A query header decides transport trust only for a URL it selected
204 # itself. A connection file is exempt: the operator chose it explicitly.
205 # A header field the command line overrides is never applied, so there is
206 # nothing to refuse. Checked before any password source is resolved, so a
207 # header that fails here never causes a secret to be read.
208 118 ▶ my $header_owns_tls = $connection_file_selected || $header_url_selected;
Condition100%3/3
$connection_file_selected || $header_url_selected
l!l&&r!l&&!r
192079
MC/DC100%2/2
$connection_file_selected || $header_url_selected
$connection_file_selected $header_url_selected
Truth table100%3/3
A || B
A= $connection_file_selectedB= $header_url_selected
ABresult
1X1
011
000
209 118 my $trust_help = "Give the header its own 'url' field so it controls the endpoint it is changing trust for, move the setting to the command line, or select the connection with --connection-file.\n";
210 die "A query header may not choose the CA for a URL it does not select.\n$trust_help"
211 118 ▶ if !$header_owns_tls && !defined($cli->{ca_file}) && exists $header->{'ca-file'};
Branch: 1 branch
BranchTrueFalseTotal
if not $header_owns_tls || defined $cli->{"ca_file"} and exists $header->{"ca-file"} 1 117 118
Condition100%6/6
not $header_owns_tls || defined $cli->{"ca_file"} and exists $header->{"ca-file"}
!ll&&!rl&&r
42751
$header_owns_tls || defined $cli->{'ca_file'}
l!l&&r!l&&!r
39376
MC/DC100%3/3
not $header_owns_tls || defined $cli->{"ca_file"} and exists $header->{"ca-file"}
$header_owns_tls defined $cli->{'ca_file'} exists $header->{"ca-file"}
Truth table100%4/4
not(A || B) and C
A= $header_owns_tlsB= defined $cli->{'ca_file'}C= exists $header->{"ca-file"}
ABCresult
1XX0
01X0
0000
0011
212 die "A query header may not disable TLS verification for a URL it does not select.\n$trust_help"
213 if !$header_owns_tls && !defined($cli->{insecure})
214 117 ▶ && exists($header->{'tls-verify'}) && $header->{'tls-verify'} eq 'false';
Branch: 1 branch
BranchTrueFalseTotal
if not $header_owns_tls || defined $cli->{"insecure"} and exists $header->{"tls-verify"} and $header->{"tls-verify"} eq "false" 2 115 117
Condition100%9/9
not $header_owns_tls || defined $cli->{"insecure"} and exists $header->{"tls-verify"} and $header->{"tls-verify"} eq "false"
!ll&&!rl&&r
11322
not $header_owns_tls || defined $cli->{"insecure"} and exists $header->{"tls-verify"}
!ll&&!rl&&r
41724
$header_owns_tls || defined $cli->{'insecure'}
l!l&&r!l&&!r
39276
MC/DC100%4/4
not $header_owns_tls || defined $cli->{"insecure"} and exists $header->{"tls-verify"} and $header->{"tls-verify"} eq "false"
$header_owns_tls defined $cli->{'insecure'} exists $header->{"tls-verify"} $header->{"tls-verify"} eq "false"
Truth table100%5/5
not(A || B) and C and D
A= $header_owns_tlsB= defined $cli->{'insecure'}C= exists $header->{"tls-verify"}D= $header->{"tls-verify"} eq "false"
ABCDresult
1XXX0
01XX0
000X0
00100
00111
215  
216 115 my ($password, $password_file);
217 115 ▶ if (defined $cli->{password_file}) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $cli->{'password_file'}) { } 4 111 115
elsif (exists $header->{'password-environment'}) { } 13 98 111
elsif (exists $header->{'password-file'}) { } 10 88 98
elsif (not $header_url_selected) { } 81 7 88
218 4 $password_file = $cli->{password_file};
219 } elsif (exists $header->{'password-environment'}) {
220 13 my $name = $header->{'password-environment'};
221 die "$name must be set for header password-environment, but it is not present in the environment.\n"
222 . "The header names it as the password source, so no other source is tried. Export $name, or point the header at a variable that is set.\n"
223 13 ▶ if !exists $ENV{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ENV{$name} 2 11 13
224 11 $password = environment_nonempty($name);
225 } elsif (exists $header->{'password-file'}) {
226 10 $password_file = $header->{'password-file'};
227 } elsif (!$header_url_selected) {
228 81 $password = environment_nonempty('PPLQUERY_PASSWORD');
229 80 $password_file = environment_nonempty('PPLQUERY_PASSWORD_FILE');
230 79 ▶ die "PPLQUERY_PASSWORD and PPLQUERY_PASSWORD_FILE cannot both be set; use one password source\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $password and defined $password_file 1 78 79
Condition100%3/3
defined $password and defined $password_file
!ll&&!rl&&r
7171
MC/DC100%2/2
defined $password and defined $password_file
defined $password defined $password_file
Truth table100%3/3
A and B
A= defined $passwordB= defined $password_file
ABresult
0X0
100
111
231 if defined($password) && defined($password_file);
232 }
233 109 ▶ if (defined($user) != (defined($password) || defined($password_file))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $user != (defined $password || defined $password_file)) 11 98 109
Condition100%3/3
defined $password || defined $password_file
l!l&&r!l&&!r
171874
MC/DC100%2/2
defined $password || defined $password_file
defined $password defined $password_file
Truth table100%3/3
A || B
A= defined $passwordB= defined $password_file
ABresult
1X1
011
000
234 11 ▶ my $isolation = $header_url_selected
Branch: 1 branch
BranchTrueFalseTotal
$header_url_selected ? : 3 8 11
235 ? "The query header selected the URL, so PPLQUERY_USER, PPLQUERY_PASSWORD, and PPLQUERY_PASSWORD_FILE are deliberately not used for it. Supply both halves in the header, or take control of the URL with --url.\n"
236 : '';
237 11 ▶ die defined($user)
Branch: 1 branch
BranchTrueFalseTotal
defined $user ? : 5 6 11
238 ? "Basic-auth username and password must be set together, but only a username was supplied.\n${isolation}Add a password with --password-file, a 'password-environment' or 'password-file' header field, PPLQUERY_PASSWORD, or PPLQUERY_PASSWORD_FILE.\n"
239 : "Basic-auth username and password must be set together, but only a password was supplied.\n${isolation}Add a username with --user, a 'user' header field, or PPLQUERY_USER.\n";
240 }
241 98 ▶ $password = read_password_file($password_file) if defined $password_file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $password_file 15 83 98
242  
243 my $ca_file = defined($cli->{ca_file}) ? $cli->{ca_file}
244 92 ▶ : exists($header->{'ca-file'}) ? $header->{'ca-file'}
Branch: 2 branches
BranchTrueFalseTotal
defined $cli->{'ca_file'} ? : 4 88 92
exists $header->{'ca-file'} ? : 6 82 88
245 : environment_nonempty('PPLQUERY_CA_FILE');
246 91 my $insecure = $cli->{insecure};
247 91 ▶ if (!defined $insecure) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $insecure) 88 3 91
248 my $tls_verify = exists($header->{'tls-verify'})
249 88 ▶ ? $header->{'tls-verify'}
Branch: 1 branch
BranchTrueFalseTotal
exists $header->{'tls-verify'} ? : 10 78 88
250 : environment_nonempty('PPLQUERY_TLS_VERIFY');
251 87 ▶ if (defined $tls_verify) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tls_verify) 21 66 87
252 21 ▶ die "PPLQUERY_TLS_VERIFY must be exactly 'true' or 'false', not '"
Branch: 1 branch
BranchTrueFalseTotal
if $tls_verify ne "true" and $tls_verify ne "false" 8 13 21
Condition100%3/3
$tls_verify ne "true" and $tls_verify ne "false"
!ll&&!rl&&r
948
MC/DC100%2/2
$tls_verify ne "true" and $tls_verify ne "false"
$tls_verify ne "true" $tls_verify ne "false"
Truth table100%3/3
A and B
A= $tls_verify ne "true"B= $tls_verify ne "false"
ABresult
0X0
100
111
253 . OpenSearch::PPLQuery::terminal_text($tls_verify) . "'\n"
254 if $tls_verify ne 'true' && $tls_verify ne 'false';
255 13 ▶ $insecure = $tls_verify eq 'false' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$tls_verify eq 'false' ? : 4 9 13
256 }
257 }
258  
259 82 my $timeout = $cli->{timeout};
260 82 ▶ if (!defined $timeout) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $timeout) 78 4 82
261 78 ▶ if (exists $header->{timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $header->{'timeout'}) { } 7 71 78
262 7 $timeout = $header->{timeout};
263 } else {
264 71 $timeout = environment_nonempty('PPLQUERY_TIMEOUT');
265 70 ▶ $timeout = OpenSearch::PPLQuery::validate_timeout($timeout, 'PPLQUERY_TIMEOUT') if defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
if defined $timeout 13 57 70
266 }
267 }
268  
269 72 ▶ my %connection = (
Condition100%4/4
$url // "http://127.0.0.1:9200"
l!l
684
$timeout // 60
l!l
1557
MC/DC100%1/1
$url // "http://127.0.0.1:9200"
$url
MC/DC100%1/1
$timeout // 60
$timeout
Truth table100%2/2
A
A= $url
Aresult
11
00
Truth table100%2/2
A
A= $timeout
Aresult
11
00
270 url => $url // 'http://127.0.0.1:9200', timeout => $timeout // 60,
271 user => $user, password => $password, ca_file => $ca_file, insecure => $insecure,
272 );
273 72 delete @connection{grep { !defined $connection{$_} } keys %connection};
274 72 return \%connection;
275 }
276  
277 15 ▶sub read_password_file {
Subroutine read_password_file: called CC=3 SCAR=11.0
Pod undocumented
278 15 my ($path) = @_;
279 15 my $password = read_utf8_file($path, "password file $path");
280 # Trailing line endings are a property of how the file was written, never
281 # of the password. Trailing spaces may be part of it, so they stay.
282 11 $password =~ s/[\r\n]+\z//;
283 11 ▶ die "Password file $path must not be empty\n" if $password eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $password eq "" 1 10 11
284 10 ▶ die "Password file $path must contain a single line, but it holds a line break or control character.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $password =~ /[\x00-\x1f\x7f]/u 1 9 10
285 . "Trailing line endings are removed automatically; a file with more than one line is a mistake rather than a multi-line password.\n"
286 if $password =~ /[\x00-\x1f\x7f]/;
287 9 return $password;
288 }
289  
290 552 ▶sub environment_text {
Subroutine environment_text: called CC=2 SCAR=6.9
Pod undocumented
291 552 my ($name) = @_;
292 552 ▶ return undef if !exists $ENV{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ENV{$name} 427 125 552
293 125 return OpenSearch::PPLQuery::decode_utf8($ENV{$name}, "environment variable $name");
294 }
295  
296 552 ▶sub environment_nonempty {
Subroutine environment_nonempty: called CC=3 SCAR=11.0
Pod undocumented
297 552 my ($name) = @_;
298 552 my $value = environment_text($name);
299 552 ▶ die "$name must not be empty. Unset it entirely to fall back to the next connection source, or give it a value.\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and $value eq "" 8 544 552
Condition100%3/3
defined $value and $value eq ""
!ll&&!rl&&r
4271178
MC/DC100%2/2
defined $value and $value eq ""
defined $value $value eq ""
Truth table100%3/3
A and B
A= defined $valueB= $value eq ""
ABresult
0X0
100
111
300 if defined($value) && $value eq '';
301 544 return $value;
302 }
303  
304 27 1;
305  
306 __END__
↑ Summary lib/OpenSearch/PPLQuery.pm »