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.

56 lines
1.7 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. if not version:
  7. return
  8. # Don't act when coming from version 9
  9. cr.execute("SELECT 1 FROM pg_class WHERE relname = 'res_banned_remote'")
  10. if not cr.fetchone():
  11. return
  12. # Fix typo across DB
  13. cr.execute(
  14. """ UPDATE res_authentication_attempt
  15. SET result = 'successful'
  16. WHERE result = 'successfull'""",
  17. )
  18. # Store whitelist IPs in new format
  19. cr.execute(
  20. """ SELECT remote
  21. FROM res_banned_remote
  22. WHERE active IS FALSE""",
  23. )
  24. remotes = {record[0] for record in cr.fetchall()}
  25. try:
  26. with cr.savepoint():
  27. cr.execute(
  28. "INSERT INTO ir_config_parameter (key, value) VALUES (%s, %s)",
  29. (
  30. "auth_brute_force.whitelist_remotes",
  31. ",".join(remotes),
  32. ),
  33. )
  34. except IntegrityError:
  35. # Parameter already exists
  36. cr.execute(
  37. "SELECT value FROM ir_config_parameter WHERE key = %s",
  38. ("auth_brute_force.whitelist_remotes",)
  39. )
  40. current = set(cr.fetchall()[0][0].split(","))
  41. cr.execute(
  42. "UPDATE ir_config_parameter SET value = %s WHERE key = %s",
  43. (",".join(current | remotes),
  44. "auth_brute_force.whitelist_remotes"),
  45. )
  46. # Update the configured IP limit parameter
  47. cr.execute(
  48. "UPDATE ir_config_parameter SET key = %s WHERE key = %s",
  49. (
  50. "auth_brute_force.whitelist_remotes",
  51. "auth_brute_force.max_by_ip",
  52. )
  53. )