Skip to content

Commit

Permalink
Fix FileNotFoundError when writing nginx keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvolodin7 committed Dec 10, 2023
1 parent 7441391 commit 6ec0cc9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

To see unreleased changes, please see the [CHANGELOG on the master branch](https://github.com/gufolabs/gufo_thor/blob/master/CHANGELOG.md) guide.

## [Unreleased]

### Fixed

* `FileNotFoundError` when writing nginx keys.

### Changed

* `write_file` accepts content as `str` and `bytes`.

## 0.3.0 - 2023-11-28

### Added
Expand Down
16 changes: 7 additions & 9 deletions src/gufo/thor/services/nginx.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from ..config import Config, ServiceConfig
from ..error import CancelExecution
from ..log import logger
from ..utils import write_file
from .base import BaseService
from .login import login
from .traefik import traefik
Expand Down Expand Up @@ -144,9 +145,9 @@ def _rebuild_certificate(self: "NginxService", config: Config) -> None:
"""Rebuild SSL certificates."""
from gufo.acme.clients.base import AcmeClient

key_path = "etc/nginx/ssl/noc.key"
csr_path = "etc/nginx/ssl/noc.csr"
cert_path = "etc/nginx/ssl/noc.crt"
key_path = Path("etc", "nginx", "ssl", "noc.key")
csr_path = Path("etc", "nginx", "ssl", "noc.csr")
cert_path = Path("etc", "nginx", "ssl", "noc.crt")

di = DOMAINS.get(config.expose.domain_name)
if di is None:
Expand All @@ -165,18 +166,15 @@ def _rebuild_certificate(self: "NginxService", config: Config) -> None:
# Generate key
logger.warning("Generating private key: %s", key_path)
private_key = AcmeClient.get_domain_private_key()
with open(key_path, "wb") as fp:
fp.write(private_key)
write_file(key_path, private_key)
# Create CSR
logger.warning("Generating certificate signing request: %s", csr_path)
csr = AcmeClient.get_domain_csr(config.expose.domain_name, private_key)
with open(csr_path, "wb") as fp:
fp.write(csr)
write_file(csr_path, csr)
# Sign CSR
logger.warning("Signing certificate: %s", cert_path)
cert = self._get_signed_csr(di.csr_proxy, csr)
with open(cert_path, "wb") as fp:
fp.write(cert)
write_file(cert_path, cert)
# Write subj
logger.warning("Writing %s", self.SUBJ_PATH)
with open(self.SUBJ_PATH, "w") as fp:
Expand Down
7 changes: 4 additions & 3 deletions src/gufo/thor/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
import os
import shutil
from pathlib import Path
from typing import Optional
from typing import Optional, Union

# Gufo Thor modules
from .log import logger


def write_file(
path: Path, content: str, backup_path: Optional[Path] = None
path: Path, content: Union[str, bytes], backup_path: Optional[Path] = None
) -> bool:
"""
Write data to file.
Expand All @@ -42,7 +42,8 @@ def write_file(
if backup_path:
shutil.move(path, backup_path)
logger.warning("Writing file %s", path)
with open(path, "w") as fp:
mode = "w" if isinstance(content, str) else "wb"
with open(path, mode) as fp:
fp.write(content)
return True

Expand Down

0 comments on commit 6ec0cc9

Please sign in to comment.