minor chnages

This commit is contained in:
Fernandez Ludovic 2019-04-20 13:34:06 +02:00
parent 04eb9f13fc
commit c2bcf153e9
4 changed files with 41 additions and 54 deletions

View File

@ -1,19 +1,15 @@
package main package main
// Certificates Data Sources.
const ( const (
// FILE backend File = "file"
FILE string = "file" Consul = "consul"
// CONSUL backend Etcd = "etcd"
CONSUL string = "consul" Zookeeper = "zookeeper"
// ETCD backend BoldDB = "boltdb"
ETCD string = "etcd"
// ZOOKEEPER backend
ZOOKEEPER string = "zookeeper"
// BOLTDB backend
BOLTDB string = "boltdb"
) )
// Config represents a configuration for dumping cerificates // Config represents a configuration for dumping certificates
type Config struct { type Config struct {
Path string Path string
CertInfo fileInfo CertInfo fileInfo

View File

@ -18,6 +18,7 @@ func getStoredDataFromFile(path string) (*StoredData, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
data := &StoredData{} data := &StoredData{}
if err = json.NewDecoder(f).Decode(&data); err != nil { if err = json.NewDecoder(f).Decode(&data); err != nil {
return nil, err return nil, err
@ -55,7 +56,6 @@ func loopFile(path string, watcher *fsnotify.Watcher, dataCh chan *StoredData, e
} }
func (b FileBackend) getStoredData(watch bool) (<-chan *StoredData, <-chan error) { func (b FileBackend) getStoredData(watch bool) (<-chan *StoredData, <-chan error) {
dataCh := make(chan *StoredData) dataCh := make(chan *StoredData)
errCh := make(chan error) errCh := make(chan error)
go func() { go func() {

43
kv.go
View File

@ -15,9 +15,7 @@ import (
"github.com/abronan/valkeyrie/store/zookeeper" "github.com/abronan/valkeyrie/store/zookeeper"
) )
const ( const storeKey = "traefik/acme/account/object"
storeKey = "traefik/acme/account/object"
)
func getStoredDataFromGzip(value []byte) (*StoredData, error) { func getStoredDataFromGzip(value []byte) (*StoredData, error) {
data := &StoredData{} data := &StoredData{}
@ -49,16 +47,16 @@ type KVBackend struct {
func register(backend string) (store.Backend, error) { func register(backend string) (store.Backend, error) {
switch backend { switch backend {
case CONSUL: case Consul:
consul.Register() consul.Register()
return store.CONSUL, nil return store.CONSUL, nil
case ETCD: case Etcd:
etcdv3.Register() etcdv3.Register()
return store.ETCDV3, nil return store.ETCDV3, nil
case ZOOKEEPER: case Zookeeper:
zookeeper.Register() zookeeper.Register()
return store.ZK, nil return store.ZK, nil
case BOLTDB: case BoldDB:
boltdb.Register() boltdb.Register()
return store.BOLTDB, nil return store.BOLTDB, nil
default: default:
@ -66,19 +64,20 @@ func register(backend string) (store.Backend, error) {
} }
} }
func loopKV(watch bool, kvstore store.Store, dataCh chan *StoredData, errCh chan error) { func loopKV(watch bool, kvStore store.Store, dataCh chan *StoredData, errCh chan error) {
stopCh := make(<-chan struct{}) stopCh := make(<-chan struct{})
events, err := kvstore.Watch(storeKey, stopCh, nil) events, err := kvStore.Watch(storeKey, stopCh, nil)
if err != nil { if err != nil {
errCh <- err errCh <- err
} }
for { for {
kvpair := <-events kvPair := <-events
if kvpair == nil { if kvPair == nil {
errCh <- fmt.Errorf("could not fetch Key/Value pair for key %v", storeKey) errCh <- fmt.Errorf("could not fetch Key/Value pair for key %v", storeKey)
return return
} }
dataCh <- extractStoredData(kvpair, errCh) dataCh <- extractStoredData(kvPair, errCh)
if !watch { if !watch {
close(dataCh) close(dataCh)
close(errCh) close(errCh)
@ -86,31 +85,31 @@ func loopKV(watch bool, kvstore store.Store, dataCh chan *StoredData, errCh chan
} }
} }
func extractStoredData(kvpair *store.KVPair, errCh chan error) *StoredData { func extractStoredData(kvPair *store.KVPair, errCh chan error) *StoredData {
storedData, err := getStoredDataFromGzip(kvpair.Value) storedData, err := getStoredDataFromGzip(kvPair.Value)
if err != nil { if err != nil {
errCh <- err errCh <- err
} }
return storedData return storedData
} }
func getSingleData(kvstore store.Store, dataCh chan *StoredData, errCh chan error) { func getSingleData(kvStore store.Store, dataCh chan *StoredData, errCh chan error) {
kvpair, err := kvstore.Get(storeKey, nil) kvPair, err := kvStore.Get(storeKey, nil)
if err != nil { if err != nil {
errCh <- err errCh <- err
return return
} }
if kvpair == nil { if kvPair == nil {
errCh <- fmt.Errorf("could not fetch Key/Value pair for key %v", storeKey) errCh <- fmt.Errorf("could not fetch Key/Value pair for key %v", storeKey)
return return
} }
dataCh <- extractStoredData(kvpair, errCh)
dataCh <- extractStoredData(kvPair, errCh)
close(dataCh) close(dataCh)
close(errCh) close(errCh)
} }
func (b KVBackend) getStoredData(watch bool) (<-chan *StoredData, <-chan error) { func (b KVBackend) getStoredData(watch bool) (<-chan *StoredData, <-chan error) {
dataCh := make(chan *StoredData) dataCh := make(chan *StoredData)
errCh := make(chan error) errCh := make(chan error)
@ -121,7 +120,7 @@ func (b KVBackend) getStoredData(watch bool) (<-chan *StoredData, <-chan error)
}() }()
return dataCh, errCh return dataCh, errCh
} }
kvstore, err := valkeyrie.NewStore( kvStore, err := valkeyrie.NewStore(
backend, backend,
b.Client, b.Client,
b.Config, b.Config,
@ -135,11 +134,11 @@ func (b KVBackend) getStoredData(watch bool) (<-chan *StoredData, <-chan error)
} }
if !watch { if !watch {
go getSingleData(kvstore, dataCh, errCh) go getSingleData(kvStore, dataCh, errCh)
return dataCh, errCh return dataCh, errCh
} }
go loopKV(watch, kvstore, dataCh, errCh) go loopKV(watch, kvStore, dataCh, errCh)
return dataCh, errCh return dataCh, errCh

32
main.go
View File

@ -16,14 +16,14 @@ import (
) )
func main() { func main() {
var rootCmd = &cobra.Command{ rootCmd := &cobra.Command{
Use: "traefik-certs-dumper", Use: "traefik-certs-dumper",
Short: "Dump Let's Encrypt certificates from Traefik", Short: "Dump Let's Encrypt certificates from Traefik",
Long: `Dump ACME data from Traefik of different storage backends to certificates.`, Long: `Dump ACME data from Traefik of different storage backends to certificates.`,
Version: version, Version: version,
} }
var dumpCmd = &cobra.Command{ dumpCmd := &cobra.Command{
Use: "dump", Use: "dump",
Short: "Dump Let's Encrypt certificates from Traefik", Short: "Dump Let's Encrypt certificates from Traefik",
Long: `Dump ACME data from Traefik of different storage backends to certificates.`, Long: `Dump ACME data from Traefik of different storage backends to certificates.`,
@ -75,23 +75,22 @@ func main() {
} }
} }
func commandPreRun(cmd *cobra.Command, args []string) error { func commandPreRun(cmd *cobra.Command, _ []string) error {
source := cmd.Flag("source").Value.String() source := cmd.Flag("source").Value.String()
sourceFile := cmd.Flag("source.file").Value.String() sourceFile := cmd.Flag("source.file").Value.String()
watch, _ := strconv.ParseBool(cmd.Flag("watch").Value.String()) watch, _ := strconv.ParseBool(cmd.Flag("watch").Value.String())
switch source { switch source {
case FILE: case File:
if _, err := os.Stat(sourceFile); os.IsNotExist(err) { if _, err := os.Stat(sourceFile); os.IsNotExist(err) {
return fmt.Errorf("--source.file (%q) does not exist", sourceFile) return fmt.Errorf("--source.file (%q) does not exist", sourceFile)
} }
case BOLTDB: case BoldDB:
if watch { if watch {
return fmt.Errorf("--watch=true is not supported for boltdb") return fmt.Errorf("--watch=true is not supported for boltdb")
} }
case CONSUL: case Consul, Etcd, Zookeeper:
case ETCD: // noop
case ZOOKEEPER:
default: default:
return fmt.Errorf("--source (%q) is not allowed, use one of 'file', 'consul', 'etcd', 'zookeeper', 'boltdb'", source) return fmt.Errorf("--source (%q) is not allowed, use one of 'file', 'consul', 'etcd', 'zookeeper', 'boltdb'", source)
} }
@ -110,7 +109,6 @@ func commandPreRun(cmd *cobra.Command, args []string) error {
} }
func commandRun(cmd *cobra.Command, _ []string) error { func commandRun(cmd *cobra.Command, _ []string) error {
config := &Config{} config := &Config{}
source := cmd.Flag("source").Value.String() source := cmd.Flag("source").Value.String()
@ -158,25 +156,19 @@ func commandRun(cmd *cobra.Command, _ []string) error {
storeConfig.Token = cmd.Flag("source.kv.consul.token").Value.String() storeConfig.Token = cmd.Flag("source.kv.consul.token").Value.String()
switch source { switch source {
case FILE: case File:
config.BackendConfig = FileBackend{ config.BackendConfig = FileBackend{
Name: FILE, Name: File,
Path: acmeFile, Path: acmeFile,
} }
case CONSUL: case Consul, Etcd, Zookeeper, BoldDB:
fallthrough
case ETCD:
fallthrough
case ZOOKEEPER:
fallthrough
case BOLTDB:
fallthrough
default:
config.BackendConfig = KVBackend{ config.BackendConfig = KVBackend{
Name: source, Name: source,
Client: endpoints, Client: endpoints,
Config: storeConfig, Config: storeConfig,
} }
default:
return fmt.Errorf("unsuported source: %s", source)
} }
config.Path = cmd.Flag("dest").Value.String() config.Path = cmd.Flag("dest").Value.String()