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

filter: Statementstmt 88.4% 123 / 139 Branchbran 58.7% 27 / 46 Conditioncond 32.2% 20 / 62 MC/DCmcdc 6.0% 2 / 33 Subroutinesub 94.7% 18 / 19 Podpod 25.0% 1 / 4 totaltotal 63.0% 191 / 303 CC 47 SCAR 29.9
CC
47
Cov
69%
_new_from_options
72.0
_new_from_profile
23.8
get_role_credentials
15.1
CRAP 19.8
↑ Summary blib/lib/Amazon/Credentials/Role/SSOCache.pm »
1 package Amazon::Credentials::Provider::SSO;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 ▶use parent qw(Amazon::Credentials::Provider);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 11 __PACKAGE__->follow_best_practice;
11  
12 11 __PACKAGE__->mk_accessors(
13 qw(
14 account_id
15 cache_key
16 profile
17 role_name
18 session_name
19 sso_region
20 sso_start_url
21 user_agent
22 )
23 );
24  
25 11 ▶use Amazon::Credentials::HTTP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 11 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 11 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 11 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 11 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 11 with 'Amazon::Credentials::Role::File';
33 11 with 'Amazon::Credentials::Role::SSOCache';
34  
35 11 ▶use constant { ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
36 11 DEFAULT_REGION => 'us-east-1',
37 11 };
38  
39 ########################################################################
40 12 ▶sub new {
Subroutine new: called CC=4 SCAR=16.4
Pod documented
41 ########################################################################
42 12 my ( $class, @args ) = @_;
43  
44 0 ▶ my $options = ref $args[0] ? { %{ $args[0] } } : {@args};
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 12 12
45  
46 return $class->_new_from_options($options)
47 12 ▶ if $options->{sso_account_id} || $options->{sso_role_name};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"sso_account_id"} or $options->{"sso_role_name"} 0 12 12
Condition33%1/3
$options->{"sso_account_id"} or $options->{"sso_role_name"}
l!l&&r!l&&!r
0012
MC/DC0%0/2
$options->{"sso_account_id"} or $options->{"sso_role_name"}
$options->{"sso_account_id"} $options->{"sso_role_name"}
Truth table33%1/3
A or B
A= $options->{"sso_account_id"}B= $options->{"sso_role_name"}
ABresult
1X1
011
000
48  
49 12 return $class->_new_from_profile($options);
50 }
51  
52 ########################################################################
53 0 ▶sub _new_from_options {
Subroutine _new_from_options: not called CC=8 SCAR=42.8
54 ########################################################################
55 0 my ( $class, $options ) = @_;
56  
57 0 my $account_id = $options->{sso_account_id};
58 0 my $role_name = $options->{sso_role_name};
59  
60 0 ▶ croak 'sso_account_id and sso_role_name are both required'
Branch: 1 branch
BranchTrueFalseTotal
unless $account_id and $role_name 0 0 0
Condition0%0/3
$account_id and $role_name
!ll&&!rl&&r
000
MC/DC0%0/2
$account_id and $role_name
$account_id $role_name
Truth table0%0/3
A and B
A= $account_idB= $role_name
ABresult
0X0
100
111
61 if !$account_id || !$role_name;
62  
63 0 ▶ my $sso_region = $options->{sso_region} || $options->{region} || DEFAULT_REGION;
Condition0%0/5
$options->{'sso_region'} || $options->{'region'} || 'us-east-1'
l!l
00
$options->{"sso_region"} or $options->{"region"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options->{'sso_region'} || $options->{'region'} || 'us-east-1'
$options->{"sso_region"} $options->{"region"}
Truth table0%0/3
A or B
A= $options->{"sso_region"}B= $options->{"region"}
ABresult
1X1
011
000
64  
65 0 ▶ my $ua = $options->{user_agent} || Amazon::Credentials::HTTP::UserAgent->new;
Condition0%0/3
$options->{'user_agent'} || 'Amazon::Credentials::HTTP::UserAgent'->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$options->{'user_agent'} || 'Amazon::Credentials::HTTP::UserAgent'->new
$options->{'user_agent'} 'Amazon::Credentials::HTTP::UserAgent'->new
Truth table0%0/3
A || B
A= $options->{'user_agent'}B= 'Amazon::Credentials::HTTP::UserAgent'->new
ABresult
1X1
011
000
66  
67 #
68 # Direct SSO credentials have no profile/session information from
69 # which to derive a cache key. Search the SSO cache and use the
70 # freshest usable access token.
71 #
72 0 my $access_token = $class->_get_access_token(
73 region => $sso_region,
74 user_agent => $ua,
75 );
76  
77 0 ▶ croak 'no valid SSO token in cache; run: aws sso login'
Branch: 1 branch
BranchTrueFalseTotal
unless $access_token 0 0 0
78 if !$access_token;
79  
80 0 my $credentials = $class->_get_role_credentials(
81 account_id => $account_id,
82 access_token => $access_token,
83 region => $sso_region,
84 role_name => $role_name,
85 user_agent => $ua,
86 );
87  
88 my $self = $class->SUPER::new(
89 aws_access_key_id => $credentials->{accessKeyId},
90 aws_secret_access_key => $credentials->{secretAccessKey},
91 expiration => _normalize_expiration( $credentials->{expiration} ),
92 region => $options->{region} || $sso_region,
93 source => 'sso',
94 token => $credentials->{sessionToken},
95 0 ▶ );
Condition0%0/3
$options->{'region'} || $sso_region
l!l&&r!l&&!r
000
MC/DC0%0/2
$options->{'region'} || $sso_region
$options->{'region'} $sso_region
Truth table0%0/3
A || B
A= $options->{'region'}B= $sso_region
ABresult
1X1
011
000
96  
97 0 $self->set_account_id($account_id);
98 0 $self->set_role_name($role_name);
99 0 $self->set_sso_region($sso_region);
100 0 $self->set_user_agent($ua);
101  
102 0 return $self;
103 }
104  
105 ########################################################################
106 12 ▶sub _new_from_profile {
Subroutine _new_from_profile: called CC=20 SCAR=31.7
107 ########################################################################
108 12 my ( $class, $options ) = @_;
109  
110 12 ▶ my $profile = $options->{profile} || 'default';
Condition50%1/2
$options->{'profile'} || 'default'
l!l
120
MC/DC0%0/1
$options->{'profile'} || 'default'
$options->{'profile'}
Truth table50%1/2
A
A= $options->{'profile'}
Aresult
11
00
111  
112 12 my $sections = $class->_get_profile_sections($profile);
113  
114 return
115 12 ▶ if !@{$sections};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$sections;} 0 12 12
116  
117 12 my $config = $class->_get_profile($profile);
118  
119 12 my $account_id = $config->{sso_account_id};
120 12 my $role_name = $config->{sso_role_name};
121  
122 return
123 12 ▶ if !$account_id && !$role_name && !$config->{sso_session} && !$config->{sso_start_url};
Branch: 1 branch
BranchTrueFalseTotal
unless $account_id or $role_name or $config->{"sso_session"} or $config->{"sso_start_url"} 1 11 12
Condition66%6/9
$account_id or $role_name or $config->{"sso_session"} or $config->{"sso_start_url"}
l!l&&r!l&&!r
1101
$account_id or $role_name or $config->{"sso_session"}
l!l&&r!l&&!r
1101
$account_id or $role_name
l!l&&r!l&&!r
1101
MC/DC25%1/4
$account_id or $role_name or $config->{"sso_session"} or $config->{"sso_start_url"}
$account_id $role_name $config->{"sso_session"} $config->{"sso_start_url"}
Truth table40%2/5
A or B or C or D
A= $account_idB= $role_nameC= $config->{"sso_session"}D= $config->{"sso_start_url"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
124  
125 11 ▶ croak "profile '$profile' contains incomplete SSO configuration"
Branch: 1 branch
BranchTrueFalseTotal
unless $account_id and $role_name 2 9 11
Condition66%2/3
$account_id and $role_name
!ll&&!rl&&r
029
MC/DC50%1/2
$account_id and $role_name
$account_id $role_name
Truth table66%2/3
A and B
A= $account_idB= $role_name
ABresult
0X0
100
111
126 if !$account_id || !$role_name;
127  
128 9 my ( $session_name, $sso_region, $sso_start_url, $cache_key );
129  
130 9 ▶ if ( $config->{sso_session} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'sso_session'}) { } 4 5 9
131 4 $session_name = $config->{sso_session};
132  
133 4 my $session;
134  
135 4 foreach my $entry ( @{$sections} ) {
136 4 $session = $entry->{ini}->{"sso-session $session_name"};
137  
138 last
139 4 ▶ if $session;
Branch: 1 branch
BranchTrueFalseTotal
if $session 3 1 4
140 }
141  
142 4 ▶ croak "no [sso-session $session_name] section for profile '$profile'"
Branch: 1 branch
BranchTrueFalseTotal
unless $session 1 3 4
143 if !$session;
144  
145 3 $sso_region = $session->{sso_region};
146 3 $sso_start_url = $session->{sso_start_url};
147 3 $cache_key = $class->_sso_cache_key($session_name);
148 }
149 else {
150 5 $sso_region = $config->{sso_region};
151 5 $sso_start_url = $config->{sso_start_url};
152 5 $cache_key = $class->_sso_cache_key($sso_start_url);
153 }
154  
155 8 ▶ $sso_region ||= $config->{region} || $options->{region} || DEFAULT_REGION;
Condition12%1/8
$sso_region ||= $config->{"region"} || $options->{"region"} || "us-east-1"
l!l&&r!l&&!r
800
$config->{'region'} || $options->{'region'} || 'us-east-1'
l!l
00
$config->{"region"} or $options->{"region"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$sso_region ||= $config->{"region"} || $options->{"region"} || "us-east-1"
$sso_region $config->{"region"} $options->{"region"}
Truth table0%0/4
A ||= B or C
A= $sso_regionB= $config->{"region"}C= $options->{"region"}
ABCresult
1XX1
01X1
0011
0000
156  
157 8 ▶ croak "profile '$profile' contains no SSO start URL"
Branch: 1 branch
BranchTrueFalseTotal
unless $sso_start_url 0 8 8
158 if !$sso_start_url;
159  
160 8 ▶ my $ua = $options->{user_agent} || Amazon::Credentials::HTTP::UserAgent->new;
Condition33%1/3
$options->{'user_agent'} || 'Amazon::Credentials::HTTP::UserAgent'->new
l!l&&r!l&&!r
800
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
161  
162 8 my $access_token = $class->_get_access_token(
163 cache_key => $cache_key,
164 region => $sso_region,
165 user_agent => $ua,
166 );
167  
168 8 ▶ croak 'no valid SSO token in cache; run: aws sso login'
Branch: 1 branch
BranchTrueFalseTotal
unless $access_token 0 8 8
169 if !$access_token;
170  
171 8 my $credentials = $class->_get_role_credentials(
172 account_id => $account_id,
173 access_token => $access_token,
174 region => $sso_region,
175 role_name => $role_name,
176 user_agent => $ua,
177 );
178  
179 my $self = $class->SUPER::new(
180 aws_access_key_id => $credentials->{accessKeyId},
181 aws_secret_access_key => $credentials->{secretAccessKey},
182 expiration => _normalize_expiration( $credentials->{expiration} ),
183 region => $config->{region} || $sso_region,
184 source => 'sso',
185 token => $credentials->{sessionToken},
186 7 ▶ );
Condition66%2/3
$config->{'region'} || $sso_region
l!l&&r!l&&!r
250
MC/DC0%0/2
$config->{'region'} || $sso_region
$config->{'region'} $sso_region
Truth table66%2/3
A || B
A= $config->{'region'}B= $sso_region
ABresult
1X1
011
000
187  
188 7 $self->set_account_id($account_id);
189 7 $self->set_cache_key($cache_key);
190 7 $self->set_profile($profile);
191 7 $self->set_role_name($role_name);
192 7 $self->set_session_name($session_name);
193 7 $self->set_sso_region($sso_region);
194 7 $self->set_sso_start_url($sso_start_url);
195 7 $self->set_user_agent($ua);
196  
197 7 return $self;
198 }
199  
200 ########################################################################
201 10 ▶sub _get_role_credentials {
Subroutine _get_role_credentials: called CC=6 SCAR=18.2
202 ########################################################################
203 10 my ( $self, @args ) = @_;
204  
205 10 ▶ my $options
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 10 10
206 = ref $args[0]
207 ? $args[0]
208 : {@args};
209  
210 my $host = sprintf
211 'portal.sso.%s.amazonaws.com',
212 10 $options->{region};
213  
214 my $url = sprintf
215 'https://%s/federation/credentials?account_id=%s&role_name=%s',
216 $host,
217 $options->{account_id},
218 10 $options->{role_name};
219  
220 10 my $req = HTTP::Request->new( GET => $url );
221  
222 10 $req->header( ':x-amz-sso_bearer_token', $options->{access_token} );
223  
224 10 my $rsp = $options->{user_agent}->request($req);
225  
226 10 ▶ croak "no response from get($url)"
Branch: 1 branch
BranchTrueFalseTotal
unless $rsp 0 10 10
227 if !$rsp;
228  
229 10 ▶ my $content = eval { return decode_json( $rsp->content ) if $rsp->content_type eq 'application/json'; };
Branch: 1 branch
BranchTrueFalseTotal
if $rsp->content_type eq "application/json" 10 0 10
230  
231 10 ▶ croak "could not decode response from GetRoleCredentials\n$EVAL_ERROR"
Branch: 1 branch
BranchTrueFalseTotal
unless $content 0 10 10
232 if !$content;
233  
234 return $content->{roleCredentials}
235 10 ▶ if $rsp->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $rsp->is_success 9 1 10
236  
237 croak sprintf
238 "Status: %s\n%s",
239 $rsp->status_line,
240 1 $content->{message};
241 }
242  
243 ########################################################################
244 1 ▶sub get_role_credentials {
Subroutine get_role_credentials: called CC=10 SCAR=27.2
Pod undocumented
245 ########################################################################
246 1 my ( $class, @args ) = @_;
247  
248 my $options
249 = ref $args[0]
250 1 ▶ ? { %{ $args[0] } }
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 1 0 1
251 : {@args};
252  
253 1 my $account_id = $options->{account_id};
254 1 my $role_name = $options->{role_name};
255  
256 1 ▶ croak 'account_id and role_name are both required'
Branch: 1 branch
BranchTrueFalseTotal
unless $account_id and $role_name 0 1 1
Condition33%1/3
$account_id and $role_name
!ll&&!rl&&r
001
MC/DC0%0/2
$account_id and $role_name
$account_id $role_name
Truth table33%1/3
A and B
A= $account_idB= $role_name
ABresult
0X0
100
111
257 if !$account_id || !$role_name;
258  
259 my $region
260 = $options->{region}
261 || $ENV{AWS_REGION}
262 || $ENV{AWS_DEFAULT_REGION}
263 1 ▶ || DEFAULT_REGION;
Condition37%3/8
$options->{'region'} || $ENV{'AWS_REGION'} || $ENV{'AWS_DEFAULT_REGION'} || 'us-east-1'
l!l
10
$options->{"region"} or $ENV{"AWS_REGION"} or $ENV{"AWS_DEFAULT_REGION"}
l!l&&r!l&&!r
100
$options->{"region"} or $ENV{"AWS_REGION"}
l!l&&r!l&&!r
100
MC/DC0%0/3
$options->{'region'} || $ENV{'AWS_REGION'} || $ENV{'AWS_DEFAULT_REGION'} || 'us-east-1'
$options->{"region"} $ENV{"AWS_REGION"} $ENV{"AWS_DEFAULT_REGION"}
Truth table25%1/4
A or B or C
A= $options->{"region"}B= $ENV{"AWS_REGION"}C= $ENV{"AWS_DEFAULT_REGION"}
ABCresult
1XX1
01X1
0011
0000
264  
265 my $ua = $options->{user_agent}
266 1 ▶ || Amazon::Credentials::HTTP::UserAgent->new;
Condition33%1/3
$options->{'user_agent'} || 'Amazon::Credentials::HTTP::UserAgent'->new
l!l&&r!l&&!r
010
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
267  
268 1 my $access_token = $options->{access_token};
269  
270 #
271 # The public compatibility API historically allowed callers to omit
272 # the access token. In that case use the freshest usable token from
273 # the AWS SSO cache.
274 #
275 1 ▶ if ( !$access_token ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($access_token) 1 0 1
276 1 $access_token = $class->_get_access_token(
277 region => $region,
278 user_agent => $ua,
279 );
280 }
281  
282 1 ▶ croak 'no access token'
Branch: 1 branch
BranchTrueFalseTotal
unless $access_token 0 1 1
283 if !$access_token;
284  
285 1 return $class->_get_role_credentials(
286 account_id => $account_id,
287 access_token => $access_token,
288 region => $region,
289 role_name => $role_name,
290 user_agent => $ua,
291 );
292 }
293  
294 ########################################################################
295 9 ▶sub _normalize_expiration {
Subroutine _normalize_expiration: called CC=3 SCAR=12.0
296 ########################################################################
297 9 my ($expiration) = @_;
298  
299 9 ▶ return $expiration
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expiration and $expiration =~ /\A\d+\z/msx 0 9 9
Condition33%1/3
defined $expiration and $expiration =~ /\A\d+\z/msx
!ll&&!rl&&r
009
MC/DC0%0/2
defined $expiration and $expiration =~ /\A\d+\z/msx
defined $expiration $expiration =~ /\A\d+\z/msx
Truth table33%1/3
A and B
A= defined $expirationB= $expiration =~ /\A\d+\z/msx
ABresult
0X0
100
111
300 if !defined $expiration
301 || $expiration !~ /\A\d+\z/xsm;
302  
303 9 my @g = gmtime int $expiration / 1000;
304  
305 9 return sprintf
306 '%04d-%02d-%02dT%02d:%02d:%02dZ',
307 $g[5] + 1900,
308 $g[4] + 1,
309 @g[ 3, 2, 1, 0 ];
310 }
311  
312 ########################################################################
313 1 ▶sub is_refreshable {
Subroutine is_refreshable: called CC=1 SCAR=0.0
Pod undocumented
314 ########################################################################
315 1 return 1;
316 }
317  
318 ########################################################################
319 2 ▶sub refresh_credentials {
Subroutine refresh_credentials: called CC=2 SCAR=6.9
Pod undocumented
320 ########################################################################
321 2 my ($self) = @_;
322  
323 2 my $access_token = $self->_get_access_token(
324 cache_key => $self->get_cache_key,
325 region => $self->get_sso_region,
326 user_agent => $self->get_user_agent,
327 );
328  
329 2 ▶ croak 'no valid SSO token in cache; run: aws sso login'
Branch: 1 branch
BranchTrueFalseTotal
unless $access_token 0 2 2
330 if !$access_token;
331  
332 2 my $credentials = $self->_get_role_credentials(
333 account_id => $self->get_account_id,
334 access_token => $access_token,
335 region => $self->get_sso_region,
336 role_name => $self->get_role_name,
337 user_agent => $self->get_user_agent,
338 );
339  
340 2 $self->set_aws_access_key_id( $credentials->{accessKeyId} );
341  
342 2 $self->set_aws_secret_access_key( $credentials->{secretAccessKey} );
343  
344 2 $self->set_token( $credentials->{sessionToken} );
345  
346 2 $self->set_expiration( _normalize_expiration( $credentials->{expiration} ) );
347  
348 2 $self->set_last_refreshed(time);
349  
350 2 return $self;
351 }
352  
353 11 1;
↑ Summary blib/lib/Amazon/Credentials/Role/SSOCache.pm »