将授权探测纳入执行租约保护并补充撤权回归
This commit is contained in:
1 parent
88aa5bb940
commit
9872298e14
9 files changed
+152
-5
No files matched your search
@@ -28,6 +28,41 @@ func (s *Service) SetDNSExecutionLeaseStore(store DNSExecutionLeaseStore) {
|
||||
}
|
||||
}
|
||||
|
||||
// 授权探测也会写临时 TXT;错误可能表示远端写入或清理结果未知。
|
||||
// 不以缺少 ACME 挑战记录作为释放依据,成功探测才确认本次引用可以收尾。
|
||||
type leasedDNSAuthorization struct {
|
||||
checker DNSAuthorizationChecker
|
||||
store DNSExecutionLeaseStore
|
||||
lease domain.DNSExecutionLease
|
||||
mu sync.Mutex
|
||||
pending int
|
||||
}
|
||||
|
||||
func (a *leasedDNSAuthorization) VerifyDomain(ctx context.Context, providerID, name string, credentials map[string]string) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := a.store.ValidateDNSExecutionLease(ctx, a.lease); err != nil {
|
||||
return errors.New("DNS 执行授权已失效,未执行授权探测")
|
||||
}
|
||||
a.mu.Lock()
|
||||
a.pending++
|
||||
a.mu.Unlock()
|
||||
if err := a.checker.VerifyDomain(ctx, providerID, name, credentials); err != nil {
|
||||
return err
|
||||
}
|
||||
a.mu.Lock()
|
||||
a.pending--
|
||||
a.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *leasedDNSAuthorization) clean() bool {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
return a.pending == 0
|
||||
}
|
||||
|
||||
// 每次写挑战前再次检查任务和权限;清理只能使用原 Provider 和固定凭据。
|
||||
// 失败的 Present 也可能已写入远端,因此必须等待匹配的 CleanUp 成功才释放引用。
|
||||
type leasedDNSProvider struct {
|
||||
|
||||
@@ -85,8 +85,14 @@ func (i executionIssuer) Obtain(ctx context.Context, _ ObtainRequest) (Resource,
|
||||
return i.run(ctx)
|
||||
}
|
||||
|
||||
type executionAuthorization func(context.Context, string, string, map[string]string) error
|
||||
|
||||
func (a executionAuthorization) VerifyDomain(ctx context.Context, provider, name string, credentials map[string]string) error {
|
||||
return a(ctx, provider, name, credentials)
|
||||
}
|
||||
|
||||
func TestIndependentDNSExecutionLifecycle(t *testing.T) {
|
||||
for _, scenario := range []string{"success", "acquire-failed", "validate-failed", "credential-failed", "present-failed", "cleanup-failed", "missing-cleanup", "release-failed", "cancelled", "revoked-before-present", "revoked-before-cleanup"} {
|
||||
for _, scenario := range []string{"success", "acquire-failed", "validate-failed", "credential-failed", "present-failed", "cleanup-failed", "missing-cleanup", "release-failed", "cancelled", "revoked-before-present", "revoked-before-cleanup", "authorization-failed", "revoked-before-authorization", "cancelled-before-authorization"} {
|
||||
t.Run(scenario, func(t *testing.T) {
|
||||
cfg := certificateTestConfig(t.TempDir())
|
||||
cfg.DNS.Provider, cfg.DNS.CredentialRef, cfg.DNS.VerificationState = "aliyun", "secret://dns/system-must-not-read", "unverified"
|
||||
@@ -108,6 +114,28 @@ func TestIndependentDNSExecutionLifecycle(t *testing.T) {
|
||||
defer cancel()
|
||||
ctx = WithDNSExecutionIdentity(ctx, domain.DNSExecutionIdentity{TaskID: "task", AttemptID: "attempt", AttemptNumber: 2})
|
||||
failure := errors.New("测试失败")
|
||||
authorizationCalls := 0
|
||||
service.authorization = executionAuthorization(func(ctx context.Context, id, name string, credentials map[string]string) error {
|
||||
authorizationCalls++
|
||||
if id != "cloudflare" || name != item.Name || credentials["api_token"] != "independent-test-only" {
|
||||
t.Fatal("授权探测未使用租约的固定账户")
|
||||
}
|
||||
if scenario == "authorization-failed" {
|
||||
return failure
|
||||
}
|
||||
return nil
|
||||
})
|
||||
ctx = WithProgressReporter(ctx, func(_ context.Context, _, message string) error {
|
||||
if strings.HasPrefix(message, "正在验证域名 ") {
|
||||
if scenario == "revoked-before-authorization" {
|
||||
store.validateErr = failure
|
||||
}
|
||||
if scenario == "cancelled-before-authorization" {
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
switch scenario {
|
||||
case "acquire-failed":
|
||||
store.acquireErr = failure
|
||||
@@ -165,7 +193,7 @@ func TestIndependentDNSExecutionLifecycle(t *testing.T) {
|
||||
if scenario != "acquire-failed" && scenario != "validate-failed" && reader.reference != "secret://dns/independent" {
|
||||
t.Fatal("未读取固定凭据")
|
||||
}
|
||||
retained := scenario == "present-failed" || scenario == "cleanup-failed" || scenario == "missing-cleanup" || scenario == "acquire-failed"
|
||||
retained := scenario == "present-failed" || scenario == "cleanup-failed" || scenario == "missing-cleanup" || scenario == "acquire-failed" || scenario == "authorization-failed"
|
||||
if (store.released == 0) != retained {
|
||||
t.Fatalf("不确定挑战清理时释放引用,或正常收尾未释放:%d", store.released)
|
||||
}
|
||||
@@ -178,10 +206,52 @@ func TestIndependentDNSExecutionLifecycle(t *testing.T) {
|
||||
if scenario == "release-failed" && (err == nil || !strings.Contains(err.Error(), "租约释放失败")) {
|
||||
t.Fatal("释放失败未上报")
|
||||
}
|
||||
if scenario == "authorization-failed" && (authorizationCalls != 1 || !strings.Contains(err.Error(), "授权探测清理尚未确认")) {
|
||||
t.Fatal("探测失败后未保留不确定清理引用")
|
||||
}
|
||||
if (scenario == "revoked-before-authorization" || scenario == "cancelled-before-authorization") && authorizationCalls != 0 {
|
||||
t.Fatal("撤权或取消后仍发起远端授权探测")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIndependentDNSAuthorizationEachDomainRevalidates(t *testing.T) {
|
||||
store := &executionLeaseStub{}
|
||||
calls := 0
|
||||
checker := &leasedDNSAuthorization{store: store, checker: executionAuthorization(func(context.Context, string, string, map[string]string) error {
|
||||
calls++
|
||||
store.validateErr = errors.New("权限已撤回")
|
||||
return nil
|
||||
})}
|
||||
service := &Service{}
|
||||
err := service.verifyDNSAuthorizationUsing(t.Context(), "cloudflare", []string{"one.example.test", "two.example.test"}, nil, checker)
|
||||
if err == nil || calls != 1 || store.validated != 2 || !checker.clean() {
|
||||
t.Fatal("第二个域名前撤权应停止探测,已确认清理的引用可释放", err, calls, store.validated)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIndependentDNSAuthorizationFailureCannotBeClearedByLaterSuccess(t *testing.T) {
|
||||
store := &executionLeaseStub{}
|
||||
calls := 0
|
||||
checker := &leasedDNSAuthorization{store: store, checker: executionAuthorization(func(context.Context, string, string, map[string]string) error {
|
||||
calls++
|
||||
if calls == 1 {
|
||||
return errors.New("探测远端状态不确定")
|
||||
}
|
||||
return nil
|
||||
})}
|
||||
if err := checker.VerifyDomain(t.Context(), "cloudflare", "one.example.test", nil); err == nil {
|
||||
t.Fatal("首次探测应失败")
|
||||
}
|
||||
if err := checker.VerifyDomain(t.Context(), "cloudflare", "two.example.test", nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if checker.clean() {
|
||||
t.Fatal("后续域名成功不能清除先前未知外部状态")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIndependentDNSExecutionRequiresTaskContext(t *testing.T) {
|
||||
item, _ := domain.NewDomain("lease.example.test", domain.CertificateSingle, nil)
|
||||
item.DNSAccountID, item.DNSBindingID = "account", "binding"
|
||||
|
||||
@@ -170,6 +170,7 @@ func (s *Service) Issue(ctx context.Context, domainID string) (result IssueResul
|
||||
credentialRef := s.config.DNS.CredentialRef
|
||||
var dnsLease domain.DNSExecutionLease
|
||||
var guardedProvider *leasedDNSProvider
|
||||
var guardedAuthorization *leasedDNSAuthorization
|
||||
var lease *httpchallenge.Lease
|
||||
if independent {
|
||||
if challengeType != domain.ChallengeDNS01 || s.credentials == nil || s.providers == nil {
|
||||
@@ -184,6 +185,10 @@ func (s *Service) Issue(ctx context.Context, domainID string) (result IssueResul
|
||||
return IssueResult{}, errors.New("获取独立 DNS 账户执行租约失败,未使用系统凭据")
|
||||
}
|
||||
defer func() {
|
||||
if guardedAuthorization != nil && !guardedAuthorization.clean() {
|
||||
returnErr = errors.Join(returnErr, errors.New("DNS 授权探测清理尚未确认,执行租约及原凭据引用已保留"))
|
||||
return
|
||||
}
|
||||
if guardedProvider != nil && !guardedProvider.clean() {
|
||||
returnErr = errors.Join(returnErr, errors.New("DNS 挑战清理尚未确认,执行租约及原凭据引用已保留"))
|
||||
return
|
||||
@@ -240,7 +245,12 @@ func (s *Service) Issue(ctx context.Context, domainID string) (result IssueResul
|
||||
if err := ReportProgress(ctx, "dns_validation", "正在验证 DNS 凭据权限"); err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
if err := s.verifyDNSAuthorization(ctx, providerID, domains, credentials); err != nil {
|
||||
checker := s.authorization
|
||||
if independent && checker != nil {
|
||||
guardedAuthorization = &leasedDNSAuthorization{checker: checker, store: s.dnsLeases, lease: dnsLease}
|
||||
checker = guardedAuthorization
|
||||
}
|
||||
if err := s.verifyDNSAuthorizationUsing(ctx, providerID, domains, credentials, checker); err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
if err := ReportProgress(ctx, "certificate_issue", "DNS 凭据验证通过,正在申请证书"); err != nil {
|
||||
@@ -379,11 +389,19 @@ func (s *Service) IssueForSetup(ctx context.Context, request SetupCertificateReq
|
||||
}
|
||||
|
||||
func (s *Service) verifyDNSAuthorization(ctx context.Context, providerID string, domains []string, credentials map[string]string) error {
|
||||
var checker DNSAuthorizationChecker
|
||||
if s != nil {
|
||||
checker = s.authorization
|
||||
}
|
||||
return s.verifyDNSAuthorizationUsing(ctx, providerID, domains, credentials, checker)
|
||||
}
|
||||
|
||||
func (s *Service) verifyDNSAuthorizationUsing(ctx context.Context, providerID string, domains []string, credentials map[string]string, checker DNSAuthorizationChecker) error {
|
||||
// 必须先校验全部 SAN;后续域名越界时也不能先写入前面的域名。
|
||||
if err := dnsprovider.ValidateAliyunTargetDomains(providerID, domains); err != nil {
|
||||
return err
|
||||
}
|
||||
if s == nil || s.authorization == nil {
|
||||
if checker == nil {
|
||||
if strings.EqualFold(strings.TrimSpace(providerID), dnsprovider.Aliyun) {
|
||||
return errors.New("阿里云权限探测器未初始化,已阻止证书申请")
|
||||
}
|
||||
@@ -393,7 +411,7 @@ func (s *Service) verifyDNSAuthorization(ctx context.Context, providerID string,
|
||||
if err := ReportProgress(ctx, "dns_validation", fmt.Sprintf("正在验证域名 %s 的 DNS 权限", requestedDomain)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.authorization.VerifyDomain(ctx, providerID, requestedDomain, credentials); err != nil {
|
||||
if err := checker.VerifyDomain(ctx, providerID, requestedDomain, credentials); err != nil {
|
||||
return fmt.Errorf("DNS 凭据无权操作域名 %s:%w", requestedDomain, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user