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
from cryptography import x509
from cryptography.hazmat.primitives.serialization import Encoding
from dateutil.parser import parse
from typeguard import typechecked
import requests
import coloredlogs
def create_nonce(length: int=45):
@typechecked
def create_nonce(length: int=45) -> str:
"""
Generate a random nonce string.
@@ -72,7 +75,9 @@ def create_nonce(length: int=45):
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.
@@ -170,8 +175,10 @@ def get_bearer(hostname: str, verify: bool, username: str, password:str):
return bearer
@typechecked
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.
@@ -252,12 +259,16 @@ def get_csr(hostname: str, verify: bool, username: str, password: str, ou: str,
finished = True
csr = csr['certificateData']
print(csr)
logger.debug('CSR:\n %s', csr)
with open(filename, 'w+', encoding='utf-8') as f:
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.
@@ -333,7 +344,10 @@ def install_certificate(hostname, verify, username, password, filename, bearer=N
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.
@@ -386,6 +400,8 @@ def get_certificates(hostname, verify, username, password, bearer=None):
return res
@typechecked
def delete_certificate(hostname, verify, username, password, certificates, certid, bearer=None):
"""
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')
sys.exit(-1)
logger.info('Certificate deleted with success.')
def main():
"""
Main entry point of the program.
@@ -579,10 +597,14 @@ def main():
certs = get_certificates(hostname=args.hostname, verify=args.verify,
username=args.username,
password=args.password)
logger.info('List of certificates:')
for certid, cert in certs.items():
subject = cert.get('subject')
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':
bearer = get_bearer(hostname=args.hostname, verify=args.verify, username=args.username,
password=args.password)

View File

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