Projet

Général

Profil

0001-misc-truncate-schema-names-to-63-chars-43042.patch

Benjamin Dauvergne, 20 mai 2020 17:27

Télécharger (4,06 ko)

Voir les différences:

Subject: [PATCH] misc: truncate schema names to 63 chars (#43042)

To comply with postgresql limitation, to prevent collisions a hash of
the full identifier is inserted in the middle.
 .../management/commands/hobo_deploy.py        | 23 +++++++++++++--
 tests/test_hobo_agent.py                      | 28 +++++++++++++++++++
 2 files changed, 49 insertions(+), 2 deletions(-)
 create mode 100644 tests/test_hobo_agent.py
bijoe/hobo_agent/management/commands/hobo_deploy.py
14 14
# You should have received a copy of the GNU Affero General Public License
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17
import hashlib
17 18
import os
18 19

  
19 20
from django.utils.six.moves import configparser as ConfigParser
......
35 36
    return value.replace('%', '%%')
36 37

  
37 38

  
39
def truncate_pg_identifier(identifier, hash_length=6, force_hash=False):
40
    if len(identifier) < 64 and not force_hash:
41
        return identifier
42
    else:
43
        # insert hash in the middle, to keep some readability
44
        return (
45
            identifier[:(63 - hash_length) // 2]
46
            + hashlib.md5(identifier.encode('utf-8')).hexdigest()[:hash_length]
47
            + identifier[-(63 - hash_length) // 2:])
48

  
49

  
50
def schema_from_url(url, hash_length=6):
51
    netloc = urlparse.urlparse(url).netloc
52
    assert netloc
53
    domain = netloc.split(':')[0]
54
    pg_identifier = domain.replace('.', '_').replace('-', '_')
55
    return truncate_pg_identifier(pg_identifier, hash_length=hash_length)
56

  
57

  
38 58
class Command(hobo_deploy.Command):
39 59
    def deploy_specifics(self, hobo_environment, tenant):
40 60
        super(Command, self).deploy_specifics(hobo_environment, tenant)
......
78 98
                    # skip secondary instances unless they were already added,
79 99
                    # in that case they need to be kept uptodate
80 100
                    continue
81
                schema = (urlparse.urlparse(base_url).netloc.split(':')[0]
82
                          .replace('.', '_').replace('-', '_'))
101
                schema = schema_from_url(base_url)
83 102
                orig = urlparse.urlparse(this.get('base_url')).netloc.split(':')[0]
84 103
                their_key = service.get('secret_key')
85 104
                key = KnownServices.shared_secret(our_key, their_key)
tests/test_hobo_agent.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 bijoe.hobo_agent.management.commands import hobo_deploy
18

  
19

  
20
def test_schema_from_url():
21
    for hash_length in [4, 5, 6, 7]:
22
        for length in [64, 65, 66]:
23
            assert len(hobo_deploy.schema_from_url('https://' + ('x' * length), hash_length=hash_length)) == 63
24

  
25
    schema = hobo_deploy.schema_from_url('https://demarches-saint-didier-au-mont-dor.guichet-recette.grandlyon.com/')
26

  
27
    assert len(schema) == 63
28
    assert schema == 'demarches_saint_didier_au_mo0757cfguichet_recette_grandlyon_com'
0
-