From 029ba108ec9cffbc3d02d813c6e0dea69a616956 Mon Sep 17 00:00:00 2001 From: Philipp Thun Date: Wed, 2 Sep 2026 12:36:31 +0200 Subject: [PATCH] Introduce set_env method in db options factory VCAP::CloudController::DB invokes dedicated factories for MySQL and Postgres connection options. Add 'set_env' methods to the factories that set required environment variables depending on the adapter type and call it just before building the connection options. This mechanism provides a single place where e.g. MARIADB_TLS_DISABLE_PEER_VERIFICATION can be set instead of exporting it in various code places. --- lib/cloud_controller/db.rb | 1 + .../db_connection/mysql_options_factory.rb | 8 +++++ .../db_connection/options_factory.rb | 18 +++++++++-- .../db_connection/postgres_options_factory.rb | 4 +++ .../mysql_options_factory_spec.rb | 32 +++++++++++++++++++ .../db_connection/options_factory_spec.rb | 23 +++++++++++++ 6 files changed, 84 insertions(+), 2 deletions(-) diff --git a/lib/cloud_controller/db.rb b/lib/cloud_controller/db.rb index 0c5c0ffd3db..e87956f06b1 100644 --- a/lib/cloud_controller/db.rb +++ b/lib/cloud_controller/db.rb @@ -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) diff --git a/lib/cloud_controller/db_connection/mysql_options_factory.rb b/lib/cloud_controller/db_connection/mysql_options_factory.rb index b11302959ce..03686750d27 100644 --- a/lib/cloud_controller/db_connection/mysql_options_factory.rb +++ b/lib/cloud_controller/db_connection/mysql_options_factory.rb @@ -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 diff --git a/lib/cloud_controller/db_connection/options_factory.rb b/lib/cloud_controller/db_connection/options_factory.rb index 78c11aaec23..13ead500930 100644 --- a/lib/cloud_controller/db_connection/options_factory.rb +++ b/lib/cloud_controller/db_connection/options_factory.rb @@ -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 = { @@ -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) diff --git a/lib/cloud_controller/db_connection/postgres_options_factory.rb b/lib/cloud_controller/db_connection/postgres_options_factory.rb index e7887be9896..e48ac45bd51 100644 --- a/lib/cloud_controller/db_connection/postgres_options_factory.rb +++ b/lib/cloud_controller/db_connection/postgres_options_factory.rb @@ -25,6 +25,10 @@ def self.build(opts) options end + + def self.reset_env(_); end + + def self.set_env(_, _); end end end end diff --git a/spec/unit/lib/cloud_controller/db_connection/mysql_options_factory_spec.rb b/spec/unit/lib/cloud_controller/db_connection/mysql_options_factory_spec.rb index f7db69f8715..4ec6fbeb773 100644 --- a/spec/unit/lib/cloud_controller/db_connection/mysql_options_factory_spec.rb +++ b/spec/unit/lib/cloud_controller/db_connection/mysql_options_factory_spec.rb @@ -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 diff --git a/spec/unit/lib/cloud_controller/db_connection/options_factory_spec.rb b/spec/unit/lib/cloud_controller/db_connection/options_factory_spec.rb index fb0f4d132c8..cb2b116d5c6 100644 --- a/spec/unit/lib/cloud_controller/db_connection/options_factory_spec.rb +++ b/spec/unit/lib/cloud_controller/db_connection/options_factory_spec.rb @@ -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