Skip to content

Commit

Permalink
KV and commands (#8)
Browse files Browse the repository at this point in the history
- new commands
- support for KV

Co-authored-by: Stephan Müller <mail@stephanmueller.eu>
  • Loading branch information
ldez and smueller18 committed Apr 20, 2019
1 parent adc8296 commit e2b5cc7
Show file tree
Hide file tree
Showing 37 changed files with 1,528 additions and 322 deletions.
8 changes: 8 additions & 0 deletions .dockerignore
@@ -0,0 +1,8 @@
.idea/
vendor/
dist/
dump/
dumpcerts.sh
acme.json
acme-backup.json
traefik-certs-dumper
3 changes: 3 additions & 0 deletions .golangci.toml
Expand Up @@ -39,3 +39,6 @@
[[issues.exclude-rules]]
path = "version.go"
text = "`(version|commit|date)` is a global variable"
[[issues.exclude-rules]]
path = "cmd/"
linters = ["gochecknoglobals", "gochecknoinits"]
6 changes: 4 additions & 2 deletions .goreleaser.yml
Expand Up @@ -2,12 +2,14 @@ project_name: traefik-certs-dumper

builds:
- binary: traefik-certs-dumper
ldflags:
- -s -w -X github.com/ldez/traefik-certs-dumper/cmd.version={{.Version}} -X github.com/ldez/traefik-certs-dumper/cmd.commit={{.ShortCommit}} -X github.com/ldez/traefik-certs-dumper/cmd.date={{.Date}}
env:
- GO111MODULE=on
goos:
- windows
- darwin
- linux
- darwin
- windows
- freebsd
- openbsd
goarch:
Expand Down
11 changes: 5 additions & 6 deletions Dockerfile
@@ -1,21 +1,20 @@
FROM golang:1-alpine as builder

RUN apk --update upgrade \
&& apk --no-cache --no-progress add git make gcc musl-dev \
&& rm -rf /var/cache/apk/*
&& apk --no-cache --no-progress add git make gcc musl-dev

WORKDIR /go/src/github.com/ldez/traefik-certs-dumper
COPY . .

RUN go get -u github.com/golang/dep/cmd/dep
ENV GO111MODULE on
COPY go.mod go.sum ./
RUN go mod download

COPY . .
RUN make build

FROM alpine:3.9
RUN apk --update upgrade \
&& apk --no-cache --no-progress add ca-certificates git \
&& rm -rf /var/cache/apk/*
&& apk --no-cache --no-progress add ca-certificates

COPY --from=builder /go/src/github.com/ldez/traefik-certs-dumper/traefik-certs-dumper /usr/bin/traefik-certs-dumper

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -16,7 +16,7 @@ clean:

build: clean
@echo Version: $(VERSION) $(BUILD_DATE)
go build -v -ldflags '-X "main.version=${VERSION}" -X "main.commit=${SHA}" -X "main.date=${BUILD_DATE}"'
go build -v -ldflags '-X "github.com/ldez/traefik-certs-dumper/cmd.version=${VERSION}" -X "github.com/ldez/traefik-certs-dumper/cmd.commit=${SHA}" -X "github.com/ldez/traefik-certs-dumper/cmd.date=${BUILD_DATE}"'

checks:
golangci-lint run
39 changes: 39 additions & 0 deletions cmd/boltdb.go
@@ -0,0 +1,39 @@
package cmd

import (
"github.com/abronan/valkeyrie/store"
"github.com/abronan/valkeyrie/store/boltdb"
"github.com/ldez/traefik-certs-dumper/dumper"
"github.com/ldez/traefik-certs-dumper/dumper/kv"
"github.com/spf13/cobra"
)

// boltdbCmd represents the boltdb command
var boltdbCmd = &cobra.Command{
Use: "boltdb",
Short: "Dump the content of BoltDB.",
Long: `Dump the content of BoltDB.`,
RunE: runE(boltdbRun),
}

func init() {
kvCmd.AddCommand(boltdbCmd)

boltdbCmd.Flags().Bool("persist-connection", false, "Persist connection for boltdb.")
boltdbCmd.Flags().String("bucket", "traefik", "Bucket for boltdb.")
}

func boltdbRun(baseConfig *dumper.BaseConfig, cmd *cobra.Command) error {
config, err := getKvConfig(cmd)
if err != nil {
return err
}

config.Options.Bucket = cmd.Flag("bucket").Value.String()
config.Options.PersistConnection, _ = cmd.Flags().GetBool("persist-connection")

config.Backend = store.BOLTDB
boltdb.Register()

return kv.Dump(config, baseConfig)
}
37 changes: 37 additions & 0 deletions cmd/consul.go
@@ -0,0 +1,37 @@
package cmd

import (
"github.com/abronan/valkeyrie/store"
"github.com/abronan/valkeyrie/store/consul"
"github.com/ldez/traefik-certs-dumper/dumper"
"github.com/ldez/traefik-certs-dumper/dumper/kv"
"github.com/spf13/cobra"
)

// consulCmd represents the consul command
var consulCmd = &cobra.Command{
Use: "consul",
Short: "Dump the content of Consul.",
Long: `Dump the content of Consul.`,
RunE: runE(consulRun),
}

func init() {
kvCmd.AddCommand(consulCmd)

consulCmd.Flags().String("token", "", "Token for consul.")
}

func consulRun(baseConfig *dumper.BaseConfig, cmd *cobra.Command) error {
config, err := getKvConfig(cmd)
if err != nil {
return err
}

config.Options.Token = cmd.Flag("token").Value.String()

config.Backend = store.CONSUL
consul.Register()

return kv.Dump(config, baseConfig)
}
20 changes: 20 additions & 0 deletions cmd/doc.go
@@ -0,0 +1,20 @@
package cmd

import (
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
)

// docCmd represents the doc command
var docCmd = &cobra.Command{
Use: "doc",
Short: "Generate documentation",
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
return doc.GenMarkdownTree(rootCmd, "./docs")
},
}

func init() {
rootCmd.AddCommand(docCmd)
}
43 changes: 43 additions & 0 deletions cmd/etcd.go
@@ -0,0 +1,43 @@
package cmd

import (
"time"

"github.com/abronan/valkeyrie/store"
"github.com/abronan/valkeyrie/store/etcd/v2"
"github.com/ldez/traefik-certs-dumper/dumper"
"github.com/ldez/traefik-certs-dumper/dumper/kv"
"github.com/spf13/cobra"
)

// etcdCmd represents the etcd command
var etcdCmd = &cobra.Command{
Use: "etcd",
Short: "Dump the content of etcd.",
Long: `Dump the content of etcd.`,
RunE: runE(etcdRun),
}

func init() {
kvCmd.AddCommand(etcdCmd)

etcdCmd.Flags().Int("sync-period", 0, "Sync period for etcd in seconds.")
}

func etcdRun(baseConfig *dumper.BaseConfig, cmd *cobra.Command) error {
config, err := getKvConfig(cmd)
if err != nil {
return err
}

synPeriod, err := cmd.Flags().GetInt("sync-period")
if err != nil {
return err
}
config.Options.SyncPeriod = time.Duration(synPeriod) * time.Second

config.Backend = store.ETCD
etcd.Register()

return kv.Dump(config, baseConfig)
}
25 changes: 25 additions & 0 deletions cmd/file.go
@@ -0,0 +1,25 @@
package cmd

import (
"github.com/ldez/traefik-certs-dumper/dumper"
"github.com/ldez/traefik-certs-dumper/dumper/file"
"github.com/spf13/cobra"
)

// fileCmd represents the file command
var fileCmd = &cobra.Command{
Use: "file",
Short: `Dump the content of the "acme.json" file.`,
Long: `Dump the content of the "acme.json" file from Traefik to certificates.`,
RunE: runE(func(baseConfig *dumper.BaseConfig, cmd *cobra.Command) error {
acmeFile := cmd.Flag("source").Value.String()

return file.Dump(acmeFile, baseConfig)
}),
}

func init() {
rootCmd.AddCommand(fileCmd)

fileCmd.Flags().String("source", "./acme.json", "Path to 'acme.json' file.")
}
58 changes: 58 additions & 0 deletions cmd/kv.go
@@ -0,0 +1,58 @@
package cmd

import (
"strconv"
"time"

"github.com/abronan/valkeyrie/store"
"github.com/ldez/traefik-certs-dumper/dumper/kv"
"github.com/spf13/cobra"
)

// kvCmd represents the kv command
var kvCmd = &cobra.Command{
Use: "kv",
Short: `Dump the content of a KV store.`,
Long: `Dump the content of a KV store.`,
}

func init() {
rootCmd.AddCommand(kvCmd)

kvCmd.PersistentFlags().StringSlice("endpoints", []string{"localhost:8500"}, "List of endpoints.")
kvCmd.PersistentFlags().Int("connection-timeout", 0, "Connection timeout in seconds.")
kvCmd.PersistentFlags().String("prefix", "traefik", "Prefix used for KV store.")
kvCmd.PersistentFlags().String("password", "", "Password for connection.")
kvCmd.PersistentFlags().String("username", "", "Username for connection.")
kvCmd.PersistentFlags().Bool("watch", false, "Enable watching changes.")

// FIXME review TLS parts
// kvCmd.PersistentFlags().Bool("tls.enable", false, "Enable TLS encryption.")
// kvCmd.PersistentFlags().Bool("tls.insecureskipverify", false, "Trust unverified certificates if TLS is enabled.")
// kvCmd.PersistentFlags().String("tls.ca-cert-file", "", "Root CA file for certificate verification if TLS is enabled.")
}

func getKvConfig(cmd *cobra.Command) (*kv.Config, error) {
endpoints, err := cmd.Flags().GetStringSlice("endpoints")
if err != nil {
return nil, err
}

connectionTimeout, err := cmd.Flags().GetInt("connection-timeout")
if err != nil {
return nil, err
}

watch, _ := strconv.ParseBool(cmd.Flag("watch").Value.String())

return &kv.Config{
Endpoints: endpoints,
Prefix: cmd.Flag("prefix").Value.String(),
Options: &store.Config{
ConnectionTimeout: time.Duration(connectionTimeout) * time.Second,
Username: cmd.Flag("password").Value.String(),
Password: cmd.Flag("username").Value.String(),
},
Watch: watch,
}, nil
}

0 comments on commit e2b5cc7

Please sign in to comment.