update dkim-key on commit only

master
Alexander Graf 4 years ago
parent 0a594aaa2c
commit 815f47667b

@ -369,6 +369,21 @@ class Config(Base):
value = db.Column(JSONEncoded) value = db.Column(JSONEncoded)
@sqlalchemy.event.listens_for(db.session, 'after_commit')
def store_dkim_key(session):
""" Store DKIM key on commit
"""
for obj in session.identity_map.values():
if isinstance(obj, Domain):
if obj._dkim_key_changed:
file_path = obj._dkim_file()
if obj._dkim_key:
with open(file_path, 'wb') as handle:
handle.write(obj._dkim_key)
elif os.path.exists(file_path):
os.unlink(file_path)
class Domain(Base): class Domain(Base):
""" A DNS domain that has mail addresses associated to it. """ A DNS domain that has mail addresses associated to it.
""" """
@ -425,6 +440,9 @@ class Domain(Base):
max_quota_bytes = db.Column(db.BigInteger(), nullable=False, default=0) max_quota_bytes = db.Column(db.BigInteger(), nullable=False, default=0)
signup_enabled = db.Column(db.Boolean(), nullable=False, default=False) signup_enabled = db.Column(db.Boolean(), nullable=False, default=False)
_dkim_key = None
_dkim_key_changed = False
def _dkim_file(self): def _dkim_file(self):
return app.config['DKIM_PATH'].format( return app.config['DKIM_PATH'].format(
domain=self.name, selector=app.config['DKIM_SELECTOR']) domain=self.name, selector=app.config['DKIM_SELECTOR'])
@ -457,26 +475,28 @@ class Domain(Base):
@property @property
def dkim_key(self): def dkim_key(self):
file_path = self._dkim_file() if self._dkim_key is None:
if os.path.exists(file_path): file_path = self._dkim_file()
with open(file_path, 'rb') as handle: if os.path.exists(file_path):
return handle.read() with open(file_path, 'rb') as handle:
self._dkim_key = handle.read()
else:
self._dkim_key = b''
return self._dkim_key if self._dkim_key else None
@dkim_key.setter @dkim_key.setter
def dkim_key(self, value): def dkim_key(self, value):
file_path = self._dkim_file() old_key = self.dkim_key
if value is None: if value is None:
if os.path.exists(file_path): value = b''
os.unlink(file_path) self._dkim_key_changed = value != old_key
else: self._dkim_key = value
with open(file_path, 'wb') as handle:
handle.write(value)
@property @property
def dkim_publickey(self): def dkim_publickey(self):
dkim_key = self.dkim_key dkim_key = self.dkim_key
if dkim_key: if dkim_key:
return dkim.strip_key(self.dkim_key).decode('utf8') return dkim.strip_key(dkim_key).decode('utf8')
def generate_dkim_key(self): def generate_dkim_key(self):
self.dkim_key = dkim.gen_key() self.dkim_key = dkim.gen_key()

Loading…
Cancel
Save