AWS-Signature-V4
view release on metacpan or search on metacpan
lib/AWS/Signature/V4.pod view on Meta::CPAN
=pod
=for vim
vim: tw=72 ts=3 sts=3 sw=3 et ai :
=encoding utf8
=head1 NAME
AWS::Signature::V4 - User-Agent agnostic AWS Signatures V4 for credentials and X509
=head1 VERSION
This document describes AWS::Signature::V4 version 0.004.
=head1 SYNOPSIS
use AWS::Signature::V4;
# traditional variant, based on credentials
my $s = AWS::Signature::V4->new(
service => 'iam', region => 'us-east-1',
credentials => {
access_key_id => $key_id,
secret_access_key => $secret,
session_token => $token, # optional
},
);
# certificate-based variant (IAM Roles Anywhere)
my $x = AWS::Signature::V4->new(
service => 'rolesanywhere', region => 'eu-west-1',
x509 => {
key_type => 'ECDSA', # or RSA
certificate_file => 'cert.pem',
private_key_file => 'key.pem',
},
);
# sign a request: nothing is sent, nothing depends on the user agent
my $r = $s->sign(
method => 'POST',
url => 'https://iam.amazonaws.com/?Action=ListUsers',
headers => { 'Content-Type' => 'application/json' },
body => $payload,
);
$ua_request->header($_ => $r->{headers}{$_}) for keys $r->{headers}->%*;
# presigned URL, i.e. signature in the query string; the signer must
# be for the service of the URL, S3 here
my $s3 = AWS::Signature::V4->new(
service => 's3', region => 'us-east-1',
credentials => {
access_key_id => $key_id,
secret_access_key => $secret,
},
);
my $p = $s3->presign(
url => 'https://bucket.s3.amazonaws.com/key',
expires => 3600,
);
my $url = $p->{url};
=head1 DESCRIPTION
This module implements the AWS Signature Version 4 algorithm without
being tied to any specific user agent: it does not send anything, it
just takes the pieces of a request (method, URL, headers, body) and
returns what has to be added to it, so that it can be used with whatever
HTTP client is at hand.
Two variants are supported:
=over
=item *
the traditional one, based on credentials (algorithm
C<AWS4-HMAC-SHA256>), with optional session token;
=item *
the one based on X.509 certificates, as used by IAM Roles Anywhere
(algorithms C<AWS4-X509-RSA-SHA256> and C<AWS4-X509-ECDSA-SHA256>). The
signature is made with the private key that goes with the certificate,
using L<CryptX> (L<Crypt::PK::RSA> and L<Crypt::PK::ECC>) or a signing
function of your own.
=back
Both header-based signing (L</sign>) and presigned URLs (L</presign>)
are available, as well as chunked and streaming uploads to S3 (see
L</sign> and L</encoded_length>) and hashing of large payloads without
loading them in memory.
Some conventions apply to the inputs, because what is signed has to be
exactly what goes on the wire and the module cannot guess an encoding:
=over
=item *
the C<url> must be ASCII, with anything else already percent-encoded;
=item *
the body is a I<byte string>, or a reference to one: encode text as the
user agent will, wide characters are an error;
=item *
headers are a hash reference or an array reference of pairs, with names
that are case insensitive.
=back
Defaults that depend on the service (e.g. S3 handles path encoding
differently from all other services) are set from the C<service> name,
and can be overridden (see L</new>).
=head1 INTERFACE
=head2 new
my $s = AWS::Signature::V4->new(%args);
Create a signer. C<service> and C<region> are mandatory (for the
X.509 variant used with IAM Roles Anywhere, the service is
C<rolesanywhere>) and can only hold letters, digits, C<.>, C<_> and
C<->; exactly one of C<credentials> or C<x509> must be provided.
The classes of this distribution are built with L<Moo>: the constructor
also accepts a hash reference, and every option below is available as a
read-only accessor of the same name (e.g. C<< $s->region >>).
Options that are C<undef> are treated as missing. B<This is important>:
if you want to pass a false value in an input that accepts a boolean value,
use C<0> for I<false>.
The constructor checks the options, including that certificates and keys
can be read and loaded, and that C<signer> is a code reference, so
problems are reported by it and not by the first signature; it does not
check that a certificate is otherwise valid, nor that a key matches it.
C<credentials> and C<x509> are copied (shallowly), so changing your own
hash afterwards has no effect, but the accessors give back the copies
held by the object: leave them alone. Mind that C<< ->credentials >>
includes the secret access key. C<private_key> and
C<private_key_password> are dropped from the copy of C<x509> as soon as
the key is loaded, so C<< ->x509 >> does not have them. Subclasses and
roles work as usual.
The two variants are implemented by two internal classes, one per
option, that this class uses on your behalf:
L<AWS::Signature::V4::Credentials> and L<AWS::Signature::V4::X509>. They
are documented for the record, but you do not need to know about them.
=over
=item C<credentials>
hash reference with C<access_key_id>, C<secret_access_key> and the
optional C<session_token>. Other keys are an error, so that a misspelled
C<session_token> is not silently ignored. An empty value is like a
missing one: an error for the first two, no token for C<session_token>
(so that an empty C<AWS_SESSION_TOKEN> can be passed as it is).
=item C<x509>
hash reference with the following keys (others are an error):
=over
=item C<key_type>
C<RSA> or C<ECDSA>, depending on the key (mandatory). It determines the
signing algorithm, see L</algorithm>;
=item C<certificate>, C<certificate_file>
the certificate, either as PEM or DER content, or as the path of a file
holding it. One of them is needed, C<certificate> wins if both are given.
If the PEM holds several certificates (e.g. a F<fullchain.pem>), only the
first one is used and the others are ignored: put the intermediate
certificates in C<chain> or C<chain_files>;
=item C<chain>, C<chain_files>
optional intermediate certificates. C<chain> is either an array
reference of certificates (PEM or DER content, possibly mixed) or a plain
string, that then MUST be PEM. C<chain_files> is either an array
reference of file paths or a single path, each file holding PEM or DER
content. C<chain> wins if both are given.
A PEM item, whether from a string or a file, can be a bundle of several
certificates: whatever sits between the C<BEGIN CERTIFICATE>/C<END
CERTIFICATE> blocks is ignored, so they can be separated by empty or
whitespace-only lines, and CRLF line endings are fine;
=item C<serial>
the serial number of the certificate, in decimal. It is taken from the
certificate if not provided;
=item C<private_key_file>, C<private_key>
the private key, either as the path of a file or as its content, in PEM
or DER format (as understood by L<CryptX>). C<private_key> wins if both
are given;
=item C<private_key_password>
only for encrypted keys. Like C<private_key>, it is not kept once the key
is loaded;
=item C<signer>
alternative to the keys: a function that gets the bytes to sign and
returns the signature of their SHA-256, as raw bytes: PKCS#1 v1.5 for
RSA and DER for ECDSA (e.g. to delegate to an HSM or a KMS). Decode it
first if your HSM or KMS gives it in base64 or hex: the returned bytes are
put in the request as they are, hex-encoded, and AWS would just refuse a
signature in the wrong form. It takes precedence over
the keys. If it returns C<undef>, an empty string, a reference or a string
with wide characters, signing fails with an L<Ouch> exception with code
C<400>; if it dies, the error goes through as it is.
=back
=item C<double_encode>, C<normalize_path>, C<payload_header>
boolean overrides for what is decided from the service: encode the
path twice, remove empty, C<.> and C<..> segments from it, and always
add the C<x-amz-content-sha256> header. They default to true, true, false
for every service but S3, where they are false, false, true. S3 goes by
several names: C<s3>, C<s3-object-lambda>, C<s3-outposts> and
C<s3express>.
Again, setting an C<undef> value for these three arguments does I<NOT>
mean I<false> but I<default> instead (i.e. false, false, and true).
C<payload_header> also tells L</presign> what the service takes for
granted in a presigned URL: C<UNSIGNED-PAYLOAD> if true, as S3 does, the
hash of the body it gets otherwise.
=back
=head2 algorithm
my $name = $s->algorithm;
The name of the signing algorithm in use, e.g. C<AWS4-HMAC-SHA256>.
=head2 sign
my $r = $s->sign(%args);
my $r = $s->sign(\%args);
Sign a request. The arguments are name/value pairs or a hash reference;
an unknown name, or one that does not apply (e.g. C<expires>, which is
for L</presign>), is an error, and an C<undef> value is the same as a
missing argument. Named arguments:
=over
=item C<method>, C<url>
the HTTP method and the full URL (mandatory). The method must be an HTTP
token, and it is uppercased. The host, lowercased and without default
ports, provides the C<host> header unless it is given; user information
(C<user@host>) is an error, it is not stripped.
The URL can also be just a path, starting with C</>, if a C<Host> header
is given. See L</DESCRIPTION> and L</SECURITY CONSIDERATIONS> for what
( run in 1.435 second using v1.01-cache-2.11-cpan-8379b9b8ec2 )