Unify early_adopter construction functions

It was awkward for the generated code to call an API that offered
both insecure_server and secure_server as well as insecure_stub and
secure_stub. With this change there is just a single server function
and a single stub function and security is decided based on arguments
passed.
pull/1148/head
Nathaniel Manista 10 years ago
parent 675de61e4b
commit f492b16d48
  1. 19
      src/compiler/python_generator.cc
  2. 4
      src/python/interop/interop/_insecure_interop_test.py
  3. 9
      src/python/interop/interop/_secure_interop_test.py
  4. 8
      src/python/interop/interop/client.py
  5. 8
      src/python/interop/interop/server.py
  6. 80
      src/python/src/grpc/early_adopter/implementations.py
  7. 4
      src/python/src/grpc/early_adopter/implementations_test.py
  8. 2
      test/compiler/python_plugin_test.py

@ -271,7 +271,7 @@ bool GetModuleAndMessagePath(const Descriptor* type,
bool PrintServerFactory(const grpc::string& package_qualified_service_name,
const ServiceDescriptor* service, Printer* out) {
out->Print("def early_adopter_create_$Service$_server(servicer, port, "
"root_certificates, key_chain_pairs):\n",
"private_key=None, certificate_chain=None):\n",
"Service", service->name());
{
IndentScope raii_create_server_indent(out);
@ -339,10 +339,10 @@ bool PrintServerFactory(const grpc::string& package_qualified_service_name,
}
out->Print("}\n");
out->Print(
"return implementations.secure_server("
"return implementations.server("
"\"$PackageQualifiedServiceName$\","
" method_service_descriptions, port, root_certificates,"
" key_chain_pairs)\n",
" method_service_descriptions, port, private_key=private_key,"
" certificate_chain=certificate_chain)\n",
"PackageQualifiedServiceName", package_qualified_service_name);
}
return true;
@ -353,7 +353,9 @@ bool PrintStubFactory(const grpc::string& package_qualified_service_name,
map<grpc::string, grpc::string> dict = ListToDict({
"Service", service->name(),
});
out->Print(dict, "def early_adopter_create_$Service$_stub(host, port):\n");
out->Print(dict, "def early_adopter_create_$Service$_stub(host, port,"
" secure=False, root_certificates=None, private_key=None,"
" certificate_chain=None, server_host_override=None):\n");
{
IndentScope raii_create_server_indent(out);
map<grpc::string, grpc::string> method_description_constructors;
@ -419,9 +421,12 @@ bool PrintStubFactory(const grpc::string& package_qualified_service_name,
}
out->Print("}\n");
out->Print(
"return implementations.insecure_stub("
"return implementations.stub("
"\"$PackageQualifiedServiceName$\","
" method_invocation_descriptions, host, port)\n",
" method_invocation_descriptions, host, port, secure=secure,"
" root_certificates=root_certificates, private_key=private_key,"
" certificate_chain=certificate_chain,"
" server_host_override=server_host_override)\n",
"PackageQualifiedServiceName", package_qualified_service_name);
}
return true;

@ -42,11 +42,11 @@ class InsecureInteropTest(
unittest.TestCase):
def setUp(self):
self.server = implementations.insecure_server(
self.server = implementations.server(
methods.SERVICE_NAME, methods.SERVER_METHODS, 0)
self.server.start()
port = self.server.port()
self.stub = implementations.insecure_stub(
self.stub = implementations.stub(
methods.SERVICE_NAME, methods.CLIENT_METHODS, 'localhost', port)
def tearDown(self):

@ -45,14 +45,15 @@ class SecureInteropTest(
unittest.TestCase):
def setUp(self):
self.server = implementations.secure_server(
self.server = implementations.server(
methods.SERVICE_NAME, methods.SERVER_METHODS, 0,
resources.private_key(), resources.certificate_chain())
private_key=resources.private_key(),
certificate_chain=resources.certificate_chain())
self.server.start()
port = self.server.port()
self.stub = implementations.secure_stub(
self.stub = implementations.stub(
methods.SERVICE_NAME, methods.CLIENT_METHODS, 'localhost', port,
resources.test_root_certificates(), None, None,
secure=True, root_certificates=resources.test_root_certificates(),
server_host_override=_SERVER_HOST_OVERRIDE)
def tearDown(self):

@ -66,14 +66,14 @@ def _stub(args):
else:
root_certificates = resources.prod_root_certificates()
stub = implementations.secure_stub(
stub = implementations.stub(
methods.SERVICE_NAME, methods.CLIENT_METHODS, args.server_host,
args.server_port, root_certificates, None, None,
args.server_port, secure=True, root_certificates=root_certificates,
server_host_override=args.server_host_override)
else:
stub = implementations.insecure_stub(
stub = implementations.stub(
methods.SERVICE_NAME, methods.CLIENT_METHODS, args.server_host,
args.server_port)
args.server_port, secure=False)
return stub

@ -53,11 +53,11 @@ def serve():
if args.use_tls:
private_key = resources.private_key()
certificate_chain = resources.certificate_chain()
server = implementations.secure_server(
methods.SERVICE_NAME, methods.SERVER_METHODS, args.port, private_key,
certificate_chain)
server = implementations.server(
methods.SERVICE_NAME, methods.SERVER_METHODS, args.port,
private_key=private_key, certificate_chain=certificate_chain)
else:
server = implementations.insecure_server(
server = implementations.server(
methods.SERVICE_NAME, methods.SERVER_METHODS, args.port)
server.start()

@ -188,43 +188,10 @@ class _Stub(interfaces.Stub):
raise AttributeError(attr)
def _build_stub(
service_name, methods, host, port, secure, root_certificates, private_key,
certificate_chain, server_host_override=None):
breakdown = _face_utilities.break_down_invocation(service_name, methods)
return _Stub(
breakdown, host, port, secure, root_certificates, private_key,
certificate_chain, server_host_override=server_host_override)
def _build_server(service_name, methods, port, private_key, certificate_chain):
breakdown = _face_utilities.break_down_service(service_name, methods)
return _Server(breakdown, port, private_key, certificate_chain)
def insecure_stub(service_name, methods, host, port):
"""Constructs an insecure interfaces.Stub.
Args:
service_name: The package-qualified full name of the service.
methods: A dictionary from RPC method name to
interfaces.RpcMethodInvocationDescription describing the RPCs to be
supported by the created stub. The RPC method names in the dictionary are
not qualified by the service name or decorated in any other way.
host: The host to which to connect for RPC service.
port: The port to which to connect for RPC service.
Returns:
An interfaces.Stub affording RPC invocation.
"""
return _build_stub(
service_name, methods, host, port, False, None, None, None)
def secure_stub(
service_name, methods, host, port, root_certificates, private_key,
certificate_chain, server_host_override=None):
"""Constructs an insecure interfaces.Stub.
def stub(
service_name, methods, host, port, secure=False, root_certificates=None,
private_key=None, certificate_chain=None, server_host_override=None):
"""Constructs an interfaces.Stub.
Args:
service_name: The package-qualified full name of the service.
@ -234,6 +201,7 @@ def secure_stub(
not qualified by the service name or decorated in any other way.
host: The host to which to connect for RPC service.
port: The port to which to connect for RPC service.
secure: Whether or not to construct the stub with a secure connection.
root_certificates: The PEM-encoded root certificates or None to ask for
them to be retrieved from a default location.
private_key: The PEM-encoded private key to use or None if no private key
@ -246,32 +214,15 @@ def secure_stub(
Returns:
An interfaces.Stub affording RPC invocation.
"""
return _build_stub(
service_name, methods, host, port, True, root_certificates, private_key,
breakdown = _face_utilities.break_down_invocation(service_name, methods)
return _Stub(
breakdown, host, port, secure, root_certificates, private_key,
certificate_chain, server_host_override=server_host_override)
def insecure_server(service_name, methods, port):
"""Constructs an insecure interfaces.Server.
Args:
service_name: The package-qualified full name of the service.
methods: A dictionary from RPC method name to
interfaces.RpcMethodServiceDescription describing the RPCs to
be serviced by the created server. The RPC method names in the dictionary
are not qualified by the service name or decorated in any other way.
port: The desired port on which to serve or zero to ask for a port to
be automatically selected.
Returns:
An interfaces.Server that will run with no security and
service unsecured raw requests.
"""
return _build_server(service_name, methods, port, None, None)
def secure_server(service_name, methods, port, private_key, certificate_chain):
"""Constructs a secure interfaces.Server.
def server(
service_name, methods, port, private_key=None, certificate_chain=None):
"""Constructs an interfaces.Server.
Args:
service_name: The package-qualified full name of the service.
@ -281,11 +232,12 @@ def secure_server(service_name, methods, port, private_key, certificate_chain):
are not qualified by the service name or decorated in any other way.
port: The port on which to serve or zero to ask for a port to be
automatically selected.
private_key: A pem-encoded private key.
certificate_chain: A pem-encoded certificate chain.
private_key: A pem-encoded private key, or None for an insecure server.
certificate_chain: A pem-encoded certificate chain, or None for an insecure
server.
Returns:
An interfaces.Server that will serve secure traffic.
"""
return _build_server(
service_name, methods, port, private_key, certificate_chain)
breakdown = _face_utilities.break_down_service(service_name, methods)
return _Server(breakdown, port, private_key, certificate_chain)

@ -106,11 +106,11 @@ _TIMEOUT = 3
class EarlyAdopterImplementationsTest(unittest.TestCase):
def setUp(self):
self.server = implementations.insecure_server(
self.server = implementations.server(
SERVICE_NAME, _SERVICE_DESCRIPTIONS, 0)
self.server.start()
port = self.server.port()
self.stub = implementations.insecure_stub(
self.stub = implementations.stub(
SERVICE_NAME, _INVOCATION_DESCRIPTIONS, 'localhost', port)
def tearDown(self):

@ -177,7 +177,7 @@ def _CreateService(test_pb2, delay):
servicer = Servicer()
server = getattr(
test_pb2, SERVER_FACTORY_IDENTIFIER)(servicer, 0, None, None)
test_pb2, SERVER_FACTORY_IDENTIFIER)(servicer, 0)
with server:
port = server.port()
stub = getattr(test_pb2, STUB_FACTORY_IDENTIFIER)('localhost', port)

Loading…
Cancel
Save