blib/lib/Amazon/Credentials/Provider/WebIdentity.pm

filter: Statementstmt 98.0% 98 / 100 Branchbran 62.5% 20 / 32 Conditioncond 65.9% 29 / 44 MC/DCmcdc 44.0% 11 / 25 Subroutinesub 100% 17 / 17 Podpod 33.3% 1 / 3 totaltotal 79.6% 176 / 221 CC 33 SCAR 21.9
CC
33
Cov
84%
new
17.0
_extract_sts_error
10.1
_read_token_file
6.8
CRAP 8.9
« blib/lib/Amazon/Credentials/Resolver/Profile.pm ↑ Summary blib/lib/Amazon/Credentials.pm »
1 package Amazon::Credentials::Provider::WebIdentity;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 ▶use parent qw(Amazon::Credentials::Provider);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 __PACKAGE__->follow_best_practice;
11  
12 10 __PACKAGE__->mk_accessors(
13 qw(
14 role_arn
15 role_session_name
16 token_file
17 user_agent
18 )
19 );
20  
21 10 ▶use Amazon::Credentials::HTTP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 10 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 10 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 10 ▶use constant { ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
27 10 DEFAULT_REGION => 'us-east-1',
28 DEFAULT_SESSION_NAME => 'amazon-credentials-session',
29 10 };
30  
31 ########################################################################
32 9 ▶sub new {
Subroutine new: called CC=16 SCAR=28.3
Pod documented
33 ########################################################################
34 9 my ( $class, @args ) = @_;
35  
36 0 ▶ my $options = ref $args[0] ? { %{ $args[0] } } : {@args};
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 9 9
37  
38 9 ▶ my $token_file = $options->{token_file} // $ENV{AWS_WEB_IDENTITY_TOKEN_FILE};
Condition100%3/3
$options->{"token_file"} // $ENV{"AWS_WEB_IDENTITY_TOKEN_FILE"}
l!l&&r!l&&!r
162
MC/DC100%2/2
$options->{"token_file"} // $ENV{"AWS_WEB_IDENTITY_TOKEN_FILE"}
$options->{"token_file"} $ENV{"AWS_WEB_IDENTITY_TOKEN_FILE"}
Truth table100%3/3
A // B
A= $options->{"token_file"}B= $ENV{"AWS_WEB_IDENTITY_TOKEN_FILE"}
ABresult
1X1
011
000
39  
40 9 ▶ my $role_arn = $options->{role_arn} // $ENV{AWS_ROLE_ARN};
Condition100%3/3
$options->{"role_arn"} // $ENV{"AWS_ROLE_ARN"}
l!l&&r!l&&!r
162
MC/DC100%2/2
$options->{"role_arn"} // $ENV{"AWS_ROLE_ARN"}
$options->{"role_arn"} $ENV{"AWS_ROLE_ARN"}
Truth table100%3/3
A // B
A= $options->{"role_arn"}B= $ENV{"AWS_ROLE_ARN"}
ABresult
1X1
011
000
41  
42 return
43 9 ▶ if !$token_file && !$role_arn;
Branch: 1 branch
BranchTrueFalseTotal
unless $token_file or $role_arn 1 8 9
Condition100%3/3
$token_file or $role_arn
l!l&&r!l&&!r
711
MC/DC100%2/2
$token_file or $role_arn
$token_file $role_arn
Truth table100%3/3
A or B
A= $token_fileB= $role_arn
ABresult
1X1
011
000
44  
45 8 ▶ croak 'AWS_WEB_IDENTITY_TOKEN_FILE is set but AWS_ROLE_ARN is not set'
Branch: 1 branch
BranchTrueFalseTotal
if $token_file and not $role_arn 1 7 8
Condition100%3/3
$token_file and not $role_arn
!ll&&!rl&&r
161
MC/DC100%2/2
$token_file and not $role_arn
$token_file not $role_arn
Truth table100%3/3
A and not(B)
A= $token_fileB= not $role_arn
ABresult
0X0
100
111
46 if $token_file && !$role_arn;
47  
48 7 ▶ croak 'AWS_ROLE_ARN is set but AWS_WEB_IDENTITY_TOKEN_FILE is not set'
Branch: 1 branch
BranchTrueFalseTotal
if not $token_file and $role_arn 1 6 7
Condition66%2/3
not $token_file and $role_arn
!ll&&!rl&&r
601
MC/DC50%1/2
not $token_file and $role_arn
not $token_file $role_arn
Truth table66%2/3
not(A) and B
A= not $token_fileB= $role_arn
ABresult
0X0
100
111
49 if !$token_file && $role_arn;
50  
51 6 ▶ my $role_session_name = $options->{role_session_name} // $ENV{AWS_ROLE_SESSION_NAME} // DEFAULT_SESSION_NAME;
Condition40%2/5
($options->{"role_session_name"} // $ENV{"AWS_ROLE_SESSION_NAME"}) // "amazon-credentials-session"
l!l
06
$options->{"role_session_name"} // $ENV{"AWS_ROLE_SESSION_NAME"}
l!l&&r!l&&!r
006
MC/DC0%0/2
($options->{"role_session_name"} // $ENV{"AWS_ROLE_SESSION_NAME"}) // "amazon-credentials-session"
$options->{"role_session_name"} $ENV{"AWS_ROLE_SESSION_NAME"}
Truth table33%1/3
A // B
A= $options->{"role_session_name"}B= $ENV{"AWS_ROLE_SESSION_NAME"}
ABresult
1X1
011
000
52  
53 6 ▶ my $region = $options->{region} // $ENV{AWS_REGION} // $ENV{AWS_DEFAULT_REGION} // DEFAULT_REGION;
Condition87%7/8
(($options->{"region"} // $ENV{"AWS_REGION"}) // $ENV{"AWS_DEFAULT_REGION"}) // "us-east-1"
l!l
33
($options->{"region"} // $ENV{"AWS_REGION"}) // $ENV{"AWS_DEFAULT_REGION"}
l!l&&r!l&&!r
303
$options->{"region"} // $ENV{"AWS_REGION"}
l!l&&r!l&&!r
123
MC/DC66%2/3
(($options->{"region"} // $ENV{"AWS_REGION"}) // $ENV{"AWS_DEFAULT_REGION"}) // "us-east-1"
$options->{"region"} $ENV{"AWS_REGION"} $ENV{"AWS_DEFAULT_REGION"}
Truth table75%3/4
A // B // C
A= $options->{"region"}B= $ENV{"AWS_REGION"}C= $ENV{"AWS_DEFAULT_REGION"}
ABCresult
1XX1
01X1
0011
0000
54  
55 my $ua = $options->{user_agent}
56 6 ▶ || Amazon::Credentials::HTTP::UserAgent->new;
Condition33%1/3
$options->{'user_agent'} || 'Amazon::Credentials::HTTP::UserAgent'->new
l!l&&r!l&&!r
600
MC/DC0%0/2
$options->{'user_agent'} || 'Amazon::Credentials::HTTP::UserAgent'->new
$options->{'user_agent'} 'Amazon::Credentials::HTTP::UserAgent'->new
Truth table33%1/3
A || B
A= $options->{'user_agent'}B= 'Amazon::Credentials::HTTP::UserAgent'->new
ABresult
1X1
011
000
57  
58 6 my $credentials = $class->_get_credentials(
59 region => $region,
60 role_arn => $role_arn,
61 role_session_name => $role_session_name,
62 token_file => $token_file,
63 user_agent => $ua,
64 );
65  
66 my $self = $class->SUPER::new(
67 aws_access_key_id => $credentials->{AccessKeyId},
68 aws_secret_access_key => $credentials->{SecretAccessKey},
69 expiration => $credentials->{Expiration},
70 region => $region,
71 source => 'web_identity',
72 token => $credentials->{SessionToken},
73 5 );
74  
75 5 $self->set_role_arn($role_arn);
76 5 $self->set_role_session_name($role_session_name);
77 5 $self->set_token_file($token_file);
78 5 $self->set_user_agent($ua);
79  
80 5 return $self;
81 }
82  
83 ########################################################################
84 7 ▶sub _get_credentials {
Subroutine _get_credentials: called CC=6 SCAR=18.1
85 ########################################################################
86 7 my ( $self, @args ) = @_;
87  
88 7 ▶ my $options
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 7 7
89 = ref $args[0]
90 ? $args[0]
91 : {@args};
92  
93 7 my $token = _read_token_file( $options->{token_file} );
94  
95 my $sts_host = sprintf
96 'https://sts.%s.amazonaws.com',
97 7 $options->{region};
98  
99 my %params = (
100 Action => 'AssumeRoleWithWebIdentity',
101 RoleArn => $options->{role_arn},
102 RoleSessionName => $options->{role_session_name},
103 7 Version => '2011-06-15',
104 WebIdentityToken => $token,
105 );
106  
107 7 my $query = join q{&}, map { _uri_escape($_) . q{=} . _uri_escape( $params{$_} ) }
108 sort keys %params;
109  
110 7 my $url = "$sts_host/?$query";
111  
112 7 my $req = HTTP::Request->new( GET => $url );
113  
114 7 $req->header( Accept => '*/*' );
115  
116 7 my $rsp = $options->{user_agent}->request($req);
117  
118 7 ▶ croak 'no response from AssumeRoleWithWebIdentity'
Branch: 1 branch
BranchTrueFalseTotal
unless $rsp 0 7 7
119 if !$rsp;
120  
121 7 ▶ if ( !$rsp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rsp->is_success) 1 6 7
122 1 my $detail = _extract_sts_error( $rsp->content );
123  
124 1 croak sprintf
125 'AssumeRoleWithWebIdentity failed: %s%s',
126 $rsp->status_line,
127 $detail;
128 }
129  
130 6 my $xml = $rsp->content;
131  
132 6 my $credentials = {
133 AccessKeyId => _xml_element( $xml, 'AccessKeyId' ),
134 SecretAccessKey => _xml_element( $xml, 'SecretAccessKey' ),
135 SessionToken => _xml_element( $xml, 'SessionToken' ),
136 Expiration => _xml_element( $xml, 'Expiration' ),
137 };
138  
139 croak 'AssumeRoleWithWebIdentity returned no AccessKeyId'
140 6 ▶ if !$credentials->{AccessKeyId};
Branch: 1 branch
BranchTrueFalseTotal
unless $credentials->{"AccessKeyId"} 0 6 6
141  
142 croak 'AssumeRoleWithWebIdentity returned no SecretAccessKey'
143 6 ▶ if !$credentials->{SecretAccessKey};
Branch: 1 branch
BranchTrueFalseTotal
unless $credentials->{"SecretAccessKey"} 0 6 6
144  
145 6 return $credentials;
146 }
147  
148 ########################################################################
149 7 ▶sub _read_token_file {
Subroutine _read_token_file: called CC=6 SCAR=19.2
150 ########################################################################
151 7 my ($token_file) = @_;
152  
153 7 ▶ open my $fh, '<', $token_file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $token_file or croak("cannot open web identity token file '${token_file}': $Amazon::Credentials::Provider::WebIdentity::OS_ERROR") 7 0 7
154 or croak "cannot open web identity token file '$token_file': $OS_ERROR";
155  
156 7 my $token;
157  
158 {
159 7 local $RS = undef;
160 7 $token = <$fh>;
161 }
162  
163 7 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("cannot close web identity token file '${token_file}': $Amazon::Credentials::Provider::WebIdentity::OS_ERROR") 7 0 7
164 or croak "cannot close web identity token file '$token_file': $OS_ERROR";
165  
166 7 ▶ chomp $token
Branch: 1 branch
BranchTrueFalseTotal
if defined $token 7 0 7
167 if defined $token;
168  
169 7 ▶ croak "web identity token file '$token_file' is empty"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token and length $token 0 7 7
Condition33%1/3
defined $token and length $token
!ll&&!rl&&r
007
MC/DC0%0/2
defined $token and length $token
defined $token length $token
Truth table33%1/3
A and B
A= defined $tokenB= length $token
ABresult
0X0
100
111
170 if !defined $token || !length $token;
171  
172 7 return $token;
173 }
174  
175 ########################################################################
176 26 ▶sub _xml_element {
Subroutine _xml_element: called CC=2 SCAR=7.6
177 ########################################################################
178 26 my ( $xml, $tag ) = @_;
179  
180 26 ▶ if ( $xml =~ m{<$tag>([^<]+)</$tag>}xsm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xml =~ m[<$tag>([^<]+)</$tag>]msx) 26 0 26
181 26 return $1;
182 }
183  
184 0 return undef; ## no critic
185 }
186  
187 ########################################################################
188 1 ▶sub _extract_sts_error {
Subroutine _extract_sts_error: called CC=7 SCAR=23.2
189 ########################################################################
190 1 my ($xml) = @_;
191  
192 1 ▶ return q{}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xml and length $xml 0 1 1
Condition33%1/3
defined $xml and length $xml
!ll&&!rl&&r
001
MC/DC0%0/2
defined $xml and length $xml
defined $xml length $xml
Truth table33%1/3
A and B
A= defined $xmlB= length $xml
ABresult
0X0
100
111
193 if !defined $xml || !length $xml;
194  
195 1 ▶ my $code = _xml_element( $xml, 'Code' ) // q{};
Condition50%1/2
_xml_element($xml, "Code") // ""
l!l
10
MC/DC0%0/1
_xml_element($xml, "Code") // ""
_xml_element($xml, "Code")
Truth table50%1/2
A
A= _xml_element($xml, "Code")
Aresult
11
00
196  
197 1 ▶ my $message = _xml_element( $xml, 'Message' ) // q{};
Condition50%1/2
_xml_element($xml, "Message") // ""
l!l
10
MC/DC0%0/1
_xml_element($xml, "Message") // ""
_xml_element($xml, "Message")
Truth table50%1/2
A
A= _xml_element($xml, "Message")
Aresult
11
00
198  
199 1 ▶ return q{}
Branch: 1 branch
BranchTrueFalseTotal
unless $code or $message 0 1 1
Condition33%1/3
$code or $message
l!l&&r!l&&!r
100
MC/DC0%0/2
$code or $message
$code $message
Truth table33%1/3
A or B
A= $codeB= $message
ABresult
1X1
011
000
200 if !$code && !$message;
201  
202 1 return " - $code: $message";
203 }
204  
205 ########################################################################
206 70 ▶sub _uri_escape {
Subroutine _uri_escape: called CC=1 SCAR=0.0
207 ########################################################################
208 70 my ($str) = @_;
209  
210 70 $str =~ s/([^A-Za-z0-9\-_.~])/sprintf '%%%02X', ord($1)/xsmeg;
211  
212 70 return $str;
213 }
214  
215 ########################################################################
216 1 ▶sub is_refreshable {
Subroutine is_refreshable: called CC=1 SCAR=0.0
Pod undocumented
217 ########################################################################
218 1 return 1;
219 }
220  
221 ########################################################################
222 1 ▶sub refresh_credentials {
Subroutine refresh_credentials: called CC=1 SCAR=0.0
Pod undocumented
223 ########################################################################
224 1 my ($self) = @_;
225  
226 1 my $credentials = $self->_get_credentials(
227 region => $self->get_region,
228 role_arn => $self->get_role_arn,
229 role_session_name => $self->get_role_session_name,
230 token_file => $self->get_token_file,
231 user_agent => $self->get_user_agent,
232 );
233  
234 1 $self->set_aws_access_key_id( $credentials->{AccessKeyId} );
235  
236 1 $self->set_aws_secret_access_key( $credentials->{SecretAccessKey} );
237  
238 1 $self->set_token( $credentials->{SessionToken} );
239  
240 1 $self->set_expiration( $credentials->{Expiration} );
241  
242 1 $self->set_last_refreshed(time);
243  
244 1 return $self;
245 }
246  
247 10 1;
« blib/lib/Amazon/Credentials/Resolver/Profile.pm ↑ Summary blib/lib/Amazon/Credentials.pm »