Class: BoltServer::BaseConfig

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

Direct Known Subclasses

Config

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ BaseConfig

Returns a new instance of BaseConfig.



46
47
48
49
50
# File 'lib/bolt_server/base_config.rb', line 46

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



108
109
110
# File 'lib/bolt_server/base_config.rb', line 108

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

#config_keysObject



8
9
10
11
12
13
# 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 allowlist
     projects-dir environments-codedir
     environmentpath basemodulepath]
end

#defaultsObject



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

def defaults
  { 'host' => '127.0.0.1',
    'loglevel' => 'warn',
    '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



15
16
17
# File 'lib/bolt_server/base_config.rb', line 15

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

#load_env_configObject



71
72
73
# File 'lib/bolt_server/base_config.rb', line 71

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

#load_file_config(path) ⇒ Object



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

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)


75
76
77
# File 'lib/bolt_server/base_config.rb', line 75

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

#required_keysObject



38
39
40
# File 'lib/bolt_server/base_config.rb', line 38

def required_keys
  ssl_keys
end

#service_nameObject



42
43
44
# File 'lib/bolt_server/base_config.rb', line 42

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

#ssl_keysObject



34
35
36
# File 'lib/bolt_server/base_config.rb', line 34

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

#validateObject



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
105
106
# File 'lib/bolt_server/base_config.rb', line 79

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['allowlist'].nil? || @data['allowlist'].is_a?(Array)
    raise Bolt::ValidationError, "Configured 'allowlist' must be an array of names"
  end
end