Skip to content

Commit

Permalink
fix: Ensure writing the certificate is indeed atomic by staying in th…
Browse files Browse the repository at this point in the history
…e target directory
  • Loading branch information
ngrewe committed Jul 22, 2020
1 parent 05d4193 commit 795bb0a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 2 additions & 2 deletions vault_ssh_renew/cert.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ def get(cls, key_path: Path, cert_path: Path) -> HostCertificateInit:
return cast(HostCertificateInit, cls(key_path, cert_path))

def read(self) -> HostCertificateValidate:
self.public_key = self._key_path.read_text(encoding='utf-8')
self.public_key = self._key_path.read_text(encoding="utf-8")
if not self._cert_path.exists():
return HostCertificateStatusNoCert(self, True)
certificate_contents = self._cert_path.read_text(encoding='utf-8').split(" ")
certificate_contents = self._cert_path.read_text(encoding="utf-8").split(" ")
if len(certificate_contents) != 2:
raise RenewError("Invalid certificate file")
self.cert_type = certificate_contents[0]
Expand Down
4 changes: 3 additions & 1 deletion vault_ssh_renew/vault.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ def renew(self) -> VaultRenewDone:

def write_certificate(self):
assert self._signed_key is not None
with NamedTemporaryFile(delete=False) as tmp:
with NamedTemporaryFile(
delete=False, dir=os.path.dirname(str(self._cert_path))
) as tmp:
tmp.write(self._signed_key.encode("utf-8"))
tmp.flush()
shutil.move(tmp.name, str(self._cert_path))
Expand Down

0 comments on commit 795bb0a

Please sign in to comment.