[ADD] base: add support for custom imbus & cron_trigger postgres notifications

This patch provides the possibility to implement a custom security layer on top
of Odoo's imbus notification system and ir_cron live triggering system.

The key addition is the `ODOO_NOTIFY_FUNCTION` environment variable (opt-in),
which can now define a custom postgresql function to be called instead of the
default `pg_notify` function. This allows for greater flexibility and control
over the notification and triggering mechanisms within Odoo.

closes odoo/odoo#140089

Signed-off-by: Fabien Meghazi (fme) <fme@odoo.com>
This commit is contained in:
Fabien Meghazi
2023-10-28 19:59:03 +00:00
parent b28c4f2b1d
commit 2738f8972e
2 changed files with 13 additions and 3 deletions
+7 -2
View File
@@ -3,11 +3,12 @@ import contextlib
import datetime
import json
import logging
import os
import random
import selectors
import threading
import time
from psycopg2 import InterfaceError
from psycopg2 import InterfaceError, sql
import odoo
from odoo import api, fields, models
@@ -20,6 +21,9 @@ _logger = logging.getLogger(__name__)
# longpolling timeout connection
TIMEOUT = 50
# custom function to call instead of default PostgreSQL's `pg_notify`
ODOO_NOTIFY_FUNCTION = os.getenv('ODOO_NOTIFY_FUNCTION', 'pg_notify')
#----------------------------------------------------------
# Bus
#----------------------------------------------------------
@@ -77,7 +81,8 @@ class ImBus(models.Model):
@self.env.cr.postcommit.add
def notify():
with odoo.sql_db.db_connect('postgres').cursor() as cr:
cr.execute("notify imbus, %s", (json_dump(list(channels)),))
query = sql.SQL("SELECT {}('imbus', %s)").format(sql.Identifier(ODOO_NOTIFY_FUNCTION))
cr.execute(query, (json_dump(list(channels)), ))
@api.model
def _sendone(self, channel, notification_type, message):
+6 -1
View File
@@ -7,6 +7,7 @@ import psycopg2
import pytz
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
from psycopg2 import sql
import odoo
from odoo import api, fields, models, _
@@ -17,6 +18,9 @@ _logger = logging.getLogger(__name__)
BASE_VERSION = odoo.modules.get_manifest('base')['version']
MAX_FAIL_TIME = timedelta(hours=5) # chosen with a fair roll of the dice
# custom function to call instead of default PostgreSQL's `pg_notify`
ODOO_NOTIFY_FUNCTION = os.getenv('ODOO_NOTIFY_FUNCTION', 'pg_notify')
class BadVersion(Exception):
pass
@@ -515,7 +519,8 @@ class ir_cron(models.Model):
ir_cron modification and on trigger creation (regardless of call_at)
"""
with odoo.sql_db.db_connect('postgres').cursor() as cr:
cr.execute('NOTIFY cron_trigger, %s', [self.env.cr.dbname])
query = sql.SQL("SELECT {}('cron_trigger', %s)").format(sql.Identifier(ODOO_NOTIFY_FUNCTION))
cr.execute(query, [self.env.cr.dbname])
_logger.debug("cron workers notified")