Skip to content

Commit f94f236

Browse files
committed
Use URI and CGI to parse the Candlepin DB URL
1 parent 34d18d0 commit f94f236

1 file changed

Lines changed: 10 additions & 14 deletions

File tree

definitions/features/candlepin_database.rb

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
require 'uri'
2+
require 'cgi'
3+
14
class Features::CandlepinDatabase < ForemanMaintain::Feature
25
CANDLEPIN_DB_CONFIG = '/etc/candlepin/candlepin.conf'.freeze
36

@@ -33,17 +36,17 @@ def check_option_using_cpdb_help(option_name, parent_cmd = '')
3336
def load_configuration
3437
raw_config = File.read(CANDLEPIN_DB_CONFIG)
3538
full_config = Hash[raw_config.scan(/(^[^#\n][^=]*)=(.*)/)]
36-
uri_regexp = %r{://(([^/:]*):?([^/]*))/([^?]*)\??(ssl=([^&]*))?}
3739
url = full_config['jpa.config.hibernate.connection.url']
38-
uri = uri_regexp.match(url)
40+
uri = URI.parse(url)
41+
query = uri.query ? CGI.parse(uri.query) : {}
3942
{
4043
'username' => full_config['jpa.config.hibernate.connection.username'],
4144
'password' => full_config['jpa.config.hibernate.connection.password'],
42-
'database' => uri[4],
43-
'host' => uri[2],
44-
'port' => uri[3] || '5432',
45-
'ssl' => (fetch_extra_param(url, 'ssl') == 'true'),
46-
'sslfactory' => fetch_extra_param(url, 'sslfactory'),
45+
'database' => uri.path,
46+
'host' => uri.host,
47+
'port' => uri.port || '5432',
48+
'ssl' => query['ssl']&.first == 'true',
49+
'sslfactory' => query['sslfactory']&.first,
4750
'driver_class' => full_config['jpa.config.hibernate.connection.driver_class'],
4851
'url' => url,
4952
}
@@ -57,11 +60,4 @@ def extend_with_db_options
5760
end
5861
db_options
5962
end
60-
61-
def fetch_extra_param(url, key_name)
62-
query_string = url.split('?')[1]
63-
return nil unless query_string
64-
output = /#{key_name}=([^&]*)?/.match(query_string)
65-
output[1] if output
66-
end
6763
end

0 commit comments

Comments
 (0)