【elasticsearch】慢查询替代查询审计的尝试

【elasticsearch】慢查询替代查询审计的尝试

使用了es有两年了,突然发现一个,es没有查询审计日志,某个用户查询了某个索引的审计。

找了官方文档和社区的回复都是说使用slow log替代慢查询。

尝试一下。

参考链接1:https://discuss.elastic.co/t/does-elasticsearch-capture-audit-logs-for-query-dsl-eql-and-sql-or-not/339398/8

参考链接2:https://www.elastic.co/guide/en/elasticsearch/reference/7.17/auditing-search-queries.html

前置条件

elasticsearch: 7.17.13

操作系统:linux7.9

es加密配置,只有加密后才可以使用audit log

yml 复制代码
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate 
xpack.security.transport.ssl.keystore.path: certs/elastic-certificates.p12 
xpack.security.transport.ssl.truststore.path: certs/elastic-certificates.p12

普通查询

bash 复制代码
 curl -u elastic:AAA320 localhost:9200/sq-20240506/_search

普通插入

bash 复制代码
curl -u elastic:AAA320 -H 'Content-Type: application/json' -X POST localhost:9200/sq-20240506/_doc/1 -d '
{
"firstname": "Krishna",
"lastname": "kumar"
}'

上述查询和插入没有任何slow log信息。

设置慢查询

这里设置trace的慢查询为0ms,会捕获所有查询。

bash 复制代码
curl -u elastic:AAA320 -H 'Content-Type: application/json'  -XPUT 'http://localhost:9200/_all/_settings?preserve_existing=true' -d '{
  "index.search.slowlog.threshold.fetch.debug" : "500ms",
  "index.search.slowlog.threshold.fetch.info" : "800ms",
  "index.search.slowlog.threshold.fetch.trace" : "0ms",
  "index.search.slowlog.threshold.fetch.warn" : "1s",
  "index.search.slowlog.threshold.query.debug" : "2s",
  "index.search.slowlog.threshold.query.info" : "5s",
  "index.search.slowlog.threshold.query.trace" : "0ms",
  "index.search.slowlog.threshold.query.warn" : "10s"
}'

查看日志

慢查询日志,存放再es的日志文件夹中,elasticsearch_index_search_slowlog.log,这里可以看到TRACE级别的日志,可以看到分query和fetch两种,有节点信息、索引信息、分片以及耗时,但是我还是觉得这里缺少了查询用户,查询来源信息,目前还是无法满足我的实际排查需求。

bash 复制代码
] tailf elasticsearch_index_search_slowlog.log
[2024-05-06T16:33:29,528][TRACE][i.s.s.query              ] [node-1] [sq-20240506][0] took[179.4micros], took_millis[0], total_hits[1 hits], types[], stats[], search_type[QUERY_THEN_FETCH], total_shards[1], source[{}], id[], 
[2024-05-06T16:33:29,529][TRACE][i.s.s.fetch              ] [node-1] [sq-20240506][0] took[3.3ms], took_millis[3], total_hits[1 hits], types[], stats[], search_type[QUERY_THEN_FETCH], total_shards[1], source[{}], id[],

取消慢查询

bash 复制代码
curl -u elastic:AAA320 -H 'Content-Type: application/json' -XPUT 'http://localhost:9200/_all/_settings' -d '{
  "index.search.slowlog.threshold.fetch.debug": null,
  "index.search.slowlog.threshold.fetch.info": null,
  "index.search.slowlog.threshold.fetch.trace": null,
  "index.search.slowlog.threshold.fetch.warn": null,
  "index.search.slowlog.threshold.query.debug": null,
  "index.search.slowlog.threshold.query.info": null,
  "index.search.slowlog.threshold.query.trace": null,
  "index.search.slowlog.threshold.query.warn": null
}'
相关推荐
sunshine88510 分钟前
集成电路(IC)设计与封测行业10大常用ERP系统分析
大数据·数据库·人工智能
Mr清奇16 分钟前
AI辅助开发笔记(1)开发工具和配置
大数据·笔记·intellij idea
国际学术会议-杨老师41 分钟前
2027年人文发展、城市管理与数字化社会国际会议(HUCD 2027)
大数据·人工智能·人文发展
科技每日热闻1 小时前
电气设备综合维保服务商怎么选?从预防性维护到数字化运维,看懂五个关键能力——企业选择维保合作伙伴,不能只看“坏了能不能修”,更要看能否覆盖资产与系统的长期运营
大数据·运维·人工智能
电子制造自留地1 小时前
AI服务器高多层PCB设计:阻抗控制、层间对位与孔金属化工程要点
大数据·网络·人工智能·科技·pcb工艺
星云低代码开发平台1 小时前
ERP 与 CRM 客户信用核对:业务键、时点和权限验收
大数据·数据库·人工智能
xiaogai_gai1 小时前
销售出库单同步实战:旺店通→金蝶云星空的单一策略深度拆解
大数据·前端·数据库
guhunyihan1 小时前
零代码快速搭建平台典型应用场景,恐象 AI 落地能力分析
大数据·人工智能
AscendKing2 小时前
Docker Compose 一键部署 SkyWalking 10.2.0 + Elasticsearch8.11,分布式链路追踪完整实战
elasticsearch·docker·skywalking·链路追踪·微服务监控