fix: ログのIDを設計とあわせる

This commit is contained in:
shimoda.m@nds-tyo.co.jp 2022-07-07 15:12:07 +09:00
parent 4f1c4ea86a
commit 28d10dc054
3 changed files with 48 additions and 62 deletions

View File

@ -18,41 +18,18 @@ class S3Resource:
class ConfigBucket:
__s3_resource: S3Resource = None
__bucket_name: str
__check_target_schema_names_file_path: str
__notice_mail_title_template_file_path: str
__notice_mail_body_template_file_path: str
def __init__(self) -> None:
self.__bucket_name = environments.CONFIG_BUCKET_NAME
self.__check_target_schema_names_file_path = environments.CHECK_TARGET_SCHEMA_NAMES_PATH
self.__notice_mail_title_template_file_path = environments.NOTICE_MAIL_TITLE_TEMPLATE_PATH
self.__notice_mail_body_template_file_path = environments.NOTICE_MAIL_BODY_TEMPLATE_PATH
self.__s3_resource = S3Resource(self.__bucket_name)
@property
def bucket_name(self):
return self.__bucket_name
@property
def check_target_schema_names_file_path(self):
return self.__check_target_schema_names_file_path
@property
def mail_body_file_path(self):
return self.__notice_mail_body_template_file_path
@property
def mail_title_file_path(self):
return self.__notice_mail_title_template_file_path
self.__s3_resource = S3Resource(environments.CONFIG_BUCKET_NAME)
@property
def check_target_schema_names(self):
return self.__s3_resource.get_object(self.__check_target_schema_names_file_path)
return self.__s3_resource.get_object(environments.CHECK_TARGET_SCHEMA_NAMES_PATH)
@property
def notice_mail_title_template(self):
return self.__s3_resource.get_object(self.__notice_mail_title_template_file_path)
return self.__s3_resource.get_object(environments.NOTICE_MAIL_TITLE_TEMPLATE_PATH)
@property
def notice_mail_body_template(self):
return self.__s3_resource.get_object(self.__notice_mail_body_template_file_path)
return self.__s3_resource.get_object(environments.NOTICE_MAIL_BODY_TEMPLATE_PATH)

View File

@ -2,6 +2,6 @@ from dataclasses import dataclass
@dataclass
class ViewSecurityOption:
class NoSecurityOptionView:
schema_name: str
table_name: str

View File

@ -15,8 +15,10 @@ from constants import (CHECK_TARGET_SCHEMAS,
RESPONSE_CODE_PARAMETER_NOT_FOUND, RESPONSE_ERROR,
RESPONSE_ERROR_CODE)
from database import Database
from dto.view_secutiry_option import ViewSecurityOption
from environments import MBJ_NOTICE_TOPIC
from dto.no_security_option_view import NoSecurityOptionView
from environments import (CONFIG_BUCKET_NAME, MBJ_NOTICE_TOPIC,
NDS_NOTICE_TOPIC, NOTICE_MAIL_BODY_TEMPLATE_PATH,
NOTICE_MAIL_TITLE_TEMPLATE_PATH)
from exceptions import (DatabaseConnectionException, FileNotFoundException,
MeDaCaException, ParameterNotFoundException,
QueryExecutionException, SNSPublishException)
@ -28,7 +30,7 @@ logger = MeDaCaLogger.get_logger()
def handler(event, context):
try:
logger.info('I-01-01', '処理開始 Viewセキュリティオプション付与チェック')
logger.info('I-02-02', 'チェック対象スキーマ名ファイルを読み込み 開始')
logger.info('I-02-01', 'チェック対象スキーマ名ファイルを読み込み 開始')
check_target_schemas = read_check_target_schemas()
logger.info('I-02-02', f'チェック対象スキーマ名ファイルを読み込み 終了 チェック対象スキーマ名:{check_target_schemas}')
# print(check_target_schemas)
@ -36,7 +38,7 @@ def handler(event, context):
# DB接続のためのパラメータ取得
db_host, db_user_name, db_user_password = read_db_param_from_parameter_store()
connection = connection_database(db_host, db_user_name, db_user_password)
logger.info('I-03-01', 'データベースへの接続開始 終了')
logger.info('I-03-02', 'データベースへの接続開始 成功')
logger.info('I-04-01', 'Viewセキュリティオプション チェック開始')
check_result = fetch_view_security_options(connection, check_target_schemas)
@ -45,9 +47,18 @@ def handler(event, context):
return
logger.info('I-04-01', 'Viewセキュリティオプション 未設定のViewがあるため、メール送信処理を開始します。')
view_security_options = [ViewSecurityOption(*row) for row in check_result]
mail_title, mail_body = make_notice_mail(view_security_options)
no_security_option_views = [NoSecurityOptionView(*row) for row in check_result]
logger.info(
'I-05-02', f'通知メール(タイトル)テンプレートファイル読込 読込元:{CONFIG_BUCKET_NAME}/{NOTICE_MAIL_TITLE_TEMPLATE_PATH}')
mail_title = read_mail_title()
logger.info(
'I-05-03', '通知メール(タイトル)テンプレートファイルを読み込みました')
logger.info(
'I-05-04', f'通知メール(本文)テンプレートファイル読込 読込元:{CONFIG_BUCKET_NAME}/{NOTICE_MAIL_BODY_TEMPLATE_PATH}')
mail_body_template = read_mail_body_template()
logger.info(
'I-05-05', '通知メール(本文)テンプレートファイルを読み込みました')
mail_body = make_notice_mail_body(no_security_option_views, mail_body_template)
logger.info('I-05-06', f'メール送信指示をします 送信先トピック:{MBJ_NOTICE_TOPIC}')
notice_to_mbj(mail_title, mail_body)
@ -55,10 +66,12 @@ def handler(event, context):
except MeDaCaException as e:
logger.exception(e.error_id, e)
logger.error(f'処理異常通知の送信指示をしました 通知先トピック:{NDS_NOTICE_TOPIC}')
notice_to_nds(e.error_id, e)
raise e
except Exception as e:
logger.exception('E-99', f'想定外のエラーが発生しました エラー内容:{e}')
logger.error('E-ERR-01', f'処理異常通知の送信指示をしました 通知先トピック:{NDS_NOTICE_TOPIC}')
notice_to_nds('E-99', e)
raise e
finally:
@ -104,7 +117,7 @@ def read_db_param_from_parameter_store() -> tuple:
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}')
raise ParameterNotFoundException('E-03-01', f'パラメータストアの取得に失敗しました エラー内容:{e}')
else:
raise Exception(e)
@ -162,30 +175,27 @@ def fetch_view_security_options(connection: Database, check_target_schemas: list
result = cursor.fetchall()
return result
except Exception as e:
raise QueryExecutionException('E-03-02', f'Viewセキュリティオプションチェックに失敗しました エラー内容{e}')
raise QueryExecutionException('E-04-01', f'Viewセキュリティオプションチェックに失敗しました エラー内容{e}')
def make_notice_mail(view_security_options: list[ViewSecurityOption]) -> tuple[str]:
config_bucket = ConfigBucket()
logger.info(
'I-05-02', f'通知メール(タイトル)テンプレートファイル読込 読込元:{config_bucket.bucket_name}/{config_bucket.mail_title_file_path}')
mail_title_template = read_mail_title(config_bucket)
logger.info(
'I-05-02', f'通知メール(本文)テンプレートファイル読込 読込元:{config_bucket.bucket_name}/{config_bucket.mail_body_file_path}')
mail_body_template = read_mail_body_template(config_bucket)
mail_message = MAIL_INDENT.join([f'{option.schema_name}.{option.table_name}' for option in view_security_options])
mail_body = mail_body_template.format(no_option_views=mail_message)
return mail_title_template, mail_body
def read_mail_title(config_bucket: ConfigBucket) -> str:
"""メールタイトルを読み込む
def make_notice_mail_body(no_security_option_views: list[NoSecurityOptionView], mail_body_template: str) -> tuple[str]:
"""メール本文を生成します
Args:
config_bucket (ConfigBucket): 設定ファイル保管バケット操作クラス
view_security_options (list[NoSecurityOptionView]): チェック対象のビュー一覧
mail_body_template (str): メール本文のテンプレート
Returns:
tuple[str]: メール本文
"""
mail_message = MAIL_INDENT.join(
[f'{option.schema_name}.{option.table_name}' for option in no_security_option_views])
mail_body = mail_body_template.format(no_option_views=mail_message)
return mail_body
def read_mail_title() -> str:
"""メールタイトルを読み込む
Raises:
FileNotFoundException: ファイルが読み込めなかったエラー
@ -196,22 +206,20 @@ def read_mail_title(config_bucket: ConfigBucket) -> str:
"""
try:
config_bucket = ConfigBucket()
mail_title = config_bucket.notice_mail_title_template
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}')
raise FileNotFoundException('E-05-01', f'通知メール(タイトル)テンプレートファイルの読み込みに失敗しました エラー内容:{e}')
else:
raise Exception(e)
return mail_title
def read_mail_body_template(config_bucket: ConfigBucket) -> str:
def read_mail_body_template() -> str:
"""メール本文を読み込む
Args:
config_bucket (ConfigBucket): 設定ファイル保管バケット操作クラス
Raises:
FileNotFoundException: ファイルが読み込めなかったエラー
Exception: 想定外のエラー
@ -220,10 +228,11 @@ def read_mail_body_template(config_bucket: ConfigBucket) -> str:
str: メール本文
"""
try:
config_bucket = ConfigBucket()
mail_body_template = config_bucket.notice_mail_body_template
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}')
raise FileNotFoundException('E-05-02', f'通知メール(本文)テンプレートファイルの読み込みに失敗しました エラー内容:{e}')
else:
raise Exception(e)