File Coverage

blib/lib/Paws/Credential/InstanceProfile.pm
Criterion Covered Total %
statement 38 38 100.0
branch 8 10 80.0
condition n/a
subroutine 9 9 100.0
pod 0 3 0.0
total 55 60 91.6


line stmt bran cond sub pod time code
1             package Paws::Credential::InstanceProfile;
2 1     1   916 use JSON::MaybeXS;
  1         3  
  1         88  
3 1     1   6 use Moose;
  1         2  
  1         8  
4 1     1   9175 use DateTime::Format::ISO8601;
  1         580908  
  1         111  
5             with 'Paws::Credential';
6              
7             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             has timeout => (is => 'ro', isa => 'Int', default => 1);
14              
15             has ua => (
16             is => 'ro',
17             lazy => 1,
18             default => sub {
19             my $self = shift;
20 1     1   657 use HTTP::Tiny;
  1         32319  
  1         284  
21             HTTP::Tiny->new(
22             agent => 'AWS Perl SDK',
23             timeout => $self->timeout,
24             );
25             }
26             );
27              
28             has expiration => (
29             is => 'rw',
30             isa => 'Int',
31             default => sub { 0 }
32             );
33              
34             has actual_creds => (is => 'rw', default => sub { {} });
35              
36             sub access_key {
37 4     4 0 1235 my $self = shift;
38 4         20 $self->_refresh;
39 3         81 $self->actual_creds->{AccessKeyId};
40             }
41              
42             sub secret_key {
43 2     2 0 5 my $self = shift;
44 2         5 $self->_refresh;
45 2         50 $self->actual_creds->{SecretAccessKey};
46             }
47              
48             sub session_token {
49 2     2 0 5 my $self = shift;
50 2         7 $self->_refresh;
51 2         50 $self->actual_creds->{Token};
52             }
53              
54             #TODO: Raise exceptions if HTTP get didn't return success
55             sub _refresh {
56 8     8   13 my $self = shift;
57              
58 8 100       314 return if $self->expiration >= time;
59              
60 4         106 my $ua = $self->ua;
61 4         106 my $r = $ua->get($self->metadata_url);
62 4 50       52 return unless $r->{success};
63 4 100       13 return unless $r->{content};
64              
65 3         82 $r = $ua->get($self->metadata_url . $r->{content});
66 3 50       3135 return unless $r->{success};
67              
68 3         8 my $json = eval { decode_json($r->{content}) };
  3         43  
69 3 100       131 if ($@) { die "Error in JSON from metadata URL" }
  1         10  
70              
71 2         67 $self->actual_creds($json);
72 2         18 $self->expiration(DateTime::Format::ISO8601->parse_datetime($json->{Expiration})->epoch);
73             }
74              
75 1     1   10 no Moose;
  1         2  
  1         10  
76             1;
77             ### main pod documentation begin ###
78              
79             =encoding UTF-8
80              
81             =head1 NAME
82              
83             Paws::Credential::InstanceProfile
84              
85             =head1 SYNOPSIS
86              
87             use Paws::Credential::InstanceProfile;
88              
89             my $paws = Paws->new(config => {
90             credentials => Paws::Credential::InstanceProfile->new(
91             metadata_url => 'http://localhost:8000/security-credentials',
92             timeout => 5,
93             )
94             });
95              
96             =head1 DESCRIPTION
97              
98             The InstanceProfile credential provider is used to call retrieve AWS credentials from instances running on AWS
99              
100             When running on an instance in AWS, if said instance has a Role attached to it (also named InstanceProfile), Paws
101             can retrieve short-term credentials (and refresh them when needed) from the AWS provided "metadata service".
102              
103             =head2 metadata_url: Str
104              
105             The section in the ini file where credentials will be looked up:
106              
107             =head2 timetout: Int
108              
109             Number of seconds to wait before timinig out a connection to the metadata service. It defaults to 1 second, as
110             the metadata service is almost local, and very fast responding. Note that if set too high, and the metadata
111             service is not present (not running on an AWS instance), the connection has to time out
112              
113             =head2 ua
114              
115             A user agent that has a C<get> method. Defaults to HTTP::Tiny
116              
117             =cut