def search(self): ''' This functions searches within the audit trail It returns the audit information for the given search pattern method: audit/search arguments: key, value pairs as search patterns. * outform - optional: if set to "csv", than the token list will be given in CSV or: Usually the key=values will be locally AND concatenated. it a parameter or=true is passed, the filters will be OR concatenated. The Flexigrid provides us the following parameters: ('page', u'1'), ('rp', u'25'), ('sortname', u'number'), ('sortorder', u'asc'), ('query', u''), ('qtype', u'serial')] returns: JSON response or csv format ''' param = {} try: param.update(request.params) log.debug("[search] params: %s" % param) checkPolicyPre('audit', 'view', {}) # remove the param outform (and other parameters that should not # be used for search! search_params = {} search_params.update(param) for key in ["outform", 'delimiter']: if key in search_params: del search_params[key] output_format = param.get("outform", 'json') or 'json' delimiter = param.get('delimiter', ',') or ',' audit_iterator = None audit_query = AuditQuery(search_params, audit) if output_format == "csv": filename = "linotp-audit.csv" response.content_type = "application/force-download" response.headers['Content-disposition'] = ( 'attachment; filename=%s' % filename) audit_iterator = CSVAuditIterator(audit_query, delimiter) else: response.content_type = 'application/json' audit_iterator = JSONAuditIterator(audit_query) c.audit['success'] = True Session.commit() return audit_iterator except PolicyException as pe: log.exception("[getotp] gettoken/getotp policy failed: %r" % pe) Session.rollback() return sendError(response, unicode(pe), 1) except Exception as e: log.exception("[search] audit/search failed: %r" % e) Session.rollback() return sendError(response, "audit/search failed", 0) finally: Session.close()
def test_JSONAuditIterator_1(self): """ Verify that the the JSONAuditIterator outputs the expected data given certain input values """ from linotp.lib.audit.iterator import (AuditQuery, JSONAuditIterator) param = {u'user': u'حافظ'} next_1 = { 'info': u'', 'administrator': u'', 'realm': u'se_realm1', 'success': u'1', 'linotp_server': u'oldjoe', 'sig_check': 'OK', 'number': 768L, 'token_type': u'spass', 'action': u'validate/check', 'client': u'192.168.33.44', 'user': u'حافظ', 'clearance_level': 0L, 'action_detail': u'', 'date': '2014-04-25 11:52:54.243084', 'log_level': u'INFO', 'serial': u'LSSP000120D8' } next_2 = { 'info': u'', 'administrator': u'admin', 'realm': u'se_realm1', 'success': u'1', 'linotp_server': u'oldjoe', 'sig_check': 'OK', 'number': 764L, 'token_type': u'', 'action': u'admin/init', 'client': u'192.168.33.44', 'user': u'حافظ', 'clearance_level': 0L, 'action_detail': u'tokennum = 10', 'date': '2014-04-25 11:52:24.937293', 'log_level': u'INFO', 'serial': u'' } audit = MagicMock(spec=["searchQuery", "getTotal"]) audit.searchQuery.return_value = iter([next_1, next_2]) audit.getTotal.return_value = 2 audit_query = AuditQuery(param, audit) json_audit_iterator = JSONAuditIterator(audit_query) result_json = "" for value in json_audit_iterator: result_json += value expected_json = \ u"""{ "page": 1, "rows": [ { "cell": [ 768, "2014-04-25 11:52:54.243084", "OK", null, "validate/check", "1", "LSSP000120D8", "spass", "حافظ", "se_realm1", "", "", "", "oldjoe", "192.168.33.44", "INFO", 0 ], "id": 768 }, { "cell": [ 764, "2014-04-25 11:52:24.937293", "OK", null, "admin/init", "1", "", "", "حافظ", "se_realm1", "admin", "tokennum = 10", "", "oldjoe", "192.168.33.44", "INFO", 0 ], "id": 764 }], "total": 2 }""" self.assertEqual(json.loads(result_json), json.loads(expected_json))
def search(self): ''' This functions searches within the audit trail It returns the audit information for the given search pattern method: audit/search arguments: key, value pairs as search patterns. * outform - optional: if set to "csv", than the token list will be given in CSV or: Usually the key=values will be locally AND concatenated. it a parameter or=true is passed, the filters will be OR concatenated. The Flexigrid provides us the following parameters: ('page', u'1'), ('rp', u'25'), ('sortname', u'number'), ('sortorder', u'asc'), ('query', u''), ('qtype', u'serial')] returns: JSON response or csv format ''' try: log.debug("[search] params: %s" % self.request_params) checkPolicyPre('audit', 'view', {}) # remove the param outform (and other parameters that should not # be used for search! search_params = self.request_params.copy() for key in ["outform", 'delimiter']: if key in search_params: del search_params[key] output_format = self.request_params.get("outform", 'json') or 'json' streamed_response = None audit_obj = current_app.audit_obj audit_query = AuditQuery(search_params, audit_obj) if output_format == "csv": delimiter = self.request_params.get('delimiter', ',') or ',' streamed_response = Response(stream_with_context( CSVAuditIterator(audit_query, delimiter)), mimetype='text/csv') streamed_response.headers.set('Content-disposition', 'attachment', filename='linotp-audit.csv') else: streamed_response = Response(stream_with_context( JSONAuditIterator(audit_query)), mimetype='application/json') g.audit['success'] = True db.session.commit() return streamed_response except PolicyException as pe: log.exception("[getotp] gettoken/getotp policy failed: %r" % pe) db.session.rollback() return sendError(response, str(pe), 1) except Exception as e: log.exception("[search] audit/search failed: %r" % e) db.session.rollback() return sendError(response, "audit/search failed", 0)
def search(self): """ This functions searches within the audit trail It returns the audit information for the given search pattern method: audit/search arguments: key, value pairs as search patterns. * outform - optional: if set to "csv", than the token list will be given in CSV or: Usually the key=values will be locally AND concatenated. it a parameter or=true is passed, the filters will be OR concatenated. The Flexigrid provides us the following parameters: ('page', u'1'), ('rp', u'25'), ('sortname', u'number'), ('sortorder', u'asc'), ('query', u''), ('qtype', u'serial')] returns: JSON response or csv format """ try: log.debug("[search] params: %r", self.request_params) checkPolicyPre("audit", "view", {}) # remove the param outform (and other parameters that should not # be used for search! search_params = self.request_params.copy() for key in ["outform", "delimiter"]: if key in search_params: del search_params[key] output_format = ( self.request_params.get("outform", "json") or "json" ) delimiter = self.request_params.get("delimiter", ",") or "," audit_obj = current_app.audit_obj audit_query = AuditQuery(search_params, audit_obj) # ------------------------------------------------------------- -- # check if we are running with sqlite which does not support # streaming responses stream_output = True db_uri = current_app.config["SQLALCHEMY_BINDS"]["auditdb"] if db_uri.startswith("sqlite"): stream_output = False if output_format == "csv": audit_iterator = CSVAuditIterator(audit_query, delimiter) mimetype = "text/csv" reponse_headers_args = { "_key": "Content-disposition", "_value": "attachment", "filename": "linotp-audit.csv", } else: audit_iterator = JSONAuditIterator(audit_query) mimetype = "application/json" reponse_headers_args = {} if stream_output: audit_output = stream_with_context(audit_iterator) else: audit_output = "" try: while True: audit_output = audit_output + next(audit_iterator) except StopIteration: # continue if all data is joined pass streamed_response = Response(audit_output, mimetype=mimetype) if reponse_headers_args: streamed_response.headers.set(**reponse_headers_args) g.audit["success"] = True db.session.commit() return streamed_response except PolicyException as pe: log.error("[getotp] gettoken/getotp policy failedi: %r", pe) db.session.rollback() return sendError(response, pe, 1) except Exception as exx: log.error("[search] audit/search failed: %r", exx) db.session.rollback() return sendError(response, "audit/search failed", 0)
def test_JSONAuditIterator_1(self): """ Verify that the the JSONAuditIterator outputs the expected data given certain input values """ from linotp.lib.audit.iterator import AuditQuery, JSONAuditIterator param = {"user": "******"} next_1 = { "info": "", "administrator": "", "realm": "se_realm1", "success": "1", "linotp_server": "oldjoe", "sig_check": "OK", "number": 768, "token_type": "spass", "action": "validate/check", "client": "192.168.33.44", "user": "******", "clearance_level": 0, "action_detail": "", "date": "2014-04-25 11:52:54.243084", "log_level": "INFO", "serial": "LSSP000120D8", } next_2 = { "info": "", "administrator": "admin", "realm": "se_realm1", "success": "1", "linotp_server": "oldjoe", "sig_check": "OK", "number": 764, "token_type": "", "action": "admin/init", "client": "192.168.33.44", "user": "******", "clearance_level": 0, "action_detail": "tokennum = 10", "date": "2014-04-25 11:52:24.937293", "log_level": "INFO", "serial": "", } audit = MagicMock(spec=["searchQuery", "getTotal"]) audit.searchQuery.return_value = iter([next_1, next_2]) audit.getTotal.return_value = 2 audit_query = AuditQuery(param, audit) json_audit_iterator = JSONAuditIterator(audit_query) result_json = "" for value in json_audit_iterator: result_json += value expected_json = """{ "page": 1, "rows": [ { "cell": [ 768, "2014-04-25 11:52:54.243084", "OK", null, "validate/check", "1", "LSSP000120D8", "spass", "حافظ", "se_realm1", "", "", "", "oldjoe", "192.168.33.44", "INFO", 0 ], "id": 768 }, { "cell": [ 764, "2014-04-25 11:52:24.937293", "OK", null, "admin/init", "1", "", "", "حافظ", "se_realm1", "admin", "tokennum = 10", "", "oldjoe", "192.168.33.44", "INFO", 0 ], "id": 764 }], "total": 2 }""" assert json.loads(result_json) == json.loads(expected_json)