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
// Certificates Data Sources.
const (
// FILE backend
FILE string = "file"
// CONSUL backend
CONSUL string = "consul"
// ETCD backend
ETCD string = "etcd"
// ZOOKEEPER backend
ZOOKEEPER string = "zookeeper"
// BOLTDB backend
BOLTDB string = "boltdb"
File = "file"
Consul = "consul"
Etcd = "etcd"
Zookeeper = "zookeeper"
BoldDB = "boltdb"
)
// Config represents a configuration for dumping cerificates
// Config represents a configuration for dumping certificates
type Config struct {
Path string
CertInfo fileInfo

View File

@ -18,6 +18,7 @@ func getStoredDataFromFile(path string) (*StoredData, error) {
if err != nil {
return nil, err
}
data := &StoredData{}
if err = json.NewDecoder(f).Decode(&data); err != nil {
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) {
dataCh := make(chan *StoredData)
errCh := make(chan error)
go func() {

43
kv.go
View File

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

32
main.go
View File

@ -16,14 +16,14 @@ import (
)
func main() {
var rootCmd = &cobra.Command{
rootCmd := &cobra.Command{
Use: "traefik-certs-dumper",
Short: "Dump Let's Encrypt certificates from Traefik",
Long: `Dump ACME data from Traefik of different storage backends to certificates.`,
Version: version,
}
var dumpCmd = &cobra.Command{
dumpCmd := &cobra.Command{
Use: "dump",
Short: "Dump Let's Encrypt certificates from Traefik",
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()
sourceFile := cmd.Flag("source.file").Value.String()
watch, _ := strconv.ParseBool(cmd.Flag("watch").Value.String())
switch source {
case FILE:
case File:
if _, err := os.Stat(sourceFile); os.IsNotExist(err) {
return fmt.Errorf("--source.file (%q) does not exist", sourceFile)
}
case BOLTDB:
case BoldDB:
if watch {
return fmt.Errorf("--watch=true is not supported for boltdb")
}
case CONSUL:
case ETCD:
case ZOOKEEPER:
case Consul, Etcd, Zookeeper:
// noop
default:
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 {
config := &Config{}
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()
switch source {
case FILE:
case File:
config.BackendConfig = FileBackend{
Name: FILE,
Name: File,
Path: acmeFile,
}
case CONSUL:
fallthrough
case ETCD:
fallthrough
case ZOOKEEPER:
fallthrough
case BOLTDB:
fallthrough
default:
case Consul, Etcd, Zookeeper, BoldDB:
config.BackendConfig = KVBackend{
Name: source,
Client: endpoints,
Config: storeConfig,
}
default:
return fmt.Errorf("unsuported source: %s", source)
}
config.Path = cmd.Flag("dest").Value.String()