def test_read_yaml_success(self): trace_id = copy.deepcopy(DataCommon.USER_ID_TEST.format(str(3))) with patch('premembers.common.FileUtils.read_object', return_value=DataYaml.DATA_FILE_YAML): response = FileUtils.read_yaml( trace_id, CommonConst.S3_SETTING_BUCKET, CommonConst.NOTIFY_CONFIG_CIS_RESULT_MAIL) self.assertEqual(DataJson.DATA_AFTER_YAML_LOAD, response)
def execute_change_email(apply_id): pm_logger = common_utils.begin_logger(apply_id, __name__, inspect.currentframe()) # バリデーションチェックを行います if common_utils.is_null(apply_id): return common_utils.error_common(MsgConst.ERR_201, HTTPStatus.UNPROCESSABLE_ENTITY, pm_logger) # set default value caller_service_name = 'insightwatch' # S3から通知メール送信設定ファイルを取得します。 try: config = FileUtils.read_yaml(apply_id, CommonConst.S3_SETTING_BUCKET, CommonConst.NOTIFY_CONFIG_CIS_RESULT_MAIL) except PmError as e: pm_logger.error( "メールアドレス変更通知メール送信設定ファイルの取得に失敗しました。:s3://%s/%s", common_utils.get_environ(CommonConst.S3_SETTING_BUCKET), CommonConst.NOTIFY_CONFIG_CIS_RESULT_MAIL) common_utils.error_exception(MsgConst.ERR_S3_702, HTTPStatus.INTERNAL_SERVER_ERROR, e, pm_logger, True) return common_utils.get_response_by_response_body( HTTPStatus.OK, CommonConst.DEFAULT_RESPONSE_ERROR_PAGE[caller_service_name], is_response_json=False, content_type=CommonConst.CONTENT_TYPE_TEXT_HTML) # set data response error page default response_error_page = config[CommonConst.KEY_RESPONSE_ERROR_PAGE.format( serviceName=caller_service_name)] # メールアドレス変更申請テーブルから申請レコードを取得します。 try: email_change_apply_info = pm_emailChangeApply.query_key( apply_id, apply_id, None) except PmError: pm_logger.error("メールアドレス変更申請テーブルでレコード取得に失敗しました。変更申請ID: %s", apply_id) return common_utils.get_response_by_response_body( HTTPStatus.OK, response_error_page, is_response_json=False, content_type=CommonConst.CONTENT_TYPE_TEXT_HTML) if not email_change_apply_info: pm_logger.warning("メールアドレス変更申請テーブルでレコードが存在しませんでした。変更申請ID: %s", apply_id) return common_utils.get_response_by_response_body( HTTPStatus.OK, response_error_page, is_response_json=False, content_type=CommonConst.CONTENT_TYPE_TEXT_HTML) user_name = email_change_apply_info['UserID'] if common_utils.check_key('CallerServiceName', email_change_apply_info): caller_service_name = email_change_apply_info['CallerServiceName'] # data response page response_error_page = config[CommonConst.KEY_RESPONSE_ERROR_PAGE.format( serviceName=caller_service_name)] response_execute_change_email = config[ CommonConst.KEY_RESPONSE_EXECUTE_CHANGE_EMAIL.format( serviceName=caller_service_name)] # メールアドレス変更申請テーブルから取得した UserID でCognito に合致する該当するユーザー情報を取得します。 try: user_info = aws_common.get_cognito_user_info_by_user_name( apply_id, user_name) except PmError: pm_logger.error("Cognitoから情報取得に失敗しました。") return common_utils.get_response_by_response_body( HTTPStatus.OK, response_error_page, is_response_json=False, content_type=CommonConst.CONTENT_TYPE_TEXT_HTML) if not user_info: pm_logger.warning("Cognitoにユーザーが存在しませんでした。ユーザーID: %s", user_name) return common_utils.get_response_by_response_body( HTTPStatus.OK, response_error_page, is_response_json=False, content_type=CommonConst.CONTENT_TYPE_TEXT_HTML) before_mail_address = email_change_apply_info['BeforeMailAddress'] after_mail_address = email_change_apply_info['AfterMailAddress'] # get cognito email cognito_email = jmespath.search("[?Name=='email'].Value | [0]", user_info['UserAttributes']) if before_mail_address != cognito_email: pm_logger.warning("変更前メールアドレスがCognitoのメールアドレスと合致しませんでした。") return common_utils.get_response_by_response_body( HTTPStatus.OK, response_error_page, is_response_json=False, content_type=CommonConst.CONTENT_TYPE_TEXT_HTML) # Cognitoのメールアドレスを変更する try: user_attributes = [{ 'Name': 'email', 'Value': after_mail_address }, { 'Name': 'email_verified', 'Value': 'true' }] aws_common.update_cognito_user_attributes(apply_id, user_name, user_attributes) except PmError: pm_logger.error("Cognitoの項目変更に失敗しました。") return common_utils.get_response_by_response_body( HTTPStatus.OK, response_error_page, is_response_json=False, content_type=CommonConst.CONTENT_TYPE_TEXT_HTML) # get list affiliations try: affiliations = pm_affiliation.query_userid_key(apply_id, user_name) except PmError: pm_logger.error("ユーザー所属テーブルでレコード取得に失敗しました。") return common_utils.get_response_by_response_body( HTTPStatus.OK, response_error_page, is_response_json=False, content_type=CommonConst.CONTENT_TYPE_TEXT_HTML) for affiliation in affiliations: try: org_notify_mail_destinations = pm_orgNotifyMailDestinations.query_key( apply_id, affiliation['OrganizationID'], CommonConst.NOTIFY_CODE, None) except PmError: pm_logger.error("組織別通知メール宛先テーブルでレコード取得に失敗しました。") return common_utils.get_response_by_response_body( HTTPStatus.OK, response_error_page, is_response_json=False, content_type=CommonConst.CONTENT_TYPE_TEXT_HTML) if org_notify_mail_destinations: destinations_update = [] for destination in org_notify_mail_destinations['Destinations']: if destination['MailAddress'] == before_mail_address: destination['MailAddress'] = after_mail_address destinations_update.append(destination) # update pm_orgNotifyMailDestinations try: attribute = {'Destinations': {"Value": destinations_update}} pm_orgNotifyMailDestinations.update( apply_id, org_notify_mail_destinations['OrganizationID'], org_notify_mail_destinations['NotifyCode'], attribute) except PmError: pm_logger.error("組織別通知メール宛先テーブルでレコード更新に失敗しました。") return common_utils.get_response_by_response_body( HTTPStatus.OK, response_error_page, is_response_json=False, content_type=CommonConst.CONTENT_TYPE_TEXT_HTML) # update pm_affiliation try: attribute = {'MailAddress': {"Value": after_mail_address}} pm_affiliation.update_affiliation(apply_id, affiliation['UserID'], affiliation['OrganizationID'], attribute, affiliation['UpdatedAt']) except PmError: pm_logger.error("ユーザー所属テーブルでレコード更新に失敗しました。") return common_utils.get_response_by_response_body( HTTPStatus.OK, response_error_page, is_response_json=False, content_type=CommonConst.CONTENT_TYPE_TEXT_HTML) # メールアドレス変更申請テーブルで変更申請ID{applyid}をキーにして申請レコードを削除する。 try: pm_emailChangeApply.delete(apply_id, apply_id) except PmError: pm_logger.error("メールアドレス変更申請テーブルでレコード削除に失敗しました。変更申請ID: %s", apply_id) return common_utils.get_response_by_response_body( HTTPStatus.OK, response_error_page, is_response_json=False, content_type=CommonConst.CONTENT_TYPE_TEXT_HTML) # data response response = common_utils.get_response_by_response_body( HTTPStatus.OK, response_execute_change_email, is_response_json=False, content_type=CommonConst.CONTENT_TYPE_TEXT_HTML) return common_utils.response(response, pm_logger)
def apply_change_email(user_id, mail_before_change, data_body): pm_logger = common_utils.begin_logger(user_id, __name__, inspect.currentframe()) # Parse JSON try: body_object = json.loads(data_body) except Exception as e: return common_utils.error_exception(MsgConst.ERR_REQUEST_202, HTTPStatus.BAD_REQUEST, e, pm_logger, True) mail_after_change = common_utils.get_value("mailAddress", body_object, None) caller_service_name = common_utils.get_value("callerServiceName", body_object, None) mail_lang = common_utils.get_value("mailLang", body_object, None) # validate list_errors = validate_param_apply_change_email(user_id, mail_lang, caller_service_name, mail_before_change) if list_errors: return common_utils.error_validate(MsgConst.ERR_REQUEST_201, HTTPStatus.UNPROCESSABLE_ENTITY, list_errors, pm_logger) # Cognito UserPoolsから変更するメールアドレス{mailaddress}に該当するユーザー情報情報を取得します。 try: list_users = aws_common.get_cognito_user_pools(user_id, mail_after_change, "email") except PmError as e: return common_utils.error_exception(MsgConst.ERR_COGNITO_501, HTTPStatus.INTERNAL_SERVER_ERROR, e, pm_logger, True) if list_users: return common_utils.error_common(MsgConst.ERR_302, HTTPStatus.CONFLICT, pm_logger) # メールアドレス変更申請テーブルから申請レコードを取得します。 try: list_email_change_apply = pm_emailChangeApply.query_user_index(user_id) except PmError as e: return common_utils.error_exception(MsgConst.ERR_402, HTTPStatus.INTERNAL_SERVER_ERROR, e, pm_logger, True) if list_email_change_apply: return common_utils.error_common(MsgConst.ERR_302, HTTPStatus.CONFLICT, pm_logger) # メールアドレス変更申請テーブルに申請レコードを作成します。 apply_id = common_utils.get_uuid4() time_to_live = common_utils.get_time_to_live( CommonConst.EMAIL_CHANGE_APPLY_EXPIRATION_DATE) try: pm_emailChangeApply.create(user_id, apply_id, mail_before_change, mail_after_change, time_to_live, caller_service_name) except PmError as e: return common_utils.error_exception(MsgConst.ERR_DB_403, HTTPStatus.INTERNAL_SERVER_ERROR, e, pm_logger, True) # get record PM_EmailChangeApply try: result = pm_emailChangeApply.query_key(user_id, apply_id, convert_response=True) except PmError as e: return common_utils.error_exception(MsgConst.ERR_402, HTTPStatus.INTERNAL_SERVER_ERROR, e, pm_logger, True) # S3から通知メール送信設定ファイルを取得します。 try: config = FileUtils.read_yaml(user_id, CommonConst.S3_SETTING_BUCKET, CommonConst.NOTIFY_CONFIG_CIS_RESULT_MAIL) except PmError as e: pm_logger.error( "メールアドレス変更通知メール送信設定ファイルの取得に失敗しました。:s3://%s/%s", common_utils.get_environ(CommonConst.S3_SETTING_BUCKET), CommonConst.NOTIFY_CONFIG_CIS_RESULT_MAIL) return common_utils.error_exception(MsgConst.ERR_S3_702, HTTPStatus.INTERNAL_SERVER_ERROR, e, pm_logger, True) path_file_template = config[ CommonConst.KEY_GET_PATH_FILE_TEMPLATE_MAIL_SERVICE.format( language=mail_lang, serviceName=caller_service_name)] # 通知メール本文を作成 try: template_body_mail = FileUtils.read_decode( user_id, CommonConst.S3_SETTING_BUCKET, path_file_template) except PmError as e: pm_logger.error( "メールアドレス変更通知メール本文テンプレートファイルの取得に失敗しました。:s3://%s/%s", common_utils.get_environ(CommonConst.S3_SETTING_BUCKET), path_file_template) return common_utils.error_exception(MsgConst.ERR_S3_702, HTTPStatus.INTERNAL_SERVER_ERROR, e, pm_logger, True) # SESで通知メールを送信します。 bcc_addresses = [mail_after_change] template_body_mail = Template(template_body_mail) body_mail = template_body_mail.render(ApplyID=apply_id) mail_subject = config[CommonConst.KEY_MAIL_SUBJECT_SERVICE.format( language=mail_lang, serviceName=caller_service_name)] mail_form = config[CommonConst.KEY_MAIL_FROM_SERVICE.format( serviceName=caller_service_name)] try: aws_common.send_email(user_id, config['ses.region'], mail_form, bcc_addresses, mail_subject, body_mail) except PmError as e: pm_logger.error("通知メール送信に失敗しました。") return common_utils.error_exception(MsgConst.ERR_SES_801, HTTPStatus.INTERNAL_SERVER_ERROR, e, pm_logger, True) # return data response response = common_utils.get_response_by_response_body( HTTPStatus.CREATED, result) return common_utils.response(response, pm_logger)
def execute_send_result_slack(trace_id, check_history_id, language): pm_logger = common_utils.begin_logger(trace_id, __name__, inspect.currentframe()) # チェック結果テーブルのCheckHistoryIndexから、チェック履歴ID{CheckHistoryId}をキーとしてチェック結果一覧を取得します。 try: list_check_result = pm_checkResults.query_history_index( trace_id, check_history_id) except PmError as e: msg_err = "チェック結果の取得に失敗しました。" pm_logger.error(msg_err) common_utils.write_log_pm_error(e, pm_logger, msg_err) return if (not list_check_result): msg_err = "チェック結果の取得に失敗しました。" pm_logger.error(msg_err) return # チェック結果一覧のうちの一つから、組織IDを取得します。 organization_id = list_check_result[0]['OrganizationID'] organization_name = list_check_result[0]['OrganizationName'] project_name = list_check_result[0]['ProjectName'] # 組織別Slack通知設定テーブルから、組織ID、通知コードCHECK_CISをキーとして設定情報を取得します。 try: org_notify_slack = pm_orgNotifySlack.query_key(trace_id, organization_id, CommonConst.NOTIFY_CODE) except PmError as e: msg_err = "Slack設定情報の取得に失敗しました。:OrganizationID={0}, CheckCode={1}".format( organization_id, CommonConst.NOTIFY_CODE) pm_logger.error(msg_err) common_utils.write_log_pm_error(e, pm_logger, msg_err) return if (not org_notify_slack): msg_err = "Slack設定情報の取得に失敗しました。:OrganizationID={0}, CheckCode={1}".format( organization_id, CommonConst.NOTIFY_CODE) pm_logger.error(msg_err) return # S3から通知Slack設定ファイルを取得します。 try: config = FileUtils.read_yaml(trace_id, CommonConst.S3_SETTING_BUCKET, CommonConst.NOTIFY_CONFIG_CIS_RESULT_MAIL) except PmError as e: msg_err = "通知Slack設定ファイルの取得に失敗しました。:s3://{0}/{1}".format( common_utils.get_environ(CommonConst.S3_SETTING_BUCKET), CommonConst.NOTIFY_CONFIG_CIS_RESULT_MAIL) pm_logger.error(msg_err) common_utils.write_log_pm_error(e, pm_logger, msg_err) return if language not in CommonConst.LANGUAGE_SUPPORT: language = CommonConst.LANGUAGE_DEFAULT path_file_template = config[ CommonConst.KEY_GET_PATH_FILE_TEMPLATE_SLACK.format(language=language)] # メッセージ本文を作成します。 try: template_message = FileUtils.read_decode(trace_id, CommonConst.S3_SETTING_BUCKET, path_file_template) except PmError as e: msg_err = "Slack通知本文テンプレートファイルの取得に失敗しました。:s3://{0}/{1}".format( common_utils.get_environ(CommonConst.S3_SETTING_BUCKET), path_file_template) pm_logger.error(msg_err) common_utils.write_log_pm_error(e, pm_logger, msg_err) return executed_date_time = date_utils.toString( date_utils.toDate(list_check_result[0]['ExecutedDateTime'], date_utils.UTC), date_utils.PATTERN_YYYYMMDDHHMM, date_utils.get_time_zone_by_language(language)) check_results = [] for check_result in list_check_result: account_aws = CommonConst.AWS_ACCOUNT_MAIL.format( check_result['AWSAccount'][:4], check_result['AWSAccount'][4:8]) check_result = { "accountAWS": account_aws, "okCount": check_result['OKCount'], "ngCount": check_result['NGCount'], "criticalCount": check_result['CriticalCount'], "managedCount": check_result['ManagedCount'], "errorCount": check_result['ErrorCount'] } check_results.append(check_result) template = Template(template_message) context = { 'organizationName': organization_name, 'projectName': project_name, 'executedDateTime': executed_date_time, 'checkResults': check_results } body = template.render(context) mentions = slack_common.convert_command_slack(org_notify_slack['Mentions']) body = CommonConst.KEY_BODY_MESSAGE_SLACK.format(mentions=mentions, body=body) notify_slack = { "username": config[CommonConst.KEY_USER_NAME_SLACK.format(language=language)], "icon_url": config['slack.iconurl'], "text": body } notify_slack = json.dumps(notify_slack) try: slack_common.send_message_slack(trace_id, org_notify_slack['WebhookURL'], notify_slack) except PmError as e: msg_err = "Slack通知のリクエスト送信に失敗しました。:OrganizationID={0}, CheckCode={1}".format( organization_id, CommonConst.NOTIFY_CODE) pm_logger.warning(msg_err) common_utils.write_log_pm_error(e, pm_logger, msg_err)
def send_result_email(trace_id, check_history_id, language): pm_logger = common_utils.begin_logger(trace_id, __name__, inspect.currentframe()) # チェック結果テーブルのCheckHistoryIndexから、チェック履歴ID{CheckHistoryId}をキーとしてチェック結果一覧を取得します。 try: list_check_result = pm_checkResults.query_history_index( trace_id, check_history_id) except PmError as e: msg_err = "チェック結果の取得に失敗しました。" pm_logger.error(msg_err) raise common_utils.write_log_pm_error(e, pm_logger, msg_err) if (not list_check_result): msg_err = "チェック結果の取得に失敗しました。" pm_logger.error(msg_err) raise PmError(message=msg_err) # チェック結果一覧のうちの一つから、組織IDを取得します。 organization_id = list_check_result[0]['OrganizationID'] organization_name = list_check_result[0]['OrganizationName'] project_name = list_check_result[0]['ProjectName'] # 宛先ユーザーの確認をします。ユーザー所属テーブルのOrganizationIndexから、組織IDをキー try: users = pm_affiliation.query_users_organization_index( trace_id, organization_id, InvitationStatus.Belong) except PmError as e: msg_err = "ユーザー所属情報の取得に失敗しました。:OrganizationID={0}".format( organization_id) pm_logger.error(msg_err) raise common_utils.write_log_pm_error(e, pm_logger, msg_err) # 組織別通知メール宛先テーブルから、組織ID、通知コードCHECK_CISをキーとして宛先情報を取得します。 try: org_notify_mail_destinations = pm_orgNotifyMailDestinations.query_key( trace_id, organization_id, CommonConst.NOTIFY_CODE) except PmError as e: msg_err = "宛先情報の取得に失敗しました。:OrganizationID={0}, CheckCode={1}".format( organization_id, CommonConst.NOTIFY_CODE) pm_logger.error(msg_err) raise common_utils.write_log_pm_error(e, pm_logger, msg_err) if (not org_notify_mail_destinations): msg_err = "宛先情報の取得に失敗しました。:OrganizationID={0}, CheckCode={1}".format( organization_id, CommonConst.NOTIFY_CODE) pm_logger.error(msg_err) raise PmError(message=msg_err) destinations = [] for destination in org_notify_mail_destinations['Destinations']: for user in users: if destination['UserID'] == user['UserID']: destinations.append(destination) break try: if (len(destinations) == 0): pm_orgNotifyMailDestinations.delete(trace_id, organization_id, CommonConst.NOTIFY_CODE) else: attribute = {'Destinations': {"Value": destinations}} pm_orgNotifyMailDestinations.update(trace_id, organization_id, CommonConst.NOTIFY_CODE, attribute) except PmError as e: msg_err = "通知メール宛先ユーザーの更新に失敗しました。:OrganizationID={0}, CheckCode={1}".format( organization_id, CommonConst.NOTIFY_CODE) pm_logger.error(msg_err) raise common_utils.write_log_pm_error(e, pm_logger, msg_err) # S3から通知メール送信設定ファイルを取得します。 try: config = FileUtils.read_yaml(trace_id, CommonConst.S3_SETTING_BUCKET, CommonConst.NOTIFY_CONFIG_CIS_RESULT_MAIL) except PmError as e: msg_err = "通知メール送信設定ファイルの取得に失敗しました。:s3://{0}/{1}".format( common_utils.get_environ(CommonConst.S3_SETTING_BUCKET), CommonConst.NOTIFY_CONFIG_CIS_RESULT_MAIL) pm_logger.error(msg_err) raise common_utils.write_log_pm_error(e, pm_logger, msg_err) language_mail = CommonConst.LANGUAGE_DEFAULT if language in CommonConst.LANGUAGE_SUPPORT: language_mail = language path_file_template = config[CommonConst.KEY_GET_PATH_FILE_TEMPLATE_MAIL. format(language=language_mail)] mail_subject_config = config[CommonConst.KEY_MAIL_SUBJECT.format( language=language_mail)] # 通知メール本文を作成 try: template_body = FileUtils.read_decode(trace_id, CommonConst.S3_SETTING_BUCKET, path_file_template) except PmError as e: msg_err = "知メール本文テンプレートファイルの取得に失敗しました。:s3://{0}/{1}".format( common_utils.get_environ(CommonConst.S3_SETTING_BUCKET), path_file_template) pm_logger.error(msg_err) raise common_utils.write_log_pm_error(e, pm_logger, msg_err) # 通知メール件名を作成 subject_template = Template(mail_subject_config) mail_subject = subject_template.render(project_name=project_name) # メール本文 executed_date_time = date_utils.toString( date_utils.toDate(list_check_result[0]['ExecutedDateTime'], date_utils.UTC), date_utils.PATTERN_YYYYMMDDHHMM, date_utils.ASIA_TOKYO) executed_date_time_utc = date_utils.toString( date_utils.toDate(list_check_result[0]['ExecutedDateTime'], date_utils.UTC), date_utils.PATTERN_YYYYMMDDHHMM, date_utils.UTC) check_results = [] for check_result in list_check_result: account_aws = CommonConst.AWS_ACCOUNT_MAIL.format( check_result['AWSAccount'][:4], check_result['AWSAccount'][4:8]) check_result = { "accountAWS": account_aws, "okCount": check_result['OKCount'], "ngCount": check_result['NGCount'], "criticalCount": check_result['CriticalCount'], "managedCount": check_result['ManagedCount'], "errorCount": check_result['ErrorCount'] } check_results.append(check_result) template = Template(template_body) context = { 'organizationName': organization_name, 'projectName': project_name, 'executedDateTime': executed_date_time, 'executedDateTimeUTC': executed_date_time_utc, 'checkResults': check_results } body = template.render(context) # SESで通知メールを送信します。 list_bcc_addresses = [] bcc_addresses = [] for index, destination in enumerate(destinations): bcc_addresses.append(destination['MailAddress']) if (len(bcc_addresses) >= 50 or len(destinations) == index + 1): list_bcc_addresses.append(bcc_addresses) bcc_addresses = [] for index, bcc_addresses in enumerate(list_bcc_addresses): try: aws_common.send_email(trace_id, config['ses.region'], config['mail.from'], bcc_addresses, mail_subject, body) except PmError as e: msg_err = "通知メール送信に失敗しました。({0}/{1})".format( index + 1, len(list_bcc_addresses)) pm_logger.error(msg_err) raise common_utils.write_log_pm_error(e, pm_logger, msg_err)
def send_checkerror_email(trace_id, aws_account, check_history_id, organization_id, project_id, error_code, execute_user_id, region_name, check_code_item, data_body): pm_logger = common_utils.begin_logger(trace_id, __name__, inspect.currentframe()) # 組織情報を取得する。 try: organization = pm_organizations.get_organization( trace_id, organization_id) except PmError as e: msg_err = "組織情報の取得に失敗しました。OrganizationID={0}".format(organization_id) pm_logger.error(msg_err) raise common_utils.write_log_pm_error(e, pm_logger, msg_err) # プロジェクト情報を取得する。 try: project = pm_projects.query_key(trace_id, project_id) except PmError as e: msg_err = "プロジェクト情報の取得に失敗しました。ProjectID={0}".format(project_id) pm_logger.error(msg_err) raise common_utils.write_log_pm_error(e, pm_logger, msg_err) # チェック実行ユーザーのメールアドレスと言語を取得します。 try: user_info = aws_common.get_cognito_user_info_by_user_name( trace_id, execute_user_id) except PmError as e: msg_err = "Cognitoから情報取得に失敗しました。" pm_logger.error(msg_err) raise common_utils.write_log_pm_error(e, pm_logger, msg_err) language = jmespath.search("[?Name=='locale'].Value | [0]", user_info['UserAttributes']) mail_execute_user = jmespath.search("[?Name=='email'].Value | [0]", user_info['UserAttributes']) # S3から通知メール送信設定ファイルを取得します。 try: config = FileUtils.read_yaml(trace_id, CommonConst.S3_SETTING_BUCKET, CommonConst.NOTIFY_CONFIG_CIS_RESULT_MAIL) except PmError as e: msg_err = "通知メール送信設定ファイルの取得に失敗しました。:s3://{0}/{1}".format( common_utils.get_environ(CommonConst.S3_SETTING_BUCKET), CommonConst.NOTIFY_CONFIG_CIS_RESULT_MAIL) pm_logger.error(msg_err) raise common_utils.write_log_pm_error(e, pm_logger, msg_err) language_mail = CommonConst.LANGUAGE_ENGLISH if language in CommonConst.LANGUAGE_SUPPORT: language_mail = language path_file_template_check_error_notice_mail = config[ CommonConst.KEY_GET_PATH_FILE_TEMPLATE_CHECK_ERROR_NOTICE_MAIL.format( language=language_mail)] path_file_message_notice_error_execute_check_security = config[ CommonConst.KEY_GET_PATH_MESSAGE_NOTICE_ERROR_EXECUTE_CHECK_SECURITY. format(language=language_mail)] mail_subject_config = config[ CommonConst.KEY_CHECK_ERROR_NOTICE_MAIL_SUBJECT.format( language=language_mail)] # エラー通知内容設定ファイルを取得します。 try: messages_notice_error_execute_check_security = FileUtils.read_yaml( trace_id, CommonConst.S3_SETTING_BUCKET, path_file_message_notice_error_execute_check_security) except PmError as e: msg_err = "エラー通知内容設定ファイルの取得に失敗しました。:s3://{0}/{1}".format( common_utils.get_environ(CommonConst.S3_SETTING_BUCKET), path_file_message_notice_error_execute_check_security) pm_logger.error(msg_err) raise common_utils.write_log_pm_error(e, pm_logger, msg_err) # S3から通知メール本文テンプレートファイルを取得します。 try: template_body = FileUtils.read_decode( trace_id, CommonConst.S3_SETTING_BUCKET, path_file_template_check_error_notice_mail) except PmError as e: msg_err = "通知メール本文テンプレートファイルの取得に失敗しました。:s3://{0}/{1}".format( common_utils.get_environ(CommonConst.S3_SETTING_BUCKET), path_file_template_check_error_notice_mail) pm_logger.error(msg_err) raise common_utils.write_log_pm_error(e, pm_logger, msg_err) # に該当する通知内容を取得します。 content_message = messages_notice_error_execute_check_security[ CommonConst.KEY_GET_MESSAGE_NOTICE_ERROR_EXECUTE_CHECK_SECURITY.format( errorCode=error_code)] if data_body is not None: content_message = content_message.format_map(data_body) # 通知メール件名を作成 subject_template = Template(mail_subject_config) project_name = project['ProjectName'] mail_subject = subject_template.render(project_name=project_name) if check_code_item is not None: check_code_item = CommonConst.LIST_CHECK_ITEM_CODE_CONVERT[ check_code_item] template = Template(template_body) context = { 'organizationName': organization['OrganizationName'], 'projectName': project_name, 'checkCodeItem': check_code_item, 'awsAccount': aws_account, 'regionName': region_name, 'contentMessage': content_message } body = template.render(context) # SESで通知メールを送信します bcc_addresses = [mail_execute_user] try: aws_common.send_email(trace_id, config['ses.region'], config['mail.from'], bcc_addresses, mail_subject, body) except PmError as e: msg_err = "通知メール送信に失敗しました。" pm_logger.error(msg_err) raise common_utils.write_log_pm_error(e, pm_logger, msg_err)
def execute_force_invites(trace_id, body_object, organization_id): pm_logger = common_utils.begin_logger(trace_id, __name__, inspect.currentframe()) # parse json try: body_object_json = json.loads(body_object) except Exception as e: return common_utils.error_exception(MsgConst.ERR_REQUEST_202, HTTPStatus.BAD_REQUEST, e, pm_logger, True) caller_service_name = common_utils.get_value("callerServiceName", body_object_json, None) mail_lang = common_utils.get_value("mailLang", body_object_json, None) mail_address = common_utils.get_value("mailAddress", body_object_json, None) authority = common_utils.get_value("authority", body_object_json, None) # validate param execute invite unregistered list_error = validate_param_invite_unregistered_user( trace_id, mail_lang, caller_service_name, mail_address, authority) if list_error: return common_utils.error_validate(MsgConst.ERR_REQUEST_201, HTTPStatus.UNPROCESSABLE_ENTITY, list_error, pm_logger) # get list cognito users try: list_users = aws_common.get_cognito_user_pools(trace_id, mail_address) except PmError as e: return common_utils.error_exception(MsgConst.ERR_COGNITO_501, HTTPStatus.INTERNAL_SERVER_ERROR, e, pm_logger, True) if list_users: return common_utils.error_common(MsgConst.ERR_302, HTTPStatus.CONFLICT, pm_logger) # regist Cognito UserPools temporary_password = '' pattern = re.compile(CommonConst.FORMAT_PASSWORD_TEMPORARY) while pattern.match(temporary_password) is None: temporary_password = common_utils.get_password_temporary( CommonConst.NUMBER_CHARACTERS_PASSWORD_TEMPORARY) user_attributes = [{"Name": "email", "Value": mail_address}] user_name = common_utils.get_uuid4() message_action = MessageAction.Suppress try: aws_common.process_admin_create_user_pools(trace_id, user_name, user_attributes, temporary_password, message_action) except PmError as e: return common_utils.error_exception(MsgConst.ERR_COGNITO_501, HTTPStatus.INTERNAL_SERVER_ERROR, e, pm_logger, True) # enable confirm email try: user_attributes = [{'Name': 'email_verified', 'Value': 'true'}] aws_common.update_cognito_user_attributes(trace_id, user_name, user_attributes) except PmError as e: return common_utils.error_exception(MsgConst.ERR_COGNITO_501, HTTPStatus.INTERNAL_SERVER_ERROR, e, pm_logger, True) # get affiliation try: affiliation = pm_affiliation.get_affiliation(user_name, organization_id) except PmError as e: return common_utils.error_exception(MsgConst.ERR_402, HTTPStatus.INTERNAL_SERVER_ERROR, e, pm_logger, True) if affiliation and affiliation["InvitationStatus"] != InvitationStatus.Deny: return common_utils.error_common(MsgConst.ERR_302, HTTPStatus.CONFLICT, pm_logger) # get organization try: organization = pm_organizations.get_organization( trace_id, organization_id) except PmError as e: return common_utils.error_exception(MsgConst.ERR_402, HTTPStatus.INTERNAL_SERVER_ERROR, e, pm_logger, True) if len(organization) == 0: return common_utils.error_common(MsgConst.ERR_301, HTTPStatus.NOT_FOUND, pm_logger) # create affiliation try: pm_affiliation.create_affiliation(trace_id, mail_address, user_name, organization_id, authority, InvitationStatus.Invited) except PmError as e: return common_utils.error_exception(MsgConst.ERR_DB_403, HTTPStatus.INTERNAL_SERVER_ERROR, e, pm_logger, True) # Get data affiliation try: affiliation_result = pm_affiliation.get_affiliation( user_name, organization_id, True) except PmError as e: return common_utils.error_exception(MsgConst.ERR_402, HTTPStatus.INTERNAL_SERVER_ERROR, e, pm_logger, True) # Get data user_attribute try: user_attribute = pm_userAttribute.query_key(trace_id) except PmError as e: return common_utils.error_exception(MsgConst.ERR_402, HTTPStatus.INTERNAL_SERVER_ERROR, e, pm_logger, True) # S3から通知メール送信設定ファイルを取得します。 try: config = FileUtils.read_yaml(trace_id, CommonConst.S3_SETTING_BUCKET, CommonConst.NOTIFY_CONFIG_CIS_RESULT_MAIL) except PmError as e: pm_logger.error("メール送信設定ファイルの取得に失敗しました。:s3://{0}/{1}".format( common_utils.get_environ(CommonConst.S3_SETTING_BUCKET), CommonConst.NOTIFY_CONFIG_CIS_RESULT_MAIL)) return common_utils.error_exception(MsgConst.ERR_S3_702, HTTPStatus.INTERNAL_SERVER_ERROR, e, pm_logger, True) # メッセージ本文を作成します。 path_file_template = config[ CommonConst.KEY_GET_PATH_FILE_TEMPLATE_USER_INVITE_MAIL.format( language=mail_lang, serviceName=caller_service_name)] try: template_body_mail = FileUtils.read_decode( trace_id, CommonConst.S3_SETTING_BUCKET, path_file_template) except PmError as e: pm_logger.error("招待メール本文テンプレートファイルの取得に失敗しました。:s3://{0}/{1}".format( common_utils.get_environ(CommonConst.S3_SETTING_BUCKET), path_file_template)) return common_utils.error_exception(MsgConst.ERR_S3_702, HTTPStatus.INTERNAL_SERVER_ERROR, e, pm_logger, True) # SESで通知メールを送信します。 bcc_addresses = [mail_address] user_name_sign_in = common_utils.get_value("UserName", user_attribute, None) if not user_name_sign_in: try: affiliation_sign_in = pm_affiliation.get_affiliation( trace_id, organization_id) except PmError as e: return common_utils.error_exception( MsgConst.ERR_402, HTTPStatus.INTERNAL_SERVER_ERROR, e, pm_logger, True) user_name_sign_in = common_utils.get_value("MailAddress", affiliation_sign_in, None) organization_name = common_utils.get_value("OrganizationName", organization, None) time_zone = date_utils.get_time_zone_by_language(mail_lang) time_to_live_date = date_utils.get_current_date() + timedelta(days=6) time_to_live = date_utils.toString(time_to_live_date, date_utils.PATTERN_YYYYMMDD_SLASH, time_zone) template_body_mail = Template(template_body_mail) context = { 'mailAddress': mail_address, 'userName': user_name_sign_in, 'organizationName': organization_name, 'temporaryPassword': temporary_password, 'timeToLive': time_to_live } body_mail = template_body_mail.render(context) mail_subject = config[CommonConst.KEY_MAIL_SUBJECT_USER_INVITE.format( language=mail_lang, serviceName=caller_service_name)] mail_from = config[CommonConst.KEY_INVITE_MAIL_FROM_SERVICE.format( serviceName=caller_service_name)] ses_region = config['ses.region'] try: aws_common.send_email(user_name, ses_region, mail_from, bcc_addresses, mail_subject, body_mail) except PmError as e: pm_logger.error("通知メール送信に失敗しました。") return common_utils.error_exception(MsgConst.ERR_SES_801, HTTPStatus.INTERNAL_SERVER_ERROR, e, pm_logger, True) response = common_utils.get_response_by_response_body( HTTPStatus.CREATED, affiliation_result) # return data response return common_utils.response(response, pm_logger)