mirror of
https://github.com/DullJZ/s3-balance.git
synced 2026-09-05 07:46:42 +08:00
Feat: Token auth for metrics
This commit is contained in:
@@ -88,12 +88,12 @@ go build -o s3-balance cmd/s3-balance/main.go
|
|||||||
- `database`:GORM 支持 sqlite/mysql/postgres,并存储对象元数据、分片会话等。
|
- `database`:GORM 支持 sqlite/mysql/postgres,并存储对象元数据、分片会话等。
|
||||||
- `buckets`:列出真实与虚拟桶。`virtual: true` 的条目会对外暴露,真实桶为 `virtual: false`。可设置 `path_style` 与 `max_size`。
|
- `buckets`:列出真实与虚拟桶。`virtual: true` 的条目会对外暴露,真实桶为 `virtual: false`。可设置 `path_style` 与 `max_size`。
|
||||||
- `balancer`:策略 (`round-robin`|`least-space`|`weighted`)、健康检查周期、重试次数与延迟。
|
- `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 路由)。
|
- `s3api`:Access/Secret Key、`proxy_mode`(true=服务代理,false=重定向)、`auth_required`(SigV4 校验)、`virtual_host`(Host-style 路由)。
|
||||||
|
|
||||||
## API & 测试
|
## 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 与凭据。
|
- 可使用 AWS CLI、s3cmd、MinIO Client 或 `python3 test_virtual_bucket_s3.py` 验证兼容性;脚本运行前需修改 endpoint 与凭据。
|
||||||
|
|
||||||
## 项目结构
|
## 项目结构
|
||||||
|
|||||||
+3
-3
@@ -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.
|
- `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.
|
- `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.
|
- `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).
|
- `s3api`: Access/Secret Key, `proxy_mode` (true=proxy, false=redirect), `auth_required` (SigV4 validation), `virtual_host` (host-style routing).
|
||||||
|
|
||||||
## API & Testing
|
## 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.
|
- 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
|
## Project Structure
|
||||||
@@ -106,4 +106,4 @@ internal/storage/ # GORM models and services
|
|||||||
pkg/presigner/ # Pre-signed URL utilities
|
pkg/presigner/ # Pre-signed URL utilities
|
||||||
config/ # Example configurations and deployment manifests
|
config/ # Example configurations and deployment manifests
|
||||||
deploy/ # Docker/Kubernetes/Helm manifests
|
deploy/ # Docker/Kubernetes/Helm manifests
|
||||||
```
|
```
|
||||||
|
|||||||
+10
-2
@@ -137,8 +137,16 @@ func main() {
|
|||||||
|
|
||||||
// 添加指标端点
|
// 添加指标端点
|
||||||
if cfg.Metrics.Enabled {
|
if cfg.Metrics.Enabled {
|
||||||
router.Path(cfg.Metrics.Path).Handler(promhttp.Handler())
|
metricsHandler := promhttp.Handler()
|
||||||
log.Printf("Metrics server enabled at %s", cfg.Metrics.Path)
|
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路由(如果启用)
|
// 注册管理API路由(如果启用)
|
||||||
|
|||||||
@@ -128,6 +128,8 @@ balancer:
|
|||||||
metrics:
|
metrics:
|
||||||
enabled: true
|
enabled: true
|
||||||
path: "/metrics"
|
path: "/metrics"
|
||||||
|
# Prometheus 抓取时需要携带的 Token(可选,配置后需使用 Authorization: Bearer <token> 访问)
|
||||||
|
token: ""
|
||||||
|
|
||||||
# S3兼容API配置
|
# S3兼容API配置
|
||||||
s3api:
|
s3api:
|
||||||
|
|||||||
@@ -63,6 +63,7 @@ type BalancerConfig struct {
|
|||||||
type MetricsConfig struct {
|
type MetricsConfig struct {
|
||||||
Enabled bool `yaml:"enabled"`
|
Enabled bool `yaml:"enabled"`
|
||||||
Path string `yaml:"path"`
|
Path string `yaml:"path"`
|
||||||
|
Token string `yaml:"token"` // 可选Token,保护Prometheus端点
|
||||||
// Port int `yaml:"port"` // 目前未使用,与主服务共享端口
|
// Port int `yaml:"port"` // 目前未使用,与主服务共享端口
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user