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

filter: Statementstmt 98.2% 112 / 114 Branchbran 76.4% 26 / 34 Conditioncond 62.8% 22 / 35 MC/DCmcdc 31.5% 6 / 19 Subroutinesub 100% 17 / 17 Podpod 33.3% 1 / 3 totaltotal 82.8% 184 / 222 CC 32 SCAR 20.6
CC
32
Cov
87%
new
12.7
_assume_role
11.1
_extract_sts_error
10.1
CRAP 7.9
« blib/lib/Amazon/Credentials.pm ↑ Summary blib/lib/Amazon/Credentials/Provider/InstanceRole.pm »
1 package Amazon::Credentials::Provider::AssumeRole;
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 duration_seconds
15 external_id
16 mfa_serial
17 mfa_token
18 role_arn
19 role_session_name
20 source_provider
21 user_agent
22 )
23 );
24  
25 10 ▶use Amazon::Credentials::HTTP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 10 ▶use Amazon::Signature4::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 10 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 10 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 10 ▶use URI::Escape qw(uri_escape_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 10 ▶use constant { ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
33 10 DEFAULT_REGION => 'us-east-1',
34 DEFAULT_SESSION_NAME => 'amazon-credentials-session',
35 STS_VERSION => '2011-06-15',
36 10 };
37  
38 ########################################################################
39 26 ▶sub new {
Subroutine new: called CC=12 SCAR=25.4
Pod documented
40 ########################################################################
41 26 my ( $class, @args ) = @_;
42  
43 my $options
44 = ref $args[0]
45 0 ▶ ? { %{ $args[0] } }
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 26 26
46 : {@args};
47  
48 26 my $role_arn = $options->{role_arn};
49  
50 return
51 26 ▶ if !defined $role_arn || !length $role_arn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $role_arn and length $role_arn 1 25 26
Condition66%2/3
defined $role_arn and length $role_arn
!ll&&!rl&&r
1025
MC/DC50%1/2
defined $role_arn and length $role_arn
defined $role_arn length $role_arn
Truth table66%2/3
A and B
A= defined $role_arnB= length $role_arn
ABresult
0X0
100
111
52  
53 25 my $source_provider = $options->{source_provider};
54  
55 25 ▶ croak 'source_provider is required for AssumeRole'
Branch: 1 branch
BranchTrueFalseTotal
unless $source_provider and ref $source_provider 1 24 25
Condition66%2/3
$source_provider and ref $source_provider
!ll&&!rl&&r
1024
MC/DC50%1/2
$source_provider and ref $source_provider
$source_provider ref $source_provider
Truth table66%2/3
A and B
A= $source_providerB= ref $source_provider
ABresult
0X0
100
111
56 if !$source_provider || !ref $source_provider;
57  
58 my $region = $options->{region} // $source_provider->get_region // $ENV{AWS_REGION} // $ENV{AWS_DEFAULT_REGION}
59 24 ▶ // DEFAULT_REGION;
Condition81%9/11
((($options->{"region"} // $source_provider->get_region) // $ENV{"AWS_REGION"}) // $ENV{"AWS_DEFAULT_REGION"}) // "us-east-1"
l!l
222
(($options->{"region"} // $source_provider->get_region) // $ENV{"AWS_REGION"}) // $ENV{"AWS_DEFAULT_REGION"}
l!l&&r!l&&!r
2202
($options->{"region"} // $source_provider->get_region) // $ENV{"AWS_REGION"}
l!l&&r!l&&!r
2202
$options->{"region"} // $source_provider->get_region
l!l&&r!l&&!r
8142
MC/DC50%2/4
((($options->{"region"} // $source_provider->get_region) // $ENV{"AWS_REGION"}) // $ENV{"AWS_DEFAULT_REGION"}) // "us-east-1"
$options->{"region"} $source_provider->get_region $ENV{"AWS_REGION"} $ENV{"AWS_DEFAULT_REGION"}
Truth table60%3/5
A // B // C // D
A= $options->{"region"}B= $source_provider->get_regionC= $ENV{"AWS_REGION"}D= $ENV{"AWS_DEFAULT_REGION"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
60  
61 24 ▶ my $role_session_name = $options->{role_session_name} // DEFAULT_SESSION_NAME;
Condition100%2/2
$options->{"role_session_name"} // "amazon-credentials-session"
l!l
420
MC/DC100%1/1
$options->{"role_session_name"} // "amazon-credentials-session"
$options->{"role_session_name"}
Truth table100%2/2
A
A= $options->{"role_session_name"}
Aresult
11
00
62  
63 my $ua = $options->{user_agent}
64 24 ▶ || Amazon::Credentials::HTTP::UserAgent->new;
Condition33%1/3
$options->{'user_agent'} || 'Amazon::Credentials::HTTP::UserAgent'->new
l!l&&r!l&&!r
2400
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
65  
66 my $credentials = $class->_assume_role(
67 duration_seconds => $options->{duration_seconds},
68 external_id => $options->{external_id},
69 mfa_serial => $options->{mfa_serial},
70 mfa_token => $options->{mfa_token},
71 24 region => $region,
72 role_arn => $role_arn,
73 role_session_name => $role_session_name,
74 source_provider => $source_provider,
75 user_agent => $ua,
76 );
77  
78 my $self = $class->SUPER::new(
79 aws_access_key_id => $credentials->{AccessKeyId},
80 aws_secret_access_key => $credentials->{SecretAccessKey},
81 expiration => $credentials->{Expiration},
82 region => $region,
83 source => 'assume_role',
84 token => $credentials->{SessionToken},
85 23 );
86  
87 23 $self->set_duration_seconds( $options->{duration_seconds} );
88  
89 23 $self->set_external_id( $options->{external_id} );
90  
91 23 $self->set_mfa_serial( $options->{mfa_serial} );
92  
93 23 $self->set_role_arn($role_arn);
94  
95 23 $self->set_role_session_name($role_session_name);
96  
97 23 $self->set_source_provider($source_provider);
98  
99 23 $self->set_user_agent($ua);
100  
101 23 return $self;
102 }
103  
104 ########################################################################
105 29 ▶sub _assume_role {
Subroutine _assume_role: called CC=11 SCAR=24.1
106 ########################################################################
107 29 my ( $self, @args ) = @_;
108  
109 29 ▶ my $options
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 29 29
110 = ref $args[0]
111 ? $args[0]
112 : {@args};
113  
114 my %params = (
115 Action => 'AssumeRole',
116 RoleArn => $options->{role_arn},
117 RoleSessionName => $options->{role_session_name},
118 29 Version => STS_VERSION,
119 );
120  
121 29 ▶ if ( defined $options->{mfa_serial} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"mfa_serial"}) 5 24 29
122 5 $params{SerialNumber} = $options->{mfa_serial};
123 5 $params{TokenCode} = $options->{mfa_token};
124 }
125  
126 29 ▶ if ( defined $options->{external_id}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"external_id"} and length $options->{"external_id"}) 2 27 29
Condition66%2/3
defined $options->{"external_id"} and length $options->{"external_id"}
!ll&&!rl&&r
2702
MC/DC50%1/2
defined $options->{"external_id"} and length $options->{"external_id"}
defined $options->{"external_id"} length $options->{"external_id"}
Truth table66%2/3
A and B
A= defined $options->{"external_id"}B= length $options->{"external_id"}
ABresult
0X0
100
111
127 && length $options->{external_id} ) {
128  
129 2 $params{ExternalId} = $options->{external_id};
130 }
131  
132 29 ▶ if ( defined $options->{duration_seconds} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"duration_seconds"}) 2 27 29
133 2 $params{DurationSeconds} = $options->{duration_seconds};
134 }
135  
136 29 my $content = join q{&}, map { uri_escape_utf8($_) . q{=} . uri_escape_utf8( $params{$_} ) }
137 sort keys %params;
138  
139 29 my $region = $options->{region};
140  
141 29 my $url = sprintf 'https://sts.%s.amazonaws.com/', $region;
142  
143 29 my %headers = ( 'content-type' => 'application/x-www-form-urlencoded; charset=utf-8', );
144  
145 29 my $source = $options->{source_provider};
146  
147 29 my $signer = Amazon::Signature4::Lite->new(
148 access_key => $source->get_aws_access_key_id,
149 secret_key => $source->get_aws_secret_access_key,
150 session_token => $source->get_token,
151 region => $region,
152 service => 'sts',
153 );
154  
155 29 my $signed_headers = $signer->sign(
156 headers => \%headers,
157 method => 'POST',
158 payload => \$content,
159 url => $url,
160 add_sha256_header => 0,
161 );
162  
163 29 my $req = HTTP::Request->new( POST => $url );
164  
165 29 foreach my $header ( keys %{$signed_headers} ) {
166 130 $req->header( $header, $signed_headers->{$header} );
167 }
168  
169 29 $req->content($content);
170  
171 29 my $rsp = $options->{user_agent}->request($req);
172  
173 29 ▶ croak 'no response from AssumeRole'
Branch: 1 branch
BranchTrueFalseTotal
unless $rsp 0 29 29
174 if !$rsp;
175  
176 29 ▶ if ( !$rsp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rsp->is_success) 1 28 29
177 1 my $detail = _extract_sts_error( $rsp->content );
178  
179 1 croak sprintf
180 'AssumeRole failed: %s%s',
181 $rsp->status_line,
182 $detail;
183 }
184  
185 28 my $xml = $rsp->content;
186  
187 28 my $credentials = {
188 AccessKeyId => _xml_element( $xml, 'AccessKeyId' ),
189  
190 SecretAccessKey => _xml_element( $xml, 'SecretAccessKey' ),
191  
192 SessionToken => _xml_element( $xml, 'SessionToken' ),
193  
194 Expiration => _xml_element( $xml, 'Expiration' ),
195 };
196  
197 croak 'AssumeRole returned no AccessKeyId'
198 28 ▶ if !$credentials->{AccessKeyId};
Branch: 1 branch
BranchTrueFalseTotal
unless $credentials->{"AccessKeyId"} 0 28 28
199  
200 croak 'AssumeRole returned no SecretAccessKey'
201 28 ▶ if !$credentials->{SecretAccessKey};
Branch: 1 branch
BranchTrueFalseTotal
unless $credentials->{"SecretAccessKey"} 0 28 28
202  
203 28 return $credentials;
204 }
205  
206 ########################################################################
207 114 ▶sub _xml_element {
Subroutine _xml_element: called CC=2 SCAR=7.6
208 ########################################################################
209 114 my ( $xml, $tag ) = @_;
210  
211 114 ▶ if ( $xml =~ m{<$tag>([^<]+)</$tag>}xsm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xml =~ m[<$tag>([^<]+)</$tag>]msx) 114 0 114
212 114 return $1;
213 }
214  
215 0 return undef; ## no critic
216 }
217  
218 ########################################################################
219 1 ▶sub _extract_sts_error {
Subroutine _extract_sts_error: called CC=7 SCAR=23.2
220 ########################################################################
221 1 my ($xml) = @_;
222  
223 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
224 if !defined $xml || !length $xml;
225  
226 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
227  
228 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
229  
230 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
231 if !$code && !$message;
232  
233 1 return " - $code: $message";
234 }
235  
236 ########################################################################
237 2 ▶sub is_refreshable {
Subroutine is_refreshable: called CC=1 SCAR=0.0
Pod undocumented
238 ########################################################################
239 2 return 1;
240 }
241  
242 ########################################################################
243 6 ▶sub refresh_credentials {
Subroutine refresh_credentials: called CC=4 SCAR=13.9
Pod undocumented
244 ########################################################################
245 6 my ($self) = @_;
246  
247 6 my $source_provider = $self->get_source_provider;
248  
249 6 ▶ if ( $source_provider->is_refreshable ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source_provider->is_refreshable) 2 4 6
250 2 $source_provider->refresh_credentials;
251 }
252  
253 6 my $mfa_token;
254  
255 6 ▶ if ( defined $self->get_mfa_serial ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->get_mfa_serial) 2 4 6
256 croak 'MFA refresh requires AMAZON_CREDENTIALS_MFA_TOKEN'
257 2 ▶ if !defined $ENV{AMAZON_CREDENTIALS_MFA_TOKEN};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ENV{"AMAZON_CREDENTIALS_MFA_TOKEN"} 1 1 2
258  
259 1 $mfa_token = $ENV{AMAZON_CREDENTIALS_MFA_TOKEN};
260 }
261  
262 5 my $credentials = $self->_assume_role(
263 duration_seconds => $self->get_duration_seconds,
264 external_id => $self->get_external_id,
265 mfa_serial => $self->get_mfa_serial,
266 mfa_token => $mfa_token,
267 region => $self->get_region,
268 role_arn => $self->get_role_arn,
269 role_session_name => $self->get_role_session_name,
270 source_provider => $source_provider,
271 user_agent => $self->get_user_agent,
272 );
273  
274 5 $self->set_aws_access_key_id( $credentials->{AccessKeyId} );
275  
276 5 $self->set_aws_secret_access_key( $credentials->{SecretAccessKey} );
277  
278 5 $self->set_token( $credentials->{SessionToken} );
279  
280 5 $self->set_expiration( $credentials->{Expiration} );
281  
282 5 $self->set_last_refreshed(time);
283  
284 5 return $self;
285 }
286  
287 10 1;
« blib/lib/Amazon/Credentials.pm ↑ Summary blib/lib/Amazon/Credentials/Provider/InstanceRole.pm »