| 1 | package WebService::Amazon::IAM; | ||||||||||||||||||||
| 2 | # ABSTRACT: IAM role implementation | ||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.001'; | |||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | WebService::Amazon::IAM - basic Amazon IAM role functionality | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | =head1 VERSION | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | version 0.001 | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | Coming soon. | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | =cut | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 2 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | 2 | ▶ | use Time::Moment; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 2 | ▶ | use Log::Any qw($log); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | =head1 METHODS | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | =cut | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | sub new { | ||||||||||||||||||||
| 32 | 0 | ▶ | my ($class, %args) = @_; | ||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 33 | 0 | bless { | |||||||||||||||||||
| 34 | # This is the address on the internal network used | ||||||||||||||||||||
| 35 | # for all IAM-related functionality. | ||||||||||||||||||||
| 36 | base_uri => 'http://169.254.169.254', | ||||||||||||||||||||
| 37 | %args | ||||||||||||||||||||
| 38 | }, $class | ||||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | =head2 active_roles | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | Retrieves a list of the active roles in this AWS instance. | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | Currently only one role is supported per server (!), so this will just | ||||||||||||||||||||
| 46 | resolve to a single string. | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | is($iam->active_roles->get, 'some_role_name'); | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | =cut | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | sub active_roles { | ||||||||||||||||||||
| 53 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
active_roles: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 54 | 0 | my $uri = $self->build_uri('/latest/meta-data/iam/security-credentials/'); | |||||||||||||||||||
| 55 | 0 | $log->debugf("Requesting list of roles from [%s]", "$uri"); | |||||||||||||||||||
| 56 | 0 | $self->ua->get($uri) | |||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | =head2 credentials_for_role | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | Resolves to the credentials for the given role. | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | my $creds = $iam->credentials_for_role('some_role'); | ||||||||||||||||||||
| 64 | say "Access key: " . $creds->{access_key}; | ||||||||||||||||||||
| 65 | say "Secret key: " . $creds->{secret_key}; | ||||||||||||||||||||
| 66 | say "Token: " . $creds->{token}; | ||||||||||||||||||||
| 67 | say "Will expire at " . strftime '%Y-%m-%d %H:%M:%S', localtime $creds->{expiry}; | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | =cut | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | sub credentials_for_role { | ||||||||||||||||||||
| 72 | 0 | ▶ | my ($self, $role) = @_; | ||||||||||||||||||
|
Subroutine
credentials_for_role: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 73 | 0 | my $uri = $self->build_uri('/latest/meta-data/iam/security-credentials/' . $role); | |||||||||||||||||||
| 74 | 0 | $log->debugf("Requesting credentials from [%s]", "$uri"); | |||||||||||||||||||
| 75 | $self->ua->get($uri)->then(sub { | ||||||||||||||||||||
| 76 | 0 | ▶ | my $data = $self->json->decode(shift); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 77 | 0 | ▶ | return Future->fail("Invalid return code", iam => $data->{Code}, $data) unless $data->{Code} eq 'Success'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | 0 | ▶ | return Future->fail("Invalid access key type", iam => $data->{Type}, $data) unless $data->{Type} eq 'AWS-HMAC'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | 0 | my $expiry = Time::Moment->from_string($data->{Expiration})->epoch; | |||||||||||||||||||
| 80 | Future->wrap({ | ||||||||||||||||||||
| 81 | access_key => $data->{AccessKeyId}, | ||||||||||||||||||||
| 82 | secret_key => $data->{SecretAccessKey}, | ||||||||||||||||||||
| 83 | token => $data->{Token}, | ||||||||||||||||||||
| 84 | 0 | }, $expiry) | |||||||||||||||||||
| 85 | }) | ||||||||||||||||||||
| 86 | 0 | } | |||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 0 | ▶ | sub base_uri { shift->{base_uri} } | ||||||||||||||||||
|
Subroutine
base_uri: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 89 | 0 | ▶ | sub build_uri { my $self = shift; URI->new(join '', $self->base_uri, @_) } | ||||||||||||||||||
|
Subroutine
build_uri: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 90 | 0 | ▶ | sub json { shift->{json} ||= JSON::MaybeXS->new } | ||||||||||||||||||
|
Subroutine
json: not called CC=2 SCAR=17.9
Pod
undocumented
Condition0%0/3
+(shift())->{"json"} ||= "JSON::MaybeXS"->new
MC/DC0%0/2
+(shift())->{"json"} ||= "JSON::MaybeXS"->new
+(shift())->{"json"}
"JSON::MaybeXS"->new
Truth table0%0/3
A ||= B
A= +(shift())->{"json"}B= "JSON::MaybeXS"->new
| |||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 0 | ▶ | sub ua { shift->{ua} // die "no user agent provided" } | ||||||||||||||||||
|
Subroutine
ua: not called CC=2 SCAR=17.9
Pod
undocumented
Condition0%0/2
+(shift())->{"ua"} // die("no user agent provided")
MC/DC0%0/1
+(shift())->{"ua"} // die("no user agent provided")
+(shift())->{"ua"}
Truth table0%0/2
A
A= +(shift())->{"ua"}
| |||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 2 | 1; | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | __END__ | ||||||||||||||||||||