Skip to content

Commit

Permalink
Migration from Esmith smarthosts to sqlite relayrules (#102)
Browse files Browse the repository at this point in the history
Add import-module/50import_smarthosts action for importing smarthosts into relayrules table
  • Loading branch information
stephdl authored Apr 12, 2024
1 parent 65351c5 commit 857e400
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions imageroot/actions/import-module/50import_smarthosts
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 857e400

Please sign in to comment.