Repository: moov-io/customers
Pull request #114 (2020-08-10 18:17:44)

fed, paygate, watchman: config for debugging API calls
Fixes: https://github.com/moov-io/customers/issues/111
Fixes: https://github.com/moov-io/customers/issues/112
Fixes: https://github.com/moov-io/customers/issues/113


Patch:
diff --git a/cmd/server/fed/client.go b/cmd/server/fed/client.go
index 75a8849..329f3fb 100644
--- a/cmd/server/fed/client.go
+++ b/cmd/server/fed/client.go
@@ -90,9 +90,10 @@ var (
 	}
 )
 
-func NewClient(logger log.Logger, endpoint string) Client {
+func NewClient(logger log.Logger, endpoint string, debug bool) Client {
 	conf := moovfed.NewConfiguration()
 	conf.HTTPClient = httpClient
+	conf.Debug = debug
 
 	if endpoint != "" {
 		conf.BasePath = endpoint
diff --git a/cmd/server/fed/fed.go b/cmd/server/fed/fed.go
index 9c99c9f..c8cb128 100644
--- a/cmd/server/fed/fed.go
+++ b/cmd/server/fed/fed.go
@@ -13,8 +13,8 @@ import (
 	"github.com/go-kit/kit/log"
 )
 
-func Cache(logger log.Logger, endpoint string) Client {
-	client := NewClient(logger, endpoint)
+func Cache(logger log.Logger, endpoint string, debug bool) Client {
+	client := NewClient(logger, endpoint, debug)
 
 	data := util.Or(os.Getenv("FED_CACHE_SIZE"), "1024")
 	maxSize, _ := strconv.ParseInt(data, 10, 32)
diff --git a/cmd/server/main.go b/cmd/server/main.go
index 12e2243..81bf382 100644
--- a/cmd/server/main.go
+++ b/cmd/server/main.go
@@ -116,7 +116,9 @@ func main() {
 	signer := setupStorageBucket(logger, bucketName, cloudProvider)
 
 	// Create our Watchman client
-	watchmanClient := newWatchmanClient(logger, util.Or(os.Getenv("WATCHMAN_ENDPOINT"), os.Getenv("OFAC_ENDPOINT")))
+	debugWatchmanCalls := util.Or(os.Getenv("WATCHMAN_DEBUG_CALLS"), "false")
+	watchmanEndpoint := util.Or(os.Getenv("WATCHMAN_ENDPOINT"), os.Getenv("OFAC_ENDPOINT"))
+	watchmanClient := newWatchmanClient(logger, watchmanEndpoint, util.Yes(debugWatchmanCalls))
 	if watchmanClient == nil {
 		panic("No Watchman client created, see WATCHMAN_ENDPOINT")
 	}
@@ -150,10 +152,12 @@ func main() {
 	}
 	transitStringKeeper := secrets.NewStringKeeper(transitKeeper, 10*time.Second)
 
-	fedClient := fed.Cache(logger, os.Getenv("FED_ENDPOINT"))
+	debugFedCalls := util.Or(os.Getenv("FED_DEBUG_CALLS"), "false")
+	fedClient := fed.Cache(logger, os.Getenv("FED_ENDPOINT"), util.Yes(debugFedCalls))
 	adminServer.AddLivenessCheck("fed", fedClient.Ping)
 
-	paygateClient := paygate.NewClient(logger, os.Getenv("PAYGATE_ENDPOINT"))
+	debugPayGateCalls := util.Or(os.Getenv("PAYGATE_DEBUG_CALLS"), "false")
+	paygateClient := paygate.NewClient(logger, os.Getenv("PAYGATE_ENDPOINT"), util.Yes(debugPayGateCalls))
 	adminServer.AddLivenessCheck("paygate", paygateClient.Ping)
 
 	// Setup business HTTP routes
diff --git a/cmd/server/paygate/client.go b/cmd/server/paygate/client.go
index 55550b7..638fc30 100644
--- a/cmd/server/paygate/client.go
+++ b/cmd/server/paygate/client.go
@@ -109,9 +109,10 @@ var (
 	}
 )
 
-func NewClient(logger log.Logger, endpoint string) Client {
+func NewClient(logger log.Logger, endpoint string, debug bool) Client {
 	conf := client.NewConfiguration()
 	conf.HTTPClient = httpClient
+	conf.Debug = debug
 
 	if endpoint != "" {
 		conf.BasePath = endpoint
diff --git a/cmd/server/watchman.go b/cmd/server/watchman.go
index 85280fd..60882c9 100644
--- a/cmd/server/watchman.go
+++ b/cmd/server/watchman.go
@@ -162,9 +162,10 @@ func (c *moovWatchmanClient) Search(ctx context.Context, name string, requestID
 //
 // endpoint is a DNS record responsible for routing us to an Watchman instance.
 // Example: http://watchman.apps.svc.cluster.local:8080
-func newWatchmanClient(logger log.Logger, endpoint string) WatchmanClient {
+func newWatchmanClient(logger log.Logger, endpoint string, debug bool) WatchmanClient {
 	conf := watchman.NewConfiguration()
 	conf.BasePath = "http://localhost" + bind.HTTP("watchman")
+	conf.Debug = debug
 
 	if k8s.Inside() {
 		conf.BasePath = "http://watchman.apps.svc.cluster.local:8080"
diff --git a/internal/util/util.go b/internal/util/util.go
index 3530625..e854c98 100644
--- a/internal/util/util.go
+++ b/internal/util/util.go
@@ -5,6 +5,7 @@
 package util
 
 import (
+	"strconv"
 	"strings"
 )
 
@@ -17,3 +18,13 @@ func Or(options ...string) string {
 	}
 	return ""
 }
+
+// Yes returns true if the provided case-insensitive string matches 'yes' and is used to parse config values.
+func Yes(in string) bool {
+	in = strings.TrimSpace(in)
+	if strings.EqualFold(in, "yes") {
+		return true
+	}
+	v, _ := strconv.ParseBool(in)
+	return v
+}


Test patch:
diff --git a/cmd/server/fed/client_test.go b/cmd/server/fed/client_test.go
index 27f981f..b4b93a5 100644
--- a/cmd/server/fed/client_test.go
+++ b/cmd/server/fed/client_test.go
@@ -21,7 +21,7 @@ func TestFED(t *testing.T) {
 		w.Write([]byte(`PONG`))
 	}))
 
-	client := NewClient(log.NewNopLogger(), svc.URL)
+	client := NewClient(log.NewNopLogger(), svc.URL, false)
 	if err := client.Ping(); err != nil {
 		t.Fatal(err)
 	}
@@ -37,7 +37,7 @@ func TestFED(t *testing.T) {
 		w.Write([]byte(`{"achParticipants": [{"routingNumber": "121042882"}]}`)) // partial fed.AchDictionary response
 	}))
 
-	client = NewClient(log.NewNopLogger(), svc.URL)
+	client = NewClient(log.NewNopLogger(), svc.URL, false)
 	if details, err := client.LookupInstitution("121042882"); err != nil {
 		t.Fatal(err)
 	} else {
@@ -49,7 +49,7 @@ func TestFED(t *testing.T) {
 }
 
 func TestFED__NewClient(t *testing.T) {
-	client := NewClient(log.NewNopLogger(), "")
+	client := NewClient(log.NewNopLogger(), "", false)
 	if client == nil {
 		t.Error("expected FED client")
 	}
diff --git a/cmd/server/paygate/client_test.go b/cmd/server/paygate/client_test.go
index 2661fbf..fa686d1 100644
--- a/cmd/server/paygate/client_test.go
+++ b/cmd/server/paygate/client_test.go
@@ -71,7 +71,7 @@ func spawnPayGate(t *testing.T) *deployment {
 		t.Fatal(err)
 	}
 
-	client := NewClient(log.NewNopLogger(), fmt.Sprintf("http://localhost:%s", resource.GetPort("8080/tcp")))
+	client := NewClient(log.NewNopLogger(), fmt.Sprintf("http://localhost:%s", resource.GetPort("8080/tcp")), false)
 	err = pool.Retry(func() error {
 		return client.Ping()
 	})
@@ -118,7 +118,7 @@ func writePayGateConfig(t *testing.T, dir string, tag string) {
 }
 
 func TestPayGate__client(t *testing.T) {
-	if client := NewClient(log.NewNopLogger(), ""); client == nil {
+	if client := NewClient(log.NewNopLogger(), "", false); client == nil {
 		t.Fatal("expected non-nil client")
 	}
 
diff --git a/cmd/server/watchman_test.go b/cmd/server/watchman_test.go
index a007603..c1ac91c 100644
--- a/cmd/server/watchman_test.go
+++ b/cmd/server/watchman_test.go
@@ -73,7 +73,7 @@ func spawnWatchman(t *testing.T) *watchmanDeployment {
 		t.Fatal(err)
 	}
 
-	client := newWatchmanClient(log.NewNopLogger(), fmt.Sprintf("http://localhost:%s", resource.GetPort("8080/tcp")))
+	client := newWatchmanClient(log.NewNopLogger(), fmt.Sprintf("http://localhost:%s", resource.GetPort("8080/tcp")), false)
 	err = pool.Retry(func() error {
 		return client.Ping()
 	})
@@ -85,7 +85,7 @@ func spawnWatchman(t *testing.T) *watchmanDeployment {
 
 func TestWatchman__client(t *testing.T) {
 	endpoint := ""
-	if client := newWatchmanClient(log.NewNopLogger(), endpoint); client == nil {
+	if client := newWatchmanClient(log.NewNopLogger(), endpoint, false); client == nil {
 		t.Fatal("expected non-nil client")
 	}
 
diff --git a/internal/util/util_test.go b/internal/util/util_test.go
index eb6fb08..11f10dd 100644
--- a/internal/util/util_test.go
+++ b/internal/util/util_test.go
@@ -19,3 +19,23 @@ func TestOr(t *testing.T) {
 		t.Errorf("v=%s", v)
 	}
 }
+
+func TestYes(t *testing.T) {
+	if !Yes("yes") {
+		t.Error("expected true")
+	}
+	if Yes("no") {
+		t.Error("expected no")
+	}
+
+	if !Yes("  true") {
+		t.Error("expected true")
+	}
+	if Yes("false") {
+		t.Error("expected no")
+	}
+
+	if Yes("") {
+		t.Error("expected no")
+	}
+}