Module: OverSIP::Config::Validators

Extended by:
Logger
Included in:
OverSIP::Config, ProxiesConfig
Defined in:
lib/oversip/config.rb,
lib/oversip/config_validators.rb

Overview

Pre-declaration of Validators module (defined in other file).

Constant Summary collapse

DOMAIN_REGEXP =
/^(([0-9a-zA-Z\-_])+\.)*([0-9a-zA-Z\-_])+$/
TLS_PEM_CHAIN_REGEXP =
/-{5}BEGIN CERTIFICATE-{5}\n.*?-{5}END CERTIFICATE-{5}\n/m

Instance Method Summary collapse

Methods included from Logger

fg_system_msg2str, load_methods, log_id

Instance Method Details

#boolean(value) ⇒ Object



15
16
17
# File 'lib/oversip/config_validators.rb', line 15

def boolean value
  value == true or value == false
end

#choices(value, choices) ⇒ Object



55
56
57
# File 'lib/oversip/config_validators.rb', line 55

def choices value, choices
  choices.include? value
end

#domain(value) ⇒ Object



51
52
53
# File 'lib/oversip/config_validators.rb', line 51

def domain value
  value =~ DOMAIN_REGEXP
end

#fixnum(value) ⇒ Object



23
24
25
# File 'lib/oversip/config_validators.rb', line 23

def fixnum value
  value.is_a? ::Fixnum
end

#greater_equal_than(value, minimum) ⇒ Object



63
64
65
# File 'lib/oversip/config_validators.rb', line 63

def greater_equal_than value, minimum
  value >= minimum  rescue false
end

#greater_than(value, minimum) ⇒ Object



59
60
61
# File 'lib/oversip/config_validators.rb', line 59

def greater_than value, minimum
  value > minimum  rescue false
end

#ipv4(value) ⇒ Object



31
32
33
34
# File 'lib/oversip/config_validators.rb', line 31

def ipv4 value
  return false  unless value.is_a? ::String
  ::OverSIP::Utils.ip_type(value) == :ipv4 and value != "0.0.0.0"
end

#ipv4_any(value) ⇒ Object



41
42
43
44
# File 'lib/oversip/config_validators.rb', line 41

def ipv4_any value
  return false  unless value.is_a? ::String
  ::OverSIP::Utils.ip_type(value) == :ipv4
end

#ipv6(value) ⇒ Object



36
37
38
39
# File 'lib/oversip/config_validators.rb', line 36

def ipv6 value
  return false  unless value.is_a? ::String
  ::OverSIP::Utils.ip_type(value) == :ipv6 and ::OverSIP::Utils.normalize_ipv6(value) != "::"
end

#ipv6_any(value) ⇒ Object



46
47
48
49
# File 'lib/oversip/config_validators.rb', line 46

def ipv6_any value
  return false  unless value.is_a? ::String
  ::OverSIP::Utils.ip_type(value) == :ipv6
end

#minor_equal_than(value, maximum) ⇒ Object



71
72
73
# File 'lib/oversip/config_validators.rb', line 71

def minor_equal_than value, maximum
  value <= maximum  rescue false
end

#minor_than(value, maximum) ⇒ Object



67
68
69
# File 'lib/oversip/config_validators.rb', line 67

def minor_than value, maximum
  value < maximum  rescue false
end

#port(value) ⇒ Object



27
28
29
# File 'lib/oversip/config_validators.rb', line 27

def port value
  fixnum(value) and value.between?(1,65536)
end

#readable_dir(dir) ⇒ Object



79
80
81
# File 'lib/oversip/config_validators.rb', line 79

def readable_dir dir
  ::File.directory?(dir) and ::File.readable?(dir)
end

#readable_file(file) ⇒ Object



75
76
77
# File 'lib/oversip/config_validators.rb', line 75

def readable_file file
  ::File.file?(file) and ::File.readable?(file)
end

#string(value) ⇒ Object



19
20
21
# File 'lib/oversip/config_validators.rb', line 19

def string value
  value.is_a? ::String
end

#tls_pem_chain(file) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/oversip/config_validators.rb', line 83

def tls_pem_chain file
  chain = ::File.read file
  pems = chain.scan(TLS_PEM_CHAIN_REGEXP).flatten
  pem_found = nil

  begin
    pems.each do |pem|
      ::OpenSSL::X509::Certificate.new pem
      pem_found = true
    end
  rescue => e
    log_system_error "#{e.class}: #{e.message}"
    return false
  end

  if pem_found
    return true
  else
    log_system_error "no valid X509 PEM found in the file"
    return false
  end
end

#tls_pem_private(file) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/oversip/config_validators.rb', line 106

def tls_pem_private file
  pem = ::File.read file
  key_classes = [::OpenSSL::PKey::RSA, ::OpenSSL::PKey::DSA]

  begin
    key_class = key_classes.shift
    key_class.new pem
    return true
  rescue => e
    retry if key_classes.any?
    log_system_error e.message
  end

  return false
end