AWS-Signature-V4
view release on metacpan or search on metacpan
script/aws-iam-ra view on Meta::CPAN
my $config = get_options(
[
{
getopt => 'certificate|c=s',
environment => 'CERT_FILE',
required => 1,
},
{
getopt => 'duration|d=i',
environment => 'DURATION',
default => 3600,
},
{
getopt => 'full!',
default => 0,
},
{
getopt => 'key|k=s',
environment => 'KEY_FILE',
required => 1,
},
{
getopt => 'key-type|K=s',
environment => 'KEY_TYPE',
default => 'RSA',
},
{
getopt => 'profile-arn|p=s',
environment => 'PROFILE_ARN',
required => 1,
},
{
getopt => 'region|R=s',
environment => 'AWS_REGION',
required => 1,
},
{
getopt => 'role-arn|r=s',
environment => 'ROLE_ARN',
required => 1,
},
{
getopt => 'trust-anchor-arn|t=s',
environment => 'TRUST_ANCHOR_ARN',
required => 1,
},
],
@ARGV,
);
die "invalid region: letters, digits and dashes only\n"
unless $config->{region} =~ m{\A[a-z0-9-]+\z};
my $signer = AWS::Signature::V4->new(
service => 'rolesanywhere',
region => $config->{region},
x509 => {
key_type => $config->{'key-type'},
certificate_file => $config->{certificate},
private_key_file => $config->{key},
},
);
my $url = "https://rolesanywhere.$config->{region}.amazonaws.com/sessions";
my $body = encode_json(
{
trustAnchorArn => $config->{'trust-anchor-arn'},
profileArn => $config->{'profile-arn'},
roleArn => $config->{'role-arn'},
durationSeconds => $config->{duration},
}
);
my $r = $signer->sign(
method => 'POST',
url => $url,
headers => {'Content-Type' => 'application/json'},
body => $body,
);
# beside Authorization, the headers now include X-Amz-X509 (the
# certificate) and, if there is one, X-Amz-X509-Chain
# HTTP::Tiny adds the Host header itself, from the URL, and refuses to be
# given one: it is signed all the same, and it will have the same value
my %headers = $r->{headers}->%*;
delete $headers{host};
my $ua = HTTP::Tiny->new(verify_SSL => 1);
my $response = $ua->request(POST => $url,
{headers => \%headers, content => $body});
say {*STDERR} "$response->{status} $response->{reason}";
if (!$response->{success}) {
say {*STDERR} $response->{content}; # this says what went wrong
die "the request failed\n";
}
else {
my $data = decode_json($response->{content});
$data = $data->{credentialSet}[0]{credentials}
unless $config->{full};
say {*STDOUT} JSON::PP->new->ascii->canonical->pretty->encode($data);
}
sub get_options ($specs, @args) {
my (%cmdline, %environment, %default);
my @cmdline_options = qw< help! man! usage! version! >;
my @required;
for my $spec ($specs->@*) {
my ($optnames, $default, $env_var, $required) =
ref $spec
? $spec->@{qw< getopt default environment required >}
: ($spec, undef, undef, undef);
push @cmdline_options, $optnames;
my $name = $optnames =~ s{[^-\w].*}{}rmxs;
$default{$name} = $default if defined $default;
$environment{$name} = $ENV{$env_var}
if defined $env_var && exists $ENV{$env_var};
push @required, $name if $required;
} ## end for my $spec ($specs->@*)
GetOptionsFromArray(\@args, \%cmdline, @cmdline_options)
( run in 1.520 second using v1.01-cache-2.11-cpan-8379b9b8ec2 )