From 001811b96abe958fe0d86c195aa5c64d0d86e962 Mon Sep 17 00:00:00 2001 From: Ahmed Aly Date: Mon, 14 May 2018 09:46:31 +0200 Subject: [PATCH] dp_dump_anonym --- dev/odoo-server-dev-aa.conf | 2 +- ext/custom-addons/dp_dump_anonym/LICENSE | 27 ++ ext/custom-addons/dp_dump_anonym/__init__.py | 5 + .../dp_dump_anonym/__manifest__.py | 26 ++ .../dp_dump_anonym/controllers/__init__.py | 4 + .../dp_dump_anonym/controllers/main.py | 66 ++++ .../dp_dump_anonym/data/anon.sql | 12 + ext/custom-addons/dp_dump_anonym/i18n/de.po | 12 + .../security/ir.model.access.csv | 1 + .../dp_dump_anonym/service/__init__.py | 4 + .../dp_dump_anonym/service/db.py | 64 +++ .../static/description/icon.png | Bin 0 -> 2225 bytes .../views/database_manager.html | 366 ++++++++++++++++++ 13 files changed, 588 insertions(+), 1 deletion(-) create mode 100755 ext/custom-addons/dp_dump_anonym/LICENSE create mode 100755 ext/custom-addons/dp_dump_anonym/__init__.py create mode 100755 ext/custom-addons/dp_dump_anonym/__manifest__.py create mode 100755 ext/custom-addons/dp_dump_anonym/controllers/__init__.py create mode 100755 ext/custom-addons/dp_dump_anonym/controllers/main.py create mode 100755 ext/custom-addons/dp_dump_anonym/data/anon.sql create mode 100755 ext/custom-addons/dp_dump_anonym/i18n/de.po create mode 100755 ext/custom-addons/dp_dump_anonym/security/ir.model.access.csv create mode 100755 ext/custom-addons/dp_dump_anonym/service/__init__.py create mode 100755 ext/custom-addons/dp_dump_anonym/service/db.py create mode 100755 ext/custom-addons/dp_dump_anonym/static/description/icon.png create mode 100755 ext/custom-addons/dp_dump_anonym/views/database_manager.html diff --git a/dev/odoo-server-dev-aa.conf b/dev/odoo-server-dev-aa.conf index fa317cd4..d609e036 100644 --- a/dev/odoo-server-dev-aa.conf +++ b/dev/odoo-server-dev-aa.conf @@ -17,7 +17,7 @@ timezone = Europe/Vienna show_debug = 1 workers = 0 -server_wide_modules = web,base_sparse_field,queue_job +server_wide_modules = web,base_sparse_field,queue_job,dp_dump_anonym portal_url = https://dev-portal.tzaustria.info/ portal_secret = hH43413$74O0 diff --git a/ext/custom-addons/dp_dump_anonym/LICENSE b/ext/custom-addons/dp_dump_anonym/LICENSE new file mode 100755 index 00000000..1a40586b --- /dev/null +++ b/ext/custom-addons/dp_dump_anonym/LICENSE @@ -0,0 +1,27 @@ +Odoo Proprietary License v1.0 + +This software and associated files (the "Software") may only be used (executed, +modified, executed after modifications) if you have purchased a valid license +from the authors, typically via Odoo Apps, or if you have received a written +agreement from the authors of the Software (see the COPYRIGHT file). + +You may develop Odoo modules that use the Software as a library (typically by +depending on it, importing it and using its resources), but without copying any +source code or material from the Software. You may distribute those modules +under the license of your choice, provided that this license is compatible with +the terms of the Odoo Proprietary License (For example: LGPL, MIT, +or proprietary licenses similar to this one). + +It is forbidden to publish, distribute, sublicense, or sell copies of the Software +or modified copies of the Software. + +The above copyright notice and this permission notice must be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/ext/custom-addons/dp_dump_anonym/__init__.py b/ext/custom-addons/dp_dump_anonym/__init__.py new file mode 100755 index 00000000..792f09f3 --- /dev/null +++ b/ext/custom-addons/dp_dump_anonym/__init__.py @@ -0,0 +1,5 @@ +# Copyright 2018-Today datenpol gmbh() +# License OPL-1 or later (https://www.odoo.com/documentation/user/11.0/legal/licenses/licenses.html#licenses). + +from . import service +from . import controllers diff --git a/ext/custom-addons/dp_dump_anonym/__manifest__.py b/ext/custom-addons/dp_dump_anonym/__manifest__.py new file mode 100755 index 00000000..fbf3dbb5 --- /dev/null +++ b/ext/custom-addons/dp_dump_anonym/__manifest__.py @@ -0,0 +1,26 @@ +# Copyright 2018-Today datenpol gmbh() +# License OPL-1 or later (https://www.odoo.com/documentation/user/11.0/legal/licenses/licenses.html#licenses). + +# noinspection PyStatementEffect +{ + 'name': 'Ermöglicht die Anonymisierung der Datenbank-Backups', + 'category': 'Administration', + 'version': '11.0.1.0.0', + 'license': 'OPL-1', + 'author': 'datenpol gmbh', + 'website': 'https://www.datenpol.at', + 'summary': '', + 'description': """ + offen + """, + 'depends': [ + 'web', + ], + 'auto_install': True, + 'data': [ + 'security/ir.model.access.csv', + ], + 'demo': [ + ], + 'bootstrap': True, +} diff --git a/ext/custom-addons/dp_dump_anonym/controllers/__init__.py b/ext/custom-addons/dp_dump_anonym/controllers/__init__.py new file mode 100755 index 00000000..3265d5d3 --- /dev/null +++ b/ext/custom-addons/dp_dump_anonym/controllers/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2018-Today datenpol gmbh() +# License OPL-1 or later (https://www.odoo.com/documentation/user/11.0/legal/licenses/licenses.html#licenses). + +from . import main diff --git a/ext/custom-addons/dp_dump_anonym/controllers/main.py b/ext/custom-addons/dp_dump_anonym/controllers/main.py new file mode 100755 index 00000000..15e66492 --- /dev/null +++ b/ext/custom-addons/dp_dump_anonym/controllers/main.py @@ -0,0 +1,66 @@ +# Copyright 2018-Today datenpol gmbh() +# License OPL-1 or later (https://www.odoo.com/documentation/user/11.0/legal/licenses/licenses.html#licenses). + +import datetime +import json +import logging +import os +import sys + +import jinja2 +import werkzeug +from odoo.addons.web.controllers.main import DBNAME_PATTERN, db_monodb, Database + +import odoo +from odoo import http +from odoo.http import content_disposition + +_logger = logging.getLogger(__name__) + +if hasattr(sys, 'frozen'): + # When running on compiled windows binary, we don't have access to package loader. + path = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', 'views')) + loader = jinja2.FileSystemLoader(path) +else: + loader = jinja2.PackageLoader('odoo.addons.dp_dump_anonym', "views") + +env = jinja2.Environment(loader=loader, autoescape=True) +env.filters["json"] = json.dumps + + +class DatabaseExtended(Database): + def _render_template(self, **d): + d.setdefault('manage', True) + d['insecure'] = odoo.tools.config.verify_admin_password('admin') + d['list_db'] = odoo.tools.config['list_db'] + d['langs'] = odoo.service.db.exp_list_lang() + d['countries'] = odoo.service.db.exp_list_countries() + d['pattern'] = DBNAME_PATTERN + # databases list + d['databases'] = [] + try: + d['databases'] = http.db_list() + d['incompatible_databases'] = odoo.service.db.list_db_incompatible(d['databases']) + except odoo.exceptions.AccessDenied: + monodb = db_monodb() + if monodb: + d['databases'] = [monodb] + return env.get_template("database_manager.html").render(d) + + @http.route('/web/database/backup', type='http', auth="none", methods=['POST'], csrf=False) + def backup(self, master_pwd, name, backup_format='zip', raw=False): + try: + odoo.service.db.check_super(master_pwd) + ts = datetime.datetime.utcnow().strftime("%Y-%m-%d_%H-%M-%S") + filename = "%s_%s.%s" % (name, ts, backup_format) + headers = [ + ('Content-Type', 'application/octet-stream; charset=binary'), + ('Content-Disposition', content_disposition(filename)), + ] + dump_stream = odoo.service.db.dump_db(name, None, backup_format, raw) + response = werkzeug.wrappers.Response(dump_stream, headers=headers, direct_passthrough=True) + return response + except Exception as e: + _logger.exception('Database.backup') + error = "Database backup error: %s" % (str(e) or repr(e)) + return self._render_template(error=error) diff --git a/ext/custom-addons/dp_dump_anonym/data/anon.sql b/ext/custom-addons/dp_dump_anonym/data/anon.sql new file mode 100755 index 00000000..c61d9582 --- /dev/null +++ b/ext/custom-addons/dp_dump_anonym/data/anon.sql @@ -0,0 +1,12 @@ +-- Set Admin password to 'x' +UPDATE res_users SET password_crypt = '$pbkdf2-sha512$25000$.r93rhUipFQKIYSQci6FcA$hesuvDYTEwTkjUYm/LSt6CH0B/oEMN3JUkpbL1K9gU3vnM3EEdojChU4cjSs21nCIjx88aoVJZ12PBahjM/0Yw' where id = 1; + +-- Disable schedulers +UPDATE ir_cron SET active = FALSE where id > 4; + +-- Corrupt E-Mail addresses +UPDATE res_partner SET email = replace(email, '@', '#'); + +-- Deactivate all mail servers +UPDATE ir_mail_server SET active = FALSE; +UPDATE fetchmail_server SET active = FALSE; diff --git a/ext/custom-addons/dp_dump_anonym/i18n/de.po b/ext/custom-addons/dp_dump_anonym/i18n/de.po new file mode 100755 index 00000000..2e830ad9 --- /dev/null +++ b/ext/custom-addons/dp_dump_anonym/i18n/de.po @@ -0,0 +1,12 @@ +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 8.saas~6\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2016-05-02 13:28+0000\n" +"PO-Revision-Date: 2016-05-02 13:28+0000\n" +"Last-Translator: <>\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" diff --git a/ext/custom-addons/dp_dump_anonym/security/ir.model.access.csv b/ext/custom-addons/dp_dump_anonym/security/ir.model.access.csv new file mode 100755 index 00000000..97dd8b91 --- /dev/null +++ b/ext/custom-addons/dp_dump_anonym/security/ir.model.access.csv @@ -0,0 +1 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink diff --git a/ext/custom-addons/dp_dump_anonym/service/__init__.py b/ext/custom-addons/dp_dump_anonym/service/__init__.py new file mode 100755 index 00000000..1071cd82 --- /dev/null +++ b/ext/custom-addons/dp_dump_anonym/service/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2018-Today datenpol gmbh() +# License OPL-1 or later (https://www.odoo.com/documentation/user/11.0/legal/licenses/licenses.html#licenses). + +from . import db diff --git a/ext/custom-addons/dp_dump_anonym/service/db.py b/ext/custom-addons/dp_dump_anonym/service/db.py new file mode 100755 index 00000000..dbd78cd6 --- /dev/null +++ b/ext/custom-addons/dp_dump_anonym/service/db.py @@ -0,0 +1,64 @@ +# Copyright 2018-Today datenpol gmbh() +# License OPL-1 or later (https://www.odoo.com/documentation/user/11.0/legal/licenses/licenses.html#licenses). + +import json +import logging +import os +import shutil +import tempfile +import base64 + +import odoo +from odoo.service.db import check_db_management_enabled, dump_db_manifest + +_logger = logging.getLogger(__name__) + + +@check_db_management_enabled +def exp_dump(db_name, backup_format, raw): + with tempfile.TemporaryFile(mode='w+b') as t: + dump_db(db_name, t, backup_format, raw) + t.seek(0) + return base64.b64encode(t.read()).decode() + + +odoo.service.db.exp_dump = exp_dump + + +@check_db_management_enabled +def dump_db(db_name, stream, backup_format='zip', raw=None): + _logger.info('DUMP DB: %s format %s', db_name, backup_format) + + self_dir = os.path.dirname(os.path.realpath(__file__)) + anon_sql_file = os.path.join(self_dir, '..', 'data', 'anon.sql') + + with odoo.tools.osutil.tempdir() as dump_dir: + + if backup_format == 'zip': + filestore = odoo.tools.config.filestore(db_name) + if os.path.exists(filestore): + shutil.copytree(filestore, os.path.join(dump_dir, 'filestore')) + with open(os.path.join(dump_dir, 'manifest.json'), 'w') as fh: + db = odoo.sql_db.db_connect(db_name) + with db.cursor() as cr: + json.dump(dump_db_manifest(cr), fh, indent=4) + + cmd = ['pg_dump', '--no-owner', db_name, '--file=' + os.path.join(dump_dir, 'dump.sql')] + odoo.tools.exec_pg_command(*cmd) + + if not raw: + with open(os.path.join(dump_dir, 'dump.sql'), 'ab') as dump_file, open(anon_sql_file, 'rb') as anon: + shutil.copyfileobj(anon, dump_file) + + if stream: + odoo.tools.osutil.zip_dir(dump_dir, stream, include_dir=False, + fnct_sort=lambda file_name: file_name != 'dump.sql') + else: + t = tempfile.TemporaryFile() + odoo.tools.osutil.zip_dir(dump_dir, t, include_dir=False, + fnct_sort=lambda file_name: file_name != 'dump.sql') + t.seek(0) + return t + + +odoo.service.db.dump_db = dump_db diff --git a/ext/custom-addons/dp_dump_anonym/static/description/icon.png b/ext/custom-addons/dp_dump_anonym/static/description/ic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slmSlot_GlrH!7G&g z@EjZoI2ewg@!BW+&7GXjkN$ zP9V?c50t9~0|^8sJp#MB+Q&dRs(hvIdps?!oR>bsP%9BeVgjR&z)829_9`QbW1UDj zi}el7_s6PcZxc$~o=mxlZ34SH_MKG^paDFSOc_MB!IZoMO1iEB7 zuXu=3X&J~QdDC<$9qJCXeK(!CjdUI!yxjNpvWluOfVIk>Jj*Mp?ps{E?nE*@92wd3 zHf~uj1Wu}w7i%_r%x-VX^m!H?i%itH6kBA0LH<;%Te|RgT3qgu|NJ!3x&T~~aD7on zm6@Sqv4i~=ddeyubQ5T`@^$W&jBhPlS@qw0OV+(HThi$D2P+Hj8hw>O8xnm~hU?3^ zO7v!}<)48*B5Y5$%jm;mTT}85HNuFG1qz~tn{<{Z&-$ek47^5!U86Tjdv#U$`}~1& zQ-~mef|#axF()pah?fK#*PaY9@k<0cl?s_Yf1q5F@hB4-)pY!n_vF&GCl-~iYYM>L z#XAuO&I!UGOY}3uJMmQkEX4vMS<1vqh-oE&O4zt_iwFlzc5TmONpi-oewsY6i9R)x zdSw8)fAv#BA}5=yHzBz|9(m-EM;>|Pk)!w@TDLdXvm#rS00000NkvXXu0mjf$QcM^ literal 0 HcmV?d00001 diff --git a/ext/custom-addons/dp_dump_anonym/views/database_manager.html b/ext/custom-addons/dp_dump_anonym/views/database_manager.html new file mode 100755 index 00000000..71bc3cd1 --- /dev/null +++ b/ext/custom-addons/dp_dump_anonym/views/database_manager.html @@ -0,0 +1,366 @@ + + + + + Odoo + + + + + + + + + + +{% macro master_input() -%} +
+ {% if insecure %} + + {% else %} + + + {% endif %} +
+{%- endmacro %} + +{% macro create_form() -%} +

Odoo is up and running!
+ Fill out this form to create a new database. You will install your first app afterwards.

+ {{ master_input() }} +
+
+
+ + +
+
+
+
+
+
+ + +
+
+
+
+ + + +
+
+
+
+ + +
+
+ + +
+
+
+
+
+ +
+
+{%- endmacro %} + + + +
+
+
+ +
+ {% if not list_db %} +
+ The database manager has been disabled by the administrator +
+ {% elif insecure and databases %} +
+ Warning, your Odoo database manager is not protected. + Please set a master password + to secure it. +
+ {% endif %} + {% if error %} +
{{ error }}
+ {% endif %} + {% if list_db and databases %} + + {% if manage %} +
+ + + +
+ {% else %} + + {% endif %} + {% elif list_db %} +
+ {{ create_form() }} + +
+ + or restore a database + + {% endif %} +
+
+ + + + + + + + + + + + + + + + + + + + + + +