Skip to content

Commit 3f15227

Browse files
committed
[ADD] fastapi_encrypted_errors
1 parent c89c938 commit 3f15227

18 files changed

+791
-0
lines changed

fastapi_encrypted_errors/README.rst

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
========================
2+
FastAPI Encrypted Errors
3+
========================
4+
5+
..
6+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7+
!! This file is generated by oca-gen-addon-readme !!
8+
!! changes will be overwritten. !!
9+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10+
!! source digest: sha256:296fbef824a5eb64e9bbaedc382ef15e41d146f0cc59d458c0d76de46a54358e
11+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12+
13+
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
14+
:target: https://odoo-community.org/page/development-status
15+
:alt: Beta
16+
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
17+
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
18+
:alt: License: AGPL-3
19+
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github
20+
:target: https://github.com/OCA/rest-framework/tree/16.0/fastapi_encrypted_errors
21+
:alt: OCA/rest-framework
22+
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
23+
:target: https://translation.odoo-community.org/projects/rest-framework-16-0/rest-framework-16-0-fastapi_encrypted_errors
24+
:alt: Translate me on Weblate
25+
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
26+
:target: https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&target_branch=16.0
27+
:alt: Try me on Runboat
28+
29+
|badge1| |badge2| |badge3| |badge4| |badge5|
30+
31+
This module adds a "ref" field in the error response of FastAPI.
32+
This field is an AES encrypted string that contains the error message / traceback.
33+
This encrypted string can be decrypted using the endpoint decrypt error wizard.
34+
35+
**Table of contents**
36+
37+
.. contents::
38+
:local:
39+
40+
Usage
41+
=====
42+
43+
First you have to enable the encryption for an endpoint by checking the `Encrypt Errors` checkbox
44+
in the endpoint configuration.
45+
46+
To decrypt an error message, you can use the "Decrypt Error" wizard in the
47+
FastAPI menu.
48+
49+
You can regenerate a new key by clicking on the "Regenerate Key" button next to the `Errors Secret Key` field.
50+
51+
Bug Tracker
52+
===========
53+
54+
Bugs are tracked on `GitHub Issues <https://github.com/OCA/rest-framework/issues>`_.
55+
In case of trouble, please check there if your issue has already been reported.
56+
If you spotted it first, help us to smash it by providing a detailed and welcomed
57+
`feedback <https://github.com/OCA/rest-framework/issues/new?body=module:%20fastapi_encrypted_errors%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
58+
59+
Do not contact contributors directly about support or help with technical issues.
60+
61+
Credits
62+
=======
63+
64+
Authors
65+
~~~~~~~
66+
67+
* Akretion
68+
69+
Contributors
70+
~~~~~~~~~~~~
71+
72+
* `Akretion <https://www.akretion.com>`_:
73+
74+
* Florian Mounier
75+
76+
Maintainers
77+
~~~~~~~~~~~
78+
79+
This module is maintained by the OCA.
80+
81+
.. image:: https://odoo-community.org/logo.png
82+
:alt: Odoo Community Association
83+
:target: https://odoo-community.org
84+
85+
OCA, or the Odoo Community Association, is a nonprofit organization whose
86+
mission is to support the collaborative development of Odoo features and
87+
promote its widespread use.
88+
89+
.. |maintainer-paradoxxxzero| image:: https://github.com/paradoxxxzero.png?size=40px
90+
:target: https://github.com/paradoxxxzero
91+
:alt: paradoxxxzero
92+
93+
Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:
94+
95+
|maintainer-paradoxxxzero|
96+
97+
This module is part of the `OCA/rest-framework <https://github.com/OCA/rest-framework/tree/16.0/fastapi_encrypted_errors>`_ project on GitHub.
98+
99+
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

fastapi_encrypted_errors/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from . import models
2+
from . import wizards
3+
from . import fastapi_dispatcher
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright 2024 Akretion (http://www.akretion.com).
2+
# @author Florian Mounier <[email protected]>
3+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
4+
5+
{
6+
"name": "FastAPI Encrypted Errors",
7+
"summary": "Adds encrypted error messages to FastAPI error responses.",
8+
"version": "16.0.1.0.0",
9+
"license": "AGPL-3",
10+
"author": "Akretion,Odoo Community Association (OCA)",
11+
"maintainers": ["paradoxxxzero"],
12+
"website": "https://github.com/OCA/rest-framework",
13+
"depends": [
14+
"fastapi",
15+
"extendable_fastapi", # Just to make sure dispatcher won't be overriden
16+
],
17+
"data": [
18+
"security/ir.model.access.csv",
19+
"views/fastapi_endpoint_views.xml",
20+
"wizards/wizard_fastapi_decrypt_errors_views.xml",
21+
],
22+
"demo": [],
23+
"external_dependencies": {
24+
"python": ["cryptography"],
25+
},
26+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2024 Akretion (http://www.akretion.com).
2+
# @author Florian Mounier <[email protected]>
3+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
4+
5+
from odoo.http import _dispatchers
6+
7+
from odoo.addons.fastapi.error_handlers import convert_exception_to_status_body
8+
9+
10+
# Inherit from last dispatcher of fastapi registered
11+
# This handles extendable_fastapi overloaded dispatcher for instance
12+
class FastApiDispatcher(_dispatchers["fastapi"]):
13+
routing_type = "fastapi"
14+
15+
def handle_error(self, exc):
16+
environ = self._get_environ()
17+
root_path = "/" + environ["PATH_INFO"].split("/")[1]
18+
fastapi_endpoint = (
19+
self.request.env["fastapi.endpoint"]
20+
.sudo()
21+
.search([("root_path", "=", root_path)])
22+
)
23+
if fastapi_endpoint.encrypt_errors:
24+
headers = getattr(exc, "headers", None)
25+
status_code, body = convert_exception_to_status_body(exc)
26+
if body:
27+
body["ref"] = fastapi_endpoint._encrypt_error(exc)
28+
return self.request.make_json_response(
29+
body, status=status_code, headers=headers
30+
)
31+
32+
return super().handle_error(exc)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import fastapi_endpoint
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2024 Akretion (http://www.akretion.com).
2+
# @author Florian Mounier <[email protected]>
3+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
4+
import traceback
5+
import zlib
6+
7+
from cryptography.fernet import Fernet
8+
9+
from odoo import fields, models
10+
11+
12+
class FastapiEndpoint(models.Model):
13+
_inherit = "fastapi.endpoint"
14+
15+
encrypt_errors = fields.Boolean(
16+
string="Encrypt Errors",
17+
help="Encrypt errors before sending them to the client.",
18+
)
19+
encrypted_errors_secret_key = fields.Char(
20+
string="Errors Secret Key",
21+
help="The secret key used to encrypt errors before sending them to the client.",
22+
default=lambda _: Fernet.generate_key(),
23+
readonly=True,
24+
)
25+
26+
def action_generate_encrypted_errors_secret_key(self):
27+
for record in self:
28+
record.encrypted_errors_secret_key = Fernet.generate_key()
29+
30+
def _encrypt_error(self, exc):
31+
self.ensure_one()
32+
if not self.encrypt_errors or not self.encrypted_errors_secret_key:
33+
return
34+
35+
# Get full traceback
36+
error = "".join(traceback.format_exception(exc))
37+
# zlib compression works quite well on tracebacks
38+
error = zlib.compress(error.encode("utf-8"))
39+
f = Fernet(self.encrypted_errors_secret_key)
40+
return f.encrypt(error)
41+
42+
def _decrypt_error(self, error):
43+
self.ensure_one()
44+
if not self.encrypt_errors or not self.encrypted_errors_secret_key:
45+
return
46+
47+
f = Fernet(self.encrypted_errors_secret_key)
48+
error = f.decrypt(error)
49+
return zlib.decompress(error).decode("utf-8")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
* `Akretion <https://www.akretion.com>`_:
2+
3+
* Florian Mounier
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This module adds a "ref" field in the error response of FastAPI.
2+
This field is an AES encrypted string that contains the error message / traceback.
3+
This encrypted string can be decrypted using the endpoint decrypt error wizard.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
First you have to enable the encryption for an endpoint by checking the `Encrypt Errors` checkbox
2+
in the endpoint configuration.
3+
4+
To decrypt an error message, you can use the "Decrypt Error" wizard in the
5+
FastAPI menu.
6+
7+
You can regenerate a new key by clicking on the "Regenerate Key" button next to the `Errors Secret Key` field.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
2+
access_fastapi_wizard_auth_partner_impersonate,wizard_fastapi_decrypt_errors,model_wizard_fastapi_decrypt_errors,fastapi.group_fastapi_manager,1,1,1,1

0 commit comments

Comments
 (0)