3 # Copyright (C) 2019 Bell Canada.
4 # Modifications Copyright © 2018-2019 AT&T Intellectual Property.
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
21 from builtins import KeyboardInterrupt
22 from concurrent import futures
23 from pathlib import Path, PurePath
26 from manager.servicer import ArtifactManagerServicer
27 from proto.BluePrintManagement_pb2_grpc import add_BluePrintManagementServiceServicer_to_server
29 from blueprints_grpc import BluePrintProcessing_pb2_grpc, ScriptExecutorConfiguration
30 from blueprints_grpc.blueprint_processing_server import BluePrintProcessingServer
31 from blueprints_grpc.request_header_validator_interceptor import RequestHeaderValidatorInterceptor
33 logger = logging.getLogger("Server")
35 _ONE_DAY_IN_SECONDS = 60 * 60 * 24
38 def serve(configuration: ScriptExecutorConfiguration):
39 port = configuration.script_executor_property('port')
40 authType = configuration.script_executor_property('authType')
41 maxWorkers = configuration.script_executor_property('maxWorkers')
43 if authType == 'tls-auth':
44 cert_chain_file = configuration.script_executor_property('certChain')
45 private_key_file = configuration.script_executor_property('privateKey')
46 logger.info("Setting GRPC server TLS authentication, cert file(%s) private key file(%s)", cert_chain_file,
48 # read in key and certificate
49 with open(cert_chain_file, 'rb') as f:
50 certificate_chain = f.read()
51 with open(private_key_file, 'rb') as f:
52 private_key = f.read()
54 # create server credentials
55 server_credentials = grpc.ssl_server_credentials(((private_key, certificate_chain),))
58 server = grpc.server(futures.ThreadPoolExecutor(max_workers=int(maxWorkers)))
59 BluePrintProcessing_pb2_grpc.add_BluePrintProcessingServiceServicer_to_server(
60 BluePrintProcessingServer(configuration), server
62 add_BluePrintManagementServiceServicer_to_server(ArtifactManagerServicer(), server)
64 # add secure port using credentials
65 server.add_secure_port('[::]:' + port, server_credentials)
68 logger.info("Setting GRPC server base authentication")
69 basic_auth = configuration.script_executor_property('token')
70 header_validator = RequestHeaderValidatorInterceptor(
71 'authorization', basic_auth, grpc.StatusCode.UNAUTHENTICATED,
73 # create server with token authentication interceptors
74 server = grpc.server(futures.ThreadPoolExecutor(max_workers=int(maxWorkers)),
75 interceptors=(header_validator,))
76 BluePrintProcessing_pb2_grpc.add_BluePrintProcessingServiceServicer_to_server(
77 BluePrintProcessingServer(configuration), server
79 add_BluePrintManagementServiceServicer_to_server(ArtifactManagerServicer(), server)
81 server.add_insecure_port('[::]:' + port)
84 logger.info("Command Executor Server started on %s" % port)
88 time.sleep(_ONE_DAY_IN_SECONDS)
89 except KeyboardInterrupt:
93 if __name__ == '__main__':
94 default_configuration_file = str(PurePath(Path().absolute(), "../../configuration.ini"))
95 supplied_configuration_file = os.environ.get("CONFIGURATION")
96 config_file = str(os.path.expanduser(Path(supplied_configuration_file or default_configuration_file)))
98 configuration = ScriptExecutorConfiguration(config_file)
99 logging_formater = '%(asctime)s - %(name)s - %(threadName)s - %(levelname)s - %(message)s'
100 logging.basicConfig(filename=configuration.script_executor_property('logFile'),
102 format=logging_formater)
103 console = logging.StreamHandler()
104 console.setLevel(logging.INFO)
105 formatter = logging.Formatter(logging_formater)
106 console.setFormatter(formatter)
107 logging.getLogger('').addHandler(console)