Projet

Général

Profil

0001-misc-use-quoting-in-pg_dsn-40275.patch

Benjamin Dauvergne, 03 mars 2020 11:00

Télécharger (5,21 ko)

Voir les différences:

Subject: [PATCH] misc: use quoting in pg_dsn (#40275)

 .../management/commands/hobo_deploy.py        | 15 +++-
 tests/test_hobo_deploy.py                     | 78 +++++++++++++++++++
 tox.ini                                       |  1 +
 3 files changed, 90 insertions(+), 4 deletions(-)
 create mode 100644 tests/test_hobo_deploy.py
bijoe/hobo_agent/management/commands/hobo_deploy.py
25 25
from django.conf import settings
26 26

  
27 27

  
28
class Command(hobo_deploy.Command):
28
def pg_dsn_quote(value):
29
    return "'%s'" % value.replace('\\', '\\\\').replace('\'', '\\\'')
30

  
31

  
32
def config_parser_quote(value):
33
    return value.replace('%', '%%')
29 34

  
35

  
36
class Command(hobo_deploy.Command):
30 37
    def deploy_specifics(self, hobo_environment, tenant):
31 38
        super(Command, self).deploy_specifics(hobo_environment, tenant)
32 39
        with tenant_context(tenant):
33 40
            services = hobo_environment.get('services')
34 41
            ini_file = os.path.join(tenant.get_directory(), 'wcs-olap.ini')
35 42
            schemas_path = os.path.join(tenant.get_directory(), 'schemas')
36
            config = ConfigParser.ConfigParser()
43
            config = ConfigParser.SafeConfigParser()
37 44
            config.read(ini_file)
38 45

  
39 46
            if not os.path.exists(schemas_path):
......
50 57
                if settings.DATABASES['default'].get(pg_dsn_part[0]):
51 58
                    pg_dsn_parts.append('%s=%s' % (
52 59
                        pg_dsn_part[1],
53
                        settings.DATABASES['default'].get(pg_dsn_part[0])))
54
            config.set('wcs-olap', 'pg_dsn', ' '.join(pg_dsn_parts))
60
                        pg_dsn_quote(settings.DATABASES['default'].get(pg_dsn_part[0]))))
61
            config.set('wcs-olap', 'pg_dsn', config_parser_quote(' '.join(pg_dsn_parts)))
55 62

  
56 63
            for service in services:
57 64
                if service.get('this'):
tests/test_hobo_deploy.py
1
# bijoe - BI dashboard
2
# Copyright (C) 2015  Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
from contextlib import contextmanager
18
import ConfigParser
19

  
20
from psycopg2.extensions import parse_dsn
21

  
22
from bijoe.hobo_agent.management.commands import hobo_deploy
23

  
24

  
25
@contextmanager
26
def donothing(tenant):
27
    yield
28

  
29

  
30
class FakeTenant(object):
31
    domain_url = 'fake.tenant.com'
32

  
33
    def __init__(self, directory):
34
        self.directory = directory
35

  
36
    def get_directory(self):
37
        return self.directory
38

  
39

  
40
def test_deploy_specifics(tmpdir, settings, monkeypatch):
41
    monkeypatch.setattr(hobo_deploy, 'tenant_context', donothing)
42

  
43
    settings.DATABASES = {
44
        'default': {
45
            'NAME': 'coucou',
46
            'HOST': 'hostname.zob.org',
47
            'USER': 'hep',
48
            'PASSWORD': 'a \'%fc',
49
            'PORT': '1234',
50
        }
51
    }
52
    hobo_environment = {
53
        'services': [
54
            {
55
                'this': True,
56
                'secret_key': 'xx',
57
            }
58
        ],
59
    }
60

  
61
    command = hobo_deploy.Command()
62
    tenant_dir = tmpdir.mkdir('tenant')
63
    tenant = FakeTenant(str(tenant_dir))
64

  
65
    command.deploy_specifics(hobo_environment, tenant)
66

  
67
    wcs_olap_ini_path = tenant_dir / 'wcs-olap.ini'
68
    assert wcs_olap_ini_path.exists()
69
    with wcs_olap_ini_path.open() as fd:
70
        config = ConfigParser.SafeConfigParser()
71
        config.readfp(fd)
72
        pg_dsn = config.get('wcs-olap', 'pg_dsn')
73
        parsed_pg_dsn = parse_dsn(pg_dsn)
74
        assert parsed_pg_dsn['dbname'] == 'coucou'
75
        assert parsed_pg_dsn['host'] == 'hostname.zob.org'
76
        assert parsed_pg_dsn['user'] == 'hep'
77
        assert parsed_pg_dsn['password'] == 'a \'%fc'
78
        assert parsed_pg_dsn['port'] == '1234'
tox.ini
25 25
	django-webtest<1.9.3
26 26
	pyquery
27 27
	tabulate
28
	http://git.entrouvert.org/hobo.git/snapshot/hobo-master.tar.gz
28 29
commands =
29 30
        dj111: py.test {posargs: --junitxml=test_{envname}_results.xml --cov-report xml --cov-report html --cov=bijoe tests/}
30 31
[pytest]
31
-