Connector/Proxy/SOAP/Lite.pm untested

filter: Statementstmt 0.0% 0 / 188 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 324 CC 37 SCAR 55.4
CC
37
Cov
0%
_soap_call
600.0
get
42.0
get_hash
20.0
CRAP 255.8
1 # Connector::Proxy::SOAP::Lite
2 #
3 # Proxy class for accessing SOAP servers
4 #
5 # Written by Martin Bartosch for the OpenXPKI project 2012
6 #
7 package Connector::Proxy::SOAP::Lite;
8  
9 use strict;
10 use warnings;
11 use English;
12 use SOAP::Lite;
13 use Data::Dumper;
14  
15 use Moose;
16 extends 'Connector::Proxy';
17  
18 has uri => (
19 is => 'rw',
20 isa => 'Str',
21 required => 1,
22 );
23  
24 has namespace => (
25 is => 'rw',
26 isa => 'Str',
27 required => 0,
28 default => '',
29 );
30  
31 has method => (
32 is => 'rw',
33 isa => 'Str',
34 required => 1,
35 );
36  
37 has do_not_use_charset => (
38 is => 'ro',
39 isa => 'Bool',
40 default => 0,
41 );
42  
43 has use_microsoft_dot_net_compatible_separator => (
44 is => 'rw',
45 isa => 'Bool',
46 default => 0,
47 );
48  
49 # By default the SOAP call uses positional parameters. If this flag is set,
50 # the argument list to the call is interpreted as a Hash
51 has named_parameters => (
52 is => 'rw',
53 isa => 'ArrayRef|Str|Undef',
54 trigger => \&_convert_parameters,
55 );
56  
57 has attrmap => (
58 is => 'rw',
59 isa => 'HashRef',
60 required => 0,
61 predicate => 'has_attrmap'
62 );
63  
64 has use_net_ssl => (
65 is => 'rw',
66 isa => 'Bool',
67 default => 0,
68 );
69  
70 has ssl_ignore_hostname => (
71 is => 'rw',
72 isa => 'Bool',
73 default => 0,
74 );
75  
76 has certificate_file => (
77 is => 'rw',
78 isa => 'Str',
79 );
80  
81 has certificate_key_file => (
82 is => 'rw',
83 isa => 'Str',
84 );
85  
86 has certificate_p12_file => (
87 is => 'rw',
88 isa => 'Str',
89 );
90  
91 has certificate_key_password => (
92 is => 'rw',
93 isa => 'Str',
94 );
95  
96 has ca_certificate_path => (
97 is => 'rw',
98 isa => 'Str',
99 );
100  
101 has ca_certificate_file => (
102 is => 'rw',
103 isa => 'Str',
104 );
105  
106 sub BUILD {
107 my $self = shift;
108 if ($self->do_not_use_charset) {
109 $SOAP::Constants::DO_NOT_USE_CHARSET = 1;
110 }
111 }
112  
113 # If named_parameters is set using a string (necessary atm for Config::Std)
114 # its converted to an arrayref. Might be removed if Config::* improves
115 # This might create indefinite loops if something goes wrong on the conversion!
116 sub _convert_parameters {
117 my ( $self, $new, $old ) = @_;
118  
119 # Test if the given value is a non empty scalar
120 if ($new && !ref $new && (!$old || $new ne $old)) {
121 my @attrs = split(" ", $new);
122 $self->named_parameters( \@attrs )
123 }
124  
125 }
126  
127 sub _build_config {
128 my $self = shift;
129 }
130  
131 # override this to add authentication headers
132 # must return an array (not array ref!) with SOAP::Head objects
133 sub _make_head {
134 return;
135 }
136  
137 # can be overriden in case you need a more complex data structure
138 sub _make_params {
139 my $self = shift;
140 my $args = shift;
141  
142 my @params;
143 if (ref $self->named_parameters eq 'ARRAY' && scalar @{$self->named_parameters}) {
144 my @args = @{$args};
145 foreach my $key (@{$self->named_parameters}) {
146 my $value = shift @args;
147 push @params, SOAP::Data->new(name => $key, value => $value );
148 $self->log()->debug('Named parameter: ' . $key . ' => ' . $value );
149 }
150 } else {
151 @params = @{$args};
152 $self->log()->debug('Parameters: ' . join(', ', @params));
153 }
154 return @params;
155 }
156  
157 sub _soap_call {
158 my $self = shift;
159  
160 my @args = $self->_build_path( shift );
161  
162 my $proxy = $self->LOCATION();
163  
164 my %ENV_BACKUP = %ENV;
165  
166 my $client = SOAP::Lite
167 ->uri($self->uri);
168  
169 $self->log()->debug('Performing SOAP call to method ' . $self->method . ' on service ' . $self->uri . ' via ' . $proxy);
170  
171 # Force usage of net::ssl
172 if ($self->use_net_ssl()) {
173 require Net::SSL;
174 $ENV{PERL_NET_HTTPS_SSL_SOCKET_CLASS} = "Net::SSL";
175  
176 if ($self->certificate_p12_file) {
177 $ENV{HTTPS_PKCS12_FILE} = $self->certificate_p12_file;
178  
179 if ($self->certificate_key_file || $self->certificate_file) {
180 die "Options certificate_file/certificate_key_file and certificate_p12_file are mutually exclusive";
181 }
182  
183 if ($self->certificate_key_password) {
184 $ENV{HTTPS_PKCS12_PASSWORD} = $self->certificate_key_password;
185 }
186 }
187  
188 if ($self->certificate_key_file) {
189 if ($self->certificate_key_password) {
190 die "Net::SSL does not support password protected keys - use certificate_p12_file instead";
191 }
192  
193 if (!$self->certificate_file) {
194 die "You need to pass certificate AND key file, use certificate_p12_file to pass a PKCS12";
195 }
196  
197 $ENV{HTTPS_KEY_FILE} = $self->certificate_key_file;
198 $ENV{HTTPS_CERT_FILE} = $self->certificate_file;
199  
200 } elsif ($self->certificate_file) {
201 die "You need to pass certificate AND key file, use certificate_p12_file to pass a PKCS12";
202 }
203  
204 if ($self->ca_certificate_path) {
205 $ENV{HTTPS_CA_DIR} = $self->ca_certificate_path;
206 }
207  
208 if ($self->ca_certificate_file) {
209 $ENV{HTTPS_CA_FILE} = $self->ca_certificate_file;
210 }
211  
212 $client->proxy($proxy);
213  
214 $self->log()->trace('Using Net::SSL, EVN is' . Dumper $ENV);
215  
216 } # end of Net:SSL, IO::Socket::SSL
217 elsif( $proxy =~ /^https:/i ) {
218  
219 use IO::Socket::SSL;
220 $ENV{PERL_NET_HTTPS_SSL_SOCKET_CLASS} = "IO::Socket::SSL";
221  
222 my $ssl_opts = {
223 verify_hostname => ($self->ssl_ignore_hostname ? 0 : 1)
224 };
225  
226 if ($self->certificate_p12_file) {
227 die "Using pkcs12 containers is not supported by IO::Socket::SSL"
228 }
229  
230 if ($self->certificate_key_file) {
231 if (!$self->certificate_file) {
232 die "You need to pass certificate AND key file";
233 }
234 $ssl_opts->{SSL_key_file} = $self->certificate_key_file;
235 $ssl_opts->{SSL_cert_file} = $self->certificate_file;
236  
237 if ( $self->certificate_key_password ) {
238 $ssl_opts->{SSL_passwd_cb} = sub { return $self->certificate_key_password; };
239 }
240  
241 } elsif ($self->certificate_file) {
242 die "You need to pass certificate AND key file";
243 }
244  
245 if ($self->ca_certificate_path) {
246 $ssl_opts->{SSL_ca_path} = $self->ca_certificate_path;
247 }
248  
249 if ($self->ca_certificate_file) {
250 $ssl_opts->{SSL_ca_file} = $self->ca_certificate_file;
251 }
252 # For whatever reason LWP wants the ssl_opts as ARRAYref!
253 $client-> proxy($proxy, ssl_opts => [%{$ssl_opts}] );
254  
255 $self->log()->trace('Using IO::Socket::SSL with options ' . Dumper $ssl_opts);
256 } else {
257 # No ssl
258 $client->proxy($proxy);
259 }
260  
261 if ($self->use_microsoft_dot_net_compatible_separator) {
262 # This modifies the seperator used in the SOAPAction Header to add
263 # the method to the uri. Default is # but .Net expects a slash.
264 $client->on_action( sub { join('/', @_) } );
265 }
266  
267 if (my $ns = $self->namespace()) {
268 $client->ns($ns);
269 }
270  
271 my @params = $self->_make_head();
272 push @params, $self->_make_params( \@args );
273  
274 my $som;
275 eval {
276 $som = $client->call($self->method, @params);
277 };
278 if ($@) {
279 $self->log()->error('SOAP call died: ' . $@);
280 die 'Fatal SOAP Error: ' . $@ . " [method=" . $self->method . ", params=(" . join(', ', @params) . ")]";
281 }
282  
283  
284 # restore environment
285 %ENV = %ENV_BACKUP;
286  
287 if ($som->fault) {
288 $self->log()->error('SOAP call returned error: ' . $som->fault->{faultstring});
289 die $som->fault->{faultstring};
290 }
291  
292 return $som->result;
293 }
294  
295  
296 sub get {
297 my $self = shift;
298  
299 my $result = $self->_soap_call(@_);
300 return if (! defined $result);
301  
302 if ((ref $result eq 'HASH') && $self->has_attrmap()) {
303 my @keys = keys %{$self->attrmap()};
304 if (scalar @keys != 1) {
305 $self->log()->error('SOAP result is hash but attrmap has more than one item');
306 die 'SOAP result is hash but attrmap has more than one item';
307 }
308 if (!defined $result->{$keys[0]}) {
309 return $self->_node_not_exists();
310 }
311 return $result->{$keys[0]};
312 }
313  
314 if (ref $result ne '') {
315 die "SOAP call result is not a scalar";
316 }
317  
318 return $result;
319 }
320  
321 sub get_size {
322 my $self = shift;
323  
324 my $result = $self->get_list(@_);
325 return scalar @{$result};
326 }
327  
328 sub get_list {
329 my $self = shift;
330  
331 my $result = $self->_soap_call(@_);
332  
333 return $self->_node_not_exists() if (! defined $result);
334  
335 if (ref $result ne 'ARRAY' ) {
336 die "SOAP call result is not a list";
337 }
338  
339 return @{$result};
340 }
341  
342 sub get_keys {
343 my $self = shift;
344  
345 my $result = $self->get_hash(@_);
346 return keys %{$result};
347 }
348  
349 sub get_hash {
350 my $self = shift;
351  
352 my $result = $self->_soap_call(@_);
353  
354 return $self->_node_not_exists() if (! defined $result);
355  
356 if (ref $result ne 'HASH' ) {
357 die "SOAP call result is not a hash";
358 }
359  
360  
361 my $res;
362 if ($self->has_attrmap()) {
363 my %map = %{$self->attrmap()};
364 foreach my $key (keys %map) {
365 $res->{ $map{$key} } = $result->{$key};
366 }
367 return $res;
368 }
369  
370 return $result;
371 }
372  
373 sub get_meta {
374 my $self = shift;
375 # FIXME
376 die "Sorry that is not supported, yet";
377 }
378  
379 sub exists {
380  
381 my $self = shift;
382  
383 # FIXME
384 die "Sorry that is not supported, yet";
385  
386 }
387 no Moose;
388 __PACKAGE__->meta->make_immutable;
389  
390 1;
391 __END__