停用 SSH 密钥兼容入口

停用历史 SSH 私钥生成、存储、读取和管理入口,强制服务器使用密码认证。
This commit is contained in:
xuans committed 2026-10-02 04:03:23 +08:00
1 parent 95450cb1eb
commit 7778aa5ced
10 files changed
+80 -614

No files matched your search

+3 -10
View File
@@ -65,9 +65,6 @@ func newServerAddCommand() *cobra.Command {
item.CreatedAt = now
item.UpdatedAt = now
item.ID = domain.NewID()
if _, err := service.SSHKeys.ValidateReference(cmd.Context(), item.PrivateKeyRef); err != nil {
return fmt.Errorf("SSH 私钥引用无效:%w", err)
}
if err := service.Servers.CreateServer(cmd.Context(), item); err != nil {
return err
}
@@ -96,9 +93,6 @@ func newServerEditCommand() *cobra.Command {
item.UpdatedBy = ""
item.CreatedAt = existing.CreatedAt
item.UpdatedAt = time.Now().UTC()
if _, err := service.SSHKeys.ValidateReference(cmd.Context(), item.PrivateKeyRef); err != nil {
return fmt.Errorf("SSH 私钥引用无效:%w", err)
}
if err := service.Servers.UpdateServer(cmd.Context(), item); err != nil {
return err
}
@@ -185,7 +179,7 @@ func newDomainServersSetCommand() *cobra.Command {
}
type serverFlags struct {
name, address, username, privateKeyRef, fingerprint string
name, address, username, fingerprint string
certificatePath, privateKeyPath, chainPath, webServer string
validationCommand, reloadCommand []string
port int
@@ -196,7 +190,6 @@ func (f *serverFlags) bind(command *cobra.Command) {
command.Flags().StringVar(&f.address, "address", "", "服务器地址")
command.Flags().IntVar(&f.port, "port", 22, "SSH 端口")
command.Flags().StringVar(&f.username, "username", "", "SSH 用户")
command.Flags().StringVar(&f.privateKeyRef, "private-key-ref", "", "SSH 私钥的敏感信息引用")
command.Flags().StringVar(&f.fingerprint, "host-key-fingerprint", "", "SSH 主机 SHA256 指纹")
command.Flags().StringVar(&f.certificatePath, "certificate-path", deployment.DefaultCertificatePath, "远程证书文件路径模板")
command.Flags().StringVar(&f.privateKeyPath, "private-key-path", deployment.DefaultPrivateKeyPath, "远程私钥文件路径模板")
@@ -204,7 +197,7 @@ func (f *serverFlags) bind(command *cobra.Command) {
command.Flags().StringVar(&f.webServer, "web-server", string(deployment.WebServerNginx), "Web 服务类型:nginx、caddy、apache、custom")
command.Flags().StringSliceVar(&f.validationCommand, "validation-command", nil, "自定义配置校验命令及参数")
command.Flags().StringSliceVar(&f.reloadCommand, "reload-command", nil, "自定义 reload 命令及参数")
for _, name := range []string{"name", "address", "username", "private-key-ref", "host-key-fingerprint"} {
for _, name := range []string{"name", "address", "username", "host-key-fingerprint"} {
_ = command.MarkFlagRequired(name)
}
}
@@ -212,7 +205,7 @@ func (f *serverFlags) bind(command *cobra.Command) {
func (f serverFlags) server() deployment.Server {
return deployment.Server{
Name: f.name, Address: f.address, Port: f.port, Username: f.username,
PrivateKeyRef: f.privateKeyRef, HostKeyFingerprint: f.fingerprint,
AuthMethod: deployment.SSHAuthPassword, HostKeyFingerprint: f.fingerprint,
CertificatePath: f.certificatePath, PrivateKeyPath: f.privateKeyPath,
CertificateChainPath: f.chainPath, WebServer: deployment.WebServer(f.webServer),
ValidationCommand: normalizeCommand(f.validationCommand), ReloadCommand: normalizeCommand(f.reloadCommand),
+10 -99
View File
@@ -2,117 +2,28 @@ package cli
import (
"errors"
"fmt"
"io"
"os"
"strings"
"github.com/spf13/cobra"
"git.pcux.cn/xuans/Scert/backend/internal/sshkey"
)
func newSSHKeyCommand() *cobra.Command {
command := &cobra.Command{Use: "ssh-key", Short: "管理 SSH 部署私钥"}
command.AddCommand(newSSHKeyListCommand(), newSSHKeyAddCommand(), newSSHKeyDeleteCommand())
return command
return &cobra.Command{
Use: "ssh-key",
Short: "SSH 密钥登录功能已停用",
RunE: func(cmd *cobra.Command, args []string) error {
return errors.New("SSH 密钥登录功能已永久停用,仅支持 SSH 密码认证")
},
}
}
func newSSHKeyListCommand() *cobra.Command {
return &cobra.Command{Use: "list", Short: "查看 SSH 私钥元数据", RunE: func(cmd *cobra.Command, args []string) error {
service, err := openApplication(cmd)
if err != nil {
return err
}
defer service.Close()
items, err := service.SSHKeys.List(cmd.Context())
if err != nil {
return err
}
return printJSON(cmd, items)
}}
return newSSHKeyCommand()
}
func newSSHKeyAddCommand() *cobra.Command {
var name, filePath, passphraseFile string
command := &cobra.Command{Use: "add", Short: "从文件安全录入 SSH 私钥", RunE: func(cmd *cobra.Command, args []string) error {
privateKey, err := readLimitedFile(filePath, sshkey.MaxPrivateKeyBytes)
if err != nil {
return fmt.Errorf("读取 SSH 私钥文件失败:%w", err)
}
passphrase := ""
if strings.TrimSpace(passphraseFile) != "" {
content, err := readLimitedFile(passphraseFile, 4096)
if err != nil {
return fmt.Errorf("读取 SSH 私钥口令文件失败:%w", err)
}
passphrase = strings.TrimRight(content, "\r\n")
}
service, err := openApplication(cmd)
if err != nil {
return err
}
defer service.Close()
item, err := service.SSHKeys.Create(cmd.Context(), name, privateKey, passphrase)
if err != nil {
return err
}
return printJSON(cmd, item)
}}
command.Flags().StringVar(&name, "name", "", "SSH 私钥名称")
command.Flags().StringVar(&filePath, "file", "", "SSH 私钥文件路径")
command.Flags().StringVar(&passphraseFile, "passphrase-file", "", "可选的私钥口令文件路径")
_ = command.MarkFlagRequired("name")
_ = command.MarkFlagRequired("file")
return command
return newSSHKeyCommand()
}
func newSSHKeyDeleteCommand() *cobra.Command {
var id string
command := &cobra.Command{Use: "delete", Short: "删除未被服务器使用的 SSH 私钥", RunE: func(cmd *cobra.Command, args []string) error {
service, err := openApplication(cmd)
if err != nil {
return err
}
defer service.Close()
reference, err := service.SSHKeys.Reference(id)
if err != nil {
return err
}
used, err := service.Servers.IsPrivateKeyRefUsed(cmd.Context(), reference)
if err != nil {
return err
}
if used {
return errors.New("SSH 私钥仍被部署服务器使用,不能删除")
}
if err := service.SSHKeys.Delete(cmd.Context(), id); err != nil {
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "SSH 私钥已删除:%s\n", id)
return nil
}}
command.Flags().StringVar(&id, "id", "", "SSH 私钥 ID")
_ = command.MarkFlagRequired("id")
return command
}
func readLimitedFile(path string, limit int) (string, error) {
path = strings.TrimSpace(path)
if path == "" {
return "", errors.New("文件路径不能为空")
}
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
content, err := io.ReadAll(io.LimitReader(file, int64(limit)+1))
if err != nil {
return "", err
}
if len(content) > limit {
return "", fmt.Errorf("文件不能超过 %d 字节", limit)
}
return string(content), nil
return newSSHKeyCommand()
}