feat: adds an option to remove sub-directories.
This commit is contained in:
parent
7e63c722b8
commit
b608309b9e
24
dumper.go
24
dumper.go
@ -11,6 +11,11 @@ import (
|
||||
"github.com/xenolf/lego/registration"
|
||||
)
|
||||
|
||||
const (
|
||||
certsSubDir = "certs"
|
||||
keysSubDir = "private"
|
||||
)
|
||||
|
||||
// StoredData represents the data managed by the Store
|
||||
type StoredData struct {
|
||||
Account *Account
|
||||
@ -40,7 +45,7 @@ type Account struct {
|
||||
KeyType certcrypto.KeyType
|
||||
}
|
||||
|
||||
func dump(acmeFile, dumpPath string, crtExt, keyExt string) error {
|
||||
func dump(acmeFile, dumpPath string, crtExt, keyExt string, subDir bool) error {
|
||||
f, err := os.Open(acmeFile)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -55,29 +60,29 @@ func dump(acmeFile, dumpPath string, crtExt, keyExt string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
err = os.MkdirAll(filepath.Join(dumpPath, "certs"), 0755)
|
||||
err = os.MkdirAll(filepath.Join(dumpPath, withSubDir(subDir, certsSubDir)), 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = os.MkdirAll(filepath.Join(dumpPath, "private"), 0755)
|
||||
err = os.MkdirAll(filepath.Join(dumpPath, withSubDir(subDir, keysSubDir)), 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
privateKeyPem := extractPEMPrivateKey(data.Account)
|
||||
err = ioutil.WriteFile(filepath.Join(dumpPath, "private", "letsencrypt"+keyExt), privateKeyPem, 0666)
|
||||
err = ioutil.WriteFile(filepath.Join(dumpPath, withSubDir(subDir, keysSubDir), "letsencrypt"+keyExt), privateKeyPem, 0666)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, cert := range data.Certificates {
|
||||
err = ioutil.WriteFile(filepath.Join(dumpPath, "private", cert.Domain.Main+keyExt), cert.Key, 0666)
|
||||
err = ioutil.WriteFile(filepath.Join(dumpPath, withSubDir(subDir, keysSubDir), cert.Domain.Main+keyExt), cert.Key, 0666)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(filepath.Join(dumpPath, "certs", cert.Domain.Main+crtExt), cert.Certificate, 0666)
|
||||
err = ioutil.WriteFile(filepath.Join(dumpPath, withSubDir(subDir, certsSubDir), cert.Domain.Main+crtExt), cert.Certificate, 0666)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -86,6 +91,13 @@ func dump(acmeFile, dumpPath string, crtExt, keyExt string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func withSubDir(sub bool, name string) string {
|
||||
if sub {
|
||||
return name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func extractPEMPrivateKey(account *Account) []byte {
|
||||
var block *pem.Block
|
||||
switch account.KeyType {
|
||||
|
||||
9
main.go
9
main.go
@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
@ -25,8 +26,9 @@ func main() {
|
||||
dumpPath := cmd.Flag("dest").Value.String()
|
||||
crtExt := cmd.Flag("crt-ext").Value.String()
|
||||
keyExt := cmd.Flag("key-ext").Value.String()
|
||||
subDir, _ := strconv.ParseBool(cmd.Flag("use-subdir").Value.String())
|
||||
|
||||
err := dump(acmeFile, dumpPath, crtExt, keyExt)
|
||||
err := dump(acmeFile, dumpPath, crtExt, keyExt, subDir)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@ -35,8 +37,9 @@ func main() {
|
||||
|
||||
dumpCmd.Flags().String("source", "./acme.json", "Path to 'acme.json' file.")
|
||||
dumpCmd.Flags().String("dest", "./dump", "Path to store the dump content.")
|
||||
dumpCmd.Flags().String("crt-ext", ".crt", "The file extension of the generated certificates")
|
||||
dumpCmd.Flags().String("key-ext", ".key", "The file extension of the generated private keys")
|
||||
dumpCmd.Flags().String("crt-ext", ".crt", "The file extension of the generated certificates.")
|
||||
dumpCmd.Flags().String("key-ext", ".key", "The file extension of the generated private keys.")
|
||||
dumpCmd.Flags().Bool("use-subdir", true, "Use separated directories for certificates and keys.")
|
||||
rootCmd.AddCommand(dumpCmd)
|
||||
|
||||
var versionCmd = &cobra.Command{
|
||||
|
||||
@ -29,11 +29,12 @@ Usage:
|
||||
traefik-certs-dumper dump [flags]
|
||||
|
||||
Flags:
|
||||
--crt-ext string The file extension of the generated certificates (default ".crt")
|
||||
--crt-ext string The file extension of the generated certificates. (default ".crt")
|
||||
--dest string Path to store the dump content. (default "./dump")
|
||||
-h, --help help for dump
|
||||
--key-ext string The file extension of the generated private keys (default ".key")
|
||||
--key-ext string The file extension of the generated private keys. (default ".key")
|
||||
--source string Path to 'acme.json' file. (default "./acme.json")
|
||||
--use-subdir Use separated directories for certificates and keys. (default true)
|
||||
```
|
||||
|
||||
## Examples
|
||||
@ -50,6 +51,10 @@ traefik-certs-dumper dump --source ./acme.json --dest ./dump
|
||||
traefik-certs-dumper dump --crt-ext=.pem --key-ext=.pem
|
||||
```
|
||||
|
||||
```bash
|
||||
traefik-certs-dumper dump --use-subdir=false
|
||||
```
|
||||
|
||||
- https://github.com/containous/traefik/issues/4381
|
||||
- https://github.com/containous/traefik/issues/2418
|
||||
- https://github.com/containous/traefik/issues/3847
|
||||
|
||||
Loading…
Reference in New Issue
Block a user