email: Drop atomic writing to a file

Not very critical. Reduce complexity. Re-implement later at framework level.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2022-02-16 11:42:37 -08:00 committed by James Valleroy
parent 48f17c4eeb
commit 6cfa0589a7
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
2 changed files with 9 additions and 39 deletions

View File

@ -1,30 +0,0 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
import contextlib
import os
import uuid
@contextlib.contextmanager
def atomically_rewrite(filepath):
successful = False
tmp = '%s.%s.plinth-tmp' % (filepath, uuid.uuid4().hex)
fd = open(tmp, 'x')
try:
# Let client write to a temporary file
yield fd
successful = True
finally:
fd.close()
try:
if successful:
# Invoke rename(2) to atomically replace the original
os.rename(tmp, filepath)
finally:
# Delete temp file
try:
os.unlink(tmp)
except FileNotFoundError:
pass

View File

@ -1,7 +1,9 @@
"""TLS configuration for postfix and dovecot."""
# SPDX-License-Identifier: AGPL-3.0-or-later # SPDX-License-Identifier: AGPL-3.0-or-later
"""TLS configuration for postfix and dovecot."""
from .. import interproc, postfix import pathlib
from .. import postfix
# Mozilla Guideline v5.6, Postfix 1.17.7, OpenSSL 1.1.1d, intermediate # Mozilla Guideline v5.6, Postfix 1.17.7, OpenSSL 1.1.1d, intermediate
# Generated 2021-08 # Generated 2021-08
@ -41,11 +43,11 @@ _postfix_config = {
def set_postfix_config(primary_domain, all_domains): def set_postfix_config(primary_domain, all_domains):
"""Set postfix configuration for TLS certificates.""" """Set postfix configuration for TLS certificates."""
tls_sni_map = '/etc/postfix/freedombox-tls-sni.map' tls_sni_map = pathlib.Path('/etc/postfix/freedombox-tls-sni.map')
config = dict(_postfix_config) config = dict(_postfix_config)
config.update({ config.update({
'tls_server_sni_maps': 'tls_server_sni_maps':
tls_sni_map, str(tls_sni_map),
'smtpd_tls_chain_files': 'smtpd_tls_chain_files':
f'/etc/postfix/letsencrypt/{primary_domain}/chain.pem' f'/etc/postfix/letsencrypt/{primary_domain}/chain.pem'
}) })
@ -54,8 +56,7 @@ def set_postfix_config(primary_domain, all_domains):
for domain in all_domains: for domain in all_domains:
content += f'{domain} /etc/postfix/letsencrypt/{domain}/chain.pem\n' content += f'{domain} /etc/postfix/letsencrypt/{domain}/chain.pem\n'
with interproc.atomically_rewrite(tls_sni_map) as file_handle: tls_sni_map.write_text(content)
file_handle.write(content)
def set_dovecot_config(primary_domain, all_domains): def set_dovecot_config(primary_domain, all_domains):
@ -71,6 +72,5 @@ local_name {domain} {{
ssl_key = </etc/dovecot/letsencrypt/{domain}/privkey.pem ssl_key = </etc/dovecot/letsencrypt/{domain}/privkey.pem
}} }}
''' '''
dovecot_cert_config = '/etc/dovecot/conf.d/91-freedombox-tls.conf' cert_config = pathlib.Path('/etc/dovecot/conf.d/91-freedombox-tls.conf')
with interproc.atomically_rewrite(dovecot_cert_config) as file_handle: cert_config.write_text(content)
file_handle.write(content)