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

filter: Statementstmt 98.9% 92 / 93 Branchbran 70.5% 24 / 34 Conditioncond 60.0% 9 / 15 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 100% 18 / 18 Podpod 33.3% 1 / 3 totaltotal 85.5% 148 / 173 CC 23 SCAR 16.8
CC
23
Cov
88%
_get_credentials
11.6
_get_authorization_token
6.3
new
6.0
CRAP 5.4
« blib/lib/Amazon/Credentials/Provider/Env.pm ↑ Summary blib/lib/Amazon/Credentials/Provider/Process.pm »
1 package Amazon::Credentials::Provider::Container;
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 authorization_token
15 authorization_token_file
16 container_type
17 credential_uri
18 user_agent
19 )
20 );
21  
22 10 ▶use Amazon::Credentials::HTTP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 10 ▶use Amazon::Credentials::Utils qw(choose);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 10 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 10 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 10 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 10 ▶use constant { ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
30 10 AWS_CONTAINER_CREDENTIALS_URL => q{http://169.254.170.2},
31 10 };
32  
33 ########################################################################
34 9 ▶sub new {
Subroutine new: called CC=6 SCAR=18.0
Pod documented
35 ########################################################################
36 9 my ( $class, @args ) = @_;
37  
38 9 ▶ my $options = ref $args[0] ? { %{ $args[0] } } : {@args};
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 1 8 9
39  
40 9 my $relative_uri = $ENV{AWS_CONTAINER_CREDENTIALS_RELATIVE_URI};
41 9 my $full_uri = $ENV{AWS_CONTAINER_CREDENTIALS_FULL_URI};
42  
43 return
44 9 ▶ if !$relative_uri && !$full_uri;
Branch: 1 branch
BranchTrueFalseTotal
unless $relative_uri or $full_uri 1 8 9
Condition100%3/3
$relative_uri or $full_uri
l!l&&r!l&&!r
531
MC/DC100%2/2
$relative_uri or $full_uri
$relative_uri $full_uri
Truth table100%3/3
A or B
A= $relative_uriB= $full_uri
ABresult
1X1
011
000
45  
46 8 ▶ my ( $container_type, $credential_uri ) = choose {
Subroutine __ANON__: called CC=3 SCAR=11.1
47 8 ▶ return ( 'relative', AWS_CONTAINER_CREDENTIALS_URL . $relative_uri ) if $relative_uri;
Branch: 1 branch
BranchTrueFalseTotal
if $relative_uri 5 3 8
48  
49 3 ▶ return ( 'full', $full_uri ) if $full_uri;
Branch: 1 branch
BranchTrueFalseTotal
if $full_uri 3 0 3
50 8 };
51  
52 8 ▶ if ( $container_type eq 'full' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($container_type eq "full") 3 5 8
53 3 _validate_full_uri($credential_uri);
54 }
55  
56 7 ▶ 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
700
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 my $credentials = $class->_get_credentials(
59 credential_uri => $credential_uri,
60 authorization_token => $ENV{AWS_CONTAINER_AUTHORIZATION_TOKEN},
61 authorization_token_file => $ENV{AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE},
62 7 user_agent => $ua,
63 );
64  
65 my $self = $class->SUPER::new(
66 aws_access_key_id => $credentials->{AccessKeyId},
67 aws_secret_access_key => $credentials->{SecretAccessKey},
68 expiration => $credentials->{Expiration},
69 source => 'container',
70 token => $credentials->{Token},
71 7 );
72  
73 7 $self->set_authorization_token( $ENV{AWS_CONTAINER_AUTHORIZATION_TOKEN} );
74  
75 7 $self->set_authorization_token_file( $ENV{AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE} );
76  
77 7 $self->set_container_type($container_type);
78 7 $self->set_credential_uri($credential_uri);
79 7 $self->set_user_agent($ua);
80  
81 7 return $self;
82 }
83  
84 ########################################################################
85 8 ▶sub _get_credentials {
Subroutine _get_credentials: called CC=10 SCAR=24.5
86 ########################################################################
87 8 my ( $self, @args ) = @_;
88  
89 8 ▶ my $options
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 8 8
90 = ref $args[0]
91 ? $args[0]
92 : {@args};
93  
94 8 my $token = _get_authorization_token( $options->{authorization_token}, $options->{authorization_token_file}, );
95  
96 8 my $req = HTTP::Request->new( GET => $options->{credential_uri} );
97  
98 8 $req->header( Accept => '*/*' );
99  
100 8 ▶ if ( defined $token && length $token ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $token and length $token) 2 6 8
Condition66%2/3
defined $token and length $token
!ll&&!rl&&r
602
MC/DC50%1/2
defined $token and length $token
defined $token length $token
Truth table66%2/3
A and B
A= defined $tokenB= length $token
ABresult
0X0
100
111
101 2 $req->header( Authorization => $token );
102 }
103  
104 8 my $rsp = $options->{user_agent}->request($req);
105  
106 8 ▶ croak 'no response from container credential endpoint'
Branch: 1 branch
BranchTrueFalseTotal
unless $rsp 0 8 8
107 if !$rsp;
108  
109 8 ▶ croak sprintf 'container credential request failed: %s', $rsp->status_line
Branch: 1 branch
BranchTrueFalseTotal
unless $rsp->is_success 0 8 8
110 if !$rsp->is_success;
111  
112 8 my $credentials = eval { return decode_json( $rsp->content ); };
113  
114 8 ▶ croak "could not decode container credentials\n$EVAL_ERROR"
Branch: 1 branch
BranchTrueFalseTotal
if $Amazon::Credentials::Provider::Container::EVAL_ERROR or not ref $credentials 0 8 8
Condition33%1/3
$Amazon::Credentials::Provider::Container::EVAL_ERROR or not ref $credentials
l!l&&r!l&&!r
008
MC/DC0%0/2
$Amazon::Credentials::Provider::Container::EVAL_ERROR or not ref $credentials
$Amazon::Credentials::Provider::Container::EVAL_ERROR not ref $credentials
Truth table33%1/3
A or not(B)
A= $Amazon::Credentials::Provider::Container::EVAL_ERRORB= not ref $credentials
ABresult
1X1
011
000
115 if $EVAL_ERROR || !ref $credentials;
116  
117 croak 'container credential response contains no AccessKeyId'
118 8 ▶ if !$credentials->{AccessKeyId};
Branch: 1 branch
BranchTrueFalseTotal
unless $credentials->{"AccessKeyId"} 0 8 8
119  
120 croak 'container credential response contains no SecretAccessKey'
121 8 ▶ if !$credentials->{SecretAccessKey};
Branch: 1 branch
BranchTrueFalseTotal
unless $credentials->{"SecretAccessKey"} 0 8 8
122  
123 8 return $credentials;
124 }
125  
126 ########################################################################
127 8 ▶sub _get_authorization_token {
Subroutine _get_authorization_token: called CC=6 SCAR=18.4
128 ########################################################################
129 8 my ( $token, $token_file ) = @_;
130  
131 8 ▶ return $token
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token_file and length $token_file 7 1 8
Condition66%2/3
defined $token_file and length $token_file
!ll&&!rl&&r
701
MC/DC50%1/2
defined $token_file and length $token_file
defined $token_file length $token_file
Truth table66%2/3
A and B
A= defined $token_fileB= length $token_file
ABresult
0X0
100
111
132 if !defined $token_file || !length $token_file;
133  
134 1 ▶ open my $fh, '<', $token_file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $token_file or croak("cannot open token file '${token_file}': $Amazon::Credentials::Provider::Container::OS_ERROR") 1 0 1
135 or croak "cannot open token file '$token_file': $OS_ERROR";
136  
137 {
138 1 local $RS = undef;
139 1 $token = <$fh>;
140 }
141  
142 1 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("cannot close token file '${token_file}': $Amazon::Credentials::Provider::Container::OS_ERROR") 1 0 1
143 or croak "cannot close token file '$token_file': $OS_ERROR";
144  
145 1 ▶ chomp $token
Branch: 1 branch
BranchTrueFalseTotal
if defined $token 1 0 1
146 if defined $token;
147  
148 1 return $token;
149 }
150  
151 ########################################################################
152 3 ▶sub _validate_full_uri {
Subroutine _validate_full_uri: called CC=2 SCAR=7.0
153 ########################################################################
154 3 my ($url) = @_;
155  
156 return
157 3 ▶ if $url =~ m{
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ m( \A (?: https:// | http://127[.] | http://\[::1\] | http://169[.]254[.]170[.]23 ) )msx 2 1 3
158 \A
159 (?: https://
160 | http://127[.]
161 | http://\[::1\]
162 | http://169[.]254[.]170[.]23
163 )
164 }xsm;
165  
166 1 croak 'AWS_CONTAINER_CREDENTIALS_FULL_URI must be https://, ' . 'http://127.x.x.x, http://[::1], or http://169.254.170.23';
167  
168 0 return;
169 }
170  
171 ########################################################################
172 1 ▶sub is_refreshable {
Subroutine is_refreshable: called CC=1 SCAR=0.0
Pod undocumented
173 ########################################################################
174 1 return 1;
175 }
176  
177 ########################################################################
178 1 ▶sub refresh_credentials {
Subroutine refresh_credentials: called CC=1 SCAR=0.0
Pod undocumented
179 ########################################################################
180 1 my ($self) = @_;
181  
182 1 my $credentials = $self->_get_credentials(
183 credential_uri => $self->get_credential_uri,
184 authorization_token => $self->get_authorization_token,
185 authorization_token_file => $self->get_authorization_token_file,
186 user_agent => $self->get_user_agent,
187 );
188  
189 1 $self->set_aws_access_key_id( $credentials->{AccessKeyId} );
190  
191 1 $self->set_aws_secret_access_key( $credentials->{SecretAccessKey} );
192  
193 1 $self->set_token( $credentials->{Token} );
194  
195 1 $self->set_expiration( $credentials->{Expiration} );
196  
197 1 $self->set_last_refreshed(time);
198  
199 1 return $self;
200 }
201  
202 10 1;
« blib/lib/Amazon/Credentials/Provider/Env.pm ↑ Summary blib/lib/Amazon/Credentials/Provider/Process.pm »