将阿里云 DNS 权限验证限制为六秒
This commit is contained in:
1 parent
3c1fce99be
commit
0e4ed9b2d9
13 files changed
+73
-20
No files matched your search
@@ -20,7 +20,7 @@ const (
|
||||
aliyunDefaultRegion = "cn-hangzhou"
|
||||
aliyunAuthorizationTTL = 600
|
||||
aliyunAuthorizationTimeout = 30 * time.Second
|
||||
aliyunAPICallTimeout = 3 * time.Second
|
||||
aliyunAPICallTimeout = 2 * time.Second
|
||||
)
|
||||
|
||||
// AuthorizationDomain 描述一个阿里云 DNS 根域名的授权探测结果。
|
||||
@@ -54,7 +54,7 @@ type AliyunAuthorizationChecker struct {
|
||||
}
|
||||
|
||||
func NewAliyunAuthorizationChecker() *AliyunAuthorizationChecker {
|
||||
return &AliyunAuthorizationChecker{now: time.Now, recordAPI: newAliyunRecordAPI}
|
||||
return &AliyunAuthorizationChecker{now: time.Now, recordAPI: newAliyunAuthorizationRecordAPI}
|
||||
}
|
||||
|
||||
func (c *AliyunAuthorizationChecker) Supports(providerID string) bool {
|
||||
@@ -270,6 +270,19 @@ func (c *AliyunAuthorizationChecker) currentTime() time.Time {
|
||||
}
|
||||
|
||||
func newAliyunClient(credentials map[string]string) (*client.Client, error) {
|
||||
return newAliyunClientWithRetryOptions(credentials, nil)
|
||||
}
|
||||
|
||||
// newAliyunAuthorizationClient 为权限探测建立独立的单次调用客户端。
|
||||
// 探测流程有严格的阶段时限,不能因 SDK 重试或退避拉长用户等待;证书签发仍使用普通客户端策略。
|
||||
func newAliyunAuthorizationClient(credentials map[string]string) (*client.Client, error) {
|
||||
return newAliyunClientWithRetryOptions(credentials, &dara.RetryOptions{
|
||||
Retryable: false,
|
||||
MaxAttempts: 1,
|
||||
})
|
||||
}
|
||||
|
||||
func newAliyunClientWithRetryOptions(credentials map[string]string, retryOptions *dara.RetryOptions) (*client.Client, error) {
|
||||
cleaned, err := ValidateCredentials(Aliyun, credentials)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -280,6 +293,9 @@ func newAliyunClient(credentials map[string]string) (*client.Client, error) {
|
||||
if token := cleaned["security_token"]; token != "" {
|
||||
config.SetSecurityToken(token)
|
||||
}
|
||||
if retryOptions != nil {
|
||||
config.SetRetryOptions(retryOptions)
|
||||
}
|
||||
result, err := client.NewClient(config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("创建阿里云 DNS 客户端失败:%w", err)
|
||||
|
||||
@@ -7,6 +7,34 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestAliyunAuthorizationRecordAPIUsesSingleAttemptWithoutRetry(t *testing.T) {
|
||||
api, err := newAliyunAuthorizationRecordAPI(scopeCredentials())
|
||||
if err != nil {
|
||||
t.Fatalf("创建阿里云权限探测客户端失败:%v", err)
|
||||
}
|
||||
records, ok := api.(aliyunRecords)
|
||||
if !ok || records.client == nil || records.client.RetryOptions == nil {
|
||||
t.Fatalf("权限探测必须使用带显式重试策略的专用客户端:%T", api)
|
||||
}
|
||||
if records.client.RetryOptions.Retryable || records.client.RetryOptions.MaxAttempts != 1 {
|
||||
t.Fatalf("权限探测客户端不得自动重试:%+v", records.client.RetryOptions)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAliyunACMERecordAPIDoesNotUseAuthorizationRetryPolicy(t *testing.T) {
|
||||
api, err := newAliyunRecordAPI(scopeCredentials())
|
||||
if err != nil {
|
||||
t.Fatalf("创建阿里云 ACME 客户端失败:%v", err)
|
||||
}
|
||||
records, ok := api.(aliyunRecords)
|
||||
if !ok || records.client == nil {
|
||||
t.Fatalf("ACME 客户端类型错误:%T", api)
|
||||
}
|
||||
if records.client.RetryOptions != nil {
|
||||
t.Fatalf("ACME 客户端不应复用权限探测专用重试策略:%+v", records.client.RetryOptions)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAliyunAuthorizationCheckerRechecksEveryRequestAndDetectsRevocation(t *testing.T) {
|
||||
now := time.Date(2026, 9, 14, 12, 0, 0, 0, time.UTC)
|
||||
calls := 0
|
||||
|
||||
@@ -207,7 +207,7 @@ func TestAliyunAuthorizationVerifyCleansUpAfterRequestCancellation(t *testing.T)
|
||||
}
|
||||
|
||||
func TestPermissionVerificationTimeoutForAliyunIsIndependentFromPropagation(t *testing.T) {
|
||||
if got := PermissionVerificationTimeoutFor(Aliyun); got != 10*time.Second {
|
||||
if got := PermissionVerificationTimeoutFor(Aliyun); got != 6*time.Second {
|
||||
t.Fatalf("阿里云权限验证时限错误:%s", got)
|
||||
}
|
||||
if got := PermissionVerificationTimeoutFor(Cloudflare); got != 60*time.Second {
|
||||
|
||||
@@ -78,6 +78,15 @@ func newAliyunRecordAPI(credentials map[string]string) (aliyunRecordAPI, error)
|
||||
return aliyunRecords{client: dnsClient}, nil
|
||||
}
|
||||
|
||||
// newAliyunAuthorizationRecordAPI 仅供短时权限探测使用,不改变 ACME DNS-01 的客户端策略。
|
||||
func newAliyunAuthorizationRecordAPI(credentials map[string]string) (aliyunRecordAPI, error) {
|
||||
dnsClient, err := newAliyunAuthorizationClient(credentials)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return aliyunRecords{client: dnsClient}, nil
|
||||
}
|
||||
|
||||
// 不使用自动发现托管区的上游适配器,也不沿 CNAME 自动扩大写入范围。
|
||||
type scopedAliyunProvider struct {
|
||||
scope AliyunScope
|
||||
|
||||
@@ -40,7 +40,7 @@ const (
|
||||
|
||||
// AliyunPermissionVerificationTimeout 限制阿里云账号内的创建、查询和清理闭环。
|
||||
// 该流程不等待公网 DNS 传播。
|
||||
AliyunPermissionVerificationTimeout = 10 * time.Second
|
||||
AliyunPermissionVerificationTimeout = 6 * time.Second
|
||||
|
||||
dnsCleanupTimeout = 30 * time.Second
|
||||
defaultDNSPermissionTXTLookupTimeout = 30 * time.Second
|
||||
|
||||
Reference in new issue
Block a user