Skip to content

Commit

Permalink
Add viper to allow config files
Browse files Browse the repository at this point in the history
  • Loading branch information
iknite committed Feb 4, 2019
1 parent 9753845 commit 3cb2606
Show file tree
Hide file tree
Showing 14 changed files with 390 additions and 101 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ coverage.txt
*.tfstate*
*terraform.tfvars*
.golangci.yml
config.yml
18 changes: 15 additions & 3 deletions client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,24 @@ type Config struct {

// Enable self-signed certificates, allowing MiTM vector attacks.
Insecure bool

// Seconds to wait for an established connection.
TimeoutSeconds int

// Seconds to wait for the connection to be established.
DialTimeoutSeconds int

// Seconds to wait for a handshake negotiation.
HandshakeTimeoutSeconds int
}

func DefaultConfig() *Config {
return &Config{
Endpoint: "localhost:8080",
APIKey: "my-key",
Insecure: true,
Endpoint: "localhost:8080",
APIKey: "my-key",
Insecure: true,
TimeoutSeconds: 10,
DialTimeoutSeconds: 5,
HandshakeTimeoutSeconds: 5,
}
}
52 changes: 34 additions & 18 deletions cmd/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package cmd

import (
"github.com/bbva/qed/gossip"
"github.com/spf13/cobra"
v "github.com/spf13/viper"

"github.com/bbva/qed/gossip"
)

func newAgentCommand(cmdCtx *cmdContext) *cobra.Command {
Expand All @@ -28,27 +30,41 @@ func newAgentCommand(cmdCtx *cmdContext) *cobra.Command {
cmd := &cobra.Command{
Use: "agent",
Short: "Start a gossip agent for the verifiable log QED",
Long: ``,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
config.EnableCompression = true
},
TraverseChildren: true,
}

cmd.PersistentFlags().StringVar(&config.NodeName, "node", "", "Unique name for node. If not set, fallback to hostname")
cmd.PersistentFlags().StringVar(&config.BindAddr, "bind", "", "Bind address for TCP/UDP gossip on (host:port)")
cmd.PersistentFlags().StringVar(&config.AdvertiseAddr, "advertise", "", "Address to advertise to cluster")
cmd.PersistentFlags().StringSliceVar(&config.StartJoin, "join", []string{}, "Comma-delimited list of nodes ([host]:port), through which a cluster can be joined")
cmd.Flags().StringSliceVar(&config.AlertsUrls, "alertsUrls", []string{}, "Comma-delimited list of Alert servers ([host]:port), through which an agent can post alerts")
f := cmd.PersistentFlags()
f.StringVar(&config.NodeName, "node", "", "Unique name for node. If not set, fallback to hostname")
f.StringVar(&config.BindAddr, "bind", "", "Bind address for TCP/UDP gossip on (host:port)")
f.StringVar(&config.AdvertiseAddr, "advertise", "", "Address to advertise to cluster")
f.StringSliceVar(&config.StartJoin, "join", []string{}, "Comma-delimited list of nodes ([host]:port), through which a cluster can be joined")
f.StringSliceVar(&config.AlertsUrls, "alertsUrls", []string{}, "Comma-delimited list of Alert servers ([host]:port), through which an agent can post alerts")

agentPreRun := func(cmd *cobra.Command, args []string) {
config.EnableCompression = true
config.NodeName = v.GetString("agent.node")
config.BindAddr = v.GetString("agent.bind")
config.AdvertiseAddr = v.GetString("agent.advertise")
config.StartJoin = v.GetStringSlice("agent.join")
config.AlertsUrls = v.GetStringSlice("agent.alert_urls")

markStringRequired(config.NodeName, "node")
markStringRequired(config.BindAddr, "bind")
markSliceStringRequired(config.StartJoin, "join")
markSliceStringRequired(config.AlertsUrls, "alertsUrls")
}

cmd.MarkPersistentFlagRequired("node")
cmd.MarkPersistentFlagRequired("bind")
cmd.MarkPersistentFlagRequired("join")
cmd.MarkFlagRequired("alertUrls")
cmd.AddCommand(
newAgentMonitorCommand(cmdCtx, config, agentPreRun),
newAgentAuditorCommand(cmdCtx, config, agentPreRun),
newAgentPublisherCommand(cmdCtx, config, agentPreRun),
)

cmd.AddCommand(newAgentMonitorCommand(cmdCtx, config))
cmd.AddCommand(newAgentAuditorCommand(cmdCtx, config))
cmd.AddCommand(newAgentPublisherCommand(cmdCtx, config))
// Lookups
v.BindPFlag("agent.node", f.Lookup("node"))
v.BindPFlag("agent.bind", f.Lookup("bind"))
v.BindPFlag("agent.advertise", f.Lookup("advertise"))
v.BindPFlag("agent.join", f.Lookup("join"))
v.BindPFlag("agent.alert_urls", f.Lookup("alertsUrls"))

return cmd

Expand Down
34 changes: 26 additions & 8 deletions cmd/agent_auditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,40 @@
package cmd

import (
"github.com/spf13/cobra"
v "github.com/spf13/viper"

"github.com/bbva/qed/gossip"
"github.com/bbva/qed/gossip/auditor"
"github.com/bbva/qed/gossip/member"
"github.com/bbva/qed/log"
"github.com/bbva/qed/util"
"github.com/spf13/cobra"
)

func newAgentAuditorCommand(ctx *cmdContext, config *gossip.Config) *cobra.Command {
func newAgentAuditorCommand(ctx *cmdContext, config *gossip.Config, agentPreRun func(*cobra.Command, []string)) *cobra.Command {

auditorConfig := auditor.DefaultConfig()

cmd := &cobra.Command{
Use: "auditor",
Short: "Start a QED auditor",
Long: `Start a QED auditor that reacts to snapshot batches propagated by QED servers and periodically executes membership queries to verify the inclusion of events`,
Run: func(cmd *cobra.Command, args []string) {
PreRun: func(cmd *cobra.Command, args []string) {

log.SetLogger("QEDAuditor", ctx.logLevel)

log.SetLogger("QedAuditor", ctx.logLevel)
// WARN: PersitentPreRun can't be nested and we're using it in cmd/root so inbetween preRuns
// must be curried.
agentPreRun(cmd, args)

// Bindings
auditorConfig.QEDUrls = v.GetStringSlice("agent.server_urls")
auditorConfig.PubUrls = v.GetStringSlice("agent.publish_urls")
markSliceStringRequired(auditorConfig.QEDUrls, "qedUrls")
markSliceStringRequired(auditorConfig.PubUrls, "pubUrls")

},
Run: func(cmd *cobra.Command, args []string) {

config.Role = member.Auditor
auditorConfig.APIKey = ctx.apiKey
Expand All @@ -58,10 +73,13 @@ func newAgentAuditorCommand(ctx *cmdContext, config *gossip.Config) *cobra.Comma
},
}

cmd.Flags().StringSliceVarP(&auditorConfig.QEDUrls, "qedUrls", "", []string{}, "Comma-delimited list of QED servers ([host]:port), through which an auditor can make queries")
cmd.Flags().StringSliceVarP(&auditorConfig.PubUrls, "pubUrls", "", []string{}, "Comma-delimited list of QED servers ([host]:port), through which an auditor can make queries")
cmd.MarkFlagRequired("qedUrls")
cmd.MarkFlagRequired("pubUrls")
f := cmd.Flags()
f.StringSliceVarP(&auditorConfig.QEDUrls, "qedUrls", "", []string{}, "Comma-delimited list of QED servers ([host]:port), through which an auditor can make queries")
f.StringSliceVarP(&auditorConfig.PubUrls, "pubUrls", "", []string{}, "Comma-delimited list of QED servers ([host]:port), through which an auditor can make queries")

// Lookups
v.BindPFlag("agent.server_urls", f.Lookup("qedUrls"))
v.BindPFlag("agent.publish_urls", f.Lookup("pubUrls"))

return cmd
}
37 changes: 27 additions & 10 deletions cmd/agent_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,40 @@
package cmd

import (
"github.com/spf13/cobra"
v "github.com/spf13/viper"

"github.com/bbva/qed/gossip"
"github.com/bbva/qed/gossip/member"
"github.com/bbva/qed/gossip/monitor"
"github.com/bbva/qed/log"
"github.com/bbva/qed/util"
"github.com/spf13/cobra"
)

func newAgentMonitorCommand(ctx *cmdContext, config *gossip.Config) *cobra.Command {
func newAgentMonitorCommand(ctx *cmdContext, config *gossip.Config, agentPreRun func(*cobra.Command, []string)) *cobra.Command {

monitorConfig := monitor.DefaultConfig()

cmd := &cobra.Command{
Use: "monitor",
Short: "Start a QED monitor",
Long: `Start a QED monitor that reacts to snapshot batches
propagated by QED servers and periodically executes incremental
queries to verify the consistency between snaphots`,
Long: `Start a QED monitor that reacts to snapshot batches propagated by QED servers and periodically executes incremental queries to verify the consistency between snaphots`,
PreRun: func(cmd *cobra.Command, args []string) {

log.SetLogger("QEDMonitor", ctx.logLevel)

// WARN: PersitentPreRun can't be nested and we're using it in cmd/root so inbetween preRuns
// must be curried.
agentPreRun(cmd, args)

// Bindings
monitorConfig.QedUrls = v.GetStringSlice("agent.server_urls")
monitorConfig.PubUrls = v.GetStringSlice("agent.publish_urls")
markSliceStringRequired(monitorConfig.QedUrls, "qedUrls")
markSliceStringRequired(monitorConfig.PubUrls, "pubUrls")

},
Run: func(cmd *cobra.Command, args []string) {
log.SetLogger("QedMonitor", ctx.logLevel)

config.Role = member.Monitor
monitorConfig.APIKey = ctx.apiKey
Expand All @@ -60,10 +74,13 @@ func newAgentMonitorCommand(ctx *cmdContext, config *gossip.Config) *cobra.Comma
},
}

cmd.Flags().StringSliceVarP(&monitorConfig.QedUrls, "qedUrls", "", []string{}, "Comma-delimited list of QED servers ([host]:port), through which a monitor can make queries")
cmd.Flags().StringSliceVarP(&monitorConfig.PubUrls, "pubUrls", "", []string{}, "Comma-delimited list of QED servers ([host]:port), through which an monitor can publish alerts")
cmd.MarkFlagRequired("qedUrls")
cmd.MarkFlagRequired("pubUrls")
f := cmd.Flags()
f.StringSliceVarP(&monitorConfig.QedUrls, "qedUrls", "", []string{}, "Comma-delimited list of QED servers ([host]:port), through which a monitor can make queries")
f.StringSliceVarP(&monitorConfig.PubUrls, "pubUrls", "", []string{}, "Comma-delimited list of QED servers ([host]:port), through which an monitor can publish alerts")

// Lookups
v.BindPFlag("agent.server_urls", f.Lookup("qedUrls"))
v.BindPFlag("agent.publish_urls", f.Lookup("pubUrls"))

return cmd
}
27 changes: 21 additions & 6 deletions cmd/agent_publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,38 @@
package cmd

import (
"github.com/spf13/cobra"
v "github.com/spf13/viper"

"github.com/bbva/qed/gossip"
"github.com/bbva/qed/gossip/member"
"github.com/bbva/qed/gossip/publisher"
"github.com/bbva/qed/log"
"github.com/bbva/qed/util"
"github.com/spf13/cobra"
)

func newAgentPublisherCommand(ctx *cmdContext, config *gossip.Config) *cobra.Command {
func newAgentPublisherCommand(ctx *cmdContext, config *gossip.Config, agentPreRun func(*cobra.Command, []string)) *cobra.Command {

var endpoints []string

cmd := &cobra.Command{
Use: "publisher",
Short: "Start a QED publisher",
Long: `Start a QED publisher that reacts to snapshot batches propagated by QED servers and periodically publishes them to a certain log storage.`,
Run: func(cmd *cobra.Command, args []string) {
PreRun: func(cmd *cobra.Command, args []string) {

log.SetLogger("QEDPublisher", ctx.logLevel)

log.SetLogger("QedPublisher", ctx.logLevel)
// WARN: PersitentPreRun can't be nested and we're using it in
// cmd/root so inbetween preRuns must be curried.
agentPreRun(cmd, args)

// Bindings
endpoints = v.GetStringSlice("agent.publish_urls")
markSliceStringRequired(endpoints, "pubUrls")

},
Run: func(cmd *cobra.Command, args []string) {

config.Role = member.Publisher
publisherConfig := publisher.NewConfig(endpoints)
Expand All @@ -58,10 +71,12 @@ func newAgentPublisherCommand(ctx *cmdContext, config *gossip.Config) *cobra.Com
},
}

cmd.Flags().StringSliceVarP(&endpoints, "endpoints", "", []string{},
f := cmd.Flags()
f.StringSliceVarP(&endpoints, "pubUrls", "", []string{},
"Comma-delimited list of end-publishers ([host]:port), through which an publisher can send requests")

cmd.MarkFlagRequired("endpoints")
// Lookups
v.BindPFlag("agent.publish_urls", f.Lookup("pubUrls"))

return cmd
}
55 changes: 39 additions & 16 deletions cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,59 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
v "github.com/spf13/viper"

"github.com/bbva/qed/client"
"github.com/bbva/qed/log"
"github.com/spf13/cobra"
)

func newClientCommand(ctx *cmdContext) *cobra.Command {
clientCtx := &clientContext{}
clientCtx.config = client.DefaultConfig()

cmd := &cobra.Command{
Use: "client",
Short: "Client mode for qed",
Long: `Client process for emitting events to a qed server`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
log.SetLogger("QedClient", ctx.logLevel)

clientCtx.client = client.NewHTTPClient(client.Config{
Endpoint: clientCtx.endpoint,
APIKey: ctx.apiKey,
Insecure: clientCtx.insecure,
})
},
TraverseChildren: true,
}

cmd.PersistentFlags().StringVarP(&clientCtx.endpoint, "endpoint", "e", "localhost:8080", "Endpoint for REST requests on (host:port)")
cmd.PersistentFlags().BoolVar(&clientCtx.insecure, "insecure", false, "Disable TLS transport")
f := cmd.PersistentFlags()
f.StringVarP(&clientCtx.config.Endpoint, "endpoint", "e", "127.0.0.1:8080", "Endpoint for REST requests on (host:port)")
f.BoolVar(&clientCtx.config.Insecure, "insecure", false, "Allow self signed certificates")
f.IntVar(&clientCtx.config.TimeoutSeconds, "timeout-seconds", 10, "Seconds to cut the connection")
f.IntVar(&clientCtx.config.DialTimeoutSeconds, "dial-timeout-seconds", 5, "Seconds to cut the dialing")
f.IntVar(&clientCtx.config.HandshakeTimeoutSeconds, "handshake-timeout-seconds", 5, "Seconds to cut the handshaking")

// Lookups
v.BindPFlag("client.endpoint", f.Lookup("endpoint"))
v.BindPFlag("client.insecure", f.Lookup("insecure"))
v.BindPFlag("client.timeout.connection", f.Lookup("timeout-seconds"))
v.BindPFlag("client.timeout.dial", f.Lookup("dial-timeout-seconds"))
v.BindPFlag("client.timeout.handshake", f.Lookup("handshake-timeout-seconds"))

clientPreRun := func(cmd *cobra.Command, args []string) {
fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>>", "client.customprerun")
log.SetLogger("QEDClient", ctx.logLevel)

clientCtx.config.APIKey = ctx.apiKey
clientCtx.config.Endpoint = v.GetString("client.endpoint")
clientCtx.config.Insecure = v.GetBool("client.insecure")
clientCtx.config.TimeoutSeconds = v.GetInt("client.timeout.connection")
clientCtx.config.DialTimeoutSeconds = v.GetInt("client.timeout.dial")
clientCtx.config.HandshakeTimeoutSeconds = v.GetInt("client.timeout.handshake")

clientCtx.client = client.NewHTTPClient(*clientCtx.config)

}

cmd.AddCommand(newAddCommand(clientCtx))
cmd.AddCommand(newMembershipCommand(clientCtx))
cmd.AddCommand(newIncrementalCommand(clientCtx))
cmd.AddCommand(
newAddCommand(clientCtx, clientPreRun),
newMembershipCommand(clientCtx, clientPreRun),
newIncrementalCommand(clientCtx, clientPreRun),
)

return cmd
}
7 changes: 6 additions & 1 deletion cmd/client_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@ import (
"github.com/bbva/qed/log"
)

func newAddCommand(ctx *clientContext) *cobra.Command {
func newAddCommand(ctx *clientContext, clientPreRun func(*cobra.Command, []string)) *cobra.Command {

var key, value string

cmd := &cobra.Command{
Use: "add",
Short: "Add an event",
Long: `Add an event to the authenticated data structure`,
PreRun: func(cmd *cobra.Command, args []string) {
// WARN: PersitentPreRun can't be nested and we're using it in
// cmd/root so inbetween preRuns must be curried.
clientPreRun(cmd, args)
},
RunE: func(cmd *cobra.Command, args []string) error {
log.Infof("Adding key [ %s ] with value [ %s ]\n", key, value)

Expand Down
Loading

0 comments on commit 3cb2606

Please sign in to comment.