@@ -393,12 +393,35 @@ func (am *ACMEIssuer) doIssue(ctx context.Context, csr *x509.CertificateRequest,
393
393
}
394
394
}
395
395
396
- certChains , err := client .acmeClient .ObtainCertificateUsingCSR (ctx , client .account , csr )
397
- if err != nil {
398
- return nil , usingTestCA , fmt .Errorf ("%v %w (ca=%s)" , nameSet , err , client .acmeClient .Directory )
399
- }
400
- if len (certChains ) == 0 {
401
- return nil , usingTestCA , fmt .Errorf ("no certificate chains" )
396
+ // do this in a loop because there's an error case that may necessitate a retry, but not more than once
397
+ var certChains []acme.Certificate
398
+ for i := 0 ; i < 2 ; i ++ {
399
+ certChains , err = client .acmeClient .ObtainCertificateUsingCSR (ctx , client .account , csr )
400
+ if err != nil {
401
+ var prob acme.Problem
402
+ if errors .As (err , & prob ) && prob .Type == acme .ProblemTypeAccountDoesNotExist {
403
+ // the account we have no longer exists on the CA, so we need to create a new one;
404
+ // we could use the same key pair, but this is a good opportunity to rotate keys
405
+ // (see https://caddy.community/t/acme-account-is-not-regenerated-when-acme-server-gets-reinstalled/22627)
406
+ // (basically this happens if the CA gets reset or reinstalled; usually just internal PKI)
407
+ err := am .deleteAccountLocally (ctx , client .iss .CA , client .account )
408
+ if err != nil {
409
+ return nil , usingTestCA , fmt .Errorf ("%v ACME account no longer exists on CA, but resetting our local copy of the account info failed: %v" , nameSet , err )
410
+ }
411
+
412
+ // recreate account and try again
413
+ client , err = am .newACMEClientWithAccount (ctx , useTestCA , false )
414
+ if err != nil {
415
+ return nil , false , err
416
+ }
417
+ continue
418
+ }
419
+ return nil , usingTestCA , fmt .Errorf ("%v %w (ca=%s)" , nameSet , err , client .acmeClient .Directory )
420
+ }
421
+ if len (certChains ) == 0 {
422
+ return nil , usingTestCA , fmt .Errorf ("no certificate chains" )
423
+ }
424
+ break
402
425
}
403
426
404
427
preferredChain := am .selectPreferredChain (certChains )
0 commit comments