Class: Cloudfront::Rails::Importer

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudfront/rails/importer.rb,
lib/cloudfront/rails/importer/response_error.rb

Defined Under Namespace

Classes: ResponseError

Class Method Summary collapse

Class Method Details

.fetchObject



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
# File 'lib/cloudfront/rails/importer.rb', line 5

def fetch
  uri = URI("https://ip-ranges.amazonaws.com/ip-ranges.json")
   response = Net::HTTP.start(uri.host,
                             uri.port,
                             use_ssl: uri.scheme == 'https',
                             open_timeout: ::Rails.application.config.cloudfront.timeout,
                             read_timeout: ::Rails.application.config.cloudfront.timeout,
                             ssl_timeout:  ::Rails.application.config.cloudfront.timeout
                            ) do |http|
    http.request(Net::HTTP::Get.new(uri))
  end

  case response
  when Net::HTTPSuccess
    json = ActiveSupport::JSON.decode response.body

    trusted_ipv4_proxies = json["prefixes"].select do |details|
      details["service"] == 'CLOUDFRONT'
    end.map do |details|
      IPAddr.new(details["ip_prefix"])
    end

    trusted_ipv6_proxies = json["ipv6_prefixes"].select do |details|
      details["service"] == 'CLOUDFRONT'
    end.map do |details|
      IPAddr.new(details["ipv6_prefix"])
    end

    trusted_ipv4_proxies + trusted_ipv6_proxies
  else
    raise ResponseError.new(response)
  end
end

.fetch_with_cacheObject



38
39
40
41
42
43
# File 'lib/cloudfront/rails/importer.rb', line 38

def fetch_with_cache
  ::Rails.cache.fetch("cloudfront-rails-ips",
                      expires_in: ::Rails.application.config.cloudfront.expires_in) do
    self.fetch
  end
end