Jmx4Perl/Agent/UserAgent.pm

filter: Statementstmt 35.6% 26 / 73 Branchbran 7.6% 2 / 26 Conditioncond 12.5% 1 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 53.3% 8 / 15 Podpod 37.5% 3 / 8 totaltotal 29.6% 40 / 135 CC 17 SCAR 26.1
CC
17
Cov
27%
get_basic_credentials
42.0
encrypt
20.0
decrypt
12.0
CRAP 13.6
« Jmx4Perl/Util.pm ↑ Summary Jmx4Perl/Config.pm »
1 #!/usr/bin/perl
2  
3 # Helper package in order to provide credentials
4 # in the request
5 package JMX::Jmx4Perl::Agent::UserAgent;
6 3 ▶use base qw(LWP::UserAgent);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Sys::SigAction;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use vars qw($HAS_BLOWFISH_PP $BF);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.1
14 3 $HAS_BLOWFISH_PP = eval "require Crypt::Blowfish_PP; 1";
15 3 ▶ if ($HAS_BLOWFISH_PP) {
Branch: 1 branch
BranchTrueFalseTotal
if ($JMX::Jmx4Perl::Agent::UserAgent::HAS_BLOWFISH_PP) 3 0 3
16 3 $BF = new Crypt::Blowfish_PP(pack("C10",0x16,0x51,0xAE,0x13,0xF2,0xFA,0x11,0x20,0x6E,0x6A));
17 }
18 }
19  
20  
21 =head1 NAME
22  
23 JMX::Jmx4Perl::Agent::UserAgent - Specialized L<LWP::UserAgent> adding
24 authentication support
25  
26 =head1 DESCRIPTION
27  
28 Simple subclass implementing an own C<get_basic_credentials> method for support
29 of basic and proxy authentication. This is an internal class used by
30 L<JMX::Jmx4Perl::Agent>.
31  
32 =cut
33  
34  
35 # Constructor setting the proper SSL options (if possible)
36 5 ▶sub new {
Subroutine new: called CC=3 SCAR=11.8
Pod documented
37 5 my $class = shift;
38 5 ▶ my @opts = @_ || ();
Condition33%1/3
@_ || ()
l!l&&r!l&&!r
005
MC/DC0%0/2
@_ || ()
@_ ()
Truth table33%1/3
A || B
A= @_B= ()
ABresult
1X1
011
000
39 5 ▶ if (LWP::UserAgent->VERSION >= 6.00) {
Branch: 1 branch
BranchTrueFalseTotal
if ("LWP::UserAgent"->VERSION >= 6) 5 0 5
40 # We don't verify Hostnames by default, since the information we are
41 # sending is typically not critical. Also, we don't have yet a way to
42 # configure a keystore, so this is the only chance for now. Ask me to add
43 # host certificate verification if wanted. It disabled only for LWP >= 6.00
44 5 push @opts,(ssl_opts => { verify_hostname => 0 });
45 };
46 5 return $class->SUPER::new(@opts);
47 }
48  
49 # Request using a more robust timeout See
50 # http://stackoverflow.com/questions/73308/true-timeout-on-lwpuseragent-request-method
51 # for details.
52 0 ▶sub request {
Subroutine request: not called CC=2 SCAR=13.7
Pod documented
53 0 my $self = shift;
54 0 my $req = shift;
55  
56 # Get whatever timeout is set for LWP and use that to
57 # enforce a maximum timeout per request.
58 3 ▶ use Sys::SigAction qw(timeout_call);
Subroutine BEGIN: called CC=1 SCAR=0.0
59 0 our $res = undef;
60 0 ▶ if (timeout_call($self->timeout(), sub { $res = $self->SUPER::request($req); })) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&timeout_call(scalar $self->timeout, sub { $res = $self->SUPER::request($req); } )) { } 0 0 0
61 # 408 == HTTP timeout
62 0 my $ret = HTTP::Response->new(408,"Got timeout in " . $self->timeout() . "s ");
63 0 $ret->request($req);
64 0 return $ret;
65 } else {
66 0 return $res;
67 }
68  
69 }
70  
71 5 ▶sub jjagent_config {
Subroutine jjagent_config: called CC=1 SCAR=0.0
Pod undocumented
72 5 my $self = shift;
73 5 $self->{jjagent_config} = shift;
74 }
75  
76 0 ▶sub get_basic_credentials {
Subroutine get_basic_credentials: not called CC=6 SCAR=37.4
Pod documented
77 0 my ($self, $realm, $uri, $isproxy) = @_;
78  
79 0 ▶ my $cfg = $self->{jjagent_config} || {};
Condition0%0/2
$self->{'jjagent_config'} || {}
l!l
00
MC/DC0%0/1
$self->{'jjagent_config'} || {}
$self->{'jjagent_config'}
Truth table0%0/2
A
A= $self->{'jjagent_config'}
Aresult
11
00
80 0 ▶ my $user = $isproxy ? $self->proxy_cfg($cfg,"user") : $cfg->{user};
Branch: 1 branch
BranchTrueFalseTotal
$isproxy ? : 0 0 0
81 0 ▶ my $password = $isproxy ? $self->proxy_cfg($cfg,"password") : $cfg->{password};
Branch: 1 branch
BranchTrueFalseTotal
$isproxy ? : 0 0 0
82 0 ▶ if ($user && $password) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user and $password) { } 0 0 0
Condition0%0/3
$user and $password
!ll&&!rl&&r
000
MC/DC0%0/2
$user and $password
$user $password
Truth table0%0/3
A and B
A= $userB= $password
ABresult
0X0
100
111
83 0 return ($user,$self->conditionally_decrypt($password));
84 } else {
85 0 return (undef,undef);
86 }
87 }
88  
89 0 ▶sub proxy_cfg {
Subroutine proxy_cfg: not called CC=2 SCAR=17.9
Pod undocumented
90 0 my ($self,$cfg,$what) = @_;
91 0 my $proxy = $cfg->{proxy};
92 0 ▶ if (ref($proxy) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proxy eq 'HASH') { } 0 0 0
93 0 return $proxy->{$what};
94 } else {
95 0 return $cfg->{"proxy_" . $what};
96 }
97 }
98  
99 0 ▶sub conditionally_decrypt {
Subroutine conditionally_decrypt: not called CC=2 SCAR=17.9
Pod undocumented
100 0 my $self = shift;
101 0 my $password = shift;
102 0 ▶ if ($password =~ /^\[\[\s*(.*)\s*\]\]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($password =~ /^\[\[\s*(.*)\s*\]\]$/) { } 0 0 0
103 # It's a encrypted password, lets decrypt it here
104 0 return decrypt($1);
105 } else {
106 0 return $password;
107 }
108 }
109  
110 0 ▶sub decrypt {
Subroutine decrypt: not called CC=3 SCAR=24.8
Pod undocumented
111 0 my $encrypted = shift;
112 0 ▶ die "No encryption available. Please install Crypt::Blowfish_PP" unless $HAS_BLOWFISH_PP;
Branch: 1 branch
BranchTrueFalseTotal
unless $JMX::Jmx4Perl::Agent::UserAgent::HAS_BLOWFISH_PP 0 0 0
113 0 my $rest = $encrypted;
114 0 my $ret = "";
115 0 ▶ while (length($rest) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rest > 0) 0 0 0
116 0 my $block = substr($rest,0,16);
117 0 $rest = substr($rest,16);
118 0 $ret .= $BF->decrypt(pack("H*",$block));
119 }
120 0 $ret =~ s/\s*$//;
121 0 return $ret;
122 }
123  
124 0 ▶sub encrypt {
Subroutine encrypt: not called CC=4 SCAR=30.0
Pod undocumented
125 0 my $plain = shift;
126 0 ▶ die "No encryption available. Please install Crypt::Blowfish_PP" unless $HAS_BLOWFISH_PP;
Branch: 1 branch
BranchTrueFalseTotal
unless $JMX::Jmx4Perl::Agent::UserAgent::HAS_BLOWFISH_PP 0 0 0
127 0 my $rest = $plain;
128 0 my $ret = "";
129 0 ▶ while (length($rest) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rest > 0) 0 0 0
130 0 my $block = substr($rest,0,8);
131 0 ▶ if (length($block) < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $block < 8) 0 0 0
132 0 $block .= " " x (8 - length($block));
133 }
134 0 $rest = substr($rest,8);
135 0 $ret .= unpack("H*",$BF->encrypt($block));
136 }
137 0 return $ret;
138 }
139  
140 =head1 LICENSE
141  
142 This file is part of jmx4perl.
143 Jmx4perl is free software: you can redistribute it and/or modify
144 it under the terms of the GNU General Public License as published by
145 The Free Software Foundation, either version 2 of the License, or
146 (at your option) any later version.
147
148 jmx4perl is distributed in the hope that it will be useful,
149 but WITHOUT ANY WARRANTY; without even the implied warranty of
150 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
151 GNU General Public License for more details.
152
153 You should have received a copy of the GNU General Public License
154 along with jmx4perl. If not, see <http://www.gnu.org/licenses/>.
155  
156 A commercial license is available as well. Please contact roland@cpan.org for
157 further details.
158  
159 =head1 AUTHOR
160  
161 roland@cpan.org
162  
163 =cut
164  
165 3 1;
166  
167 __DATA__
« Jmx4Perl/Util.pm ↑ Summary Jmx4Perl/Config.pm »