| 1 | package Paws::Credential::InstanceProfileV2; | ||||||||||
| 2 | 1 | ▶ | use JSON::MaybeXS; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use DateTime::Format::ISO8601; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | with 'Paws::Credential'; | |||||||||
| 6 | |||||||||||
| 7 | 1 | has metadata_url => ( | |||||||||
| 8 | is => 'ro', | ||||||||||
| 9 | isa => 'Str', | ||||||||||
| 10 | default => 'http://169.254.169.254/latest/meta-data/iam/security-credentials/' | ||||||||||
| 11 | ); | ||||||||||
| 12 | |||||||||||
| 13 | 1 | has token_url => ( | |||||||||
| 14 | is => 'ro', | ||||||||||
| 15 | isa => 'Str', | ||||||||||
| 16 | default => 'http://169.254.169.254/latest/api/token' | ||||||||||
| 17 | ); | ||||||||||
| 18 | |||||||||||
| 19 | 1 | has timeout => (is => 'ro', isa => 'Int', default => 1); | |||||||||
| 20 | |||||||||||
| 21 | has ua => ( | ||||||||||
| 22 | is => 'ro', | ||||||||||
| 23 | lazy => 1, | ||||||||||
| 24 | 0 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.6
| |||||||||||
| 25 | 0 | my $self = shift; | |||||||||
| 26 | 1 | ▶ | use HTTP::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 0 | HTTP::Tiny->new( | |||||||||
| 28 | agent => 'AWS Perl SDK', | ||||||||||
| 29 | timeout => $self->timeout, | ||||||||||
| 30 | ); | ||||||||||
| 31 | } | ||||||||||
| 32 | 1 | ); | |||||||||
| 33 | |||||||||||
| 34 | has expiration => ( | ||||||||||
| 35 | is => 'rw', | ||||||||||
| 36 | isa => 'Int', | ||||||||||
| 37 | 2 | ▶ | default => sub { 0 } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 1 | ); | |||||||||
| 39 | |||||||||||
| 40 | 2 | ▶ | has actual_creds => (is => 'rw', default => sub { {} }); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 41 | |||||||||||
| 42 | 4 | ▶ | sub access_key { | ||||||||
|
Subroutine
access_key: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 43 | 4 | my $self = shift; | |||||||||
| 44 | 4 | $self->_refresh; | |||||||||
| 45 | 3 | $self->actual_creds->{AccessKeyId}; | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 2 | ▶ | sub secret_key { | ||||||||
|
Subroutine
secret_key: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 49 | 2 | my $self = shift; | |||||||||
| 50 | 2 | $self->_refresh; | |||||||||
| 51 | 2 | $self->actual_creds->{SecretAccessKey}; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 2 | ▶ | sub session_token { | ||||||||
|
Subroutine
session_token: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 55 | 2 | my $self = shift; | |||||||||
| 56 | 2 | $self->_refresh; | |||||||||
| 57 | 2 | $self->actual_creds->{Token}; | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | #TODO: Raise exceptions if HTTP get didn't return success | ||||||||||
| 61 | 8 | ▶ | sub _refresh { | ||||||||
|
Subroutine
_refresh: called CC=8 SCAR=20.9
| |||||||||||
| 62 | 8 | my $self = shift; | |||||||||
| 63 | |||||||||||
| 64 | 8 | ▶ | return if $self->expiration - 240 >= time; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | |||||||||||
| 66 | 4 | my $ua = $self->ua; | |||||||||
| 67 | 4 | my $r = $ua->put( $self->token_url, { headers => { 'X-aws-ec2-metadata-token-ttl-seconds' => '21600' } } ); | |||||||||
| 68 | 4 | ▶ | return unless $r->{success}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 4 | ▶ | return unless $r->{content}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | |||||||||||
| 71 | 4 | my $token = $r->{content}; | |||||||||
| 72 | 4 | my $options = { headers => { 'X-aws-ec2-metadata-token' => $token } }; | |||||||||
| 73 | |||||||||||
| 74 | 4 | $r = $ua->get( $self->metadata_url, $options ); | |||||||||
| 75 | 4 | ▶ | return unless $r->{success}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 4 | ▶ | return unless $r->{content}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | |||||||||||
| 78 | 3 | $r = $ua->get( $self->metadata_url . $r->{content}, $options ); | |||||||||
| 79 | 3 | ▶ | return unless $r->{success}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | |||||||||||
| 81 | 3 | my $json = eval { decode_json($r->{content}) }; | |||||||||
| 82 | 3 | ▶ | if ($@) { die "Error in JSON from metadata URL" } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | |||||||||||
| 84 | 2 | $self->actual_creds($json); | |||||||||
| 85 | 2 | $self->expiration(DateTime::Format::ISO8601->parse_datetime($json->{Expiration})->epoch); | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 1 | ▶ | no Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 89 | 1 | 1; | |||||||||
| 90 | ### main pod documentation begin ### | ||||||||||
| 91 | |||||||||||
| 92 | =encoding UTF-8 | ||||||||||
| 93 | |||||||||||
| 94 | =head1 NAME | ||||||||||
| 95 | |||||||||||
| 96 | Paws::Credential::InstanceProfileV2 | ||||||||||
| 97 | |||||||||||
| 98 | =head1 SYNOPSIS | ||||||||||
| 99 | |||||||||||
| 100 | use Paws::Credential::InstanceProfileV2; | ||||||||||
| 101 | |||||||||||
| 102 | my $paws = Paws->new(config => { | ||||||||||
| 103 | credentials => Paws::Credential::InstanceProfile->new( | ||||||||||
| 104 | metadata_url => 'http://localhost:8000/security-credentials', | ||||||||||
| 105 | timeout => 5, | ||||||||||
| 106 | ) | ||||||||||
| 107 | }); | ||||||||||
| 108 | |||||||||||
| 109 | =head1 DESCRIPTION | ||||||||||
| 110 | |||||||||||
| 111 | The InstanceProfileV2 credential provider is used to call retrieve AWS credentials from instances running on AWS | ||||||||||
| 112 | |||||||||||
| 113 | When running on an instance in AWS, if said instance has a Role attached to it (also named InstanceProfile), Paws | ||||||||||
| 114 | can retrieve short-term credentials (and refresh them when needed) from the AWS provided "metadata service" | ||||||||||
| 115 | using IMDSv2 approach. | ||||||||||
| 116 | |||||||||||
| 117 | =head2 metadata_url: Str | ||||||||||
| 118 | |||||||||||
| 119 | The section in the ini file where credentials will be looked up: | ||||||||||
| 120 | |||||||||||
| 121 | =head2 metadata_url: Str | ||||||||||
| 122 | |||||||||||
| 123 | URL for client to fetch token from | ||||||||||
| 124 | |||||||||||
| 125 | =head2 timetout: Int | ||||||||||
| 126 | |||||||||||
| 127 | Number of seconds to wait before timinig out a connection to the metadata service. It defaults to 1 second, as | ||||||||||
| 128 | the metadata service is almost local, and very fast responding. Note that if set too high, and the metadata | ||||||||||
| 129 | service is not present (not running on an AWS instance), the connection has to time out | ||||||||||
| 130 | |||||||||||
| 131 | =head2 ua | ||||||||||
| 132 | |||||||||||
| 133 | A user agent that has a C<get> method. Defaults to HTTP::Tiny | ||||||||||
| 134 | |||||||||||
| 135 | =cut | ||||||||||