Skip to content

Commit 0ff2b20

Browse files
michael.yakmichaelyaakoby
authored andcommitted
Allow using custom SSL context for registration requests
Pyctuator should support using custom `ssl.SSLContext` when registering with HTTPS enabled Spring Boot Admin. This is in addition to the option to disable certificate validation, that should never be used except for testing, using the `PYCTUATOR_REGISTRATION_NO_CERT` environment-variable. Solves #65
1 parent 87f4e63 commit 0ff2b20

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ Log in to the Spring Boot Admin UI at `http://localhost:8080` to interact with t
173173
When registering a service in Spring Boot Admin, note that:
174174
* **Docker** - If the Spring Boot Admin is running in a container while the managed service is running in the docker-host directly, the `app_url` and `pyctuator_endpoint_url` should use `host.docker.internal` as the url's host so Spring Boot Admin will be able to connect to the monitored service.
175175
* **Http Traces** - In order for the "Http Traces" tab to be able to hide requests sent by Spring Boot Admin to the Pyctuator endpoint, `pyctuator_endpoint_url` must be using the same host and port as `app_url`.
176-
* **HTTPS** - If Spring Boot Admin is using HTTPS with self-signed certificate, set the `PYCTUATOR_REGISTRATION_NO_CERT` environment variable so Pyctuator will disable certificate validation when registering (and deregistering).
176+
* **HTTPS** - If Pyctuator is to be registered with Spring Boot Admin using HTTPS and the default SSL context is inappropriate, you can provide your own `ssl.SSLContext` using the `ssl_context` optional parameter of the `Pyctuator` constructor.
177+
* **Insecure HTTPS** - If Spring Boot Admin is using HTTPS with self-signed certificate, set the `PYCTUATOR_REGISTRATION_NO_CERT` environment variable so Pyctuator will disable certificate validation when registering (and deregistering).
177178

178179
## Advanced Configuration
179180
The following sections are intended for advanced users who want to configure advanced Pyctuator features.

pyctuator/impl/spring_boot_admin_registration.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ def __init__(
2525
start_time: datetime,
2626
service_url: str,
2727
registration_interval_sec: float,
28-
application_metadata: Optional[dict] = None
28+
application_metadata: Optional[dict] = None,
29+
ssl_context: Optional[ssl.SSLContext] = None,
2930
) -> None:
3031
self.registration_url = registration_url
3132
self.registration_auth = registration_auth
@@ -36,6 +37,7 @@ def __init__(
3637
self.registration_interval_sec = registration_interval_sec
3738
self.instance_id = None
3839
self.application_metadata = application_metadata if application_metadata else {}
40+
self.ssl_context = ssl_context
3941

4042
self.should_continue_registration_schedule: bool = False
4143
self.disable_certificate_validation_for_https_registration: bool = \
@@ -137,8 +139,8 @@ def _http_request(self, url: str, method: str, headers: Dict[str, str], body: Op
137139
if url_parts.scheme == "http":
138140
conn = http.client.HTTPConnection(url_parts.hostname, url_parts.port)
139141
elif url_parts.scheme == "https":
140-
context = None
141-
if self.disable_certificate_validation_for_https_registration:
142+
context = self.ssl_context
143+
if not context and self.disable_certificate_validation_for_https_registration:
142144
context = ssl.SSLContext()
143145
context.verify_mode = ssl.CERT_NONE
144146
conn = http.client.HTTPSConnection(url_parts.hostname, url_parts.port, context=context)

pyctuator/pyctuator.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import atexit
33
import importlib.util
44
import logging
5+
import ssl
56
from datetime import datetime, timezone
67
from typing import Any, Optional, Dict, Callable
78

@@ -40,6 +41,7 @@ def __init__(
4041
auto_deregister: bool = True,
4142
metadata: Optional[dict] = None,
4243
additional_app_info: Optional[dict] = None,
44+
ssl_context: Optional[ssl.SSLContext] = None,
4345
) -> None:
4446
"""The entry point for integrating pyctuator with a web-frameworks such as FastAPI and Flask.
4547
@@ -76,6 +78,7 @@ def __init__(
7678
with SBA showing "offline" instances
7779
:param metadata: optional metadata key-value pairs that are displayed in SBA main page of an instance
7880
:param additional_app_info: additional arbitrary information to add to the application's "Info" section
81+
:param ssl_context: optional SSL context to be used when registering with SBA
7982
"""
8083

8184
self.auto_deregister = auto_deregister
@@ -99,6 +102,7 @@ def __init__(
99102
self.boot_admin_registration_handler: Optional[BootAdminRegistrationHandler] = None
100103

101104
self.metadata = metadata
105+
self.ssl_context = ssl_context
102106

103107
root_logger = logging.getLogger()
104108
# If application did not initiate logging module, add default handler to root logger
@@ -130,7 +134,8 @@ def __init__(
130134
start_time,
131135
app_url,
132136
registration_interval_sec,
133-
self.metadata
137+
self.metadata,
138+
self.ssl_context,
134139
)
135140

136141
# Deregister from SBA on exit

0 commit comments

Comments
 (0)