Brinda Santh | 807fbaa | 2019-09-23 10:10:58 -0400 | [diff] [blame] | 1 | #!/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 Szwalkiewicz | be4c464 | 2020-01-30 13:49:18 +0000 | [diff] [blame] | 18 | import logging |
| 19 | import os |
| 20 | import time |
chsailakshmi | 6b6f236 | 2021-08-25 08:03:28 -0400 | [diff] [blame^] | 21 | import yaml |
Brinda Santh | 807fbaa | 2019-09-23 10:10:58 -0400 | [diff] [blame] | 22 | from builtins import KeyboardInterrupt |
| 23 | from concurrent import futures |
Brinda Santh | 807fbaa | 2019-09-23 10:10:58 -0400 | [diff] [blame] | 24 | from pathlib import Path, PurePath |
Marek Szwalkiewicz | be4c464 | 2020-01-30 13:49:18 +0000 | [diff] [blame] | 25 | |
| 26 | import grpc |
| 27 | from manager.servicer import ArtifactManagerServicer |
Jozsef Csongvai | daab14b | 2021-07-26 12:00:59 -0400 | [diff] [blame] | 28 | from proto.BluePrintManagement_pb2_grpc import add_BluePrintManagementServiceServicer_to_server |
Marek Szwalkiewicz | be4c464 | 2020-01-30 13:49:18 +0000 | [diff] [blame] | 29 | |
Jozsef Csongvai | daab14b | 2021-07-26 12:00:59 -0400 | [diff] [blame] | 30 | from blueprints_grpc import BluePrintProcessing_pb2_grpc, ScriptExecutorConfiguration |
| 31 | from blueprints_grpc.blueprint_processing_server import BluePrintProcessingServer |
Marek Szwalkiewicz | be4c464 | 2020-01-30 13:49:18 +0000 | [diff] [blame] | 32 | from blueprints_grpc.request_header_validator_interceptor import RequestHeaderValidatorInterceptor |
Brinda Santh | 807fbaa | 2019-09-23 10:10:58 -0400 | [diff] [blame] | 33 | |
| 34 | logger = logging.getLogger("Server") |
| 35 | |
| 36 | _ONE_DAY_IN_SECONDS = 60 * 60 * 24 |
| 37 | |
| 38 | |
| 39 | def serve(configuration: ScriptExecutorConfiguration): |
| 40 | port = configuration.script_executor_property('port') |
Brinda Santh | a5ceb24 | 2019-10-22 20:47:12 -0400 | [diff] [blame] | 41 | authType = configuration.script_executor_property('authType') |
Brinda Santh | 807fbaa | 2019-09-23 10:10:58 -0400 | [diff] [blame] | 42 | maxWorkers = configuration.script_executor_property('maxWorkers') |
| 43 | |
Brinda Santh | a5ceb24 | 2019-10-22 20:47:12 -0400 | [diff] [blame] | 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, |
| 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 Santh | 807fbaa | 2019-09-23 10:10:58 -0400 | [diff] [blame] | 54 | |
Brinda Santh | a5ceb24 | 2019-10-22 20:47:12 -0400 | [diff] [blame] | 55 | # create server credentials |
| 56 | server_credentials = grpc.ssl_server_credentials(((private_key, certificate_chain),)) |
Brinda Santh | 807fbaa | 2019-09-23 10:10:58 -0400 | [diff] [blame] | 57 | |
Brinda Santh | a5ceb24 | 2019-10-22 20:47:12 -0400 | [diff] [blame] | 58 | # create server |
| 59 | server = grpc.server(futures.ThreadPoolExecutor(max_workers=int(maxWorkers))) |
Jozsef Csongvai | daab14b | 2021-07-26 12:00:59 -0400 | [diff] [blame] | 60 | BluePrintProcessing_pb2_grpc.add_BluePrintProcessingServiceServicer_to_server( |
| 61 | BluePrintProcessingServer(configuration), server |
Marek Szwalkiewicz | be4c464 | 2020-01-30 13:49:18 +0000 | [diff] [blame] | 62 | ) |
Jozsef Csongvai | daab14b | 2021-07-26 12:00:59 -0400 | [diff] [blame] | 63 | add_BluePrintManagementServiceServicer_to_server(ArtifactManagerServicer(), server) |
Brinda Santh | 807fbaa | 2019-09-23 10:10:58 -0400 | [diff] [blame] | 64 | |
Brinda Santh | a5ceb24 | 2019-10-22 20:47:12 -0400 | [diff] [blame] | 65 | # 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 Csongvai | daab14b | 2021-07-26 12:00:59 -0400 | [diff] [blame] | 77 | BluePrintProcessing_pb2_grpc.add_BluePrintProcessingServiceServicer_to_server( |
| 78 | BluePrintProcessingServer(configuration), server |
Marek Szwalkiewicz | be4c464 | 2020-01-30 13:49:18 +0000 | [diff] [blame] | 79 | ) |
Jozsef Csongvai | daab14b | 2021-07-26 12:00:59 -0400 | [diff] [blame] | 80 | add_BluePrintManagementServiceServicer_to_server(ArtifactManagerServicer(), server) |
Brinda Santh | a5ceb24 | 2019-10-22 20:47:12 -0400 | [diff] [blame] | 81 | |
| 82 | server.add_insecure_port('[::]:' + port) |
| 83 | server.start() |
Brinda Santh | 807fbaa | 2019-09-23 10:10:58 -0400 | [diff] [blame] | 84 | |
| 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 | |
| 94 | if __name__ == '__main__': |
Marek Szwalkiewicz | be4c464 | 2020-01-30 13:49:18 +0000 | [diff] [blame] | 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))) |
| 98 | |
Brinda Santh | 807fbaa | 2019-09-23 10:10:58 -0400 | [diff] [blame] | 99 | configuration = ScriptExecutorConfiguration(config_file) |
chsailakshmi | 6b6f236 | 2021-08-25 08:03:28 -0400 | [diff] [blame^] | 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") |
| 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 Santh | 807fbaa | 2019-09-23 10:10:58 -0400 | [diff] [blame] | 116 | format=logging_formater) |
chsailakshmi | 6b6f236 | 2021-08-25 08:03:28 -0400 | [diff] [blame^] | 117 | console = logging.handlers.RotatingFileHandler(log_file_name, maxBytes=log_config["logfilesize"], |
| 118 | backupCount=log_config["rollovercount"]) |
| 119 | |
| 120 | console.setLevel(loglevel) |
Brinda Santh | 807fbaa | 2019-09-23 10:10:58 -0400 | [diff] [blame] | 121 | formatter = logging.Formatter(logging_formater) |
| 122 | console.setFormatter(formatter) |
| 123 | logging.getLogger('').addHandler(console) |
| 124 | serve(configuration) |
chsailakshmi | 6b6f236 | 2021-08-25 08:03:28 -0400 | [diff] [blame^] | 125 | |