完善阿里云授权动态刷新与失效处理
This commit is contained in:
1 parent
b5733b0e7c
commit
fce03798b5
4 files changed
+350
-43
No files matched your search
@@ -33,6 +33,10 @@ type DNSAuthorizationChecker interface {
|
||||
VerifyDomain(ctx context.Context, providerID, requestedDomain string, credentials map[string]string) error
|
||||
}
|
||||
|
||||
type DNSAuthorizationCacheInvalidator interface {
|
||||
InvalidateDomain(providerID, requestedDomain string, credentials map[string]string)
|
||||
}
|
||||
|
||||
type AccountManager interface {
|
||||
LoadOrCreate(ctx context.Context, environment, email string) (*AccountUser, error)
|
||||
Save(ctx context.Context, environment string, user *AccountUser) error
|
||||
@@ -160,6 +164,9 @@ func (s *Service) Issue(ctx context.Context, domainID string) (IssueResult, erro
|
||||
resource, issueErr := issuer.Obtain(ctx, ObtainRequest{
|
||||
Domains: domains, AccountEmail: email, Environment: item.ACMEEnvironment, PreferredChain: s.preferredChain,
|
||||
})
|
||||
if issueErr != nil {
|
||||
s.invalidateDNSAuthorizationOnFailure(providerID, domains, credentials, issueErr)
|
||||
}
|
||||
if saveErr := s.accounts.Save(ctx, item.ACMEEnvironment, user); saveErr != nil {
|
||||
if issueErr != nil {
|
||||
return fail(fmt.Errorf("%v;保存 ACME 账户状态失败:%w", issueErr, saveErr))
|
||||
@@ -234,6 +241,9 @@ func (s *Service) IssueForSetup(ctx context.Context, request SetupCertificateReq
|
||||
resource, issueErr := issuer.Obtain(ctx, ObtainRequest{
|
||||
Domains: domains, AccountEmail: accountEmail, Environment: environment, PreferredChain: s.preferredChain,
|
||||
})
|
||||
if issueErr != nil {
|
||||
s.invalidateDNSAuthorizationOnFailure(providerID, domains, credentials, issueErr)
|
||||
}
|
||||
if saveErr := s.accounts.Save(ctx, environment, user); saveErr != nil {
|
||||
if issueErr != nil {
|
||||
return SavedResource{}, fmt.Errorf("%v;保存 ACME 账户状态失败:%w", issueErr, saveErr)
|
||||
@@ -270,6 +280,39 @@ func (s *Service) verifyDNSAuthorization(ctx context.Context, providerID string,
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) invalidateDNSAuthorization(providerID string, domains []string, credentials map[string]string) {
|
||||
invalidator, ok := s.authorization.(DNSAuthorizationCacheInvalidator)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
for _, requestedDomain := range domains {
|
||||
invalidator.InvalidateDomain(providerID, requestedDomain, credentials)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) invalidateDNSAuthorizationOnFailure(providerID string, domains []string, credentials map[string]string, issueErr error) {
|
||||
if !looksLikeDNSAuthorizationFailure(issueErr) {
|
||||
return
|
||||
}
|
||||
s.invalidateDNSAuthorization(providerID, domains, credentials)
|
||||
}
|
||||
|
||||
func looksLikeDNSAuthorizationFailure(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
message := strings.ToLower(err.Error())
|
||||
for _, marker := range []string{
|
||||
"dns-01", "dns challenge", "txt record", "present", "cleanup", "clean up",
|
||||
"unauthorized", "forbidden", "permission", "accessdenied", "权限", "无权", "授权",
|
||||
} {
|
||||
if strings.Contains(message, marker) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *Service) Import(ctx context.Context, request ImportRequest) (IssueResult, error) {
|
||||
if s == nil || s.repository == nil {
|
||||
return IssueResult{}, errors.New("证书导入服务未初始化")
|
||||
|
||||
@@ -58,6 +58,20 @@ func (serviceProviderFactory) Create(string, map[string]string) (challenge.Provi
|
||||
return serviceProvider{}, nil
|
||||
}
|
||||
|
||||
type serviceAuthorizationChecker struct {
|
||||
verifyCalls int
|
||||
invalidateCalls int
|
||||
}
|
||||
|
||||
func (c *serviceAuthorizationChecker) VerifyDomain(context.Context, string, string, map[string]string) error {
|
||||
c.verifyCalls++
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *serviceAuthorizationChecker) InvalidateDomain(string, string, map[string]string) {
|
||||
c.invalidateCalls++
|
||||
}
|
||||
|
||||
type recordingProviderFactory struct {
|
||||
providerID string
|
||||
credentials map[string]string
|
||||
@@ -157,6 +171,50 @@ func TestServiceFailureKeepsExistingCertificate(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceInvalidatesDNSAuthorizationAfterACMEFailure(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
cfg := certificateTestConfig(root)
|
||||
item, err := domain.NewDomainWithOptions("example.com", domain.CertificateSingle, nil, true, domain.ACMEEnvironmentStaging, "cloudflare")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repository := &serviceRepository{item: item}
|
||||
issuer := &serviceIssuer{err: errors.New("模拟 DNS 权限已被收回")}
|
||||
authorization := &serviceAuthorizationChecker{}
|
||||
service := NewService(cfg, repository, serviceCredentials{}, serviceProviderFactory{}, serviceAccounts{})
|
||||
service.SetDNSAuthorizationChecker(authorization)
|
||||
service.newIssuer = func(*AccountUser, challenge.Provider, string) Issuer { return issuer }
|
||||
|
||||
if _, err := service.Issue(context.Background(), item.ID); err == nil {
|
||||
t.Fatal("ACME 申请失败时应返回错误")
|
||||
}
|
||||
if authorization.verifyCalls != 1 || authorization.invalidateCalls != 1 {
|
||||
t.Fatalf("ACME 失败后的授权缓存处理错误:verify=%d invalidate=%d", authorization.verifyCalls, authorization.invalidateCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceDoesNotInvalidateDNSAuthorizationForUnrelatedACMEFailure(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
cfg := certificateTestConfig(root)
|
||||
item, err := domain.NewDomainWithOptions("example.com", domain.CertificateSingle, nil, true, domain.ACMEEnvironmentStaging, "cloudflare")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repository := &serviceRepository{item: item}
|
||||
issuer := &serviceIssuer{err: errors.New("ACME 服务暂时不可用")}
|
||||
authorization := &serviceAuthorizationChecker{}
|
||||
service := NewService(cfg, repository, serviceCredentials{}, serviceProviderFactory{}, serviceAccounts{})
|
||||
service.SetDNSAuthorizationChecker(authorization)
|
||||
service.newIssuer = func(*AccountUser, challenge.Provider, string) Issuer { return issuer }
|
||||
|
||||
if _, err := service.Issue(context.Background(), item.ID); err == nil {
|
||||
t.Fatal("ACME 申请失败时应返回错误")
|
||||
}
|
||||
if authorization.invalidateCalls != 0 {
|
||||
t.Fatalf("无关的 ACME 错误不应清除授权缓存:invalidate=%d", authorization.invalidateCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceImportsOwnedCertificate(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
cfg := certificateTestConfig(root)
|
||||
|
||||
Reference in new issue
Block a user