AWS-Signature-V4

 view release on metacpan or  search on metacpan

eg/10-s3-content-encoding-probe.pl  view on Meta::CPAN

# Two objects are uploaded: a control with no Content-Encoding of its
# own, and the real case with gzip. The control tells a wrong order apart
# from a wrong bucket, region or set of credentials. Both are deleted
# again, unless KEEP=1.
#
# The report at the end is meant to be pasted in a bug report or a chat:
# it is built from a fixed list of fields, so it carries no credentials,
# no Authorization header, no session token, and neither bucket nor key.
use v5.24;
use warnings;
use FindBin '$Bin';
use lib "$Bin/../lib";
use AWS::Signature::V4;
use HTTP::Tiny;
use IO::Compress::Gzip qw< gzip $GzipError >;

my ($bucket, $prefix) = @ARGV;
# an empty bucket would also leave redact() with an empty pattern, which
# in Perl means "the last pattern that matched", i.e. no redaction at all
die "usage: $0 BUCKET [KEY_PREFIX]\n" unless defined $bucket && length $bucket;
$prefix = 'aws-sigv4-probe/' unless defined $prefix && length $prefix;
my $region = $ENV{AWS_REGION} // 'us-east-1';

# the bucket and the region are pasted into the host name below. A "/" in
# either of them moves the host somewhere else entirely -- "evil.com/x"
# makes the URL https://evil.com/x.s3... -- and the request would be
# signed and sent there, session token included. The prefix goes in the
# path, where a "?" or a "#" would change which object is addressed, and
# the DELETE at the end follows the same URL
die "invalid bucket name: it must be 3 to 63 letters, digits, dots or dashes\n"
   unless $bucket =~ m{\A[A-Za-z0-9][A-Za-z0-9.-]{1,61}[A-Za-z0-9]\z};
die "invalid region: it must be letters, digits and dashes\n"
   unless $region =~ m{\A[a-z0-9-]+\z};
die "invalid key prefix: use letters, digits, dot, dash, underscore, tilde and /\n"
   unless $prefix =~ m{\A[A-Za-z0-9._~/-]+\z};

my $signer = AWS::Signature::V4->new(
   service     => 's3',
   region      => $region,
   credentials => {
      access_key_id     => $ENV{AWS_ACCESS_KEY_ID},
      secret_access_key => $ENV{AWS_SECRET_ACCESS_KEY},
      session_token     => $ENV{AWS_SESSION_TOKEN},
   },
);

# something that compresses well, so that the gzipped payload is clearly
# not the plain one and a body that came back unzipped cannot be mistaken
# for a good round trip
my $plain = "the quick brown fox jumps over the lazy dog\n" x 100;
gzip \$plain, \my $gz or die "gzip failed: $GzipError\n";
my $chunk_size = 64 * 1024;    # one data chunk: the last one, so S3's
my $size       = length $gz;   # 8 KiB minimum does not apply to it
my $chunks     = int($size / $chunk_size) + ($size % $chunk_size ? 1 : 0);

my $http = HTTP::Tiny->new(verify_SSL => 1);
my $base = "https://$bucket.s3.$region.amazonaws.com/";

# HTTP::Tiny adds the Host header itself and refuses to be given one; it
# is signed all the same, and it will have the same value
sub without_host {
   my ($r) = @_;
   my %headers = $r->{headers}->%*;
   delete $headers{host};
   return \%headers;
}

# S3 reports failures as XML; the first two fields are the useful ones.
# HTTP::Tiny never reached it when the status is 599, and then the reason
# (no route, TLS refused, name not resolved) is in the content instead
sub s3_error {
   my ($response) = @_;
   my $content = $response->{content} // '';
   my ($code) = $content =~ m{<Code>([^<]*)</Code>};
   my ($msg)  = $content =~ m{<Message>([^<]*)</Message>};
   return one_line(defined $msg && length $msg ? "$code: $msg" : $code) if defined $code;
   return '' unless ($response->{status} // 0) == 599;
   return one_line($content);
}

# the S3 error code on its own (NoSuchBucket, AccessDenied, ...). Its
# presence is what tells a refusal by S3 apart from a failure that never
# reached it, which is the difference between an answer and a bad run
sub s3_code {
   my ($response) = @_;
   my ($code) = ($response->{content} // '') =~ m{<Code>([^<]*)</Code>};
   return undef unless defined $code && length $code;
   return one_line($code);
}

# the status line comes from the other end as well: HTTP::Tiny takes the
# reason as [^\r\n]*, so it can carry control characters, terminal escapes
# and any length at all, and it goes in the report like everything else
sub status_of {
   my ($response) = @_;
   return one_line(($response->{status} // 0) . ' ' . ($response->{reason} // ''));
}

# every word of this is written by the other end of the connection, and it
# is quoted in a report that gets read as evidence: keep it to one bounded
# line of printable ASCII, so that it cannot forge a line of that report
# (a <Message> holding a newline and "verdict: ..." otherwise would)
sub one_line {
   my ($text) = @_;
   return '' unless defined $text && length $text;
   $text =~ s{\s+}{ }g;
   $text =~ s{[^\x20-\x7E]}{.}g;
   $text =~ s{\A\s+}{};
   $text =~ s{\s+\z}{};
   $text = substr($text, 0, 197) . '...' if length $text > 200;
   return redact($text);
}

# both kinds of error quote the URL or the key back at us, and the report
# is meant to be pasted: keep the bucket and the key out of it
sub redact {
   my ($text) = @_;
   return $text unless defined $text && length $text;
   $text =~ s{\Q$bucket\E}{BUCKET}g;
   $text =~ s{\Q$prefix\E}{KEY-PREFIX/}g;
   return $text;
}

# HTTP::Tiny lowercases header names and gives an array reference when a
# header appears more than once
sub header_of {
   my ($response, $name) = @_;
   my $v = $response->{headers}{$name};
   return '(none)' unless defined $v;
   # also written by the other end, so it goes through the same sieve
   return one_line(ref $v eq 'ARRAY' ? join(',', $v->@*) : $v);
}

my @cases = (
   {name => 'A control', key => $prefix . 'control', extra => {}},
   {name => 'B gzip',    key => $prefix . 'gzip',    extra => {'Content-Encoding' => 'gzip'}},
);

# undef when the key can be used, otherwise why it cannot. This program
# deletes what it uploads, so it must only ever touch keys that hold
# nothing: anything short of a clear "not there" is taken as occupied
sub key_in_the_way {
   my ($key) = @_;
   my $url = $base . $key;
   my $r = $signer->sign(method => 'HEAD', url => $url);
   my $head = $http->request(HEAD => $url, {headers => without_host($r)});
   return undef if ($head->{status} // 0) == 404;
   return 'an object is already there' if $head->{success};
   # without s3:ListBucket, S3 answers HEAD on a key that is not there
   # with 403 rather than 404, so a free key cannot be told from a
   # forbidden one -- and Get/Put/DeleteObject alone, which is all this
   # program needs, is exactly the case where that happens
   return 'cannot tell whether it is free: S3 said 403, which without '
      . 's3:ListBucket is also the answer for a key that does not exist'
      if ($head->{status} // 0) == 403;
   return 'cannot tell whether it is free (' . status_of($head) . ')';
}

if (!$ENV{DRY_RUN}) {
   for my $c (@cases) {
      my $why = key_in_the_way($c->{key}) // next;
      die "refusing to use the key '$c->{key}': $why.\n",
          "This program overwrites and then deletes the keys it uses, so it\n",
          "only touches ones that hold nothing. If that key is in fact free,\n",
          "then the check itself failed: verify the bucket, the region and\n",
          "the credentials, and grant s3:ListBucket on the bucket so that a\n",
          "missing key answers 404 instead of 403. Otherwise pass a\n",
          "KEY_PREFIX that points at empty space, e.g.:\n",
          "   $0 $bucket probe-", time, "/\n";
   }
}

for my $c (@cases) {
   my $url = $base . $c->{key};
   my $r = $signer->sign(
      method  => 'PUT',
      url     => $url,
      headers => {
         $c->{extra}->%*,
         'Content-Type' => 'text/plain',
         # length of the *encoded* body: data plus the chunk framing
         'Content-Length' => AWS::Signature::V4->encoded_length($size, $chunk_size),
      },
      streaming              => 1,
      decoded_content_length => $size,
   );
   # what the module decided to send, rather than what we assume it sends:
   # this is the whole question, and it must be reported as it is
   $c->{sent}     = $r->{headers}{'content-encoding'};
   $c->{announced} = $r->{headers}{'content-length'};
   # the encoded body is built once, whole, and sent as a string. A
   # callback would be the thing to do for a real upload (example 05 does
   # that), but HTTP::Tiny retries an idempotent request once when the
   # socket breaks under it, and this body cannot be produced a second
   # time: the chunk signatures chain, and finish() has already been
   # called, so the retry would send nothing and fail on the length --
   # reported as a 599, which reads like a refusal and is not one. A few
   # hundred bytes in memory buy a run that means what it says
   my $chunker = $r->{chunker};
   my $body    = '';
   for (my $offset = 0; $offset < $size; $offset += $chunk_size) {
      $body .= $chunker->chunk(substr $gz, $offset, $chunk_size);



( run in 0.791 second using v1.01-cache-2.11-cpan-0e0213f3164 )