Skip to content

Commit 2b6a059

Browse files
committed
Use URI and CGI to parse the Candlepin DB URL
1 parent 4c86ebb commit 2b6a059

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

@@ -36,17 +39,17 @@ def raw_config
3639

3740
def load_configuration
3841
full_config = Hash[raw_config.scan(/(^[^#\n][^=]*)=(.*)/)]
39-
uri_regexp = %r{://(([^/:]*):?([^/]*))/([^?]*)\??(ssl=([^&]*))?}
4042
url = full_config['jpa.config.hibernate.connection.url']
41-
uri = uri_regexp.match(url)
43+
uri = URI.parse(url.delete_prefix('jdbc:'))
44+
query = uri.query ? CGI.parse(uri.query) : {}
4245
{
4346
'username' => full_config['jpa.config.hibernate.connection.username'],
4447
'password' => full_config['jpa.config.hibernate.connection.password'],
45-
'database' => uri[4],
46-
'host' => uri[2],
47-
'port' => uri[3] || '5432',
48-
'ssl' => (fetch_extra_param(url, 'ssl') == 'true'),
49-
'sslfactory' => fetch_extra_param(url, 'sslfactory'),
48+
'database' => uri.path,
49+
'host' => uri.host,
50+
'port' => uri.port || '5432',
51+
'ssl' => query['ssl']&.first == 'true',
52+
'sslfactory' => query['sslfactory']&.first,
5053
'driver_class' => full_config['jpa.config.hibernate.connection.driver_class'],
5154
'url' => url,
5255
}
@@ -60,11 +63,4 @@ def extend_with_db_options
6063
end
6164
db_options
6265
end
63-
64-
def fetch_extra_param(url, key_name)
65-
query_string = url.split('?')[1]
66-
return nil unless query_string
67-
output = /#{key_name}=([^&]*)?/.match(query_string)
68-
output[1] if output
69-
end
7066
end

0 commit comments

Comments
 (0)