Compare commits

..

3 Commits

Author SHA1 Message Date
Frédéric Tronel
54d2338da6 Improve logging when operations are successful. 2025-12-29 15:10:21 +01:00
Frédéric Tronel
c286b5147f Add typeguard and dateutils as requirements. 2025-12-29 15:09:52 +01:00
Frédéric Tronel
115fa39618 Add typechecking. 2025-12-29 15:08:07 +01:00
2 changed files with 31 additions and 7 deletions

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.
@@ -430,6 +446,8 @@ def delete_certificate(hostname, verify, username, password, certificates, certi
logger.error('Impossible to delete certificate') logger.error('Impossible to delete certificate')
sys.exit(-1) sys.exit(-1)
logger.info('Certificate deleted with success.')
def main(): def main():
""" """
Main entry point of the program. Main entry point of the program.
@@ -579,10 +597,14 @@ def main():
certs = get_certificates(hostname=args.hostname, verify=args.verify, certs = get_certificates(hostname=args.hostname, verify=args.verify,
username=args.username, username=args.username,
password=args.password) password=args.password)
logger.info('List of certificates:')
for certid, cert in certs.items(): for certid, cert in certs.items():
subject = cert.get('subject') subject = cert.get('subject')
issuer = cert.get('issuer') issuer = cert.get('issuer')
print(f'{certid} - {subject} issued by {issuer}.') validity = cert.get('validity')
begin = parse(validity.get('fromDate'))
end = parse(validity.get('toDate'))
print(f'{certid} - {subject} issued by {issuer}. From: {begin} to {end}')
case 'del': case 'del':
bearer = get_bearer(hostname=args.hostname, verify=args.verify, username=args.username, bearer = get_bearer(hostname=args.hostname, verify=args.verify, username=args.username,
password=args.password) password=args.password)

View File

@@ -1,3 +1,5 @@
requests requests
coloredlogs coloredlogs
cryptography cryptography
typeguard
dateutils