Skip to content

Commit

Permalink
Use URI and CGI to parse the Candlepin DB URL
Browse files Browse the repository at this point in the history
  • Loading branch information
ekohl committed Oct 18, 2024
1 parent 34d18d0 commit f94f236
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions definitions/features/candlepin_database.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require 'uri'
require 'cgi'

class Features::CandlepinDatabase < ForemanMaintain::Feature
CANDLEPIN_DB_CONFIG = '/etc/candlepin/candlepin.conf'.freeze

Expand Down Expand Up @@ -33,17 +36,17 @@ def check_option_using_cpdb_help(option_name, parent_cmd = '')
def load_configuration
raw_config = File.read(CANDLEPIN_DB_CONFIG)
full_config = Hash[raw_config.scan(/(^[^#\n][^=]*)=(.*)/)]
uri_regexp = %r{://(([^/:]*):?([^/]*))/([^?]*)\??(ssl=([^&]*))?}
url = full_config['jpa.config.hibernate.connection.url']
uri = uri_regexp.match(url)
uri = URI.parse(url)
query = uri.query ? CGI.parse(uri.query) : {}
{
'username' => full_config['jpa.config.hibernate.connection.username'],
'password' => full_config['jpa.config.hibernate.connection.password'],
'database' => uri[4],
'host' => uri[2],
'port' => uri[3] || '5432',
'ssl' => (fetch_extra_param(url, 'ssl') == 'true'),
'sslfactory' => fetch_extra_param(url, 'sslfactory'),
'database' => uri.path,
'host' => uri.host,
'port' => uri.port || '5432',
'ssl' => query['ssl']&.first == 'true',
'sslfactory' => query['sslfactory']&.first,
'driver_class' => full_config['jpa.config.hibernate.connection.driver_class'],
'url' => url,
}
Expand All @@ -57,11 +60,4 @@ def extend_with_db_options
end
db_options
end

def fetch_extra_param(url, key_name)
query_string = url.split('?')[1]
return nil unless query_string
output = /#{key_name}=([^&]*)?/.match(query_string)
output[1] if output
end
end

0 comments on commit f94f236

Please sign in to comment.