Exemplo n.º 1
0
class RequestRouterWithCRLValidatorTestCase(TestCase):
    def setUp(self) -> None:
        super().setUp()
        self.sas_url = 'https://fake.sas.url'
        self.rc_ingest_url = 'https://fake.rc.url'
        self.crl_validator_mock = mock.MagicMock(spec=CRLValidator)
        self.router = RequestRouter(
            sas_url=self.sas_url,
            rc_ingest_url=self.rc_ingest_url,
            cert_path='fake/cert/path',
            ssl_key_path='fake/key/path',
            request_mapping=request_mapping,
            ssl_verify=False,
            crl_validator=self.crl_validator_mock,
        )

    @mocket.mocketize
    def test_request_router_uses_crl_validator(self, mocker: requests_mock.Mocker) -> None:
        # Given
        mocker.register_uri('POST', f'{self.sas_url}/registration')

        # When
        self.router.post_to_sas(request_dict={"registrationRequest": [{"foo": "bar"}]})

        # Then
        self.crl_validator_mock.is_valid.assert_called_once_with(url=self.sas_url)
Exemplo n.º 2
0
 def setUp(self):
     super().setUp()
     self.sas_url = 'https://fake.sas.url'
     self.rc_ingest_url = 'https://fake.rc.url'
     self.router = RequestRouter(
         sas_url=self.sas_url,
         rc_ingest_url=self.rc_ingest_url,
         cert_path='fake/cert/path',
         ssl_key_path='fake/key/path',
         request_mapping=request_mapping,
         ssl_verify=False,
     )
Exemplo n.º 3
0
 def setUp(self) -> None:
     super().setUp()
     self.sas_url = 'https://fake.sas.url'
     self.rc_ingest_url = 'https://fake.rc.url'
     self.crl_validator_mock = mock.MagicMock(spec=CRLValidator)
     self.router = RequestRouter(
         sas_url=self.sas_url,
         rc_ingest_url=self.rc_ingest_url,
         cert_path='fake/cert/path',
         ssl_key_path='fake/key/path',
         request_mapping=request_mapping,
         ssl_verify=False,
         crl_validator=self.crl_validator_mock,
     )
Exemplo n.º 4
0
def run():
    """
    Top-level function for configuration controller
    """
    config = get_config()
    scheduler = BackgroundScheduler()
    db_engine = create_engine(
        url=config.SQLALCHEMY_DB_URI,
        encoding=config.SQLALCHEMY_DB_ENCODING,
        echo=config.SQLALCHEMY_ECHO,
        future=config.SQLALCHEMY_FUTURE,
        pool_size=config.SQLALCHEMY_ENGINE_POOL_SIZE,
        max_overflow=config.SQLALCHEMY_ENGINE_MAX_OVERFLOW,
    )
    session_manager = SessionManager(db_engine=db_engine)
    router = RequestRouter(
        sas_url=config.SAS_URL,
        rc_ingest_url=config.RC_INGEST_URL,
        cert_path=config.CC_CERT_PATH,
        ssl_key_path=config.CC_SSL_KEY_PATH,
        request_mapping=request_mapping,
        ssl_verify=config.SAS_CERT_PATH,
    )
    fluentd_client = FluentdClient()
    for request_type in RequestTypes:
        req_type = request_type.value
        response_type = request_response[req_type]
        consumer = RequestDBConsumer(
            request_type=req_type,
            request_processing_limit=config.REQUEST_PROCESSING_LIMIT,
        )
        processor = ResponseDBProcessor(
            response_type=response_type,
            process_responses_func=processor_strategies[req_type]["process_responses"],
            fluentd_client=fluentd_client,
        )

        scheduler.add_job(
            process_requests,
            args=[consumer, processor, router, session_manager, fluentd_client],
            trigger=IntervalTrigger(
                seconds=config.REQUEST_PROCESSING_INTERVAL_SEC,
            ),
            max_instances=1,
            name=f"{req_type}_job",
        )
    scheduler.start()

    while True:
        time.sleep(1)
Exemplo n.º 5
0
def process_requests(
        consumer: RequestDBConsumer,
        processor: ResponseDBProcessor,
        router: RequestRouter,
        session_manager: SessionManager,
        fluentd_client: FluentdClient,
) -> Optional[requests.Response]:
    """
    Process SAS requests
    """

    with session_manager.session_scope() as session:
        requests_map = consumer.get_pending_requests(session)
        requests_type = next(iter(requests_map))
        requests_list = requests_map[requests_type]

        if not requests_list:
            logger.debug(f"Received no {requests_type} requests.")
            return None

        no_of_requests = len(requests_list)
        logger.info(
            f'Processing {no_of_requests} {requests_type} requests',
        )
        bulked_sas_requests = merge_requests(requests_map)

        _log_requests_map(requests_map, fluentd_client)
        try:
            sas_response = router.post_to_sas(bulked_sas_requests)
            logger.info(
                f"Sent {bulked_sas_requests} to SAS and got the following response: {sas_response.content}",
            )
        except RequestRouterError as e:
            logging.error(f"Error posting request to SAS: {e}")
            return None

        logger.info(f"About to process responses {sas_response=}")
        processor.process_response(requests_list, sas_response, session)

        session.commit()

        return sas_response
Exemplo n.º 6
0
class RequestRouterTestCase(TestCase):
    def setUp(self):
        super().setUp()
        self.sas_url = 'https://fake.sas.url'
        self.rc_ingest_url = 'https://fake.rc.url'
        self.router = RequestRouter(
            sas_url=self.sas_url,
            rc_ingest_url=self.rc_ingest_url,
            cert_path='fake/cert/path',
            ssl_key_path='fake/key/path',
            request_mapping=request_mapping,
            ssl_verify=False,
        )

    def test_router_forwarding_existing_sas_methods(self, mocker):
        # Given
        self._register_test_post_endpoints(
            mocker,
            f'{self.sas_url}/registration',
        )

        # When
        resp = self.router.post_to_sas(
            request_dict={"registrationRequest": [{
                "foo": "bar"
            }]}, )

        # Then
        self.assertEqual(200, resp.status_code)

    def test_sas_response_is_forwarded_to_rc(self, mocker):
        # Given
        self._register_test_post_endpoints(
            mocker,
            f'{self.sas_url}/registration',
        )
        self._register_test_post_endpoints(mocker, self.rc_ingest_url)

        # When
        sas_resp = self.router.post_to_sas(
            request_dict={"registrationRequest": [{
                "foo": "bar"
            }]}, )
        rc_resp = self.router.redirect_sas_response_to_radio_controller(
            sas_resp, )

        # Then
        self.assertEqual(200, rc_resp.status_code)

    def test_router_raises_exception_for_non_existing_sas_method(self, mocker):
        # Given
        self._register_test_post_endpoints(mocker, f'{self.sas_url}/test_post')

        # When / Then
        with self.assertRaises(RequestRouterError):
            self.router.post_to_sas(
                request_dict={"nonExistingSasMethod": [{}]}, )

    def _register_test_post_endpoints(self, mocker, url):
        mocker.register_uri('POST', url, json=self._response_callback)

    @staticmethod
    def _get_from_fixture(fixture_rel_path):
        return os.path.join(os.path.dirname(__file__), "fixtures",
                            fixture_rel_path)

    @staticmethod
    def _response_callback(request, context):
        context.status_code = 200
        return request.text