def setUp(self): self._servicer = _Servicer() self._method_implementations = { (_GROUP, _UNARY_UNARY): utilities.unary_unary_inline(self._servicer.unary_unary), (_GROUP, _UNARY_STREAM): utilities.unary_stream_inline(self._servicer.unary_stream), (_GROUP, _STREAM_UNARY): utilities.stream_unary_inline(self._servicer.stream_unary), (_GROUP, _STREAM_STREAM): utilities.stream_stream_inline(self._servicer.stream_stream), } self._cardinalities = { _UNARY_UNARY: cardinality.Cardinality.UNARY_UNARY, _UNARY_STREAM: cardinality.Cardinality.UNARY_STREAM, _STREAM_UNARY: cardinality.Cardinality.STREAM_UNARY, _STREAM_STREAM: cardinality.Cardinality.STREAM_STREAM, } self._server_options = implementations.server_options( thread_pool_size=test_constants.POOL_SIZE) self._server_credentials = implementations.ssl_server_credentials([ ( resources.private_key(), resources.certificate_chain(), ), ]) self._channel_credentials = implementations.ssl_channel_credentials( resources.test_root_certificates()) self._stub_options = implementations.stub_options( thread_pool_size=test_constants.POOL_SIZE)
def setUp(self): self._servicer = _Servicer() self._method_implementations = { (_GROUP, _UNARY_UNARY): utilities.unary_unary_inline(self._servicer.unary_unary), (_GROUP, _UNARY_STREAM): utilities.unary_stream_inline(self._servicer.unary_stream), (_GROUP, _STREAM_UNARY): utilities.stream_unary_inline(self._servicer.stream_unary), (_GROUP, _STREAM_STREAM): utilities.stream_stream_inline(self._servicer.stream_stream), } self._cardinalities = { _UNARY_UNARY: cardinality.Cardinality.UNARY_UNARY, _UNARY_STREAM: cardinality.Cardinality.UNARY_STREAM, _STREAM_UNARY: cardinality.Cardinality.STREAM_UNARY, _STREAM_STREAM: cardinality.Cardinality.STREAM_STREAM, } self._server_options = implementations.server_options( thread_pool_size=test_constants.POOL_SIZE) self._server_credentials = implementations.ssl_server_credentials( [(resources.private_key(), resources.certificate_chain(),),]) self._client_credentials = implementations.ssl_client_credentials( resources.test_root_certificates(), None, None) self._stub_options = implementations.stub_options( thread_pool_size=test_constants.POOL_SIZE)
async def start_test_server(port=0, secure=False, server_credentials=None, interceptors=None): server = aio.server(options=(('grpc.so_reuseport', 0), ), interceptors=interceptors) servicer = TestServiceServicer() test_pb2_grpc.add_TestServiceServicer_to_server(servicer, server) server.add_generic_rpc_handlers( (_create_extra_generic_handler(servicer), )) if secure: if server_credentials is None: server_credentials = grpc.ssl_server_credentials([ (resources.private_key(), resources.certificate_chain()) ]) port = server.add_secure_port('[::]:%d' % port, server_credentials) else: port = server.add_insecure_port('[::]:%d' % port) await server.start() # NOTE(lidizheng) returning the server to prevent it from deallocation return 'localhost:%d' % port, server
def _create_server(self, config): if config.server_type == control_pb2.SYNC_SERVER: servicer = benchmark_server.BenchmarkServer() server = services_pb2.beta_create_BenchmarkService_server(servicer) elif config.server_type == control_pb2.ASYNC_GENERIC_SERVER: resp_size = config.payload_config.bytebuf_params.resp_size servicer = benchmark_server.GenericBenchmarkServer(resp_size) method_implementations = { ('grpc.testing.BenchmarkService', 'StreamingCall'): utilities.stream_stream_inline(servicer.StreamingCall), ('grpc.testing.BenchmarkService', 'UnaryCall'): utilities.unary_unary_inline(servicer.UnaryCall), } server = implementations.server(method_implementations) else: raise Exception('Unsupported server type {}'.format(config.server_type)) if config.HasField('security_params'): # Use SSL server_creds = implementations.ssl_server_credentials([( resources.private_key(), resources.certificate_chain())]) port = server.add_secure_port('[::]:{}'.format(config.port), server_creds) else: port = server.add_insecure_port('[::]:{}'.format(config.port)) return (server, port)
def instantiate( self, methods, method_implementations, multi_method_implementation): serialization_behaviors = _serialization_behaviors_from_test_methods( methods) # TODO(nathaniel): Add a "groups" attribute to _digest.TestServiceDigest. service = next(iter(methods))[0] # TODO(nathaniel): Add a "cardinalities_by_group" attribute to # _digest.TestServiceDigest. cardinalities = { method: method_object.cardinality() for (group, method), method_object in six.iteritems(methods)} server_options = implementations.server_options( request_deserializers=serialization_behaviors.request_deserializers, response_serializers=serialization_behaviors.response_serializers, thread_pool_size=test_constants.POOL_SIZE) server = implementations.server( method_implementations, options=server_options) server_credentials = implementations.ssl_server_credentials( [(resources.private_key(), resources.certificate_chain(),),]) port = server.add_secure_port('[::]:0', server_credentials) server.start() channel_credentials = implementations.ssl_channel_credentials( resources.test_root_certificates()) channel = test_utilities.not_really_secure_channel( 'localhost', port, channel_credentials, _SERVER_HOST_OVERRIDE) stub_options = implementations.stub_options( request_serializers=serialization_behaviors.request_serializers, response_deserializers=serialization_behaviors.response_deserializers, thread_pool_size=test_constants.POOL_SIZE) generic_stub = implementations.generic_stub(channel, options=stub_options) dynamic_stub = implementations.dynamic_stub( channel, service, cardinalities, options=stub_options) return generic_stub, {service: dynamic_stub}, server
def _create_server(self, config): if config.server_type == control_pb2.SYNC_SERVER: servicer = benchmark_server.BenchmarkServer() server = services_pb2.beta_create_BenchmarkService_server(servicer) elif config.server_type == control_pb2.ASYNC_GENERIC_SERVER: resp_size = config.payload_config.bytebuf_params.resp_size servicer = benchmark_server.GenericBenchmarkServer(resp_size) method_implementations = { ('grpc.testing.BenchmarkService', 'StreamingCall'): utilities.stream_stream_inline(servicer.StreamingCall), ('grpc.testing.BenchmarkService', 'UnaryCall'): utilities.unary_unary_inline(servicer.UnaryCall), } server = implementations.server(method_implementations) else: raise Exception('Unsupported server type {}'.format( config.server_type)) if config.HasField('security_params'): # Use SSL server_creds = implementations.ssl_server_credentials([ (resources.private_key(), resources.certificate_chain()) ]) port = server.add_secure_port('[::]:{}'.format(config.port), server_creds) else: port = server.add_insecure_port('[::]:{}'.format(config.port)) return (server, port)
def _create_server(self, config): if config.async_server_threads == 0: # This is the default concurrent.futures thread pool size, but # None doesn't seem to work server_threads = multiprocessing.cpu_count() * 5 else: server_threads = config.async_server_threads server = grpc.server((), futures.ThreadPoolExecutor( max_workers=server_threads)) if config.server_type == control_pb2.ASYNC_SERVER: servicer = benchmark_server.BenchmarkServer() services_pb2.add_BenchmarkServiceServicer_to_server(servicer, server) elif config.server_type == control_pb2.ASYNC_GENERIC_SERVER: resp_size = config.payload_config.bytebuf_params.resp_size servicer = benchmark_server.GenericBenchmarkServer(resp_size) method_implementations = { 'StreamingCall': grpc.stream_stream_rpc_method_handler(servicer.StreamingCall), 'UnaryCall': grpc.unary_unary_rpc_method_handler(servicer.UnaryCall), } handler = grpc.method_handlers_generic_handler( 'grpc.testing.BenchmarkService', method_implementations) server.add_generic_rpc_handlers((handler,)) else: raise Exception('Unsupported server type {}'.format(config.server_type)) if config.HasField('security_params'): # Use SSL server_creds = grpc.ssl_server_credentials( ((resources.private_key(), resources.certificate_chain()),)) port = server.add_secure_port('[::]:{}'.format(config.port), server_creds) else: port = server.add_insecure_port('[::]:{}'.format(config.port)) return (server, port)
def instantiate( self, methods, method_implementations, multi_method_implementation): serialization_behaviors = _serialization_behaviors_from_test_methods( methods) # TODO(nathaniel): Add a "groups" attribute to _digest.TestServiceDigest. service = next(iter(methods))[0] # TODO(nathaniel): Add a "cardinalities_by_group" attribute to # _digest.TestServiceDigest. cardinalities = { method: method_object.cardinality() for (group, method), method_object in methods.iteritems()} server_options = implementations.server_options( request_deserializers=serialization_behaviors.request_deserializers, response_serializers=serialization_behaviors.response_serializers, thread_pool_size=test_constants.POOL_SIZE) server = implementations.server( method_implementations, options=server_options) server_credentials = implementations.ssl_server_credentials( [(resources.private_key(), resources.certificate_chain(),),]) port = server.add_secure_port('[::]:0', server_credentials) server.start() channel_credentials = implementations.ssl_channel_credentials( resources.test_root_certificates(), None, None) channel = test_utilities.not_really_secure_channel( 'localhost', port, channel_credentials, _SERVER_HOST_OVERRIDE) stub_options = implementations.stub_options( request_serializers=serialization_behaviors.request_serializers, response_deserializers=serialization_behaviors.response_deserializers, thread_pool_size=test_constants.POOL_SIZE) generic_stub = implementations.generic_stub(channel, options=stub_options) dynamic_stub = implementations.dynamic_stub( channel, service, cardinalities, options=stub_options) return generic_stub, {service: dynamic_stub}, server
def setUp(self): server_credentials = cygrpc.server_credentials_ssl( None, [cygrpc.SslPemKeyCertPair(resources.private_key(), resources.certificate_chain())], False) client_credentials = cygrpc.channel_credentials_ssl( resources.test_root_certificates(), None) self.setUpMixin(server_credentials, client_credentials, _SSL_HOST_OVERRIDE)
def xds_channel_server_without_xds(server_fallback_creds): server = grpc.server(futures.ThreadPoolExecutor()) server.add_generic_rpc_handlers((_GenericHandler(), )) server_server_fallback_creds = grpc.ssl_server_credentials( ((resources.private_key(), resources.certificate_chain()), )) server_creds = grpc.xds_server_credentials(server_fallback_creds) port = server.add_secure_port("localhost:0", server_creds) server.start() try: yield "localhost:{}".format(port) finally: server.stop(None)
async def test_port_binding_exception(self): server = aio.server(options=(('grpc.so_reuseport', 0),)) port = server.add_insecure_port('localhost:0') bind_address = "localhost:%d" % port with self.assertRaises(RuntimeError): server.add_insecure_port(bind_address) server_credentials = grpc.ssl_server_credentials([ (resources.private_key(), resources.certificate_chain()) ]) with self.assertRaises(RuntimeError): server.add_secure_port(bind_address, server_credentials)
def test_xds_creds_fallback_ssl(self): # Since there is no xDS server, the fallback credentials will be used. # In this case, SSL credentials. server_fallback_creds = grpc.ssl_server_credentials( ((resources.private_key(), resources.certificate_chain()), )) with xds_channel_server_without_xds( server_fallback_creds) as server_address: override_options = (("grpc.ssl_target_name_override", "foo.test.google.fr"), ) channel_fallback_creds = grpc.ssl_channel_credentials( root_certificates=resources.test_root_certificates(), private_key=resources.private_key(), certificate_chain=resources.certificate_chain()) channel_creds = grpc.xds_channel_credentials( channel_fallback_creds) with grpc.secure_channel(server_address, channel_creds, options=override_options) as channel: request = b"abc" response = channel.unary_unary("/test/method")( request, wait_for_ready=True) self.assertEqual(response, request)
async def setUp(self): server_credentials = grpc.ssl_server_credentials([ (resources.private_key(), resources.certificate_chain()) ]) channel_credentials = grpc.ssl_channel_credentials( resources.test_root_certificates()) self._server_address, self._server = await start_test_server( secure=True, server_credentials=server_credentials) channel_options = (( 'grpc.ssl_target_name_override', _SERVER_HOST_OVERRIDE, ), ) self._channel = aio.secure_channel(self._server_address, channel_credentials, channel_options) self._stub = test_pb2_grpc.TestServiceStub(self._channel)
def setUp(self): self._servicer = _Servicer() method_implementations = { (_GROUP, _UNARY_UNARY): utilities.unary_unary_inline(self._servicer.unary_unary), (_GROUP, _UNARY_STREAM): utilities.unary_stream_inline(self._servicer.unary_stream), (_GROUP, _STREAM_UNARY): utilities.stream_unary_inline(self._servicer.stream_unary), (_GROUP, _STREAM_STREAM): utilities.stream_stream_inline(self._servicer.stream_stream), } cardinalities = { _UNARY_UNARY: cardinality.Cardinality.UNARY_UNARY, _UNARY_STREAM: cardinality.Cardinality.UNARY_STREAM, _STREAM_UNARY: cardinality.Cardinality.STREAM_UNARY, _STREAM_STREAM: cardinality.Cardinality.STREAM_STREAM, } server_options = implementations.server_options( thread_pool_size=test_constants.POOL_SIZE) self._server = implementations.server(method_implementations, options=server_options) server_credentials = implementations.ssl_server_credentials([ ( resources.private_key(), resources.certificate_chain(), ), ]) port = self._server.add_secure_port('[::]:0', server_credentials) self._server.start() self._channel_credentials = implementations.ssl_channel_credentials( resources.test_root_certificates()) self._call_credentials = implementations.metadata_call_credentials( _metadata_plugin) channel = test_utilities.not_really_secure_channel( 'localhost', port, self._channel_credentials, _SERVER_HOST_OVERRIDE) stub_options = implementations.stub_options( thread_pool_size=test_constants.POOL_SIZE) self._dynamic_stub = implementations.dynamic_stub(channel, _GROUP, cardinalities, options=stub_options)
def setUp(self): server_credentials = cygrpc.server_credentials_ssl( None, [cygrpc.SslPemKeyCertPair(resources.private_key(), resources.certificate_chain())], False) channel_credentials = cygrpc.channel_credentials_ssl( resources.test_root_certificates(), None) self.server_completion_queue = cygrpc.CompletionQueue() self.server = cygrpc.Server() self.server.register_completion_queue(self.server_completion_queue) self.port = self.server.add_http2_port('[::]:0', server_credentials) self.server.start() self.client_completion_queue = cygrpc.CompletionQueue() client_channel_arguments = cygrpc.ChannelArgs([ cygrpc.ChannelArg(cygrpc.ChannelArgKey.ssl_target_name_override, _SSL_HOST_OVERRIDE)]) self.client_channel = cygrpc.Channel( 'localhost:{}'.format(self.port), client_channel_arguments, channel_credentials)
def test_default_ssl(self): _private_key = resources.private_key() _certificate_chain = resources.certificate_chain() _server_certs = ((_private_key, _certificate_chain), ) _server_host_override = 'foo.test.google.fr' _test_root_certificates = resources.test_root_certificates() _property_options = (( 'grpc.ssl_target_name_override', _server_host_override, ), ) cert_dir = os.path.join(os.path.dirname(resources.__file__), "credentials") cert_file = os.path.join(cert_dir, "ca.pem") with _env("GRPC_DEFAULT_SSL_ROOTS_FILE_PATH", cert_file): server_creds = grpc.ssl_server_credentials(_server_certs) with _server(server_creds) as port: target = f'localhost:{port}' response = grpc.experimental.unary_unary( _REQUEST, target, _UNARY_UNARY, options=_property_options)
def _create_server(self, config): if config.async_server_threads == 0: # This is the default concurrent.futures thread pool size, but # None doesn't seem to work server_threads = multiprocessing.cpu_count() * 5 else: server_threads = config.async_server_threads server = test_common.test_server(max_workers=server_threads) if config.server_type == control_pb2.ASYNC_SERVER: servicer = benchmark_server.BenchmarkServer() benchmark_service_pb2_grpc.add_BenchmarkServiceServicer_to_server( servicer, server) elif config.server_type == control_pb2.ASYNC_GENERIC_SERVER: resp_size = config.payload_config.bytebuf_params.resp_size servicer = benchmark_server.GenericBenchmarkServer(resp_size) method_implementations = { 'StreamingCall': grpc.stream_stream_rpc_method_handler(servicer.StreamingCall ), 'UnaryCall': grpc.unary_unary_rpc_method_handler(servicer.UnaryCall), } handler = grpc.method_handlers_generic_handler( 'grpc.testing.BenchmarkService', method_implementations) server.add_generic_rpc_handlers((handler,)) else: raise Exception('Unsupported server type {}'.format( config.server_type)) if self._server_port is not None and config.port == 0: server_port = self._server_port else: server_port = config.port if config.HasField('security_params'): # Use SSL server_creds = grpc.ssl_server_credentials( ((resources.private_key(), resources.certificate_chain()),)) port = server.add_secure_port('[::]:{}'.format(server_port), server_creds) else: port = server.add_insecure_port('[::]:{}'.format(server_port)) return (server, port)
def setUp(self): self._servicer = _Servicer() method_implementations = { (_GROUP, _UNARY_UNARY): utilities.unary_unary_inline(self._servicer.unary_unary), (_GROUP, _UNARY_STREAM): utilities.unary_stream_inline(self._servicer.unary_stream), (_GROUP, _STREAM_UNARY): utilities.stream_unary_inline(self._servicer.stream_unary), (_GROUP, _STREAM_STREAM): utilities.stream_stream_inline(self._servicer.stream_stream), } cardinalities = { _UNARY_UNARY: cardinality.Cardinality.UNARY_UNARY, _UNARY_STREAM: cardinality.Cardinality.UNARY_STREAM, _STREAM_UNARY: cardinality.Cardinality.STREAM_UNARY, _STREAM_STREAM: cardinality.Cardinality.STREAM_STREAM, } server_options = implementations.server_options( thread_pool_size=test_constants.POOL_SIZE) self._server = implementations.server( method_implementations, options=server_options) server_credentials = implementations.ssl_server_credentials([ ( resources.private_key(), resources.certificate_chain(), ), ]) port = self._server.add_secure_port('[::]:0', server_credentials) self._server.start() self._channel_credentials = implementations.ssl_channel_credentials( resources.test_root_certificates()) self._call_credentials = implementations.metadata_call_credentials( _metadata_plugin) channel = test_utilities.not_really_secure_channel( 'localhost', port, self._channel_credentials, _SERVER_HOST_OVERRIDE) stub_options = implementations.stub_options( thread_pool_size=test_constants.POOL_SIZE) self._dynamic_stub = implementations.dynamic_stub( channel, _GROUP, cardinalities, options=stub_options)
def setUp(self): server_credentials = cygrpc.server_credentials_ssl( None, [ cygrpc.SslPemKeyCertPair(resources.private_key(), resources.certificate_chain()) ], False) channel_credentials = cygrpc.channel_credentials_ssl( resources.test_root_certificates(), None) self.server_completion_queue = cygrpc.CompletionQueue() self.server = cygrpc.Server() self.server.register_completion_queue(self.server_completion_queue) self.port = self.server.add_http2_port('[::]:0', server_credentials) self.server.start() self.client_completion_queue = cygrpc.CompletionQueue() client_channel_arguments = cygrpc.ChannelArgs([ cygrpc.ChannelArg(cygrpc.ChannelArgKey.ssl_target_name_override, _SSL_HOST_OVERRIDE) ]) self.client_channel = cygrpc.Channel('localhost:{}'.format(self.port), client_channel_arguments, channel_credentials)
def _create_server(config: control_pb2.ServerConfig) -> Tuple[aio.Server, int]: """Creates a server object according to the ServerConfig.""" channel_args = tuple( (arg.name, arg.str_value) if arg.HasField('str_value') else (arg.name, int(arg.int_value)) for arg in config.channel_args) server = aio.server(options=channel_args + (('grpc.so_reuseport', 1), )) if config.server_type == control_pb2.ASYNC_SERVER: servicer = benchmark_servicer.BenchmarkServicer() benchmark_service_pb2_grpc.add_BenchmarkServiceServicer_to_server( servicer, server) elif config.server_type == control_pb2.ASYNC_GENERIC_SERVER: resp_size = config.payload_config.bytebuf_params.resp_size servicer = benchmark_servicer.GenericBenchmarkServicer(resp_size) method_implementations = { 'StreamingCall': grpc.stream_stream_rpc_method_handler(servicer.StreamingCall), 'UnaryCall': grpc.unary_unary_rpc_method_handler(servicer.UnaryCall), } handler = grpc.method_handlers_generic_handler( 'grpc.testing.BenchmarkService', method_implementations) server.add_generic_rpc_handlers((handler, )) else: raise NotImplementedError('Unsupported server type {}'.format( config.server_type)) if config.HasField('security_params'): # Use SSL server_creds = grpc.ssl_server_credentials( ((resources.private_key(), resources.certificate_chain()), )) port = server.add_secure_port('[::]:{}'.format(config.port), server_creds) else: port = server.add_insecure_port('[::]:{}'.format(config.port)) return server, port
_UNARY_UNARY = '/test/UnaryUnary' _SERVER_HOST_OVERRIDE = 'foo.test.google.fr' _CLIENT_IDS = ( b'*.test.google.fr', b'waterzooi.test.google.be', b'*.test.youtube.com', b'192.168.1.3', ) _ID = 'id' _ID_KEY = 'id_key' _AUTH_CTX = 'auth_ctx' _PRIVATE_KEY = resources.private_key() _CERTIFICATE_CHAIN = resources.certificate_chain() _TEST_ROOT_CERTIFICATES = resources.test_root_certificates() _SERVER_CERTS = ((_PRIVATE_KEY, _CERTIFICATE_CHAIN),) _PROPERTY_OPTIONS = (( 'grpc.ssl_target_name_override', _SERVER_HOST_OVERRIDE, ),) def handle_unary_unary(request, servicer_context): return pickle.dumps({ _ID: servicer_context.peer_identities(), _ID_KEY: servicer_context.peer_identity_key(), _AUTH_CTX: servicer_context.auth_context() })
_UNARY_UNARY = '/test/UnaryUnary' _SERVER_HOST_OVERRIDE = 'foo.test.google.fr' _CLIENT_IDS = ( b'*.test.google.fr', b'waterzooi.test.google.be', b'*.test.youtube.com', b'192.168.1.3', ) _ID = 'id' _ID_KEY = 'id_key' _AUTH_CTX = 'auth_ctx' _PRIVATE_KEY = resources.private_key() _CERTIFICATE_CHAIN = resources.certificate_chain() _TEST_ROOT_CERTIFICATES = resources.test_root_certificates() _SERVER_CERTS = ((_PRIVATE_KEY, _CERTIFICATE_CHAIN), ) _PROPERTY_OPTIONS = (( 'grpc.ssl_target_name_override', _SERVER_HOST_OVERRIDE, ), ) def handle_unary_unary(request, servicer_context): return pickle.dumps({ _ID: servicer_context.peer_identities(), _ID_KEY: servicer_context.peer_identity_key(), _AUTH_CTX: servicer_context.auth_context() })