blib/lib/Amazon/Credentials/Role/SSOCache.pm

filter: Statementstmt 100% 123 / 123 Branchbran 60.0% 30 / 50 Conditioncond 41.6% 15 / 36 MC/DCmcdc 4.3% 1 / 23 Subroutinesub 100% 17 / 17 Podpod n/a totaltotal 74.7% 186 / 249 CC 40 SCAR 24.7
CC
40
Cov
80%
_refresh_sso_access_token
19.3
_get_access_token
17.7
_write_sso_cache_file
8.2
CRAP 11.8
« blib/lib/Amazon/Credentials/Provider/SSO.pm ↑ Summary blib/lib/Amazon/Credentials/Resolver/Profile.pm »
1 package Amazon::Credentials::Role::SSOCache;
2  
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 12 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 12 ▶use Amazon::Credentials::HTTP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 ▶use Time::Local qw(timegm_modern);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 12 ▶use constant { ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
18 12 CACHE_DIR => '.aws/sso/cache',
19 SSO_OIDC_HOSTNAME => 'oidc.%s.amazonaws.com',
20 SSO_OIDC_TOKEN_URI => 'token',
21 12 };
22  
23 ########################################################################
24 12 ▶sub _sso_cache_key {
Subroutine _sso_cache_key: called CC=3 SCAR=12.0
25 ########################################################################
26 12 my ( $self, $value ) = @_;
27  
28 return
29 12 ▶ if !defined $value || !length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 0 12 12
Condition33%1/3
defined $value and length $value
!ll&&!rl&&r
0012
MC/DC0%0/2
defined $value and length $value
defined $value length $value
Truth table33%1/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
30  
31 12 require Digest::SHA;
32  
33 12 return Digest::SHA::sha1_hex($value);
34 }
35  
36 ########################################################################
37 15 ▶sub _get_access_token {
Subroutine _get_access_token: called CC=16 SCAR=28.7
38 ########################################################################
39 15 my ( $self, @args ) = @_;
40  
41 15 ▶ my $options
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 15 15
42 = ref $args[0]
43 ? $args[0]
44 : {@args};
45  
46 15 ▶ my $home = $options->{home} // $ENV{HOME};
Condition66%2/3
$options->{"home"} // $ENV{"HOME"}
l!l&&r!l&&!r
5100
MC/DC0%0/2
$options->{"home"} // $ENV{"HOME"}
$options->{"home"} $ENV{"HOME"}
Truth table66%2/3
A // B
A= $options->{"home"}B= $ENV{"HOME"}
ABresult
1X1
011
000
47  
48 15 my $cache_key = $options->{cache_key};
49  
50 15 my $region = $options->{region};
51  
52 15 ▶ my $user_agent = $options->{user_agent} // Amazon::Credentials::HTTP::UserAgent->new;
Condition66%2/3
$options->{"user_agent"} // "Amazon::Credentials::HTTP::UserAgent"->new
l!l&&r!l&&!r
1230
MC/DC0%0/2
$options->{"user_agent"} // "Amazon::Credentials::HTTP::UserAgent"->new
$options->{"user_agent"} "Amazon::Credentials::HTTP::UserAgent"->new
Truth table66%2/3
A // B
A= $options->{"user_agent"}B= "Amazon::Credentials::HTTP::UserAgent"->new
ABresult
1X1
011
000
53  
54 15 my $cache_dir = sprintf
55 '%s/%s',
56 $home,
57 CACHE_DIR;
58  
59 15 ▶ croak "no $cache_dir found"
Branch: 1 branch
BranchTrueFalseTotal
unless -d $cache_dir 1 14 15
60 if !-d $cache_dir;
61  
62 14 my @json_files;
63  
64 14 ▶ if ( defined $cache_key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cache_key) { } 13 1 14
65 13 @json_files = ( sprintf '%s/%s.json', $cache_dir, $cache_key );
66 }
67 else {
68 1 ▶ opendir my $dh, $cache_dir
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $cache_dir or croak("could not open ${cache_dir}: $Amazon::Credentials::Role::SSOCache::OS_ERROR") 1 0 1
69 or croak "could not open $cache_dir: $OS_ERROR";
70  
71 2 @json_files = map { sprintf '%s/%s', $cache_dir, $_ }
72 1 grep {/[.]json\z/xsm} readdir $dh;
73  
74 1 closedir $dh;
75 }
76  
77 14 my @candidates;
78  
79 14 foreach my $file (@json_files) {
80 15 ▶ open my $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or next 15 0 15
81 or next;
82  
83 15 my $content;
84  
85 {
86 15 local $RS = undef;
87 15 $content = <$fh>;
88 }
89  
90 15 close $fh;
91  
92 15 my $json = eval { return decode_json($content); };
93  
94 next
95 15 ▶ if !ref $json || !$json->{accessToken};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $json and $json->{"accessToken"} 0 15 15
Condition33%1/3
ref $json and $json->{"accessToken"}
!ll&&!rl&&r
0015
MC/DC0%0/2
ref $json and $json->{"accessToken"}
ref $json $json->{"accessToken"}
Truth table33%1/3
A and B
A= ref $jsonB= $json->{"accessToken"}
ABresult
0X0
100
111
96  
97 15 my $expires_at = $self->_parse_expires_at( $json->{expiresAt} );
98  
99 next
100 15 ▶ if !defined $expires_at;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expires_at 0 15 15
101  
102 push @candidates,
103 {
104 data => $json,
105 expires_at => $expires_at,
106 file => $file,
107 token => $json->{accessToken},
108 15 };
109 }
110  
111 return
112 14 ▶ if !@candidates;
Branch: 1 branch
BranchTrueFalseTotal
unless @candidates 0 14 14
113  
114 14 @candidates = reverse sort { $a->{expires_at} <=> $b->{expires_at} } @candidates;
115  
116 14 my $candidate = $candidates[0];
117  
118 return $candidate->{token}
119 14 ▶ if $candidate->{expires_at} > time;
Branch: 1 branch
BranchTrueFalseTotal
if $candidate->{"expires_at"} > time 11 3 14
120  
121 return
122 3 ▶ if !$region;
Branch: 1 branch
BranchTrueFalseTotal
unless $region 0 3 3
123  
124 return $self->_refresh_sso_access_token(
125 cache_file => $candidate->{file},
126 data => $candidate->{data},
127 3 ▶ region => $candidate->{data}{region} || $region,
Condition33%1/3
$candidate->{'data'}{'region'} || $region
l!l&&r!l&&!r
030
MC/DC0%0/2
$candidate->{'data'}{'region'} || $region
$candidate->{'data'}{'region'} $region
Truth table33%1/3
A || B
A= $candidate->{'data'}{'region'}B= $region
ABresult
1X1
011
000
128 user_agent => $user_agent,
129 );
130 }
131  
132 ########################################################################
133 3 ▶sub _refresh_sso_access_token {
Subroutine _refresh_sso_access_token: called CC=15 SCAR=29.6
134 ########################################################################
135 3 my ( $self, @args ) = @_;
136  
137 3 ▶ my $options
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 3 3
138 = ref $args[0]
139 ? $args[0]
140 : {@args};
141  
142 3 my $cache_file = $options->{cache_file};
143  
144 3 my $token = $options->{data};
145  
146 3 my $region = $options->{region};
147  
148 3 ▶ 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
300
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
149  
150 return
151 3 ▶ if !ref $token;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $token 0 3 3
152  
153 3 foreach my $key (
154 qw(
155 clientId
156 clientSecret
157 refreshToken
158 )
159 ) {
160 return
161 if !defined $token->{$key}
162 9 ▶ || !length $token->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token->{$key} and length $token->{$key} 0 9 9
Condition33%1/3
defined $token->{$key} and length $token->{$key}
!ll&&!rl&&r
009
MC/DC0%0/2
defined $token->{$key} and length $token->{$key}
defined $token->{$key} length $token->{$key}
Truth table33%1/3
A and B
A= defined $token->{$key}B= length $token->{$key}
ABresult
0X0
100
111
163 }
164  
165 3 my $registration_expires_at = $self->_parse_expires_at( $token->{registrationExpiresAt} );
166  
167 return
168 3 ▶ if defined $registration_expires_at
Branch: 1 branch
BranchTrueFalseTotal
if defined $registration_expires_at and $registration_expires_at <= time 1 2 3
Condition66%2/3
defined $registration_expires_at and $registration_expires_at <= time
!ll&&!rl&&r
021
MC/DC50%1/2
defined $registration_expires_at and $registration_expires_at <= time
defined $registration_expires_at $registration_expires_at <= time
Truth table66%2/3
A and B
A= defined $registration_expires_atB= $registration_expires_at <= time
ABresult
0X0
100
111
169 && $registration_expires_at <= time;
170  
171 2 my $host = sprintf
172 SSO_OIDC_HOSTNAME,
173 $region;
174  
175 2 my $url = sprintf
176 'https://%s/%s',
177 $host,
178 SSO_OIDC_TOKEN_URI;
179  
180 my $content = encode_json(
181 { clientId => $token->{clientId},
182 clientSecret => $token->{clientSecret},
183 grantType => 'refresh_token',
184 refreshToken => $token->{refreshToken},
185 }
186 2 );
187  
188 2 my $req = HTTP::Request->new( POST => $url );
189  
190 2 $req->header( 'Content-Type', 'application/json' );
191  
192 2 $req->content($content);
193  
194 2 my $rsp = $ua->request($req);
195  
196 return
197 2 ▶ if !$rsp || !$rsp->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $rsp and $rsp->is_success 0 2 2
Condition33%1/3
$rsp and $rsp->is_success
!ll&&!rl&&r
002
MC/DC0%0/2
$rsp and $rsp->is_success
$rsp $rsp->is_success
Truth table33%1/3
A and B
A= $rspB= $rsp->is_success
ABresult
0X0
100
111
198  
199 2 my $refreshed = eval { return decode_json( $rsp->content ); };
200  
201 return
202 if !ref $refreshed
203 || !$refreshed->{accessToken}
204 2 ▶ || !$refreshed->{expiresIn};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $refreshed and $refreshed->{"accessToken"} and $refreshed->{"expiresIn"} 0 2 2
Condition33%2/6
ref $refreshed and $refreshed->{"accessToken"} and $refreshed->{"expiresIn"}
!ll&&!rl&&r
002
ref $refreshed and $refreshed->{"accessToken"}
!ll&&!rl&&r
002
MC/DC0%0/3
ref $refreshed and $refreshed->{"accessToken"} and $refreshed->{"expiresIn"}
ref $refreshed $refreshed->{"accessToken"} $refreshed->{"expiresIn"}
Truth table25%1/4
A and B and C
A= ref $refreshedB= $refreshed->{"accessToken"}C= $refreshed->{"expiresIn"}
ABCresult
0XX0
10X0
1100
1111
205  
206 2 $token->{accessToken} = $refreshed->{accessToken};
207  
208 2 $token->{expiresAt} = $self->_iso8601_utc( time + $refreshed->{expiresIn} );
209  
210 2 ▶ if ( $refreshed->{refreshToken} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refreshed->{"refreshToken"}) 1 1 2
211 1 $token->{refreshToken} = $refreshed->{refreshToken};
212 }
213  
214 2 $self->_write_sso_cache_file( $cache_file, $token );
215  
216 2 return $token->{accessToken};
217 }
218  
219 ########################################################################
220 2 ▶sub _write_sso_cache_file {
Subroutine _write_sso_cache_file: called CC=7 SCAR=21.1
221 ########################################################################
222 2 my ( $self, $cache_file, $token ) = @_;
223  
224 return
225 2 ▶ if !$cache_file || !ref $token;
Branch: 1 branch
BranchTrueFalseTotal
unless $cache_file and ref $token 0 2 2
Condition33%1/3
$cache_file and ref $token
!ll&&!rl&&r
002
MC/DC0%0/2
$cache_file and ref $token
$cache_file ref $token
Truth table33%1/3
A and B
A= $cache_fileB= ref $token
ABresult
0X0
100
111
226  
227 2 my $tmp_file = sprintf
228 '%s.%d.tmp',
229 $cache_file,
230 $PROCESS_ID;
231  
232 2 ▶ open my $fh, '>', $tmp_file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $tmp_file or croak("could not open ${tmp_file}: $Amazon::Credentials::Role::SSOCache::OS_ERROR") 2 0 2
233 or croak "could not open $tmp_file: $OS_ERROR";
234  
235 2 print {$fh} encode_json($token);
236  
237 2 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("could not close ${tmp_file}: $Amazon::Credentials::Role::SSOCache::OS_ERROR") 2 0 2
238 or croak "could not close $tmp_file: $OS_ERROR";
239  
240 2 ▶ if ( my @stat = stat $cache_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@stat) = stat $cache_file) 2 0 2
241 2 chmod $stat[2] & oct('7777'), $tmp_file;
242 }
243  
244 2 ▶ rename $tmp_file, $cache_file
Branch: 1 branch
BranchTrueFalseTotal
rename $tmp_file, $cache_file or croak("could not rename $tmp_file to ${cache_file}: $Amazon::Credentials::Role::SSOCache::OS_ERROR") 2 0 2
245 or croak "could not rename $tmp_file to $cache_file: $OS_ERROR";
246  
247 2 return 1;
248 }
249  
250 ########################################################################
251 2 ▶sub _iso8601_utc {
Subroutine _iso8601_utc: called CC=1 SCAR=0.0
252 ########################################################################
253 2 my ( $self, $epoch ) = @_;
254  
255 2 my @g = gmtime $epoch;
256  
257 2 return sprintf
258 '%04d-%02d-%02dT%02d:%02d:%02dZ',
259 $g[5] + 1900,
260 $g[4] + 1,
261 @g[ 3, 2, 1, 0 ];
262 }
263  
264 ########################################################################
265 18 ▶sub _parse_expires_at {
Subroutine _parse_expires_at: called CC=3 SCAR=11.2
266 ########################################################################
267 18 my ( $self, $expires_at ) = @_;
268  
269 return
270 18 ▶ if !defined $expires_at;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expires_at 0 18 18
271  
272 18 my @time_parts = $expires_at =~ /\A
273 (\d{4})-(\d{2})-(\d{2})
274 [T[:space:]]
275 (\d{2}):(\d{2}):(\d{2})
276 (?:[.]\d+)?
277 (?:Z|UTC|[+]00:?00)?
278 \z/xsm;
279  
280 return
281 18 ▶ if !@time_parts;
Branch: 1 branch
BranchTrueFalseTotal
unless @time_parts 0 18 18
282  
283 18 $time_parts[1] -= 1;
284  
285 18 return scalar eval { timegm_modern( reverse @time_parts ); };
286 }
287  
288 12 1;
« blib/lib/Amazon/Credentials/Provider/SSO.pm ↑ Summary blib/lib/Amazon/Credentials/Resolver/Profile.pm »