From e6fe35d14eaa38249b5272657b89f4059d9b2e34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Tue, 5 Apr 2022 22:39:09 +0000 Subject: [PATCH] EASYRSA global object --- api/kiwi_vpn_api/easyrsa.py | 27 ++++++--------------------- api/kiwi_vpn_api/routers/device.py | 4 ++-- 2 files changed, 8 insertions(+), 23 deletions(-) diff --git a/api/kiwi_vpn_api/easyrsa.py b/api/kiwi_vpn_api/easyrsa.py index ab2d1f6..aeb2a21 100644 --- a/api/kiwi_vpn_api/easyrsa.py +++ b/api/kiwi_vpn_api/easyrsa.py @@ -4,7 +4,6 @@ Python interface to EasyRSA CA. from __future__ import annotations -import functools import subprocess from enum import Enum, auto from pathlib import Path @@ -140,20 +139,6 @@ class EasyRSA: None: {}, } - @classmethod - @functools.lru_cache - def _load(cls) -> EasyRSA: - return cls() - - @classmethod - @property - def _(cls) -> EasyRSA: - """ - Get the singleton - """ - - return cls._load() - @property def output_directory(self) -> Path: """ @@ -300,14 +285,14 @@ class EasyRSA: ) +EASYRSA = EasyRSA() + + # some basic test if __name__ == "__main__": - easy_rsa = EasyRSA() - easy_rsa.init_pki() - - ca = easy_rsa.build_ca() - server = easy_rsa.issue(CertificateType.server) + ca = EASYRSA.build_ca() + server = EASYRSA.issue(CertificateType.server) client = None # check if configured @@ -316,7 +301,7 @@ if __name__ == "__main__": Connection.connect(current_config.db.uri) if (device := Device.get(1)) is not None: - client = easy_rsa.issue( + client = EASYRSA.issue( dn=DistinguishedName.build(device) ) diff --git a/api/kiwi_vpn_api/routers/device.py b/api/kiwi_vpn_api/routers/device.py index 8b38410..1b45a33 100644 --- a/api/kiwi_vpn_api/routers/device.py +++ b/api/kiwi_vpn_api/routers/device.py @@ -5,7 +5,7 @@ from fastapi import APIRouter, Depends, HTTPException, status from ..db import Device, DeviceCreate, DeviceRead, User -from ..easyrsa import DistinguishedName, EasyRSA +from ..easyrsa import EASYRSA, DistinguishedName from ._common import (Responses, get_current_user, get_device_by_id, get_user_by_name) @@ -111,7 +111,7 @@ async def request_certificate_issuance( if device.approved: # issue the certificate immediately - if (certificate := EasyRSA._.issue( + if (certificate := EASYRSA.issue( dn=DistinguishedName.build(device) )) is not None: device.expiry = certificate.not_valid_after