File: awsbase.rb

package info (click to toggle)
ruby-aws 2.10.2-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 748 kB
  • sloc: ruby: 7,748; makefile: 16
file content (615 lines) | stat: -rw-r--r-- 24,605 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
#
# Copyright (c) 2007-2008 RightScale Inc
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

# Test
module Aws
  require 'digest/md5'
  require 'pp'
  require 'cgi'
  require 'uri'
  require 'xmlsimple'
  require 'net/http'

  require_relative 'utils'
  require_relative 'errors'
  require_relative 'parsers'


  class AwsBenchmarkingBlock #:nodoc:
    attr_accessor :xml, :service

    def initialize
      # Benchmark::Tms instance for service (Ec2, S3, or SQS) access benchmarking.
      @service = Benchmark::Tms.new()
      # Benchmark::Tms instance for XML parsing benchmarking.
      @xml = Benchmark::Tms.new()
    end
  end

  class AwsNoChange < RuntimeError
  end

  class AwsBase

    # Amazon HTTP Error handling

    # Text, if found in an error message returned by AWS, indicates that this may be a transient
    # error. Transient errors are automatically retried with exponential back-off.
    AMAZON_PROBLEMS = ['internal service error',
                       'is currently unavailable',
                       'no response from',
                       'Please try again',
                       'InternalError',
                       'ServiceUnavailable', #from SQS docs
                       'Unavailable',
                       'This application is not currently available',
                       'InsufficientInstanceCapacity'
    ]
    @@amazon_problems = AMAZON_PROBLEMS
    # Returns a list of Amazon service responses which are known to be transient problems.
    # We have to re-request if we get any of them, because the problem will probably disappear.
    # By default this method returns the same value as the AMAZON_PROBLEMS const.
    def self.amazon_problems
      @@amazon_problems
    end

    # Sets the list of Amazon side problems.  Use in conjunction with the
    # getter to append problems.
    def self.amazon_problems=(problems_list)
      @@amazon_problems = problems_list
    end

  end

  module AwsBaseInterface

    DEFAULT_SIGNATURE_VERSION = '2'

    module ClassMethods

      def self.bench
        @@bench
      end

      def self.bench_xml
        @@bench.xml
      end

      def self.bench_s3
        @@bench.service
      end
    end

    @@caching = false

    def self.caching
      @@caching
    end

    def self.caching=(caching)
      @@caching = caching
    end

    # Current aws_access_key_id
    attr_reader :aws_access_key_id
    # Last HTTP request object
    attr_reader :last_request
    # Last HTTP response object
    attr_reader :last_response
    # Last AWS errors list (used by AWSErrorHandler)
    attr_accessor :last_errors
    # Last AWS request id (used by AWSErrorHandler)
    attr_accessor :last_request_id
    # Logger object
    attr_accessor :logger
    # Initial params hash
    attr_accessor :params
    # RightHttpConnection instance
    # there's a method now to get this since it could be per thread or what have you
    # attr_reader :connection
    # Cache
    attr_reader :cache
    # Signature version (all services except s3)
    attr_reader :signature_version

    def init(service_info, aws_access_key_id, aws_secret_access_key, params={}) #:nodoc:
      @params = params
      if Aws::Utils.blank?(aws_access_key_id) || Aws::Utils.blank?(aws_secret_access_key)
        raise AwsError.new("AWS access keys are required to operate on #{service_info[:name]}")
      end
      @aws_access_key_id = aws_access_key_id
      @aws_secret_access_key = aws_secret_access_key
      # if the endpoint was explicitly defined - then use it
      if @params[:endpoint_url]
        @params[:server] = URI.parse(@params[:endpoint_url]).host
        @params[:port] = URI.parse(@params[:endpoint_url]).port
        @params[:service] = URI.parse(@params[:endpoint_url]).path
        @params[:protocol] = URI.parse(@params[:endpoint_url]).scheme
        @params[:api_version] ||= service_info[:api_version]
        @params[:region] = nil
      else
        @params[:server] ||= service_info[:default_host]
        @params[:server] = "#{@params[:region]}.#{@params[:server]}" if @params[:region]
        @params[:port] ||= service_info[:default_port]
        @params[:service] ||= service_info[:default_service]
        @params[:protocol] ||= service_info[:default_protocol]
        @params[:api_version] ||= service_info[:api_version]
      end
      if !@params[:multi_thread].nil? && @params[:connection_mode].nil? # user defined this
        @params[:connection_mode] = @params[:multi_thread] ? :per_thread : :single
      end
      #      @params[:multi_thread] ||= defined?(AWS_DAEMON)
      @params[:connection_mode] ||= :default
      @params[:connection_mode] = :per_request if @params[:connection_mode] == :default
      @logger = @params[:logger]
      @logger = Rails.logger if !@logger && defined?(Rails) && defined?(Rails.logger)
      @logger = ::Rails.logger if !@logger && defined?(::Rails.logger)
      if !@logger
        @logger = Logger.new(STDOUT)
        @logger.level = Logger::INFO
      end
      @logger.debug "New #{self.class.name} using #{@params[:connection_mode].to_s}-connection mode"
      @error_handler = nil
      @cache = {}
      @signature_version = (params[:signature_version] || service_info[:signature_version] || DEFAULT_SIGNATURE_VERSION).to_s
    end

    def signed_service_params(aws_secret_access_key, service_hash, http_verb=nil, host=nil, service=nil)
      case signature_version.to_s
        when '0' then
          Utils::sign_request_v0(aws_secret_access_key, service_hash)
        when '1' then
          Utils::sign_request_v1(aws_secret_access_key, service_hash)
        when '2' then
          Utils::sign_request_v2(aws_secret_access_key, service_hash, http_verb, host, service)
        else
          raise AwsError.new("Unknown signature version (#{signature_version.to_s}) requested")
      end
    end

    def generate_request(action, params={})
      generate_request2(@aws_access_key_id, @aws_secret_access_key, action, @params[:api_version], @params, params)
    end

    # FROM SDB
    def generate_request2(aws_access_key, aws_secret_key, action, api_version, lib_params, user_params={}, options={}) #:nodoc:
                                                                                                                       # remove empty params from request
      user_params.delete_if { |key, value| value.nil? }
#            user_params.each_pair do |k,v|
#                user_params[k] = v.force_encoding("UTF-8")
#            end
#params_string  = params.to_a.collect{|key,val| key + "=#{CGI::escape(val.to_s)}" }.join("&")
# prepare service data
      service = lib_params[:service]

      now = Time.now.getutc
      service_hash = {"Action" => action,
                      "AWSAccessKeyId" => aws_access_key}
      service_hash.update("Version" => api_version) if api_version
      service_hash.update(user_params)
      headers = {}
      if signature_version == '3'
        service_hash["Timestamp"] = now.iso8601
        service_params = escape_params(service_hash)
        signature, algorithm = Aws::Utils.signature_version3(aws_secret_key, now)
        headers['X-Amzn-Authorization'] = "AWS3-HTTPS AWSAccessKeyId=#{aws_access_key}, Algorithm=#{algorithm.upcase}, Signature=#{signature}"
        headers['Date'] = now.httpdate
      else
#      puts 'service=' + service.to_s
        service_params = signed_service_params(aws_secret_key, service_hash, :get, lib_params[:server], lib_params[:service])
      end

      #
      # use POST method if the length of the query string is too large
      # see http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/MakingRESTRequests.html
      if service_params.size > 2000
        if signature_version == '2'
          # resign the request because HTTP verb is included into signature
          service_params = signed_service_params(aws_secret_key, service_hash, :post, lib_params[:server], service)
        end
        request = Net::HTTP::Post.new(service)
        request.body = service_params
        request['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'
      else
        request = Net::HTTP::Get.new("#{service}?#{service_params}")
      end
      headers.each_pair do |k, v|
        request[k] = v
      end
      #      puts "header=" + request['X-Amzn-Authorization']

      #puts "\n\n --------------- QUERY REQUEST TO AWS -------------- \n\n"
      #puts "#{@params[:service]}?#{service_params}\n\n"

      # prepare output hash
      {:request => request,
       :server => lib_params[:server],
       :port => lib_params[:port],
       :protocol => lib_params[:protocol]}
    end

    def escape_params(service_hash)
      canonical_string = service_hash.keys.sort.map do |key|
        "#{Aws::Utils.amz_escape(key)}=#{Aws::Utils.amz_escape(service_hash[key])}"
      end.join('&')
      canonical_string
    end

    def get_conn(connection_name, lib_params, logger)
#            thread = lib_params[:multi_thread] ? Thread.current : Thread.main
#            thread[connection_name] ||= Rightscale::HttpConnection.new(:exception => Aws::AwsError, :logger => logger)
#            conn = thread[connection_name]
#            return conn
      http_conn = nil
      conn_mode = lib_params[:connection_mode]

      params = {:exception => AwsError, :logger => logger}

      # Adds all parameters accepted by Rightscale::HttpConnection#new
      [:user_agent, :ca_file, :http_connection_retry_count,
       :http_connection_open_timeout, :http_connection_read_timeout,
       :http_connection_retry_delay
      ].each do |key|
        params[key] = lib_params[key] if lib_params.has_key?(key)
      end

      if conn_mode == :per_request
        http_conn = Rightscale::HttpConnection.new(params)

      elsif conn_mode == :per_thread || conn_mode == :single
        thread = conn_mode == :per_thread ? Thread.current : Thread.main
        thread[connection_name] ||= Rightscale::HttpConnection.new(params)
        http_conn = thread[connection_name]
#                ret = request_info_impl(http_conn, bench, request, parser, &block)
      end
      return http_conn

    end

    def close_conn(conn_name)
      conn_mode = @params[:connection_mode]
      if conn_mode == :per_thread || conn_mode == :single
        thread = conn_mode == :per_thread ? Thread.current : Thread.main
        if !thread[conn_name].nil?
          thread[conn_name].finish
          thread[conn_name] = nil
        end
      end
    end

    def connection
      get_conn(self.class.connection_name, self.params, self.logger)
    end

    def close_connection
      close_conn(self.class.connection_name)
    end


    def request_info2(request, parser, lib_params, connection_name, logger, bench, options={}, &block) #:nodoc:
      ret = nil
#            puts 'OPTIONS=' + options.inspect
      http_conn = get_conn(connection_name, lib_params, logger)
      begin
        # todo: this QueryTimeout retry should go into a SimpleDbErrorHandler, not here
        retry_count = 1
        count = 0
        while count <= retry_count
          puts 'RETRYING QUERY due to QueryTimeout...' if count > 0
          begin
            ret = request_info_impl(http_conn, bench, request, parser, options, &block)
          rescue Aws::AwsError => ex
            if !ex.include?(/QueryTimeout/) || count == retry_count
              raise ex
            end
          end
          break if ret
          count += 1
        end
      ensure
        http_conn.finish if http_conn && lib_params[:connection_mode] == :per_request
      end
      ret
    end

    # This is the latest and greatest now. Service must have connection_name defined.
    def request_info3(service_interface, request, parser, options, &block)
      request_info2(request, parser,
                    service_interface.params,
                    service_interface.class.connection_name,
                    service_interface.logger,
                    service_interface.class.bench,
                    options, &block)
    end


    # This is the direction we should head instead of writing our own parsers for everything, much simpler
    # params:
    #  - :group_tags => hash of indirection to eliminate, see: http://xml-simple.rubyforge.org/
    #  - :force_array => true for all or an array of tag names to force
    #  - :pull_out_array => an array of levels to dig into when generating return value (see rds.rb for example)
    def request_info_xml_simple(connection_name, lib_params, request, logger, params = {})

      connection = get_conn(connection_name, lib_params, logger)
      begin
        @last_request = request[:request]
        @last_response = nil

        response = connection.request(request)
        #       puts "response=" + response.body
        #            benchblock.service.add!{ response = connection.request(request) }
        # check response for errors...
        @last_response = response
        if response.is_a?(Net::HTTPSuccess)
          @error_handler = nil
#                benchblock.xml.add! { parser.parse(response) }
#                return parser.result
          force_array = params[:force_array] || false
# Force_array and group_tags don't work nice together so going to force array manually
          xml_simple_options = {"KeyToSymbol" => false, 'ForceArray' => false}
          xml_simple_options["GroupTags"] = params[:group_tags] if params[:group_tags]

#                { 'GroupTags' => { 'searchpath' => 'dir' }
#                'ForceArray' => %r(_list$)
          parsed = XmlSimple.xml_in(response.body, xml_simple_options)
# todo: we may want to consider stripping off a couple of layers when doing this, for instance:
# <DescribeDBInstancesResponse xmlns="http://rds.amazonaws.com/admin/2009-10-16/">
#  <DescribeDBInstancesResult>
#    <DBInstances>
# <DBInstance>....
# Strip it off and only return an array or hash of <DBInstance>'s (hash by identifier).
# would have to be able to make the RequestId available somehow though, perhaps some special array subclass which included that?
          unless force_array.is_a? Array
            force_array = []
          end
          parsed = symbolize(parsed, force_array)
#                puts 'parsed=' + parsed.inspect
          if params[:pull_out_array]
            ret = Aws::AwsResponseArray.new(parsed[:response_metadata])
            level_hash = parsed
            params[:pull_out_array].each do |x|
              level_hash = level_hash[x]
            end
            if level_hash.is_a? Hash # When there's only one
              ret << level_hash
            else # should be array
                 #                            puts 'level_hash=' + level_hash.inspect
              level_hash.each do |x|
                ret << x
              end
            end
          elsif params[:pull_out_single]
            # returns a single object
            ret = AwsResponseObjectHash.new(parsed[:response_metadata])
            level_hash = parsed
            params[:pull_out_single].each do |x|
              level_hash = level_hash[x]
            end
            ret.merge!(level_hash)
          else
            ret = parsed
          end
          return ret

        else
          @error_handler = AWSErrorHandler.new(self, nil, :errors_list => self.class.amazon_problems) unless @error_handler
          check_result = @error_handler.check(request)
          if check_result
            @error_handler = nil
            return check_result
          end
          request_text_data = "#{request[:server]}:#{request[:port]}#{request[:request].path}"
          raise AwsError2.new(@last_response.code, @last_request_id, request_text_data, @last_response.body)
        end
      ensure
        connection.finish if connection && lib_params[:connection_mode] == :per_request
      end

    end

    # This is the latest and greatest now. Service must have connection_name defined.
    def request_info_xml_simple3(service_interface, request, options)
      request_info_xml_simple(service_interface.class.connection_name,
                              service_interface.params,
                              request,
                              service_interface.logger,
                              options)
    end

    def symbolize(hash, force_array)
      ret = {}
      hash.keys.each do |key|
        val = hash[key]
        if val.is_a? Hash
          val = symbolize(val, force_array)
          if force_array.include? key
            val = [val]
          end
        elsif val.is_a? Array
          val = val.collect { |x| symbolize(x, force_array) }
        end
        ret[Aws::Utils.underscore(key).to_sym] = val
      end
      ret
    end

    # Returns +true+ if the describe_xxx responses are being cached
    def caching?
      @params.key?(:cache) ? @params[:cache] : @@caching
    end

    # Check if the aws function response hits the cache or not.
    # If the cache hits:
    # - raises an +AwsNoChange+ exception if +do_raise+ == +:raise+.
    # - returnes parsed response from the cache if it exists or +true+ otherwise.
    # If the cache miss or the caching is off then returns +false+.
    def cache_hits?(function, response, do_raise=:raise)
      result = false
      if caching?
        function = function.to_sym
        # get rid of requestId (this bad boy was added for API 2008-08-08+ and it is uniq for every response)
        response = response.sub(%r{<requestId>.+?</requestId>}, '')
        response_md5 =Digest::MD5.hexdigest(response).to_s
        # check for changes
        unless @cache[function] && @cache[function][:response_md5] == response_md5
          # well, the response is new, reset cache data
          update_cache(function, {:response_md5 => response_md5,
                                  :timestamp => Time.now,
                                  :hits => 0,
                                  :parsed => nil})
        else
          # aha, cache hits, update the data and throw an exception if needed
          @cache[function][:hits] += 1
          if do_raise == :raise
            raise(AwsNoChange, "Cache hit: #{function} response has not changed since "+
                "#{@cache[function][:timestamp].strftime('%Y-%m-%d %H:%M:%S')}, "+
                "hits: #{@cache[function][:hits]}.")
          else
            result = @cache[function][:parsed] || true
          end
        end
      end
      result
    end

    def update_cache(function, hash)
      (@cache[function.to_sym] ||= {}).merge!(hash) if caching?
    end

    def on_exception(options={:raise => true, :log => true}) # :nodoc:
      raise if $!.is_a?(AwsNoChange)
      AwsError::on_aws_exception(self, options)
    end

    # Return +true+ if this instance works in multi_thread mode and +false+ otherwise.
    def multi_thread
      @params[:multi_thread]
    end


    def request_info_impl(connection, benchblock, request, parser, options={}, &block) #:nodoc:
      connection = connection
      @last_request = request[:request]
      @last_response = nil
      response =nil
      blockexception = nil

#             puts 'OPTIONS2=' + options.inspect

      if (block != nil)
        # TRB 9/17/07 Careful - because we are passing in blocks, we get a situation where
        # an exception may get thrown in the block body (which is high-level
        # code either here or in the application) but gets caught in the
        # low-level code of HttpConnection.  The solution is not to let any
        # exception escape the block that we pass to HttpConnection::request.
        # Exceptions can originate from code directly in the block, or from user
        # code called in the other block which is passed to response.read_body.
        benchblock.service.add! do
          responsehdr = connection.request(request) do |response|
            #########
            begin
              @last_response = response
              if response.is_a?(Net::HTTPSuccess)
                @error_handler = nil
                response.read_body(&block)
              else
                @error_handler = AWSErrorHandler.new(self, parser, :errors_list => self.class.amazon_problems) unless @error_handler
                check_result = @error_handler.check(request, options)
                if check_result
                  @error_handler = nil
                  return check_result
                end
                request_text_data = "#{request[:server]}:#{request[:port]}#{request[:request].path}"
                raise AwsError.new(@last_errors, @last_response.code, @last_request_id, request_text_data)
              end
            rescue Exception => e
              blockexception = e
            end
          end
          #########

          #OK, now we are out of the block passed to the lower level
          if (blockexception)
            raise blockexception
          end
          benchblock.xml.add! do
            parser.parse(responsehdr)
          end
          return parser.result
        end
      else
        benchblock.service.add! { response = connection.request(request) }
        # check response for errors...
        @last_response = response
        if response.is_a?(Net::HTTPSuccess)
          @error_handler = nil
          benchblock.xml.add! { parser.parse(response) }
          return parser.result
        else
          @error_handler = AWSErrorHandler.new(self, parser, :errors_list => self.class.amazon_problems) unless @error_handler
          check_result = @error_handler.check(request, options)
          if check_result
            @error_handler = nil
            return check_result
          end
          request_text_data = "#{request[:server]}:#{request[:port]}#{request[:request].path}"
          raise AwsError.new(@last_errors, @last_response.code, @last_request_id, request_text_data)
        end
      end
    rescue
      @error_handler = nil
      raise
    end

    def request_cache_or_info(method, link, parser_class, benchblock, use_cache=true) #:nodoc:
                                                                                      # We do not want to break the logic of parsing hence will use a dummy parser to process all the standard
                                                                                      # steps (errors checking etc). The dummy parser does nothig - just returns back the params it received.
                                                                                      # If the caching is enabled and hit then throw  AwsNoChange.
                                                                                      # P.S. caching works for the whole images list only! (when the list param is blank)
                                                                                      # check cache
      response, params = request_info(link, RightDummyParser.new)
      cache_hits?(method.to_sym, response.body) if use_cache
      parser = parser_class.new(:logger => @logger)
      benchblock.xml.add! { parser.parse(response, params) }
      result = block_given? ? yield(parser) : parser.result
      # update parsed data
      update_cache(method.to_sym, :parsed => result) if use_cache
      result
    end

    # Returns Amazons request ID for the latest request
    def last_request_id
      @last_response && @last_response.body.to_s[%r{<requestId>(.+?)</requestId>}] && $1
    end

    def hash_params(prefix, list) #:nodoc:
      groups = {}
      list.each_index { |i| groups.update("#{prefix}.#{i+1}" => list[i]) } if list
      return groups
    end

  end


end