Connector/Role/SSLUserAgent.pm

filter: Statementstmt 54.1% 39 / 72 Branchbran 23.8% 10 / 42 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 80.0% 4 / 5 Podpod n/a totaltotal 42.7% 53 / 124 CC 23 SCAR 52.2
CC
23
Cov
42%
_init_agent
217.0
__ANON__
2.0
BEGIN
1.0
CRAP 185.0
« Connector/Proxy/SOAP/Lite.pm ↑ Summary Connector/Proxy/Net/LDAP/Single.pm »
1 package Connector::Role::SSLUserAgent;
2 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 has timeout => (
7 is => 'rw',
8 isa => 'Int',
9 required => 0,
10 default => 10,
11 );
12  
13 1 has proxy => (
14 is => 'rw',
15 isa => 'Str',
16 required => 0,
17 );
18  
19 1 has agent => (
20 is => 'rw',
21 isa => 'Object',
22 lazy => 1,
23 builder => '_init_agent',
24 );
25
26 1 has use_net_ssl => (
27 is => 'rw',
28 isa => 'Bool',
29 default => 0,
30 );
31  
32 1 has ssl_ignore_hostname => (
33 is => 'rw',
34 isa => 'Bool',
35 default => 0,
36 );
37  
38 1 has ssl_ignore_mode => (
39 is => 'rw',
40 isa => 'Bool',
41 default => 0,
42 );
43  
44 1 has certificate_file => (
45 is => 'rw',
46 isa => 'Str',
47 );
48  
49 1 has certificate_key_file => (
50 is => 'rw',
51 isa => 'Str',
52 );
53  
54 1 has certificate_p12_file => (
55 is => 'rw',
56 isa => 'Str',
57 );
58  
59 1 has certificate_key_password => (
60 is => 'rw',
61 isa => 'Str',
62 );
63  
64 1 has ca_certificate_path => (
65 is => 'rw',
66 isa => 'Str',
67 );
68  
69 1 has ca_certificate_file => (
70 is => 'rw',
71 isa => 'Str',
72 );
73  
74 4 ▶sub _init_agent {
Subroutine _init_agent: called CC=23 SCAR=53.8
75  
76 4 my $self = shift;
77  
78 4 my %ENV_BACKUP = %ENV;
79  
80 4 my $ua = LWP::UserAgent->new;
81 4 $ua->timeout( $self->timeout() );
82 4 ▶ if ($self->proxy()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->proxy) 0 4 4
83 0 $ua->proxy(['http', 'https'], $self->proxy());
84 }
85  
86 # Force usage of net::ssl
87 4 ▶ if ($self->use_net_ssl()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->use_net_ssl) { } 0 4 4
elsif ($self->LOCATION =~ /^https:/i) { } 4 0 4
88 0 require Net::SSL;
89 0 $ENV{PERL_NET_HTTPS_SSL_SOCKET_CLASS} = "Net::SSL";
90  
91 0 ▶ if ($self->certificate_p12_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->certificate_p12_file) 0 0 0
92 0 $ENV{HTTPS_PKCS12_FILE} = $self->certificate_p12_file;
93  
94 0 ▶ if ($self->certificate_key_file || $self->certificate_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->certificate_key_file or $self->certificate_file) 0 0 0
Condition0%0/3
$self->certificate_key_file or $self->certificate_file
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->certificate_key_file or $self->certificate_file
$self->certificate_key_file $self->certificate_file
Truth table0%0/3
A or B
A= $self->certificate_key_fileB= $self->certificate_file
ABresult
1X1
011
000
95 0 die "Options certificate_file/certificate_key_file and certificate_p12_file are mutually exclusive";
96 }
97  
98 0 ▶ if ($self->certificate_key_password) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->certificate_key_password) 0 0 0
99 0 $ENV{HTTPS_PKCS12_PASSWORD} = $self->certificate_key_password;
100 }
101 }
102  
103 0 ▶ if ($self->certificate_key_file) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->certificate_key_file) { } 0 0 0
elsif ($self->certificate_file) { } 0 0 0
104 0 ▶ if ($self->certificate_key_password) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->certificate_key_password) 0 0 0
105 0 die "Net::SSL does not support password protected keys - use certificate_p12_file instead";
106 }
107  
108 0 ▶ if (!$self->certificate_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->certificate_file) 0 0 0
109 0 die "You need to pass certificate AND key file, use certificate_p12_file to pass a PKCS12";
110 }
111  
112 0 $ENV{HTTPS_KEY_FILE} = $self->certificate_key_file;
113 0 $ENV{HTTPS_CERT_FILE} = $self->certificate_file;
114  
115 } elsif ($self->certificate_file) {
116 0 die "You need to pass certificate AND key file, use certificate_p12_file to pass a PKCS12";
117 }
118  
119 0 ▶ if ($self->ca_certificate_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ca_certificate_path) 0 0 0
120 0 $ENV{HTTPS_CA_DIR} = $self->ca_certificate_path;
121 }
122  
123 0 ▶ if ($self->ca_certificate_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ca_certificate_file) 0 0 0
124 0 $ENV{HTTPS_CA_FILE} = $self->ca_certificate_file;
125 }
126  
127 0 $self->log()->trace('Using Net::SSL, EVN is' . Dumper $ENV);
128  
129 } # end of Net:SSL, IO::Socket::SSL
130 elsif( $self->LOCATION() =~ /^https:/i ) {
131  
132 4 require IO::Socket::SSL;
133 4 $ENV{PERL_NET_HTTPS_SSL_SOCKET_CLASS} = "IO::Socket::SSL";
134  
135 4 ▶ my $ssl_opts = {
Branch: 2 branches
BranchTrueFalseTotal
$self->ssl_ignore_hostname ? : 0 4 4
$self->ssl_ignore_mode ? : 0 4 4
136 verify_hostname => ($self->ssl_ignore_hostname ? 0 : 1),
137 SSL_verify_mode => ($self->ssl_ignore_mode ? 0 : 1)
138 };
139 4 ▶ if ($self->certificate_p12_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->certificate_p12_file) 0 4 4
140 0 die "Using pkcs12 containers is not supported by IO::Socket::SSL"
141 }
142  
143 4 ▶ if ($self->certificate_key_file) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->certificate_key_file) { } 0 4 4
elsif ($self->certificate_file) { } 0 4 4
144 0 ▶ if (!$self->certificate_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->certificate_file) 0 0 0
145 0 die "You need to pass certificate AND key file";
146 }
147 0 $ssl_opts->{SSL_key_file} = $self->certificate_key_file;
148 0 $ssl_opts->{SSL_cert_file} = $self->certificate_file;
149  
150 0 ▶ if ( $self->certificate_key_password ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->certificate_key_password) 0 0 0
151 0 ▶ $ssl_opts->{SSL_passwd_cb} = sub { return $self->certificate_key_password; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
152 }
153  
154 } elsif ($self->certificate_file) {
155 0 die "You need to pass certificate AND key file";
156 }
157  
158 4 ▶ if ($self->ca_certificate_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ca_certificate_path) 0 4 4
159 0 $ssl_opts->{SSL_ca_path} = $self->ca_certificate_path;
160 }
161  
162 4 ▶ if ($self->ca_certificate_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ca_certificate_file) 0 4 4
163 0 $ssl_opts->{SSL_ca_file} = $self->ca_certificate_file;
164 }
165  
166 4 $ua->ssl_opts( %{$ssl_opts} );
167  
168 4 $self->log()->trace('Using IO::Socket::SSL with options ' . Dumper $ssl_opts);
169 } else {
170 # No ssl
171 }
172  
173 4 return $ua;
174 }
175  
176 1 1;
177  
178 __END__;
« Connector/Proxy/SOAP/Lite.pm ↑ Summary Connector/Proxy/Net/LDAP/Single.pm »