Amazon-SQS-Client
view release on metacpan or search on metacpan
lib/Amazon/SQS/Client.pm view on Meta::CPAN
# Implements AWS Signature, as per following spec:
#
# If Signature Version is 0, it signs concatenated Action and Timestamp
#
# If Signature Version is 1, it performs the following:
#
# Sorts all parameters (including SignatureVersion and excluding Signature,
# the value of which is being created), ignoring case.
#
# Iterate over the sorted list and append the parameter name (in original case)
# and then its value. It will not URL-encode the parameter values before
# constructing this string. There are no separators.
#
sub _signParameters {
my ( $self, $parameters, $queueUrl, $key ) = @_;
my $algorithm = 'HmacSHA1';
my $data = $EMPTY;
my $signatureVersion = $parameters->{SignatureVersion};
if ( '0' eq $signatureVersion ) {
$data = $self->_calculateStringToSignV0($parameters);
}
elsif ( '1' eq $signatureVersion ) {
$data = $self->_calculateStringToSignV1($parameters);
}
elsif ( '2' eq $signatureVersion ) {
$algorithm = $self->get_SignatureMethod;
$parameters->{SignatureMethod} = $algorithm;
$data = $self->_calculateStringToSignV2( $parameters, $queueUrl );
}
else {
Carp::croak('Invalid Signature Version specified');
}
return $self->_sign( $data, $key, $algorithm );
}
sub _calculateStringToSignV0 {
my ( $self, $parameters ) = @_;
return $parameters->{Action} . $parameters->{Timestamp};
}
sub _calculateStringToSignV1 {
my ( $self, $parameters ) = @_;
my $data = $EMPTY;
foreach my $parameterName ( sort { lc($a) cmp lc $b } keys %{$parameters} ) {
no warnings 'uninitialized'; ## no critic
$data .= $parameterName . $parameters->{$parameterName};
}
return $data;
}
sub _calculateStringToSignV2 {
my ( $self, $parameters, $queueUrl ) = @_;
my $endpoint = URI->new($queueUrl);
my $data = 'POST';
$data .= "\n";
$data .= $endpoint->host;
$data .= "\n";
my $path = $endpoint->path || $SLASH;
$data .= $self->_urlencode( $path, 1 );
$data .= "\n";
my @parameterKeys = keys %{$parameters};
foreach my $parameterName ( sort { $a cmp $b } @parameterKeys ) {
no warnings 'uninitialized'; ## no critic
$data .= $parameterName . $EQUALS . $self->_urlencode( $parameters->{$parameterName} );
$data .= $AMPERSAND;
}
chop $data;
return $data;
}
########################################################################
sub _urlencode {
########################################################################
my ( $self, $value, $path ) = @_;
use URI::Escape qw(uri_escape_utf8);
my $escapepattern = '^A-Za-z0-9\-_.~';
if ($path) {
$escapepattern = $escapepattern . $SLASH;
}
return uri_escape_utf8( $value, $escapepattern );
}
#
# Computes RFC 2104-compliant HMAC signature.
#
sub _sign {
my ( $self, $data, $key, $algorithm ) = @_;
my $output = $EMPTY;
if ( 'HmacSHA1' eq $algorithm ) {
$output = hmac_sha1_base64( $data, $key );
}
elsif ( 'HmacSHA256' eq $algorithm ) {
$output = hmac_sha256_base64( $data, $key );
}
else {
Carp::croak('Non-supported signing method specified');
}
return $output . $EQUALS;
}
#
# Formats date as ISO 8601 timestamp
#
sub _getFormattedTimestamp {
return sprintf '%04d-%02d-%02dT%02d:%02d:%02d.000Z', sub {
( $_[5] + 1900, $_[4] + 1, $_[3], $_[2], $_[1], $_[0] )
}
lib/Amazon/SQS/Client.pm view on Meta::CPAN
scalable hosted queue for storing messages as they travel between
computers.
By using Amazon SQS, developers can simply move data between
distributed application components performing different tasks, without
losing messages or requiring each component to be always available.
Amazon SQS works by exposing Amazon's web-scale messaging
infrastructure as a web service. Any computer on the Internet can add
or read messages without any installed software or special firewall
configurations. Components of applications using Amazon SQS can run
independently, and do not need to be on the same network, developed
with the same technologies, or running at the same time.
=head1 METHODS AND SUBROUTINES
=head2 new
new( aws-access-key-id, aws-secret-access-key, [token], [options] )
=over 5
=item aws-access-key-id
The AWS I<access key> your were given when you signed up for AWS services.
You can create a new account by visiting L</http://aws.amazon.com/>.
=item aws-secret-access-key
The AWS I<secret access key> you were given when you signed up for AWS services.
=item token
Pass the session token as the third argument on as SecurityToken in
the options hash described below.
=item options
C<options> is an optional hash reference containing the options listed
below.
=over 5
=item * ServiceURL
default: C<https://queue.amazonaws.com>
Set the ServiceUrl when you want to use a mocking service like
L<LocalStack|https://www.localstack.cloud>.
=item * UserAgent
default: LWP::UserAgent
=item * SignatureVersion
default: 2
I<Note: Signature Version 4 is supported by AWS::Signature4. If you
use the Signature 4 signing facility, make sure your ServiceURL
includes the region endpoint. Ex:
https://sqs.us-east-1.amazonaws.com.>
=item * SecurityToken
For temporary credentials, add the security token returned from the
AWS Security Token Service.
=item * ServiceVersion
default: 2012-11-05
=item * MaxErrorRetry
default: 3
=item * credentials
An instance of a class (e.g. Amazon::Credentials) which supports the getters:
get_aws_access_key_id
get_aws_secret_access_key
get_token
You are encouraged to use this option rather than sending the
credentials in the constructor.
=back
=back
=head2 createQueue
createQueue( request )
The C<CreateQueue> action creates a new queue, or returns the URL of an
existing one. When you request C<CreateQueue>, you provide a name for
the queue. To successfully create a new queue, you must provide a name
that is unique within the scope of your own queues. If you provide the
name of an existing queue, a new queue isnE<039>t created and an error
isnE<039>t returned. Instead, the request succeeds and the queue URL for
the existing queue is returned.
I<Exception: if you provide a value for C<DefaultVisibilityTimeout> that is
different from the value for the existing queue, you receive an error.>
See
L</http://docs.amazonwebservices.com/AWSSimpleQueueService/2009-02-01/SQSDeveloperGuide/Query_QueryCreateQueue.html>.
Returns an C<Amazon::SQS::Model::CreateQueueResponse> object.
Throws an C<Amazon::SQS::Exception>. Use eval to catch it.
=over 5
=item request
C<request> is either a hash reference of parameters for
a C<Amazon::SQS::Model::CreateQueueRequest> object or
a C<Amazon::SQS::Model::CreateQueueRequest> object itself.
( run in 0.657 second using v1.01-cache-2.11-cpan-39bf76dae61 )