blib/lib/oEdtk/Net.pm

filter: Statementstmt 64.2% 27 / 42 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 72.7% 8 / 11 Podpod 100% 2 / 2 totaltotal 46.2% 37 / 80 CC 11 SCAR 27.2
CC
11
Cov
44%
ua_ssl_opts
30.0
_verify_enabled
20.0
make_ua
20.0
CRAP 15.1
« blib/lib/oEdtk/XML/Default.pm ↑ Summary blib/lib/oEdtk/IBAN.pm »
1 package oEdtk::Net;
2  
3 # Shared HTTP transport settings for the clients that talk to corporate HTTP
4 # services: the ECM injector here, and the oEUser-side Keycloak / Emailing /
5 # DisseminationPlan clients. The fork hard-coded the transport security of every
6 # one of them (host-name verification off, a corporate CA pinned); here that is
7 # centralised and made safe by default.
8 #
9 # Keys read from the configuration hash:
10 # EDTK_SSL_VERIFY 0 (or no/false/off) disables peer and host-name
11 # verification; anything else, and the default when unset,
12 # keeps them ON.
13 # EDTK_SSL_CA_PATH CA bundle: a file (SSL_ca_file) or a directory
14 # (SSL_ca_path). Empty or unset: the system trust store.
15  
16 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 our @EXPORT_OK = qw( ua_ssl_opts make_ua );
24  
25 2 ▶use IO::Socket::SSL qw( SSL_VERIFY_PEER SSL_VERIFY_NONE );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 2 our $VERSION = 2.1131;
29  
30 2 ▶use constant DEFAULT_VERIFY => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 # A missing or empty value keeps the default; only an explicit false-looking
33 # value disables verification.
34 0 ▶sub _verify_enabled ($value) {
Subroutine _verify_enabled: not called CC=4 SCAR=30.0
35 0 ▶ return DEFAULT_VERIFY unless defined $value && length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 0 0 0
Condition0%0/3
defined $value and length $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value and length $value
defined $value length $value
Truth table0%0/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
36 0 ▶ return 0 if $value =~ /^(?:0|no|false|off)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^(?:0|no|false|off)$/i 0 0 0
37 0 return 1;
38 }
39  
40 # LWP / IO::Socket::SSL options derived from the configuration. Verification is
41 # on by default; the CA location is passed through when configured.
42 0 ▶sub ua_ssl_opts ($cfg) {
Subroutine ua_ssl_opts: not called CC=5 SCAR=34.0
Pod documented
43 0 my $verify = _verify_enabled( $cfg->{'EDTK_SSL_VERIFY'} );
44 0 ▶ my %ssl = (
Branch: 1 branch
BranchTrueFalseTotal
$verify ? : 0 0 0
45 verify_hostname => $verify,
46 SSL_verify_mode => $verify ? SSL_VERIFY_PEER : SSL_VERIFY_NONE,
47 );
48 0 my $ca = $cfg->{'EDTK_SSL_CA_PATH'};
49 0 ▶ if ( defined $ca && length $ca ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ca and length $ca) 0 0 0
Condition0%0/3
defined $ca and length $ca
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ca and length $ca
defined $ca length $ca
Truth table0%0/3
A and B
A= defined $caB= length $ca
ABresult
0X0
100
111
50 0 ▶ if ( -d $ca ) { $ssl{SSL_ca_path} = $ca }
Branch: 1 branch
BranchTrueFalseTotal
if (-d $ca) { } 0 0 0
51 0 else { $ssl{SSL_ca_file} = $ca }
52 }
53 0 return \%ssl;
54 }
55  
56 # A UserAgent using the options above, with an optional timeout (seconds).
57 0 ▶sub make_ua ( $cfg, $timeout = undef ) {
Subroutine make_ua: not called CC=4 SCAR=30.0
Pod documented
58 0 ▶ my $ua = LWP::UserAgent->new( ssl_opts => ua_ssl_opts( $cfg // {} ) );
Condition0%0/2
$cfg // {}
l!l
00
MC/DC0%0/1
$cfg // {}
$cfg
Truth table0%0/2
A
A= $cfg
Aresult
11
00
59 0 ▶ $ua->timeout($timeout) if defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
if defined $timeout 0 0 0
60 0 return $ua;
61 }
62  
63 2 1;
64  
65 =head1 NAME
66  
67 oEdtk::Net - shared HTTP transport settings for the service clients
68  
69 =head1 SYNOPSIS
70  
71 use oEdtk::Net qw(make_ua);
72  
73 my $ua = make_ua( $cfg ); # TLS verification on, system trust store
74 my $ua = make_ua( $cfg, 500 ); # ... with a 500 s timeout
75  
76 =head1 DESCRIPTION
77  
78 The service clients share one transport policy. The fork disabled host-name
79 verification and pinned a corporate CA directory in every client; here that is
80 centralised and made safe by default: peer and host-name verification are ON
81 unless C<EDTK_SSL_VERIFY> asks otherwise, and the CA bundle is taken from
82 C<EDTK_SSL_CA_PATH> when set (a file becomes C<SSL_ca_file>, a directory becomes
83 C<SSL_ca_path>). With an empty C<EDTK_SSL_CA_PATH> the system trust store is used.
84  
85 The oEUser-side clients reach this module through their local name
86 L<oEUser::Net>, a thin delegate.
87  
88 =head1 FUNCTIONS
89  
90 =over 4
91  
92 =item ua_ssl_opts( $cfg )
93  
94 Return the C<ssl_opts> hashref for LWP::UserAgent derived from the
95 configuration.
96  
97 =item make_ua( $cfg, $timeout )
98  
99 Return an LWP::UserAgent using those options. C<$timeout> is optional.
100  
101 =back
102  
103 =head1 AUTHOR
104  
105 The oEdtk team.
106  
107 =cut
« blib/lib/oEdtk/XML/Default.pm ↑ Summary blib/lib/oEdtk/IBAN.pm »