customized_auth_server.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright 2019 The gRPC Authors
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Server of the Python example of customizing authentication mechanism."""
  15. import argparse
  16. from concurrent import futures
  17. import contextlib
  18. import logging
  19. import _credentials
  20. import grpc
  21. helloworld_pb2, helloworld_pb2_grpc = grpc.protos_and_services(
  22. "helloworld.proto")
  23. _LOGGER = logging.getLogger(__name__)
  24. _LOGGER.setLevel(logging.INFO)
  25. _LISTEN_ADDRESS_TEMPLATE = 'localhost:%d'
  26. _SIGNATURE_HEADER_KEY = 'x-signature'
  27. class SignatureValidationInterceptor(grpc.ServerInterceptor):
  28. def __init__(self):
  29. def abort(ignored_request, context):
  30. context.abort(grpc.StatusCode.UNAUTHENTICATED, 'Invalid signature')
  31. self._abortion = grpc.unary_unary_rpc_method_handler(abort)
  32. def intercept_service(self, continuation, handler_call_details):
  33. # Example HandlerCallDetails object:
  34. # _HandlerCallDetails(
  35. # method=u'/helloworld.Greeter/SayHello',
  36. # invocation_metadata=...)
  37. method_name = handler_call_details.method.split('/')[-1]
  38. expected_metadata = (_SIGNATURE_HEADER_KEY, method_name[::-1])
  39. if expected_metadata in handler_call_details.invocation_metadata:
  40. return continuation(handler_call_details)
  41. else:
  42. return self._abortion
  43. class SimpleGreeter(helloworld_pb2_grpc.GreeterServicer):
  44. def SayHello(self, request, unused_context):
  45. return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)
  46. @contextlib.contextmanager
  47. def run_server(port):
  48. # Bind interceptor to server
  49. server = grpc.server(futures.ThreadPoolExecutor(),
  50. interceptors=(SignatureValidationInterceptor(),))
  51. helloworld_pb2_grpc.add_GreeterServicer_to_server(SimpleGreeter(), server)
  52. # Loading credentials
  53. server_credentials = grpc.ssl_server_credentials(((
  54. _credentials.SERVER_CERTIFICATE_KEY,
  55. _credentials.SERVER_CERTIFICATE,
  56. ),))
  57. # Pass down credentials
  58. port = server.add_secure_port(_LISTEN_ADDRESS_TEMPLATE % port,
  59. server_credentials)
  60. server.start()
  61. try:
  62. yield server, port
  63. finally:
  64. server.stop(0)
  65. def main():
  66. parser = argparse.ArgumentParser()
  67. parser.add_argument('--port',
  68. nargs='?',
  69. type=int,
  70. default=50051,
  71. help='the listening port')
  72. args = parser.parse_args()
  73. with run_server(args.port) as (server, port):
  74. logging.info('Server is listening at port :%d', port)
  75. server.wait_for_termination()
  76. if __name__ == '__main__':
  77. logging.basicConfig(level=logging.INFO)
  78. main()