blib/lib/Thrift/SASL/Transport.pm

filter: Statementstmt 17.6% 23 / 130 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 30.0% 6 / 20 Podpod 0.0% 0 / 9 totaltotal 10.4% 29 / 277 CC 47 SCAR 42.9
CC
47
Cov
10%
_sasl_handshake
240.0
flush
72.0
open
30.0
CRAP 72.8
↑ Summary
1 package Thrift::SASL::Transport;
2 2 $Thrift::SASL::Transport::VERSION = '0.007';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # Nasty hack to make the Thrift libs handle the extra 4-bytes
8 # header put by GSSAPI in front of unencoded (auth only) replies
9  
10 2 ▶use Thrift::BinaryProtocol;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 my $real_readMessageBegin = \&Thrift::BinaryProtocol::readMessageBegin;
12 {
13 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 *Thrift::BinaryProtocol::readMessageBegin = \&BinaryProtocolOverride_readMessageBegin;
15 }
16  
17 0 ▶sub BinaryProtocolOverride_readMessageBegin {
Subroutine BinaryProtocolOverride_readMessageBegin: not called CC=3 SCAR=24.8
Pod undocumented
18 0 my $self = shift;
19 0 ▶ if ( $self->{trans}{_sasl_client} && !$self->{trans}{_sasl_encode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"trans"}{"_sasl_client"} and not $self->{"trans"}{"_sasl_encode"}) 0 0 0
Condition0%0/3
$self->{"trans"}{"_sasl_client"} and not $self->{"trans"}{"_sasl_encode"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"trans"}{"_sasl_client"} and not $self->{"trans"}{"_sasl_encode"}
$self->{"trans"}{"_sasl_client"} not $self->{"trans"}{"_sasl_encode"}
Truth table0%0/3
A and not(B)
A= $self->{"trans"}{"_sasl_client"}B= not $self->{"trans"}{"_sasl_encode"}
ABresult
0X0
100
111
20 0 $self->readI32( \my $foo ); # discard GSSAPI auth header (message length)
21 }
22 0 return $real_readMessageBegin->( $self, @_ );
23 }
24  
25 # end of nasty hack, phew.
26  
27 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 SASL_START => 1,
29 SASL_OK => 2,
30 SASL_BAD => 3,
31 SASL_ERROR => 4,
32 SASL_COMPLETE => 5,
33 2 };
34  
35 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod undocumented
36 0 my ( $class, $transport, $sasl, $debug, $principal ) = @_;
37 0 ▶ return bless {
Branch: 1 branch
BranchTrueFalseTotal
$principal ? : 0 0 0
Condition0%0/2
$debug || 0
l!l
00
MC/DC0%0/1
$debug || 0
$debug
Truth table0%0/2
A
A= $debug
Aresult
11
00
38 _transport => $transport,
39 _sasl => $sasl,
40 _debug => $debug || 0,
41 ($principal ? (_principal => $principal): ()),
42 }, $class;
43 }
44  
45 0 ▶sub _sasl_write {
Subroutine _sasl_write: not called CC=3 SCAR=24.8
46 0 my ( $self, $code, $payload ) = @_;
47 0 ▶ $payload //= '';
Condition0%0/2
$payload //= ""
l!l
00
MC/DC0%0/1
$payload //= ""
$payload
Truth table0%0/2
A
A= $payload
Aresult
11
00
48 0 print STDERR "<< code $code + payload (@{[bytes::length($payload)]} bytes)\n"
49 0 ▶ if $self->{_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 0 0
50 0 $self->{_transport}->write( pack "CN", $code, bytes::length($payload) );
51 0 $self->{_transport}->write($payload);
52 0 $self->{_transport}->flush;
53 }
54  
55 0 ▶sub _sasl_read {
Subroutine _sasl_read: not called CC=4 SCAR=30.0
56 0 my ($self) = @_;
57 0 my $data = $self->read(5);
58 0 ▶ die "No data from server" unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
59 0 my ( $code, $length ) = unpack "CN", $data;
60 0 ▶ if ($length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length) { } 0 0 0
61 0 $data = $self->read($length);
62 }
63 else {
64 0 $data = undef;
65 }
66 print STDERR ">> code $code + response ($length bytes)\n"
67 0 ▶ if $self->{_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 0 0
68 0 return ( $code, $data );
69 }
70  
71 0 ▶sub open {
Subroutine open: not called CC=5 SCAR=34.0
Pod undocumented
72 0 my ($self) = @_;
73 0 ▶ $self->{_transport}->open if !( $self->{_transport} && $self->isOpen );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_transport"} and $self->isOpen 0 0 0
Condition0%0/3
$self->{"_transport"} and $self->isOpen
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_transport"} and $self->isOpen
$self->{"_transport"} $self->isOpen
Truth table0%0/3
A and B
A= $self->{"_transport"}B= $self->isOpen
ABresult
0X0
100
111
74 0 ▶ die "Could not open transport" if !$self->isOpen;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isOpen 0 0 0
75 0 ▶ return $self->{_sasl_client} || $self->_sasl_handshake;
Condition0%0/3
$self->{'_sasl_client'} || $self->_sasl_handshake
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'_sasl_client'} || $self->_sasl_handshake
$self->{'_sasl_client'} $self->_sasl_handshake
Truth table0%0/3
A || B
A= $self->{'_sasl_client'}B= $self->_sasl_handshake
ABresult
1X1
011
000
76 }
77  
78 0 ▶sub close {
Subroutine close: not called CC=3 SCAR=24.8
Pod undocumented
79 0 my ($self) = @_;
80 0 ▶ $self->{_transport}->close if $self->{_transport} && $self->isOpen;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_transport"} and $self->isOpen 0 0 0
Condition0%0/3
$self->{"_transport"} and $self->isOpen
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_transport"} and $self->isOpen
$self->{"_transport"} $self->isOpen
Truth table0%0/3
A and B
A= $self->{"_transport"}B= $self->isOpen
ABresult
0X0
100
111
81 0 return 1;
82 }
83  
84 0 ▶sub _sasl_handshake {
Subroutine _sasl_handshake: not called CC=15 SCAR=54.8
85 0 my ($self) = @_;
86  
87 print STDERR "SASL start: "
88 0 ▶ if $self->{_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 0 0
89 0 $self->_sasl_write( SASL_START, $self->{_sasl}->mechanism );
90  
91 # The socket passed to BufferedTransport was put in that object's
92 # "transport" property, this is a bit confusing imho
93 0 my $client;
94 0 ▶ if($self->{_sasl}->mechanism eq 'GSSAPI'){
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_sasl'}->mechanism eq 'GSSAPI') { } 0 0 0
elsif ($self->{'_sasl'}->mechanism eq 'DIGEST-MD5') { } 0 0 0
95 0 ▶ if(!$self->{_principal}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_principal"}) 0 0 0
96 0 die "Principal needs to be specified for kerberos authentication."
97 }
98 0 my @kerberos_name_split = split('[/@]', $self->{_principal});
99 0 ▶ if(scalar @kerberos_name_split != 3){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @kerberos_name_split != 3) 0 0 0
100 0 die "Kerberos principal name should have 3 parts. Eg: hive/_HOST\@REALM.COM";
101 }
102 0 $client = $self->{_sasl}->client_new( $kerberos_name_split[0], $kerberos_name_split[1] );
103 }elsif($self->{_sasl}->mechanism eq 'DIGEST-MD5'){
104 # Server is configured with null and default for authentication with delegationtoken.
105 0 $client = $self->{_sasl}->client_new( 'null', 'default' );
106 }else{
107 #Keeping this line here for keep support for other mechanisms.
108 0 $client = $self->{_sasl}->client_new( 'hive', $self->{_transport}{transport}{host} );
109 }
110 0 my $resp = $client->client_start();
111  
112 0 my $step;
113 0 ▶ while ( ++$step ) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$step) 0 0 0
114 print STDERR "SASL step $step: "
115 0 ▶ if $self->{_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 0 0
116  
117 #print STDERR Dumper{map {$_ => [$client->$_()]} qw(error code mechanism need_step)};
118 0 $self->_sasl_write( SASL_OK, $resp );
119 0 my ( $code, $data ) = $self->_sasl_read();
120  
121 0 ▶ if ( $code == SASL_COMPLETE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 5) 0 0 0
122 print STDERR "Authentication OK\n"
123 0 ▶ if $self->{_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 0 0
124  
125 #$client->client_step($data // '') if $client->need_step;
126 0 last;
127 }
128  
129 0 my $extra_msg = $self->__probe_env_and_xs_sasl_bug( $client );
130  
131 0 ▶ if ( $code == SASL_BAD || $code == SASL_ERROR ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 3 or $code == 4) 0 0 0
Condition0%0/3
$code == 3 or $code == 4
l!l&&r!l&&!r
000
MC/DC0%0/2
$code == 3 or $code == 4
$code == 3 $code == 4
Truth table0%0/3
A or B
A= $code == 3B= $code == 4
ABresult
1X1
011
000
132 0 ▶ die sprintf "Authentication failed: %s > %s%s",
Branch: 1 branch
BranchTrueFalseTotal
$extra_msg ? : 0 0 0
133 $code,
134 $data,
135 $extra_msg ? '. ' . $extra_msg : '',
136 ;
137 }
138  
139 0 $resp = $client->client_step($data);
140 0 ▶ if ( ! defined $data ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 0 0
141 0 ▶ die sprintf 'Client rejected authentication%s',
Branch: 1 branch
BranchTrueFalseTotal
$extra_msg ? : 0 0 0
142 $extra_msg ? '. ' . $extra_msg : '',
143 ;
144 }
145 }
146  
147 0 $self->{_sasl_encode_check} = 1;
148 0 $self->{_sasl_client} = $client;
149  
150 0 return $self->{_sasl_client};
151 }
152  
153 0 ▶sub __probe_env_and_xs_sasl_bug {
Subroutine __probe_env_and_xs_sasl_bug: not called CC=5 SCAR=34.0
154 # See: https://github.com/Perl-Hadoop/Thrift-SASL/issues/1
155 #
156 0 my($self, $client) = @_;
157 0 ▶ return if ! $client->isa('Authen::SASL::XS')
Branch: 1 branch
BranchTrueFalseTotal
unless $client->isa("Authen::SASL::XS") or $client->isa("Authen::SASL::Cyrus") 0 0 0
Condition0%0/3
$client->isa("Authen::SASL::XS") or $client->isa("Authen::SASL::Cyrus")
l!l&&r!l&&!r
000
MC/DC0%0/2
$client->isa("Authen::SASL::XS") or $client->isa("Authen::SASL::Cyrus")
$client->isa("Authen::SASL::XS") $client->isa("Authen::SASL::Cyrus")
Truth table0%0/3
A or B
A= $client->isa("Authen::SASL::XS")B= $client->isa("Authen::SASL::Cyrus")
ABresult
1X1
011
000
158 && ! $client->isa('Authen::SASL::Cyrus')
159 ;
160  
161 0 ▶ return if exists $ENV{USER} || exists $ENV{USERNAME};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ENV{"USER"} or exists $ENV{"USERNAME"} 0 0 0
Condition0%0/3
exists $ENV{"USER"} or exists $ENV{"USERNAME"}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $ENV{"USER"} or exists $ENV{"USERNAME"}
exists $ENV{"USER"} exists $ENV{"USERNAME"}
Truth table0%0/3
A or B
A= exists $ENV{"USER"}B= exists $ENV{"USERNAME"}
ABresult
1X1
011
000
162  
163 0 my $sasl_class = ref $client;
164  
165 0 return join ' ',
166 "cyrus-sasl and in turn $sasl_class needs either USER or USERNAME",
167 'environment variable to be present and they seem to be missing',
168 'in your environment.',
169 'The error you have received might be caused by that.',
170 ;
171 }
172  
173 0 ▶sub write {
Subroutine write: not called CC=2 SCAR=17.9
Pod undocumented
174 0 my $self = shift;
175 0 my $buffer = shift;
176 print STDERR "<< writing " . bytes::length($buffer) . " bytes\n"
177 0 ▶ if $self->{_debug} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} > 1 0 0 0
178 0 $self->{_out_buffer} .= $buffer;
179 0 return 1;
180 }
181  
182 0 ▶sub read {
Subroutine read: not called CC=3 SCAR=24.8
Pod undocumented
183 0 my $self = shift;
184 0 my @passthru_args = @_;
185 print STDERR ">> reading\n"
186 0 ▶ if $self->{_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 0 0
187 0 my $buf = $self->{_transport}->read(@passthru_args);
188 0 ▶ return $buf if bytes::length($buf) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if &bytes::length($buf) > 0 0 0 0
189  
190 # not sure about this, it is copied over from the python version
191 0 $self->_read_frame();
192 0 return $self->{_transport}->read(@passthru_args);
193 }
194  
195 # completely unsure about this, taken from the python version when trying to
196 # make the whole thing work, turned out my problem was with the BinaryProtocol
197 # needing a 4-byte offset on readMessageBegin as kerberos auth adds a 4 bytes
198 # header to replies, which was mistakenly used as the expected thrift header.
199 # leaving this in place in case it is actually needed (probably not working
200 # in the current state though)
201 0 ▶sub _read_frame {
Subroutine _read_frame: not called CC=3 SCAR=24.8
202 0 my $self = shift;
203 0 my $header = $self->{_transport}->readAll(4);
204 0 my $length = unpack( "N", $header );
205 0 my $decoded;
206 0 ▶ if ( $self->{_sasl_encode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_sasl_encode'}) { } 0 0 0
207 0 my $encoded = $header . $self->{_transport}->readAll($length);
208 0 ▶ my $decoded = $self->{_sasl_client}->decode($encoded)
Branch: 1 branch
BranchTrueFalseTotal
my $decoded = $self->{"_sasl_client"}->decode($encoded) or die "SASL decode returned nothing" 0 0 0
209 or die 'SASL decode returned nothing';
210  
211 # TODO throw a real per TTransportException like the python version
212 #die "SASL decode error: " .
213 # raise TTransportException(type=TTransportException.UNKNOWN,
214 # message=self.sasl.getError())
215 }
216 else {
217 0 $decoded = $self->{_transport}->readAll($length);
218 }
219 0 $self->{_transport}{rBuf} = $decoded;
220 }
221  
222 0 ▶sub flush {
Subroutine flush: not called CC=8 SCAR=42.8
Pod undocumented
223 0 my $self = shift;
224  
225 print STDERR "<<< flush " . bytes::length( $self->{_out_buffer} ) . " bytes \n"
226 0 ▶ if $self->{_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 0 0
227  
228 0 ▶ if ( $self->{_sasl_encode_check} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_sasl_encode_check"}) 0 0 0
229 0 my $encoded = $self->{_sasl_client}->encode( $self->{_out_buffer} );
230 0 ▶ if ( bytes::length($encoded)
Branch: 1 branch
BranchTrueFalseTotal
if (&bytes::length($encoded) and &bytes::length($encoded) != &bytes::length($self->{'_out_buffer'})) { } 0 0 0
Condition0%0/3
&bytes::length($encoded) and &bytes::length($encoded) != &bytes::length($self->{"_out_buffer"})
!ll&&!rl&&r
000
MC/DC0%0/2
&bytes::length($encoded) and &bytes::length($encoded) != &bytes::length($self->{"_out_buffer"})
&bytes::length($encoded) &bytes::length($encoded) != &bytes::length($self->{"_out_buffer"})
Truth table0%0/3
A and B
A= &bytes::length($encoded)B= &bytes::length($encoded) != &bytes::length($self->{"_out_buffer"})
ABresult
0X0
100
111
231 && bytes::length($encoded) != bytes::length( $self->{_out_buffer} ) )
232 {
233 0 $self->{_sasl_encode} = 1;
234 0 ▶ print STDERR "GSSAPI Will encode from now on\n" if $self->{_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 0 0
235 }
236 else {
237 0 $self->{_sasl_encode} = 0;
238 0 ▶ print STDERR "GSSAPI Will *not* encode from now on\n" if $self->{_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 0 0
239 }
240 0 $self->{_sasl_encode_check} = undef;
241 }
242 0 ▶ if ( $self->{_sasl_encode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_sasl_encode'}) { } 0 0 0
243 0 $self->{_out_buffer} = $self->{_sasl_client}->encode( $self->{_out_buffer} );
244 }
245 else {
246 $self->{_out_buffer}
247 0 = pack( "N", bytes::length( $self->{_out_buffer} ) ) . $self->{_out_buffer};
248 }
249 0 $self->{_transport}->write( $self->{_out_buffer} );
250 0 $self->{_transport}->flush();
251 0 $self->{_out_buffer} = '';
252  
253 #print STDERR Dumper $self;
254 }
255  
256 0 ▶sub isOpen {
Subroutine isOpen: not called CC=1 SCAR=6.9
Pod undocumented
257 0 shift->{_transport}->isOpen(@_);
258 }
259  
260 0 ▶sub readAll {
Subroutine readAll: not called CC=2 SCAR=17.9
Pod undocumented
261 0 my $self = shift;
262 print STDERR ">>> readAll $_[0] bytes\n"
263 0 ▶ if $self->{_debug} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} > 1 0 0 0
264 0 my $ret = $self->{_transport}->readAll(@_);
265 0 return $ret;
266 }
267  
268 2 1;
269  
270 #ABSTRACT: Thrift Transport allowing Kerberos auth/encryption through GSSAPI
271  
272 __END__
↑ Summary