Serialized/Server.pm

filter: Statementstmt 84.8% 140 / 165 Branchbran 60.6% 40 / 66 Conditioncond 41.6% 5 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 87.5% 21 / 24 Podpod 21.4% 3 / 14 totaltotal 72.5% 209 / 288 CC 40 SCAR 19.4
CC
40
Cov
76%
log_call
13.4
dispatch
11.3
new
10.3
CRAP 7.0
« Serialized.pm ↑ Summary Serialized/AuthzHandler/ACL.pm »
1 package RPC::Serialized::Server;
2 {
3 16 $RPC::Serialized::Server::VERSION = '1.123630';
4 }
5  
6 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 16 ▶use base 'RPC::Serialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 16 ▶use UNIVERSAL;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 16 ▶use RPC::Serialized::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 16 ▶use RPC::Serialized::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 16 ▶use RPC::Serialized::AuthzHandler;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 16 __PACKAGE__->mk_ro_accessors(qw/
17 timeout
18 /);
19 16 __PACKAGE__->mk_accessors(qw/
20 trace handler_namespaces args_suppress_log callbacks
21 /);
22  
23 7 ▶sub new {
Subroutine new: called CC=7 SCAR=23.3
Pod documented
24 7 my $class = shift;
25 7 my $params = RPC::Serialized::Config->parse(@_);
26  
27 7 my $ns = $params->rpc_serialized->{handler_namespaces};
28 $params->rpc_serialized->{handler_namespaces} =
29 7 ▶ (!defined $ns ? [] : (!ref $ns ? [$ns] : $ns));
Branch: 2 branches
BranchTrueFalseTotal
!defined($ns) ? : 0 7 7
!ref($ns) ? : 5 2 7
30  
31 7 my $self = $class->SUPER::new($params);
32  
33 7 ▶ if ($self->trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->trace) 0 7 7
34 0 eval { require Log::Dispatch::Syslog };
35  
36 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
37 0 throw_app "Failed to load Log::Dispatch but trace is on: $@";
38 }
39 else {
40 0 $self->trace( Log::Dispatch::Syslog->new(
41 $params->log_dispatch_syslog,
42 ));
43 }
44 }
45  
46 # FIXME erm, should these be accessors?
47 $self->{HANDLER} = $params->rpc_serialized->{handlers}
48 7 ▶ if exists $params->rpc_serialized->{handlers};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params->rpc_serialized->{"handlers"} 0 7 7
49 7 $self->{AUTHZ_HANDLER} = RPC::Serialized::AuthzHandler->new;
50 $self->{CALLBACKS} = $params->rpc_serialized->{callbacks}
51 7 ▶ if exists $params->rpc_serialized->{callbacks};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params->rpc_serialized->{"callbacks"} 7 0 7
52  
53 7 return $self;
54 }
55  
56 38 ▶sub log {
Subroutine log: called CC=2 SCAR=9.2
Pod documented
57 38 my $self = shift;
58 38 ▶ return unless $self->trace;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->trace 38 0 38
59  
60 0 ( my $log = $self->ds->raw_serialize(@_) ) =~ s/^/[$$] /gm;
61 0 $self->trace->log( level => $self->trace->{min_level}, message => $log);
62 }
63  
64 19 ▶sub log_call {
Subroutine log_call: called CC=6 SCAR=26.0
Pod undocumented
65 19 my $self = shift;
66 19 my ( $call, $args ) = @_;
67  
68 # strip suppressed (sensitive) arguments, e.g. password fields
69 19 ▶ if (scalar @{$args} % 2 == 0
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$args;} % 2 == 0 and exists $self->args_suppress_log->{$call} and ref $self->args_suppress_log->{$call} eq ref []) 0 19 19
Condition50%3/6
scalar @{$args;} % 2 == 0 and exists $self->args_suppress_log->{$call} and ref $self->args_suppress_log->{$call} eq ref []
!ll&&!rl&&r
1900
scalar @{$args;} % 2 == 0 and exists $self->args_suppress_log->{$call}
!ll&&!rl&&r
6130
MC/DC0%0/3
scalar @{$args;} % 2 == 0 and exists $self->args_suppress_log->{$call} and ref $self->args_suppress_log->{$call} eq ref []
scalar @{$args;} % 2 == 0 exists $self->args_suppress_log->{$call} ref $self->args_suppress_log->{$call} eq ref []
Truth table50%2/4
A and B and C
A= scalar @{$args;} % 2 == 0B= exists $self->args_suppress_log->{$call}C= ref $self->args_suppress_log->{$call} eq ref []
ABCresult
0XX0
10X0
1100
1111
70 and exists $self->args_suppress_log->{$call}
71 and ref $self->args_suppress_log->{$call} eq ref []) {
72  
73 0 my %args = @{$args};
74 0 foreach ( @{ $self->args_suppress_log->{$call} } ) {
75 0 ▶ if ( exists $args{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{$_}) 0 0 0
76 0 $args{$_} = '[suppressed]';
77 }
78 }
79 0 $args = [%args];
80 }
81  
82 19 $self->log( { CALL => $call, SUBJECT => $self->subject, ARGS => $args } );
83 }
84  
85 19 ▶sub log_response {
Subroutine log_response: called CC=1 SCAR=0.0
Pod undocumented
86 19 my $self = shift;
87 19 my $response = shift;
88 19 $self->log($response);
89 }
90  
91 25 ▶sub handler {
Subroutine handler: called CC=3 SCAR=11.0
Pod documented
92 25 my $self = shift;
93 25 my $call = shift;
94  
95 25 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 19 25
96 6 $self->{HANDLER}->{$call} = shift;
97 }
98  
99 return $self->{HANDLER}->{$call}
100 25 ▶ if exists $self->{HANDLER}->{$call};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"HANDLER"}{$call} 15 10 25
101 10 return;
102 }
103  
104 13 ▶sub authz_handler {
Subroutine authz_handler: called CC=3 SCAR=16.0
Pod undocumented
105 13 my $self = shift;
106  
107 13 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 13 13
108 0 my $handler = shift;
109  
110 0 ▶ throw_app 'Not a RPC::Serialized::AuthzHandler'
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($handler, "RPC::Serialized::AuthzHandler") 0 0 0
111 unless UNIVERSAL::isa( $handler, 'RPC::Serialized::AuthzHandler' );
112 0 $self->{AUTHZ_HANDLER} = $handler;
113 }
114  
115 13 return $self->{AUTHZ_HANDLER};
116 }
117  
118 24 ▶sub recv {
Subroutine recv: called CC=6 SCAR=19.6
Pod undocumented
119 24 my $self = shift;
120 24 ▶ my ($data, @token) = $self->SUPER::recv or return;
Branch: 1 branch
BranchTrueFalseTotal
() = $self->SUPER::recv or return 19 5 24
121  
122 19 my $call = $data->{CALL};
123 19 ▶ throw_proto 'Invalid or missing CALL'
Branch: 1 branch
BranchTrueFalseTotal
unless $call and not ref $call 0 19 19
Condition33%1/3
$call and not ref $call
!ll&&!rl&&r
0019
MC/DC0%0/2
$call and not ref $call
$call not ref $call
Truth table33%1/3
A and not(B)
A= $callB= not ref $call
ABresult
0X0
100
111
124 unless $call and not ref($call);
125  
126 19 my $args = $data->{ARGS};
127 19 ▶ throw_proto 'Invalid or missing ARGS'
Branch: 1 branch
BranchTrueFalseTotal
unless $args and ref $args eq "ARRAY" 0 19 19
Condition33%1/3
$args and ref $args eq "ARRAY"
!ll&&!rl&&r
0019
MC/DC0%0/2
$args and ref $args eq "ARRAY"
$args ref $args eq "ARRAY"
Truth table33%1/3
A and B
A= $argsB= ref $args eq "ARRAY"
ABresult
0X0
100
111
128 unless $args and ref($args) eq 'ARRAY';
129  
130 19 return ( $call, $args, @token );
131 }
132  
133 32 ▶sub subject {
Subroutine subject: called CC=1 SCAR=0.0
Pod undocumented
134 32 my $self = shift;
135 32 return undef;
136 }
137  
138 13 ▶sub authorize {
Subroutine authorize: called CC=1 SCAR=0.0
Pod undocumented
139 13 my $self = shift;
140 13 my $call = shift;
141 13 my $target = shift;
142 13 $self->authz_handler->check_authz( $self->subject, $call, $target );
143 }
144  
145 19 ▶sub dispatch {
Subroutine dispatch: called CC=11 SCAR=24.3
Pod undocumented
146 19 my $self = shift;
147 19 my $call = shift;
148 19 my $args = shift;
149  
150 19 my $hc = undef;
151 19 ▶ if ($hc = $self->handler($call)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hc = $self->handler($call)) { } 9 10 19
152 9 ▶ eval "require $hc"
Branch: 1 branch
BranchTrueFalseTotal
eval "require $hc" or throw_system("Failed to load ${hc}: $@") 9 0 9
153 or throw_system "Failed to load $hc: $@";
154 }
155 else {
156 10 $call = quotemeta($call);
157 10 ▶ throw_app "Cannot search for invalid name: $call"
Branch: 1 branch
BranchTrueFalseTotal
if $call =~ /\W/ 0 10 10
158 if $call =~ m/\W/;
159  
160 10 (my $name = $call) =~ s/_([a-z])/::\u$1/g;
161 10 $name = ucfirst $name;
162  
163 10 foreach my $ns (@{ $self->handler_namespaces }) {
164 12 ▶ eval "require ${ns}::${name}" or next;
Branch: 1 branch
BranchTrueFalseTotal
eval "require ${ns}::$name" or next 4 8 12
165  
166 # install the handler class we have just found
167 4 $hc = "${ns}::$name";
168 4 $self->handler($call, $hc);
169 4 last;
170 }
171 }
172  
173 19 ▶ throw_app "No handler for $call"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hc 6 13 19
174 if !defined $hc;
175  
176 13 ▶ throw_app "$hc not a RPC::Serialized::Handler"
Branch: 1 branch
BranchTrueFalseTotal
unless $hc->isa("RPC::Serialized::Handler") 0 13 13
177 unless $hc->isa('RPC::Serialized::Handler');
178  
179 13 ▶ $self->authorize( $call, $hc->target(@$args) )
Branch: 1 branch
BranchTrueFalseTotal
$self->authorize($call, $hc->target(@$args)) or throw_authz("Permission denied") 13 0 13
180 or throw_authz "Permission denied";
181  
182 13 ▶ if ($self->callbacks->{pre_handler_argument_filter}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->callbacks->{"pre_handler_argument_filter"}) 2 11 13
183 2 eval {
184 2 $args = [ $self->callbacks->{pre_handler_argument_filter}->(
185 { call => $call, server => $self },
186 @$args) ];
187 };
188 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
189 0 throw_app sprintf("Callback '%s' for call '%s' returned %s"
190 , 'pre_handler_argument_filter'
191 , $call
192 , $@);
193 }
194 }
195
196 13 return { RESPONSE => $hc->invoke(@$args) };
197 }
198  
199 6 ▶sub exception {
Subroutine exception: called CC=2 SCAR=7.1
Pod undocumented
200 6 my $self = shift;
201 6 my $err = shift;
202  
203 6 my $exception;
204 6 ▶ if ( UNIVERSAL::isa( $err, 'RPC::Serialized::X' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($err, 'RPC::Serialized::X')) { } 6 0 6
205 6 $exception = {
206 CLASS => ref($err),
207 MESSAGE => $err->message
208 };
209 }
210 else {
211 0 $exception = {
212 CLASS => 'RPC::Serialized::X',
213 MESSAGE => "$err"
214 };
215 }
216  
217 6 return { EXCEPTION => $exception };
218 }
219  
220 5 ▶sub process {
Subroutine process: called CC=8 SCAR=21.6
Pod undocumented
221 5 my $self = shift;
222  
223 5 my $alarm_bak = 0;
224 5 my @token_bak = ();
225  
226 5 while ( 1 ) {
227 24 my ($response, @token);
228  
229 24 eval {
230 0 ▶ local $SIG{ALRM} = sub { die "Timeout on Receive\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
231 24 $alarm_bak = alarm $self->timeout;
232 24 (my ($call, $args), @token) = ($self->recv);
233 24 alarm $alarm_bak;
234  
235 24 ▶ if ($call) {
Branch: 1 branch
BranchTrueFalseTotal
if ($call) 19 5 24
236 19 $self->log_call( $call, $args );
237  
238 0 ▶ local $SIG{ALRM} = sub { die "Timeout on Dispatch\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
239 19 $alarm_bak = alarm $self->timeout;
240 19 $response = $self->dispatch( $call, $args );
241 13 alarm $alarm_bak;
242 }
243 };
244 24 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 6 18 24
245 6 alarm $alarm_bak;
246 6 $response = $self->exception($@);
247 }
248  
249 24 ▶ last unless $response;
Branch: 1 branch
BranchTrueFalseTotal
unless $response 5 19 24
250 19 $self->log_response($response);
251  
252 # use same serializer for response as on received msg
253 19 ▶ @token_bak = $self->set_token(@token)
Branch: 1 branch
BranchTrueFalseTotal
unless $self->debug 19 0 19
254 if !$self->debug;
255  
256 19 eval {
257 0 ▶ local $SIG{ALRM} = sub { die "Timeout on Send\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
258 19 $alarm_bak = alarm $self->timeout;
259 19 $self->send($response);
260 19 alarm $alarm_bak;
261 };
262 19 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 19 19
263 0 alarm $alarm_bak;
264 0 ▶ $self->restore_token(@token_bak) if !$self->debug;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->debug 0 0 0
265 0 throw_system $@; # likely caught outside of RPC::Serialized
266 }
267  
268 # restore our default serializer
269 19 ▶ $self->restore_token(@token_bak) if !$self->debug;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->debug 19 0 19
270 }
271  
272 5 alarm $alarm_bak;
273 }
274  
275 38 ▶sub restore_token {
Subroutine restore_token: called CC=1 SCAR=0.0
Pod undocumented
276 38 my $self = shift;
277 38 my ($serializer, $cipher, $digester, $encoding, $compressor) = @_;
278  
279 38 $self->ds->serializer($serializer);
280 38 $self->ds->cipher($cipher);
281 38 $self->ds->digester($digester);
282 38 $self->ds->encoding($encoding);
283 38 $self->ds->compressor($compressor);
284 }
285  
286 19 ▶sub set_token {
Subroutine set_token: called CC=1 SCAR=0.0
Pod undocumented
287 19 my $self = shift;
288 19 my ($serializer, $cipher, $digester, $encoding, $compressor) = @_;
289  
290 19 my @retval = (
291 $self->ds->serializer,
292 $self->ds->cipher,
293 $self->ds->digester,
294 $self->ds->encoding,
295 $self->ds->compressor,
296 );
297  
298 19 $self->restore_token(@_);
299 19 return @retval;
300 }
301  
302 16 1;
303  
« Serialized.pm ↑ Summary Serialized/AuthzHandler/ACL.pm »