2021-12-03 01:12:51 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2021-12-03 03:15:48 +00:00
|
|
|
"fmt"
|
2023-02-10 03:00:14 +00:00
|
|
|
"time"
|
2022-03-27 19:54:06 +00:00
|
|
|
|
2023-02-10 03:00:14 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
2021-12-03 01:12:51 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
|
2021-12-03 03:15:48 +00:00
|
|
|
"codeberg.org/codeberg/pages/server/database"
|
2021-12-03 01:12:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var Certs = &cli.Command{
|
2022-03-20 22:18:00 +00:00
|
|
|
Name: "certs",
|
|
|
|
Usage: "manage certs manually",
|
|
|
|
Subcommands: []*cli.Command{
|
2022-03-27 19:54:06 +00:00
|
|
|
{
|
2022-03-20 22:18:00 +00:00
|
|
|
Name: "list",
|
|
|
|
Usage: "list all certificates in the database",
|
|
|
|
Action: listCerts,
|
|
|
|
},
|
2022-03-27 19:54:06 +00:00
|
|
|
{
|
2022-03-20 22:18:00 +00:00
|
|
|
Name: "remove",
|
|
|
|
Usage: "remove a certificate from the database",
|
|
|
|
Action: removeCert,
|
|
|
|
},
|
2023-02-10 03:00:14 +00:00
|
|
|
{
|
|
|
|
Name: "migrate",
|
|
|
|
Usage: "migrate from \"pogreb\" driver to dbms driver",
|
|
|
|
Action: migrateCerts,
|
|
|
|
},
|
2022-03-20 22:18:00 +00:00
|
|
|
},
|
2023-02-10 03:00:14 +00:00
|
|
|
Flags: append(CertStorageFlags, []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "verbose",
|
|
|
|
Usage: "print trace info",
|
|
|
|
EnvVars: []string{"VERBOSE"},
|
|
|
|
Value: false,
|
|
|
|
},
|
|
|
|
}...),
|
2021-12-03 01:12:51 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 03:00:14 +00:00
|
|
|
func migrateCerts(ctx *cli.Context) error {
|
|
|
|
dbType := ctx.String("db-type")
|
|
|
|
if dbType == "" {
|
|
|
|
dbType = "sqlite3"
|
|
|
|
}
|
|
|
|
dbConn := ctx.String("db-conn")
|
|
|
|
dbPogrebConn := ctx.String("db-pogreb")
|
|
|
|
verbose := ctx.Bool("verbose")
|
|
|
|
|
|
|
|
log.Level(zerolog.InfoLevel)
|
|
|
|
if verbose {
|
|
|
|
log.Level(zerolog.TraceLevel)
|
|
|
|
}
|
|
|
|
|
|
|
|
xormDB, err := database.NewXormDB(dbType, dbConn)
|
2022-03-20 22:18:00 +00:00
|
|
|
if err != nil {
|
2023-02-10 03:00:14 +00:00
|
|
|
return fmt.Errorf("could not connect to database: %w", err)
|
2022-03-20 22:18:00 +00:00
|
|
|
}
|
2023-02-10 03:00:14 +00:00
|
|
|
defer xormDB.Close()
|
2021-12-03 01:12:51 +00:00
|
|
|
|
2023-02-10 03:00:14 +00:00
|
|
|
pogrebDB, err := database.NewPogreb(dbPogrebConn)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not open database: %w", err)
|
|
|
|
}
|
|
|
|
defer pogrebDB.Close()
|
|
|
|
|
|
|
|
fmt.Printf("Start migration from \"%s\" to \"%s:%s\" ...\n", dbPogrebConn, dbType, dbConn)
|
|
|
|
|
|
|
|
certs, err := pogrebDB.Items(0, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cert := range certs {
|
|
|
|
if err := xormDB.Put(cert.Domain, cert.Raw()); err != nil {
|
2022-03-20 22:18:00 +00:00
|
|
|
return err
|
2021-12-03 01:12:51 +00:00
|
|
|
}
|
2023-02-10 03:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("... done")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func listCerts(ctx *cli.Context) error {
|
|
|
|
certDB, closeFn, err := openCertDB(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closeFn()
|
|
|
|
|
|
|
|
items, err := certDB.Items(0, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Domain\tValidTill\n\n")
|
|
|
|
for _, cert := range items {
|
|
|
|
if cert.Domain[0] == '.' {
|
|
|
|
cert.Domain = "*" + cert.Domain
|
2021-12-03 01:12:51 +00:00
|
|
|
}
|
2023-02-10 03:00:14 +00:00
|
|
|
fmt.Printf("%s\t%s\n",
|
|
|
|
cert.Domain,
|
|
|
|
time.Unix(cert.ValidTill, 0).Format(time.RFC3339))
|
2022-03-20 22:18:00 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeCert(ctx *cli.Context) error {
|
|
|
|
if ctx.Args().Len() < 1 {
|
|
|
|
return fmt.Errorf("'certs remove' requires at least one domain as an argument")
|
|
|
|
}
|
|
|
|
|
|
|
|
domains := ctx.Args().Slice()
|
|
|
|
|
2023-02-10 03:00:14 +00:00
|
|
|
certDB, closeFn, err := openCertDB(ctx)
|
2022-03-20 22:18:00 +00:00
|
|
|
if err != nil {
|
2023-02-10 03:00:14 +00:00
|
|
|
return err
|
2022-03-20 22:18:00 +00:00
|
|
|
}
|
2023-02-10 03:00:14 +00:00
|
|
|
defer closeFn()
|
2022-03-20 22:18:00 +00:00
|
|
|
|
|
|
|
for _, domain := range domains {
|
|
|
|
fmt.Printf("Removing domain %s from the database...\n", domain)
|
2023-02-10 03:00:14 +00:00
|
|
|
if err := certDB.Delete(domain); err != nil {
|
2022-03-20 22:18:00 +00:00
|
|
|
return err
|
2021-12-03 01:12:51 +00:00
|
|
|
}
|
2022-03-20 22:18:00 +00:00
|
|
|
}
|
2021-12-03 01:12:51 +00:00
|
|
|
return nil
|
|
|
|
}
|