AWS-Signature-V4
view release on metacpan or search on metacpan
use v5.24;
use Test2::V0;
use FindBin '$Bin';
use lib "$Bin/../lib";
use experimental 'signatures';
use Digest::SHA qw< sha256 >;
use MIME::Base64 qw< encode_base64 >;
use AWS::Signature::V4;
use AWS::Signature::V4::Chunker;
my %cred = (credentials => {access_key_id => 'a', secret_access_key => 'b'});
my $s = AWS::Signature::V4->new(service => 's3', region => 'r', %cred);
my $secret = 'Hunter2-TopSecret';
sub ouch_like ($code, $status, $name) {
is dies { $code->() },
object { prop blessed => 'Ouch'; call code => $status },
$name;
}
sub streaming ($signer, %opts) {
$signer->sign(method => 'PUT', url => 'http://h/', streaming => 1, %opts);
}
# errors caused by the caller are Ouch exceptions with code 400
my %caller = (
'no service' => sub { AWS::Signature::V4->new(region => 'r', %cred) },
'no region' => sub { AWS::Signature::V4->new(service => 's', %cred) },
'no credentials' => sub { AWS::Signature::V4->new(service => 's', region => 'r') },
'both variants' => sub { AWS::Signature::V4->new(service => 's', region => 'r', %cred, x509 => {}) },
'bad key type' => sub { AWS::Signature::V4->new(service => 's', region => 'r', x509 => {key_type => 'DSA'}) },
'x509 key type under its old name' =>
sub { AWS::Signature::V4->new(service => 's', region => 'r', x509 => {algorithm => 'RSA'}) },
'credentials not a hash' =>
sub { AWS::Signature::V4->new(service => 's', region => 'r', credentials => 'AKID:SECRET') },
'no method' => sub { $s->sign(url => 'http://h/') },
'non-ASCII url' => sub { $s->sign(method => 'GET', url => "http://h/\x{e9}") },
'control chars in url' => sub { $s->sign(method => 'GET', url => "http://h/a\nb") },
'empty url' => sub { $s->sign(method => 'GET', url => '') },
'no host' => sub { $s->presign(url => '/path') },
'wide body' => sub { $s->sign(method => 'PUT', url => 'http://h/', body => "\x{263a}") },
'body_fh not a handle' => sub { $s->sign(method => 'PUT', url => 'http://h/', body_fh => 'file.txt') },
'bad expires' => sub { $s->presign(url => 'http://h/', expires => 0) },
'unknown checksum' => sub { streaming($s, decoded_content_length => 1, checksum => 'md5') },
'finish twice' => sub {
my $ck = streaming($s, decoded_content_length => 0)->{chunker};
$ck->finish;
$ck->finish;
},
);
ouch_like $caller{$_}, 400, $_ for sort keys %caller;
# ... including what goes wrong when loading certificates and keys
my %x509 = (service => 's', region => 'r');
my $der = "\x30\x03\x02\x01\x05"; # a tiny, valid, DER structure
my %x509_errors = (
'garbage certificate' => {key_type => 'RSA', certificate => 'garbage', serial => 1, signer => sub { 'x' }},
'truncated certificate' => {key_type => 'RSA', certificate => "\x30\x82\x01", signer => sub { 'x' }},
'signer not code' => {key_type => 'RSA', certificate => $der, serial => 1, signer => 'nope'},
'missing key file' => {key_type => 'RSA', certificate => $der, serial => 1, private_key_file => '/no/such/key'},
'garbage key' => {key_type => 'ECDSA', certificate => $der, serial => 1, private_key => 'garbage'},
);
( run in 2.285 seconds using v1.01-cache-2.11-cpan-234fa220139 )