2019-03-11 16:56:48 +00:00
|
|
|
package cmd
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
import (
|
2021-10-21 18:36:11 +00:00
|
|
|
"github.com/go-acme/lego/v4/acme"
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/log"
|
2022-02-13 11:28:51 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2018-12-06 21:50:17 +00:00
|
|
|
)
|
|
|
|
|
2024-09-20 17:47:50 +00:00
|
|
|
// Flag names.
|
|
|
|
const (
|
|
|
|
flgKeep = "keep"
|
|
|
|
flgReason = "reason"
|
|
|
|
)
|
|
|
|
|
2022-02-13 11:28:51 +00:00
|
|
|
func createRevoke() *cli.Command {
|
|
|
|
return &cli.Command{
|
2018-12-06 21:50:17 +00:00
|
|
|
Name: "revoke",
|
|
|
|
Usage: "Revoke a certificate",
|
|
|
|
Action: revoke,
|
|
|
|
Flags: []cli.Flag{
|
2022-02-13 11:28:51 +00:00
|
|
|
&cli.BoolFlag{
|
2024-09-20 17:47:50 +00:00
|
|
|
Name: flgKeep,
|
2022-02-13 11:28:51 +00:00
|
|
|
Aliases: []string{"k"},
|
|
|
|
Usage: "Keep the certificates after the revocation instead of archiving them.",
|
2018-12-06 21:50:17 +00:00
|
|
|
},
|
2022-02-13 11:28:51 +00:00
|
|
|
&cli.UintFlag{
|
2024-09-20 17:47:50 +00:00
|
|
|
Name: flgReason,
|
2023-01-08 13:53:15 +00:00
|
|
|
Usage: "Identifies the reason for the certificate revocation." +
|
|
|
|
" See https://www.rfc-editor.org/rfc/rfc5280.html#section-5.3.1." +
|
|
|
|
" Valid values are:" +
|
|
|
|
" 0 (unspecified), 1 (keyCompromise), 2 (cACompromise), 3 (affiliationChanged)," +
|
|
|
|
" 4 (superseded), 5 (cessationOfOperation), 6 (certificateHold), 8 (removeFromCRL)," +
|
|
|
|
" 9 (privilegeWithdrawn), or 10 (aACompromise).",
|
2021-10-21 18:36:11 +00:00
|
|
|
Value: acme.CRLReasonUnspecified,
|
|
|
|
},
|
2018-12-06 21:50:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func revoke(ctx *cli.Context) error {
|
|
|
|
acc, client := setup(ctx, NewAccountsStorage(ctx))
|
|
|
|
|
|
|
|
if acc.Registration == nil {
|
|
|
|
log.Fatalf("Account %s is not registered. Use 'run' to register a new account.\n", acc.Email)
|
|
|
|
}
|
|
|
|
|
|
|
|
certsStorage := NewCertificatesStorage(ctx)
|
|
|
|
certsStorage.CreateRootFolder()
|
|
|
|
|
2024-09-20 17:47:50 +00:00
|
|
|
for _, domain := range ctx.StringSlice(flgDomains) {
|
2018-12-06 21:50:17 +00:00
|
|
|
log.Printf("Trying to revoke certificate for domain %s", domain)
|
|
|
|
|
2024-09-20 17:47:50 +00:00
|
|
|
certBytes, err := certsStorage.ReadFile(domain, certExt)
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error while revoking the certificate for domain %s\n\t%v", domain, err)
|
|
|
|
}
|
|
|
|
|
2024-09-20 17:47:50 +00:00
|
|
|
reason := ctx.Uint(flgReason)
|
2021-10-21 18:36:11 +00:00
|
|
|
|
|
|
|
err = client.Certificate.RevokeWithReason(certBytes, &reason)
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error while revoking the certificate for domain %s\n\t%v", domain, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Certificate was revoked.")
|
|
|
|
|
2024-09-20 17:47:50 +00:00
|
|
|
if ctx.Bool(flgKeep) {
|
2018-12-06 21:50:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
certsStorage.CreateArchiveFolder()
|
|
|
|
|
|
|
|
err = certsStorage.MoveToArchive(domain)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Certificate was archived for domain:", domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|