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.
22 from builtins import KeyboardInterrupt
23 from concurrent import futures
24 from pathlib import Path, PurePath
27 from manager.servicer import ArtifactManagerServicer
28 from proto.BluePrintManagement_pb2_grpc import add_BluePrintManagementServiceServicer_to_server
30 from blueprints_grpc import BluePrintProcessing_pb2_grpc, ScriptExecutorConfiguration
31 from blueprints_grpc.blueprint_processing_server import BluePrintProcessingServer
32 from blueprints_grpc.request_header_validator_interceptor import RequestHeaderValidatorInterceptor
34 logger = logging.getLogger("Server")
36 _ONE_DAY_IN_SECONDS = 60 * 60 * 24
39 def serve(configuration: ScriptExecutorConfiguration):
40 port = configuration.script_executor_property('port')
41 authType = configuration.script_executor_property('authType')
42 maxWorkers = configuration.script_executor_property('maxWorkers')
44 if authType == 'tls-auth':
45 cert_chain_file = configuration.script_executor_property('certChain')
46 private_key_file = configuration.script_executor_property('privateKey')
47 logger.info("Setting GRPC server TLS authentication, cert file(%s) private key file(%s)", cert_chain_file,
49 # read in key and certificate
50 with open(cert_chain_file, 'rb') as f:
51 certificate_chain = f.read()
52 with open(private_key_file, 'rb') as f:
53 private_key = f.read()
55 # create server credentials
56 server_credentials = grpc.ssl_server_credentials(((private_key, certificate_chain),))
59 server = grpc.server(futures.ThreadPoolExecutor(max_workers=int(maxWorkers)))
60 BluePrintProcessing_pb2_grpc.add_BluePrintProcessingServiceServicer_to_server(
61 BluePrintProcessingServer(configuration), server
63 add_BluePrintManagementServiceServicer_to_server(ArtifactManagerServicer(), server)
65 # add secure port using credentials
66 server.add_secure_port('[::]:' + port, server_credentials)
69 logger.info("Setting GRPC server base authentication")
70 basic_auth = configuration.script_executor_property('token')
71 header_validator = RequestHeaderValidatorInterceptor(
72 'authorization', basic_auth, grpc.StatusCode.UNAUTHENTICATED,
74 # create server with token authentication interceptors
75 server = grpc.server(futures.ThreadPoolExecutor(max_workers=int(maxWorkers)),
76 interceptors=(header_validator,))
77 BluePrintProcessing_pb2_grpc.add_BluePrintProcessingServiceServicer_to_server(
78 BluePrintProcessingServer(configuration), server
80 add_BluePrintManagementServiceServicer_to_server(ArtifactManagerServicer(), server)
82 server.add_insecure_port('[::]:' + port)
85 logger.info("Command Executor Server started on %s" % port)
89 time.sleep(_ONE_DAY_IN_SECONDS)
90 except KeyboardInterrupt:
94 if __name__ == '__main__':
95 default_configuration_file = str(PurePath(Path().absolute(), "../../configuration.ini"))
96 supplied_configuration_file = os.environ.get("CONFIGURATION")
97 config_file = str(os.path.expanduser(Path(supplied_configuration_file or default_configuration_file)))
99 configuration = ScriptExecutorConfiguration(config_file)
100 log_file_name = configuration.script_executor_property('logFile')
101 log_file = os.path.join(os.path.dirname(os.path.abspath(os.path.dirname(__file__))), "logging.yaml")
103 with open(log_file) as log:
104 log_config = yaml.safe_load(log)
106 logging_formater = log_config["formatters"]["default"]["format"]
107 print(log_config["loglevel"])
108 if log_config["loglevel"] == "debug":
109 loglevel = logging.DEBUG
110 elif log_config["loglevel"] == "info":
111 loglevel = logging.INFO
112 elif log_config["loglevel"] == "error":
113 loglevel = logging.ERROR
114 logging.basicConfig(filename=log_file_name,
116 format=logging_formater)
117 console = logging.handlers.RotatingFileHandler(log_file_name, maxBytes=log_config["logfilesize"],
118 backupCount=log_config["rollovercount"])
120 console.setLevel(loglevel)
121 formatter = logging.Formatter(logging_formater)
122 console.setFormatter(formatter)
123 logging.getLogger('').addHandler(console)