2019-03-11 16:56:48 +00:00
|
|
|
package cmd
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/url"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2019-07-30 19:19:32 +00:00
|
|
|
"github.com/go-acme/lego/v3/certcrypto"
|
2018-12-06 21:50:17 +00:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func createList() cli.Command {
|
|
|
|
return cli.Command{
|
|
|
|
Name: "list",
|
|
|
|
Usage: "Display certificates and accounts information.",
|
|
|
|
Action: list,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "accounts, a",
|
|
|
|
Usage: "Display accounts.",
|
|
|
|
},
|
2020-03-11 11:17:50 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "names, n",
|
|
|
|
Usage: "Display certificate common names only.",
|
|
|
|
},
|
2018-12-06 21:50:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func list(ctx *cli.Context) error {
|
2020-03-11 11:17:50 +00:00
|
|
|
if ctx.Bool("accounts") && !ctx.Bool("names") {
|
2018-12-06 21:50:17 +00: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
|
|
|
|
}
|
|
|
|
|
2020-03-11 11:17:50 +00:00
|
|
|
names := ctx.Bool("names")
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
if len(matches) == 0 {
|
2020-03-11 11:17:50 +00:00
|
|
|
if !names {
|
|
|
|
fmt.Println("No certificates found.")
|
|
|
|
}
|
2018-12-06 21:50:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-11 11:17:50 +00:00
|
|
|
if !names {
|
|
|
|
fmt.Println("Found the following certs:")
|
|
|
|
}
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
for _, filename := range matches {
|
|
|
|
if strings.HasSuffix(filename, ".issuer.crt") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pCert, err := certcrypto.ParsePEMCertificate(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-11 11:17:50 +00:00
|
|
|
if names {
|
|
|
|
fmt.Println(pCert.Subject.CommonName)
|
|
|
|
} else {
|
|
|
|
fmt.Println(" Certificate Name:", pCert.Subject.CommonName)
|
|
|
|
fmt.Println(" Domains:", strings.Join(pCert.DNSNames, ", "))
|
|
|
|
fmt.Println(" Expiry Date:", pCert.NotAfter)
|
|
|
|
fmt.Println(" Certificate Path:", filename)
|
|
|
|
fmt.Println()
|
|
|
|
}
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func listAccount(ctx *cli.Context) error {
|
|
|
|
// fake email, needed by NewAccountsStorage
|
|
|
|
if err := ctx.GlobalSet("email", "unknown"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
data, err := ioutil.ReadFile(filename)
|
|
|
|
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
|
|
|
|
}
|