Add typechecking.

This commit is contained in:
Frédéric Tronel
2025-12-29 15:08:07 +01:00
parent 2cf4c996f9
commit 115fa39618

View File

@@ -39,11 +39,14 @@ import random
import base64 import base64
from cryptography import x509 from cryptography import x509
from cryptography.hazmat.primitives.serialization import Encoding from cryptography.hazmat.primitives.serialization import Encoding
from dateutil.parser import parse
from typeguard import typechecked
import requests import requests
import coloredlogs import coloredlogs
def create_nonce(length: int=45): @typechecked
def create_nonce(length: int=45) -> str:
""" """
Generate a random nonce string. Generate a random nonce string.
@@ -72,7 +75,9 @@ def create_nonce(length: int=45):
return nonce return nonce
def get_bearer(hostname: str, verify: bool, username: str, password:str):
@typechecked
def get_bearer(hostname: str, verify: bool, username: str, password:str) -> str:
""" """
Retrieve an OAuth bearer token for authentication with a HP CDM server. Retrieve an OAuth bearer token for authentication with a HP CDM server.
@@ -170,8 +175,10 @@ def get_bearer(hostname: str, verify: bool, username: str, password:str):
return bearer return bearer
@typechecked
def get_csr(hostname: str, verify: bool, username: str, password: str, ou: str, org: str, city:str, def get_csr(hostname: str, verify: bool, username: str, password: str, ou: str, org: str, city:str,
state: str, country: str, filename: str): state: str, country: str, filename: str) -> None:
""" """
Generate a Certificate Signing Request (CSR) for a HP CDM server. Generate a Certificate Signing Request (CSR) for a HP CDM server.
@@ -252,12 +259,16 @@ def get_csr(hostname: str, verify: bool, username: str, password: str, ou: str,
finished = True finished = True
csr = csr['certificateData'] csr = csr['certificateData']
print(csr)
logger.debug('CSR:\n %s', csr)
with open(filename, 'w+', encoding='utf-8') as f: with open(filename, 'w+', encoding='utf-8') as f:
f.write(csr) f.write(csr)
def install_certificate(hostname, verify, username, password, filename, bearer=None):
@typechecked
def install_certificate(hostname: str, verify: bool, username: str, password: str, filename:str,
bearer=None) -> None:
""" """
Install a certificate on a HP CDM server. Install a certificate on a HP CDM server.
@@ -333,7 +344,10 @@ def install_certificate(hostname, verify, username, password, filename, bearer=N
logger.info('Certificate successfully installed.') logger.info('Certificate successfully installed.')
def get_certificates(hostname, verify, username, password, bearer=None):
@typechecked
def get_certificates(hostname:str, verify:bool, username: str, password: str,
bearer=None) -> dict[int, dict]:
""" """
Retrieve a list of certificates from an HP CDM server. Retrieve a list of certificates from an HP CDM server.
@@ -386,6 +400,8 @@ def get_certificates(hostname, verify, username, password, bearer=None):
return res return res
@typechecked
def delete_certificate(hostname, verify, username, password, certificates, certid, bearer=None): def delete_certificate(hostname, verify, username, password, certificates, certid, bearer=None):
""" """
Delete a certificate from an HP CDM server. Delete a certificate from an HP CDM server.