예제 #1
0
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)
예제 #2
0
def _write_registration_file(
    username: str,
    registration_body: RegistrationWithLabelsBody,
) -> None:
    (dir_new_requests := REGISTRATION_REQUESTS / "NEW").mkdir(
        mode=0o770,
        parents=True,
        exist_ok=True,
    )
    (new_request :=
     dir_new_requests / f"{registration_body.uuid}.json").write_text(
         json.dumps({
             "uuid": str(registration_body.uuid),
             "username": username,
             "agent_labels": registration_body.agent_labels,
         }))
    new_request.chmod(0o660)
    logger.info(
        "uuid=%s Stored new request for registration",
        registration_body.uuid,
    )
예제 #3
0
async def register_with_hostname(
    *,
    credentials: HTTPBasicCredentials = Depends(security),
    registration_body: RegistrationWithHNBody,
) -> Response:
    _validate_registration_request(
        host_configuration(
            credentials,
            registration_body.host_name,
        ))
    link_host_with_uuid(
        credentials,
        registration_body.host_name,
        registration_body.uuid,
    )
    logger.info(
        "uuid=%s registered host %s",
        registration_body.uuid,
        registration_body.host_name,
    )
    return Response(status_code=HTTP_204_NO_CONTENT)
예제 #4
0
async def register_with_hostname(
    *,
    credentials: HTTPBasicCredentials = Depends(security),
    registration_body: RegistrationWithHNBody,
) -> Response:
    if not host_exists(
            credentials,
            registration_body.host_name,
    ):
        raise HTTPException(
            status_code=HTTP_404_NOT_FOUND,
            detail=f"Host {registration_body.host_name} does not exist",
        )
    link_host_with_uuid(
        credentials,
        registration_body.host_name,
        registration_body.uuid,
    )
    logger.info(
        "uuid=%s registered host %s",
        registration_body.uuid,
        registration_body.host_name,
    )
    return Response(status_code=HTTP_204_NO_CONTENT)
예제 #5
0
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)
예제 #6
0
    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,
        )
    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,
        )