Module: Nephelae::AWS

Defined in:
lib/nephelae/cloud_watch/simple_aws.rb

Class Method Summary collapse

Class Method Details

.escape(string) ⇒ Object



8
9
10
11
12
# File 'lib/nephelae/cloud_watch/simple_aws.rb', line 8

def self.escape(string)
  string.gsub(/([^a-zA-Z0-9_.\-~]+)/) {
    "%" + $1.unpack("H2" * $1.bytesize).join("%").upcase
  }
end

.get_instance_idObject



50
51
52
53
# File 'lib/nephelae/cloud_watch/simple_aws.rb', line 50

def self.get_instance_id
  Excon.get('http://169.254.169.254/latest/meta-data/instance-id').body
  #"i-f746ec92"
end

.hmac_sign(string_to_sign, key) ⇒ Object



14
15
16
17
# File 'lib/nephelae/cloud_watch/simple_aws.rb', line 14

def self.hmac_sign(string_to_sign, key)
  digest = OpenSSL::Digest::Digest.new('sha256')
  OpenSSL::HMAC.digest(digest, key, string_to_sign)
end

.request(url, params, &block) ⇒ Object



45
46
47
48
# File 'lib/nephelae/cloud_watch/simple_aws.rb', line 45

def self.request(url, params, &block)
  excon = Excon.new(url, params)
  excon.request(params, &block)
end

.signed_params(params, options = {}) ⇒ Object



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
# File 'lib/nephelae/cloud_watch/simple_aws.rb', line 19

def self.signed_params(params, options = {})
  params.merge!({
    'AWSAccessKeyId'    => options[:aws_access_key_id],
    'SignatureMethod'   => 'HmacSHA256',
    'SignatureVersion'  => '2',
    'Timestamp'         => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
    'Version'           => options[:version]
  })

  params.merge!({
    'SecurityToken'     => options[:aws_session_token]
  }) if options[:aws_session_token]

  body = ''
  for key in params.keys.sort
    unless (value = params[key]).nil?
      body << "#{key}=#{escape(value.to_s)}&"
    end
  end
  string_to_sign = "POST\n#{options[:host]}:#{options[:port]}\n#{options[:path]}\n" << body.chop
  signed_string = hmac_sign(string_to_sign, options[:aws_secret_access_key])
  body << "Signature=#{escape(Base64.encode64(signed_string).chomp!)}"

  body
end