diff --git a/bijoe/hobo_agent/management/commands/hobo_deploy.py b/bijoe/hobo_agent/management/commands/hobo_deploy.py index 250710e..bda970e 100644 --- a/bijoe/hobo_agent/management/commands/hobo_deploy.py +++ b/bijoe/hobo_agent/management/commands/hobo_deploy.py @@ -14,6 +14,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import hashlib import os from django.utils.six.moves import configparser as ConfigParser @@ -35,6 +36,25 @@ def config_parser_quote(value): return value.replace('%', '%%') +def truncate_pg_identifier(identifier, hash_length=6, force_hash=False): + if len(identifier) < 64 and not force_hash: + return identifier + else: + # insert hash in the middle, to keep some readability + return ( + identifier[:(63 - hash_length) // 2] + + hashlib.md5(identifier.encode('utf-8')).hexdigest()[:hash_length] + + identifier[-(63 - hash_length) // 2:]) + + +def schema_from_url(url, hash_length=6): + netloc = urlparse.urlparse(url).netloc + assert netloc + domain = netloc.split(':')[0] + pg_identifier = domain.replace('.', '_').replace('-', '_') + return truncate_pg_identifier(pg_identifier, hash_length=hash_length) + + class Command(hobo_deploy.Command): def deploy_specifics(self, hobo_environment, tenant): super(Command, self).deploy_specifics(hobo_environment, tenant) @@ -78,8 +98,7 @@ class Command(hobo_deploy.Command): # skip secondary instances unless they were already added, # in that case they need to be kept uptodate continue - schema = (urlparse.urlparse(base_url).netloc.split(':')[0] - .replace('.', '_').replace('-', '_')) + schema = schema_from_url(base_url) orig = urlparse.urlparse(this.get('base_url')).netloc.split(':')[0] their_key = service.get('secret_key') key = KnownServices.shared_secret(our_key, their_key) diff --git a/tests/test_hobo_agent.py b/tests/test_hobo_agent.py new file mode 100644 index 0000000..9f53d39 --- /dev/null +++ b/tests/test_hobo_agent.py @@ -0,0 +1,28 @@ +# 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 . + +from bijoe.hobo_agent.management.commands import hobo_deploy + + +def test_schema_from_url(): + for hash_length in [4, 5, 6, 7]: + for length in [64, 65, 66]: + assert len(hobo_deploy.schema_from_url('https://' + ('x' * length), hash_length=hash_length)) == 63 + + schema = hobo_deploy.schema_from_url('https://demarches-saint-didier-au-mont-dor.guichet-recette.grandlyon.com/') + + assert len(schema) == 63 + assert schema == 'demarches_saint_didier_au_mo0757cfguichet_recette_grandlyon_com'