func (a *ACME) renewCertificates(client *acme.Client, account *Account) error { for _, certificateResource := range account.DomainsCertificate.Certs { if certificateResource.needRenew() { log.Debugf("Renewing certificate %+v", certificateResource.Domains) renewedCert, err := client.RenewCertificate(acme.CertificateResource{ Domain: certificateResource.Certificate.Domain, CertURL: certificateResource.Certificate.CertURL, CertStableURL: certificateResource.Certificate.CertStableURL, PrivateKey: certificateResource.Certificate.PrivateKey, Certificate: certificateResource.Certificate.Certificate, }, false) if err != nil { return err } log.Debugf("Renewed certificate %+v", certificateResource.Domains) renewedACMECert := &Certificate{ Domain: renewedCert.Domain, CertURL: renewedCert.CertURL, CertStableURL: renewedCert.CertStableURL, PrivateKey: renewedCert.PrivateKey, Certificate: renewedCert.Certificate, } err = account.DomainsCertificate.renewCertificates(renewedACMECert, certificateResource.Domains) if err != nil { return err } if err = a.saveAccount(account); err != nil { return err } } } return nil }
// renewCertificates loops through all configured site and // looks for certificates to renew. Nothing is mutated // through this function; all changes happen directly on disk. // It returns the number of certificates renewed and any errors // that occurred. It only performs a renewal if necessary. // If useCustomPort is true, a custom port will be used, and // whatever is listening at 443 better proxy ACME requests to it. // Otherwise, the acme package will create its own listener on 443. func renewCertificates(configs []server.Config, useCustomPort bool) (int, []error) { log.Printf("[INFO] Checking certificates for %d hosts", len(configs)) var errs []error var n int for _, cfg := range configs { // Host must be TLS-enabled and have existing assets managed by LE if !cfg.TLS.Enabled || !existingCertAndKey(cfg.Host) { continue } // Read the certificate and get the NotAfter time. certBytes, err := ioutil.ReadFile(storage.SiteCertFile(cfg.Host)) if err != nil { errs = append(errs, err) continue // still have to check other certificates } expTime, err := acme.GetPEMCertExpiration(certBytes) if err != nil { errs = append(errs, err) continue } // The time returned from the certificate is always in UTC. // So calculate the time left with local time as UTC. // Directly convert it to days for the following checks. daysLeft := int(expTime.Sub(time.Now().UTC()).Hours() / 24) // Renew with two weeks or less remaining. if daysLeft <= 14 { log.Printf("[INFO] Certificate for %s has %d days remaining; attempting renewal", cfg.Host, daysLeft) var client *acme.Client if useCustomPort { client, err = newClientPort("", alternatePort) // email not used for renewal } else { client, err = newClient("") } if err != nil { errs = append(errs, err) continue } // Read and set up cert meta, required for renewal metaBytes, err := ioutil.ReadFile(storage.SiteMetaFile(cfg.Host)) if err != nil { errs = append(errs, err) continue } privBytes, err := ioutil.ReadFile(storage.SiteKeyFile(cfg.Host)) if err != nil { errs = append(errs, err) continue } var certMeta acme.CertificateResource err = json.Unmarshal(metaBytes, &certMeta) certMeta.Certificate = certBytes certMeta.PrivateKey = privBytes // Renew certificate Renew: newCertMeta, err := client.RenewCertificate(certMeta, true, true) if err != nil { if _, ok := err.(acme.TOSError); ok { err := client.AgreeToTOS() if err != nil { errs = append(errs, err) } goto Renew } time.Sleep(10 * time.Second) newCertMeta, err = client.RenewCertificate(certMeta, true, true) if err != nil { errs = append(errs, err) continue } } saveCertsAndKeys([]acme.CertificateResource{newCertMeta}) n++ } else if daysLeft <= 30 { // Warn on 30 days remaining. TODO: Just do this once... log.Printf("[WARNING] Certificate for %s has %d days remaining; will automatically renew when 14 days remain\n", cfg.Host, daysLeft) } } return n, errs }