【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
}'
相关推荐
jiedaodezhuti16 分钟前
Flink Checkpoint失败问题分析与解决方案
大数据·flink
海豚调度1 小时前
(二)一文读懂数仓设计的核心规范:从层次、类型到生命周期
大数据·数仓·技术规范
在未来等你1 小时前
Elasticsearch面试精讲 Day 15:索引别名与零停机更新
大数据·分布式·elasticsearch·搜索引擎·面试
IT研究室1 小时前
大数据毕业设计选题推荐-基于大数据的国内旅游景点游客数据分析系统-Spark-Hadoop-Bigdata
大数据·hadoop·spark·毕业设计·源码·数据可视化·bigdata
Lx3522 小时前
YARN资源调度优化:最大化集群利用率
大数据·hadoop
智能化咨询2 小时前
【56页PPT】数字化智能工厂总体设计SRMWCSWMSMES&EMS系统建设方案(附下载方式)
大数据·云计算
在未来等你2 小时前
Elasticsearch面试精讲 Day 12:数据建模与字段类型选择
大数据·分布式·elasticsearch·搜索引擎·面试
Leo.yuan2 小时前
不同数据仓库模型有什么不同?企业如何选择适合的数据仓库模型?
大数据·数据库·数据仓库·信息可视化·spark
Britz_Kevin3 小时前
从零开始的云计算生活——第六十天,志在千里,使用Jenkins部署K8S
云计算·jenkins·生活
chat2tomorrow3 小时前
数据采集平台的起源与演进:从ETL到数据复制
大数据·数据库·数据仓库·mysql·低代码·postgresql·etl