Files
pangolin/server/internal/admin/services.go
T
wangjia 6e99e32285 feat(admin): 管理端最小后台(独立监听 + 白名单 + 2FA)tsk_SCMtcGF4F434
实现「一个二进制三个监听」中的管理端:

- 独立监听 ADMIN_LISTEN(默认 127.0.0.1:9443,FromEnv 拒绝 0.0.0.0/:: 等公网绑定)
- 中间件链:mw_ipallow(CIDR 白名单,仅信任 RemoteAddr,默认内网段)→
  mw_session(HttpOnly+Secure+SameSite=Strict cookie,Redis 30min 滑动 TTL)
- 登录双因素:argon2id 密码 + RFC6238 TOTP;失败限流与临时锁定;
  TOTP 密钥 AES-GCM 加密入库;migration 000008 增补 admins 表;
  首个管理员由 cmd/adminctl create 创建(终端输出 otpauth URI/Secret)
- 功能三块(html/template + embed 静态资源,原生 JS 二次确认):
  1. 码批次:表单生成→明文仅在本次 CSV 下载出现(不落盘/不入日志);
     批次列表 + 整批作废(复用 #3 codes,新增 Store.ListBatches/VoidBatch)
  2. 节点操作:列表 + 近期 node_events;replace 经 #14 ProvisionService、
     draining/up 经 #5 Lifecycle(二者未就绪→注入 stub,UI 置灰);二次确认 + CSRF
  3. audit_log 查看:actor/action/target/时间范围过滤分页;同页查 node_events
- 所有写操作写 audit_log(actor=用户名,meta 不含明文);管理端仅记安全事件
  (登录失败/锁定/白名单拦截),不记常规访问日志
- internal/totp 复用包(与 doc/05 用户中心 2FA 同算法,纯标准库)

测试:totp RFC6238 向量、argon2/AES 往返、白名单放行/拦截、会话滑动过期、
登录成功/密码错/TOTP 错/未知用户/锁定、批次 CSV 含明文且 audit 不泄露、
作废/节点操作的二次确认与 CSRF 缺失被拒、mock 断言 service 调用参数、审计过滤。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-13 14:49:32 +08:00

168 lines
5.4 KiB
Go

package admin
import (
"context"
"errors"
"time"
"github.com/wangjia/pangolin/server/internal/codes"
)
// ErrServiceUnavailable is returned by stub services whose real implementation
// (#5 lifecycle, #14 provisioning) is not yet wired in. The UI greys out the
// corresponding controls when Ready() is false.
var ErrServiceUnavailable = errors.New("admin: service not available yet")
// --------------------------------------------------------------------------
// Code-batch service (#3 codes — implemented)
// --------------------------------------------------------------------------
// CodeBatchParams are the inputs to a batch generation.
type CodeBatchParams struct {
Plan string
DurationDays int
Count int
Channel string
Note string
CreatedBy string
}
// GeneratedBatch carries the one-and-only plaintext output of a generation.
type GeneratedBatch struct {
BatchID int64
Plan string
DurationDays int
Channel string
Codes []string // plaintext, streamed to CSV once and never stored
GeneratedAt time.Time
}
// CodesService is the subset of the #3 codes service the admin UI consumes.
type CodesService interface {
CreateBatch(ctx context.Context, p CodeBatchParams) (*GeneratedBatch, error)
ListBatches(ctx context.Context, limit, offset int) ([]BatchSummary, int, error)
VoidBatch(ctx context.Context, batchID int64) (int64, error)
}
// CodesAdapter adapts the real codes.Service / codes.Store to CodesService.
type CodesAdapter struct {
svc *codes.Service
store *codes.Store
}
// NewCodesAdapter wires the real codes implementation.
func NewCodesAdapter(svc *codes.Service, store *codes.Store) *CodesAdapter {
return &CodesAdapter{svc: svc, store: store}
}
// CreateBatch generates a batch and returns the plaintext codes.
func (a *CodesAdapter) CreateBatch(ctx context.Context, p CodeBatchParams) (*GeneratedBatch, error) {
res, err := a.svc.CreateBatch(ctx, codes.BatchRequest{
PlanCode: codes.PlanCode(p.Plan),
DurationDays: p.DurationDays,
Count: p.Count,
Channel: codes.BatchChannel(p.Channel),
Note: p.Note,
CreatedBy: p.CreatedBy,
})
if err != nil {
return nil, err
}
return &GeneratedBatch{
BatchID: res.BatchID,
Plan: string(res.PlanCode),
DurationDays: res.DurationDays,
Channel: string(res.Channel),
Codes: res.Codes,
GeneratedAt: time.Now().UTC(),
}, nil
}
// ListBatches returns paginated batch summaries.
func (a *CodesAdapter) ListBatches(ctx context.Context, limit, offset int) ([]BatchSummary, int, error) {
infos, total, err := a.store.ListBatches(ctx, limit, offset)
if err != nil {
return nil, 0, err
}
out := make([]BatchSummary, len(infos))
for i, b := range infos {
out[i] = BatchSummary{
ID: b.ID,
Channel: string(b.Channel),
CreatedBy: b.CreatedBy,
Note: b.Note,
CreatedAt: b.CreatedAt,
Total: b.Total,
Redeemed: b.Redeemed,
Void: b.Void,
Unused: b.Unused,
}
}
return out, total, nil
}
// VoidBatch voids all unused codes in a batch.
func (a *CodesAdapter) VoidBatch(ctx context.Context, batchID int64) (int64, error) {
return a.store.VoidBatch(ctx, batchID)
}
// --------------------------------------------------------------------------
// Node lifecycle service (#5) — not yet implemented
// --------------------------------------------------------------------------
// LifecycleService transitions a node's status (draining / up). Backed by the
// #5 lifecycle module once available.
type LifecycleService interface {
// TransitionStatus moves nodeID to target ("draining" | "up").
TransitionStatus(ctx context.Context, nodeID int64, target, actor string) error
// Ready reports whether the real implementation is wired (UI greys out
// controls when false).
Ready() bool
}
// StubLifecycle is the placeholder used until #5 lands.
type StubLifecycle struct{}
// NewStubLifecycle returns a not-ready lifecycle service.
func NewStubLifecycle() *StubLifecycle { return &StubLifecycle{} }
// TransitionStatus always fails until #5 is wired.
func (StubLifecycle) TransitionStatus(context.Context, int64, string, string) error {
return ErrServiceUnavailable
}
// Ready reports false.
func (StubLifecycle) Ready() bool { return false }
// --------------------------------------------------------------------------
// Node provisioning service (#14) — not yet implemented
// --------------------------------------------------------------------------
// ProvisionService replaces a node by provisioning a fresh one. Backed by the
// #14 ProvisionService once available.
type ProvisionService interface {
// Replace decommissions nodeID and provisions a replacement.
Replace(ctx context.Context, nodeID int64, actor string) error
// Ready reports whether the real implementation is wired.
Ready() bool
}
// StubProvision is the placeholder used until #14 lands.
type StubProvision struct{}
// NewStubProvision returns a not-ready provision service.
func NewStubProvision() *StubProvision { return &StubProvision{} }
// Replace always fails until #14 is wired.
func (StubProvision) Replace(context.Context, int64, string) error { return ErrServiceUnavailable }
// Ready reports false.
func (StubProvision) Ready() bool { return false }
// Services bundles the three downstream services the admin UI depends on.
type Services struct {
Codes CodesService
Lifecycle LifecycleService
Provision ProvisionService
}