|
1 | 1 | import http.client |
2 | 2 | import json |
3 | 3 | import logging |
| 4 | +import os |
| 5 | +import ssl |
4 | 6 | import threading |
5 | 7 | import urllib.parse |
6 | 8 | from base64 import b64encode |
7 | 9 | from datetime import datetime |
8 | | - |
9 | | -from http.client import HTTPConnection |
| 10 | +from http.client import HTTPConnection, HTTPResponse |
10 | 11 | from typing import Optional, Dict |
11 | 12 |
|
12 | 13 | from pyctuator.auth import Auth, BasicAuth |
@@ -35,6 +36,8 @@ def __init__( |
35 | 36 | self.instance_id = None |
36 | 37 |
|
37 | 38 | 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 |
38 | 41 |
|
39 | 42 | def _schedule_next_registration(self, registration_interval_sec: int) -> None: |
40 | 43 | timer = threading.Timer( |
@@ -66,15 +69,7 @@ def _register_with_admin_server(self) -> None: |
66 | 69 | headers = {"Content-type": "application/json"} |
67 | 70 | self.authenticate(headers) |
68 | 71 |
|
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)) |
78 | 73 |
|
79 | 74 | if response.status < 200 or response.status >= 300: |
80 | 75 | 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: |
104 | 99 |
|
105 | 100 | conn: Optional[HTTPConnection] = None |
106 | 101 | 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) |
115 | 103 |
|
116 | 104 | if response.status < 200 or response.status >= 300: |
117 | 105 | logging.warning("Failed deregistering from boot-admin, got %s - %s", response.status, response.read()) |
@@ -139,3 +127,24 @@ def start(self) -> None: |
139 | 127 | def stop(self) -> None: |
140 | 128 | logging.info("Stopping recurring registration") |
141 | 129 | 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