async def discovery_response( xds_type: discovery.DiscoveryTypes, discovery_request: DiscoveryRequest = Body(None), host: str = Header('no_host_provided'), ): authenticate(discovery_request) response: dict = await discovery.response(discovery_request, xds_type.value, host=host) extra_headers = { 'X-Sovereign-Client-Build': discovery_request.envoy_version, 'X-Sovereign-Client-Version': discovery_request.version_info, 'X-Sovereign-Requested-Resources': ','.join(discovery_request.resource_names) or 'all', 'X-Sovereign-Requested-Type': xds_type.value, 'X-Sovereign-Response-Version': response['version_info'] } add_log_context(resource_names=discovery_request.resource_names, envoy_ver=discovery_request.envoy_version) if response['version_info'] == discovery_request.version_info: # Configuration is identical, send a Not Modified response return Response(status_code=304, headers=extra_headers) elif len(response.get('resources', [])) == 0: return json_response_class(content={'detail': 'No resources found'}, status_code=404, headers=extra_headers) elif response['version_info'] != discovery_request.version_info: return json_response_class(content=response, headers=extra_headers)
async def perform_discovery( req: DiscoveryRequest, api_version: str, xds: discovery.DiscoveryTypes, skip_auth: bool = False, ) -> ProcessedTemplate: if not skip_auth: authenticate(req) try: type_url = type_urls[api_version][xds.value] req.type_url = type_url except TypeError: pass response = await discovery.response(req, xds) return response
def test_authenticate_rejects_badly_typed_input(bad_input): with pytest.raises(HTTPException) as e: authenticate(bad_input) assert e.status_code == 400
def test_authenticate_rejects_auth_which_does_not_match_configured_passwords( discovery_request, ): discovery_request.node.metadata["auth"] = cipher_suite.encrypt("not valid") with pytest.raises(HTTPException) as e: authenticate(discovery_request) assert e.status_code == 401
def test_authenticate_rejects_a_request_with_missing_auth(discovery_request): with pytest.raises(HTTPException) as e: authenticate(discovery_request) assert e.status_code == 401
def test_authenticate_works_with_mock_request(discovery_request, auth_string): discovery_request.node.metadata["auth"] = auth_string authenticate(discovery_request)
def test_authenticate_rejects_auth_which_does_not_match_configured_passwords( discovery_request): discovery_request.node.metadata['auth'] = encrypt('not valid') with pytest.raises(HTTPException) as e: authenticate(discovery_request) assert e.status_code == 401