-
Notifications
You must be signed in to change notification settings - Fork 368
Expand file tree
/
Copy pathconfig.rb
More file actions
189 lines (151 loc) · 6.11 KB
/
config.rb
File metadata and controls
189 lines (151 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
require 'cloud_controller/account_capacity'
require 'uri'
require 'cloud_controller/backends/stagers'
require 'cloud_controller/backends/runners'
require 'cloud_controller/index_stopper'
require 'cloud_controller/backends/instances_reporters'
require 'repositories/service_event_repository'
require 'cloud_controller/config_schemas/vms/api_schema'
require 'cloud_controller/config_schemas/vms/clock_schema'
require 'cloud_controller/config_schemas/vms/migrate_schema'
require 'cloud_controller/config_schemas/vms/route_syncer_schema'
require 'cloud_controller/config_schemas/vms/worker_schema'
require 'cloud_controller/config_schemas/vms/deployment_updater_schema'
require 'cloud_controller/config_schemas/vms/rotate_database_key_schema'
require 'utils/hash_utils'
module VCAP::CloudController
class Config
class InvalidConfigPath < StandardError
end
class << self
def load_from_file(file_name, context: :api, secrets_hash: {})
config = read_file(file_name)
load_from_hash(config, context:, secrets_hash:)
end
def config
@instance
end
def schema_class_for_context(context, _config)
module_name = 'Vms'
const_get("VCAP::CloudController::ConfigSchemas::#{module_name}::#{context.to_s.camelize}Schema")
end
def read_file(file_name)
deep_symbolize_keys_except_in_arrays(VCAP::CloudController::YAMLConfig.safe_load_file(file_name)) || {}
end
def load_from_hash(config_hash, context: :api, secrets_hash: {})
config = deep_symbolize_keys_except_in_arrays(config_hash)
secrets_hash = deep_symbolize_keys_except_in_arrays(secrets_hash)
config = config.deep_merge(secrets_hash)
schema_class = schema_class_for_context(context, config)
schema_class.validate(config)
hash = merge_defaults(config)
@instance = new(hash, context:)
end
private
def deep_symbolize_keys_except_in_arrays(hash)
return hash unless hash.is_a? Hash
hash.each.with_object({}) do |(k, v), new_hash|
new_hash[k.to_sym] = deep_symbolize_keys_except_in_arrays(v)
end
end
def merge_defaults(orig_config)
config = orig_config.dup
config[:db] ||= {}
ensure_config_has_database_parts(config)
sanitize(config)
end
def ensure_config_has_database_parts(config)
abort_no_db_connection! if ENV['DB_CONNECTION_STRING'].nil? && config[:db][:database].nil?
config[:db][:db_connection_string] ||= ENV.fetch('DB_CONNECTION_STRING', nil)
config[:db][:database] ||= DatabasePartsParser.database_parts_from_connection(config[:db][:db_connection_string])
end
def abort_no_db_connection!
abort('No database connection set (consider setting DB_CONNECTION_STRING)')
end
def sanitize(config)
if config.key?(:staging) && config[:staging].key?(:auth)
auth = config[:staging][:auth]
auth[:user] = escape_userinfo(auth[:user]) unless valid_in_userinfo?(auth[:user])
auth[:password] = escape_password(auth[:password]) unless valid_in_userinfo?(auth[:password])
end
config
end
def escape_password(value)
escape_userinfo(value).gsub('\"', '%22')
end
def escape_userinfo(value)
CGI.escape(value)
end
def valid_in_userinfo?(value)
URI::REGEXP::PATTERN::USERINFO.match(value)
end
end
attr_reader :config_hash, :context
def initialize(config_hash, context: :api)
@config_hash = config_hash
@context = context
@schema_class = self.class.schema_class_for_context(context, config_hash)
end
def configure_components
Encryptor.db_encryption_key = get(:db_encryption_key)
if get(:database_encryption)
Encryptor.database_encryption_keys = get(:database_encryption)[:keys]
Encryptor.current_encryption_key_label = get(:database_encryption)[:current_key_label]
Encryptor.pbkdf2_hmac_iterations = get(:database_encryption)[:pbkdf2_hmac_iterations]
end
dependency_locator = CloudController::DependencyLocator.instance
dependency_locator.config = self
run_initializers
ProcessObserver.configure(dependency_locator.stagers, dependency_locator.runners)
@schema_class.configure_components(self)
end
def get(*keys)
invalid_config_path!(keys) unless valid_config_path?(keys, @schema_class.schema)
HashUtils.dig(config_hash, *keys)
end
def set(key, value)
config_hash[key] = value
end
def valid_config_path?(keys, some_schema)
keys.each do |key|
if some_schema.is_a?(Membrane::Schemas::Record) && some_schema.schemas.key?(key)
some_schema = some_schema.schemas[key]
else
invalid_config_path!(keys)
end
end
end
def package_image_registry_configured?
!get(:packages, :image_registry).nil?
end
private
def invalid_config_path!(keys)
raise InvalidConfigPath.new(%("#{keys.join('.')}" is not a valid config key))
end
def run_initializers
return if @initialized
run_initializers_in_directory('../../../config/initializers/*.rb')
if @config_hash[:newrelic_enabled]
require 'newrelic_rpm'
# We need to explicitly initialize NewRelic before running our initializers
# When Rails is present, NewRelic adds itself to the Rails initializers instead
# of initializing immediately.
opts = if Rails.env.local? && !ENV['NRCONFIG']
{ env: ENV['NEW_RELIC_ENV'] || 'production', monitor_mode: false }
else
{ env: ENV['NEW_RELIC_ENV'] || 'production' }
end
NewRelic::Agent.manual_start(opts)
run_initializers_in_directory('../../../config/newrelic/initializers/*.rb')
end
@initialized = true
end
def run_initializers_in_directory(path)
Dir.glob(File.expand_path(path, __FILE__)).each do |file|
require file
method = File.basename(file).sub('.rb', '').tr('-', '_')
CCInitializers.send(method, @config_hash, @context)
end
end
end
end