From 857e400d41300eea505b85b4985bcd033c1b643d Mon Sep 17 00:00:00 2001 From: Stephane de Labrusse Date: Fri, 12 Apr 2024 12:10:02 +0200 Subject: [PATCH] Migration from Esmith smarthosts to sqlite relayrules (#102) Add import-module/50import_smarthosts action for importing smarthosts into relayrules table --- .../actions/import-module/50import_smarthosts | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100755 imageroot/actions/import-module/50import_smarthosts diff --git a/imageroot/actions/import-module/50import_smarthosts b/imageroot/actions/import-module/50import_smarthosts new file mode 100755 index 00000000..4da9e789 --- /dev/null +++ b/imageroot/actions/import-module/50import_smarthosts @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 + +# +# Copyright (C) 2024 Nethesis S.r.l. +# SPDX-License-Identifier: GPL-3.0-or-later +# + +import sys +import json +import agent +import mail + +# Global stdout redirection: +sys.stdout = sys.stderr + +sdb = mail.pcdb_connect() +sdb.set_trace_callback(lambda m: print(agent.SD_DEBUG + m)) + +# import the Esmith smarthosts database +with sdb: + cur = sdb.cursor() + + print(agent.SD_INFO + "Importing smarthosts...", file=sys.stderr) + for smart in json.load(open('smarthosts.json')): + + vals = { + 'rule_type':smart['type'], + 'rule_subject': smart['name'].removeprefix('@'), + 'host': smart['props'].get('Host'), + 'port': smart['props'].get('Port'), + 'mandatory_tls': 'encrypt' if smart['props'].get('TlsStatus') == 'enabled' else 'may', + 'username': smart['props'].get('Username'), + 'password': smart['props'].get('Password'), + 'enabled': True if smart['props'].get('status') == 'enabled' else False, + } + cur.execute("""INSERT INTO relayrules (rule_type, rule_subject , host, port, tls, username, password, enabled) + VALUES (:rule_type, :rule_subject, :host, :port, :mandatory_tls, :username, :password, :enabled)""", vals)