Module: Auth::Centric::Firewall

Includes:
Common::Settings
Defined in:
lib/auth/centric/firewall.rb,
lib/auth/centric/firewall/version.rb,
lib/auth/centric/firewall/constants.rb,
lib/auth/centric/firewall/capture_request.rb,
lib/auth/centric/firewall/internet_protocol.rb,
sig/auth/centric/firewall.rbs,
sig/auth/centric/firewall/capture_request.rbs,
sig/auth/centric/firewall/internet_protocol.rbs

Overview

Client firewall module

Defined Under Namespace

Classes: CaptureRequest, Error, InternetProtocol

Constant Summary collapse

VERSION =

Returns:

  • (string)
'0.2.7'
IGNORE_HEADER_KEYS =

Returns:

  • ([])
%w[
  HTTP_HOST
  HTTP_REFERER
  HTTP_IF_NONE_MATCH
  HTTP_CACHE_CONTROL
  ORIGINAL_FULLPATH
  PATH_INFO
  QUERY_STRING
  REMOTE_ADDR
  REQUEST_URI
  REQUEST_PATH
  REQUEST_METHOD
  SERVER_NAME
  SERVER_SOFTWARE
  warden
].freeze
IGNORE_IP =

Returns:

  • ([])
%w[
  0.0.0.0
  127.0.0.1
  127.0.0.2
].freeze
IGNORE_REQUEST =

Returns:

  • ([])
%w[/ delayed_job favicon.ico robots.txt ads.txt humans.txt].freeze

Instance Method Summary collapse

Methods included from Common::Settings

#apikey, #app_id, #enabled?, #host, #ip, #timeout_seconds

Instance Method Details

#capture_pathstring

Returns:

  • (string)


79
80
81
# File 'lib/auth/centric/firewall.rb', line 79

def capture_path
  @capture_path ||= [host, 'api/v1/incoming_requests/capture'].join('/')
end

#ip_status_path(ip_address, app_id, url) ⇒ string

Returns:

  • (string)


72
73
74
75
76
77
# File 'lib/auth/centric/firewall.rb', line 72

def ip_status_path(ip_address, app_id, url)
  [
    host,
    "api/v1/internet_protocols/status?ip=#{ip_address}&app_id=#{app_id}&url=#{url}"
  ].join('/')
end

#log_firewall(request, forced: false, exception: nil) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/auth/centric/firewall.rb', line 20

def log_firewall(request, forced: false, exception: nil)
  return true unless enabled?

  unless forced
    return true if IGNORE_IP.include?(request.remote_ip)
    return true if IGNORE_REQUEST.include?(request.original_fullpath)
  end

  cr = CaptureRequest.new(request)
  payload = { request: cr.as_json, exception: }

  http = HTTP
         .timeout(timeout_seconds)
         .headers(apikey:)
         .post(capture_path, json: payload)

  http.status == 200
rescue HTTP::TimeoutError
  true
end

#valid_ip?(request, forced: false) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/auth/centric/firewall.rb', line 41

def valid_ip?(request, forced: false)
  return true unless enabled?

  ip_address = InternetProtocol.new(request).ip
  return true if !forced && IGNORE_IP.include?(ip_address)

  original_fullpath = if request.original_fullpath.length == 1
                        request.original_fullpath
                      else
                        request.original_fullpath[1...]
                      end

  http = HTTP
         .timeout(timeout_seconds)
         .headers(apikey:)
         .get(ip_status_path(ip_address, app_id, original_fullpath))

  case http.status
    when 200, 202
      true
    when 403
      false
    else
      raise "#{http.status}: #{http.body}"
  end
rescue HTTP::TimeoutError
  true
end