Beispiel #1
0
def _test_register_with_labels(
    mocker: MockerFixture,
    client: TestClient,
    uuid: UUID,
) -> None:
    mocker.patch(
        "agent_receiver.endpoints.cmk_edition",
        lambda _c: CMKEdition["cpe"],
    )
    response = client.post(
        "/register_with_labels",
        auth=("monitoring", "supersafe"),
        json={
            "uuid": str(uuid),
            "agent_labels": {
                "a": "b",
                "c": "d",
            },
        },
    )
    assert response.status_code == 204
    assert json.loads(
        (site_context.r4r_dir() / "NEW" / f"{uuid}.json").read_text()) == {
            "uuid": str(uuid),
            "username": "******",
            "agent_labels": {
                "a": "b",
                "c": "d",
            },
        }
    assert (oct(
        stat.S_IMODE((site_context.r4r_dir() / "NEW" /
                      f"{uuid}.json").stat().st_mode)) == "0o660")
Beispiel #2
0
def test_register_with_labels_folder_missing(
    mocker: MockerFixture,
    client: TestClient,
    uuid: UUID,
) -> None:
    assert not (site_context.r4r_dir() / "NEW").exists()
    _test_register_with_labels(
        mocker,
        client,
        uuid,
    )
    assert oct(stat.S_IMODE(
        (site_context.r4r_dir() / "NEW").stat().st_mode)) == "0o770"
Beispiel #3
0
def test_agent_data_move_ready(
    client: TestClient,
    uuid: UUID,
    agent_data_headers: Mapping[str, str],
    compressed_agent_data: io.BytesIO,
) -> None:
    (path_ready := site_context.r4r_dir() / "READY").mkdir()
    (path_ready / f"{uuid}.json").touch()

    client.post(
        f"/agent_data/{uuid}",
        headers=typeshed_issue_7724(agent_data_headers),
        files={"monitoring_data": ("filename", compressed_agent_data)},
    )

    assert (site_context.r4r_dir() / "DISCOVERABLE" / f"{uuid}.json").exists()
Beispiel #4
0
def test_register_with_labels_folder_exists(
    mocker: MockerFixture,
    client: TestClient,
    uuid: UUID,
) -> None:
    (site_context.r4r_dir() / "NEW").mkdir(parents=True)
    _test_register_with_labels(
        mocker,
        client,
        uuid,
    )
Beispiel #5
0
def get_registration_status_from_file(uuid: UUID) -> Optional[RegistrationData]:
    for status in RegistrationStatusEnum:
        path = r4r_dir() / status.name / f"{uuid}.json"
        if path.exists():
            message = (
                read_message_from_file(path) if status is RegistrationStatusEnum.DECLINED else None
            )
            # access time is used to determine when to remove registration request file
            update_file_access_time(path)
            return RegistrationData(status=status, message=message)

    return None
Beispiel #6
0
def test_registration_status_push_host(
    client: TestClient,
    uuid: UUID,
    registration_status_headers: Mapping[str, str],
) -> None:
    (path_discoverable := site_context.r4r_dir() / "DISCOVERABLE").mkdir()
    (path_discoverable / f"{uuid}.json").touch()

    response = client.get(
        f"/registration_status/{uuid}",
        headers=typeshed_issue_7724(registration_status_headers),
    )

    assert response.status_code == 200
    assert response.json() == {
        "hostname": "hostname",
        "status": "discoverable",
        "type": HostTypeEnum.PUSH.value,
        "message": "Host registered",
    }
Beispiel #7
0
def test_registration_status_declined(
    client: TestClient,
    uuid: UUID,
    registration_status_headers: Mapping[str, str],
) -> None:
    (path_declined := site_context.r4r_dir() / "DECLINED").mkdir()
    (path_declined / f"{uuid}.json").write_text(
        json.dumps({"message": "Registration request declined"}))

    response = client.get(
        f"/registration_status/{uuid}",
        headers=typeshed_issue_7724(registration_status_headers),
    )

    assert response.status_code == 200
    assert response.json() == {
        "hostname": None,
        "status": "declined",
        "type": None,
        "message": "Registration request declined",
    }
Beispiel #8
0
def _write_registration_file(
    username: str,
    registration_body: RegistrationWithLabelsBody,
) -> None:
    (dir_new_requests := r4r_dir() / RegistrationStatusEnum.NEW.name).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,
    )
Beispiel #9
0
def setup_site_context() -> None:
    site_context.agent_output_dir().mkdir(parents=True)
    site_context.r4r_dir().mkdir(parents=True)
    site_context.log_path().parent.mkdir(parents=True)
Beispiel #10
0
def _move_ready_file(uuid: UUID) -> None:
    (dir_discoverable :=
     r4r_dir() / RegistrationStatusEnum.DISCOVERABLE.name).mkdir(exist_ok=True)
    with suppress(FileNotFoundError):
        (r4r_dir() / RegistrationStatusEnum.READY.name /
         f"{uuid}.json").rename(dir_discoverable / f"{uuid}.json")