""" Viewセキュリティオプション付与チェック用Lambda関数のエントリーポイント """ import json import botocore from aws.s3 import ConfigBucket from aws.ssm import SSMParameterStore from constants import (CHECK_TARGET_SCHEMAS, RESPONSE_CODE_NO_SUCH_KEY, RESPONSE_CODE_PARAMETER_NOT_FOUND, RESPONSE_ERROR, RESPONSE_ERROR_CODE) from exceptions import (FileNotFoundException, MeDaCaException, ParameterNotFoundException) from medaca_logger import MeDaCaLogger def handler(event, context): logger = MeDaCaLogger.get_logger() try: logger.info('I-01-01', '処理開始 Viewセキュリティオプション付与チェック') logger.info('I-02-02', 'チェック対象スキーマ名ファイルを読み込み 開始') check_target_schemas = read_check_target_schemas() logger.info('I-02-02', f'チェック対象スキーマ名ファイルを読み込み 終了 チェック対象スキーマ名:{check_target_schemas}') # print(check_target_schemas) logger.info('I-03-01', 'データベースへの接続開始 開始') # DB接続のためのパラメータ取得 db_host, db_user_name, db_user_password = read_db_param_from_parameter_store() # print(db_host, db_user_name, db_user_password) logger.info('I-03-01', 'データベースへの接続開始 終了') except MeDaCaException as e: logger.exception(e.error_id, e) raise e except Exception as e: logger.exception('E-99', f'想定外のエラーが発生しました エラー内容:{e}') raise e finally: logger.info('I-06-01', '処理終了 Viewセキュリティオプション付与チェック') def read_check_target_schemas() -> list: """設定ファイル[チェック対象スキーマ名ファイル]を読み込む Raises: exceptions.FileNotFoundException: ファイルが読み込めなかったエラー Exception: 想定外のエラー Returns: list: チェック対象のスキーマ名のリスト """ try: config_bucket = ConfigBucket() check_target_schema_names = config_bucket.check_target_schema_names() return json.loads(check_target_schema_names)[CHECK_TARGET_SCHEMAS] except botocore.exceptions.ClientError as e: if e.response[RESPONSE_ERROR][RESPONSE_ERROR_CODE] == RESPONSE_CODE_NO_SUCH_KEY: raise FileNotFoundException('E-02-01', f'チェック対象スキーマ名ファイルの読み込みに失敗しました エラー内容:{e}') else: raise Exception(e) def read_db_param_from_parameter_store() -> tuple: """パラメータストアからDB接続情報を取得する Raises: FileNotFoundException: _description_ Exception: 想定外のエラー Returns: tuple: DB接続情報 """ try: parameter_store = SSMParameterStore() db_host = parameter_store.db_host() db_user_name = parameter_store.db_user_name() db_user_password = parameter_store.db_user_password() return db_host, db_user_name, db_user_password except botocore.exceptions.ClientError as e: if e.response[RESPONSE_ERROR][RESPONSE_ERROR_CODE] == RESPONSE_CODE_PARAMETER_NOT_FOUND: raise ParameterNotFoundException('E-03-02', f'パラメータストアの取得に失敗しました エラー内容:{e}') else: raise Exception(e) # ローカル実行用 if __name__ == '__main__': handler({}, {})