def _process(self, flow: HTTPFlow, phase: Phase): route, filters = match_route( proxy_mode=ctx.get_proxy_context().mode, phase=phase, flow=flow, ) if not route: return match_details = {'filters': [], 'route_id': route.id} matched_filters = match_details['filters'] for fltr in filters: if fltr.has_operations: pipeline = build_pipeline(fltr) pipeline.evaluate(flow, phase) operation_applied = True else: operation_applied = transform(flow, phase, fltr) matched_filters.append({ 'id': fltr.id, 'operation_applied': operation_applied, }) phase_obj = getattr(flow, phase.value.lower()) phase_obj.match_details = match_details
def test_match_route_inbound(monkeypatch): route = RouteFactory() filters = RuleEntryFactory.build_batch(2, route_id=route.id) filters[1].expression_snapshot['rules'][0]['expression']['values'] = ['/put'] route.rule_entries_list = filters monkeypatch.setattr( 'satellite.routes.matcher.route_manager', Mock(get_all_by_type=Mock(return_value=[route])), ) emit_audit_log = Mock() monkeypatch.setattr( 'satellite.routes.matcher.audit_logs.emit', emit_audit_log, ) flow = load_flow('http_raw') matched_route, matched_filters = match_route( proxy_mode=ProxyMode.REVERSE, phase=Phase.REQUEST, flow=flow, ) assert matched_route is route assert matched_filters == [filters[0]] emit_audit_log.assert_has_calls( [ call( FilterEvaluationLogRecord( flow_id=flow.id, matched=True, phase=Phase.REQUEST, proxy_mode=ProxyMode.REVERSE, route_id=route.id, filter_id=filters[0].id, ) ), call( FilterEvaluationLogRecord( flow_id=flow.id, matched=False, phase=Phase.REQUEST, proxy_mode=ProxyMode.REVERSE, route_id=route.id, filter_id=filters[1].id, ) ), call( RouteEvaluationLogRecord( flow_id=flow.id, matched=True, phase=Phase.REQUEST, proxy_mode=ProxyMode.REVERSE, route_id=route.id, ) ), ] )
def test_match_route_no_match(monkeypatch): monkeypatch.setattr('satellite.routes.matcher.route_manager', Mock(get_all_by_type=Mock(return_value=[]))) emit_audit_log = Mock() monkeypatch.setattr( 'satellite.routes.matcher.audit_logs.emit', emit_audit_log, ) flow = load_flow('http_raw') matched_route, matched_filters = match_route( proxy_mode=ProxyMode.REVERSE, phase=Phase.REQUEST, flow=flow, ) assert matched_route is None assert matched_filters == [] emit_audit_log.assert_not_called()
def test_match_route_outbound(monkeypatch): filters = RuleEntryFactory.build_batch(2) filters[1].expression_snapshot['rules'][0]['expression']['values'] = ['/put'] route1 = RouteFactory(rule_entries_list=filters) route1.host_endpoint = r'((echo\.apps\.verygood\.systems)|(httpbin\.org))' route2 = RouteFactory(rule_entries_list=[RuleEntryFactory()]) route2.host_endpoint = r'echo\.apps\.verygood\.systems' monkeypatch.setattr( 'satellite.routes.matcher.route_manager', Mock(get_all_by_type=Mock(return_value=[route1, route2])), ) flow = load_flow('http_raw') matched_route, matched_filters = match_route( proxy_mode=ProxyMode.FORWARD, phase=Phase.REQUEST, flow=flow, ) assert matched_route is route1 assert matched_filters == [filters[0]]