Class: Elasticity::AwsRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticity/aws_request.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aws_access_key_id, aws_secret_access_key, options = {}) ⇒ AwsRequest

Returns a new instance of AwsRequest.



5
6
7
8
9
# File 'lib/elasticity/aws_request.rb', line 5

def initialize(aws_access_key_id, aws_secret_access_key, options = {})
  @access_key = aws_access_key_id
  @secret_key = aws_secret_access_key
  @options = {:secure => true}.merge(options)
end

Class Method Details

.aws_escape(param) ⇒ Object

(Used from RightScale’s right_aws gem) Escape a string according to Amazon’s rules. See: docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/index.html?REST_RESTAuth.html



42
43
44
45
46
# File 'lib/elasticity/aws_request.rb', line 42

def aws_escape(param)
  param.to_s.gsub(/([^a-zA-Z0-9._~-]+)/n) do
    '%' + $1.unpack('H2' * $1.size).join('%').upcase
  end
end

Instance Method Details

#aws_emr_request(params) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/elasticity/aws_request.rb', line 11

def aws_emr_request(params)
  host = @options[:region] ? "elasticmapreduce.#{@options[:region]}.amazonaws.com" : "elasticmapreduce.amazonaws.com"
  protocol = @options[:secure] ? "https" : "http"

  signed_params = sign_params(params, "GET", host, "/")
  signed_request = "#{protocol}://#{host}?#{signed_params}"
  RestClient.get signed_request
end

#sign_params(service_hash, http_verb, host, uri) ⇒ Object

(Used from RightScale’s right_aws gem.) EC2, SQS, SDB and EMR requests must be signed by this guy. See: docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/index.html?REST_RESTAuth.html

http://developer.amazonwebservices.com/connect/entry.jspa?externalID=1928


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/elasticity/aws_request.rb', line 24

def sign_params(service_hash, http_verb, host, uri)
  service_hash["AWSAccessKeyId"] = @access_key
  service_hash["Timestamp"] = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S.000Z")
  service_hash["SignatureVersion"] = "2"
  service_hash['SignatureMethod'] = 'HmacSHA256'
  canonical_string = service_hash.keys.sort.map do |key|
    "#{AwsRequest.aws_escape(key)}=#{AwsRequest.aws_escape(service_hash[key])}"
  end.join('&')
  string_to_sign = "#{http_verb.to_s.upcase}\n#{host.downcase}\n#{uri}\n#{canonical_string}"
  signature = AwsRequest.aws_escape(Base64.encode64(OpenSSL::HMAC.digest("sha256", @secret_key, string_to_sign)).strip)
  "#{canonical_string}&Signature=#{signature}"
end