Perform handshakes asynchronously

This commit is contained in:
2022-10-11 06:55:34 +10:30
parent 8046f04e33
commit 10ff26b17f
4 changed files with 45 additions and 14 deletions

View File

@@ -1,6 +1,6 @@
"""
Usage:
certo [-vj] <hostnames>... [-d DAYS|--days-to-expiration=DAYS] [-t SECONDS|--timeout=SECONDS]
certo [-vj] [-d DAYS|--days-to-expiration=DAYS] [-t SECONDS|--timeout=SECONDS] <hostnames>...
certo -h | --help
Options:
@@ -10,6 +10,7 @@ Options:
-d DAYS --days-to-expiration=DAYS Warn about near expiration if within DAYS of the cert's notAfter [default: 5].
-t SECONDS --timeout=SECONDS Timeout for SSL Handshake [default: 5].
"""
import asyncio
import logging
from docopt import docopt
@@ -17,7 +18,8 @@ from docopt import docopt
from certo.checks.hostname import check_host_certificate_expiration
from certo.report import JSONReporter, DefaultReporter
if __name__ == "__main__":
async def main():
args = docopt(__doc__)
output_as_json = args.get("-j")
@@ -32,12 +34,19 @@ if __name__ == "__main__":
else:
reporter = DefaultReporter()
# @todo async
for hs in hostnames:
logging.info(f"Getting CERT from {hs}")
reporter.add_check(check_host_certificate_expiration(hs, days_to_expiration))
jobs = {
check_host_certificate_expiration(hs, days_to_expiration) for hs in hostnames
}
checks = await asyncio.gather(*jobs)
for check in checks:
reporter.append(check)
if log := reporter.report():
print(log)
exit(reporter.num_failed())
if __name__ == "__main__":
asyncio.run(main())