Class: ForestLiana::IpWhitelist

Inherits:
Object
  • Object
show all
Defined in:
app/services/forest_liana/ip_whitelist.rb

Constant Summary collapse

@@use_ip_whitelist =
true
@@ip_whitelist_rules =
nil

Class Method Summary collapse

Class Method Details

.is_ip_valid(ip) ⇒ Object



36
37
38
39
40
41
42
# File 'app/services/forest_liana/ip_whitelist.rb', line 36

def self.is_ip_valid(ip)
  if @@use_ip_whitelist
    return ForestLiana::IpWhitelistChecker.is_ip_matches_any_rule(ip, @@ip_whitelist_rules)
  end

  true
end

.is_ip_whitelist_retrievedObject



32
33
34
# File 'app/services/forest_liana/ip_whitelist.rb', line 32

def self.is_ip_whitelist_retrieved
  !@@use_ip_whitelist || !@@ip_whitelist_rules.nil?
end

.retrieveObject



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
# File 'app/services/forest_liana/ip_whitelist.rb', line 6

def self.retrieve
  begin
    response = ForestLiana::ForestApiRequester.get('/liana/v1/ip-whitelist-rules')

    if response.is_a?(Net::HTTPOK)
      body = JSON.parse(response.body)
      ip_whitelist_data = body['data']['attributes']

      @@use_ip_whitelist = ip_whitelist_data['use_ip_whitelist']
      @@ip_whitelist_rules = ip_whitelist_data['rules']
      true
    else
      FOREST_LOGGER.error 'An error occured while retrieving your IP whitelist. Your Forest ' +
        'env_secret seems to be missing or unknown. Can you check that you properly set your ' +
        'Forest env_secret in the forest_liana initializer?'
      false
    end
  rescue => exception
    FOREST_REPORTER.report exception
    FOREST_LOGGER.error 'Cannot retrieve the IP Whitelist from the Forest server.'
    FOREST_LOGGER.error 'Which was caused by:'
    ForestLiana::Errors::ExceptionHelper.recursively_print(exception, margin: ' ', is_error: true)
    false
  end
end