async def register_with_labels( *, credentials: HTTPBasicCredentials = Depends(security), registration_body: RegistrationWithLabelsBody, ) -> Response: if not (edition := cmk_edition(credentials)).supports_registration_with_labels(): logger.error( "uuid=%s Registration with labels not supported", registration_body.uuid, ) raise HTTPException( status_code=HTTP_501_NOT_IMPLEMENTED, detail= f"The Checkmk {edition.value} edition does not support registration with agent labels", )
async def pairing( *, credentials: HTTPBasicCredentials = Depends(security), pairing_body: PairingBody, ) -> PairingResponse: uuid = uuid_from_pem_csr(pairing_body.csr) if not (rest_api_root_cert_resp := get_root_cert(credentials)).ok: logger.error( "uuid=%s Getting root cert failed with %s", uuid, rest_api_root_cert_resp.text, ) raise HTTPException( status_code=rest_api_root_cert_resp.status_code, detail=rest_api_root_cert_resp.text, )
async def agent_data( uuid: str, *, certificate: str = Header(...), monitoring_data: UploadFile = File(...), ) -> Response: host = Host(uuid) if not host.registered: logger.error( "uuid=%s Host is not registered", uuid, ) raise HTTPException( status_code=HTTP_403_FORBIDDEN, detail="Host is not registered", ) if host.host_type is not HostTypeEnum.PUSH: logger.error( "uuid=%s Host is not a push host", uuid, ) raise HTTPException( status_code=HTTP_403_FORBIDDEN, detail="Host is not a push host", ) try: temp_file = tempfile.NamedTemporaryFile( dir=host.source_path, delete=False, ) except FileNotFoundError: # We only end up here in case someone re-configures the host at exactly the same time when # data is being pushed. To avoid internal server errors, we still handle this case. logger.error( "uuid=%s Host is not registered or not configured as push host.", uuid, ) raise HTTPException( status_code=403, detail="Host is not registered or not configured as push host", ) shutil.copyfileobj(monitoring_data.file, temp_file) try: os.rename(temp_file.name, host.source_path / "agent_output") finally: Path(temp_file.name).unlink(missing_ok=True) _move_ready_file(uuid) logger.info( "uuid=%s Agent data saved", uuid, ) return Response(status_code=HTTP_204_NO_CONTENT)
async def agent_data( uuid: UUID, *, certificate: str = Header(...), compression: str = Header(...), monitoring_data: UploadFile = File(...), ) -> Response: host = Host(uuid) if not host.registered: logger.error( "uuid=%s Host is not registered", uuid, ) raise HTTPException( status_code=HTTP_403_FORBIDDEN, detail="Host is not registered", ) if host.host_type is not HostTypeEnum.PUSH: logger.error( "uuid=%s Host is not a push host", uuid, ) raise HTTPException( status_code=HTTP_403_FORBIDDEN, detail="Host is not a push host", ) try: decompressor = Decompressor(compression) except ValueError: logger.error( "uuid=%s Unsupported compression algorithm: %s", uuid, compression, ) raise HTTPException( status_code=400, detail=f"Unsupported compression algorithm: {compression}", ) try: decompressed_agent_data = decompressor(monitoring_data.file.read()) except DecompressionError as e: logger.error( "uuid=%s Decompression of agent data failed: %s", uuid, e, ) raise HTTPException( status_code=400, detail="Decompression of agent data failed", ) from e try: _store_agent_data( host.source_path, decompressed_agent_data, ) except FileNotFoundError: # We only end up here in case someone re-configures the host at exactly the same time when # data is being pushed. To avoid internal server errors, we still handle this case. logger.error( "uuid=%s Host is not registered or not configured as push host.", uuid, ) raise HTTPException( status_code=403, detail="Host is not registered or not configured as push host", ) _move_ready_file(uuid) logger.info( "uuid=%s Agent data saved", uuid, ) return Response(status_code=HTTP_204_NO_CONTENT)
raise HTTPException( status_code=rest_api_root_cert_resp.status_code, detail=rest_api_root_cert_resp.text, ) logger.info( "uuid=%s Got root cert", uuid, ) if not (rest_api_csr_resp := post_csr( credentials, pairing_body.csr, )).ok: logger.error( "uuid=%s CSR failed with %s", uuid, rest_api_csr_resp.text, ) raise HTTPException( status_code=rest_api_csr_resp.status_code, detail=rest_api_csr_resp.text, ) logger.info( "uuid=%s CSR signed", uuid, ) return PairingResponse( root_cert=rest_api_root_cert_resp.json()["cert"], client_cert=rest_api_csr_resp.json()["cert"], )