aws 2.1.4 → 2.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/awsbase/right_awsbase.rb +76 -59
- metadata +1 -1
@@ -29,7 +29,7 @@ module Aws
|
|
29
29
|
require 'uri'
|
30
30
|
|
31
31
|
class AwsUtils #:nodoc:
|
32
|
-
@@digest1
|
32
|
+
@@digest1 = OpenSSL::Digest::Digest.new("sha1")
|
33
33
|
@@digest256 = nil
|
34
34
|
if OpenSSL::OPENSSL_VERSION_NUMBER > 0x00908000
|
35
35
|
@@digest256 = OpenSSL::Digest::Digest.new("sha256") rescue nil # Some installation may not support sha256
|
@@ -74,7 +74,7 @@ module Aws
|
|
74
74
|
# select a signing method (make an old openssl working with sha1)
|
75
75
|
# make 'HmacSHA256' to be a default one
|
76
76
|
service_hash['SignatureMethod'] = 'HmacSHA256' unless ['HmacSHA256', 'HmacSHA1'].include?(service_hash['SignatureMethod'])
|
77
|
-
service_hash['SignatureMethod'] = 'HmacSHA1'
|
77
|
+
service_hash['SignatureMethod'] = 'HmacSHA1' unless @@digest256
|
78
78
|
# select a digest
|
79
79
|
digest = (service_hash['SignatureMethod'] == 'HmacSHA256' ? @@digest256 : @@digest1)
|
80
80
|
# form string to sign
|
@@ -83,7 +83,7 @@ module Aws
|
|
83
83
|
end.join('&')
|
84
84
|
string_to_sign = "#{http_verb.to_s.upcase}\n#{host.downcase}\n#{uri}\n#{canonical_string}"
|
85
85
|
# sign the string
|
86
|
-
signature
|
86
|
+
signature = escape_sig(Base64.encode64(OpenSSL::HMAC.digest(digest, aws_secret_access_key, string_to_sign)).strip)
|
87
87
|
"#{canonical_string}&Signature=#{signature}"
|
88
88
|
end
|
89
89
|
|
@@ -191,9 +191,11 @@ module Aws
|
|
191
191
|
DEFAULT_SIGNATURE_VERSION = '2'
|
192
192
|
|
193
193
|
@@caching = false
|
194
|
+
|
194
195
|
def self.caching
|
195
196
|
@@caching
|
196
197
|
end
|
198
|
+
|
197
199
|
def self.caching=(caching)
|
198
200
|
@@caching = caching
|
199
201
|
end
|
@@ -222,21 +224,21 @@ module Aws
|
|
222
224
|
def init(service_info, aws_access_key_id, aws_secret_access_key, params={}) #:nodoc:
|
223
225
|
@params = params
|
224
226
|
raise AwsError.new("AWS access keys are required to operate on #{service_info[:name]}") \
|
225
|
-
|
226
|
-
@aws_access_key_id
|
227
|
+
if aws_access_key_id.blank? || aws_secret_access_key.blank?
|
228
|
+
@aws_access_key_id = aws_access_key_id
|
227
229
|
@aws_secret_access_key = aws_secret_access_key
|
228
230
|
# if the endpoint was explicitly defined - then use it
|
229
231
|
if @params[:endpoint_url]
|
230
|
-
@params[:server]
|
231
|
-
@params[:port]
|
232
|
-
@params[:service]
|
232
|
+
@params[:server] = URI.parse(@params[:endpoint_url]).host
|
233
|
+
@params[:port] = URI.parse(@params[:endpoint_url]).port
|
234
|
+
@params[:service] = URI.parse(@params[:endpoint_url]).path
|
233
235
|
@params[:protocol] = URI.parse(@params[:endpoint_url]).scheme
|
234
|
-
@params[:region]
|
236
|
+
@params[:region] = nil
|
235
237
|
else
|
236
|
-
@params[:server]
|
237
|
-
@params[:server]
|
238
|
-
@params[:port]
|
239
|
-
@params[:service]
|
238
|
+
@params[:server] ||= service_info[:default_host]
|
239
|
+
@params[:server] = "#{@params[:region]}.#{@params[:server]}" if @params[:region]
|
240
|
+
@params[:port] ||= service_info[:default_port]
|
241
|
+
@params[:service] ||= service_info[:default_service]
|
240
242
|
@params[:protocol] ||= service_info[:default_protocol]
|
241
243
|
end
|
242
244
|
if !@params[:multi_thread].nil? && @params[:connection_mode].nil? # user defined this
|
@@ -247,7 +249,7 @@ module Aws
|
|
247
249
|
@params[:connection_mode] = :per_request if @params[:connection_mode] == :default
|
248
250
|
@logger = @params[:logger]
|
249
251
|
@logger = RAILS_DEFAULT_LOGGER if !@logger && defined?(RAILS_DEFAULT_LOGGER)
|
250
|
-
@logger = Logger.new(STDOUT)
|
252
|
+
@logger = Logger.new(STDOUT) if !@logger
|
251
253
|
@logger.info "New #{self.class.name} using #{@params[:connection_mode].to_s}-connection mode"
|
252
254
|
@error_handler = nil
|
253
255
|
@cache = {}
|
@@ -288,9 +290,9 @@ module Aws
|
|
288
290
|
unless @cache[function] && @cache[function][:response_md5] == response_md5
|
289
291
|
# well, the response is new, reset cache data
|
290
292
|
update_cache(function, {:response_md5 => response_md5,
|
291
|
-
:timestamp
|
292
|
-
:hits
|
293
|
-
:parsed
|
293
|
+
:timestamp => Time.now,
|
294
|
+
:hits => 0,
|
295
|
+
:parsed => nil})
|
294
296
|
else
|
295
297
|
# aha, cache hits, update the data and throw an exception if needed
|
296
298
|
@cache[function][:hits] += 1
|
@@ -321,8 +323,8 @@ module Aws
|
|
321
323
|
end
|
322
324
|
|
323
325
|
def request_info_impl(connection, benchblock, request, parser, &block) #:nodoc:
|
324
|
-
@connection
|
325
|
-
@last_request
|
326
|
+
@connection = connection
|
327
|
+
@last_request = request[:request]
|
326
328
|
@last_response = nil
|
327
329
|
response=nil
|
328
330
|
blockexception = nil
|
@@ -345,7 +347,7 @@ module Aws
|
|
345
347
|
response.read_body(&block)
|
346
348
|
else
|
347
349
|
@error_handler = AWSErrorHandler.new(self, parser, :errors_list => self.class.amazon_problems) unless @error_handler
|
348
|
-
check_result
|
350
|
+
check_result = @error_handler.check(request)
|
349
351
|
if check_result
|
350
352
|
@error_handler = nil
|
351
353
|
return check_result
|
@@ -378,7 +380,7 @@ module Aws
|
|
378
380
|
return parser.result
|
379
381
|
else
|
380
382
|
@error_handler = AWSErrorHandler.new(self, parser, :errors_list => self.class.amazon_problems) unless @error_handler
|
381
|
-
check_result
|
383
|
+
check_result = @error_handler.check(request)
|
382
384
|
if check_result
|
383
385
|
@error_handler = nil
|
384
386
|
return check_result
|
@@ -413,6 +415,12 @@ module Aws
|
|
413
415
|
@last_response && @last_response.body.to_s[%r{<requestId>(.+?)</requestId>}] && $1
|
414
416
|
end
|
415
417
|
|
418
|
+
def hash_params(prefix, list) #:nodoc:
|
419
|
+
groups = {}
|
420
|
+
list.each_index{|i| groups.update("#{prefix}.#{i+1}"=>list[i])} if list
|
421
|
+
return groups
|
422
|
+
end
|
423
|
+
|
416
424
|
end
|
417
425
|
|
418
426
|
|
@@ -436,9 +444,9 @@ module Aws
|
|
436
444
|
attr_reader :request_data
|
437
445
|
|
438
446
|
def initialize(errors=nil, http_code=nil, request_id=nil, request_data=nil)
|
439
|
-
@errors
|
440
|
-
@request_id
|
441
|
-
@http_code
|
447
|
+
@errors = errors
|
448
|
+
@request_id = request_id
|
449
|
+
@http_code = http_code
|
442
450
|
@request_data = request_data
|
443
451
|
msg = @errors.is_a?(Array) ? @errors.map{|code, msg| "#{code}: #{msg}"}.join("; ") : @errors.to_s
|
444
452
|
msg += "\nREQUEST(#{@request_data})" unless @request_data.nil?
|
@@ -469,14 +477,14 @@ module Aws
|
|
469
477
|
puts error_text if options[:puts]
|
470
478
|
# Log the error
|
471
479
|
if options[:log]
|
472
|
-
request
|
480
|
+
request = aws.last_request ? aws.last_request.path : '-none-'
|
473
481
|
response = aws.last_response ? "#{aws.last_response.code} -- #{aws.last_response.message} -- #{aws.last_response.body}" : '-none-'
|
474
482
|
aws.logger.error error_text
|
475
483
|
aws.logger.error "Request was: #{request}"
|
476
484
|
aws.logger.error "Response was: #{response}"
|
477
485
|
end
|
478
486
|
end
|
479
|
-
raise if options[:raise]
|
487
|
+
raise if options[:raise] # re-raise an exception
|
480
488
|
return nil
|
481
489
|
end
|
482
490
|
|
@@ -494,33 +502,41 @@ module Aws
|
|
494
502
|
DEFAULT_CLOSE_ON_4XX_PROBABILITY = 10
|
495
503
|
|
496
504
|
@@reiteration_start_delay = 0.2
|
505
|
+
|
497
506
|
def self.reiteration_start_delay
|
498
507
|
@@reiteration_start_delay
|
499
508
|
end
|
509
|
+
|
500
510
|
def self.reiteration_start_delay=(reiteration_start_delay)
|
501
511
|
@@reiteration_start_delay = reiteration_start_delay
|
502
512
|
end
|
503
513
|
|
504
514
|
@@reiteration_time = 5
|
515
|
+
|
505
516
|
def self.reiteration_time
|
506
517
|
@@reiteration_time
|
507
518
|
end
|
519
|
+
|
508
520
|
def self.reiteration_time=(reiteration_time)
|
509
521
|
@@reiteration_time = reiteration_time
|
510
522
|
end
|
511
523
|
|
512
524
|
@@close_on_error = true
|
525
|
+
|
513
526
|
def self.close_on_error
|
514
527
|
@@close_on_error
|
515
528
|
end
|
529
|
+
|
516
530
|
def self.close_on_error=(close_on_error)
|
517
531
|
@@close_on_error = close_on_error
|
518
532
|
end
|
519
533
|
|
520
534
|
@@close_on_4xx_probability = DEFAULT_CLOSE_ON_4XX_PROBABILITY
|
535
|
+
|
521
536
|
def self.close_on_4xx_probability
|
522
537
|
@@close_on_4xx_probability
|
523
538
|
end
|
539
|
+
|
524
540
|
def self.close_on_4xx_probability=(close_on_4xx_probability)
|
525
541
|
@@close_on_4xx_probability = close_on_4xx_probability
|
526
542
|
end
|
@@ -531,26 +547,26 @@ module Aws
|
|
531
547
|
# :close_on_error = true | false
|
532
548
|
# :close_on_4xx_probability = 1-100
|
533
549
|
def initialize(aws, parser, params={}) #:nodoc:
|
534
|
-
@aws
|
535
|
-
@parser
|
536
|
-
@started_at
|
537
|
-
@stop_at
|
538
|
-
@errors_list
|
550
|
+
@aws = aws # Link to RightEc2 | RightSqs | RightS3 instance
|
551
|
+
@parser = parser # parser to parse Amazon response
|
552
|
+
@started_at = Time.now
|
553
|
+
@stop_at = @started_at + (params[:reiteration_time] || @@reiteration_time)
|
554
|
+
@errors_list = params[:errors_list] || []
|
539
555
|
@reiteration_delay = @@reiteration_start_delay
|
540
|
-
@retries
|
556
|
+
@retries = 0
|
541
557
|
# close current HTTP(S) connection on 5xx, errors from list and 4xx errors
|
542
|
-
@close_on_error
|
558
|
+
@close_on_error = params[:close_on_error].nil? ? @@close_on_error : params[:close_on_error]
|
543
559
|
@close_on_4xx_probability = params[:close_on_4xx_probability] || @@close_on_4xx_probability
|
544
560
|
end
|
545
561
|
|
546
562
|
# Returns false if
|
547
|
-
def check(request)
|
548
|
-
result
|
549
|
-
error_found
|
563
|
+
def check(request) #:nodoc:
|
564
|
+
result = false
|
565
|
+
error_found = false
|
550
566
|
redirect_detected= false
|
551
|
-
error_match
|
567
|
+
error_match = nil
|
552
568
|
last_errors_text = ''
|
553
|
-
response
|
569
|
+
response = @aws.last_response
|
554
570
|
# log error
|
555
571
|
request_text_data = "#{request[:server]}:#{request[:port]}#{request[:request].path}"
|
556
572
|
# is this a redirect?
|
@@ -563,13 +579,13 @@ module Aws
|
|
563
579
|
@aws.logger.warn("##### #{@aws.class.name} request: #{request_text_data} ####")
|
564
580
|
end
|
565
581
|
# Check response body: if it is an Amazon XML document or not:
|
566
|
-
if redirect_detected || (response.body && response.body[/<\?xml/])
|
582
|
+
if redirect_detected || (response.body && response.body[/<\?xml/]) # ... it is a xml document
|
567
583
|
@aws.class.bench_xml.add! do
|
568
584
|
error_parser = RightErrorResponseParser.new
|
569
585
|
error_parser.parse(response)
|
570
|
-
@aws.last_errors
|
586
|
+
@aws.last_errors = error_parser.errors
|
571
587
|
@aws.last_request_id = error_parser.requestID
|
572
|
-
last_errors_text
|
588
|
+
last_errors_text = @aws.last_errors.flatten.join("\n")
|
573
589
|
# on redirect :
|
574
590
|
if redirect_detected
|
575
591
|
location = response['location']
|
@@ -577,15 +593,15 @@ module Aws
|
|
577
593
|
@aws.logger.info("##### #{@aws.class.name} redirect requested: #{response.code} #{response.message} #####")
|
578
594
|
@aws.logger.info("##### New location: #{location} #####")
|
579
595
|
# ... fix the connection data
|
580
|
-
request[:server]
|
596
|
+
request[:server] = URI.parse(location).host
|
581
597
|
request[:protocol] = URI.parse(location).scheme
|
582
|
-
request[:port]
|
598
|
+
request[:port] = URI.parse(location).port
|
583
599
|
end
|
584
600
|
end
|
585
|
-
else
|
586
|
-
@aws.last_errors
|
601
|
+
else # ... it is not a xml document(probably just a html page?)
|
602
|
+
@aws.last_errors = [[response.code, "#{response.message} (#{request_text_data})"]]
|
587
603
|
@aws.last_request_id = '-undefined-'
|
588
|
-
last_errors_text
|
604
|
+
last_errors_text = response.message
|
589
605
|
end
|
590
606
|
# now - check the error
|
591
607
|
unless redirect_detected
|
@@ -690,7 +706,7 @@ module Aws
|
|
690
706
|
end
|
691
707
|
end
|
692
708
|
|
693
|
-
class AwsParser
|
709
|
+
class AwsParser #:nodoc:
|
694
710
|
# default parsing library
|
695
711
|
DEFAULT_XML_LIBRARY = 'rexml'
|
696
712
|
# a list of supported parsers
|
@@ -700,20 +716,21 @@ module Aws
|
|
700
716
|
def self.xml_lib
|
701
717
|
@@xml_lib
|
702
718
|
end
|
719
|
+
|
703
720
|
def self.xml_lib=(new_lib_name)
|
704
721
|
@@xml_lib = new_lib_name
|
705
722
|
end
|
706
723
|
|
707
724
|
attr_accessor :result
|
708
|
-
attr_reader
|
725
|
+
attr_reader :xmlpath
|
709
726
|
attr_accessor :xml_lib
|
710
727
|
|
711
728
|
def initialize(params={})
|
712
729
|
@xmlpath = ''
|
713
|
-
@result
|
714
|
-
@text
|
730
|
+
@result = false
|
731
|
+
@text = ''
|
715
732
|
@xml_lib = params[:xml_lib] || @@xml_lib
|
716
|
-
@logger
|
733
|
+
@logger = params[:logger]
|
717
734
|
reset
|
718
735
|
end
|
719
736
|
|
@@ -769,15 +786,15 @@ module Aws
|
|
769
786
|
# Parse the xml text
|
770
787
|
case @xml_lib
|
771
788
|
when 'libxml'
|
772
|
-
xml
|
789
|
+
xml = XML::SaxParser.new
|
773
790
|
xml.string = xml_text
|
774
791
|
# check libxml-ruby version
|
775
792
|
if XML::Parser::VERSION >= '0.5.1.0'
|
776
793
|
xml.callbacks = RightSaxParserCallback.new(self)
|
777
794
|
else
|
778
795
|
xml.on_start_element{|name, attr_hash| self.tag_start(name, attr_hash)}
|
779
|
-
xml.on_characters{
|
780
|
-
xml.on_end_element{
|
796
|
+
xml.on_characters{ |text| self.text(text)}
|
797
|
+
xml.on_end_element{ |name| self.tag_end(name)}
|
781
798
|
end
|
782
799
|
xml.parse
|
783
800
|
else
|
@@ -829,7 +846,7 @@ module Aws
|
|
829
846
|
#</Error>
|
830
847
|
|
831
848
|
class RightErrorResponseParser < AwsParser #:nodoc:
|
832
|
-
attr_accessor :errors
|
849
|
+
attr_accessor :errors # array of hashes: error/message
|
833
850
|
attr_accessor :requestID
|
834
851
|
# attr_accessor :endpoint, :host_id, :bucket
|
835
852
|
def tagend(name)
|
@@ -837,14 +854,14 @@ module Aws
|
|
837
854
|
when 'RequestID';
|
838
855
|
@requestID = @text
|
839
856
|
when 'Code';
|
840
|
-
@code
|
857
|
+
@code = @text
|
841
858
|
when 'Message';
|
842
|
-
@message
|
859
|
+
@message = @text
|
843
860
|
# when 'Endpoint' ; @endpoint = @text
|
844
861
|
# when 'HostId' ; @host_id = @text
|
845
862
|
# when 'Bucket' ; @bucket = @text
|
846
863
|
when 'Error';
|
847
|
-
@errors
|
864
|
+
@errors << [ @code, @message ]
|
848
865
|
end
|
849
866
|
end
|
850
867
|
|
@@ -855,7 +872,7 @@ module Aws
|
|
855
872
|
|
856
873
|
# Dummy parser - does nothing
|
857
874
|
# Returns the original params back
|
858
|
-
class RightDummyParser
|
875
|
+
class RightDummyParser # :nodoc:
|
859
876
|
attr_accessor :result
|
860
877
|
|
861
878
|
def parse(response, params={})
|