Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/cloud_controller/db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class DB
#
# @return [Sequel::Database]
def self.connect(opts, logger)
VCAP::CloudController::DbConnection::OptionsFactory.set_env(opts)
connection_options = VCAP::CloudController::DbConnection::OptionsFactory.build(opts)

db = get_connection(opts, connection_options)
Expand Down
8 changes: 8 additions & 0 deletions lib/cloud_controller/db_connection/mysql_options_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ def self.build(opts)

options
end

def self.reset_env(env)
env.delete('MARIADB_TLS_DISABLE_PEER_VERIFICATION')
end

def self.set_env(opts, env)
env['MARIADB_TLS_DISABLE_PEER_VERIFICATION'] = '1' unless opts[:ca_cert_path]
end
end
end
end
18 changes: 16 additions & 2 deletions lib/cloud_controller/db_connection/options_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ def build(opts)
compact
end

def set_env(opts) # rubocop:disable Naming/AccessorMethodName
FACTORIES.values.uniq.each do |factory|
factory.reset_env(ENV)
end
adapter_set_env(opts, ENV)
end

private

FACTORIES = {
Expand All @@ -44,8 +51,15 @@ def base_options(opts)
end

def adapter_options(opts)
adapter = opts[:database][:adapter]
factory_for(adapter).build(opts)
factory_for(adapter(opts)).build(opts)
end

def adapter_set_env(opts, env)
factory_for(adapter(opts)).set_env(opts, env)
end

def adapter(opts)
opts[:database][:adapter]
end

def factory_for(adapter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def self.build(opts)

options
end

def self.reset_env(_); end

def self.set_env(_, _); end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,36 @@
end
end
end

describe 'env vars' do
let(:env) { {} }

describe '.reset_env' do
it 'deletes MARIADB_TLS_DISABLE_PEER_VERIFICATION from env' do
env.merge!('MARIADB_TLS_DISABLE_PEER_VERIFICATION' => '1')
VCAP::CloudController::DbConnection::MysqlOptionsFactory.reset_env(env)
expect(env).not_to have_key('MARIADB_TLS_DISABLE_PEER_VERIFICATION')
end
end

describe '.set_env' do
it 'does not set MARIADB_TLS_DISABLE_PEER_VERIFICATION when there is a ca_cert_path' do
opts = { ca_cert_path: '/path/to/ca_cert' }
VCAP::CloudController::DbConnection::MysqlOptionsFactory.set_env(opts, env)
expect(env).not_to have_key('MARIADB_TLS_DISABLE_PEER_VERIFICATION')
end

it 'sets MARIADB_TLS_DISABLE_PEER_VERIFICATION when ca_cert_path is nil' do
opts = { ca_cert_path: nil }
VCAP::CloudController::DbConnection::MysqlOptionsFactory.set_env(opts, env)
expect(env['MARIADB_TLS_DISABLE_PEER_VERIFICATION']).to eq('1')
end

it 'sets MARIADB_TLS_DISABLE_PEER_VERIFICATION when there is no ca_cert_path' do
opts = {}
VCAP::CloudController::DbConnection::MysqlOptionsFactory.set_env(opts, env)
expect(env['MARIADB_TLS_DISABLE_PEER_VERIFICATION']).to eq('1')
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,27 @@
end
end
end

describe '.set_env' do
let(:adapter) { 'mysql' }

before do
allow(VCAP::CloudController::DbConnection::MysqlOptionsFactory).to receive(:reset_env).with(anything)
allow(VCAP::CloudController::DbConnection::MysqlOptionsFactory).to receive(:set_env).with(anything, anything)
allow(VCAP::CloudController::DbConnection::PostgresOptionsFactory).to receive(:reset_env).with(anything)
allow(VCAP::CloudController::DbConnection::PostgresOptionsFactory).to receive(:set_env).with(anything, anything)
end

it 'calls reset_env on all adapters exactly once' do
expect(VCAP::CloudController::DbConnection::MysqlOptionsFactory).to receive(:reset_env).with(ENV).once
expect(VCAP::CloudController::DbConnection::PostgresOptionsFactory).to receive(:reset_env).with(ENV).once
VCAP::CloudController::DbConnection::OptionsFactory.set_env(required_options)
end

it 'calls set_env on the adapter factory' do
expect(VCAP::CloudController::DbConnection::MysqlOptionsFactory).to receive(:set_env).with(required_options, ENV).once
expect(VCAP::CloudController::DbConnection::PostgresOptionsFactory).not_to receive(:set_env).with(anything, anything)
VCAP::CloudController::DbConnection::OptionsFactory.set_env(required_options)
end
end
end
Loading