Skip to content

Commit 830d363

Browse files
nareddytleahecoleDoug Mahugh
authored
endpoints: Modify gRPC bookstore to support gRPC on Cloud Run (GoogleCloudPlatform#2764)
* endpoints: Modify gRPC bookstore to support gRPC on Cloud Run - Allow client to make TLS calls - Server determines port from env var Signed-off-by: Teju Nareddy <[email protected]> * Modify dockerfile, move roots.pem Signed-off-by: Teju Nareddy <[email protected]> Co-authored-by: Leah E. Cole <[email protected]> Co-authored-by: Doug Mahugh <[email protected]>
1 parent 96a08de commit 830d363

File tree

5 files changed

+4677
-7
lines changed

5 files changed

+4677
-7
lines changed

endpoints/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Google Cloud Endpoints Examples
2+
3+
The files in this directory are referenced from [Google Cloud Endpoints Documentation](https://cloud.google.com/endpoints/docs/).
4+
5+
### CA File
6+
7+
`roots.pem` is copied from [github.com/grpc/grpc](https://github.com/grpc/grpc/blob/master/etc/roots.pem).
8+
This file should be updated regularly.

endpoints/bookstore-grpc/Dockerfile

-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,4 @@ ADD . /bookstore/
1717
WORKDIR /bookstore
1818
RUN pip install -r requirements.txt
1919

20-
EXPOSE 8000
21-
2220
ENTRYPOINT ["python", "/bookstore/bookstore_server.py"]

endpoints/bookstore-grpc/bookstore_client.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@
2222
import bookstore_pb2_grpc
2323

2424

25-
def run(host, port, api_key, auth_token, timeout):
25+
def run(host, port, api_key, auth_token, timeout, use_tls):
2626
"""Makes a basic ListShelves call against a gRPC Bookstore server."""
2727

28-
channel = grpc.insecure_channel('{}:{}'.format(host, port))
28+
if use_tls:
29+
with open('../roots.pem', 'rb') as f:
30+
creds = grpc.ssl_channel_credentials(f.read())
31+
channel = grpc.secure_channel('{}:{}'.format(host, port), creds)
32+
else:
33+
channel = grpc.insecure_channel('{}:{}'.format(host, port))
2934

3035
stub = bookstore_pb2_grpc.BookstoreStub(channel)
3136
metadata = []
@@ -52,5 +57,8 @@ def run(host, port, api_key, auth_token, timeout):
5257
parser.add_argument(
5358
'--auth_token', default=None,
5459
help='The JWT auth token to use for the call')
60+
parser.add_argument(
61+
'--use_tls', type=bool, default=False,
62+
help='Enable when the server requires TLS')
5563
args = parser.parse_args()
56-
run(args.host, args.port, args.api_key, args.auth_token, args.timeout)
64+
run(args.host, args.port, args.api_key, args.auth_token, args.timeout, args.use_tls)

endpoints/bookstore-grpc/bookstore_server.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import argparse
1818
from concurrent import futures
1919
import time
20+
import os
2021

2122
from google.protobuf import struct_pb2
2223
import grpc
@@ -109,6 +110,8 @@ def serve(port, shutdown_grace_duration):
109110
server.add_insecure_port('[::]:{}'.format(port))
110111
server.start()
111112

113+
print('Listening on port {}'.format(port))
114+
112115
try:
113116
while True:
114117
time.sleep(_ONE_DAY_IN_SECONDS)
@@ -121,11 +124,20 @@ def serve(port, shutdown_grace_duration):
121124
description=__doc__,
122125
formatter_class=argparse.RawDescriptionHelpFormatter)
123126
parser.add_argument(
124-
'--port', type=int, default=8000, help='The port to listen on')
127+
'--port', type=int, default=None,
128+
help='The port to listen on.'
129+
'If arg is not set, will listen on the $PORT env var.'
130+
'If env var is empty, defaults to 8000.')
125131
parser.add_argument(
126132
'--shutdown_grace_duration', type=int, default=5,
127133
help='The shutdown grace duration, in seconds')
128134

129135
args = parser.parse_args()
130136

131-
serve(args.port, args.shutdown_grace_duration)
137+
port = args.port
138+
if not port:
139+
port = os.environ.get('PORT')
140+
if not port:
141+
port = 8000
142+
143+
serve(port, args.shutdown_grace_duration)

0 commit comments

Comments
 (0)