blob: 7f6537011bf4a51196449102bcdb5ce5331dbd1c [file] [log] [blame]
Brinda Santh807fbaa2019-09-23 10:10:58 -04001#!/usr/bin/python
2#
3# Copyright (C) 2019 Bell Canada.
4# Modifications Copyright © 2018-2019 AT&T Intellectual Property.
5#
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
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
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.
17
Marek Szwalkiewiczbe4c4642020-01-30 13:49:18 +000018import logging
19import os
20import time
Brinda Santh807fbaa2019-09-23 10:10:58 -040021from builtins import KeyboardInterrupt
22from concurrent import futures
Brinda Santh807fbaa2019-09-23 10:10:58 -040023from pathlib import Path, PurePath
Marek Szwalkiewiczbe4c4642020-01-30 13:49:18 +000024
25import grpc
26from manager.servicer import ArtifactManagerServicer
Jozsef Csongvai45263f52021-07-26 12:00:59 -040027from proto.BluePrintManagement_pb2_grpc import add_BluePrintManagementServiceServicer_to_server
Marek Szwalkiewiczbe4c4642020-01-30 13:49:18 +000028
Jozsef Csongvai45263f52021-07-26 12:00:59 -040029from blueprints_grpc import BluePrintProcessing_pb2_grpc, ScriptExecutorConfiguration
30from blueprints_grpc.blueprint_processing_server import BluePrintProcessingServer
Marek Szwalkiewiczbe4c4642020-01-30 13:49:18 +000031from blueprints_grpc.request_header_validator_interceptor import RequestHeaderValidatorInterceptor
Brinda Santh807fbaa2019-09-23 10:10:58 -040032
33logger = logging.getLogger("Server")
34
35_ONE_DAY_IN_SECONDS = 60 * 60 * 24
36
37
38def serve(configuration: ScriptExecutorConfiguration):
39 port = configuration.script_executor_property('port')
Brinda Santha5ceb242019-10-22 20:47:12 -040040 authType = configuration.script_executor_property('authType')
Brinda Santh807fbaa2019-09-23 10:10:58 -040041 maxWorkers = configuration.script_executor_property('maxWorkers')
42
Brinda Santha5ceb242019-10-22 20:47:12 -040043 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,
47 private_key_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()
Brinda Santh807fbaa2019-09-23 10:10:58 -040053
Brinda Santha5ceb242019-10-22 20:47:12 -040054 # create server credentials
55 server_credentials = grpc.ssl_server_credentials(((private_key, certificate_chain),))
Brinda Santh807fbaa2019-09-23 10:10:58 -040056
Brinda Santha5ceb242019-10-22 20:47:12 -040057 # create server
58 server = grpc.server(futures.ThreadPoolExecutor(max_workers=int(maxWorkers)))
Jozsef Csongvai45263f52021-07-26 12:00:59 -040059 BluePrintProcessing_pb2_grpc.add_BluePrintProcessingServiceServicer_to_server(
60 BluePrintProcessingServer(configuration), server
Marek Szwalkiewiczbe4c4642020-01-30 13:49:18 +000061 )
Jozsef Csongvai45263f52021-07-26 12:00:59 -040062 add_BluePrintManagementServiceServicer_to_server(ArtifactManagerServicer(), server)
Brinda Santh807fbaa2019-09-23 10:10:58 -040063
Brinda Santha5ceb242019-10-22 20:47:12 -040064 # add secure port using credentials
65 server.add_secure_port('[::]:' + port, server_credentials)
66 server.start()
67 else:
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,
72 'Access denied!')
73 # create server with token authentication interceptors
74 server = grpc.server(futures.ThreadPoolExecutor(max_workers=int(maxWorkers)),
75 interceptors=(header_validator,))
Jozsef Csongvai45263f52021-07-26 12:00:59 -040076 BluePrintProcessing_pb2_grpc.add_BluePrintProcessingServiceServicer_to_server(
77 BluePrintProcessingServer(configuration), server
Marek Szwalkiewiczbe4c4642020-01-30 13:49:18 +000078 )
Jozsef Csongvai45263f52021-07-26 12:00:59 -040079 add_BluePrintManagementServiceServicer_to_server(ArtifactManagerServicer(), server)
Brinda Santha5ceb242019-10-22 20:47:12 -040080
81 server.add_insecure_port('[::]:' + port)
82 server.start()
Brinda Santh807fbaa2019-09-23 10:10:58 -040083
84 logger.info("Command Executor Server started on %s" % port)
85
86 try:
87 while True:
88 time.sleep(_ONE_DAY_IN_SECONDS)
89 except KeyboardInterrupt:
90 server.stop(0)
91
92
93if __name__ == '__main__':
Marek Szwalkiewiczbe4c4642020-01-30 13:49:18 +000094 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)))
97
Brinda Santh807fbaa2019-09-23 10:10:58 -040098 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'),
101 level=logging.DEBUG,
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)
108 serve(configuration)