Class: ConfigureS3Website::HttpHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/configure-s3-website/http_helper.rb

Class Method Summary collapse

Class Method Details

.call_api(path, method, body, config_source, hostname, digest, date, additional_headers = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/configure-s3-website/http_helper.rb', line 35

def self.call_api(path, method, body, config_source, hostname, digest, date, additional_headers = {})
  url = "https://#{hostname}#{path}"
  uri = URI.parse(url)
  req = method.new(uri.to_s)
  req.initialize_http_header({
    'Date' => date,
    'Content-Type' => '',
    'Content-Length' => body.length.to_s,
    'Authorization' => "AWS %s:%s" % [config_source.s3_access_key_id, digest]
  }.merge(additional_headers))
  req.body = body
  http = Net::HTTP.new(uri.host, uri.port)
  # http.set_debug_output $stderr
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  res = http.request(req)
  if res.code.to_i.between? 200, 299
    res
  else
    raise ConfigureS3Website::ErrorParser.create_error res.body
  end
end

.call_cloudfront_api(path, method, body, config_source, headers = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/configure-s3-website/http_helper.rb', line 18

def self.call_cloudfront_api(path, method, body, config_source, headers = {})
  date = Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S %Z")
  digest = create_cloudfront_digest(config_source, date)
  self.call_api(
    path,
    method,
    body,
    config_source,
    'cloudfront.amazonaws.com',
    digest,
    date,
    headers
  )
end

.call_s3_api(path, method, body, config_source) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/configure-s3-website/http_helper.rb', line 3

def self.call_s3_api(path, method, body, config_source)
  endpoint = Endpoint.by_config_source(config_source)
  date = Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S %Z")
  digest = create_s3_digest(path, method, config_source, date)
  self.call_api(
    path,
    method,
    body,
    config_source,
    endpoint.hostname,
    digest,
    date
  )
end

.create_cloudfront_digest(config_source, date) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/configure-s3-website/http_helper.rb', line 66

def self.create_cloudfront_digest(config_source, date)
  digest = Base64.encode64(
    OpenSSL::HMAC.digest(
      OpenSSL::Digest::Digest.new('sha1'),
      config_source.s3_secret_access_key,
      date
    )
  ).strip
end

.create_s3_digest(path, method, config_source, date) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/configure-s3-website/http_helper.rb', line 58

def self.create_s3_digest(path, method, config_source, date)
  digest = OpenSSL::Digest::Digest.new('sha1')
  method_string = method.to_s.match(/Net::HTTP::(\w+)/)[1].upcase
  can_string = "#{method_string}\n\n\n#{date}\n#{path}"
  hmac = OpenSSL::HMAC.digest(digest, config_source.s3_secret_access_key, can_string)
  signature = Base64.encode64(hmac).strip
end