Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] sql_shell: get easy access to database without having access to server #115744

Draft
wants to merge 3 commits into
base: 16.0
Choose a base branch
from

Conversation

william-andre
Copy link
Contributor

No description provided.

@robodoo
Copy link
Contributor

robodoo commented Mar 17, 2023

@william-andre william-andre force-pushed the 16.0-sql-shell-wan branch 3 times, most recently from 243a376 to d10ce92 Compare March 17, 2023 17:32
@C3POdoo C3POdoo added the RD research & development, internal work label Mar 17, 2023
@legalsylvain
Copy link
Contributor

Hi @william-andre
Nice move !

If you want, you can take a look on some community modules developed some years ago :
https://github.com/OCA/reporting-engine/tree/16.0/sql_export (close with your implémentation)
https://github.com/OCA/reporting-engine/tree/16.0/bi_sql_editor (more technical, allowing to create materialized views to improve performance)

Note : in your current implementation, it is possible to call INSERT, UPDATE, TRUNCATE. Is it intended ?

(I'm available to talk with you if you want some feedbacks, talk about the design, etc...)

@ajepe
Copy link
Contributor

ajepe commented Mar 18, 2023

Hi @william-andre Nice move !

If you want, you can take a look on some community modules developed some years ago : https://github.com/OCA/reporting-engine/tree/16.0/sql_export (close with your implémentation) https://github.com/OCA/reporting-engine/tree/16.0/bi_sql_editor (more technical, allowing to create materialized views to improve performance)

Note : in your current implementation, it is possible to call INSERT, UPDATE, TRUNCATE. Is it intended ?

(I'm available to talk with you if you want some feedbacks, talk about the design, etc...)
The UI is looks better than the available options in the community

@william-andre
Copy link
Contributor Author

william-andre commented Mar 18, 2023

The purpose is indeed to be able to execute any SQL queries, such as INSERT, UPDATE, DELETE FROM, but also EXPLAIN ANALYZE or CREATE INDEX.
It is not intended to be only reading data but also a debugging tool (doing a SELECT is actually the least useful feature of this module since it can already be done using a standard read() through the ORM)
Even though some of these queried can already be done with a server action, it would be easier and faster using this, for the technical support for instance who doesn't always have an easy access to the server.

@legalsylvain
Copy link
Contributor

Hi @ajepe.

The UI is looks better than the available options in the community

Thanks for your quick review. Could you elaborate on this, though ? I don't get your point.

The purpose is indeed to be able to execute any SQL queries, such as INSERT, UPDATE, DELETE FROM, but also EXPLAIN ANALYZE or CREATE INDEX.

I see. Thanks for the explanation.

Even though some of these queried can already be done with a server action

Indeed, but the ORM "protects" data inconsistency. account_move.unlink() will fail if the account move is posted. pos_order_line.write() will faill if french certification is installed.
With this module, it is possible by the UI, to destroy the data, violating ACL, security checks and business checks. It is not a neutral change.

In the other hand, I understand the need, that is valid. But as you propose the PR against a stable version, I think odoo SA should communicate on that topic. I'm sure some integrators will disable this module to prevent their client from accessing this functionality.

In other words, if today, someone would find the possibility to perform SQL write/read queries on the database via UI or xml-rpc, this would be considered as a severe security flaw. @rco-odoo, a point of view on this topic?

Thanks.

@william-andre
Copy link
Contributor Author

You can run any SQL query in a server action by running env.cr.execute. This has always been possible.
This module only makes it easier to get the response from the query.

@legalsylvain
Copy link
Contributor

You can run any SQL query in a server action by running env.cr.execute. This has always been possible.

You're right. Sorry for the noice, then.

self.env.cr.execute(self.query)
self.result = {
'header': [d.name for d in self.env.cr.description],
'rows': self.env.cr.fetchall(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some request (update, delete) doesn't return rows

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note : maybe add a limit to prevent browser crash
'rows': self.env.cr.fetchall()[:1000],

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some request (update, delete) doesn't return rows

They return how many rows were updated, for instance

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note : maybe add a limit to prevent browser crash
'rows': self.env.cr.fetchall()[:1000],

This depends on the browser/computer. Use at your own risks.
The purpose is not to fetch a bajillion rows. The way it is (temporarily) currently implemented would make the server's memory explode too btw.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, I was too quick in testing, I fixed both remarks in the latest commit

Copy link
Contributor

@Levizar Levizar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my 2 cents when reading it

addons/web/static/lib/ace/mode-sql.js Outdated Show resolved Hide resolved
addons/web/static/lib/ace/mode-sql.js Outdated Show resolved Hide resolved
@william-andre william-andre force-pushed the 16.0-sql-shell-wan branch 3 times, most recently from 71b4221 to f1355f5 Compare March 20, 2023 18:20
Iucapad and others added 2 commits March 20, 2023 19:25
This commit adds a new widget to the field registry. This button
is represented with an icon and acts as a boolean field widget.
The label is shown as a tooltip when hovered.

A test file has also been added to verify its behavior.

task #3121078

backport of 1f02263
@william-andre william-andre force-pushed the 16.0-sql-shell-wan branch 3 times, most recently from e2309d2 to f5b1fbf Compare March 21, 2023 12:32
<field name="view_mode">form</field>
</record>

<menuitem name="SQL Shell" id="sql_shell_menu" parent="base.menu_custom" groups="base.group_no_one" action="sql_shell_action" sequence="10000"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<menuitem name="SQL Shell" id="sql_shell_menu" parent="base.menu_custom" groups="base.group_no_one" action="sql_shell_action" sequence="10000"/>
<menuitem name="SQL Shell" id="sql_shell_menu" parent="base.next_id_9" groups="base.group_no_one" action="sql_shell_action" sequence="100"/>

For the time being, this entry is set at the very last position of the technical menu, that doesn't make much sense.
Propose to replace : "Setting > Technical > SQL Shell" by "Setting > Technical > Database Structure > SQL Shell"


from odoo import models, fields, api

class SQLShell(models.Model):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naive question : Is there a reason to not use classic Transient Model like other wizard ?

@legalsylvain
Copy link
Contributor

hi @william-andre. What is the state of this PR ?

Some functional review. Something is wrong if the max rows is small. could you take a look ?

Otherwise, LGTM.

SELECT with max > 40 : OK

screenshot-localhost_8016-2023 08 30-22_34_06

SELECT with max < 40 : KO

image

EXPLAIN ANALYZE : OK (nice feature, BTW)

image

image

Destroy the database : OK
(yes, I know, still possible by server action).

image

Then

image

@william-andre
Copy link
Contributor Author

The issue you raised with "Max Rows" is actually a protection to avoid crashing the browser and making too much traffic on the network; increase the limit at your own risk

And this PR will probably never get accepted because of you TRUNCATE TABLE example. Even if it is already possible in a server action, this makes it a bit too easy to do...


We might implement it someday only for Odoo support and only on duplicate databases.
Partners using their own hosting are welcome to do the same if they want to.

@legalsylvain
Copy link
Contributor

Hi @william-andre, thanks for your answer !

The issue you raised with "Max Rows" is actually a protection to avoid crashing the browser and making too much traffic on the network; increase the limit at your own risk

No : take a look again on my prinscreen. I set 5 to max rows, and an error is raised and I can not see the result. That's not expected, don't you think ?

And this PR will probably never get accepted because of you TRUNCATE TABLE example.

Well, indeed, there is no warning, but we could imagine quite simply to add a warning with some confirmation, if it is a "write" or a "delete" requests. (similar algorithm than the community module that prohibit the non select requests : https://github.com/OCA/reporting-engine/blob/16.0/sql_request_abstract/models/sql_request_mixin.py#L39)

Even if it is already possible in a server action, this makes it a bit too easy to do...

well, go to "application > select product module > click on uninstall" will do basically similar "apocalypse by UI".
IMO if correct warning is displayed, that is OK. Don't you think ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RD research & development, internal work
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants