feat(M2): SSH 密钥管理 — 公钥上传/重命名/吊销、authorized_keys 原子同步与吊销即时失效
- KeyService:crypto/ssh 解析校验(单行/类型/长度/去重指纹,拒 ssh-dss 与 RSA<2048), Create/Rename/Revoke/List,变更后以 DB 状态全量重写 authorized_keys(同步失败回滚) - system 层:SyncAuthorizedKeys 完善 —— sudo 模式经白名单命令(mkdir/chown/chmod/install) 落位并修正属主(sshd StrictModes),direct 模式 root 时同样修正属主;dry-run 计划日志 - API:GET/POST /me/keys、PATCH/DELETE /me/keys/:id(user 会话)、GET /users/:id/keys(admin), 密钥操作带审计;deploy/sudoers.example 补充密钥同步白名单 - 版本 0.3.0-m2;测试:service 单元(校验/生命周期/回滚/权限)、system 直写落盘、 API 全流程集成;容器 E2E 32 项 PASS(真实 useradd/authorized_keys/吊销即时失效/禁用清空/删除回收)
This commit is contained in:
@@ -2,6 +2,8 @@ package system
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@@ -9,6 +11,11 @@ import (
|
||||
"ws_usernode/internal/config"
|
||||
)
|
||||
|
||||
// discardLog 供测试注入的静默日志器。
|
||||
func discardLog() *slog.Logger {
|
||||
return slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
}
|
||||
|
||||
func testCfg() config.SystemConfig {
|
||||
cfg := config.Default()
|
||||
return cfg.System
|
||||
@@ -17,7 +24,7 @@ func testCfg() config.SystemConfig {
|
||||
func TestDryRunDoesNotExecute(t *testing.T) {
|
||||
cfg := testCfg()
|
||||
cfg.DryRun = true
|
||||
m := New(cfg)
|
||||
m := New(cfg, discardLog())
|
||||
ctx := context.Background()
|
||||
|
||||
// dry-run 下创建/删除不应报错(只打印计划)
|
||||
@@ -52,7 +59,7 @@ func TestExistsParsesPasswd(t *testing.T) {
|
||||
passwdPath = p
|
||||
t.Cleanup(func() { passwdPath = old })
|
||||
|
||||
m := New(testCfg())
|
||||
m := New(testCfg(), discardLog())
|
||||
ctx := context.Background()
|
||||
// 已带前缀与未带前缀都会命中同一账号
|
||||
for _, name := range []string{"ext_zhangsan", "zhangsan"} {
|
||||
@@ -85,3 +92,68 @@ func TestPrefixNormalization(t *testing.T) {
|
||||
t.Fatalf("sysName(x) = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncAuthorizedKeysDirect(t *testing.T) {
|
||||
cfg := testCfg()
|
||||
cfg.DryRun = false
|
||||
cfg.Sudo = false
|
||||
cfg.HomeBase = t.TempDir() // 临时家目录基路径,进程用户直写
|
||||
m := New(cfg, discardLog())
|
||||
ctx := context.Background()
|
||||
|
||||
keys := []Key{
|
||||
{Type: "ssh-ed25519", PublicKey: "AAAAC3NzaC1lZDI1NTE5AAAAIB-test"},
|
||||
{Type: "ssh-rsa", PublicKey: "AAAAB3NzaC1yc2EAAAADAQAB-test"},
|
||||
}
|
||||
if err := m.SyncAuthorizedKeys(ctx, "ext_zhangsan", keys); err != nil {
|
||||
t.Fatalf("sync keys: %v", err)
|
||||
}
|
||||
sshDir := filepath.Join(cfg.HomeBase, "ext_zhangsan", ".ssh")
|
||||
dest := filepath.Join(sshDir, "authorized_keys")
|
||||
content, err := os.ReadFile(dest)
|
||||
if err != nil {
|
||||
t.Fatalf("read authorized_keys: %v", err)
|
||||
}
|
||||
want := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIB-test\nssh-rsa AAAAB3NzaC1yc2EAAAADAQAB-test\n"
|
||||
if string(content) != want {
|
||||
t.Fatalf("authorized_keys = %q, want %q", content, want)
|
||||
}
|
||||
// .ssh 目录 0700、文件 0600
|
||||
di, err := os.Stat(sshDir)
|
||||
if err != nil {
|
||||
t.Fatalf("stat .ssh: %v", err)
|
||||
}
|
||||
if di.Mode().Perm() != 0o700 {
|
||||
t.Fatalf(".ssh mode = %v, want 0700", di.Mode().Perm())
|
||||
}
|
||||
fi, err := os.Stat(dest)
|
||||
if err != nil {
|
||||
t.Fatalf("stat authorized_keys: %v", err)
|
||||
}
|
||||
if fi.Mode().Perm() != 0o600 {
|
||||
t.Fatalf("authorized_keys mode = %v, want 0600", fi.Mode().Perm())
|
||||
}
|
||||
|
||||
// 无有效密钥 → 写空文件(SSH 行为一致)
|
||||
if err := m.SyncAuthorizedKeys(ctx, "ext_zhangsan", nil); err != nil {
|
||||
t.Fatalf("sync empty: %v", err)
|
||||
}
|
||||
content, err = os.ReadFile(dest)
|
||||
if err != nil {
|
||||
t.Fatalf("re-read authorized_keys: %v", err)
|
||||
}
|
||||
if len(content) != 0 {
|
||||
t.Fatalf("empty sync left content: %q", content)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncAuthorizedKeysDryRunSudoPlan(t *testing.T) {
|
||||
cfg := testCfg()
|
||||
cfg.DryRun = true
|
||||
cfg.Sudo = true
|
||||
m := New(cfg, discardLog())
|
||||
// sudo 模式的 dry-run 只打印计划(install 落位路径),不执行任何命令
|
||||
if err := m.SyncAuthorizedKeys(context.Background(), "ext_zhangsan", []Key{{Type: "ssh-ed25519", PublicKey: "AAA"}}); err != nil {
|
||||
t.Fatalf("dry-run sudo plan: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user