|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | | -class ConfigurationManager |
4 | | - DEFAULT_CONFIG_FILE = 'securenative.yml' |
5 | | - CUSTOM_CONFIG_FILE_ENV_NAME = 'SECURENATIVE_CONFIG_FILE' |
6 | | - @config = nil |
7 | | - |
8 | | - def self.read_resource_file(resource_path) |
9 | | - properties = {} |
10 | | - begin |
11 | | - @config = YAML.load_file(resource_path) |
12 | | - properties = @config unless @config.nil? |
13 | | - rescue StandardError => e |
14 | | - SecureNative::Log.error("Could not parse securenative.config file #{resource_path}; #{e}") |
| 3 | +module SecureNative |
| 4 | + module Config |
| 5 | + class ConfigurationManager |
| 6 | + DEFAULT_CONFIG_FILE = 'securenative.yml' |
| 7 | + CUSTOM_CONFIG_FILE_ENV_NAME = 'SECURENATIVE_CONFIG_FILE' |
| 8 | + @config = nil |
| 9 | + |
| 10 | + def self.read_resource_file(resource_path) |
| 11 | + properties = {} |
| 12 | + begin |
| 13 | + @config = YAML.load_file(resource_path) |
| 14 | + properties = @config unless @config.nil? |
| 15 | + rescue StandardError => e |
| 16 | + SecureNative::Log.error("Could not parse securenative.config file #{resource_path}; #{e}") |
| 17 | + end |
| 18 | + properties |
| 19 | + end |
| 20 | + |
| 21 | + def self._get_resource_path(env_name) |
| 22 | + Env.fetch(env_name, ENV[DEFAULT_CONFIG_FILE]) |
| 23 | + end |
| 24 | + |
| 25 | + def self.config_builder |
| 26 | + SecureNative::Config::ConfigurationBuilder.new |
| 27 | + end |
| 28 | + |
| 29 | + def self._get_env_or_default(properties, key, default) |
| 30 | + return ENV[key] if ENV[key] |
| 31 | + return properties[key] if properties[key] |
| 32 | + |
| 33 | + default |
| 34 | + end |
| 35 | + |
| 36 | + def self.load_config |
| 37 | + options = SecureNative::Config::ConfigurationBuilder.default_securenative_options |
| 38 | + |
| 39 | + resource_path = DEFAULT_CONFIG_FILE |
| 40 | + resource_path = ENV[CUSTOM_CONFIG_FILE_ENV_NAME] unless ENV[CUSTOM_CONFIG_FILE_ENV_NAME].nil? |
| 41 | + |
| 42 | + properties = read_resource_file(resource_path) |
| 43 | + |
| 44 | + SecureNative::Config::ConfigurationBuilder.new(api_key: _get_env_or_default(properties, 'SECURENATIVE_API_KEY', options.api_key), |
| 45 | + api_url: _get_env_or_default(properties, 'SECURENATIVE_API_URL', options.api_url), |
| 46 | + interval: _get_env_or_default(properties, 'SECURENATIVE_INTERVAL', options.interval), |
| 47 | + max_events: _get_env_or_default(properties, 'SECURENATIVE_MAX_EVENTS', options.max_events), |
| 48 | + timeout: _get_env_or_default(properties, 'SECURENATIVE_TIMEOUT', options.timeout), |
| 49 | + auto_send: _get_env_or_default(properties, 'SECURENATIVE_AUTO_SEND', options.auto_send), |
| 50 | + disable: _get_env_or_default(properties, 'SECURENATIVE_DISABLE', options.disable), |
| 51 | + log_level: _get_env_or_default(properties, 'SECURENATIVE_LOG_LEVEL', options.log_level), |
| 52 | + fail_over_strategy: _get_env_or_default(properties, 'SECURENATIVE_FAILOVER_STRATEGY', options.fail_over_strategy), |
| 53 | + proxy_headers: _get_env_or_default(properties, 'SECURENATIVE_PROXY_HEADERS', options.proxy_headers)) |
| 54 | + end |
15 | 55 | end |
16 | | - properties |
17 | | - end |
18 | | - |
19 | | - def self._get_resource_path(env_name) |
20 | | - Env.fetch(env_name, ENV[DEFAULT_CONFIG_FILE]) |
21 | | - end |
22 | | - |
23 | | - def self.config_builder |
24 | | - ConfigurationBuilder.new |
25 | | - end |
26 | | - |
27 | | - def self._get_env_or_default(properties, key, default) |
28 | | - return ENV[key] if ENV[key] |
29 | | - return properties[key] if properties[key] |
30 | | - |
31 | | - default |
32 | | - end |
33 | | - |
34 | | - def self.load_config |
35 | | - options = ConfigurationBuilder.default_securenative_options |
36 | | - |
37 | | - resource_path = DEFAULT_CONFIG_FILE |
38 | | - resource_path = ENV[CUSTOM_CONFIG_FILE_ENV_NAME] unless ENV[CUSTOM_CONFIG_FILE_ENV_NAME].nil? |
39 | | - |
40 | | - properties = read_resource_file(resource_path) |
41 | | - |
42 | | - ConfigurationBuilder.new(api_key: _get_env_or_default(properties, 'SECURENATIVE_API_KEY', options.api_key), |
43 | | - api_url: _get_env_or_default(properties, 'SECURENATIVE_API_URL', options.api_url), |
44 | | - interval: _get_env_or_default(properties, 'SECURENATIVE_INTERVAL', options.interval), |
45 | | - max_events: _get_env_or_default(properties, 'SECURENATIVE_MAX_EVENTS', options.max_events), |
46 | | - timeout: _get_env_or_default(properties, 'SECURENATIVE_TIMEOUT', options.timeout), |
47 | | - auto_send: _get_env_or_default(properties, 'SECURENATIVE_AUTO_SEND', options.auto_send), |
48 | | - disable: _get_env_or_default(properties, 'SECURENATIVE_DISABLE', options.disable), |
49 | | - log_level: _get_env_or_default(properties, 'SECURENATIVE_LOG_LEVEL', options.log_level), |
50 | | - fail_over_strategy: _get_env_or_default(properties, 'SECURENATIVE_FAILOVER_STRATEGY', options.fail_over_strategy), |
51 | | - proxy_headers: _get_env_or_default(properties, 'SECURENATIVE_PROXY_HEADERS', options.proxy_headers)) |
52 | 56 | end |
53 | 57 | end |
0 commit comments