misc: use quoting in pg_dsn (#40275)

This commit is contained in:
Benjamin Dauvergne 2020-02-28 18:55:56 +01:00
parent f19b24d100
commit 1dbee3c4b3
3 changed files with 90 additions and 4 deletions

View File

@ -25,15 +25,22 @@ from hobo.multitenant.settings_loaders import KnownServices
from django.conf import settings
class Command(hobo_deploy.Command):
def pg_dsn_quote(value):
return "'%s'" % value.replace('\\', '\\\\').replace('\'', '\\\'')
def config_parser_quote(value):
return value.replace('%', '%%')
class Command(hobo_deploy.Command):
def deploy_specifics(self, hobo_environment, tenant):
super(Command, self).deploy_specifics(hobo_environment, tenant)
with tenant_context(tenant):
services = hobo_environment.get('services')
ini_file = os.path.join(tenant.get_directory(), 'wcs-olap.ini')
schemas_path = os.path.join(tenant.get_directory(), 'schemas')
config = ConfigParser.ConfigParser()
config = ConfigParser.SafeConfigParser()
config.read(ini_file)
if not os.path.exists(schemas_path):
@ -50,8 +57,8 @@ class Command(hobo_deploy.Command):
if settings.DATABASES['default'].get(pg_dsn_part[0]):
pg_dsn_parts.append('%s=%s' % (
pg_dsn_part[1],
settings.DATABASES['default'].get(pg_dsn_part[0])))
config.set('wcs-olap', 'pg_dsn', ' '.join(pg_dsn_parts))
pg_dsn_quote(settings.DATABASES['default'].get(pg_dsn_part[0]))))
config.set('wcs-olap', 'pg_dsn', config_parser_quote(' '.join(pg_dsn_parts)))
for service in services:
if service.get('this'):

78
tests/test_hobo_deploy.py Normal file
View File

@ -0,0 +1,78 @@
# bijoe - BI dashboard
# Copyright (C) 2015 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from contextlib import contextmanager
import ConfigParser
from psycopg2.extensions import parse_dsn
from bijoe.hobo_agent.management.commands import hobo_deploy
@contextmanager
def donothing(tenant):
yield
class FakeTenant(object):
domain_url = 'fake.tenant.com'
def __init__(self, directory):
self.directory = directory
def get_directory(self):
return self.directory
def test_deploy_specifics(tmpdir, settings, monkeypatch):
monkeypatch.setattr(hobo_deploy, 'tenant_context', donothing)
settings.DATABASES = {
'default': {
'NAME': 'coucou',
'HOST': 'hostname.zob.org',
'USER': 'hep',
'PASSWORD': 'a \'%fc',
'PORT': '1234',
}
}
hobo_environment = {
'services': [
{
'this': True,
'secret_key': 'xx',
}
],
}
command = hobo_deploy.Command()
tenant_dir = tmpdir.mkdir('tenant')
tenant = FakeTenant(str(tenant_dir))
command.deploy_specifics(hobo_environment, tenant)
wcs_olap_ini_path = tenant_dir / 'wcs-olap.ini'
assert wcs_olap_ini_path.exists()
with wcs_olap_ini_path.open() as fd:
config = ConfigParser.SafeConfigParser()
config.readfp(fd)
pg_dsn = config.get('wcs-olap', 'pg_dsn')
parsed_pg_dsn = parse_dsn(pg_dsn)
assert parsed_pg_dsn['dbname'] == 'coucou'
assert parsed_pg_dsn['host'] == 'hostname.zob.org'
assert parsed_pg_dsn['user'] == 'hep'
assert parsed_pg_dsn['password'] == 'a \'%fc'
assert parsed_pg_dsn['port'] == '1234'

View File

@ -25,6 +25,7 @@ deps =
django-webtest<1.9.3
pyquery
tabulate
http://git.entrouvert.org/hobo.git/snapshot/hobo-master.tar.gz
commands =
dj111: py.test {posargs: --junitxml=test_{envname}_results.xml --cov-report xml --cov-report html --cov=bijoe tests/}
[pytest]