class Aws::Plugins::SQSMd5s::Handler

@api private

Constants

DATA_TYPE
NORMALIZED_ENCODING
TRANSPORT_TYPE_ENCODINGS

Public Instance Methods

call(context) click to toggle source
# File lib/aws-sdk-core/plugins/sqs_md5s.rb, line 16
def call(context)
  @handler.call(context).on_success do |response|
    case context.operation_name
    when :send_message
      validate_send_message(context, response)
    when :send_message_batch
      validate_send_message_batch(context, response)
    end
  end
end

Private Instance Methods

encode_length_and_bytes(bytes) click to toggle source
# File lib/aws-sdk-core/plugins/sqs_md5s.rb, line 121
def encode_length_and_bytes(bytes)
  [bytes.bytesize, bytes].pack('L>a*'.freeze)
end
encode_length_and_string(string) click to toggle source
# File lib/aws-sdk-core/plugins/sqs_md5s.rb, line 115
def encode_length_and_string(string)
  string = String.new(string)
  string.encode!(NORMALIZED_ENCODING)
  encode_length_and_bytes(string)
end
md5_of_message_attributes(message_attributes) click to toggle source
# File lib/aws-sdk-core/plugins/sqs_md5s.rb, line 92
def md5_of_message_attributes(message_attributes)
  encoded = { }
  message_attributes.each do |name, attribute|
    name = name.to_s
    encoded[name] = String.new
    data_type_without_label = DATA_TYPE.match(attribute[:data_type])[1]
    encoded[name] << encode_length_and_bytes(name) <<
    encode_length_and_bytes(attribute[:data_type]) <<
    [TRANSPORT_TYPE_ENCODINGS[data_type_without_label]].pack('C'.freeze)

    if attribute[:string_value] != nil
      encoded[name] << encode_length_and_string(attribute[:string_value])
    elsif attribute[:binary_value] != nil
      encoded[name] << encode_length_and_bytes(attribute[:binary_value])
    end
  end

  buffer = encoded.keys.sort.reduce(String.new) do |string, name|
    string << encoded[name]
  end
  OpenSSL::Digest::MD5.hexdigest(buffer)
end
md5_of_message_body(message_body) click to toggle source
# File lib/aws-sdk-core/plugins/sqs_md5s.rb, line 88
def md5_of_message_body(message_body)
  OpenSSL::Digest::MD5.hexdigest(message_body)
end
mismatch_error_message(section, local_md5, returned_md5, response) click to toggle source
# File lib/aws-sdk-core/plugins/sqs_md5s.rb, line 125
def mismatch_error_message(section, local_md5, returned_md5, response)
  m = "MD5 returned by SQS does not match " <<
  "the calculation on the original request. ("

  if response.respond_to?(:id) && !response.id.nil?
    m << "Message ID: #{response.id}, "
  end

  m << "MD5 calculated by the #{section}: " <<
  "'#{local_md5}', MD5 checksum returned: '#{returned_md5}')"
end
validate_attributes(attributes, response) click to toggle source
# File lib/aws-sdk-core/plugins/sqs_md5s.rb, line 75
def validate_attributes(attributes, response)
  calculated_md5 = md5_of_message_attributes(attributes)
  returned_md5 = response.md5_of_message_attributes
  if returned_md5 != calculated_md5
    error_message =  mismatch_error_message(
      'message attributes',
      calculated_md5,
      returned_md5,
      response)
    raise Aws::Errors::ChecksumError, error_message
  end
end
validate_body(body, response) click to toggle source
# File lib/aws-sdk-core/plugins/sqs_md5s.rb, line 62
def validate_body(body, response)
  calculated_md5 = md5_of_message_body(body)
  returned_md5 = response.md5_of_message_body
  if calculated_md5 != returned_md5
    error_message =  mismatch_error_message(
      'message body',
      calculated_md5,
      returned_md5,
      response)
    raise Aws::Errors::ChecksumError, error_message
  end
end
validate_send_message(context, response) click to toggle source
# File lib/aws-sdk-core/plugins/sqs_md5s.rb, line 39
def validate_send_message(context, response)
  body = context.params[:message_body]
  attributes = context.params[:message_attributes]
  validate_single_message(body, attributes, response)
end
validate_send_message_batch(context, response) click to toggle source
# File lib/aws-sdk-core/plugins/sqs_md5s.rb, line 45
def validate_send_message_batch(context, response)
  context.params[:entries].each do |entry|
    id = entry[:id]
    body = entry[:message_body]
    attributes = entry[:message_attributes]
    message_response = response.successful.select { |r| r.id == id }[0]
    unless message_response.nil?
      validate_single_message(body, attributes, message_response)
    end
  end
end
validate_single_message(body, attributes, response) click to toggle source
# File lib/aws-sdk-core/plugins/sqs_md5s.rb, line 57
def validate_single_message(body, attributes, response)
  validate_body(body, response)
  validate_attributes(attributes, response) unless attributes.nil?
end