Skip to content

Instantly share code, notes, and snippets.

@frioux
Created May 9, 2017 14:47
Show Gist options
  • Save frioux/ef8c2051263c70e31ab4b7ba39153aff to your computer and use it in GitHub Desktop.
Save frioux/ef8c2051263c70e31ab4b7ba39153aff to your computer and use it in GitHub Desktop.
Example for IAM slide
use Paws;
Paws->load_class('Paws::Credential::ProviderChain');
use POSIX 'strftime';
use Net::Amazon::Signature::V4;
# ...
has credential_provider => (
is => 'ro',
lazy => 1,
default => sub {
my ($self) = @_;
Paws::Credential::ProviderChain->new->selected_provider;
},
);
sub signer {
my ($self) = @_;
my $provider = $self->credential_provider;
return Net::Amazon::Signature::V4->new(
$provider->access_key,
$provider->secret_key,
$self->region, # 'us-east-1'
_service(), # 'sqs'
);
}
sub sign_req {
my ($self, $request) = @_;
$request->header(
Date => $request->header('X-Amz-Date') //
strftime( '%Y%m%dT%H%M%SZ', gmtime )
);
$request->header( Host => $request->uri->host );
if ($self->credential_provider->session_token) {
$request->header(
'X-Amz-Security-Token' => $self->credential_provider->session_token
);
}
my $sig = $self->signer;
$sig->sign( $request );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment