« 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$relative_uri or $full_uri
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 3 | ▶ | return ( 'full', $full_uri ) if $full_uri; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 8 | }; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 8 | ▶ | if ( $container_type eq 'full' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $token and length $token
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 107 | if !$rsp; | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 8 | ▶ | croak sprintf 'container credential request failed: %s', $rsp->status_line | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$Amazon::Credentials::Provider::Container::EVAL_ERROR or not ref $credentials
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
| |||||||||||||||||||||||||||||
| 115 | if $EVAL_ERROR || !ref $credentials; | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | croak 'container credential response contains no AccessKeyId' | ||||||||||||||||||||||||||||
| 118 | 8 | ▶ | if !$credentials->{AccessKeyId}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | croak 'container credential response contains no SecretAccessKey' | ||||||||||||||||||||||||||||
| 121 | 8 | ▶ | if !$credentials->{SecretAccessKey}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $token_file and length $token_file
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
| |||||||||||||||||||||||||||||
| 132 | if !defined $token_file || !length $token_file; | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 1 | ▶ | open my $fh, '<', $token_file | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 143 | or croak "cannot close token file '$token_file': $OS_ERROR"; | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 1 | ▶ | chomp $token | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||