Exemplo n.º 1
0
    def request(self, method, url, **kwargs):
        try:
            url = urljoin(self._host_address, url)
            json = kwargs.get(u"json")

            if json is not None:
                kwargs[u"data"] = json_lib.dumps(_filter_out_none(json))
            if u"json" in kwargs:
                del kwargs[u"json"]

            self._renew_authentication(use_cache=True)

            tries = 0
            max_tries = 2
            while tries < max_tries:
                response, unauthorized = self._try_make_request(method, url, **kwargs)
                tries += 1

                if unauthorized and tries < max_tries:
                    self._renew_authentication()
                    continue

                if response.status_code >= 400:
                    response.raise_for_status()

                if not kwargs.get(u"stream"):
                    response.encoding = u"utf-8"  # setting this manually speeds up read times

                return Py42Response(response)
        except requests.HTTPError as err:
            raise_py42_error(err)
Exemplo n.º 2
0
    def get_security_plan_storage_info_list(self, user_uid):
        """Gets IDs (plan UID, node GUID, and destination GUID) for the storage nodes containing
        the legacy security event data for the user with the given UID.
        `REST Documentation <https://console.us.code42.com/swagger/#/Feature/getStorageNode>`__

        Args:
            user_uid (str): A UID for the user to get plan storage information for.

        Returns:
            list[:class:`py42.modules.securitydata.PlanStorageInfo`]
        """
        locations = None
        try:
            response = self._security_client.get_security_event_locations(
                user_uid)
            locations = response[u"securityPlanLocationsByDestination"]
        except HTTPError as err:
            if err.response.status_code == 404:
                pass
            else:
                raise_py42_error(err)

        if locations:
            plan_destination_map = _get_plan_destination_map(locations)
            selected_plan_infos = self._get_plan_storage_infos(
                plan_destination_map)
            if not selected_plan_infos:
                raise Py42SecurityPlanConnectionError(
                    u"Could not establish a connection to retrieve "
                    u"security events for user {0}".format(user_uid))

            return selected_plan_infos
Exemplo n.º 3
0
 def test_raise_py42_http_error_has_correct_response_type(
         self, error_response, status_code):
     error_response.response.status_code = status_code
     try:
         raise_py42_error(error_response)
     except Exception as e:
         assert isinstance(e.response, type(error_response.response))
Exemplo n.º 4
0
 def test_raise_py42_error_raises_MFA_required_error(self, error_response):
     error_response.response.status_code = 401
     error_response.response.text = (
         '{"error":[{"primaryErrorKey":"TIME_BASED_ONE_TIME_PASSWORD_REQUIRED"}]}'
     )
     with pytest.raises(Py42MFARequiredError):
         raise_py42_error(error_response)
Exemplo n.º 5
0
    def test_raise_py42_error_raises_py42_http_error(self, error_response):
        error_response.response.status_code = 600
        with pytest.raises(Py42HTTPError):
            raise_py42_error(error_response)

        error_response.response.status_code = 999
        with pytest.raises(Py42HTTPError):
            raise_py42_error(error_response)
Exemplo n.º 6
0
def _handle_error(method, url, response):
    if response is None:
        msg = f"No response was returned for {method} request to {url}."
        raise Py42Error(msg)

    try:
        response.raise_for_status()
    except HTTPError as ex:
        raise_py42_error(ex)
Exemplo n.º 7
0
 def test_raise_py42_error_when_has_unexpected_error_returns_api_error_response(
     self, mock_error_response, mocker
 ):
     mock_error_response.response.status_code = 410
     error_message = '{"error": { "message": "error"}}'
     mock_error_response.response.text = error_message
     mock_method = mocker.patch.object(Py42ResponseError, "__init__", autospec=True)
     with pytest.raises(Py42HTTPError):
         raise_py42_error(mock_error_response)
     mock_method.assert_called_with(
         Py42HTTPError(mock_error_response),
         mock_error_response.response,
         "Failure in HTTP call {}. Response content: {}".format(
             str(mock_error_response), error_message
         ),
     )
Exemplo n.º 8
0
    def test_raise_py42_error_raises_internal_server_error(self, error_response):
        error_response.response.status_code = 500
        with pytest.raises(Py42InternalServerError):
            raise_py42_error(error_response)

        error_response.response.status_code = 501
        with pytest.raises(Py42InternalServerError):
            raise_py42_error(error_response)

        error_response.response.status_code = 599
        with pytest.raises(Py42InternalServerError):
            raise_py42_error(error_response)

        error_response.response.status_code = 550
        with pytest.raises(Py42InternalServerError):
            raise_py42_error(error_response)
Exemplo n.º 9
0
 def test_raise_py42_error_raises_not_found_error(self, error_response):
     error_response.response.status_code = 404
     with pytest.raises(Py42NotFoundError):
         raise_py42_error(error_response)
Exemplo n.º 10
0
 def test_raise_py42_error_raises_forbidden_error(self, error_response):
     error_response.response.status_code = 403
     with pytest.raises(Py42ForbiddenError):
         raise_py42_error(error_response)
Exemplo n.º 11
0
 def test_raise_py42_error_raises_unauthorized_error(self, error_response):
     error_response.response.status_code = 401
     with pytest.raises(Py42UnauthorizedError):
         raise_py42_error(error_response)
Exemplo n.º 12
0
 def test_raise_py42_error_raises_bad_request_error(self, error_response):
     error_response.response.status_code = 400
     with pytest.raises(Py42BadRequestError):
         raise_py42_error(error_response)
Exemplo n.º 13
0
 def test_raise_py42_error_raises_too_many_requests_error(self, error_response):
     error_response.response.status_code = 429
     with pytest.raises(Py42TooManyRequestsError):
         raise_py42_error(error_response)
Exemplo n.º 14
0
 def test_raise_py42_error_raises_conflict_error(self, error_response):
     error_response.response.status_code = 409
     with pytest.raises(Py42ConflictError):
         raise_py42_error(error_response)
Exemplo n.º 15
0
 def test_raise_py42_error_raises_unauthorized_error(self, error_response):
     error_response.response.status_code = 401
     with pytest.raises(Py42UnauthorizedError, match=REQUEST_EXCEPTION_MESSAGE):
         raise_py42_error(error_response)