运维噩梦:日志泄露!VictoriaLogs 如何用 vmauth 实现安全访问控制,告别裸奔?
[ 知识是人生的灯塔,只有不断学习,才能照亮前行的道路 ]
📢 大家好,我是 WeiyiGeek,一名深耕安全运维开发(SecOpsDev)领域的技术从业者,致力于探索DevOps与安全的融合(DevSecOps),自动化运维工具开发与实践,企业网络安全防护,欢迎各位道友一起学习交流、一起进步 🚀,若此文对你有帮助,一定记得倒点个关注⭐与小红星❤️,收藏学习不迷路 😋 。
0x00 前言简述
安全:授权认证访问
默认情况下,VictoriaLogs 不限制外部客户端访问,以及访问认证等,若主机防火墙、硬件防火墙也未配置对应的IP访问限制策略,这可能有可能导致一系列安全问题,例如,未经授权的访问和数据泄露。
所以作者在自身生产环境中,通常配置 VictoriaLogs 仅在内部网络上运行,并通过硬件防火墙限制外部访问,而且针对且接口读写访问配置适当的认证和授权机制,例如,使用 VictoriaMetrics的 vmauth 插件进行认证,亦或者使用 Nginx 进行反向代理和 auth_basic
配置,此处作者更推荐使用前者。
VictoriaMetrics 提供了一个名为 vmauth 的插件,用于VictoriaMetrics系列产品(VictoriaMetrics、VictoriaLogs等)的认证和授权,vmauth 是一个HTTP代理,它可以跨HTTP组件或任何其他HTTP后端授权、路由和负载平衡请求,此处以官方提供的使用场景及通信图作为示例:
更多使用配置,请参考官方文档:https://docs.victoriametrics.com/victoriametrics/vmauth/
原文链接: https://articles.zsxq.com/id_7vv1aicx4vsz.html
安装 vmauth
描述:可根据需要以及不同环境选择合适的安装方式,支持二进制安装、容器化部署等,下面作者都简单介绍一下:
二进制方式安装
只需从发布页面下载 vmutils-* 档案,解压并将以下标志传递给 vmauth 二进制文件,默认的 vmauth 端口为 8427
可以通过 -httpListenAddr
命令行标志修改监听端口,并根据提供的 -auth.config
参数指定的配置文件来代理它们。
实践环境:
$ cat /etc/openEuler-releaseopenEuler release 24.03 (LTS)
操作示例:
# https://github.com/VictoriaMetrics/VictoriaMetrics/releasesVERSION=v1.120.0TMP_DIR=$(mktemp -d)echo\"正在下载 vmutils 版本: $VERSION\"wget -c https://down.avi.gs/https://github.com/VictoriaMetrics/VictoriaMetrics/releases/download/${VERSION}/vmutils-linux-amd64-${VERSION}.tar.gz -O ${TMP_DIR}/vmutils.tar.gzecho\"正在解压 vmutils 工具包到 /usr/local/bin/\"tar -zxvf ${TMP_DIR}/vmutils.tar.gz -C /usr/local/bin/# vmagent-prod# vmalert-prod# vmalert-tool-prod# vmauth-prod# vmbackup-prod# vmrestore-prod# vmctl-prod# vmbackupmanager-prod# vmgateway-prodecho\"查看 vmauth (社区) 版本信息\"vmauth-prod --versionvmauth-20250620-151704-tags-v1.120.0-0-g07c8a4a9a7
温馨提示: VictoriaMetrics 官方提供的 VM 系列产品分为企业版(enterprise)和社区版(community),下载时不要下载错了,否则运行时会报错。
{\"ts\":\"2025-07-04T11:22:33.712+0800\",\"level\":\"error\",\"caller\":\"VictoriaMetrics/lib/license/copyrights.go:33\",\"msg\":\"VictoriaMetrics Enterprise license is required. Please obtain it at https://victoriametrics.com/products/enterprise/trial/ and pass it via either -license or -licenseFile command-line flags. See https://docs.victoriametrics.com/enterprise/\"}
防火墙配置:
# 防火墙安装(若主机不存在则安装)# yum install firewalld# 启用防火墙firewall-cmd --state # not runningsystemctl enable firewalld --nowfirewall-cmd --state # running# 配置防火墙规则,允许访问 vmauth 端口firewall-cmd --add-port=8427/tcp --permanentfirewall-cmd --reload
配置示例:在 vmauth 中通常使用 Basic Auth
认证方式,当然亦可选择Bearer Token auth
认证方式,配置文件如下:
mkdir -vp/etc/vmlogstee/etc/vmlogs/vmauth.yml<<EOF# vmauth.yml 完整配置示例users:-username:\"write\" password:\"weiyigeek.top\" url_map: -src_paths: -\"/insert/.*\" url_prefix:\"http://10.20.172.214:9428\"-username:\"read\" password:\"weiyigeek.top\" url_map: -src_paths: -\"/select/.*\" url_prefix:\"http://10.20.172.214:9428\"-bearer_token:WEIYIGEEK.TOP url_prefix:\"http://victoria-metrics:8428/\"EOF
启用 vmauth 代理:
# 1.前台启动 vmauth 代理服务(测试)vmauth-prod -httpListenAddr 10.20.172.214:8427 --auth.config=/etc/vmlogs/vmauth.yml -loggerTimezone=Asia/Shanghai --loggerFormat=json# 2.使用 systemd 服务管理 vmauth 代理服务(生产)# 监听IP端口为10.20.172.214:842,指定认证配置文件,指定日志输出格式与时区。cat > /etc/vmlogs/vmauth.conf < /etc/systemd/system/vmauth.service <<EOF[Unit]Description=vmauth is an HTTP proxy,which can authorize route and load balance Documentation=https://docs.victoriametrics.comAfter=network.target[Service]Type=simpleStartLimitBurst=5StartLimitInterval=0Restart=on-failureRestartSec=5EnvironmentFile=-/etc/vmlogs/vmauth.confExecStart=/usr/local/bin/vmauth-prod \\$ARGSExecStop=/bin/kill -s SIGTERM \\$MAINPIDExecReload=/bin/kill -HUP \\$MAINPIDLimitNOFILE=1048576LimitNPROC=1048576LimitCORE=infinityStandardOutput=journalStandardError=journalSyslogIdentifier=vmauthPrivateTmp=yesProtectHome=yesNoNewPrivileges=yesProtectSystem=strictProtectControlGroups=trueProtectKernelModules=trueProtectKernelTunables=yes[Install]WantedBy=multi-user.targetEOF# 重载 systemd 管理器配置,启动 vmauth 服务systemctl daemon-reloadsystemctl start vmauth.servicesystemctl status vmauth.service
在服务启动成功后便可,验证 vmauth 代理访问认证是否生效:
# (1)为了对比,作者先访问未经 vmauth 代理的 VictoriaLogs 服务,查看是否能正常访问,# 发送 json 格式[https://jsonlines.org/]日志数据到 VictoriaLogs 服务echo\'{ \"log\": { \"level\": \"info\", \"message\": \"hello world, VictoriaLogs\" }, \"date\": \"0\", \"stream\": \"stream1\" }{ \"log\": { \"level\": \"error\", \"message\": \"WebSite: www.weiyigeek.top\" }, \"date\": \"0\", \"stream\": \"stream1\" }{ \"log\": { \"level\": \"info\", \"message\": \"hello world\" }, \"date\": \"0\", \"stream\": \"stream2\" }\' | curl -X POST -H \'Content-Type: application/stream+json\' --data-binary @- \\\'http://10.20.172.214:9428/insert/jsonline?_stream_fields=stream&_time_field=date&_msg_field=log.message\'# 使用 curl 查看 VictoriaLogs 存储的日志数据curl \'http://10.20.172.214:9428/select/logsql/query\' \\ -X POST \\ -H \'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0\' \\ -H \'AccountID: 0\' \\ -H \'ProjectID: 0\' \\ -d \'query=log.level:*\'# {\"_time\":\"2025-07-04T03:50:57.032545761Z\",\"_stream_id\":\"0000000000000000356bfe9e3c71128c750d94c15df6b908\",\"_stream\":\"{stream=\\\"stream1\\\"}\",\"_msg\":\"hello world, VictoriaLogs\",\"stream\":\"stream1\",\"log.level\":\"info\"}# {\"_time\":\"2025-07-04T03:50:57.032570587Z\",\"_stream_id\":\"0000000000000000356bfe9e3c71128c750d94c15df6b908\",\"_stream\":\"{stream=\\\"stream1\\\"}\",\"_msg\":\"WebSite: www.weiyigeek.top\",\"stream\":\"stream1\",\"log.level\":\"error\"}# {\"_time\":\"2025-07-04T03:50:57.032574268Z\",\"_stream_id\":\"0000000000000000db1269eafe0b151b8f37d371eeaad405\",\"_stream\":\"{stream=\\\"stream2\\\"}\",\"_msg\":\"hello world\",\"log.level\":\"info\",\"stream\":\"stream2\"}# (2) 尝试使用 vmauth 代理访问 VictoriaLogs 服务,查看是否能正常访问,同样进行读操作curl \'http://10.20.172.214:8427/select/logsql/query\' \\ -X POST \\ -H \'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0\' \\ -H \'AccountID: 0\' \\ -H \'ProjectID: 0\' \\ -d \'query=log.level:*\'# missing \'Authorization\' request header # 无法访问,由于未设置认证票据,无法通过认证。# 添加认证票据 -u \'read:weiyigeek.top\' ,再次尝试访问 VictoriaLogs 服务,可以正常访问并获取数据curl \'http://10.20.172.214:8427/select/logsql/query\' \\ -X POST \\ -u \'read:weiyigeek.top\' \\ -H \'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0\' \\ -H \'AccountID: 0\' \\ -H \'ProjectID: 0\' \\ -d \'query=log.level:*\'
温馨提示:上述命令中使用 -H \'AccountID: 0\' -H \'ProjectID: 0\'
参数作用的是在多租户环境下,用以区分不同租户的日志数据,缺省的就是 0:0
。
知识扩展:vmauth 支持启用 TLS 加密访问,如有这方面要求的朋友,可参考配置方式如下:
/usr/local/bin/vmauth-prod -tls -tlsKeyFile=/path/to/tls_key_file -tlsCertFile=/path/to/tls_cert_file -httpListenAddr=0.0.0.0:443 --auth.config=/etc/vmlogs/vmauth.yml # 参数介绍-tls # 启用 TLS 加密访问-tlsKeyFile # 配置 TLS 私钥文件路径-tlsCertFile # 配置 TLS 证书文件路径-httpListenAddr # 配置监听地址与端口,例如:0.0.0.0:443 表示在所有网络接口上监听 443 端口。--auth.config # 配置访问策略配置文件路径
容器化方式安装
作者推荐使用 Docker 或者 K8S 集群方式部署 vmauth,因为这种方式更简单、易于维护,这里以 K8S 为例,演示如何通过手搓资源配置清单部署 vmauth:
Docker Hub 镜像地址:https://hub.docker.com/r/victoriametrics/vmauth/tags
# 创建 vmauth 配置文件目录mkdir -vp /storage/app/logging/vmauth/conf# 创建名称空间kubectl create ns logging# 创建 vmauth 资源(Deployment、Services)配置清单文件apiVersion: v1kind: Servicemetadata: name: vmauth namespace: loggingspec:type: NodePort selector: app: vmauth ports: - protocol: TCP port: 8427 targetPort: http nodePort: 30087---apiVersion: apps/v1kind: Deploymentmetadata: name: vmauth namespace: logging labels: app: vmauthspec: replicas: 1 selector: matchLabels: app: vmauth template: metadata: labels: app: vmauth spec: containers: - name: vmauth image: victoriametrics/vmauth:v1.118.0 args: - -auth.config=/etc/vmauth/vmauth.yml - -httpListenAddr=:8427 ports: - containerPort: 8427 name: http volumeMounts: - name: config mountPath: /etc/vmauth readOnly: true resources: requests: memory: \"128Mi\" cpu: \"100m\" limits: memory: \"4000Mi\" readinessProbe: httpGet: path: /health port: http initialDelaySeconds: 5 periodSeconds: 10 livenessProbe: httpGet: path: /health port: http initialDelaySeconds: 10 periodSeconds: 30 volumes: - name: config hostPath: path: /storage/app/logging/vmauth/conf type: Directory# vmauth.yml 文件配置示例# 例如:针对 指定路由目录 设置访问策略,外部写操作需要通过 vmauth 代理访问,只允许访问 VictoriaLogs 的上传接口tee /storage/app/logging/vmauth/conf/vmauth.yml <<EOFusers: - username: \"logs\" password: \"weiyigeek.top\" #url_prefix: \"http://victorialogs:9428\" url_map: - src_paths: - \"/insert/.*\" url_prefix: \"http://victorialogs:9428\"EOF
部署完成后,查看服务是处于正常运行状态,然后就可以通过 NodePort 方式访问 vmauth 服务了。例如:http://10.20.172.214:30087/insert/
➤ kubectl get pod -n logging vmauth-568f77fddf-vt7x4NAME READY STATUS RESTARTS AGEvmauth-568f77fddf-vt7x4 1/1 Running 0 54d➤ kubectl get svc -n logging vmauthNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEvmauth NodePort 10.96.154.74 8427:30087/TCP 54d➤ kubectl logs -f -n logging vmauth-568f77fddf-vt7x4 2025-05-25T14:42:38.538Z info VictoriaMetrics/app/vmauth/auth_config.go:691 loaded information about 1 users from -auth.config=\"/etc/vmauth/vmauth.yml\"2025-05-25T14:42:38.538Z info VictoriaMetrics/app/vmauth/main.go:117 started vmauth in 0.002 seconds2025-05-25T14:42:38.539Z info VictoriaMetrics/lib/httpserver/httpserver.go:144 started server at http://0.0.0.0:8427/
对于 VictoriaLogs 服务有那些插入 API 接口,以及测试上传用例前面的文章中已经讲解过了,所以这里不再赘述,搭建可访问【VictoriaLogs | 关键概念及日志数据模拟上传查询】文章地址:https://articles.zsxq.com/id_17n3e5xwamd9.html
温馨提示:由于 Grafana 也是部署在K8S集群中,因此可以直接使用内部服务名称(例如 victorialogs)来访问 VictoriaLogs 服务(读取存储的日志),而外部写操作,为了安全起见,则通过 vmauth 代理来完成,以限制外部客户端上传日志数据时需提供认证票据,并且只能访问到 VictoriaLogs 上传的 HTTP API 接口。
使用 vmauth
现在我们已经成功部署了 vmauth,接下来就可以通过它来代理访问 VictoriaLogs 服务了。
例如:作者现在使用 Promtail 采集器抓取 Nginx 日志,并通过 vmauth 代理上传到 VictoriaLogs 服务中进行存储,作者抽取部分配置如下:
由于作者实践环境是在 Kubernetes ,自然而然在 K8S 集群中部署 Promtail ,并在 Promtail 中使用 GeoIP2 库获取IP地址所属省份城市名称和经纬度坐标,可参考作者前面发布文章,当然也可加入作者知识星球【全栈工程师修炼指南
】获取到全套配置文件。
# promtail.yaml 配置文件片段server:disable: true http_listen_address: 127.0.0.1 http_listen_port: 9080 grpc_listen_port: 0positions: filename: /etc/promtail/positions.yaml sync_period: 30s# 关键点:使用 vmauth 代理访问 VictoriaLogs 日志上传服务接口,并且指定了上传日志数据的字段(例如:job,app,ip,filename)clients:- url: \"http://10.10.10.5:30087/insert/loki/api/v1/push?_stream_fields=job,app,ip,filename\"# 配置 vmauth 认证信息,用以访问 vmauth 代理服务接口时提供认证票据信息。 basic_auth: username: \"logs\" password: \"weiyigeek.top\" timeout: 15starget_config: sync_period: 10sscrape_configs:- job_name: openresty-ali-logs static_configs: - targets: - localhost labels: job: openresty-web __path__: /logs/ali-logs-openresty-web*/*.log pipeline_stages: - match: selector: \'{job=\"openresty-web\"}\' stages: - json: expressions: timestamp: timestamp ip: ip domain: domain uri: uri method: method status: status ua: ua - geoip: db: \"/etc/promtail/GeoIP2/GeoLite2-City.mmdb\" source: \"ip\" db_type: \"city\" - template: source: msg template: \'{{ .domain }}{{ .uri }} | {{ .method }} {{ .status }} | {{.ip }} | {{ .ua | trunc 50 }}\' - template: source: stream template: \'{app=\"openresty\",filename=\"{{ .filename }}\",domain=\"{{ .domain }}\"}\' - labeldrop: - geoip_continent_name - geoip_postal_code - geoip_timezone - geoip_subdivision_code - structured_metadata: _msg: msg _stream: stream
上传日志数据成功后,在 Victorialogs VMUI 中查询如下所示:
_time 2025-07-17T10:14:59.151728125Z_stream_id 00000000000000000d2e91967d7f83147cf8d21448eac712_stream {app=\"nginx\",filename=\"/logs/ali-logs-gateway-nginx-2-pvc-ef01643c-8479-4de4-ba20-4fa3f4f90d5c/www-2025-07-17.log\",ip=\"222.179.120.9\"}_msg www.weiyigeek.top/userfiles/fileupload/202504/123456789.pdf | GET 200 | 221.178.120.9| Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebdomain www.weiyigeek.topfilename /logs/ali-logs-gateway-nginx-2-pvc-ef01643c-8479-4de4-ba20-4fa3f4f90d5c/www-2025-07-17.loggeoip_continent_code ASgeoip_country_name Chinageoip_location_latitude 34.7732geoip_location_longitude 113.722ip 221.178.120.9job openresty-webmethod GETprotocol HTTP/1.1scheme httptime 0.000ua Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0client 172.19.13.115referer https://www.weiyigeek.top/size 85497status 200timestamp 2025-07-17T18:14:56+08:00uri /userfiles/fileupload/202504/123456789.pdf
最后,作者也在 Grafana 中安装配置了 VictoriaLogs 数据源,并将采集的 Nginx 中间件应用日志利用 Grafana 面板进行可视化展示,看看效果吧,如下图所示:
温馨提示:需要 Grafana 针对 Victorialogs Nginx 日志可视化面板配置,可加入作者知识星球【全栈工程师修炼指南】在网盘中获取,或者访问【可观测性监控实践】专栏在后续持续发布实践,通过 Nginx + Promtai + VictoriaLogs +Grafana
实现可视化展示全流程实践, 敬请期待。
关注我,获取更多运维安全干货! 🚀
END
加入:作者【全栈工程师修炼指南】知识星球
『 全栈工程师修炼指南』星球,主要涉及全栈工程师(Full Stack Development)实践文章,包括但不限于企业SecDevOps和网络安全等保合规、安全渗透测试、编程开发、云原生(Cloud Native)、物联网工业控制(IOT)、人工智能Ai,从业书籍笔记,人生职场认识等方面资料或文章。
Q: 加入作者【全栈工程师修炼指南】星球后有啥好处?
✅ 将获得作者最新工作学习实践文章以及网盘资源。
✅ 将获得作者珍藏多年的全栈学习笔记(需连续三年及以上老星球友,也可单次购买)。
✅ 将获得作者专门答疑学习交流群,解决在工作学习中的问题。
✅ 将获得作者远程支持(在作者能力范围内且合规)。
目前新人仅需 69 元即可加入作者星球,数量有限,期待你的加入!
获取:作者工作学习全栈笔记
作者整理了10年的工作学习笔记(涉及网络、安全、运维、开发),需要学习实践笔记的看友,可添加作者微信或者回复【工作学习实践笔记】,当前价格¥199,除了获得从业笔记的同时还可进行问题答疑以及每月远程技术支持,希望大家多多支持,收获定大于付出!
知识推荐 往期文章
-
🔥【最新】从零到云:Grafana 12.x 全新可观测性监控平台初识到云原生环境下部署实践
-
🔥【最新】等保合规 | Rsyslog 高阶玩法:审计日志平移直通 VictoriaLogs 实践指南
-
🔥【最新】比Loki更轻量、更高效 | VictoriaLogs 关键概念解析与日志数据模拟上传到查询实践
-
💡【相关】比Loki更轻量、更高效 | VictoriaLogs 主流日志摄取工具介绍与配置示例
若文章对你有帮助,请将它转发给更多的看友,若有疑问的小伙伴,可在评论区留言你想法哟 💬!