try: import cherrypy from cherrypy._cpserver import Server except ImportError: # to avoid sphinx build crash class Server: # type: ignore pass import logging import socket import orchestrator # noqa from mgr_module import ServiceInfoT from mgr_util import build_url from typing import Dict, List, TYPE_CHECKING, cast, Collection, Callable, NamedTuple, Optional from cephadm.services.monitoring import AlertmanagerService, NodeExporterService, PrometheusService import secrets from cephadm.services.ingress import IngressSpec from cephadm.ssl_cert_utils import SSLCerts from cephadm.services.cephadmservice import CephExporterService if TYPE_CHECKING: from cephadm.module import CephadmOrchestrator def cherrypy_filter(record: logging.LogRecord) -> int: blocked = [ 'TLSV1_ALERT_DECRYPT_ERROR' ] msg = record.getMessage() return not any([m for m in blocked if m in msg]) logging.getLogger('cherrypy.error').addFilter(cherrypy_filter) cherrypy.log.access_log.propagate = False class Route(NamedTuple): name: str route: str controller: Callable class ServiceDiscovery: KV_STORE_SD_ROOT_CERT = 'service_discovery/root/cert' KV_STORE_SD_ROOT_KEY = 'service_discovery/root/key' def __init__(self, mgr: "CephadmOrchestrator") -> None: self.mgr = mgr self.ssl_certs = SSLCerts() self.username: Optional[str] = None self.password: Optional[str] = None def validate_password(self, realm: str, username: str, password: str) -> bool: return (password == self.password and username == self.username) def configure_routes(self, server: Server, enable_auth: bool) -> None: ROUTES = [ Route('index', '/', server.index), Route('sd-config', '/prometheus/sd-config', server.get_sd_config), Route('rules', '/prometheus/rules', server.get_prometheus_rules), ] d = cherrypy.dispatch.RoutesDispatcher() for route in ROUTES: d.connect(**route._asdict()) if enable_auth: conf = { '/': { 'request.dispatch': d, 'tools.auth_basic.on': True, 'tools.auth_basic.realm': 'localhost', 'tools.auth_basic.checkpassword': self.validate_password } } else: conf = {'/': {'request.dispatch': d}} cherrypy.tree.mount(None, '/sd', config=conf) def enable_auth(self) -> None: self.username = self.mgr.get_store('service_discovery/root/username') self.password = self.mgr.get_store('service_discovery/root/password') if not self.password or not self.username: self.username = 'admin' # TODO(redo): what should be the default username self.password = secrets.token_urlsafe(20) self.mgr.set_store('service_discovery/root/password', self.password) self.mgr.set_store('service_discovery/root/username', self.username) def configure_tls(self, server: Server) -> None: old_cert = self.mgr.get_store(self.KV_STORE_SD_ROOT_CERT) old_key = self.mgr.get_store(self.KV_STORE_SD_ROOT_KEY) if old_key and old_cert: self.ssl_certs.load_root_credentials(old_cert, old_key) else: self.ssl_certs.generate_root_cert(self.mgr.get_mgr_ip()) self.mgr.set_store(self.KV_STORE_SD_ROOT_CERT, self.ssl_certs.get_root_cert()) self.mgr.set_store(self.KV_STORE_SD_ROOT_KEY, self.ssl_certs.get_root_key()) addr = self.mgr.get_mgr_ip() host_fqdn = socket.getfqdn(addr) server.ssl_certificate, server.ssl_private_key = self.ssl_certs.generate_cert_files( host_fqdn, addr) def configure(self, port: int, addr: str, enable_security: bool) -> None: # we create a new server to enforce TLS/SSL config refresh self.root_server = Root(self.mgr, port, addr) self.root_server.ssl_certificate = None self.root_server.ssl_private_key = None if enable_security: self.enable_auth() self.configure_tls(self.root_server) self.configure_routes(self.root_server, enable_security) class Root(Server): # collapse everything to '/' def _cp_dispatch(self, vpath: str) -> 'Root': cherrypy.request.path = '' return self def stop(self) -> None: # we must call unsubscribe before stopping the server, # otherwise the port is not released and we will get # an exception when trying to restart it self.unsubscribe() super().stop() def __init__(self, mgr: "CephadmOrchestrator", port: int = 0, host: str = ''): self.mgr = mgr super().__init__() self.socket_port = port self.socket_host = host self.subscribe() @cherrypy.expose def index(self) -> str: return '''