停用 SSH 密钥兼容入口
停用历史 SSH 私钥生成、存储、读取和管理入口,强制服务器使用密码认证。
This commit is contained in:
1 parent
95450cb1eb
commit
7778aa5ced
10 files changed
+80
-614
No files matched your search
@@ -27,7 +27,6 @@ import (
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/firewall"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/logging"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/repository"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/sshkey"
|
||||
)
|
||||
|
||||
type setupSecretWriter interface {
|
||||
@@ -53,7 +52,6 @@ type Dependencies struct {
|
||||
ProjectResources repository.ProjectResourceReader
|
||||
ProjectBindings repository.ProjectBindingRepository
|
||||
ProjectDomains repository.ProjectDomainRepository
|
||||
SSHKeys *sshkey.Service
|
||||
DeploymentFactory deployment.ClientFactory
|
||||
SetupDNS setupDNSVerifier
|
||||
Secrets setupSecretWriter
|
||||
@@ -93,7 +91,6 @@ type Server struct {
|
||||
projectResources repository.ProjectResourceReader
|
||||
projectBindings repository.ProjectBindingRepository
|
||||
projectDomains repository.ProjectDomainRepository
|
||||
sshKeys *sshkey.Service
|
||||
deploymentFactory deployment.ClientFactory
|
||||
setupDNS setupDNSVerifier
|
||||
secrets setupSecretWriter
|
||||
@@ -161,7 +158,6 @@ func New(deps Dependencies) *Server {
|
||||
projectResources: deps.ProjectResources,
|
||||
projectBindings: deps.ProjectBindings,
|
||||
projectDomains: deps.ProjectDomains,
|
||||
sshKeys: deps.SSHKeys,
|
||||
deploymentFactory: deps.DeploymentFactory,
|
||||
issueCertificate: deps.IssueCertificate,
|
||||
issueSetupCertificate: deps.IssueSetupCertificate,
|
||||
|
||||
@@ -1,104 +1,15 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/domain"
|
||||
)
|
||||
|
||||
const maxSSHKeyRequestBytes = 2 * 1024 * 1024
|
||||
|
||||
type sshKeyRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
PrivateKey string `json:"private_key" binding:"required"`
|
||||
Passphrase string `json:"passphrase"`
|
||||
ProjectID string `json:"project_id"`
|
||||
}
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func (s *Server) listSSHKeys(c *gin.Context) {
|
||||
if s.sshKeys == nil {
|
||||
s.writeError(c, http.StatusNotImplemented, "SSH 私钥管理接口尚未配置", errors.New("SSH 私钥管理服务未配置"))
|
||||
return
|
||||
}
|
||||
projectID := strings.TrimSpace(c.Query("project_id"))
|
||||
if projectID == "" {
|
||||
projectID = domain.DefaultProjectID
|
||||
}
|
||||
if !s.authorizeServerMutation(c, projectID) {
|
||||
return
|
||||
}
|
||||
items, err := s.sshKeys.ListForProject(c.Request.Context(), projectID)
|
||||
if err != nil {
|
||||
s.writeError(c, http.StatusInternalServerError, "查询 SSH 私钥失败", err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"items": items})
|
||||
c.Status(404)
|
||||
}
|
||||
|
||||
func (s *Server) createSSHKey(c *gin.Context) {
|
||||
if s.sshKeys == nil {
|
||||
s.writeError(c, http.StatusNotImplemented, "SSH 私钥管理接口尚未配置", errors.New("SSH 私钥管理服务未配置"))
|
||||
return
|
||||
}
|
||||
var request sshKeyRequest
|
||||
if err := bindJSON(c, &request, maxSSHKeyRequestBytes); err != nil {
|
||||
var tooLarge *http.MaxBytesError
|
||||
if errors.As(err, &tooLarge) {
|
||||
s.writeError(c, http.StatusRequestEntityTooLarge, "SSH 私钥请求内容过大", err)
|
||||
return
|
||||
}
|
||||
s.writeError(c, http.StatusBadRequest, "请求参数无效", err)
|
||||
return
|
||||
}
|
||||
projectID := strings.TrimSpace(request.ProjectID)
|
||||
if projectID == "" {
|
||||
projectID = domain.DefaultProjectID
|
||||
}
|
||||
if !s.authorizeServerMutation(c, projectID) {
|
||||
return
|
||||
}
|
||||
item, err := s.sshKeys.CreateForProject(c.Request.Context(), request.Name, request.PrivateKey, request.Passphrase, projectID)
|
||||
if err != nil {
|
||||
s.writeError(c, http.StatusBadRequest, "SSH 私钥无效", err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, item)
|
||||
c.Status(404)
|
||||
}
|
||||
|
||||
func (s *Server) deleteSSHKey(c *gin.Context) {
|
||||
if s.sshKeys == nil || s.servers == nil {
|
||||
s.writeError(c, http.StatusNotImplemented, "SSH 私钥管理接口尚未配置", errors.New("SSH 私钥或服务器管理服务未配置"))
|
||||
return
|
||||
}
|
||||
reference, err := s.sshKeys.Reference(c.Param("id"))
|
||||
if err != nil {
|
||||
s.writeError(c, http.StatusBadRequest, "SSH 私钥 ID 无效", err)
|
||||
return
|
||||
}
|
||||
metadata, err := s.sshKeys.ValidateReference(c.Request.Context(), reference)
|
||||
if err != nil {
|
||||
s.writeError(c, http.StatusBadRequest, "SSH 私钥引用无效", err)
|
||||
return
|
||||
}
|
||||
if !s.authorizeServerMutation(c, metadata.ProjectID) {
|
||||
return
|
||||
}
|
||||
used, err := s.servers.IsPrivateKeyRefUsed(c.Request.Context(), reference)
|
||||
if err != nil {
|
||||
s.writeError(c, http.StatusInternalServerError, "检查 SSH 私钥占用状态失败", err)
|
||||
return
|
||||
}
|
||||
if used {
|
||||
s.writeError(c, http.StatusConflict, "SSH 私钥仍被部署服务器使用", errors.New("SSH 私钥引用正在使用"))
|
||||
return
|
||||
}
|
||||
if err := s.sshKeys.Delete(c.Request.Context(), c.Param("id")); err != nil {
|
||||
s.writeError(c, http.StatusNotFound, "SSH 私钥不存在", err)
|
||||
return
|
||||
}
|
||||
c.Status(http.StatusNoContent)
|
||||
c.Status(404)
|
||||
}
|
||||
Reference in new issue
Block a user