blob: 2420344eebb1d8dc746b58d254d08359b728d824 [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
chsailakshmi6b6f2362021-08-25 08:03:28 -040021import yaml
Brinda Santh807fbaa2019-09-23 10:10:58 -040022from builtins import KeyboardInterrupt
23from concurrent import futures
Brinda Santh807fbaa2019-09-23 10:10:58 -040024from pathlib import Path, PurePath
Marek Szwalkiewiczbe4c4642020-01-30 13:49:18 +000025
26import grpc
27from manager.servicer import ArtifactManagerServicer
Jozsef Csongvaidaab14b2021-07-26 12:00:59 -040028from proto.BluePrintManagement_pb2_grpc import add_BluePrintManagementServiceServicer_to_server
Marek Szwalkiewiczbe4c4642020-01-30 13:49:18 +000029
Jozsef Csongvaidaab14b2021-07-26 12:00:59 -040030from blueprints_grpc import BluePrintProcessing_pb2_grpc, ScriptExecutorConfiguration
31from blueprints_grpc.blueprint_processing_server import BluePrintProcessingServer
Marek Szwalkiewiczbe4c4642020-01-30 13:49:18 +000032from blueprints_grpc.request_header_validator_interceptor import RequestHeaderValidatorInterceptor
Brinda Santh807fbaa2019-09-23 10:10:58 -040033
34logger = logging.getLogger("Server")
35
36_ONE_DAY_IN_SECONDS = 60 * 60 * 24
37
38
39def serve(configuration: ScriptExecutorConfiguration):
40 port = configuration.script_executor_property('port')
Brinda Santha5ceb242019-10-22 20:47:12 -040041 authType = configuration.script_executor_property('authType')
Brinda Santh807fbaa2019-09-23 10:10:58 -040042 maxWorkers = configuration.script_executor_property('maxWorkers')
43
Brinda Santha5ceb242019-10-22 20:47:12 -040044 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,
48 private_key_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()
Brinda Santh807fbaa2019-09-23 10:10:58 -040054
Brinda Santha5ceb242019-10-22 20:47:12 -040055 # create server credentials
56 server_credentials = grpc.ssl_server_credentials(((private_key, certificate_chain),))
Brinda Santh807fbaa2019-09-23 10:10:58 -040057
Brinda Santha5ceb242019-10-22 20:47:12 -040058 # create server
59 server = grpc.server(futures.ThreadPoolExecutor(max_workers=int(maxWorkers)))
Jozsef Csongvaidaab14b2021-07-26 12:00:59 -040060 BluePrintProcessing_pb2_grpc.add_BluePrintProcessingServiceServicer_to_server(
61 BluePrintProcessingServer(configuration), server
Marek Szwalkiewiczbe4c4642020-01-30 13:49:18 +000062 )
Jozsef Csongvaidaab14b2021-07-26 12:00:59 -040063 add_BluePrintManagementServiceServicer_to_server(ArtifactManagerServicer(), server)
Brinda Santh807fbaa2019-09-23 10:10:58 -040064
Brinda Santha5ceb242019-10-22 20:47:12 -040065 # add secure port using credentials
66 server.add_secure_port('[::]:' + port, server_credentials)
67 server.start()
68 else:
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,
73 'Access denied!')
74 # create server with token authentication interceptors
75 server = grpc.server(futures.ThreadPoolExecutor(max_workers=int(maxWorkers)),
76 interceptors=(header_validator,))
Jozsef Csongvaidaab14b2021-07-26 12:00:59 -040077 BluePrintProcessing_pb2_grpc.add_BluePrintProcessingServiceServicer_to_server(
78 BluePrintProcessingServer(configuration), server
Marek Szwalkiewiczbe4c4642020-01-30 13:49:18 +000079 )
Jozsef Csongvaidaab14b2021-07-26 12:00:59 -040080 add_BluePrintManagementServiceServicer_to_server(ArtifactManagerServicer(), server)
Brinda Santha5ceb242019-10-22 20:47:12 -040081
82 server.add_insecure_port('[::]:' + port)
83 server.start()
Brinda Santh807fbaa2019-09-23 10:10:58 -040084
85 logger.info("Command Executor Server started on %s" % port)
86
87 try:
88 while True:
89 time.sleep(_ONE_DAY_IN_SECONDS)
90 except KeyboardInterrupt:
91 server.stop(0)
92
93
94if __name__ == '__main__':
Marek Szwalkiewiczbe4c4642020-01-30 13:49:18 +000095 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)))
98
Brinda Santh807fbaa2019-09-23 10:10:58 -040099 configuration = ScriptExecutorConfiguration(config_file)
chsailakshmi6b6f2362021-08-25 08:03:28 -0400100 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")
102 print(log_file)
103 with open(log_file) as log:
104 log_config = yaml.safe_load(log)
105 print(log_config)
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,
115 level=loglevel,
Brinda Santh807fbaa2019-09-23 10:10:58 -0400116 format=logging_formater)
chsailakshmi6b6f2362021-08-25 08:03:28 -0400117 console = logging.handlers.RotatingFileHandler(log_file_name, maxBytes=log_config["logfilesize"],
118 backupCount=log_config["rollovercount"])
119
120 console.setLevel(loglevel)
Brinda Santh807fbaa2019-09-23 10:10:58 -0400121 formatter = logging.Formatter(logging_formater)
122 console.setFormatter(formatter)
123 logging.getLogger('').addHandler(console)
124 serve(configuration)
chsailakshmi6b6f2362021-08-25 08:03:28 -0400125