Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --overwrite-domains flag to renew #2355

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions cmd/cmd_renew.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
flgReuseKey = "reuse-key"
flgRenewHook = "renew-hook"
flgNoRandomSleep = "no-random-sleep"
flgOverwriteDomains = "overwrite-domains"
)

const (
Expand Down Expand Up @@ -53,6 +54,9 @@ func createRenew() *cli.Command {
if !hasDomains && !hasCsr {
log.Fatal("Please specify --%s/-d (or --%s/-c if you already have a CSR)", flgDomains, flgCSR)
}
if ctx.Bool(flgOverwriteDomains) && hasCsr {
log.Fatal("--%s only works with --%s/-d, --%s/-c doesn't support this option.", flgOverwriteDomains, flgDomains, flgCSR)
}
return nil
},
Flags: []cli.Flag{
Expand Down Expand Up @@ -110,6 +114,10 @@ func createRenew() *cli.Command {
Usage: "Do not add a random sleep before the renewal." +
" We do not recommend using this flag if you are doing your renewals in an automated way.",
},
&cli.BoolFlag{
Name: flgOverwriteDomains,
Usage: "Check and enforce that the cert's domain list matches those passed in the domains argument.",
},
},
}
}
Expand Down Expand Up @@ -172,16 +180,19 @@ func renewForDomains(ctx *cli.Context, client *lego.Client, certsStorage *Certif
}
}

if ariRenewalTime == nil && !needRenewal(cert, domain, ctx.Int(flgDays)) {
overwriteDomains := ctx.Bool(flgOverwriteDomains)

certDomains := certcrypto.ExtractDomains(cert)

if ariRenewalTime == nil && !needRenewal(cert, domain, ctx.Int(flgDays)) &&
(!overwriteDomains || slices.Equal(certDomains, domains)) {
return nil
}

// This is just meant to be informal for the user.
timeLeft := cert.NotAfter.Sub(time.Now().UTC())
log.Infof("[%s] acme: Trying renewal with %d hours remaining", domain, int(timeLeft.Hours()))

certDomains := certcrypto.ExtractDomains(cert)

var privateKey crypto.PrivateKey
if ctx.Bool(flgReuseKey) {
keyBytes, errR := certsStorage.ReadFile(domain, keyExt)
Expand All @@ -207,8 +218,13 @@ func renewForDomains(ctx *cli.Context, client *lego.Client, certsStorage *Certif
time.Sleep(sleepTime)
}

renewalDomains := domains
if !overwriteDomains {
renewalDomains = merge(certDomains, domains)
}

request := certificate.ObtainRequest{
Domains: merge(certDomains, domains),
Domains: renewalDomains,
PrivateKey: privateKey,
MustStaple: ctx.Bool(flgMustStaple),
NotBefore: getTime(ctx, flgNotBefore),
Expand Down