diff --git a/api/kiwi_vpn_api/easyrsa.py b/api/kiwi_vpn_api/easyrsa.py index 827fad1..6edda1f 100644 --- a/api/kiwi_vpn_api/easyrsa.py +++ b/api/kiwi_vpn_api/easyrsa.py @@ -13,7 +13,7 @@ from passlib import pwd from pydantic import BaseModel from .config import CertificateAlgo, Config, Settings -from .db import Device +from .db import Connection, Device class DistinguishedName(BaseModel): @@ -252,12 +252,25 @@ if __name__ == "__main__": ca = easy_rsa.build_ca() server = easy_rsa.issue("server") - # client = easy_rsa.issue(cert_type="client", cn="kiwi-vpn-client") + client = None + + # check if configured + if (current_config := Config._) is not None: + # connect to database + Connection.connect(current_config.db.uri) + + if (device := Device.get(1)) is not None: + with Connection.session as db: + db.add(device) + dn = DistinguishedName.build(device) + + client = easy_rsa.issue("client", dn) date_format, encoding = "%Y%m%d%H%M%SZ", "ascii" - for cert in (ca, server): - print(cert.get_subject().CN) - print(cert.get_signature_algorithm().decode(encoding)) - print(datetime.strptime( - cert.get_notAfter().decode(encoding), date_format)) + for cert in (ca, server, client): + if cert is not None: + print(cert.get_subject().CN) + print(cert.get_signature_algorithm().decode(encoding)) + print(datetime.strptime( + cert.get_notAfter().decode(encoding), date_format))