Skip to content

Commit 52dabdd

Browse files
author
michael.yak
committed
Should be possible to use HTTPS in SBA registraion URL
To test this change, SBA needs to be configured to use SSL/HTTPS using a self-signed certificate (never to be used beyond the test of course), see detailed procedure in https://www.baeldung.com/spring-boot-https-self-signed-certificate. When running any of the pyctuator examples, you'll need to set the `PYCTUATOR_REGISTRATION_NO_CERT` variable so Pyctoator will accept this self-signed certificate. Solves #41
1 parent 2536697 commit 52dabdd

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ 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).
176177

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

pyctuator/impl/spring_boot_admin_registration.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import http.client
22
import json
33
import logging
4+
import os
5+
import ssl
46
import threading
57
import urllib.parse
68
from base64 import b64encode
79
from datetime import datetime
8-
9-
from http.client import HTTPConnection
10+
from http.client import HTTPConnection, HTTPResponse
1011
from typing import Optional, Dict
1112

1213
from pyctuator.auth import Auth, BasicAuth
@@ -35,6 +36,8 @@ def __init__(
3536
self.instance_id = None
3637

3738
self.should_continue_registration_schedule: bool = False
39+
self.disable_certificate_validation_for_https_registration: bool = \
40+
os.getenv("PYCTUATOR_REGISTRATION_NO_CERT") is not None
3841

3942
def _schedule_next_registration(self, registration_interval_sec: int) -> None:
4043
timer = threading.Timer(
@@ -66,15 +69,7 @@ def _register_with_admin_server(self) -> None:
6669
headers = {"Content-type": "application/json"}
6770
self.authenticate(headers)
6871

69-
reg_url_split = urllib.parse.urlsplit(self.registration_url)
70-
conn = http.client.HTTPConnection(reg_url_split.hostname, reg_url_split.port)
71-
conn.request(
72-
"POST",
73-
reg_url_split.path,
74-
body=json.dumps(registration_data),
75-
headers=headers,
76-
)
77-
response = conn.getresponse()
72+
response = self._http_request(self.registration_url, "POST", headers, json.dumps(registration_data))
7873

7974
if response.status < 200 or response.status >= 300:
8075
logging.warning("Failed registering with boot-admin, got %s - %s", response.status, response.read())
@@ -104,14 +99,7 @@ def deregister_from_admin_server(self) -> None:
10499

105100
conn: Optional[HTTPConnection] = None
106101
try:
107-
reg_url_split = urllib.parse.urlsplit(deregistration_url)
108-
conn = http.client.HTTPConnection(reg_url_split.hostname, reg_url_split.port)
109-
conn.request(
110-
"DELETE",
111-
reg_url_split.path,
112-
headers=headers,
113-
)
114-
response = conn.getresponse()
102+
response = self._http_request(deregistration_url, "DELETE", headers)
115103

116104
if response.status < 200 or response.status >= 300:
117105
logging.warning("Failed deregistering from boot-admin, got %s - %s", response.status, response.read())
@@ -139,3 +127,24 @@ def start(self) -> None:
139127
def stop(self) -> None:
140128
logging.info("Stopping recurring registration")
141129
self.should_continue_registration_schedule = False
130+
131+
def _http_request(self, url: str, method: str, headers: Dict[str, str], body: Optional[str] = None) -> HTTPResponse:
132+
url_parts = urllib.parse.urlsplit(url)
133+
if url_parts.scheme == "http":
134+
conn = http.client.HTTPConnection(url_parts.hostname, url_parts.port)
135+
elif url_parts.scheme == "https":
136+
context = None
137+
if self.disable_certificate_validation_for_https_registration:
138+
context = ssl.SSLContext()
139+
context.verify_mode = ssl.CERT_NONE
140+
conn = http.client.HTTPSConnection(url_parts.hostname, url_parts.port, context=context)
141+
else:
142+
raise ValueError(f"Unknown scheme in {url}")
143+
144+
conn.request(
145+
method,
146+
url_parts.path,
147+
body=body,
148+
headers=headers,
149+
)
150+
return conn.getresponse()

0 commit comments

Comments
 (0)