2019-03-11 16:56:48 +00:00
|
|
|
package cmd
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2023-05-28 14:45:48 +00:00
|
|
|
"time"
|
2018-12-06 21:50:17 +00:00
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/certificate"
|
|
|
|
"github.com/go-acme/lego/v4/lego"
|
|
|
|
"github.com/go-acme/lego/v4/log"
|
|
|
|
"github.com/go-acme/lego/v4/registration"
|
2022-02-13 11:28:51 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2018-12-06 21:50:17 +00:00
|
|
|
)
|
|
|
|
|
2022-02-13 11:28:51 +00:00
|
|
|
func createRun() *cli.Command {
|
|
|
|
return &cli.Command{
|
2018-12-06 21:50:17 +00:00
|
|
|
Name: "run",
|
|
|
|
Usage: "Register an account, then create and install a certificate",
|
|
|
|
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
|
|
|
|
},
|
|
|
|
Action: run,
|
|
|
|
Flags: []cli.Flag{
|
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,
|
2020-05-14 21:44:08 +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: "run-hook",
|
|
|
|
Usage: "Define a hook. The hook is executed when the certificates are effectively created.",
|
|
|
|
},
|
2018-12-06 21:50:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 17:23:17 +00:00
|
|
|
const rootPathWarningMessage = `!!!! HEADS UP !!!!
|
|
|
|
|
|
|
|
Your account credentials have been saved in your Let's Encrypt
|
|
|
|
configuration directory at "%s".
|
|
|
|
|
|
|
|
You should make a secure backup of this folder now. This
|
|
|
|
configuration directory will also contain certificates and
|
|
|
|
private keys obtained from Let's Encrypt so making regular
|
|
|
|
backups of this folder is ideal.
|
|
|
|
`
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
func run(ctx *cli.Context) error {
|
|
|
|
accountsStorage := NewAccountsStorage(ctx)
|
|
|
|
|
|
|
|
account, client := setup(ctx, accountsStorage)
|
2019-03-11 15:54:35 +00:00
|
|
|
setupChallenges(ctx, client)
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
if account.Registration == nil {
|
|
|
|
reg, err := register(ctx, client)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not complete registration\n\t%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
account.Registration = reg
|
|
|
|
if err = accountsStorage.Save(account); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-04-27 17:23:17 +00:00
|
|
|
fmt.Printf(rootPathWarningMessage, accountsStorage.GetRootPath())
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
certsStorage := NewCertificatesStorage(ctx)
|
|
|
|
certsStorage.CreateRootFolder()
|
|
|
|
|
|
|
|
cert, err := obtainCertificate(ctx, client)
|
|
|
|
if err != nil {
|
|
|
|
// Make sure to return a non-zero exit code if ObtainSANCertificate returned at least one error.
|
|
|
|
// Due to us not returning partial certificate we can just exit here instead of at the end.
|
|
|
|
log.Fatalf("Could not obtain certificates:\n\t%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
certsStorage.SaveResource(cert)
|
|
|
|
|
2020-05-14 21:44:08 +00:00
|
|
|
meta := map[string]string{
|
|
|
|
renewEnvAccountEmail: account.Email,
|
|
|
|
renewEnvCertDomain: cert.Domain,
|
|
|
|
renewEnvCertPath: certsStorage.GetFileName(cert.Domain, ".crt"),
|
|
|
|
renewEnvCertKeyPath: certsStorage.GetFileName(cert.Domain, ".key"),
|
2023-05-02 16:33:13 +00:00
|
|
|
renewEnvCertPEMPath: certsStorage.GetFileName(cert.Domain, ".pem"),
|
|
|
|
renewEnvCertPFXPath: certsStorage.GetFileName(cert.Domain, ".pfx"),
|
2020-05-14 21:44:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return launchHook(ctx.String("run-hook"), meta)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleTOS(ctx *cli.Context, client *lego.Client) bool {
|
|
|
|
// Check for a global accept override
|
2022-02-13 11:28:51 +00:00
|
|
|
if ctx.Bool("accept-tos") {
|
2018-12-06 21:50:17 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
log.Printf("Please review the TOS at %s", client.GetToSURL())
|
|
|
|
|
|
|
|
for {
|
|
|
|
fmt.Println("Do you accept the TOS? Y/n")
|
|
|
|
text, err := reader.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not read from console: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
text = strings.Trim(text, "\r\n")
|
|
|
|
switch text {
|
|
|
|
case "", "y", "Y":
|
|
|
|
return true
|
|
|
|
case "n", "N":
|
2019-02-16 13:56:15 +00:00
|
|
|
return false
|
2018-12-06 21:50:17 +00:00
|
|
|
default:
|
|
|
|
fmt.Println("Your input was invalid. Please answer with one of Y/y, n/N or by pressing enter.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func register(ctx *cli.Context, client *lego.Client) (*registration.Resource, error) {
|
|
|
|
accepted := handleTOS(ctx, client)
|
|
|
|
if !accepted {
|
|
|
|
log.Fatal("You did not accept the TOS. Unable to proceed.")
|
|
|
|
}
|
|
|
|
|
2022-02-13 11:28:51 +00:00
|
|
|
if ctx.Bool("eab") {
|
|
|
|
kid := ctx.String("kid")
|
|
|
|
hmacEncoded := ctx.String("hmac")
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
if kid == "" || hmacEncoded == "" {
|
|
|
|
log.Fatalf("Requires arguments --kid and --hmac.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.Registration.RegisterWithExternalAccountBinding(registration.RegisterEABOptions{
|
|
|
|
TermsOfServiceAgreed: accepted,
|
|
|
|
Kid: kid,
|
|
|
|
HmacEncoded: hmacEncoded,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
|
|
|
|
}
|
|
|
|
|
|
|
|
func obtainCertificate(ctx *cli.Context, client *lego.Client) (*certificate.Resource, error) {
|
|
|
|
bundle := !ctx.Bool("no-bundle")
|
|
|
|
|
2022-02-13 11:28:51 +00:00
|
|
|
domains := ctx.StringSlice("domains")
|
2018-12-06 21:50:17 +00:00
|
|
|
if len(domains) > 0 {
|
|
|
|
// obtain a certificate, generating a new private key
|
|
|
|
request := certificate.ObtainRequest{
|
2021-09-08 21:14:21 +00:00
|
|
|
Domains: domains,
|
|
|
|
Bundle: bundle,
|
|
|
|
MustStaple: ctx.Bool("must-staple"),
|
|
|
|
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
|
|
|
|
|
|
|
notBefore := ctx.Timestamp("not-before")
|
|
|
|
if notBefore != nil {
|
|
|
|
request.NotBefore = *notBefore
|
|
|
|
}
|
|
|
|
|
|
|
|
notAfter := ctx.Timestamp("not-after")
|
|
|
|
if notAfter != nil {
|
|
|
|
request.NotAfter = *notAfter
|
|
|
|
}
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
return client.Certificate.Obtain(request)
|
|
|
|
}
|
|
|
|
|
|
|
|
// read the CSR
|
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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// obtain a certificate for this CSR
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return client.Certificate.ObtainForCSR(request)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|