Skip to content

Commit

Permalink
Unify early_adopter construction functions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nathanielmanistaatgoogle committed Mar 30, 2015
1 parent 675de61 commit f492b16
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 88 deletions.
19 changes: 12 additions & 7 deletions src/compiler/python_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/python/interop/interop/_insecure_interop_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
9 changes: 5 additions & 4 deletions src/python/interop/interop/_secure_interop_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions src/python/interop/interop/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
8 changes: 4 additions & 4 deletions src/python/interop/interop/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
80 changes: 16 additions & 64 deletions src/python/src/grpc/early_adopter/implementations.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,43 +188,10 @@ def __getattr__(self, attr):
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.
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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)
4 changes: 2 additions & 2 deletions src/python/src/grpc/early_adopter/implementations_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ def _sum(request_iterator, unused_context):
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):
Expand Down
2 changes: 1 addition & 1 deletion test/compiler/python_plugin_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def HalfDuplexCall(self, request_iter, context):

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)
Expand Down

0 comments on commit f492b16

Please sign in to comment.