You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.5 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 Tecnativa - Jairo Llopis
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  4. from psycopg2 import IntegrityError
  5. def migrate(cr, version):
  6. # Fix typo across DB
  7. cr.execute(
  8. """ UPDATE res_authentication_attempt
  9. SET result = 'successful'
  10. WHERE result = 'successfull'""",
  11. )
  12. # Store whitelist IPs in new format
  13. cr.execute(
  14. """ SELECT remote
  15. FROM res_banned_remote
  16. WHERE active IS FALSE""",
  17. )
  18. remotes = {record[0] for record in cr.fetchall()}
  19. try:
  20. with cr.savepoint():
  21. cr.execute(
  22. "INSERT INTO ir_config_parameter (key, value) VALUES (%s, %s)",
  23. (
  24. "auth_brute_force.whitelist_remotes",
  25. ",".join(remotes),
  26. ),
  27. )
  28. except IntegrityError:
  29. # Parameter already exists
  30. cr.execute(
  31. "SELECT value FROM ir_config_parameter WHERE key = %s",
  32. ("auth_brute_force.whitelist_remotes",)
  33. )
  34. current = set(cr.fetchall()[0][0].split(","))
  35. cr.execute(
  36. "UPDATE ir_config_parameter SET value = %s WHERE key = %s",
  37. (",".join(current | remotes),
  38. "auth_brute_force.whitelist_remotes"),
  39. )
  40. # Update the configured IP limit parameter
  41. cr.execute(
  42. "UPDATE ir_config_parameter SET key = %s WHERE key = %s",
  43. (
  44. "auth_brute_force.whitelist_remotes",
  45. "auth_brute_force.max_by_ip",
  46. )
  47. )