Skip to content

Commit

Permalink
Merge pull request #137 from jllucas/checkparams
Browse files Browse the repository at this point in the history
Cmd input parameter check
  • Loading branch information
Jose Luis Lucas committed Jun 13, 2019
2 parents 6340937 + d148ee7 commit 6653e3e
Show file tree
Hide file tree
Showing 11 changed files with 350 additions and 19 deletions.
31 changes: 29 additions & 2 deletions cmd/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ three agents into the distribution:
* Auditor agent: verifies QED membership proofs of the snapshots received
throught the gossip network
* Publisher agent: publish snapshots to the snapshot store`,
TraverseChildren: true,
TraverseChildren: true,
PersistentPreRunE: runAgent,
}

var agentCtx context.Context = configAgent()
var agentCtx context.Context

func init() {
agentCtx = configAgent()
agentCmd.SilenceUsage = true
agentCmd.MarkFlagRequired("bind-addr")
agentCmd.MarkFlagRequired("metrics-addr")
Expand All @@ -59,3 +61,28 @@ func configAgent() context.Context {

return context.WithValue(Ctx, k("agent.config"), conf)
}

func runAgent(cmd *cobra.Command, args []string) error {
// URL parsing
var err error

gossipStartJoin, _ := cmd.Flags().GetStringSlice("start-join")
err = urlParse(gossipStartJoin...)
if err != nil {
return err
}

bindAddress, _ := cmd.Flags().GetString("bind-addr")
err = urlParseNoSchemaRequired(bindAddress)
if err != nil {
return err
}

advertiseAddress, _ := cmd.Flags().GetString("advertise-addr")
err = urlParse(advertiseAddress)
if err != nil {
return err
}

return nil
}
26 changes: 26 additions & 0 deletions cmd/agent_auditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ func runAgentAuditor(cmd *cobra.Command, args []string) error {

log.SetLogger("auditor", agentConfig.Log)

// URL parse
err := checkAuditorParams(conf)
if err != nil {
return err
}

notifier := gossip.NewSimpleNotifierFromConfig(conf.Notifier)
qed, err := client.NewHTTPClientFromConfig(conf.Qed)
if err != nil {
Expand All @@ -139,6 +145,26 @@ func runAgentAuditor(cmd *cobra.Command, args []string) error {
return nil
}

func checkAuditorParams(conf *auditorConfig) error {
var err error
err = urlParse(conf.Notifier.Endpoint...)
if err != nil {
return err
}

err = urlParse(conf.Store.Endpoint...)
if err != nil {
return err
}

err = urlParse(conf.Qed.Endpoints...)
if err != nil {
return err
}

return nil
}

type membershipFactory struct{}

func (m membershipFactory) Metrics() []prometheus.Collector {
Expand Down
25 changes: 25 additions & 0 deletions cmd/agent_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ func runAgentMonitor(cmd *cobra.Command, args []string) error {

log.SetLogger("monitor", agentConfig.Log)

// URL parse
err := checkMonitorParams(conf)
if err != nil {
return err
}

notifier := gossip.NewSimpleNotifierFromConfig(conf.Notifier)
qed, err := client.NewHTTPClientFromConfig(conf.Qed)
if err != nil {
Expand Down Expand Up @@ -146,6 +152,25 @@ func runAgentMonitor(cmd *cobra.Command, args []string) error {
return nil
}

func checkMonitorParams(conf *monitorConfig) error {
var err error
err = urlParse(conf.Notifier.Endpoint...)
if err != nil {
return err
}

err = urlParse(conf.Store.Endpoint...)
if err != nil {
return err
}

err = urlParse(conf.Qed.Endpoints...)
if err != nil {
return err
}
return nil
}

type incrementalFactory struct{}

func (i incrementalFactory) Metrics() []prometheus.Collector {
Expand Down
22 changes: 21 additions & 1 deletion cmd/agent_publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ func runAgentPublisher(cmd *cobra.Command, args []string) error {

log.SetLogger("publisher", agentConfig.Log)

// URL parse
err := checkPublisherParams(conf)
if err != nil {
return err
}

notifier := gossip.NewSimpleNotifierFromConfig(conf.Notifier)
tm := gossip.NewSimpleTasksManagerFromConfig(conf.Tasks)
store := gossip.NewRestSnapshotStoreFromConfig(conf.Store)
Expand All @@ -118,6 +124,20 @@ func runAgentPublisher(cmd *cobra.Command, args []string) error {
return nil
}

func checkPublisherParams(conf *publisherConfig) error {
var err error
err = urlParse(conf.Notifier.Endpoint...)
if err != nil {
return err
}

err = urlParse(conf.Store.Endpoint...)
if err != nil {
return err
}
return nil
}

type publisherFactory struct {
}

Expand Down Expand Up @@ -148,7 +168,7 @@ func (p publisherFactory) New(ctx context.Context) gossip.Task {
_, err := a.Cache.Get(signedSnap.Signature)
if err != nil {
log.Debugf("PublishingTask: add snapshot to be published")
a.Cache.Set(signedSnap.Signature, []byte{0x0}, 0)
_ = a.Cache.Set(signedSnap.Signature, []byte{0x0}, 0)
batch.Snapshots = append(batch.Snapshots, signedSnap)
}
}
Expand Down
29 changes: 24 additions & 5 deletions cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ import (
)

var clientCmd *cobra.Command = &cobra.Command{
Use: "client",
Short: "Provdes access to the QED log client",
TraverseChildren: true,
Use: "client",
Short: "Provdes access to the QED log client",
TraverseChildren: true,
PersistentPreRunE: runClient,
}

var clientCtx context.Context = configClient()
var clientCtx context.Context

func init() {
clientCtx = configClient()
Root.AddCommand(clientCmd)
}

Expand All @@ -46,6 +48,23 @@ func configClient() context.Context {
if err != nil {
log.Fatalf("err: %v", err)
}

return context.WithValue(Ctx, k("client.config"), conf)
}

func runClient(cmd *cobra.Command, args []string) error {
var err error

endpoints, _ := cmd.Flags().GetStringSlice("endpoints")
err = urlParse(endpoints...)
if err != nil {
return err
}

snapshotStoreURL, _ := cmd.Flags().GetString("snapshot-store-url")
err = urlParse(snapshotStoreURL)
if err != nil {
return err
}

return nil
}
29 changes: 23 additions & 6 deletions cmd/client_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
package cmd

import (
"context"
"fmt"

"github.com/bbva/qed/client"
"github.com/bbva/qed/log"
"github.com/octago/sflags/gen/gpflag"
"github.com/spf13/cobra"
)

Expand All @@ -30,19 +32,34 @@ var clientAddCmd *cobra.Command = &cobra.Command{
RunE: runClientAdd,
}

var clientAddEvent string
var clientAddCtx context.Context

func init() {
type addParams struct {
Event string `desc:"QED event to insert to QED"`
}

clientAddCmd.Flags().StringVar(&clientAddEvent, "event", "", "Event to append to QED")
clientAddCmd.MarkFlagRequired("event")
func init() {

clientAddCtx = configClientAdd()
clientCmd.AddCommand(clientAddCmd)
}

func configClientAdd() context.Context {

conf := &addParams{}

err := gpflag.ParseTo(conf, clientAddCmd.PersistentFlags())
if err != nil {
log.Fatalf("err: %v", err)
}
return context.WithValue(Ctx, k("client.add.params"), conf)
}

func runClientAdd(cmd *cobra.Command, args []string) error {

if clientAddEvent == "" {
params := clientAddCtx.Value(k("client.add.params")).(*addParams)

if params.Event == "" {
return fmt.Errorf("Event must not be empty!")
}

Expand All @@ -54,7 +71,7 @@ func runClientAdd(cmd *cobra.Command, args []string) error {
return err
}

snapshot, err := client.Add(clientAddEvent)
snapshot, err := client.Add(params.Event)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/client_incremental.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func init() {

type incrementalParams struct {
Start uint64 `desc:"Starting version for the incremental proof"`
End uint64 `desc:"Endind version for the incremental proof"`
End uint64 `desc:"Ending version for the incremental proof"`
Verify bool `desc:"Set to enable proof verification process"`
AutoVerify bool `desc:"Set to enable proof automatic verification process"`
}
Expand Down
1 change: 0 additions & 1 deletion cmd/client_membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ It also verifies the proofs provided by the server if flag enabled.`,
}

var clientMembershipCtx context.Context
var isVersionSet bool

func init() {
clientMembershipCtx = configClientMembership()
Expand Down
75 changes: 75 additions & 0 deletions cmd/parameters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright 2018-2019 Banco Bilbao Vizcaya Argentaria, S.A.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"fmt"
"net/url"
"strings"
)

const (
errMalformedURL = "malformed URL"
errMissingURLScheme = "missing URL Scheme"
errMissingURLHost = "missing URL Host"
errMissingURLPort = "missing URL Port"
errUnexpectedScheme = "unexpected URL Scheme"
)

func urlParse(endpoints ...string) error {
for _, endpoint := range endpoints {
url, err := url.Parse(endpoint)

if err != nil {
return fmt.Errorf("%s in %s", errMalformedURL, endpoint)
}

if url.Scheme == "" {
return fmt.Errorf("%s in %s", errMissingURLScheme, endpoint)
}

if url.Hostname() == "" {
return fmt.Errorf("%s in %s", errMissingURLHost, endpoint)
}
}
return nil
}

func urlParseNoSchemaRequired(endpoints ...string) error {
for _, endpoint := range endpoints {

if strings.Contains(endpoint, "://") {
return fmt.Errorf("%s in %s", errUnexpectedScheme, endpoint)
}

// Add fake scheme to get an expected result from url.Parse
url, err := url.Parse("http://" + endpoint)

if err != nil {
return fmt.Errorf("%s in %s", errMalformedURL, endpoint)
}

if url.Hostname() == "" {
return fmt.Errorf("%s in %s", errMissingURLHost, endpoint)
}

if url.Port() == "" {
return fmt.Errorf("%s in %s", errMissingURLPort, endpoint)
}
}
return nil
}

0 comments on commit 6653e3e

Please sign in to comment.