优化阿里云 DNS 权限验证等待时长
This commit is contained in:
1 parent
91eaf31d15
commit
d8205ae651
20 files changed
+560
-38
No files matched your search
@@ -20,6 +20,7 @@ const (
|
||||
aliyunDefaultRegion = "cn-hangzhou"
|
||||
aliyunAuthorizationTTL = 600
|
||||
aliyunAuthorizationTimeout = 30 * time.Second
|
||||
aliyunAPICallTimeout = 5 * time.Second
|
||||
)
|
||||
|
||||
// AuthorizationDomain 描述一个阿里云 DNS 根域名的授权探测结果。
|
||||
@@ -27,6 +28,7 @@ type AuthorizationDomain struct {
|
||||
Domain string `json:"domain"`
|
||||
Listed bool `json:"listed"`
|
||||
Writable bool `json:"writable"`
|
||||
Readable bool `json:"readable"`
|
||||
Cleanup bool `json:"cleanup"`
|
||||
Status string `json:"status"`
|
||||
Error string `json:"error,omitempty"`
|
||||
@@ -37,6 +39,7 @@ type AuthorizationReport struct {
|
||||
Provider string `json:"provider"`
|
||||
RequestedDomain string `json:"requested_domain,omitempty"`
|
||||
MatchedDomain string `json:"matched_domain,omitempty"`
|
||||
RecordName string `json:"record_name,omitempty"`
|
||||
Domains []AuthorizationDomain `json:"domains"`
|
||||
Verified bool `json:"verified"`
|
||||
Diagnosis string `json:"diagnosis,omitempty"`
|
||||
@@ -120,9 +123,9 @@ func (c *AliyunAuthorizationChecker) Inspect(ctx context.Context, requestedDomai
|
||||
}
|
||||
|
||||
// Verify 只在用户显式允许的域名及托管区中执行一次写入和精确清理。
|
||||
func (c *AliyunAuthorizationChecker) Verify(ctx context.Context, requestedDomain string, credentials map[string]string) (AuthorizationReport, error) {
|
||||
func (c *AliyunAuthorizationChecker) Verify(ctx context.Context, requestedDomain string, credentials map[string]string) (report AuthorizationReport, resultErr error) {
|
||||
requestedDomain = normalizeAuthorizationDomain(requestedDomain)
|
||||
report := AuthorizationReport{Provider: Aliyun, RequestedDomain: requestedDomain, CheckedAt: c.currentTime().UTC()}
|
||||
report = AuthorizationReport{Provider: Aliyun, RequestedDomain: requestedDomain, CheckedAt: c.currentTime().UTC()}
|
||||
if !domain.IsValidName(requestedDomain) {
|
||||
return report, fmt.Errorf("待验证域名格式无效:%s", requestedDomain)
|
||||
}
|
||||
@@ -137,6 +140,8 @@ func (c *AliyunAuthorizationChecker) Verify(ctx context.Context, requestedDomain
|
||||
if err := ctx.Err(); err != nil {
|
||||
return report, err
|
||||
}
|
||||
verificationCtx, cancel := context.WithTimeout(ctx, PermissionVerificationTimeoutFor(Aliyun))
|
||||
defer cancel()
|
||||
report.Domains = []AuthorizationDomain{{Domain: zone, Status: "待验证"}}
|
||||
if c == nil || c.recordAPI == nil {
|
||||
return report, errors.New("阿里云权限探测器未初始化")
|
||||
@@ -158,7 +163,10 @@ func (c *AliyunAuthorizationChecker) Verify(ctx context.Context, requestedDomain
|
||||
if base != zone {
|
||||
rr += "." + strings.TrimSuffix(base, "."+zone)
|
||||
}
|
||||
recordID, err := api.Add(ctx, zone, rr, value)
|
||||
report.RecordName = rr + "." + zone + "."
|
||||
addCtx, addCancel := context.WithTimeout(verificationCtx, aliyunAPICallTimeout)
|
||||
recordID, err := api.Add(addCtx, zone, rr, value)
|
||||
addCancel()
|
||||
if err != nil {
|
||||
markAuthorizationFailure(&report, zone, err)
|
||||
report.Diagnosis = DiagnoseAliyunError(err, zone)
|
||||
@@ -166,18 +174,56 @@ func (c *AliyunAuthorizationChecker) Verify(ctx context.Context, requestedDomain
|
||||
}
|
||||
report.Domains[0].Writable = true
|
||||
report.MatchedDomain = zone
|
||||
cleanupContext, cancel := context.WithTimeout(context.WithoutCancel(ctx), aliyunAuthorizationTimeout)
|
||||
defer cancel()
|
||||
if err := api.Delete(cleanupContext, recordID); err != nil {
|
||||
markAuthorizationFailure(&report, zone, fmt.Errorf("清理授权探测 TXT 记录失败:%w", err))
|
||||
return report, err
|
||||
defer func() {
|
||||
cleanupBase := context.WithoutCancel(verificationCtx)
|
||||
if deadline, ok := verificationCtx.Deadline(); ok {
|
||||
var cleanupDeadlineCancel context.CancelFunc
|
||||
cleanupBase, cleanupDeadlineCancel = context.WithDeadline(cleanupBase, deadline)
|
||||
defer cleanupDeadlineCancel()
|
||||
}
|
||||
cleanupContext, cleanupCancel := context.WithTimeout(cleanupBase, aliyunAPICallTimeout)
|
||||
defer cleanupCancel()
|
||||
if cleanupErr := api.Delete(cleanupContext, recordID); cleanupErr != nil {
|
||||
cleanupErr = fmt.Errorf("清理授权探测 TXT 记录失败:%w", cleanupErr)
|
||||
markAuthorizationFailure(&report, zone, cleanupErr)
|
||||
report.Verified = false
|
||||
if resultErr == nil {
|
||||
resultErr = cleanupErr
|
||||
} else {
|
||||
resultErr = errors.Join(resultErr, cleanupErr)
|
||||
}
|
||||
return
|
||||
}
|
||||
report.Domains[0].Cleanup = true
|
||||
}()
|
||||
describeCtx, describeCancel := context.WithTimeout(verificationCtx, aliyunAPICallTimeout)
|
||||
info, err := api.Describe(describeCtx, recordID)
|
||||
describeCancel()
|
||||
if err != nil {
|
||||
resultErr = fmt.Errorf("查询授权探测 TXT 记录失败:%w", err)
|
||||
markAuthorizationFailure(&report, zone, resultErr)
|
||||
report.Diagnosis = DiagnoseAliyunError(err, zone)
|
||||
return report, resultErr
|
||||
}
|
||||
report.Domains[0].Cleanup = true
|
||||
if !matchesAliyunAuthorizationRecord(info, recordID, zone, rr, value) {
|
||||
resultErr = errors.New("阿里云返回的临时 TXT 记录与本次验证不一致")
|
||||
markAuthorizationFailure(&report, zone, resultErr)
|
||||
return report, resultErr
|
||||
}
|
||||
report.Domains[0].Readable = true
|
||||
report.Domains[0].Status = "可操作"
|
||||
report.Verified = true
|
||||
return report, nil
|
||||
}
|
||||
|
||||
func matchesAliyunAuthorizationRecord(info aliyunRecordInfo, recordID, zone, rr, value string) bool {
|
||||
return strings.TrimSpace(info.RecordID) == strings.TrimSpace(recordID) &&
|
||||
normalizeAuthorizationDomain(info.DomainName) == normalizeAuthorizationDomain(zone) &&
|
||||
strings.TrimSpace(info.RR) == strings.TrimSpace(rr) &&
|
||||
strings.EqualFold(strings.TrimSpace(info.Type), "TXT") &&
|
||||
info.Value == value
|
||||
}
|
||||
|
||||
// VerifyDomain 每次重新证明写入和清理能力,不使用跨请求缓存掩盖撤权。
|
||||
func (c *AliyunAuthorizationChecker) VerifyDomain(ctx context.Context, providerID, requestedDomain string, credentials map[string]string) error {
|
||||
if !c.Supports(providerID) {
|
||||
@@ -327,6 +373,13 @@ func dereferenceInt64(value *int64) int64 {
|
||||
return *value
|
||||
}
|
||||
|
||||
func dereferenceString(value *string) string {
|
||||
if value == nil {
|
||||
return ""
|
||||
}
|
||||
return *value
|
||||
}
|
||||
|
||||
func uniqueStrings(values []string) []string {
|
||||
result := make([]string, 0, len(values))
|
||||
seen := make(map[string]struct{}, len(values))
|
||||
|
||||
@@ -0,0 +1,224 @@
|
||||
package dnsprovider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type authorizationCall struct {
|
||||
name string
|
||||
id string
|
||||
}
|
||||
|
||||
type authorizationRecordAPI struct {
|
||||
calls []authorizationCall
|
||||
zone string
|
||||
rr string
|
||||
value string
|
||||
addErr error
|
||||
describeErr error
|
||||
deleteErr error
|
||||
describeInfo aliyunRecordInfo
|
||||
cancelAfterAdd context.CancelFunc
|
||||
cleanupWasCanceled bool
|
||||
callTimeouts []time.Duration
|
||||
}
|
||||
|
||||
func (a *authorizationRecordAPI) Add(ctx context.Context, zone, rr, value string) (string, error) {
|
||||
a.recordCall("add", "", ctx)
|
||||
a.zone, a.rr, a.value = zone, rr, value
|
||||
if a.cancelAfterAdd != nil {
|
||||
a.cancelAfterAdd()
|
||||
}
|
||||
if a.addErr != nil {
|
||||
return "", a.addErr
|
||||
}
|
||||
return "authorization-record-id", nil
|
||||
}
|
||||
|
||||
func (a *authorizationRecordAPI) Describe(ctx context.Context, recordID string) (aliyunRecordInfo, error) {
|
||||
a.recordCall("describe", recordID, ctx)
|
||||
if a.describeErr != nil {
|
||||
return aliyunRecordInfo{}, a.describeErr
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return aliyunRecordInfo{}, err
|
||||
}
|
||||
if a.describeInfo.RecordID != "" {
|
||||
return a.describeInfo, nil
|
||||
}
|
||||
return aliyunRecordInfo{
|
||||
RecordID: recordID,
|
||||
DomainName: a.zone,
|
||||
RR: a.rr,
|
||||
Type: "TXT",
|
||||
Value: a.value,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a *authorizationRecordAPI) Delete(ctx context.Context, recordID string) error {
|
||||
a.recordCall("delete", recordID, ctx)
|
||||
a.cleanupWasCanceled = ctx.Err() != nil
|
||||
return a.deleteErr
|
||||
}
|
||||
|
||||
func (a *authorizationRecordAPI) recordCall(name, id string, ctx context.Context) {
|
||||
a.calls = append(a.calls, authorizationCall{name: name, id: id})
|
||||
timeout := time.Duration(0)
|
||||
if deadline, ok := ctx.Deadline(); ok {
|
||||
timeout = time.Until(deadline)
|
||||
}
|
||||
a.callTimeouts = append(a.callTimeouts, timeout)
|
||||
}
|
||||
|
||||
func newAuthorizationCheckerForTest(api aliyunRecordAPI) *AliyunAuthorizationChecker {
|
||||
checker := NewAliyunAuthorizationChecker()
|
||||
checker.recordAPI = func(map[string]string) (aliyunRecordAPI, error) {
|
||||
return api, nil
|
||||
}
|
||||
return checker
|
||||
}
|
||||
|
||||
func TestAliyunAuthorizationVerifyClosesWriteReadCleanupLoop(t *testing.T) {
|
||||
api := &authorizationRecordAPI{}
|
||||
report, err := newAuthorizationCheckerForTest(api).Verify(t.Context(), "www.sub.example.com", scopeCredentials())
|
||||
if err != nil {
|
||||
t.Fatalf("阿里云授权闭环验证失败:%v", err)
|
||||
}
|
||||
if !report.Verified || report.RecordName == "" || len(report.Domains) != 1 {
|
||||
t.Fatalf("授权报告不完整:%+v", report)
|
||||
}
|
||||
domain := report.Domains[0]
|
||||
if !domain.Writable || !domain.Readable || !domain.Cleanup || domain.Status != "可操作" {
|
||||
t.Fatalf("授权闭环状态不正确:%+v", domain)
|
||||
}
|
||||
if got := callNames(api.calls); got != "add,describe,delete" {
|
||||
t.Fatalf("API 调用顺序错误:%s", got)
|
||||
}
|
||||
if api.calls[1].id != "authorization-record-id" || api.calls[2].id != "authorization-record-id" {
|
||||
t.Fatalf("查询或清理未使用本次创建的记录 ID:%+v", api.calls)
|
||||
}
|
||||
if len(api.callTimeouts) != 3 {
|
||||
t.Fatalf("未记录全部 API 调用时限:%+v", api.callTimeouts)
|
||||
}
|
||||
for index, timeout := range api.callTimeouts {
|
||||
if timeout <= 0 || timeout > aliyunAPICallTimeout {
|
||||
t.Fatalf("第 %d 次阿里云 API 调用未限制在 %s 内:%s", index+1, aliyunAPICallTimeout, timeout)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAliyunAuthorizationVerifyDoesNotCallCloudOutsideExplicitScope(t *testing.T) {
|
||||
api := &authorizationRecordAPI{}
|
||||
checker := newAuthorizationCheckerForTest(api)
|
||||
constructed := 0
|
||||
checker.recordAPI = func(map[string]string) (aliyunRecordAPI, error) {
|
||||
constructed++
|
||||
return api, nil
|
||||
}
|
||||
report, err := checker.Verify(t.Context(), "pcux.cn", scopeCredentials())
|
||||
if err == nil || report.Verified || constructed != 0 || len(api.calls) != 0 {
|
||||
t.Fatalf("范围外域名不应构造客户端或调用云 API:report=%+v constructed=%d calls=%+v err=%v", report, constructed, api.calls, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAliyunAuthorizationVerifyFailurePathsAlwaysPreserveProofBoundary(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
configure func(*authorizationRecordAPI)
|
||||
wantCalls string
|
||||
wantWritable bool
|
||||
wantReadable bool
|
||||
wantCleanup bool
|
||||
wantErrorPart string
|
||||
}{
|
||||
{
|
||||
name: "创建失败",
|
||||
configure: func(api *authorizationRecordAPI) { api.addErr = errors.New("Forbidden.RAM") },
|
||||
wantCalls: "add",
|
||||
wantErrorPart: "Forbidden.RAM",
|
||||
},
|
||||
{
|
||||
name: "查询失败仍清理",
|
||||
configure: func(api *authorizationRecordAPI) { api.describeErr = errors.New("Forbidden.RAM") },
|
||||
wantCalls: "add,describe,delete",
|
||||
wantWritable: true,
|
||||
wantCleanup: true,
|
||||
wantErrorPart: "查询授权探测 TXT 记录失败",
|
||||
},
|
||||
{
|
||||
name: "查询内容不一致仍清理",
|
||||
configure: func(api *authorizationRecordAPI) {
|
||||
api.describeInfo = aliyunRecordInfo{RecordID: "other-record-id", DomainName: "sub.example.com", RR: "wrong", Type: "TXT", Value: "wrong"}
|
||||
},
|
||||
wantCalls: "add,describe,delete",
|
||||
wantWritable: true,
|
||||
wantCleanup: true,
|
||||
wantErrorPart: "不一致",
|
||||
},
|
||||
{
|
||||
name: "清理失败不能通过",
|
||||
configure: func(api *authorizationRecordAPI) { api.deleteErr = errors.New("Forbidden.RAM") },
|
||||
wantCalls: "add,describe,delete",
|
||||
wantWritable: true,
|
||||
wantReadable: true,
|
||||
wantErrorPart: "清理授权探测 TXT 记录失败",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
api := &authorizationRecordAPI{}
|
||||
tc.configure(api)
|
||||
report, err := newAuthorizationCheckerForTest(api).Verify(t.Context(), "www.sub.example.com", scopeCredentials())
|
||||
if err == nil || !strings.Contains(err.Error(), tc.wantErrorPart) {
|
||||
t.Fatalf("错误结果不正确:err=%v", err)
|
||||
}
|
||||
if report.Verified || len(report.Domains) != 1 {
|
||||
t.Fatalf("失败路径不能报告已验证:%+v", report)
|
||||
}
|
||||
domain := report.Domains[0]
|
||||
if domain.Writable != tc.wantWritable || domain.Readable != tc.wantReadable || domain.Cleanup != tc.wantCleanup {
|
||||
t.Fatalf("失败路径状态错误:%+v", domain)
|
||||
}
|
||||
if got := callNames(api.calls); got != tc.wantCalls {
|
||||
t.Fatalf("失败路径 API 调用顺序错误:got=%s want=%s", got, tc.wantCalls)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAliyunAuthorizationVerifyCleansUpAfterRequestCancellation(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
api := &authorizationRecordAPI{cancelAfterAdd: cancel}
|
||||
report, err := newAuthorizationCheckerForTest(api).Verify(ctx, "www.sub.example.com", scopeCredentials())
|
||||
if !errors.Is(err, context.Canceled) || report.Verified {
|
||||
t.Fatalf("请求取消必须失败且不能报告已验证:report=%+v err=%v", report, err)
|
||||
}
|
||||
if got := callNames(api.calls); got != "add,describe,delete" {
|
||||
t.Fatalf("请求取消后必须完成受控清理:%s", got)
|
||||
}
|
||||
if api.cleanupWasCanceled {
|
||||
t.Fatal("清理请求不应继承已取消的上下文")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPermissionVerificationTimeoutForAliyunIsIndependentFromPropagation(t *testing.T) {
|
||||
if got := PermissionVerificationTimeoutFor(Aliyun); got != 15*time.Second {
|
||||
t.Fatalf("阿里云权限验证时限错误:%s", got)
|
||||
}
|
||||
if got := PermissionVerificationTimeoutFor(Cloudflare); got != 60*time.Second {
|
||||
t.Fatalf("其他 DNS 服务商权限验证时限错误:%s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func callNames(calls []authorizationCall) string {
|
||||
names := make([]string, 0, len(calls))
|
||||
for _, call := range calls {
|
||||
names = append(names, call.name)
|
||||
}
|
||||
return strings.Join(names, ",")
|
||||
}
|
||||
@@ -15,9 +15,18 @@ import (
|
||||
|
||||
type aliyunRecordAPI interface {
|
||||
Add(context.Context, string, string, string) (string, error)
|
||||
Describe(context.Context, string) (aliyunRecordInfo, error)
|
||||
Delete(context.Context, string) error
|
||||
}
|
||||
|
||||
type aliyunRecordInfo struct {
|
||||
RecordID string
|
||||
DomainName string
|
||||
RR string
|
||||
Type string
|
||||
Value string
|
||||
}
|
||||
|
||||
type aliyunRecords struct{ client *client.Client }
|
||||
|
||||
func (a aliyunRecords) Add(ctx context.Context, zone, rr, value string) (string, error) {
|
||||
@@ -35,6 +44,28 @@ func (a aliyunRecords) Add(ctx context.Context, zone, rr, value string) (string,
|
||||
return strings.TrimSpace(*response.Body.RecordId), nil
|
||||
}
|
||||
|
||||
func (a aliyunRecords) Describe(ctx context.Context, recordID string) (aliyunRecordInfo, error) {
|
||||
response, err := client.DescribeDomainRecordInfoWithContext(
|
||||
ctx,
|
||||
a.client,
|
||||
new(client.DescribeDomainRecordInfoRequest).SetRecordId(recordID),
|
||||
&dara.RuntimeOptions{},
|
||||
)
|
||||
if err != nil {
|
||||
return aliyunRecordInfo{}, err
|
||||
}
|
||||
if response == nil || response.Body == nil {
|
||||
return aliyunRecordInfo{}, errors.New("阿里云未返回临时 TXT 记录详情")
|
||||
}
|
||||
return aliyunRecordInfo{
|
||||
RecordID: strings.TrimSpace(dereferenceString(response.Body.RecordId)),
|
||||
DomainName: normalizeAuthorizationDomain(dereferenceString(response.Body.DomainName)),
|
||||
RR: strings.TrimSpace(dereferenceString(response.Body.RR)),
|
||||
Type: strings.TrimSpace(dereferenceString(response.Body.Type)),
|
||||
Value: dereferenceString(response.Body.Value),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a aliyunRecords) Delete(ctx context.Context, id string) error {
|
||||
return deleteAuthorizationRecord(ctx, a.client, id)
|
||||
}
|
||||
|
||||
@@ -50,17 +50,38 @@ func TestAliyunScopeBoundaries(t *testing.T) {
|
||||
}
|
||||
|
||||
type fakeAliyunRecords struct {
|
||||
zones, rrs, deleted []string
|
||||
addErr, deleteErr error
|
||||
canceledCleanup bool
|
||||
zones, rrs, values, deleted []string
|
||||
addErr, describeErr, deleteErr error
|
||||
describeInfo aliyunRecordInfo
|
||||
canceledCleanup bool
|
||||
}
|
||||
|
||||
func (f *fakeAliyunRecords) Add(_ context.Context, zone, rr, _ string) (string, error) {
|
||||
func (f *fakeAliyunRecords) Add(_ context.Context, zone, rr, value string) (string, error) {
|
||||
f.zones = append(f.zones, zone)
|
||||
f.rrs = append(f.rrs, rr)
|
||||
f.values = append(f.values, value)
|
||||
return "created-record-id", f.addErr
|
||||
}
|
||||
|
||||
func (f *fakeAliyunRecords) Describe(_ context.Context, _ string) (aliyunRecordInfo, error) {
|
||||
if f.describeErr != nil {
|
||||
return aliyunRecordInfo{}, f.describeErr
|
||||
}
|
||||
if f.describeInfo.RecordID != "" {
|
||||
return f.describeInfo, nil
|
||||
}
|
||||
if len(f.zones) == 0 || len(f.rrs) == 0 || len(f.values) == 0 {
|
||||
return aliyunRecordInfo{}, errors.New("没有可供查询的临时记录")
|
||||
}
|
||||
return aliyunRecordInfo{
|
||||
RecordID: "created-record-id",
|
||||
DomainName: f.zones[len(f.zones)-1],
|
||||
RR: f.rrs[len(f.rrs)-1],
|
||||
Type: "TXT",
|
||||
Value: f.values[len(f.values)-1],
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (f *fakeAliyunRecords) Delete(ctx context.Context, id string) error {
|
||||
f.deleted = append(f.deleted, id)
|
||||
f.canceledCleanup = ctx.Err() != nil
|
||||
|
||||
@@ -28,6 +28,7 @@ type TXTResolver interface {
|
||||
type Tester struct {
|
||||
factory Factory
|
||||
resolver TXTResolver
|
||||
aliyunAuthorization *AliyunAuthorizationChecker
|
||||
propagationTimeout time.Duration
|
||||
propagationInterval time.Duration
|
||||
}
|
||||
@@ -37,11 +38,22 @@ const (
|
||||
// 它与 ACME DNS-01 的公网传播等待相互独立。
|
||||
PermissionVerificationTimeout = 60 * time.Second
|
||||
|
||||
// AliyunPermissionVerificationTimeout 限制阿里云账号内的创建、查询和清理闭环。
|
||||
// 该流程不等待公网 DNS 传播。
|
||||
AliyunPermissionVerificationTimeout = 15 * time.Second
|
||||
|
||||
dnsCleanupTimeout = 30 * time.Second
|
||||
defaultDNSPermissionTXTLookupTimeout = 30 * time.Second
|
||||
defaultDNSPermissionTXTLookupInterval = 2 * time.Second
|
||||
)
|
||||
|
||||
func PermissionVerificationTimeoutFor(providerID string) time.Duration {
|
||||
if strings.EqualFold(strings.TrimSpace(providerID), Aliyun) {
|
||||
return AliyunPermissionVerificationTimeout
|
||||
}
|
||||
return PermissionVerificationTimeout
|
||||
}
|
||||
|
||||
func NewTester(factory Factory) *Tester {
|
||||
return NewTesterWithResolverAndSettings(factory, legoTXTResolver{}, defaultDNSPermissionTXTLookupTimeout, defaultDNSPermissionTXTLookupInterval)
|
||||
}
|
||||
@@ -67,6 +79,7 @@ func NewTesterWithResolverAndSettings(factory Factory, resolver TXTResolver, tim
|
||||
return &Tester{
|
||||
factory: factory,
|
||||
resolver: resolver,
|
||||
aliyunAuthorization: NewAliyunAuthorizationChecker(),
|
||||
propagationTimeout: timeout,
|
||||
propagationInterval: interval,
|
||||
}
|
||||
@@ -97,7 +110,7 @@ func (t *Tester) Verify(ctx context.Context, providerID, testDomain string, cred
|
||||
|
||||
func (t *Tester) VerifyWithReport(ctx context.Context, providerID, testDomain string, credentials map[string]string) (setup.DNSVerificationReport, error) {
|
||||
report := setup.DNSVerificationReport{}
|
||||
if t == nil || t.factory == nil || t.resolver == nil {
|
||||
if t == nil {
|
||||
return report, errors.New("DNS 验证服务未初始化")
|
||||
}
|
||||
testDomain = strings.ToLower(strings.TrimSuffix(strings.TrimSpace(testDomain), "."))
|
||||
@@ -111,6 +124,12 @@ func (t *Tester) VerifyWithReport(ctx context.Context, providerID, testDomain st
|
||||
if err := ValidateAliyunDomainScope(providerID, []string{testDomain}, cleaned); err != nil {
|
||||
return report, err
|
||||
}
|
||||
if strings.EqualFold(strings.TrimSpace(providerID), Aliyun) {
|
||||
return t.verifyAliyun(ctx, testDomain, cleaned)
|
||||
}
|
||||
if t.factory == nil || t.resolver == nil {
|
||||
return report, errors.New("DNS 验证服务未初始化")
|
||||
}
|
||||
provider, err := t.factory.Create(providerID, cleaned)
|
||||
if err != nil {
|
||||
return report, err
|
||||
@@ -172,6 +191,58 @@ func (t *Tester) VerifyWithReport(ctx context.Context, providerID, testDomain st
|
||||
return report, nil
|
||||
}
|
||||
|
||||
func (t *Tester) verifyAliyun(ctx context.Context, testDomain string, credentials map[string]string) (setup.DNSVerificationReport, error) {
|
||||
report := setup.DNSVerificationReport{}
|
||||
if t.aliyunAuthorization == nil {
|
||||
return report, errors.New("阿里云 DNS 权限验证服务未初始化")
|
||||
}
|
||||
authorization, err := t.aliyunAuthorization.Verify(ctx, testDomain, credentials)
|
||||
report.RecordName = authorization.RecordName
|
||||
if len(authorization.Domains) == 0 || !authorization.Domains[0].Writable {
|
||||
report.Stages = append(report.Stages, setup.DNSVerificationStage{
|
||||
Name: "创建记录", Status: "failed", Message: "调用阿里云 DNS API 创建临时 TXT 记录失败。",
|
||||
})
|
||||
return report, &VerificationError{Report: report, Err: aliyunVerificationError(err)}
|
||||
}
|
||||
report.Stages = append(report.Stages, setup.DNSVerificationStage{
|
||||
Name: "创建记录", Status: "completed", Message: "已通过阿里云 DNS API 创建临时 TXT 记录。",
|
||||
})
|
||||
if !authorization.Domains[0].Readable {
|
||||
report.Stages = append(report.Stages, setup.DNSVerificationStage{
|
||||
Name: "查询记录", Status: "failed", Message: "阿里云 DNS API 未能精确查询并比对本次临时 TXT 记录。",
|
||||
})
|
||||
appendAliyunCleanupStage(&report, authorization.Domains[0].Cleanup)
|
||||
return report, &VerificationError{Report: report, Err: aliyunVerificationError(err)}
|
||||
}
|
||||
report.Stages = append(report.Stages, setup.DNSVerificationStage{
|
||||
Name: "查询记录", Status: "completed", Message: "已通过阿里云 DNS API 精确查询并比对临时 TXT 记录,未等待公网 DNS 传播。",
|
||||
})
|
||||
appendAliyunCleanupStage(&report, authorization.Domains[0].Cleanup)
|
||||
if err != nil || !authorization.Verified {
|
||||
return report, &VerificationError{Report: report, Err: aliyunVerificationError(err)}
|
||||
}
|
||||
return report, nil
|
||||
}
|
||||
|
||||
func appendAliyunCleanupStage(report *setup.DNSVerificationReport, cleaned bool) {
|
||||
if cleaned {
|
||||
report.Stages = append(report.Stages, setup.DNSVerificationStage{
|
||||
Name: "清理记录", Status: "completed", Message: "已通过阿里云 DNS API 清理本次临时 TXT 记录。",
|
||||
})
|
||||
return
|
||||
}
|
||||
report.Stages = append(report.Stages, setup.DNSVerificationStage{
|
||||
Name: "清理记录", Status: "failed", Message: "临时 TXT 记录清理失败,不能确认阿里云 DNS 权限验证通过。",
|
||||
})
|
||||
}
|
||||
|
||||
func aliyunVerificationError(err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return errors.New("阿里云 DNS 权限验证未通过")
|
||||
}
|
||||
|
||||
func cleanupChallenge(ctx context.Context, provider challenge.Provider, testDomain, token, keyAuth string) error {
|
||||
cleanupBase := context.WithoutCancel(ctx)
|
||||
cleanupCtx, cancel := context.WithTimeout(cleanupBase, dnsCleanupTimeout)
|
||||
|
||||
@@ -130,6 +130,41 @@ func TestTesterCleansUpWithIndependentContextAfterQueryCancellation(t *testing.T
|
||||
}
|
||||
}
|
||||
|
||||
type forbiddenTXTResolver struct{}
|
||||
|
||||
func (forbiddenTXTResolver) LookupTXT(context.Context, string) ([]string, error) {
|
||||
return nil, errors.New("阿里云账号内权限验证不应查询公网 TXT 记录")
|
||||
}
|
||||
|
||||
func TestTesterUsesAliyunAPIReadBackWithoutPublicDNSPropagation(t *testing.T) {
|
||||
api := &authorizationRecordAPI{}
|
||||
tester := &Tester{
|
||||
resolver: forbiddenTXTResolver{},
|
||||
aliyunAuthorization: newAuthorizationCheckerForTest(api),
|
||||
}
|
||||
report, err := tester.VerifyWithReport(t.Context(), Aliyun, "www.sub.example.com", scopeCredentials())
|
||||
if err != nil {
|
||||
t.Fatalf("阿里云权限验证失败:%v", err)
|
||||
}
|
||||
if report.RecordName == "" || len(report.Stages) != 3 {
|
||||
t.Fatalf("阿里云阶段报告不完整:%+v", report)
|
||||
}
|
||||
for _, stage := range report.Stages {
|
||||
if stage.Status != "completed" {
|
||||
t.Fatalf("阿里云阶段未完成:%+v", report.Stages)
|
||||
}
|
||||
}
|
||||
if report.Stages[0].Name != "创建记录" || report.Stages[1].Name != "查询记录" || report.Stages[2].Name != "清理记录" {
|
||||
t.Fatalf("阿里云阶段顺序错误:%+v", report.Stages)
|
||||
}
|
||||
if !strings.Contains(report.Stages[1].Message, "未等待公网 DNS 传播") {
|
||||
t.Fatalf("阿里云查询阶段必须明确不等待公网传播:%+v", report.Stages[1])
|
||||
}
|
||||
if got := callNames(api.calls); got != "add,describe,delete" {
|
||||
t.Fatalf("阿里云不应调用公网 TXT 查询:%s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTesterReportsCleanupFailureAfterQueryFailure(t *testing.T) {
|
||||
t.Setenv("LEGO_DISABLE_CNAME_SUPPORT", "true")
|
||||
provider := &fakeProvider{cleanupErr: errors.New("权限不足")}
|
||||
|
||||
Reference in new issue
Block a user