mirror of
https://github.com/go-acme/lego
synced 2026-03-14 14:35:48 +01:00
refactor: factorize code related to ARI
This commit is contained in:
parent
71f254d19e
commit
bc2cdbec81
1 changed files with 48 additions and 80 deletions
128
cmd/cmd_renew.go
128
cmd/cmd_renew.go
|
|
@ -117,41 +117,9 @@ func renewForDomains(ctx context.Context, cmd *cli.Command, lazyClient lzSetUp,
|
|||
|
||||
cert := certificates[0]
|
||||
|
||||
var (
|
||||
ariRenewalTime *time.Time
|
||||
replacesCertID string
|
||||
)
|
||||
|
||||
var client *lego.Client
|
||||
|
||||
if !cmd.Bool(flgARIDisable) {
|
||||
client, err = lazyClient()
|
||||
if err != nil {
|
||||
return fmt.Errorf("set up client: %w", err)
|
||||
}
|
||||
|
||||
willingToSleep := cmd.Duration(flgARIWaitToRenewDuration)
|
||||
|
||||
ariRenewalTime = getARIRenewalTime(ctx, willingToSleep, cert, domain, client)
|
||||
if ariRenewalTime != nil {
|
||||
now := time.Now().UTC()
|
||||
|
||||
// Figure out if we need to sleep before renewing.
|
||||
if ariRenewalTime.After(now) {
|
||||
log.Info("Sleeping until renewal time",
|
||||
log.DomainAttr(domain),
|
||||
slog.Duration("sleep", ariRenewalTime.Sub(now)),
|
||||
slog.Time("renewalTime", *ariRenewalTime),
|
||||
)
|
||||
|
||||
time.Sleep(ariRenewalTime.Sub(now))
|
||||
}
|
||||
}
|
||||
|
||||
replacesCertID, err = certificate.MakeARICertID(cert)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error while constructing the ARI CertID for domain %q: %w", domain, err)
|
||||
}
|
||||
ariRenewalTime, replacesCertID, err := getARIInfo(ctx, cmd, lazyClient, domain, cert)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
forceDomains := cmd.Bool(flgForceCertDomains)
|
||||
|
|
@ -165,11 +133,9 @@ func renewForDomains(ctx context.Context, cmd *cli.Command, lazyClient lzSetUp,
|
|||
return nil
|
||||
}
|
||||
|
||||
if client == nil {
|
||||
client, err = lazyClient()
|
||||
if err != nil {
|
||||
return fmt.Errorf("set up client: %w", err)
|
||||
}
|
||||
client, err := lazyClient()
|
||||
if err != nil {
|
||||
return fmt.Errorf("set up client: %w", err)
|
||||
}
|
||||
|
||||
// This is just meant to be informal for the user.
|
||||
|
|
@ -260,41 +226,9 @@ func renewForCSR(ctx context.Context, cmd *cli.Command, lazyClient lzSetUp, cert
|
|||
|
||||
cert := certificates[0]
|
||||
|
||||
var (
|
||||
ariRenewalTime *time.Time
|
||||
replacesCertID string
|
||||
)
|
||||
|
||||
var client *lego.Client
|
||||
|
||||
if !cmd.Bool(flgARIDisable) {
|
||||
client, err = lazyClient()
|
||||
if err != nil {
|
||||
return fmt.Errorf("set up client: %w", err)
|
||||
}
|
||||
|
||||
willingToSleep := cmd.Duration(flgARIWaitToRenewDuration)
|
||||
|
||||
ariRenewalTime = getARIRenewalTime(ctx, willingToSleep, cert, domain, client)
|
||||
if ariRenewalTime != nil {
|
||||
now := time.Now().UTC()
|
||||
|
||||
// Figure out if we need to sleep before renewing.
|
||||
if ariRenewalTime.After(now) {
|
||||
log.Info("Sleeping until renewal time",
|
||||
log.DomainAttr(domain),
|
||||
slog.Duration("sleep", ariRenewalTime.Sub(now)),
|
||||
slog.Time("renewalTime", *ariRenewalTime),
|
||||
)
|
||||
|
||||
time.Sleep(ariRenewalTime.Sub(now))
|
||||
}
|
||||
}
|
||||
|
||||
replacesCertID, err = certificate.MakeARICertID(cert)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error while constructing the ARI CertID for domain %q: %w", domain, err)
|
||||
}
|
||||
ariRenewalTime, replacesCertID, err := getARIInfo(ctx, cmd, lazyClient, domain, cert)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
days := getFlagRenewDays(cmd)
|
||||
|
|
@ -303,11 +237,9 @@ func renewForCSR(ctx context.Context, cmd *cli.Command, lazyClient lzSetUp, cert
|
|||
return nil
|
||||
}
|
||||
|
||||
if client == nil {
|
||||
client, err = lazyClient()
|
||||
if err != nil {
|
||||
return fmt.Errorf("set up client: %w", err)
|
||||
}
|
||||
client, err := lazyClient()
|
||||
if err != nil {
|
||||
return fmt.Errorf("set up client: %w", err)
|
||||
}
|
||||
|
||||
// This is just meant to be informal for the user.
|
||||
|
|
@ -406,6 +338,42 @@ func needRenewalDynamic(x509Cert *x509.Certificate, domain string, now time.Time
|
|||
return false
|
||||
}
|
||||
|
||||
func getARIInfo(ctx context.Context, cmd *cli.Command, lazyClient lzSetUp, domain string, cert *x509.Certificate) (*time.Time, string, error) {
|
||||
if cmd.Bool(flgARIDisable) {
|
||||
return nil, "", nil
|
||||
}
|
||||
|
||||
client, err := lazyClient()
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("set up client: %w", err)
|
||||
}
|
||||
|
||||
willingToSleep := cmd.Duration(flgARIWaitToRenewDuration)
|
||||
|
||||
ariRenewalTime := getARIRenewalTime(ctx, willingToSleep, cert, domain, client)
|
||||
if ariRenewalTime != nil {
|
||||
now := time.Now().UTC()
|
||||
|
||||
// Figure out if we need to sleep before renewing.
|
||||
if ariRenewalTime.After(now) {
|
||||
log.Info("Sleeping until renewal time",
|
||||
log.DomainAttr(domain),
|
||||
slog.Duration("sleep", ariRenewalTime.Sub(now)),
|
||||
slog.Time("renewalTime", *ariRenewalTime),
|
||||
)
|
||||
|
||||
time.Sleep(ariRenewalTime.Sub(now))
|
||||
}
|
||||
}
|
||||
|
||||
replacesCertID, err := certificate.MakeARICertID(cert)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("error while constructing the ARI CertID for domain %q: %w", domain, err)
|
||||
}
|
||||
|
||||
return ariRenewalTime, replacesCertID, nil
|
||||
}
|
||||
|
||||
// getARIRenewalTime checks if the certificate needs to be renewed using the renewalInfo endpoint.
|
||||
func getARIRenewalTime(ctx context.Context, willingToSleep time.Duration, cert *x509.Certificate, domain string, client *lego.Client) *time.Time {
|
||||
if cert.IsCA {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue