Class: BoltServer::BaseConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt_server/base_config.rb

Direct Known Subclasses

Config, PlanExecutor::Config

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ BaseConfig

Returns a new instance of BaseConfig.



44
45
46
47
48
# File 'lib/bolt_server/base_config.rb', line 44

def initialize(config = nil)
  @data = defaults
  @data = @data.merge(config.select { |key, _| config_keys.include?(key) }) if config
  @config_path = nil
end

Instance Method Details

#[](key) ⇒ Object



106
107
108
# File 'lib/bolt_server/base_config.rb', line 106

def [](key)
  @data[key]
end

#config_keysObject



8
9
10
11
# File 'lib/bolt_server/base_config.rb', line 8

def config_keys
  %w[host port ssl-cert ssl-key ssl-ca-cert
     ssl-cipher-suites loglevel logfile whitelist]
end

#defaultsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bolt_server/base_config.rb', line 17

def defaults
  { 'host' => '127.0.0.1',
    'loglevel' => 'notice',
    'ssl-cipher-suites' => %w[ECDHE-ECDSA-AES256-GCM-SHA384
                              ECDHE-RSA-AES256-GCM-SHA384
                              ECDHE-ECDSA-CHACHA20-POLY1305
                              ECDHE-RSA-CHACHA20-POLY1305
                              ECDHE-ECDSA-AES128-GCM-SHA256
                              ECDHE-RSA-AES128-GCM-SHA256
                              ECDHE-ECDSA-AES256-SHA384
                              ECDHE-RSA-AES256-SHA384
                              ECDHE-ECDSA-AES128-SHA256
                              ECDHE-RSA-AES128-SHA256] }
end

#env_keysObject



13
14
15
# File 'lib/bolt_server/base_config.rb', line 13

def env_keys
  %w[ssl-cert ssl-key ssl-ca-cert loglevel]
end

#load_env_configObject



69
70
71
# File 'lib/bolt_server/base_config.rb', line 69

def load_env_config
  raise "load_env_config should be defined in the service class"
end

#load_file_config(path) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bolt_server/base_config.rb', line 50

def load_file_config(path)
  @config_path = path
  begin
    # This lets us get the actual config values without needing to
    # know the service name
    parsed_hocon = Hocon.load(path)[service_name]
  rescue Hocon::ConfigError => e
    raise "Hocon data in '#{path}' failed to load.\n Error: '#{e.message}'"
  rescue Errno::EACCES
    raise "Your user doesn't have permission to read #{path}"
  end

  raise "Could not find service config at #{path}" if parsed_hocon.nil?

  parsed_hocon = parsed_hocon.select { |key, _| config_keys.include?(key) }

  @data = @data.merge(parsed_hocon)
end

#natural?(num) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/bolt_server/base_config.rb', line 73

def natural?(num)
  num.is_a?(Integer) && num.positive?
end

#required_keysObject



36
37
38
# File 'lib/bolt_server/base_config.rb', line 36

def required_keys
  ssl_keys
end

#service_nameObject



40
41
42
# File 'lib/bolt_server/base_config.rb', line 40

def service_name
  raise "Method service_name must be defined in the service class"
end

#ssl_keysObject



32
33
34
# File 'lib/bolt_server/base_config.rb', line 32

def ssl_keys
  %w[ssl-cert ssl-key ssl-ca-cert]
end

#validateObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/bolt_server/base_config.rb', line 77

def validate
  required_keys.each do |k|
    # Handled nested config
    if k.is_a?(Array)
      next unless @data.dig(*k).nil?
    else
      next unless @data[k].nil?
    end
    raise Bolt::ValidationError, "You must configure #{k} in #{@config_path}"
  end

  unless natural?(@data['port'])
    raise Bolt::ValidationError, "Configured 'port' must be a valid integer greater than 0"
  end
  ssl_keys.each do |sk|
    unless File.file?(@data[sk]) && File.readable?(@data[sk])
      raise Bolt::ValidationError, "Configured #{sk} must be a valid filepath"
    end
  end

  unless @data['ssl-cipher-suites'].is_a?(Array)
    raise Bolt::ValidationError, "Configured 'ssl-cipher-suites' must be an array of cipher suite names"
  end

  unless @data['whitelist'].nil? || @data['whitelist'].is_a?(Array)
    raise Bolt::ValidationError, "Configured 'whitelist' must be an array of names"
  end
end