Sha256: 393ec9d46af7b596924122ea2100b3c08b12e35890e7d5745e7de43d55d95364

Contents?: true

Size: 1.62 KB

Versions: 65

Compression:

Stored size: 1.62 KB

Contents

module Aws
  module DynamoDB
    module Plugins
      class CRC32Validation < Seahorse::Client::Plugin

        option(:compute_checksums,
          default: true,
          doc_type: 'Boolean',
          docstring: <<-DOCS)
When `true`, a CRC32 checksum is computed of every HTTP
response body and compared against the `X-Amz-Crc32` header.
If the checksums do not match, the request is re-sent.
Failures can be retried up to `:retry_limit` times.
          DOCS

        def add_handlers(handlers, config)
          if config.compute_checksums
            handlers.add(Handler, step: :sign)
          end
        end

        class Handler < Seahorse::Client::Handler

          def call(context)
            # disable response gzipping - Net::HTTP unzips these responses
            # before we can see the body, making it impossible to verify
            # the CRC32 checksum against the compressed body stream
            context.http_request.headers['accept-encoding'] = ''

            @handler.call(context).on_success do |response|
              response.error = validate(context)
            end
          end

          private

          def validate(context)
            unless crc32_is_valid?(context.http_response)
              msg = "Response failed CRC32 check."
              return Errors::CRC32CheckFailed.new(context, msg)
            end
          end

          def crc32_is_valid?(response)
            if crc_checksum = response.headers['x-amz-crc32']
              crc_checksum.to_i == Zlib.crc32(response.body_contents)
            else
              true
            end
          end

        end
      end
    end
  end
end

Version data entries

65 entries across 65 versions & 1 rubygems

Version Path
aws-sdk-dynamodb-1.49.1 lib/aws-sdk-dynamodb/plugins/crc32_validation.rb
aws-sdk-dynamodb-1.48.0 lib/aws-sdk-dynamodb/plugins/crc32_validation.rb
aws-sdk-dynamodb-1.47.0 lib/aws-sdk-dynamodb/plugins/crc32_validation.rb
aws-sdk-dynamodb-1.46.1 lib/aws-sdk-dynamodb/plugins/crc32_validation.rb
aws-sdk-dynamodb-1.46.0 lib/aws-sdk-dynamodb/plugins/crc32_validation.rb
aws-sdk-dynamodb-1.45.0 lib/aws-sdk-dynamodb/plugins/crc32_validation.rb
aws-sdk-dynamodb-1.44.0 lib/aws-sdk-dynamodb/plugins/crc32_validation.rb
aws-sdk-dynamodb-1.43.0 lib/aws-sdk-dynamodb/plugins/crc32_validation.rb
aws-sdk-dynamodb-1.42.0 lib/aws-sdk-dynamodb/plugins/crc32_validation.rb
aws-sdk-dynamodb-1.41.0 lib/aws-sdk-dynamodb/plugins/crc32_validation.rb
aws-sdk-dynamodb-1.40.0 lib/aws-sdk-dynamodb/plugins/crc32_validation.rb
aws-sdk-dynamodb-1.39.0 lib/aws-sdk-dynamodb/plugins/crc32_validation.rb
aws-sdk-dynamodb-1.38.0 lib/aws-sdk-dynamodb/plugins/crc32_validation.rb
aws-sdk-dynamodb-1.37.0 lib/aws-sdk-dynamodb/plugins/crc32_validation.rb
aws-sdk-dynamodb-1.36.0 lib/aws-sdk-dynamodb/plugins/crc32_validation.rb
aws-sdk-dynamodb-1.35.0 lib/aws-sdk-dynamodb/plugins/crc32_validation.rb
aws-sdk-dynamodb-1.34.0 lib/aws-sdk-dynamodb/plugins/crc32_validation.rb
aws-sdk-dynamodb-1.33.0 lib/aws-sdk-dynamodb/plugins/crc32_validation.rb
aws-sdk-dynamodb-1.32.0 lib/aws-sdk-dynamodb/plugins/crc32_validation.rb
aws-sdk-dynamodb-1.31.0 lib/aws-sdk-dynamodb/plugins/crc32_validation.rb