Feat: Token auth for metrics

This commit is contained in:
DullJZ
2025-12-21 14:37:06 +08:00
parent eecda55eb3
commit 322d41fbb2
6 changed files with 19 additions and 8 deletions

View File

@@ -88,12 +88,12 @@ go build -o s3-balance cmd/s3-balance/main.go
- `database`GORM 支持 sqlite/mysql/postgres并存储对象元数据、分片会话等。
- `buckets`:列出真实与虚拟桶。`virtual: true` 的条目会对外暴露,真实桶为 `virtual: false`。可设置 `path_style``max_size`
- `balancer`:策略 (`round-robin`|`least-space`|`weighted`)、健康检查周期、重试次数与延迟。
- `metrics`:是否启用 Prometheus 指标路径。
- `metrics`:是否启用 Prometheus 指标路径与可选抓取 Token
- `s3api`Access/Secret Key、`proxy_mode`true=服务代理false=重定向)、`auth_required`SigV4 校验)、`virtual_host`Host-style 路由)。
## API & 测试
- 默认监听 `http://localhost:8080`,支持 `GET /health` 健康检查、`GET /metrics` 指标。
- 默认监听 `http://localhost:8080`,支持 `GET /health` 健康检查、`GET /metrics` 指标(配置 `metrics.token` 后需携带 `Authorization: Bearer <token>`
- 可使用 AWS CLI、s3cmd、MinIO Client 或 `python3 test_virtual_bucket_s3.py` 验证兼容性;脚本运行前需修改 endpoint 与凭据。
## 项目结构

View File

@@ -86,12 +86,12 @@ go build -o s3-balance cmd/s3-balance/main.go
- `database`: GORM supports sqlite/mysql/postgres and stores object metadata, multipart sessions, etc.
- `buckets`: Lists real and virtual buckets. Entries with `virtual: true` are exposed externally, while real buckets are marked as `virtual: false`. Supports `path_style` and `max_size` settings.
- `balancer`: Strategy (`round-robin`|`least-space`|`weighted`), health check intervals, retry counts, and delays.
- `metrics`: Whether to enable Prometheus metrics and their path.
- `metrics`: Whether to enable Prometheus metrics, the path, and optional scrape token.
- `s3api`: Access/Secret Key, `proxy_mode` (true=proxy, false=redirect), `auth_required` (SigV4 validation), `virtual_host` (host-style routing).
## API & Testing
- Default listening at `http://localhost:8080`, supports `GET /health` for health checks and `GET /metrics` for metrics.
- Default listening at `http://localhost:8080`, supports `GET /health` for health checks and `GET /metrics` for metrics (if `metrics.token` is set, send `Authorization: Bearer <token>`).
- Compatibility can be verified using AWS CLI, s3cmd, MinIO Client, or `python3 test_virtual_bucket_s3.py`. Modify the endpoint and credentials in the script before running.
## Project Structure
@@ -106,4 +106,4 @@ internal/storage/ # GORM models and services
pkg/presigner/ # Pre-signed URL utilities
config/ # Example configurations and deployment manifests
deploy/ # Docker/Kubernetes/Helm manifests
```
```

View File

@@ -1 +1 @@
v0.2.1
v0.2.2

View File

@@ -137,8 +137,16 @@ func main() {
// 添加指标端点
if cfg.Metrics.Enabled {
router.Path(cfg.Metrics.Path).Handler(promhttp.Handler())
log.Printf("Metrics server enabled at %s", cfg.Metrics.Path)
metricsHandler := promhttp.Handler()
if cfg.Metrics.Token != "" {
metricsHandler = middleware.TokenAuthMiddleware(cfg.Metrics.Token)(metricsHandler)
}
router.Path(cfg.Metrics.Path).Handler(metricsHandler)
log.Printf(
"Metrics server enabled at %s (auth required: %t)",
cfg.Metrics.Path,
cfg.Metrics.Token != "",
)
}
// 注册管理API路由如果启用

View File

@@ -128,6 +128,8 @@ balancer:
metrics:
enabled: true
path: "/metrics"
# Prometheus 抓取时需要携带的 Token可选配置后需使用 Authorization: Bearer <token> 访问)
token: ""
# S3兼容API配置
s3api:

View File

@@ -63,6 +63,7 @@ type BalancerConfig struct {
type MetricsConfig struct {
Enabled bool `yaml:"enabled"`
Path string `yaml:"path"`
Token string `yaml:"token"` // 可选Token保护Prometheus端点
// Port int `yaml:"port"` // 目前未使用,与主服务共享端口
}