Class: PactBroker::Webhooks::CheckHostWhitelist

Inherits:
Object
  • Object
show all
Defined in:
lib/pact_broker/webhooks/check_host_whitelist.rb

Class Method Summary collapse

Class Method Details

.call(host, whitelist = PactBroker.configuration.webhook_host_whitelist) ⇒ Object



7
8
9
# File 'lib/pact_broker/webhooks/check_host_whitelist.rb', line 7

def self.call(host, whitelist = PactBroker.configuration.webhook_host_whitelist)
  whitelist.select{ | whitelist_host | match?(host, whitelist_host) }
end

.host_matches_domain_with_wildcard(host, whitelist_domain) ⇒ Object



37
38
39
# File 'lib/pact_broker/webhooks/check_host_whitelist.rb', line 37

def self.host_matches_domain_with_wildcard(host, whitelist_domain)
  OpenSSL::SSL.verify_hostname(host, whitelist_domain)
end

.host_matches_regexp(host, whitelist_regexp) ⇒ Object



33
34
35
# File 'lib/pact_broker/webhooks/check_host_whitelist.rb', line 33

def self.host_matches_regexp(host, whitelist_regexp)
  host =~ whitelist_regexp
end

.ip_address_matches_range(host, maybe_whitelist_range) ⇒ Object



29
30
31
# File 'lib/pact_broker/webhooks/check_host_whitelist.rb', line 29

def self.ip_address_matches_range(host, maybe_whitelist_range)
  parse_ip_address(maybe_whitelist_range) === parse_ip_address(host)
end

.match?(host, whitelist_host) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/pact_broker/webhooks/check_host_whitelist.rb', line 11

def self.match?(host, whitelist_host)
  if parse_ip_address(host)
    ip_address_matches_range(host, whitelist_host)
  elsif whitelist_host.is_a?(Regexp)
    host_matches_regexp(host, whitelist_host)
  elsif whitelist_host.start_with?("*")
    OpenSSL::SSL.verify_hostname(host, whitelist_host)
  else
    host == whitelist_host
  end
end

.parse_ip_address(addr) ⇒ Object



23
24
25
26
27
# File 'lib/pact_broker/webhooks/check_host_whitelist.rb', line 23

def self.parse_ip_address(addr)
  IPAddr.new(addr)
rescue IPAddr::Error
  nil
end