forked from active-elastic-job/active-elastic-job
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Verify that message was received correctly
Calculate MD5 digests for message body and message attributes of message that is sent to Amazon SQS. Compare calculated digests with digests returned by the response object. Close active-elastic-job#4
- Loading branch information
Showing
6 changed files
with
224 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
require 'digest' | ||
module ActiveElasticJob | ||
# This module provides methods that calculate the MD5 digest for Amazon | ||
# SQS message bodies and message attributes. | ||
# The digest can be used to verify that Amazon SQS received the message | ||
# correctly. | ||
# | ||
# Example: | ||
# | ||
# extend ActiveElasticJob::MD5MessageDigestCalculation | ||
# | ||
# resp = Aws::SQS::Client.new.send_message( | ||
# queue_url: queue_url, | ||
# message_body: body, | ||
# message_attributes: attributes | ||
# ) | ||
# | ||
# if resp.md5_of_message_body != md5_of_message_body(body) | ||
# raise "Returned digest of message body is invalid!" | ||
# end | ||
# | ||
# if resp.md5_of_message_attributes != md5_of_message_attributes(attributes) | ||
# raise "Returned digest of message attributes is invalid!" | ||
# end | ||
module MD5MessageDigestCalculation | ||
TRANSPORT_TYPE_ENCODINGS = { | ||
'String' => 1, | ||
'Binary' => 2, | ||
'Number' => 1 | ||
} | ||
|
||
CHARSET_ENCODING = Encoding::UTF_8 | ||
|
||
# Returns MD5 digest of +message_body+. | ||
def md5_of_message_body(message_body) | ||
OpenSSL::Digest::MD5.hexdigest(message_body) | ||
end | ||
|
||
# Returns MD5 digest of +message_attributes+. | ||
# | ||
# The calculation follows the official algorithm which | ||
# is specified by Amazon. | ||
def md5_of_message_attributes(message_attributes) | ||
encoded = message_attributes.each.with_object({ }) do |(name, v), hash| | ||
hash[name.to_s] = "" | ||
data_type = v['data_type'] || v[:data_type] | ||
|
||
hash[name.to_s] << encode_length_and_bytes(name.to_s) << | ||
encode_length_and_bytes(data_type) << | ||
[ TRANSPORT_TYPE_ENCODINGS[data_type] ].pack('C') | ||
|
||
if string_value = v['string_value'] || v[:string_value] | ||
hash[name.to_s] << encode_length_and_string(string_value) | ||
elsif binary_value = v['binary_value'] || v[:binary_value] | ||
hash[name.to_s] << encode_length_and_bytes(binary_value) | ||
end | ||
end | ||
|
||
buffer = encoded.keys.sort.reduce("") do |b, name| | ||
b << encoded[name] | ||
end | ||
OpenSSL::Digest::MD5.hexdigest(buffer) | ||
end | ||
|
||
private | ||
|
||
def encode_length_and_string(string) | ||
return '' if string.nil? | ||
string = String.new(string) | ||
string.encode!(CHARSET_ENCODING) | ||
encode_length_and_bytes(string) | ||
end | ||
|
||
def encode_length_and_bytes(bytes) | ||
return '' if bytes.nil? | ||
[ bytes.bytesize, bytes ].pack("L>a#{bytes.bytesize}") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
spec/active_elastic_job/md5_message_digest_calculation_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# encoding: UTF-8 | ||
|
||
require 'spec_helper' | ||
require 'securerandom' | ||
|
||
describe ActiveElasticJob::MD5MessageDigestCalculation do | ||
let(:queue_name) { "ActiveElasticJob-integration-testing" } | ||
let(:queue_url) { aws_sqs_client.create_queue(queue_name: queue_name).queue_url } | ||
let(:base_class) { | ||
Class.new { extend ActiveElasticJob::MD5MessageDigestCalculation } | ||
} | ||
|
||
describe "#md5_of_message_body" do | ||
let(:message_body) { JSON.dump(Helpers::TestJob.new.serialize) } | ||
let(:expected_hash) { | ||
aws_sqs_client.send_message( | ||
message_body: message_body, | ||
queue_url: queue_url | ||
).md5_of_message_body | ||
} | ||
subject { base_class.md5_of_message_body(message_body) } | ||
|
||
it { is_expected.to eq(expected_hash) } | ||
end | ||
|
||
describe "#md5_of_message_attributes" do | ||
let(:message_attributes) { | ||
{ | ||
"ccc" => { | ||
string_value: "test", | ||
data_type: "String" | ||
}, | ||
aaa: { | ||
binary_value: SecureRandom.random_bytes(12), | ||
data_type: "Binary" | ||
}, | ||
zzz: { | ||
data_type: "Number", | ||
string_value: "0230.01" | ||
}, | ||
"öther_encodings" => { | ||
data_type: "String", | ||
string_value: "Tüst".encode!("ISO-8859-1") | ||
} | ||
} | ||
} | ||
|
||
let(:expected_hash) { | ||
aws_sqs_client.send_message( | ||
message_body: "test", | ||
queue_url: queue_url, | ||
message_attributes: message_attributes | ||
).md5_of_message_attributes | ||
} | ||
|
||
subject { base_class.md5_of_message_attributes(message_attributes) } | ||
|
||
it { is_expected.to eq(expected_hash) } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters