lib/VM/EC2/Security/CredentialCache.pm

filter: Statementstmt 51.7% 15 / 29 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 80.0% 4 / 5 Podpod 0.0% 0 / 1 totaltotal 41.3% 19 / 46 CC 6 SCAR 32.4
CC
6
Cov
38%
get
42.0
BEGIN
1.0
BEGIN
1.0
CRAP 25.6
↑ Summary
1 package VM::EC2::Security::CredentialCache;
2 1 $VM::EC2::Security::CredentialCache::VERSION = '0.25';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use DateTime::Format::ISO8601;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use VM::EC2::Instance::Metadata;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 VM::EC2::Security::CredentialCache -- Cache credentials respecting expiration time for IAM roles.
11  
12 =head1 SYNOPSIS
13  
14 Retrieves the current EC2 instance's IAM credentials and caches them until they expire.
15  
16 use VM::EC2::Security::CredentialCache;
17  
18 # return a VM::EC2::Security::Credentials if available undef otherwise.
19 my $credentials = VM::EC2::Security::CredentialCache->get();
20  
21 =head1 DESCRIPTION
22  
23 This module provides a cache for an EC2's IAM credentials represented by L<VM::EC2::Security::Credentials>.
24 Rather than retriving the credentials for every possible call that uses them, cache them until they
25 expire and retreive them again if they have expired.
26  
27 =cut
28  
29 1 my $credentials;
30 my $credential_expiration_dt;
31  
32 0 ▶sub get {
Subroutine get: not called CC=6 SCAR=37.4
Pod undocumented
33 0 my ($self, $now) = @_;
34 0 ▶ if (!defined($credentials)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $credentials) 0 0 0
35 0 my $meta = VM::EC2::Instance::Metadata->new;
36 0 ▶ defined($meta) || die("Unable to retrieve instance metadata");
Branch: 1 branch
BranchTrueFalseTotal
defined $meta or die "Unable to retrieve instance metadata" 0 0 0
37 0 $credentials= $meta->iam_credentials;
38 0 ▶ defined($credentials) || die("No IAM credentials retrieved from instance metadata");
Branch: 1 branch
BranchTrueFalseTotal
defined $credentials or die "No IAM credentials retrieved from instance metadata" 0 0 0
39 0 $credential_expiration_dt = DateTime::Format::ISO8601->parse_datetime($credentials->expiration())->epoch();
40 0 return $credentials;
41 }
42  
43 # AWS provides new credentials atleast 5 minutes before the expiration of the old
44 # credentials, but we'll only start looking at 4 minutes
45 0 ▶ $now //= time;
Condition0%0/2
$now //= time
l!l
00
MC/DC0%0/1
$now //= time
$now
Truth table0%0/2
A
A= $now
Aresult
11
00
46 0 ▶ if ($credential_expiration_dt - $now > 240) {
Branch: 1 branch
BranchTrueFalseTotal
if ($credential_expiration_dt - $now > 240) 0 0 0
47 0 return $credentials;
48 }
49
50 # These credentials are good for only 4 minutes or less, so clear them and attempt to
51 # retrieve new credentials.
52 0 $credentials = undef;
53 0 $credential_expiration_dt = undef;
54 0 return get();
55 }
56  
57 1 1;
↑ Summary