2019-03-11 16:56:48 +00:00
|
|
|
package cmd
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto"
|
|
|
|
"crypto/x509"
|
2023-05-27 15:05:22 +00:00
|
|
|
"errors"
|
2022-06-15 07:08:24 +00:00
|
|
|
"math/rand"
|
2022-06-17 09:15:20 +00:00
|
|
|
"os"
|
2018-12-06 21:50:17 +00:00
|
|
|
"time"
|
|
|
|
|
2023-05-27 15:05:22 +00:00
|
|
|
"github.com/go-acme/lego/v4/acme/api"
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/certcrypto"
|
|
|
|
"github.com/go-acme/lego/v4/certificate"
|
|
|
|
"github.com/go-acme/lego/v4/lego"
|
|
|
|
"github.com/go-acme/lego/v4/log"
|
2022-06-17 09:15:20 +00:00
|
|
|
"github.com/mattn/go-isatty"
|
2022-02-13 11:28:51 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2018-12-06 21:50:17 +00:00
|
|
|
)
|
|
|
|
|
2020-04-11 12:57:06 +00:00
|
|
|
const (
|
|
|
|
renewEnvAccountEmail = "LEGO_ACCOUNT_EMAIL"
|
|
|
|
renewEnvCertDomain = "LEGO_CERT_DOMAIN"
|
|
|
|
renewEnvCertPath = "LEGO_CERT_PATH"
|
|
|
|
renewEnvCertKeyPath = "LEGO_CERT_KEY_PATH"
|
2022-03-21 08:36:28 +00:00
|
|
|
renewEnvCertPEMPath = "LEGO_CERT_PEM_PATH"
|
|
|
|
renewEnvCertPFXPath = "LEGO_CERT_PFX_PATH"
|
2020-04-11 12:57:06 +00:00
|
|
|
)
|
|
|
|
|
2022-02-13 11:28:51 +00:00
|
|
|
func createRenew() *cli.Command {
|
|
|
|
return &cli.Command{
|
2018-12-06 21:50:17 +00:00
|
|
|
Name: "renew",
|
|
|
|
Usage: "Renew a certificate",
|
|
|
|
Action: renew,
|
|
|
|
Before: func(ctx *cli.Context) error {
|
|
|
|
// we require either domains or csr, but not both
|
2022-02-13 11:28:51 +00:00
|
|
|
hasDomains := len(ctx.StringSlice("domains")) > 0
|
2024-02-08 02:16:48 +00:00
|
|
|
hasCsr := ctx.String("csr") != ""
|
2018-12-06 21:50:17 +00:00
|
|
|
if hasDomains && hasCsr {
|
|
|
|
log.Fatal("Please specify either --domains/-d or --csr/-c, but not both")
|
|
|
|
}
|
|
|
|
if !hasDomains && !hasCsr {
|
|
|
|
log.Fatal("Please specify --domains/-d (or --csr/-c if you already have a CSR)")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Flags: []cli.Flag{
|
2022-02-13 11:28:51 +00:00
|
|
|
&cli.IntFlag{
|
2018-12-06 21:50:17 +00:00
|
|
|
Name: "days",
|
2019-02-08 01:43:05 +00:00
|
|
|
Value: 30,
|
2018-12-06 21:50:17 +00:00
|
|
|
Usage: "The number of days left on a certificate to renew it.",
|
|
|
|
},
|
2023-05-27 15:05:22 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "ari-enable",
|
|
|
|
Usage: "Use the renewalInfo endpoint (draft-ietf-acme-ari) to check if a certificate should be renewed.",
|
|
|
|
},
|
|
|
|
&cli.DurationFlag{
|
|
|
|
Name: "ari-wait-to-renew-duration",
|
|
|
|
Usage: "The maximum duration you're willing to sleep for a renewal time returned by the renewalInfo endpoint.",
|
|
|
|
},
|
2022-02-13 11:28:51 +00:00
|
|
|
&cli.BoolFlag{
|
2018-12-06 21:50:17 +00:00
|
|
|
Name: "reuse-key",
|
|
|
|
Usage: "Used to indicate you want to reuse your current private key for the new certificate.",
|
|
|
|
},
|
2022-02-13 11:28:51 +00:00
|
|
|
&cli.BoolFlag{
|
2018-12-06 21:50:17 +00:00
|
|
|
Name: "no-bundle",
|
|
|
|
Usage: "Do not create a certificate bundle by adding the issuers certificate to the new certificate.",
|
|
|
|
},
|
2022-02-13 11:28:51 +00:00
|
|
|
&cli.BoolFlag{
|
2023-01-08 13:53:15 +00:00
|
|
|
Name: "must-staple",
|
|
|
|
Usage: "Include the OCSP must staple TLS extension in the CSR and generated certificate." +
|
|
|
|
" Only works if the CSR is generated by lego.",
|
2018-12-06 21:50:17 +00:00
|
|
|
},
|
2023-05-28 14:45:48 +00:00
|
|
|
&cli.TimestampFlag{
|
|
|
|
Name: "not-before",
|
|
|
|
Usage: "Set the notBefore field in the certificate (RFC3339 format)",
|
|
|
|
Layout: time.RFC3339,
|
|
|
|
},
|
|
|
|
&cli.TimestampFlag{
|
|
|
|
Name: "not-after",
|
|
|
|
Usage: "Set the notAfter field in the certificate (RFC3339 format)",
|
|
|
|
Layout: time.RFC3339,
|
2019-04-02 16:38:23 +00:00
|
|
|
},
|
2022-02-13 11:28:51 +00:00
|
|
|
&cli.StringFlag{
|
2023-01-08 13:53:15 +00:00
|
|
|
Name: "preferred-chain",
|
|
|
|
Usage: "If the CA offers multiple certificate chains, prefer the chain with an issuer matching this Subject Common Name." +
|
|
|
|
" If no match, the default offered chain will be used.",
|
2020-09-02 00:22:53 +00:00
|
|
|
},
|
2022-02-13 11:28:51 +00:00
|
|
|
&cli.StringFlag{
|
2021-09-08 21:14:21 +00:00
|
|
|
Name: "always-deactivate-authorizations",
|
|
|
|
Usage: "Force the authorizations to be relinquished even if the certificate request was successful.",
|
|
|
|
},
|
2023-05-28 14:45:48 +00:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "renew-hook",
|
|
|
|
Usage: "Define a hook. The hook is executed only when the certificates are effectively renewed.",
|
|
|
|
},
|
2022-06-15 07:08:24 +00:00
|
|
|
&cli.BoolFlag{
|
2023-01-08 13:53:15 +00:00
|
|
|
Name: "no-random-sleep",
|
|
|
|
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.",
|
2022-06-15 07:08:24 +00:00
|
|
|
},
|
2018-12-06 21:50:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func renew(ctx *cli.Context) error {
|
|
|
|
account, client := setup(ctx, NewAccountsStorage(ctx))
|
2019-03-11 15:54:35 +00:00
|
|
|
setupChallenges(ctx, client)
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
if account.Registration == nil {
|
|
|
|
log.Fatalf("Account %s is not registered. Use 'run' to register a new account.\n", account.Email)
|
|
|
|
}
|
|
|
|
|
|
|
|
certsStorage := NewCertificatesStorage(ctx)
|
|
|
|
|
|
|
|
bundle := !ctx.Bool("no-bundle")
|
|
|
|
|
2020-04-11 12:57:06 +00:00
|
|
|
meta := map[string]string{renewEnvAccountEmail: account.Email}
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
// CSR
|
2022-02-13 11:28:51 +00:00
|
|
|
if ctx.IsSet("csr") {
|
2020-04-11 12:57:06 +00:00
|
|
|
return renewForCSR(ctx, client, certsStorage, bundle, meta)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Domains
|
2020-04-11 12:57:06 +00:00
|
|
|
return renewForDomains(ctx, client, certsStorage, bundle, meta)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 12:57:06 +00:00
|
|
|
func renewForDomains(ctx *cli.Context, client *lego.Client, certsStorage *CertificatesStorage, bundle bool, meta map[string]string) error {
|
2022-02-13 11:28:51 +00:00
|
|
|
domains := ctx.StringSlice("domains")
|
2018-12-06 21:50:17 +00:00
|
|
|
domain := domains[0]
|
|
|
|
|
|
|
|
// load the cert resource from files.
|
|
|
|
// We store the certificate, private key and metadata in different files
|
|
|
|
// as web servers would not be able to work with a combined file.
|
|
|
|
certificates, err := certsStorage.ReadCertificate(domain, ".crt")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error while loading the certificate for domain %s\n\t%v", domain, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cert := certificates[0]
|
|
|
|
|
2023-05-27 15:05:22 +00:00
|
|
|
var ariRenewalTime *time.Time
|
|
|
|
if ctx.Bool("ari-enable") {
|
2024-01-24 21:02:50 +00:00
|
|
|
ariRenewalTime = getARIRenewalTime(ctx, cert, domain, client)
|
2023-05-27 15:05:22 +00:00
|
|
|
if ariRenewalTime != nil {
|
|
|
|
now := time.Now().UTC()
|
|
|
|
// Figure out if we need to sleep before renewing.
|
|
|
|
if ariRenewalTime.After(now) {
|
|
|
|
log.Infof("[%s] Sleeping %s until renewal time %s", domain, ariRenewalTime.Sub(now), ariRenewalTime)
|
|
|
|
time.Sleep(ariRenewalTime.Sub(now))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ariRenewalTime == nil && !needRenewal(cert, domain, ctx.Int("days")) {
|
2018-12-06 21:50:17 +00:00
|
|
|
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("reuse-key") {
|
|
|
|
keyBytes, errR := certsStorage.ReadFile(domain, ".key")
|
|
|
|
if errR != nil {
|
|
|
|
log.Fatalf("Error while loading the private key for domain %s\n\t%v", domain, errR)
|
|
|
|
}
|
|
|
|
|
|
|
|
privateKey, errR = certcrypto.ParsePEMPrivateKey(keyBytes)
|
|
|
|
if errR != nil {
|
|
|
|
return errR
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-17 09:15:20 +00:00
|
|
|
// https://github.com/go-acme/lego/issues/1656
|
|
|
|
// https://github.com/certbot/certbot/blob/284023a1b7672be2bd4018dd7623b3b92197d4b0/certbot/certbot/_internal/renewal.py#L435-L440
|
|
|
|
if !isatty.IsTerminal(os.Stdout.Fd()) && !ctx.Bool("no-random-sleep") {
|
2022-06-15 07:08:24 +00:00
|
|
|
// https://github.com/certbot/certbot/blob/284023a1b7672be2bd4018dd7623b3b92197d4b0/certbot/certbot/_internal/renewal.py#L472
|
|
|
|
const jitter = 8 * time.Minute
|
|
|
|
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
sleepTime := time.Duration(rnd.Int63n(int64(jitter)))
|
|
|
|
|
|
|
|
log.Infof("renewal: random delay of %s", sleepTime)
|
|
|
|
time.Sleep(sleepTime)
|
|
|
|
}
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
request := certificate.ObtainRequest{
|
2021-09-08 21:14:21 +00:00
|
|
|
Domains: merge(certDomains, domains),
|
|
|
|
PrivateKey: privateKey,
|
|
|
|
MustStaple: ctx.Bool("must-staple"),
|
2023-05-28 14:45:48 +00:00
|
|
|
NotBefore: getTime(ctx, "not-before"),
|
|
|
|
NotAfter: getTime(ctx, "not-after"),
|
|
|
|
Bundle: bundle,
|
2021-09-08 21:14:21 +00:00
|
|
|
PreferredChain: ctx.String("preferred-chain"),
|
|
|
|
AlwaysDeactivateAuthorizations: ctx.Bool("always-deactivate-authorizations"),
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
2023-05-28 14:45:48 +00:00
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
certRes, err := client.Certificate.Obtain(request)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
certsStorage.SaveResource(certRes)
|
|
|
|
|
2023-05-27 15:05:22 +00:00
|
|
|
if ariRenewalTime != nil {
|
|
|
|
// Post to the renewalInfo endpoint to indicate that we have renewed and replaced the certificate.
|
2024-01-24 21:02:50 +00:00
|
|
|
err := client.Certificate.UpdateRenewalInfo(certificate.RenewalInfoRequest{Cert: certificates[0]})
|
2023-05-27 15:05:22 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Warnf("[%s] Failed to update renewal info: %v", domain, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 12:57:06 +00:00
|
|
|
meta[renewEnvCertDomain] = domain
|
2020-05-11 08:29:39 +00:00
|
|
|
meta[renewEnvCertPath] = certsStorage.GetFileName(domain, ".crt")
|
|
|
|
meta[renewEnvCertKeyPath] = certsStorage.GetFileName(domain, ".key")
|
2022-03-21 08:36:28 +00:00
|
|
|
meta[renewEnvCertPEMPath] = certsStorage.GetFileName(domain, ".pem")
|
|
|
|
meta[renewEnvCertPFXPath] = certsStorage.GetFileName(domain, ".pfx")
|
2020-04-11 12:57:06 +00:00
|
|
|
|
2020-05-14 21:44:08 +00:00
|
|
|
return launchHook(ctx.String("renew-hook"), meta)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 12:57:06 +00:00
|
|
|
func renewForCSR(ctx *cli.Context, client *lego.Client, certsStorage *CertificatesStorage, bundle bool, meta map[string]string) error {
|
2022-02-13 11:28:51 +00:00
|
|
|
csr, err := readCSRFile(ctx.String("csr"))
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-01-26 23:16:47 +00:00
|
|
|
domain, err := certcrypto.GetCSRMainDomain(csr)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error: %v", err)
|
|
|
|
}
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
// load the cert resource from files.
|
|
|
|
// We store the certificate, private key and metadata in different files
|
|
|
|
// as web servers would not be able to work with a combined file.
|
|
|
|
certificates, err := certsStorage.ReadCertificate(domain, ".crt")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error while loading the certificate for domain %s\n\t%v", domain, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cert := certificates[0]
|
|
|
|
|
2023-05-27 15:05:22 +00:00
|
|
|
var ariRenewalTime *time.Time
|
|
|
|
if ctx.Bool("ari-enable") {
|
2024-01-24 21:02:50 +00:00
|
|
|
ariRenewalTime = getARIRenewalTime(ctx, cert, domain, client)
|
2023-05-27 15:05:22 +00:00
|
|
|
if ariRenewalTime != nil {
|
|
|
|
now := time.Now().UTC()
|
|
|
|
// Figure out if we need to sleep before renewing.
|
|
|
|
if ariRenewalTime.After(now) {
|
|
|
|
log.Infof("[%s] Sleeping %s until renewal time %s", domain, ariRenewalTime.Sub(now), ariRenewalTime)
|
|
|
|
time.Sleep(ariRenewalTime.Sub(now))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ariRenewalTime == nil && !needRenewal(cert, domain, ctx.Int("days")) {
|
2018-12-06 21:50:17 +00:00
|
|
|
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()))
|
|
|
|
|
2023-05-28 14:45:48 +00:00
|
|
|
request := certificate.ObtainForCSRRequest{
|
2021-09-08 21:14:21 +00:00
|
|
|
CSR: csr,
|
2023-05-28 14:45:48 +00:00
|
|
|
NotBefore: getTime(ctx, "not-before"),
|
|
|
|
NotAfter: getTime(ctx, "not-after"),
|
2021-09-08 21:14:21 +00:00
|
|
|
Bundle: bundle,
|
|
|
|
PreferredChain: ctx.String("preferred-chain"),
|
|
|
|
AlwaysDeactivateAuthorizations: ctx.Bool("always-deactivate-authorizations"),
|
2023-05-28 14:45:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
certRes, err := client.Certificate.ObtainForCSR(request)
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
certsStorage.SaveResource(certRes)
|
|
|
|
|
2023-05-27 15:05:22 +00:00
|
|
|
if ariRenewalTime != nil {
|
|
|
|
// Post to the renewalInfo endpoint to indicate that we have renewed and replaced the certificate.
|
2024-01-24 21:02:50 +00:00
|
|
|
err := client.Certificate.UpdateRenewalInfo(certificate.RenewalInfoRequest{Cert: certificates[0]})
|
2023-05-27 15:05:22 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Warnf("[%s] Failed to update renewal info: %v", domain, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 12:57:06 +00:00
|
|
|
meta[renewEnvCertDomain] = domain
|
2020-05-11 08:29:39 +00:00
|
|
|
meta[renewEnvCertPath] = certsStorage.GetFileName(domain, ".crt")
|
|
|
|
meta[renewEnvCertKeyPath] = certsStorage.GetFileName(domain, ".key")
|
2020-04-11 12:57:06 +00:00
|
|
|
|
2020-05-14 21:44:08 +00:00
|
|
|
return launchHook(ctx.String("renew-hook"), meta)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func needRenewal(x509Cert *x509.Certificate, domain string, days int) bool {
|
|
|
|
if x509Cert.IsCA {
|
|
|
|
log.Fatalf("[%s] Certificate bundle starts with a CA certificate", domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
if days >= 0 {
|
2019-03-11 16:08:48 +00:00
|
|
|
notAfter := int(time.Until(x509Cert.NotAfter).Hours() / 24.0)
|
|
|
|
if notAfter > days {
|
|
|
|
log.Printf("[%s] The certificate expires in %d days, the number of days defined to perform the renewal is %d: no renewal.",
|
|
|
|
domain, notAfter, days)
|
2018-12-06 21:50:17 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-05-27 15:05:22 +00:00
|
|
|
// getARIRenewalTime checks if the certificate needs to be renewed using the renewalInfo endpoint.
|
2024-01-24 21:02:50 +00:00
|
|
|
func getARIRenewalTime(ctx *cli.Context, cert *x509.Certificate, domain string, client *lego.Client) *time.Time {
|
2023-05-27 15:05:22 +00:00
|
|
|
if cert.IsCA {
|
|
|
|
log.Fatalf("[%s] Certificate bundle starts with a CA certificate", domain)
|
|
|
|
}
|
|
|
|
|
2024-01-24 21:02:50 +00:00
|
|
|
renewalInfo, err := client.Certificate.GetRenewalInfo(certificate.RenewalInfoRequest{Cert: cert})
|
2023-05-27 15:05:22 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, api.ErrNoARI) {
|
|
|
|
// The server does not advertise a renewal info endpoint.
|
2024-01-24 21:02:50 +00:00
|
|
|
log.Warnf("[%s] acme: %v", domain, err)
|
2023-05-27 15:05:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-01-24 21:02:50 +00:00
|
|
|
log.Warnf("[%s] acme: calling renewal info endpoint: %v", domain, err)
|
2023-05-27 15:05:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now().UTC()
|
|
|
|
renewalTime := renewalInfo.ShouldRenewAt(now, ctx.Duration("ari-wait-to-renew-duration"))
|
|
|
|
if renewalTime == nil {
|
|
|
|
log.Infof("[%s] acme: renewalInfo endpoint indicates that renewal is not needed", domain)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
log.Infof("[%s] acme: renewalInfo endpoint indicates that renewal is needed", domain)
|
|
|
|
|
|
|
|
if renewalInfo.ExplanationURL != "" {
|
|
|
|
log.Infof("[%s] acme: renewalInfo endpoint provided an explanation: %s", domain, renewalInfo.ExplanationURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
return renewalTime
|
|
|
|
}
|
|
|
|
|
2020-07-09 23:48:18 +00:00
|
|
|
func merge(prevDomains, nextDomains []string) []string {
|
2018-12-06 21:50:17 +00:00
|
|
|
for _, next := range nextDomains {
|
|
|
|
var found bool
|
|
|
|
for _, prev := range prevDomains {
|
|
|
|
if prev == next {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
prevDomains = append(prevDomains, next)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return prevDomains
|
|
|
|
}
|