def eventsrequest(request): """ [メソッド概要] テストリクエスト実行時のリクエストを処理する """ now = datetime.datetime.now(pytz.timezone('UTC')) trace_id = EventsRequestCommon.generate_trace_id() resp_json = {} result = False msg = '' logger.system_log('LOSI13001', trace_id) try: ######################################### # リクエストのチェック ######################################### # メソッドのチェック if not request or request.method == 'GET': msg = 'Invalid request. Must be POST. Not GET.' logger.user_log('LOSM13001', trace_id) raise Exception(msg) # フォーマットのチェック try: json_str = json.loads(request.body.decode('UTF-8')) except json.JSONDecodeError: msg = 'Invalid request format. Must be JSON.' logger.user_log('LOSM13005', trace_id) raise Exception(msg) if json_str[EventsRequestCommon.KEY_REQTYPE] in [1, '1']: # 1:プロダクション環境 # プロダクション環境 json_str['traceid'] = trace_id json_str = json.dumps(json_str) # 初回の設定読込で落ちるのめんどくさいのでここに置いとく _rabbitMQ_conf() # RabbitMQへ送信 mq_lock.acquire() _produce(json_str) mq_lock.release() result = True msg = 'Accept request.' elif json_str[EventsRequestCommon.KEY_REQTYPE] in [2, '2']: # 2:ステージング環境 # ステージング環境 user = User.objects.get(user_id=1) # キーのチェック err_code = EventsRequestCommon.check_events_request_key(json_str) if err_code != EventsRequestCommon.REQUEST_OK: err_keyname = '' if err_code == EventsRequestCommon.REQUEST_ERR_RULETYPE_KEY: err_keyname = EventsRequestCommon.KEY_RULETYPE elif err_code == EventsRequestCommon.REQUEST_ERR_REQTYPE_KEY: err_keyname = EventsRequestCommon.KEY_REQTYPE elif err_code == EventsRequestCommon.REQUEST_ERR_DATETIME_KEY: err_keyname = EventsRequestCommon.KEY_EVENTTIME elif err_code == EventsRequestCommon.REQUEST_ERR_EVINFO_KEY: err_keyname = EventsRequestCommon.KEY_EVENTINFO msg = 'Invalid request.' logger.user_log('LOSM13002', err_keyname, trace_id) raise Exception(msg) # ルール情報の取得 reqtypeid = json_str[EventsRequestCommon.KEY_REQTYPE] ruletablename = json_str[EventsRequestCommon.KEY_RULETYPE] ruletypeid = RuleType.objects.get( rule_type_name=ruletablename).rule_type_id evinfo_length = DataObject.objects.filter( rule_type_id=ruletypeid).values('label').distinct().count() # イベント情報のチェック err_code = EventsRequestCommon.check_events_request_len( json_str, evinfo_length) if err_code != EventsRequestCommon.REQUEST_OK: if err_code == EventsRequestCommon.REQUEST_ERR_EVINFO_TYPE: msg = 'Unmatch, Number of event information elements.' logger.user_log('LOSM13003', trace_id, ruletypeid, 0, evinfo_length) raise Exception(msg) elif err_code == EventsRequestCommon.REQUEST_ERR_EVINFO_LENGTH: msg = 'Unmatch, Number of event information elements.' logger.user_log( 'LOSM13003', trace_id, ruletypeid, len(json_str[EventsRequestCommon.KEY_EVENTINFO]), evinfo_length) raise Exception(msg) raise Exception() ######################################### # リクエストをDBに保存 ######################################### # DB登録用に成形 evinfo_str = '' rset = DataObject.objects.filter( rule_type_id=ruletypeid).order_by('data_object_id') label_list = [] conditional_expression_id_list = [] for a in rset: if a.label not in label_list: label_list.append(a.label) conditional_expression_id_list.append( a.conditional_expression_id) for rs, v in zip(conditional_expression_id_list, json_str[EventsRequestCommon.KEY_EVENTINFO]): if evinfo_str: evinfo_str += ',' # 条件式がリストの場合 if rs in (13, 14): if not isinstance(v, list): evinfo_str += '%s' % (v) else: temp_val = '[' for i, val in enumerate(v): if i > 0: temp_val += ',' temp_val += '"%s"' % (val) temp_val += ']' evinfo_str += '%s' % (temp_val) # 条件式がリスト以外の場合 else: evinfo_str += '"%s"' % (v) evinfo_str = '{"EVENT_INFO":[%s]}' % (evinfo_str) event_dt = json_str[EventsRequestCommon.KEY_EVENTTIME] event_dt = TimeConversion.get_time_conversion_utc( event_dt, 'Asia/Tokyo') json_data = { 'trace_id': trace_id, 'request_type_id': reqtypeid, 'rule_type_id': ruletypeid, 'request_reception_time': now, 'request_user': '******', 'request_server': 'OASE Web', 'event_to_time': event_dt, 'event_info': evinfo_str, 'status': defs.UNPROCESS, 'status_update_id': '', 'retry_cnt': 0, 'last_update_timestamp': now, 'last_update_user': user.user_name, } # バリデーションチェック oters = EventsRequestSerializer(data=json_data) result_valid = oters.is_valid() # バリデーションエラー if result_valid == False: msg = '%s' % oters.errors logger.user_log('LOSM13004', trace_id, msg) # 正常の場合はDB保存 else: oters.save() result = True msg = 'Accept request.' else: # 不明なリクエスト種別 msg = 'Invalid request type.' logger.user_log('LOSM13023', trace_id, json_str[EventsRequestCommon.KEY_REQTYPE]) except Exception as e: if not msg: msg = 'Unexpected error.' logger.system_log('LOSM13013', trace_id, traceback.format_exc()) # レスポンス情報の作成 resp_json = { 'result': result, 'msg': msg, 'trace_id': trace_id, } resp_json = json.dumps(resp_json, ensure_ascii=False) logger.system_log('LOSI13002', trace_id, result, msg) # 応答 return HttpResponse(resp_json)
def get_logdata(request, response_id, execution_order, act_history_info=None): """ [メソッド概要] ログデータ取得処理 [戻り値] err_flag: エラーフラグ req_dic: リクエスト情報 act_dic: アクション情報 action_history_log_list: アクションログ履歴 """ logger.logic_log('LOSI00001', 'response_id: %s, execution_order: %s' % (response_id, execution_order), request=request) try: err_flag = True req_dic = {} act_dic = {} action_history_log_list = [] # requestからレスポンスIDとアクション実行順判定 if not response_id or not execution_order: # 上記がNoneならエラー logger.system_log('LOSM08002', response_id, execution_order, request=request) return err_flag, req_dic, act_dic, action_history_log_list # アクション情報取得 act_info = RhdmResponseAction.objects.get( response_id=response_id, execution_order=execution_order) if act_history_info is None: act_history_info = ActionHistory.objects.get( response_id=response_id, execution_order=execution_order) # リクエスト情報取得 req_info = EventsRequest.objects.get( trace_id=act_history_info.trace_id) # 日時の整形 act_time_stamp = TimeConversion.get_time_conversion( act_history_info.action_start_time, 'Asia/Tokyo', request=request) req_time_stamp = TimeConversion.get_time_conversion( req_info.event_to_time, 'Asia/Tokyo', request=request) # リクエスト情報取得 req_dic['req_time_stamp'] = req_time_stamp req_dic['req_trace_id'] = req_info.trace_id req_dic['event_info'] = req_info.event_info # アクション情報取得 act_dic['act_time_stamp'] = act_time_stamp act_dic['act_trace_id'] = act_history_info.trace_id act_dic['rule_type_name'] = act_history_info.rule_type_name act_dic['action_type_id'] = act_history_info.action_type_id act_dic['rule_name'] = act_info.rule_name act_dic['action_parameter_info'] = act_info.action_parameter_info # アクションログ取得 action_history_log_list = list( ActionLog.objects.filter( response_id=response_id, execution_order=execution_order).order_by('action_log_id')) err_flag = False logger.logic_log( 'LOSI00002', 'req_dic: %s, act_dic: %s, action_history_log_list: %s' % (req_dic, act_dic, len(action_history_log_list)), request=request) return err_flag, req_dic, act_dic, action_history_log_list except Exception as e: logger.logic_log( 'LOSI00005', 'response_id: %s, execution_order: %s, trace: %s' % (response_id, execution_order, traceback.format_exc()), request=request) return err_flag, req_dic, act_dic, action_history_log_list
def download(request, response_id, execution_order): """ [メソッド概要] アクションログダウンロード処理 """ logger.logic_log('LOSI00001', 'response_id: %s, execution_order: %s' % (response_id, execution_order), request=request) try: err_flag = False log_message = '' log_txt = '' lang = request.user.get_lang_mode() # ルール別アクセス権限チェック act_history_info = ActionHistory.objects.get( response_id=response_id, execution_order=execution_order) user_auth = request.user_config.get_menu_auth_type( MENU_ID, rule_type_id=act_history_info.rule_type_id) if user_auth not in defs.MENU_CATEGORY.ALLOW_EVERY: logger.user_log('LOSI13003', request.path, user_auth, defs.MENU_CATEGORY.ALLOW_EVERY) if 'HTTP_X_REQUESTED_WITH' in request.META and request.META[ 'HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest': return HttpResponse(status=400) else: return HttpResponseRedirect( reverse('web_app:top:notpermitted')) # ログデータ取得 err_flag, req_dic, act_dic, action_history_log_list = get_logdata( request, response_id, execution_order, act_history_info=act_history_info) if err_flag: logger.system_log( 'LOSM08001', 'req_dic: %s, act_dic: %s, action_history_log_list: %s' % (req_dic, act_dic, len(action_history_log_list)), request=request) return HttpResponse(request, status=500) # リクエスト情報整形 log_message += '[' + get_message('MOSJA13001', lang, showMsgId=False) + ']' + '\n' \ + get_message('MOSJA13002', lang, showMsgId=False) + ':' + req_dic['req_time_stamp'] + '\n' \ + get_message('MOSJA13003', lang, showMsgId=False) + ':' + req_dic['req_trace_id'] + '\n' \ + get_message('MOSJA13004', lang, showMsgId=False) + ':' + req_dic['event_info'] + '\n' \ + '\n' # アクション情報整形 log_message += '[' + get_message('MOSJA13005', lang, showMsgId=False) + ']' + '\n' \ + get_message('MOSJA13006', lang, showMsgId=False) + ':' + act_dic['act_time_stamp'] + '\n' \ + get_message('MOSJA13003', lang, showMsgId=False) + ':' + act_dic['act_trace_id'] + '\n' \ + get_message('MOSJA13007', lang, showMsgId=False) + ':' + act_dic['rule_type_name'] + '\n' \ + get_message('MOSJA13008', lang, showMsgId=False) + ':' + act_dic['rule_name'] + '\n' \ + get_message('MOSJA13010', lang, showMsgId=False) + ':' + act_dic['action_parameter_info'] + '\n' \ # 各ドライバーのアクションの詳細を取得 func = _get_get_history_data_func(act_history_info.action_type_id) history_data = func(act_history_info.pk) # ドライバー固有のアクション情報追記 for k, v in history_data.items(): if not v: v = '' log_message += get_message(k, lang, showMsgId=False) + ':' + str(v) + '\n' else: log_message += '\n' # action_history_listをループしながらテキストにいれる。 if len(action_history_log_list) > 0: log_message += '[' + get_message( 'MOSJA13011', lang, showMsgId=False) + ']' + '\n' for action_log in action_history_log_list: msg_params = action_log.message_params if not msg_params: message = get_message(action_log.message_id, request.user.get_lang_mode()) else: msg_params = ast.literal_eval(msg_params) message = get_message(action_log.message_id, request.user.get_lang_mode(), **(msg_params)) time_stamp = TimeConversion.get_time_conversion( action_log.last_update_timestamp, 'Asia/Tokyo', request=request) log_message += '[%s] %s\n' % (time_stamp, message) # ダウンロード rule_name = act_dic['rule_type_name'] act_type = act_dic['action_type_id'] act_list = ActionType.objects.all() num = act_type - 1 act_typ = str(act_list[num]) action_type = act_typ.split('(')[0] act_time = act_dic['act_time_stamp'] action_time = (act_time.translate( str.maketrans({ '-': '', '_': '', ':': '', ' ': '' }))) file_name = 'Action_log_%s_%s_%s.txt' % (rule_name, action_type, action_time) response = HttpResponse(log_message, content_type='text/plain') response['Content-Disposition'] = "attachment; filename*=UTF-8''%s" % ( urllib.parse.quote(file_name)) logger.logic_log('LOSI00002', 'success', request=request) return response except Exception as e: logger.logic_log( 'LOSI00005', 'response_id: %s, execution_order: %s, trace: %s' % (response_id, execution_order, traceback.format_exc()), request=request) return HttpResponse(request, status=500)
def dataobject(request, response_id, execution_order): """ [メソッド概要] アクション履歴画面の一覧表示 """ msg = '' req_shape_dic = {} act_shape_dic = {} log_message = [] logger.logic_log('LOSI00001', 'response_id: %s, execution_order: %s' % (response_id, execution_order), request=request) lang = request.user.get_lang_mode() try: # ルール別アクセス権限チェック act_history_info = ActionHistory.objects.get( response_id=response_id, execution_order=execution_order) user_auth = request.user_config.get_menu_auth_type( MENU_ID, rule_type_id=act_history_info.rule_type_id) if user_auth not in defs.MENU_CATEGORY.ALLOW_EVERY: logger.user_log('LOSI13003', request.path, user_auth, defs.MENU_CATEGORY.ALLOW_EVERY) if 'HTTP_X_REQUESTED_WITH' in request.META and request.META[ 'HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest': return HttpResponse(status=400) else: return HttpResponseRedirect( reverse('web_app:top:notpermitted')) # ログデータ取得 err_flag, req_dic, act_dic, action_history_log_list = get_logdata( request, response_id, execution_order, act_history_info=act_history_info) if err_flag: logger.system_log( 'LOSM08001', 'req_dic: %s, act_dic: %s, action_history_log_list: %s' % (req_dic, act_dic, len(action_history_log_list)), request=request) msg = get_message('MOSJA13000', lang) raise Exception() #[リクエスト情報] req_shape_dic[get_message('MOSJA13002', lang, showMsgId=False)] = req_dic['req_time_stamp'] req_shape_dic[get_message('MOSJA13003', lang, showMsgId=False)] = req_dic['req_trace_id'] req_shape_dic[get_message('MOSJA13004', lang, showMsgId=False)] = req_dic['event_info'] #[アクション情報] act_shape_dic[get_message('MOSJA13006', lang, showMsgId=False)] = act_dic['act_time_stamp'] act_shape_dic[get_message('MOSJA13003', lang, showMsgId=False)] = act_dic['act_trace_id'] act_shape_dic[get_message('MOSJA13007', lang, showMsgId=False)] = act_dic['rule_type_name'] act_shape_dic[get_message('MOSJA13008', lang, showMsgId=False)] = act_dic['rule_name'] act_shape_dic[get_message( 'MOSJA13010', lang, showMsgId=False)] = act_dic['action_parameter_info'] # 各ドライバーのアクションの詳細を取得 func = _get_get_history_data_func(act_history_info.action_type_id) history_data = func(act_history_info.pk) # ドライバー固有のアクション情報 for k, v in history_data.items(): act_shape_dic[get_message(k, lang, showMsgId=False)] = v #[ログ] for log in action_history_log_list: msg_params = log.message_params if not msg_params: message = get_message(log.message_id, lang) else: msg_params = ast.literal_eval(msg_params) message = get_message(log.message_id, lang, **(msg_params)) time_stamp = TimeConversion.get_time_conversion( log.last_update_timestamp, 'Asia/Tokyo', request=request) message = '[' + time_stamp + ']' + message log_message.append(message) data = { 'message': msg, 'mainmenu_list': request.user_config.get_menu_list(), 'action_info_dict': act_shape_dic, 'request_info_dict': req_shape_dic, 'action_log_list': log_message, 'user_name': request.user.user_name, 'lang_mode': request.user.get_lang_mode(), } logger.logic_log('LOSI00002', 'success', request=request) return render(request, 'rule/action_history_data.html', data) except Exception as e: logger.logic_log( 'LOSI00005', 'response_id: %s, execution_order: %s, trace: %s' % (response_id, execution_order, traceback.format_exc()), request=request) if not msg: msg = get_message('MOSJA13000', lang) data = { 'message': msg, 'mainmenu_list': request.user_config.get_menu_list(), 'action_info_dict': '', 'request_info_dict': '', 'action_log_list': '', 'user_name': request.user.user_name, 'lang_mode': request.user.get_lang_mode(), } response_json = json.dumps(data) return HttpResponse(response_json, content_type="application/json")
def data_list(body, user, rule_type_id_list, label_count_list): """ [メソッド概要] DB登録するデータをリストにする。 """ global data_obj_list now = datetime.datetime.now(pytz.timezone('UTC')) evinfo_length = 0 ruletypeid = 0 msg = '' event_dt = '----/--/-- --:--:--' disconnect() try: # フォーマットのチェック try: json_str = json.loads(body.decode('UTF-8')) except json.JSONDecodeError: logger.user_log('LOSM22000') raise Exception() trace_id = json_str[EventsRequestCommon.KEY_TRACEID] logger.system_log('LOSI22000', trace_id) # キーのチェック check_key_error(trace_id, json_str) # ルール情報の取得 reqtypeid = json_str[EventsRequestCommon.KEY_REQTYPE] ruletablename = json_str[EventsRequestCommon.KEY_RULETYPE] if ruletablename not in rule_type_id_list: rule_type_id_list.update({ruletablename: 0}) label_count_list.update({ruletablename: 0}) rset = RuleType.objects.filter( rule_type_name=ruletablename).values('rule_type_id', 'rule_type_name', 'label_count') for rs in rset: rule_type_id_list.update( {rs['rule_type_name']: rs['rule_type_id']}) label_count_list.update( {rs['rule_type_name']: rs['label_count']}) ruletype_info = { RULE_TYPE_KEY: rule_type_id_list, LABEL_CNT_KEY: label_count_list } mem_client.set(ACCEPT_RULE_KEY, ruletype_info) if ruletablename in rule_type_id_list: ruletypeid = rule_type_id_list[ruletablename] evinfo_length = label_count_list[ruletablename] # イベント情報のチェック check_evinfo_error(trace_id, json_str, ruletypeid, evinfo_length) # DB登録用に整形 evinfo_str = make_evinfo_str(json_str) evinfo_str = '{"EVENT_INFO":[%s]}' % (evinfo_str) event_dt = json_str[EventsRequestCommon.KEY_EVENTTIME] event_dt = TimeConversion.get_time_conversion_utc( event_dt, 'Asia/Tokyo') json_data = { 'trace_id': trace_id, 'request_type_id': reqtypeid, 'rule_type_id': ruletypeid, 'request_reception_time': now, 'request_user': '******', 'request_server': 'OASE Web', 'event_to_time': event_dt, 'event_info': evinfo_str, 'status': defs.UNPROCESS, 'status_update_id': '', 'retry_cnt': 0, 'last_update_timestamp': now, 'last_update_user': user.user_name, } # バリデーションチェック oters = EventsRequestSerializer(data=json_data) result_valid = oters.is_valid() # バリデーションエラー if result_valid == False: msg = '%s' % oters.errors logger.user_log('LOSM22003', trace_id, msg) return False # 正常の場合はリスト登録 else: data_object = EventsRequest(trace_id=trace_id, request_type_id=reqtypeid, rule_type_id=ruletypeid, request_reception_time=now, request_user='******', request_server='OASE Web', event_to_time=event_dt, event_info=evinfo_str, status=defs.UNPROCESS, status_update_id='', retry_cnt=0, last_update_timestamp=now, last_update_user=user.user_name) data_obj_list.append(data_object) return True except Exception as e: logger.system_log('LOSM22004', traceback.format_exc()) return False
def process_view(self, request, view_func, view_args, view_kwargs): """ [メソッド概要] view関数呼び出し時処理 [引数] request : HTTPリクエスト情報 view_func : view関数名 view_args : view関数パラメーター(list) view_kwargs : view関数パラメーター(dict) [戻り値] HTTPレスポンス情報 """ ######################################################## # セッションチェック処理 ######################################################## # 認証前のリクエスト時はログイン画面へ遷移 if not request.session: return self.get_redirect(request, 'LOSI00015', view_kwargs) # セッション情報が異常の場合、ログイン画面へ遷移 if '_auth_user_id' not in request.session: return self.get_redirect(request, 'LOSM00017', view_kwargs) if '_auth_user_backend' not in request.session: return self.get_redirect(request, 'LOSM00018', view_kwargs) if 'cookie_age' not in request.session: return self.get_redirect(request, 'LOSM00019', view_kwargs) # 認証時に使用したバックエンドクラスからユーザー情報取得 backend_path = request.session['_auth_user_backend'] backend = load_backend(backend_path) request.user = backend.get_user(request.session['_auth_user_id']) if request.user == None: request.session['msg'] = 'MOSJA10016' return self.get_redirect(request, 'LOSM00020', view_kwargs) # セッション有効期限切れの場合、ログイン画面へ遷移 now = datetime.datetime.now(pytz.timezone('UTC')) if now > TimeConversion.get_time_conversion_utc( request.session['cookie_age'], 'UTC', request): request.session.clear() request.session.flush() return self.get_redirect(request, 'LOSI00014', view_kwargs) # セッション有効期間を取得(デフォルト30分) timeout_val = System.objects.get(config_id='SESSION_TIMEOUT').value if timeout_val is None: timeout_val = 30 else: timeout_val = int(timeout_val) # 設定値にしたがいセッション有効期間を延長 request.session['cookie_age'] = ( now + timedelta(minutes=timeout_val)).strftime('%Y-%m-%d %H:%M:%S') # ユーザー設定のロード uconf = UserConfig(request.user) setattr(request, 'user_config', uconf) # パスワード有効期限チェックフラグがFalseなら何もしない if not request.user.pass_exp_check_flag: return None # パスワードの状態取得 pass_flag = self.chk_password_state(request, now) if pass_flag == 'initial_pass_expired': logger.logic_log('LOSI00016', request=request) request.session['msg'] = 'MOSJA32023' return HttpResponseRedirect(reverse('web_app:top:login')) if pass_flag in ['initial_pass', 'onetime_pass', 'pass_expired']: logger.logic_log('LOSI00017', request=request) return HttpResponseRedirect(reverse('web_app:top:pass_ch')) return None