保存执行租约与原子绑定复核前检查点
This commit is contained in:
1 parent
73dba287d9
commit
7a3b78daa3
28 files changed
+1236
-48
No files matched your search
@@ -33,6 +33,8 @@ func (s *Server) writeDNSAccountError(c *gin.Context, err error) {
|
||||
s.writeError(c, http.StatusForbidden, "无权操作此 DNS 账户", nil)
|
||||
case errors.Is(err, repository.ErrDNSAccountConflict):
|
||||
s.writeError(c, http.StatusConflict, "DNS 账户已变更,请刷新后重试", nil)
|
||||
case errors.Is(err, repository.ErrDNSAccountBusy):
|
||||
s.writeError(c, http.StatusConflict, "DNS 账户仍有执行租约,请等待任务及挑战清理完成", nil)
|
||||
default:
|
||||
// 驱动、秘密仓库和服务商的错误均可能携带凭据,不能回显或写入日志。
|
||||
s.writeError(c, http.StatusServiceUnavailable, "DNS 账户操作失败,请稍后重试", nil)
|
||||
|
||||
@@ -29,7 +29,7 @@ func (s *Server) writeDomainDNSAccountError(c *gin.Context, err error) {
|
||||
s.writeError(c, http.StatusForbidden, "无权操作此域名的 DNS 账户绑定", nil)
|
||||
case errors.Is(err, repository.ErrDNSBindingConflict), errors.Is(err, repository.ErrDNSAccountConflict):
|
||||
s.writeError(c, http.StatusConflict, "域名绑定或 DNS 账户已变更,请刷新后重试", nil)
|
||||
case errors.Is(err, repository.ErrDNSBindingBusy):
|
||||
case errors.Is(err, repository.ErrDNSBindingBusy), errors.Is(err, repository.ErrDNSAccountBusy):
|
||||
s.writeError(c, http.StatusConflict, "域名存在未结束的任务,暂不能变更 DNS 账户", nil)
|
||||
case errors.Is(err, repository.ErrDNSBindingInvalid):
|
||||
s.writeError(c, http.StatusUnprocessableEntity, "域名验证方式或 DNS 账户状态不允许绑定", nil)
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/domain"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/repository"
|
||||
)
|
||||
|
||||
func TestDomainInitialDNSAccountAtomicAPI(t *testing.T) {
|
||||
f := newProjectMemberAPIFixture(t)
|
||||
f.server.projectDomains = f.members
|
||||
f.server.repository = repository.NewSQLDomainRepository(f.db)
|
||||
account, err := f.members.CreateDNSAccount(t.Context(), "primary-super-admin", domain.DNSAccount{ID: "initial-account", Name: "初始账户", ProjectID: "default", Provider: "cloudflare", CredentialRef: "secret://dns/initial"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
account, err = f.members.RecordDNSAccountVerification(t.Context(), "primary-super-admin", account.ID, account.Revision, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, scenario := range []struct {
|
||||
name, actor, provider, challenge, accountID string
|
||||
revision int64
|
||||
status int
|
||||
}{
|
||||
{"success", "primary-super-admin", "cloudflare", "dns-01", account.ID, account.Revision, http.StatusCreated},
|
||||
{"old-version", "primary-super-admin", "cloudflare", "dns-01", account.ID, 1, http.StatusConflict},
|
||||
{"wrong-provider", "primary-super-admin", "route53", "dns-01", account.ID, account.Revision, http.StatusUnprocessableEntity},
|
||||
{"wrong-challenge", "primary-super-admin", "cloudflare", "http-01", account.ID, account.Revision, http.StatusBadRequest},
|
||||
{"no-account", "primary-super-admin", "cloudflare", "dns-01", "", 2, http.StatusBadRequest},
|
||||
{"no-permission", "viewer", "cloudflare", "dns-01", account.ID, account.Revision, http.StatusForbidden},
|
||||
} {
|
||||
t.Run(scenario.name, func(t *testing.T) {
|
||||
name := scenario.name + ".example.test"
|
||||
body := fmt.Sprintf(`{"name":%q,"certificate_type":"single","challenge_type":%q,"dns_provider":%q,"dns_account_id":%q,"dns_account_revision":%d}`, name, scenario.challenge, scenario.provider, scenario.accountID, scenario.revision)
|
||||
response := serveUserRequest(f.server, f.cookies[scenario.actor], http.MethodPost, "/api/v1/domains", body)
|
||||
if response.Code != scenario.status {
|
||||
t.Fatal("创建接口状态错误", response.Code, response.Body.String())
|
||||
}
|
||||
if response.Code == http.StatusCreated {
|
||||
var item domain.Domain
|
||||
if err := json.Unmarshal(response.Body.Bytes(), &item); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
loaded, err := repository.NewSQLDomainRepository(f.db).GetByID(t.Context(), item.ID)
|
||||
if err != nil || item.DNSBindingID == "" || item.DNSBindingID != loaded.DNSBindingID || loaded.DNSAccountID != account.ID {
|
||||
t.Fatal("创建响应与原子绑定不符", err)
|
||||
}
|
||||
response = serveUserRequest(f.server, f.cookies[scenario.actor], http.MethodPut, "/api/v1/domains/"+item.ID, body)
|
||||
if response.Code != http.StatusBadRequest {
|
||||
t.Fatal("普通更新不能绕过绑定令牌", response.Code)
|
||||
}
|
||||
} else {
|
||||
var count int
|
||||
if err := f.db.QueryRowContext(t.Context(), `SELECT COUNT(*) FROM domains WHERE name = ?`, name).Scan(&count); err != nil || count != 0 {
|
||||
t.Fatal("失败后遗留未绑定域名", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -419,13 +419,15 @@ func (s *Server) Shutdown(ctx context.Context) error {
|
||||
}
|
||||
|
||||
type domainRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
CertificateType string `json:"certificate_type" binding:"required"`
|
||||
SANs []string `json:"sans"`
|
||||
AutoRenew *bool `json:"auto_renew"`
|
||||
ACMEEnvironment string `json:"acme_environment"`
|
||||
DNSProvider string `json:"dns_provider"`
|
||||
ChallengeType *string `json:"challenge_type"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
CertificateType string `json:"certificate_type" binding:"required"`
|
||||
SANs []string `json:"sans"`
|
||||
AutoRenew *bool `json:"auto_renew"`
|
||||
ACMEEnvironment string `json:"acme_environment"`
|
||||
DNSProvider string `json:"dns_provider"`
|
||||
DNSAccountID string `json:"dns_account_id"`
|
||||
DNSAccountRevision int64 `json:"dns_account_revision"`
|
||||
ChallengeType *string `json:"challenge_type"`
|
||||
}
|
||||
|
||||
type serverRequest struct {
|
||||
@@ -645,6 +647,16 @@ func (s *Server) createDomain(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
item.ResourceScope = s.newResourceScope(c)
|
||||
if request.DNSAccountID != "" {
|
||||
if !domain.ValidProjectID(request.DNSAccountID) || request.DNSAccountRevision < 1 || item.ChallengeType != domain.ChallengeDNS01 {
|
||||
s.writeError(c, http.StatusBadRequest, "初始 DNS 账户参数无效", nil)
|
||||
return
|
||||
}
|
||||
item.DNSAccountID, item.DNSBindingID, item.InitialDNSRevision = request.DNSAccountID, domain.NewID(), request.DNSAccountRevision
|
||||
} else if request.DNSAccountRevision != 0 {
|
||||
s.writeError(c, http.StatusBadRequest, "初始 DNS 账户参数不完整", nil)
|
||||
return
|
||||
}
|
||||
if !s.authorizeDomainMutation(c, item.ProjectID) {
|
||||
return
|
||||
}
|
||||
@@ -652,6 +664,10 @@ func (s *Server) createDomain(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
if err := s.projectDomains.CreateProjectDomain(c.Request.Context(), c.GetString(authenticatedActorKey), item); err != nil {
|
||||
if errors.Is(err, repository.ErrDNSAccountConflict) || errors.Is(err, repository.ErrDNSBindingInvalid) {
|
||||
s.writeDomainDNSAccountError(c, err)
|
||||
return
|
||||
}
|
||||
if errors.Is(err, repository.ErrProjectAccessDenied) {
|
||||
s.writeError(c, http.StatusForbidden, "无权管理此项目的域名", nil)
|
||||
return
|
||||
@@ -675,6 +691,10 @@ func (s *Server) updateDomain(c *gin.Context) {
|
||||
s.writeError(c, http.StatusBadRequest, "请求参数无效", err)
|
||||
return
|
||||
}
|
||||
if request.DNSAccountID != "" || request.DNSAccountRevision != 0 {
|
||||
s.writeError(c, http.StatusBadRequest, "编辑 DNS 账户须使用独立绑定接口", nil)
|
||||
return
|
||||
}
|
||||
existing, err := s.projectResources.GetVisibleDomain(c.Request.Context(), c.GetString(authenticatedActorKey), c.Param("id"))
|
||||
if err != nil {
|
||||
s.writeProjectResourceError(c, err)
|
||||
|
||||
@@ -274,6 +274,9 @@ func (s *Server) runCertificateOperation(ctx context.Context, task repository.Ta
|
||||
return
|
||||
}
|
||||
if task.TaskType == "certificate_issue" {
|
||||
executionCtx = certificate.WithDNSExecutionIdentity(executionCtx, domain.DNSExecutionIdentity{
|
||||
TaskID: task.ID, AttemptID: attempt.ID, AttemptNumber: attempt.AttemptNumber,
|
||||
})
|
||||
executionCtx = s.withCertificateIssueAuthorization(executionCtx, task.OperatorID, task.DomainID, task.ProjectID, attempt.AttemptNumber > 1)
|
||||
}
|
||||
result, issueErr := operation(executionCtx, task.DomainID)
|
||||
|
||||
Reference in new issue
Block a user