Skip to content

Commit d529767

Browse files
author
michaelyaakoby
authored
Merge pull request #43 from SolarEdgeTech/http_registration_url
Should be possible to use HTTPS in SBA registraion URL
2 parents 2536697 + 92da590 commit d529767

File tree

8 files changed

+35
-25
lines changed

8 files changed

+35
-25
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.

examples/Advanced/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ python = "^3.7"
1111
psutil = { version = "^5.6" }
1212
fastapi = { version = "^0.41.0" }
1313
uvicorn = { version = "^0.9.0" }
14-
pyctuator = { version = "^0.13.1" }
14+
pyctuator = { version = "^0.13.2" }
1515
sqlalchemy = { version = "^1.3" }
1616
PyMySQL = { version = "^0.9.3" }
1717
cryptography = { version = "^2.8" }

examples/FastAPI/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ python = "^3.7"
1111
psutil = { version = "^5.6" }
1212
fastapi = { version = "^0.41.0" }
1313
uvicorn = { version = "^0.9.0" }
14-
pyctuator = { version = "^0.13.1" }
14+
pyctuator = { version = "^0.13.2" }
1515

1616
[build-system]
1717
requires = ["poetry>=0.12"]

examples/Flask/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ authors = [
1010
python = "^3.7"
1111
psutil = { version = "^5.6" }
1212
flask = { version = "^1.1" }
13-
pyctuator = { version = "^0.13.1" }
13+
pyctuator = { version = "^0.13.2" }
1414

1515
[build-system]
1616
requires = ["poetry>=0.12"]

examples/aiohttp/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ authors = [
1010
python = "^3.7"
1111
psutil = { version = "^5.6" }
1212
aiohttp = { version = "^3.5.4" }
13-
pyctuator = { version = "^0.13.1" }
13+
pyctuator = { version = "^0.13.2" }
1414

1515
[build-system]
1616
requires = ["poetry>=0.12"]

examples/tornado/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ authors = [
1010
python = "^3.7"
1111
psutil = { version = "^5.6" }
1212
tornado = { version = "^6.0.4" }
13-
pyctuator = { version = "^0.13.1" }
13+
pyctuator = { version = "^0.13.2" }
1414

1515
[build-system]
1616
requires = ["poetry>=0.12"]

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()

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pyctuator"
3-
version = "0.13.1"
3+
version = "0.13.2"
44
description = "A Python implementation of the Spring Actuator API for popular web frameworks"
55
authors = [
66
"Michael Yakobi <[email protected]>",

0 commit comments

Comments
 (0)