2019-03-11 18:56:48 +02:00
|
|
|
package cmd
|
2018-12-06 23:50:17 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2021-08-25 11:44:11 +02:00
|
|
|
"os"
|
2018-12-06 23:50:17 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2020-09-02 03:20:01 +02:00
|
|
|
"github.com/go-acme/lego/v4/certcrypto"
|
2022-02-13 13:28:51 +02:00
|
|
|
"github.com/urfave/cli/v2"
|
2018-12-06 23:50:17 +02:00
|
|
|
)
|
|
|
|
|
2024-09-20 19:47:50 +02:00
|
|
|
const (
|
|
|
|
flgAccounts = "accounts"
|
|
|
|
flgNames = "names"
|
|
|
|
)
|
|
|
|
|
2022-02-13 13:28:51 +02:00
|
|
|
func createList() *cli.Command {
|
|
|
|
return &cli.Command{
|
2018-12-06 23:50:17 +02:00
|
|
|
Name: "list",
|
|
|
|
Usage: "Display certificates and accounts information.",
|
|
|
|
Action: list,
|
|
|
|
Flags: []cli.Flag{
|
2022-02-13 13:28:51 +02:00
|
|
|
&cli.BoolFlag{
|
2024-09-20 19:47:50 +02:00
|
|
|
Name: flgAccounts,
|
2022-02-13 13:28:51 +02:00
|
|
|
Aliases: []string{"a"},
|
|
|
|
Usage: "Display accounts.",
|
2018-12-06 23:50:17 +02:00
|
|
|
},
|
2022-02-13 13:28:51 +02:00
|
|
|
&cli.BoolFlag{
|
2024-09-20 19:47:50 +02:00
|
|
|
Name: flgNames,
|
2022-02-13 13:28:51 +02:00
|
|
|
Aliases: []string{"n"},
|
|
|
|
Usage: "Display certificate common names only.",
|
2020-03-11 13:17:50 +02:00
|
|
|
},
|
2023-06-22 23:20:36 +02:00
|
|
|
// fake email, needed by NewAccountsStorage
|
|
|
|
&cli.StringFlag{
|
2024-09-20 19:47:50 +02:00
|
|
|
Name: flgEmail,
|
2023-06-22 23:20:36 +02:00
|
|
|
Value: "unknown",
|
|
|
|
Hidden: true,
|
|
|
|
},
|
2018-12-06 23:50:17 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func list(ctx *cli.Context) error {
|
2024-09-20 19:47:50 +02:00
|
|
|
if ctx.Bool(flgAccounts) && !ctx.Bool(flgNames) {
|
2018-12-06 23:50:17 +02:00
|
|
|
if err := listAccount(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return listCertificates(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func listCertificates(ctx *cli.Context) error {
|
|
|
|
certsStorage := NewCertificatesStorage(ctx)
|
|
|
|
|
|
|
|
matches, err := filepath.Glob(filepath.Join(certsStorage.GetRootPath(), "*.crt"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-09-20 19:47:50 +02:00
|
|
|
names := ctx.Bool(flgNames)
|
2020-03-11 13:17:50 +02:00
|
|
|
|
2018-12-06 23:50:17 +02:00
|
|
|
if len(matches) == 0 {
|
2020-03-11 13:17:50 +02:00
|
|
|
if !names {
|
|
|
|
fmt.Println("No certificates found.")
|
|
|
|
}
|
2018-12-06 23:50:17 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-11 13:17:50 +02:00
|
|
|
if !names {
|
|
|
|
fmt.Println("Found the following certs:")
|
|
|
|
}
|
|
|
|
|
2018-12-06 23:50:17 +02:00
|
|
|
for _, filename := range matches {
|
2024-09-20 19:47:50 +02:00
|
|
|
if strings.HasSuffix(filename, issuerExt) {
|
2018-12-06 23:50:17 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-25 11:44:11 +02:00
|
|
|
data, err := os.ReadFile(filename)
|
2018-12-06 23:50:17 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pCert, err := certcrypto.ParsePEMCertificate(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-27 01:16:47 +02:00
|
|
|
name, err := certcrypto.GetCertificateMainDomain(pCert)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-11 13:17:50 +02:00
|
|
|
if names {
|
2024-01-27 01:16:47 +02:00
|
|
|
fmt.Println(name)
|
2020-03-11 13:17:50 +02:00
|
|
|
} else {
|
2024-01-27 01:16:47 +02:00
|
|
|
fmt.Println(" Certificate Name:", name)
|
2020-03-11 13:17:50 +02:00
|
|
|
fmt.Println(" Domains:", strings.Join(pCert.DNSNames, ", "))
|
|
|
|
fmt.Println(" Expiry Date:", pCert.NotAfter)
|
|
|
|
fmt.Println(" Certificate Path:", filename)
|
|
|
|
fmt.Println()
|
|
|
|
}
|
2018-12-06 23:50:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func listAccount(ctx *cli.Context) error {
|
|
|
|
accountsStorage := NewAccountsStorage(ctx)
|
|
|
|
|
|
|
|
matches, err := filepath.Glob(filepath.Join(accountsStorage.GetRootPath(), "*", "*", "*.json"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matches) == 0 {
|
|
|
|
fmt.Println("No accounts found.")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Found the following accounts:")
|
|
|
|
for _, filename := range matches {
|
2021-08-25 11:44:11 +02:00
|
|
|
data, err := os.ReadFile(filename)
|
2018-12-06 23:50:17 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var account Account
|
|
|
|
err = json.Unmarshal(data, &account)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
uri, err := url.Parse(account.Registration.URI)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(" Email:", account.Email)
|
|
|
|
fmt.Println(" Server:", uri.Host)
|
|
|
|
fmt.Println(" Path:", filepath.Dir(filename))
|
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|