Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to use TLS when pushing to gateway #994

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion prometheus_client/exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ def tls_auth_handler(
ssl.CERT_REQUIRED and SSLContext.check_hostname by default. This can be
disabled by setting insecure_skip_verify to True.

Both this handler and the TLS feature on pushgateay are experimental."""
Both this handler and the TLS feature on pushgateway are experimental."""
context = ssl.SSLContext(protocol=protocol)
if cafile is not None:
context.load_verify_locations(cafile)
Expand All @@ -477,6 +477,32 @@ def tls_auth_handler(
handler = HTTPSHandler(context=context)
return _make_handler(url, method, timeout, headers, data, handler)

def tls_handler(
url: str,
method: str,
timeout: Optional[float],
headers: List[Tuple[str, str]],
data: bytes,
cafile: Optional[str] = None,
protocol: int = ssl.PROTOCOL_TLS_CLIENT,
verify_mode: ssl.VerifyMode = ssl.CERT_REQUIRED,
) -> Callable[[], None]:
"""Handler that implements an HTTPS connection.

The default protocol (ssl.PROTOCOL_TLS_CLIENT) will also enable
ssl.CERT_REQUIRED and SSLContext.check_hostname by default. This can be
changed by setting the verify_mode.
"""
context = ssl.SSLContext(protocol=protocol)
if cafile is not None:
context.load_verify_locations(cafile)
else:
context.load_default_certs()

context.verify_mode = verify_mode

handler = HTTPSHandler(context=context)
return _make_handler(url, method, timeout, headers, data, handler)

def push_to_gateway(
gateway: str,
Expand Down