6e99e32285
实现「一个二进制三个监听」中的管理端:
- 独立监听 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>
214 lines
5.1 KiB
Go
214 lines
5.1 KiB
Go
package admin
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// --------------------------------------------------------------------------
|
|
// Fake Store
|
|
// --------------------------------------------------------------------------
|
|
|
|
type fakeStore struct {
|
|
admins map[string]*Admin
|
|
nodes []NodeRow
|
|
events map[int64][]NodeEvent
|
|
audits []AuditEntry
|
|
lastLogin map[int64]time.Time
|
|
}
|
|
|
|
func newFakeStore() *fakeStore {
|
|
return &fakeStore{
|
|
admins: map[string]*Admin{},
|
|
events: map[int64][]NodeEvent{},
|
|
lastLogin: map[int64]time.Time{},
|
|
}
|
|
}
|
|
|
|
func (f *fakeStore) GetAdminByUsername(_ context.Context, username string) (*Admin, error) {
|
|
a, ok := f.admins[username]
|
|
if !ok {
|
|
return nil, ErrAdminNotFound
|
|
}
|
|
return a, nil
|
|
}
|
|
|
|
func (f *fakeStore) CreateAdmin(_ context.Context, username, pwHash string, enc []byte) (int64, error) {
|
|
id := int64(len(f.admins) + 1)
|
|
f.admins[username] = &Admin{ID: id, Username: username, PwHash: pwHash, TOTPSecretEnc: enc, Status: "active"}
|
|
return id, nil
|
|
}
|
|
|
|
func (f *fakeStore) UpdateLastLogin(_ context.Context, id int64, at time.Time) error {
|
|
f.lastLogin[id] = at
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeStore) ListNodes(_ context.Context, eventsPerNode int) ([]NodeRow, error) {
|
|
out := make([]NodeRow, len(f.nodes))
|
|
copy(out, f.nodes)
|
|
for i := range out {
|
|
ev := f.events[out[i].ID]
|
|
if eventsPerNode > 0 && len(ev) > eventsPerNode {
|
|
ev = ev[:eventsPerNode]
|
|
}
|
|
out[i].RecentEvents = ev
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (f *fakeStore) GetNode(_ context.Context, id int64) (*NodeRow, error) {
|
|
for i := range f.nodes {
|
|
if f.nodes[i].ID == id {
|
|
n := f.nodes[i]
|
|
return &n, nil
|
|
}
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func (f *fakeStore) WriteAudit(_ context.Context, actor, action, target, metaJSON string) error {
|
|
f.audits = append(f.audits, AuditEntry{
|
|
ID: int64(len(f.audits) + 1), Actor: actor, Action: action,
|
|
Target: target, Meta: metaJSON, At: time.Now().UTC(),
|
|
})
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeStore) QueryAudit(_ context.Context, flt AuditFilter) ([]AuditEntry, int, error) {
|
|
var matched []AuditEntry
|
|
for _, e := range f.audits {
|
|
if flt.Actor != "" && e.Actor != flt.Actor {
|
|
continue
|
|
}
|
|
if flt.Action != "" && e.Action != flt.Action {
|
|
continue
|
|
}
|
|
if flt.Target != "" && !strings.Contains(e.Target, flt.Target) {
|
|
continue
|
|
}
|
|
matched = append(matched, e)
|
|
}
|
|
total := len(matched)
|
|
off := flt.Offset
|
|
if off > len(matched) {
|
|
off = len(matched)
|
|
}
|
|
matched = matched[off:]
|
|
if flt.Limit > 0 && len(matched) > flt.Limit {
|
|
matched = matched[:flt.Limit]
|
|
}
|
|
return matched, total, nil
|
|
}
|
|
|
|
func (f *fakeStore) QueryNodeEvents(_ context.Context, nodeID int64, limit int) ([]NodeEvent, error) {
|
|
ev := f.events[nodeID]
|
|
if limit > 0 && len(ev) > limit {
|
|
ev = ev[:limit]
|
|
}
|
|
return ev, nil
|
|
}
|
|
|
|
// auditFor returns the audit entries whose action matches.
|
|
func (f *fakeStore) auditFor(action string) []AuditEntry {
|
|
var out []AuditEntry
|
|
for _, e := range f.audits {
|
|
if e.Action == action {
|
|
out = append(out, e)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// --------------------------------------------------------------------------
|
|
// Fake CodesService
|
|
// --------------------------------------------------------------------------
|
|
|
|
type fakeCodes struct {
|
|
batches []BatchSummary
|
|
created []CodeBatchParams
|
|
nextCodes []string
|
|
nextID int64
|
|
voided []int64
|
|
voidReturn int64
|
|
}
|
|
|
|
func (c *fakeCodes) CreateBatch(_ context.Context, p CodeBatchParams) (*GeneratedBatch, error) {
|
|
c.created = append(c.created, p)
|
|
c.nextID++
|
|
codes := c.nextCodes
|
|
if codes == nil {
|
|
codes = make([]string, p.Count)
|
|
for i := range codes {
|
|
codes[i] = "PLAINCODE" + itoa(i)
|
|
}
|
|
}
|
|
return &GeneratedBatch{
|
|
BatchID: c.nextID, Plan: p.Plan, DurationDays: p.DurationDays,
|
|
Channel: p.Channel, Codes: codes, GeneratedAt: time.Now().UTC(),
|
|
}, nil
|
|
}
|
|
|
|
func (c *fakeCodes) ListBatches(_ context.Context, limit, offset int) ([]BatchSummary, int, error) {
|
|
return c.batches, len(c.batches), nil
|
|
}
|
|
|
|
func (c *fakeCodes) VoidBatch(_ context.Context, batchID int64) (int64, error) {
|
|
c.voided = append(c.voided, batchID)
|
|
return c.voidReturn, nil
|
|
}
|
|
|
|
// --------------------------------------------------------------------------
|
|
// Recording Lifecycle / Provision services
|
|
// --------------------------------------------------------------------------
|
|
|
|
type lifeCall struct {
|
|
NodeID int64
|
|
Target string
|
|
Actor string
|
|
}
|
|
|
|
type recordingLifecycle struct {
|
|
ready bool
|
|
err error
|
|
calls []lifeCall
|
|
}
|
|
|
|
func (r *recordingLifecycle) TransitionStatus(_ context.Context, nodeID int64, target, actor string) error {
|
|
r.calls = append(r.calls, lifeCall{nodeID, target, actor})
|
|
return r.err
|
|
}
|
|
func (r *recordingLifecycle) Ready() bool { return r.ready }
|
|
|
|
type provCall struct {
|
|
NodeID int64
|
|
Actor string
|
|
}
|
|
|
|
type recordingProvision struct {
|
|
ready bool
|
|
err error
|
|
calls []provCall
|
|
}
|
|
|
|
func (r *recordingProvision) Replace(_ context.Context, nodeID int64, actor string) error {
|
|
r.calls = append(r.calls, provCall{nodeID, actor})
|
|
return r.err
|
|
}
|
|
func (r *recordingProvision) Ready() bool { return r.ready }
|
|
|
|
func itoa(i int) string {
|
|
if i == 0 {
|
|
return "0"
|
|
}
|
|
var b [20]byte
|
|
pos := len(b)
|
|
for i > 0 {
|
|
pos--
|
|
b[pos] = byte('0' + i%10)
|
|
i /= 10
|
|
}
|
|
return string(b[pos:])
|
|
}
|