From 21d18bbd38fd60cc8efacb31561b41b4493c8903 Mon Sep 17 00:00:00 2001 From: Florian da Costa Date: Fri, 5 Feb 2016 14:36:11 +0100 Subject: [PATCH] Add sql export module (migration from v7) Add rollback after executing query as a double security with blacklist terms add known issue in readme --- sql_export/README.rst | 56 ++++++++ sql_export/__init__.py | 2 + sql_export/__openerp__.py | 39 +++++ sql_export/i18n/fr.po | 142 +++++++++++++++++++ sql_export/i18n/sql_export.pot | 142 +++++++++++++++++++ sql_export/security/ir.model.access.csv | 3 + sql_export/security/sql_export_security.xml | 21 +++ sql_export/sql_export.py | 149 ++++++++++++++++++++ sql_export/sql_export_view.xml | 56 ++++++++ sql_export/static/description/icon.png | Bin 0 -> 9455 bytes sql_export/tests/__init__.py | 2 + sql_export/tests/test_sql_query.py | 71 ++++++++++ sql_export/wizard/__init__.py | 1 + sql_export/wizard/wizard_file.py | 29 ++++ sql_export/wizard/wizard_file_view.xml | 17 +++ 15 files changed, 730 insertions(+) create mode 100644 sql_export/README.rst create mode 100644 sql_export/__init__.py create mode 100644 sql_export/__openerp__.py create mode 100644 sql_export/i18n/fr.po create mode 100644 sql_export/i18n/sql_export.pot create mode 100644 sql_export/security/ir.model.access.csv create mode 100644 sql_export/security/sql_export_security.xml create mode 100644 sql_export/sql_export.py create mode 100644 sql_export/sql_export_view.xml create mode 100644 sql_export/static/description/icon.png create mode 100644 sql_export/tests/__init__.py create mode 100644 sql_export/tests/test_sql_query.py create mode 100644 sql_export/wizard/__init__.py create mode 100644 sql_export/wizard/wizard_file.py create mode 100644 sql_export/wizard/wizard_file_view.xml diff --git a/sql_export/README.rst b/sql_export/README.rst new file mode 100644 index 000000000..12e64e49d --- /dev/null +++ b/sql_export/README.rst @@ -0,0 +1,56 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :alt: License: AGPL-3 + +SQL Export +========== + +Allow to export data in csv files FROM sql requests. +There are some restrictions in the sql sql request, you can only read datas. +No update, deletion or creation are possible. +A new menu named Export is created. + +Known issues / Roadmap +====================== + +Some words are prohibeted and can't be used is the query in anyways, even in a select query : + +* delete +* drop +* insert +* alter +* truncate +* execute +* create +* update + + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed feedback +`here `_. + +Credits +======= + +Contributors +------------ + +* Florian da Costa + +Maintainer +---------- + +.. image:: http://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: http://odoo-community.org + +This module is maintained by the OCA. + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +To contribute to this module, please visit http://odoo-community.org. diff --git a/sql_export/__init__.py b/sql_export/__init__.py new file mode 100644 index 000000000..5dfb4aab3 --- /dev/null +++ b/sql_export/__init__.py @@ -0,0 +1,2 @@ +from . import sql_export +from . import wizard diff --git a/sql_export/__openerp__.py b/sql_export/__openerp__.py new file mode 100644 index 000000000..9048aa751 --- /dev/null +++ b/sql_export/__openerp__.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2015 Akretion (). +# +# 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 . +# +############################################################################## + +{'name': 'SQL Export', + 'version': '0.1', + 'author': 'Akretion,Odoo Community Association (OCA)', + 'website': 'http://www.akretion.com', + 'license': 'AGPL-3', + 'category': 'Generic Modules/Others', + 'summary': 'Export data in csv file with SQL requests', + 'depends': ['base', + ], + 'data': [ + 'sql_export_view.xml', + 'wizard/wizard_file_view.xml', + 'security/sql_export_security.xml', + 'security/ir.model.access.csv', + ], + 'installable': True, + 'images': [], + } diff --git a/sql_export/i18n/fr.po b/sql_export/i18n/fr.po new file mode 100644 index 000000000..d1b7ff6e7 --- /dev/null +++ b/sql_export/i18n/fr.po @@ -0,0 +1,142 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * sql_export +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 8.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2016-02-05 13:10+0000\n" +"PO-Revision-Date: 2016-02-05 13:10+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" + +#. module: sql_export +#: model:ir.model,name:sql_export.model_sql_file_wizard +msgid "Allow the user to save the file with sql request's data" +msgstr "Permet à l'utilisateur de sauvegarder le fichier contenant les données de la requête SQL" + +#. module: sql_export +#: field:sql.export,group_ids:0 +msgid "Allowed Groups" +msgstr "Groupes Autorisés" + +#. module: sql_export +#: view:sql.export:sql_export.sql_export_view_form +#: field:sql.export,user_ids:0 +msgid "Allowed Users" +msgstr "Utilisateurs Autorisés" + +#. module: sql_export +#: view:sql.export:sql_export.sql_export_view_form +msgid "Allowed Users Groups" +msgstr "Groupes d'utilisateurs Autorisés" + +#. module: sql_export +#: field:sql.export,copy_options:0 +msgid "Copy Options" +msgstr "Copy Options" + +#. module: sql_export +#: field:sql.export,create_uid:0 +#: field:sql.file.wizard,create_uid:0 +msgid "Created by" +msgstr "Créé par" + +#. module: sql_export +#: field:sql.export,create_date:0 +#: field:sql.file.wizard,create_date:0 +msgid "Created on" +msgstr "Créé le" + +#. module: sql_export +#: view:sql.file.wizard:sql_export.sql_file_wizard_view_form +msgid "Csv File" +msgstr "Fichier CSV" + +#. module: sql_export +#: view:sql.export:sql_export.sql_export_view_form +msgid "Execute Query" +msgstr "Exécuter" + +#. module: sql_export +#: field:sql.file.wizard,binary_file:0 +msgid "File" +msgstr "Fichier" + +#. module: sql_export +#: field:sql.file.wizard,file_name:0 +msgid "File Name" +msgstr "Nom du fichier" + +#. module: sql_export +#: field:sql.export,id:0 +#: field:sql.file.wizard,id:0 +msgid "ID" +msgstr "ID" + +#. module: sql_export +#: field:sql.export,write_uid:0 +#: field:sql.file.wizard,write_uid:0 +msgid "Last Updated by" +msgstr "Last Updated by" + +#. module: sql_export +#: field:sql.export,write_date:0 +#: field:sql.file.wizard,write_date:0 +msgid "Last Updated on" +msgstr "Last Updated on" + +#. module: sql_export +#: field:sql.export,name:0 +msgid "Name" +msgstr "Nom" + +#. module: sql_export +#: field:sql.export,query:0 +msgid "Query" +msgstr "Requête" + +#. module: sql_export +#: model:ir.actions.act_window,name:sql_export.sql_export_tree_action +#: view:sql.export:sql_export.sql_export_view_tree +msgid "SQL Export" +msgstr "SQL Export" + +#. module: sql_export +#: model:ir.model,name:sql_export.model_sql_export +#: view:sql.export:sql_export.sql_export_view_form +msgid "SQL export" +msgstr "Export SQL" + +#. module: sql_export +#: model:ir.ui.menu,name:sql_export.sql_export_menu +#: model:ir.ui.menu,name:sql_export.sql_export_menu_view +msgid "Sql Export" +msgstr "Export SQL" + +#. module: sql_export +#: model:res.groups,name:sql_export.group_sql_request_editor +msgid "Sql Request Editor" +msgstr "Edition de Requête SQL" + +#. module: sql_export +#: code:addons/sql_export/sql_export.py:132 +#, python-format +msgid "The Sql query is not valid." +msgstr "La requête SQL n'est pas valide" + +#. module: sql_export +#: constraint:sql.export:0 +msgid "The query you want make is not allowed : prohibited actions (delete, drop, insert, alter, truncate, execute, create, update)" +msgstr "La requête que vous voulez faire n'est pas autorisée : actions interdites (delete, drop, insert, alter, truncate, execute, create, update)" + +#. module: sql_export +#: help:sql.export,query:0 +msgid "You can't use the following word : delete, drop, create, insert, alter, truncate, execute, update" +msgstr "Vous ne pouvez pas utiliser les mots suivants : delete, drop, create, insert, alter, truncate, execute, update" + diff --git a/sql_export/i18n/sql_export.pot b/sql_export/i18n/sql_export.pot new file mode 100644 index 000000000..39cc40538 --- /dev/null +++ b/sql_export/i18n/sql_export.pot @@ -0,0 +1,142 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * sql_export +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 8.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2016-02-05 13:10+0000\n" +"PO-Revision-Date: 2016-02-05 13:10+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" + +#. module: sql_export +#: model:ir.model,name:sql_export.model_sql_file_wizard +msgid "Allow the user to save the file with sql request's data" +msgstr "" + +#. module: sql_export +#: field:sql.export,group_ids:0 +msgid "Allowed Groups" +msgstr "" + +#. module: sql_export +#: view:sql.export:sql_export.sql_export_view_form +#: field:sql.export,user_ids:0 +msgid "Allowed Users" +msgstr "" + +#. module: sql_export +#: view:sql.export:sql_export.sql_export_view_form +msgid "Allowed Users Groups" +msgstr "" + +#. module: sql_export +#: field:sql.export,copy_options:0 +msgid "Copy Options" +msgstr "" + +#. module: sql_export +#: field:sql.export,create_uid:0 +#: field:sql.file.wizard,create_uid:0 +msgid "Created by" +msgstr "" + +#. module: sql_export +#: field:sql.export,create_date:0 +#: field:sql.file.wizard,create_date:0 +msgid "Created on" +msgstr "" + +#. module: sql_export +#: view:sql.file.wizard:sql_export.sql_file_wizard_view_form +msgid "Csv File" +msgstr "" + +#. module: sql_export +#: view:sql.export:sql_export.sql_export_view_form +msgid "Execute Query" +msgstr "" + +#. module: sql_export +#: field:sql.file.wizard,binary_file:0 +msgid "File" +msgstr "" + +#. module: sql_export +#: field:sql.file.wizard,file_name:0 +msgid "File Name" +msgstr "" + +#. module: sql_export +#: field:sql.export,id:0 +#: field:sql.file.wizard,id:0 +msgid "ID" +msgstr "" + +#. module: sql_export +#: field:sql.export,write_uid:0 +#: field:sql.file.wizard,write_uid:0 +msgid "Last Updated by" +msgstr "" + +#. module: sql_export +#: field:sql.export,write_date:0 +#: field:sql.file.wizard,write_date:0 +msgid "Last Updated on" +msgstr "" + +#. module: sql_export +#: field:sql.export,name:0 +msgid "Name" +msgstr "" + +#. module: sql_export +#: field:sql.export,query:0 +msgid "Query" +msgstr "" + +#. module: sql_export +#: model:ir.actions.act_window,name:sql_export.sql_export_tree_action +#: view:sql.export:sql_export.sql_export_view_tree +msgid "SQL Export" +msgstr "" + +#. module: sql_export +#: model:ir.model,name:sql_export.model_sql_export +#: view:sql.export:sql_export.sql_export_view_form +msgid "SQL export" +msgstr "" + +#. module: sql_export +#: model:ir.ui.menu,name:sql_export.sql_export_menu +#: model:ir.ui.menu,name:sql_export.sql_export_menu_view +msgid "Sql Export" +msgstr "" + +#. module: sql_export +#: model:res.groups,name:sql_export.group_sql_request_editor +msgid "Sql Request Editor" +msgstr "" + +#. module: sql_export +#: code:addons/sql_export/sql_export.py:132 +#, python-format +msgid "The Sql query is not valid." +msgstr "" + +#. module: sql_export +#: constraint:sql.export:0 +msgid "The query you want make is not allowed : prohibited actions (delete, drop, insert, alter, truncate, execute, create, update)" +msgstr "" + +#. module: sql_export +#: help:sql.export,query:0 +msgid "You can't use the following word : delete, drop, create, insert, alter, truncate, execute, update" +msgstr "" + diff --git a/sql_export/security/ir.model.access.csv b/sql_export/security/ir.model.access.csv new file mode 100644 index 000000000..65b1b713f --- /dev/null +++ b/sql_export/security/ir.model.access.csv @@ -0,0 +1,3 @@ +"id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink" +"access_sql_export_all","access_sql_export_all","model_sql_export",,1,0,0,0 +"access_sql_export_editor","access_sql_export_editor","model_sql_export",group_sql_request_editor,1,1,1,1 diff --git a/sql_export/security/sql_export_security.xml b/sql_export/security/sql_export_security.xml new file mode 100644 index 000000000..9338bfc40 --- /dev/null +++ b/sql_export/security/sql_export_security.xml @@ -0,0 +1,21 @@ + + + + + + Sql Request Editor + + + + + SQL Export users and groups rules + + + + + + ['|', ('user_ids','=',user.id), ('group_ids','in', [x.id for x in user.groups_id])] + + + + diff --git a/sql_export/sql_export.py b/sql_export/sql_export.py new file mode 100644 index 000000000..26aef2fd3 --- /dev/null +++ b/sql_export/sql_export.py @@ -0,0 +1,149 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2015 Akretion (). +# +# 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 . +# +############################################################################## + +import StringIO +import base64 +import datetime +import re +from openerp import models, fields, api, _, exceptions +from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT +import uuid + + +class SqlExport(models.Model): + _name = "sql.export" + _description = "SQL export" + + PROHIBITED_WORDS = [ + 'delete', + 'drop', + 'insert', + 'alter', + 'truncate', + 'execute', + 'create', + 'update' + ] + + @api.multi + def _check_query_allowed(self): + for obj in self: + query = obj.query.lower() + for word in self.PROHIBITED_WORDS: + expr = r'\b%s\b' % word + is_not_safe = re.search(expr, query) + if is_not_safe: + return False + return True + + @api.model + def _get_editor_group(self): + ir_model_obj = self.env['ir.model.data'] + return [ir_model_obj.xmlid_to_res_id( + 'sql_export.group_sql_request_editor')] + + name = fields.Char('Name', required=True) + query = fields.Text( + 'Query', + required=True, + help="You can't use the following word : delete, drop, create, " + "insert, alter, truncate, execute, update") + copy_options = fields.Char( + 'Copy Options', + required=True, + default="CSV HEADER DELIMITER ';'") + group_ids = fields.Many2many( + 'res.groups', + 'groups_sqlquery_rel', + 'sql_id', + 'group_id', + 'Allowed Groups', + default=_get_editor_group) + user_ids = fields.Many2many( + 'res.users', + 'users_sqlquery_rel', + 'sql_id', + 'user_id', + 'Allowed Users') + + _constraints = [(_check_query_allowed, + 'The query you want make is not allowed : prohibited ' + 'actions (%s)' % ', '.join(PROHIBITED_WORDS), + ['query'])] + + @api.multi + def export_sql_query(self): + for obj in self: + today = datetime.datetime.now() + today_tz = fields.Datetime.context_timestamp( + obj, today) + date = today_tz.strftime(DEFAULT_SERVER_DATETIME_FORMAT) + output = StringIO.StringIO() + query = "COPY (" + obj.query + ") TO STDOUT WITH " + \ + obj.copy_options + name = 'export_query_%s' % uuid.uuid1().hex + self.env.cr.execute("SAVEPOINT %s" % name) + try: + self.env.cr.copy_expert(query, output) + output.getvalue() + new_output = base64.b64encode(output.getvalue()) + output.close() + finally: + self.env.cr.execute("ROLLBACK TO SAVEPOINT %s" % name) + wiz = self.env['sql.file.wizard'].create( + { + 'binary_file': new_output, + 'file_name': obj.name + '_' + date + '.csv'}) + return { + 'view_type': 'form', + 'view_mode': 'form', + 'res_model': 'sql.file.wizard', + 'res_id': wiz.id, + 'type': 'ir.actions.act_window', + 'target': 'new', + 'context': self._context, + 'nodestroy': True, + } + + @api.model + def check_query_syntax(self, vals): + if vals.get('query', False): + vals['query'] = vals['query'].strip() + if vals['query'][-1] == ';': + vals['query'] = vals['query'][:-1] + try: + self.env.cr.execute(vals['query']) + except: + raise exceptions.Warning( + _("The Sql query is not valid.")) + finally: + self.env.cr.rollback() + return vals + + @api.multi + def write(self, vals): + vals = self.check_query_syntax(vals) + return super(SqlExport, self).write(vals) + + @api.model + def create(self, vals): + vals = self.check_query_syntax(vals) + return super(SqlExport, self).create(vals) diff --git a/sql_export/sql_export_view.xml b/sql_export/sql_export_view.xml new file mode 100644 index 000000000..dbac8e882 --- /dev/null +++ b/sql_export/sql_export_view.xml @@ -0,0 +1,56 @@ + + + + + + + Sql_export_form_view + sql.export + +
+ + +