aws-sdk-sqs 1.39.0 → 1.86.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,7 +6,6 @@ module Aws
6
6
  module SQS
7
7
  module Plugins
8
8
  class Md5s < Seahorse::Client::Plugin
9
-
10
9
  # @api private
11
10
  class Handler < Seahorse::Client::Handler
12
11
  def call(context)
@@ -26,16 +25,17 @@ module Aws
26
25
  'String' => 1,
27
26
  'Binary' => 2,
28
27
  'Number' => 1
29
- }
28
+ }.freeze
30
29
 
31
- DATA_TYPE = /\A(String|Binary|Number)(\..+)?\z/
30
+ DATA_TYPE = /\A(String|Binary|Number)(\..+)?\z/.freeze
32
31
 
33
32
  NORMALIZED_ENCODING = Encoding::UTF_8
34
33
 
35
34
  def validate_send_message(context, response)
36
35
  body = context.params[:message_body]
37
36
  attributes = context.params[:message_attributes]
38
- validate_single_message(body, attributes, response)
37
+ system_attributes = context.params[:message_system_attributes]
38
+ validate_single_message(body, attributes, system_attributes, response)
39
39
  end
40
40
 
41
41
  def validate_send_message_batch(context, response)
@@ -43,63 +43,87 @@ module Aws
43
43
  id = entry[:id]
44
44
  body = entry[:message_body]
45
45
  attributes = entry[:message_attributes]
46
+ system_attributes = entry[:message_system_attributes]
46
47
  message_response = response.successful.select { |r| r.id == id }[0]
47
48
  unless message_response.nil?
48
- validate_single_message(body, attributes, message_response)
49
+ validate_single_message(body, attributes, system_attributes, message_response)
49
50
  end
50
51
  end
51
52
  end
52
53
 
53
- def validate_single_message(body, attributes, response)
54
+ def validate_single_message(body, attributes, system_attributes, response)
54
55
  validate_body(body, response)
55
56
  unless attributes.nil? || attributes.empty?
56
57
  validate_attributes(attributes, response)
57
58
  end
59
+ unless system_attributes.nil? || system_attributes.empty?
60
+ validate_system_attributes(system_attributes, response)
61
+ end
58
62
  end
59
63
 
60
64
  def validate_body(body, response)
61
65
  calculated_md5 = md5_of_message_body(body)
62
66
  returned_md5 = response.md5_of_message_body
63
- if calculated_md5 != returned_md5
64
- error_message = mismatch_error_message(
65
- 'message body',
66
- calculated_md5,
67
- returned_md5,
68
- response)
69
- raise Aws::Errors::ChecksumError, error_message
70
- end
67
+ return unless calculated_md5 != returned_md5
68
+
69
+ error_message = mismatch_error_message(
70
+ 'message body',
71
+ calculated_md5,
72
+ returned_md5,
73
+ response
74
+ )
75
+ raise Aws::Errors::ChecksumError, error_message
71
76
  end
72
77
 
73
78
  def validate_attributes(attributes, response)
74
79
  calculated_md5 = md5_of_message_attributes(attributes)
75
80
  returned_md5 = response.md5_of_message_attributes
76
- if returned_md5 != calculated_md5
77
- error_message = mismatch_error_message(
78
- 'message attributes',
79
- calculated_md5,
80
- returned_md5,
81
- response)
82
- raise Aws::Errors::ChecksumError, error_message
83
- end
81
+ return unless returned_md5 != calculated_md5
82
+
83
+ error_message = mismatch_error_message(
84
+ 'message attributes',
85
+ calculated_md5,
86
+ returned_md5,
87
+ response
88
+ )
89
+ raise Aws::Errors::ChecksumError, error_message
90
+ end
91
+
92
+ def validate_system_attributes(system_attributes, response)
93
+ calculated_md5 = md5_of_message_system_attributes(system_attributes)
94
+ returned_md5 = response.md5_of_message_system_attributes
95
+ return unless returned_md5 != calculated_md5
96
+
97
+ error_message = mismatch_error_message(
98
+ 'message system attributes',
99
+ calculated_md5,
100
+ returned_md5,
101
+ response
102
+ )
103
+ raise Aws::Errors::ChecksumError, error_message
84
104
  end
85
105
 
86
106
  def md5_of_message_body(message_body)
87
107
  OpenSSL::Digest::MD5.hexdigest(message_body)
88
108
  end
89
109
 
110
+ # MD5 of Message Attributes and System Attributes are effectively
111
+ # the same calculation. However, keeping these as two methods because
112
+ # they are modeled as two different shapes.
113
+ ###
90
114
  def md5_of_message_attributes(message_attributes)
91
- encoded = { }
115
+ encoded = {}
92
116
  message_attributes.each do |name, attribute|
93
117
  name = name.to_s
94
118
  encoded[name] = String.new
95
119
  data_type_without_label = DATA_TYPE.match(attribute[:data_type])[1]
96
120
  encoded[name] << encode_length_and_bytes(name) <<
97
- encode_length_and_bytes(attribute[:data_type]) <<
98
- [TRANSPORT_TYPE_ENCODINGS[data_type_without_label]].pack('C'.freeze)
121
+ encode_length_and_bytes(attribute[:data_type]) <<
122
+ [TRANSPORT_TYPE_ENCODINGS[data_type_without_label]].pack('C')
99
123
 
100
- if attribute[:string_value] != nil
124
+ if !attribute[:string_value].nil?
101
125
  encoded[name] << encode_length_and_string(attribute[:string_value])
102
- elsif attribute[:binary_value] != nil
126
+ elsif !attribute[:binary_value].nil?
103
127
  encoded[name] << encode_length_and_bytes(attribute[:binary_value])
104
128
  end
105
129
  end
@@ -110,6 +134,30 @@ module Aws
110
134
  OpenSSL::Digest::MD5.hexdigest(buffer)
111
135
  end
112
136
 
137
+ def md5_of_message_system_attributes(message_system_attributes)
138
+ encoded = {}
139
+ message_system_attributes.each do |name, attribute|
140
+ name = name.to_s
141
+ encoded[name] = String.new
142
+ data_type_without_label = DATA_TYPE.match(attribute[:data_type])[1]
143
+ encoded[name] << encode_length_and_bytes(name) <<
144
+ encode_length_and_bytes(attribute[:data_type]) <<
145
+ [TRANSPORT_TYPE_ENCODINGS[data_type_without_label]].pack('C')
146
+
147
+ if !attribute[:string_value].nil?
148
+ encoded[name] << encode_length_and_string(attribute[:string_value])
149
+ elsif !attribute[:binary_value].nil?
150
+ encoded[name] << encode_length_and_bytes(attribute[:binary_value])
151
+ end
152
+ end
153
+
154
+ buffer = encoded.keys.sort.reduce(String.new) do |string, name|
155
+ string << encoded[name]
156
+ end
157
+ OpenSSL::Digest::MD5.hexdigest(buffer)
158
+ end
159
+ ###
160
+
113
161
  def encode_length_and_string(string)
114
162
  string = String.new(string)
115
163
  string.encode!(NORMALIZED_ENCODING)
@@ -117,7 +165,7 @@ module Aws
117
165
  end
118
166
 
119
167
  def encode_length_and_bytes(bytes)
120
- [bytes.bytesize, bytes].pack('L>a*'.freeze)
168
+ [bytes.bytesize, bytes].pack('L>a*')
121
169
  end
122
170
 
123
171
  def mismatch_error_message(section, local_md5, returned_md5, response)
@@ -154,13 +202,14 @@ not match.
154
202
  end
155
203
 
156
204
  def add_handlers(handlers, config)
157
- if config.verify_checksums
158
- handlers.add(Handler, {
159
- priority: 10 ,
160
- step: :validate,
161
- operations: [:send_message, :send_message_batch]
162
- })
163
- end
205
+ return unless config.verify_checksums
206
+
207
+ handlers.add(
208
+ Handler,
209
+ priority: 10,
210
+ step: :validate,
211
+ operations: %i[send_message send_message_batch]
212
+ )
164
213
  end
165
214
  end
166
215
  end
@@ -5,11 +5,10 @@ module Aws
5
5
  module Plugins
6
6
  # @api private
7
7
  class QueueUrls < Seahorse::Client::Plugin
8
-
8
+ # Extract region from a provided queue_url
9
9
  class Handler < Seahorse::Client::Handler
10
-
11
10
  def call(context)
12
- if queue_url = context.params[:queue_url]
11
+ if (queue_url = context.params[:queue_url])
13
12
  update_endpoint(context, queue_url)
14
13
  update_region(context, queue_url)
15
14
  end
@@ -21,28 +20,31 @@ module Aws
21
20
  end
22
21
 
23
22
  # If the region in the queue url is not the configured
24
- # region, then we will modify the request to have
25
- # a sigv4 signer for the proper region.
23
+ # region, then we will modify signing to use it
26
24
  def update_region(context, queue_url)
27
- if queue_region = parse_region(queue_url)
28
- if queue_region != context.config.region
29
- config = context.config.dup
30
- config.region = queue_region
31
- config.sigv4_region = queue_region
32
- config.sigv4_signer = Aws::Plugins::SignatureV4.build_signer(config)
33
- context.config = config
34
- end
25
+ if (queue_region = parse_region(queue_url)) &&
26
+ queue_region != context.config.region
27
+ context[:auth_scheme]['signingRegion'] = queue_region
35
28
  end
36
29
  end
37
30
 
38
31
  private
39
32
 
40
- # take the first component after service delimiter
41
- # https://sqs.us-east-1.amazonaws.com/1234567890/demo
42
- # https://vpce-x-y.sqs.us-east-1.vpce.amazonaws.com/1234567890/demo
33
+ # take the first component after the SQS service component
34
+ # Will return us-east-1 for:
35
+ # https://sqs.us-east-1.amazonaws.com/1234567890/demo
36
+ # https://vpce-x-y.sqs.us-east-1.vpce.amazonaws.com/1234567890/demo
37
+ # Will not return for:
38
+ # https://localstack-sqs.example.dev/queue/example
43
39
  def parse_region(url)
44
- parts = url.split('sqs.')
45
- parts[1].split('.').first if parts.size > 1
40
+ parts = URI.parse(url).host.split('.')
41
+ parts.each_with_index do |part, index|
42
+ if part == 'sqs'
43
+ # assume region is the part right after the 'sqs' part
44
+ return parts[index + 1]
45
+ end
46
+ end
47
+ nil # no region found
46
48
  end
47
49
 
48
50
  end