API Reference: CertMonitor¶
Use this page when you know the operation you need and want its exact signature, arguments, or return value. For a guided first check, start with Basic Usage.
The monitor¶
The reference below comes directly from the code's docstrings. Certificate collection, validation, raw formats, connection cleanup, and snapshot refresh all belong to the same monitor.
certmonitor.core.CertMonitor ¶
CertMonitor(host: str, port: int = 443, enabled_validators: list[str] | None = None, *, connection_host: str | None = None, server_hostname: str | None = None, timeout: float = 10, cafile: str | None = None, capath: str | None = None, client_cert: str | None = None, client_key: str | None = None, starttls: str | None = None, proxy: str | None = None)
Class for monitoring and retrieving certificate details from a given host.
Initialize a monitor for a host without opening a connection.
Use a context manager to connect and close automatically. Retrieval and validation methods can also connect lazily. By default, validation checks expiration, SAN-based hostname identity, and trust through a separate verified handshake. Collection itself is permissive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
The identity the certificate is checked against. Also the default TCP destination and TLS SNI name. |
required |
port
|
int
|
Target TCP port. Defaults to 443. |
443
|
enabled_validators
|
list[str] | None
|
Names to run. |
None
|
connection_host
|
str | None
|
Override the TCP destination, such as a backend IP. |
None
|
server_hostname
|
str | None
|
Override the TLS SNI name sent to the server. |
None
|
timeout
|
float
|
Positive timeout in seconds for each network operation, including each connection attempt made while collecting the certificate. This is not a whole-scan deadline; platform DNS resolution cannot be interrupted by this timeout. |
10
|
cafile
|
str | None
|
PEM CA bundle for the separate verified trust handshake. |
None
|
capath
|
str | None
|
OpenSSL-compatible CA directory for the verified trust handshake. |
None
|
client_cert
|
str | None
|
Client certificate chain file for mutual TLS. |
None
|
client_key
|
str | None
|
Separate client private-key file, if needed. |
None
|
starttls
|
str | None
|
Application protocol whose STARTTLS preamble runs before the
TLS handshake: |
None
|
proxy
|
str | None
|
Route every connection through |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in certmonitor/core.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
enabled_validators
instance-attribute
¶
fingerprint_sha256
property
¶
Lowercase hex SHA-256 of the collected leaf DER, or None before collection.
The same value openssl x509 -fingerprint -sha256 prints without the
colons, so it can be compared with what a CA, a load balancer, or a
previous scan recorded. A changed fingerprint means the certificate
was replaced.
offline
property
¶
True when the certificate comes from a file or bytes, not a connection.
__enter__ ¶
__exit__ ¶
close ¶
Close the connection, retaining the last snapshot for inspection.
connect ¶
Establishes a connection to the host if not already connected.
Source code in certmonitor/core.py
describe_validators ¶
Describe every registered validator and the user args it accepts.
Reads each validator's cached _user_params (built by
BaseCertValidator.__init_subclass__ / BaseCipherValidator.__init_subclass__
at class definition time) and renders a serializable description suitable
for printing, logging, or feeding into a CLI --help page.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, dict[str, Any]]
|
Keyed by validator name. Each value contains:
|
Example
Source code in certmonitor/core.py
detect_protocol ¶
Detect the protocol used by the host.
Returns "ssh", "ssl", or "starttls:<protocol>" when the peeked
greeting belongs to a STARTTLS service, or an error dict.
Source code in certmonitor/core.py
from_bytes
classmethod
¶
from_bytes(data: bytes | str, *, host: str | None = None, port: int = 443, enabled_validators: list[str] | None = None) -> CertMonitor
Build a monitor from PEM text or DER bytes already in memory.
Behaves like from_file(); use it for certificates fetched from an
API, a secrets store, or a database. refresh() re-parses the same
bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes | str
|
PEM text (str or bytes) or DER bytes. |
required |
host
|
str | None
|
The identity the certificate should be valid for. |
None
|
port
|
int
|
Port to report alongside the host. Defaults to 443. |
443
|
enabled_validators
|
list[str] | None
|
Names to run. |
None
|
Source code in certmonitor/core.py
from_file
classmethod
¶
from_file(path: str | PathLike[str], *, host: str | None = None, port: int = 443, enabled_validators: list[str] | None = None) -> CertMonitor
Build a monitor from a certificate file instead of a connection.
The file may be PEM (a single certificate or a chain, leaf first) or
DER. Everything that only needs certificate data works as it does for
a connected monitor: get_cert_info(), the public key helpers,
validate(), and refresh(), which re-reads the file. Checks that
need a live connection (tls_version, weak_cipher, root_certificate,
pq_key_exchange) report status: unsupported with a reason.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
Path to the PEM or DER file. |
required |
host
|
str | None
|
The identity the certificate should be valid for, used by
|
None
|
port
|
int
|
Port to report alongside the host. Defaults to 443. |
443
|
enabled_validators
|
list[str] | None
|
Names to run. |
None
|
Example
Source code in certmonitor/core.py
get_cert_info ¶
Retrieves and structures the certificate details.
Source code in certmonitor/core.py
get_cipher_info ¶
Retrieve and structure the cipher information of the SSL/TLS connection.
Source code in certmonitor/core.py
get_enabled_validators ¶
Get the list of validators enabled for this CertMonitor instance.
Returns:
| Type | Description |
|---|---|
list[str]
|
List[str]: A list of enabled validator names for this instance. |
Source code in certmonitor/core.py
get_public_key_der ¶
Return the public key in DER format.
Source code in certmonitor/core.py
get_public_key_pem ¶
Return the public key in PEM format.
Source code in certmonitor/core.py
get_raw_der ¶
Return the raw DER format of the certificate.
Source code in certmonitor/core.py
get_raw_pem ¶
Return the raw PEM format of the certificate.
Source code in certmonitor/core.py
list_validators ¶
Get a list of all available validators that can be used.
Returns:
| Type | Description |
|---|---|
list[str]
|
List[str]: A list of all registered validator names. |
Source code in certmonitor/core.py
refresh ¶
validate ¶
Validates the target host by running all enabled validators.
This method: 1. Checks if all requested validators are implemented. 2. Separates validators into cert-based and cipher-based groups. 3. Fetches cert_info and cipher_info as needed. 4. Runs each validator with the appropriate arguments. 5. Returns a dictionary of validation results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
validator_args
|
dict
|
Additional arguments for specific validators. Example: { "subject_alt_names": {"alternate_names": ["www.example.com"]} } |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, Any]
|
A dictionary keyed by validator name, each value being the result of that validator. |
Example
with CertMonitor("example.com", enabled_validators=["expiration", "weak_cipher"]) as monitor: results = monitor.validate() print(results["expiration"]) print(results["weak_cipher"])
Source code in certmonitor/core.py
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 | |
Scan multiple hosts¶
scan_hosts() creates an independent monitor per worker and yields results in completion order. See Performance Tips for a complete example and timeout limits.
certmonitor.scanning.scan_hosts ¶
scan_hosts(hosts: Iterable[Endpoint], *, port: int = 443, max_workers: int = 8, timeout: float = 10, enabled_validators: list[str] | None = None, validator_args: dict[str, Any] | None = None, cafile: str | None = None, capath: str | None = None, client_cert: str | None = None, client_key: str | None = None, starttls: str | None = None, proxy: str | None = None) -> Iterator[dict[str, Any]]
Yield completed scans with at most max_workers endpoints in flight.
Each result is a dict with host, port, results (the validate()
output), snapshot_at, fingerprint_sha256, the parsed certificate, and
public_key_info, so two runs can be handed to compare_snapshots(). If a scan raises, the dict carries an
error (exception class name) and message instead of aborting the
whole scan, so one bad host never hides the rest. Results arrive in
completion order. Stopping iteration early returns promptly; scans that
were still queued are cancelled and in-flight ones finish in the
background.
timeout bounds individual network operations; platform DNS resolution
is not interruptible.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hosts
|
Iterable[Endpoint]
|
Endpoints to scan, consumed lazily. Each entry is a host name or
IP address, a |
required |
port
|
int
|
TCP port for entries that do not carry one. Defaults to 443. |
443
|
max_workers
|
int
|
Maximum number of concurrent scans. Defaults to 8. |
8
|
timeout
|
float
|
Per-operation network timeout in seconds. Defaults to 10. |
10
|
enabled_validators
|
list[str] | None
|
Validator names to run; |
None
|
validator_args
|
dict[str, Any] | None
|
Per-validator keyword arguments applied to every host,
in the same shape |
None
|
cafile
|
str | None
|
PEM CA bundle for trust verification on every endpoint. An endpoint dict may override it. |
None
|
capath
|
str | None
|
CA directory for trust verification on every endpoint. |
None
|
client_cert
|
str | None
|
Client certificate for mutual TLS on every endpoint. |
None
|
client_key
|
str | None
|
Client private key, if separate from |
None
|
starttls
|
str | None
|
STARTTLS protocol name applied to every endpoint ( |
None
|
proxy
|
str | None
|
|
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
from certmonitor import scan_hosts
targets = [
"example.com",
("legacy.example.net", 8443),
{"host": "api.example.com", "connection_host": "192.0.2.10"},
]
for scan in scan_hosts(targets, max_workers=4, cafile="/path/to/private-ca.pem"):
print(scan["host"], scan["port"], scan["results"]["expiration"]["status"])
Source code in certmonitor/scanning.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |