| 250 | |
| 251 | |
| 252 | class NodeProxyApi(Thread): |
| 253 | def __init__(self, node_proxy_mgr: "NodeProxyManager") -> None: |
| 254 | super().__init__() |
| 255 | self.log = get_logger(__name__) |
| 256 | self.cp_shutdown_event = Event() |
| 257 | self.node_proxy_mgr = node_proxy_mgr |
| 258 | self.username = self.node_proxy_mgr.username |
| 259 | self.password = self.node_proxy_mgr.password |
| 260 | self.ssl_crt = self.node_proxy_mgr.api_ssl_crt |
| 261 | self.ssl_key = self.node_proxy_mgr.api_ssl_key |
| 262 | self.system = self.node_proxy_mgr.system |
| 263 | self.reporter_agent = self.node_proxy_mgr.reporter_agent |
| 264 | self.config = self.node_proxy_mgr.config |
| 265 | self.api = API(self.system, self.reporter_agent, self.config) |
| 266 | |
| 267 | def check_auth(self, realm: str, username: str, password: str) -> bool: |
| 268 | return self.username == username and self.password == password |
| 269 | |
| 270 | def shutdown(self) -> None: |
| 271 | self.log.info("Stopping node-proxy API...") |
| 272 | self.cp_shutdown_event.set() |
| 273 | |
| 274 | def run(self) -> None: |
| 275 | self.log.info("node-proxy API configuration...") |
| 276 | cherrypy.config.update( |
| 277 | { |
| 278 | "environment": "production", |
| 279 | "engine.autoreload.on": False, |
| 280 | "log.screen": True, |
| 281 | } |
| 282 | ) |
| 283 | config = { |
| 284 | "/": { |
| 285 | "request.methods_with_bodies": ("POST", "PUT", "PATCH"), |
| 286 | "tools.trailing_slash.on": False, |
| 287 | "tools.auth_basic.realm": "localhost", |
| 288 | "tools.auth_basic.checkpassword": self.check_auth, |
| 289 | } |
| 290 | } |
| 291 | cherrypy.tree.mount(self.api, "/", config=config) |
| 292 | # cherrypy.tree.mount(admin, '/admin', config=config) |
| 293 | |
| 294 | ssl_crt = write_tmp_file(self.ssl_crt, prefix_name="listener-crt-") |
| 295 | ssl_key = write_tmp_file(self.ssl_key, prefix_name="listener-key-") |
| 296 | |
| 297 | self.api.ssl_certificate = ssl_crt.name |
| 298 | self.api.ssl_private_key = ssl_key.name |
| 299 | |
| 300 | cherrypy.server.unsubscribe() |
| 301 | try: |
| 302 | cherrypy.engine.start() |
| 303 | self.log.info("node-proxy API started.") |
| 304 | self.cp_shutdown_event.wait() |
| 305 | self.cp_shutdown_event.clear() |
| 306 | cherrypy.engine.exit() |
| 307 | cherrypy.server.httpserver = None |
| 308 | self.log.info("node-proxy API shutdown.") |
| 309 | except Exception as e: |