From 0c7118cb76136905e730bd69429fefeb29de6fca Mon Sep 17 00:00:00 2001 From: Kristi Burnette Date: Tue, 5 Mar 2024 09:17:34 -0700 Subject: [PATCH 1/2] fix: wire file parse bug --- cmd/server/reader.go | 4 ++-- pkg/download/download.go | 27 ++++++++++++++++----------- pkg/download/download_test.go | 11 +++++++---- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/cmd/server/reader.go b/cmd/server/reader.go index 7c9d7cc..6b822d2 100644 --- a/cmd/server/reader.go +++ b/cmd/server/reader.go @@ -35,9 +35,9 @@ func fedACHDataFile(logger log.Logger) (io.Reader, error) { } func fedWireDataFile(logger log.Logger) (io.Reader, error) { - file, err := attemptFileDownload(logger, "fedach") + file, err := attemptFileDownload(logger, "fedwire") if err != nil && !errors.Is(err, download.ErrMissingConfigValue) { - return nil, fmt.Errorf("problem downloading fedach: %v", err) + return nil, fmt.Errorf("problem downloading fedwire: %v", err) } if file != nil { diff --git a/pkg/download/download.go b/pkg/download/download.go index eeab7fd..557b21c 100644 --- a/pkg/download/download.go +++ b/pkg/download/download.go @@ -11,6 +11,7 @@ import ( "io" "net/http" "net/url" + "os" "time" ) @@ -48,23 +49,27 @@ func NewClient(opts *ClientOpts) (*Client, error) { } } - if opts.RoutingNumber == "" { - return nil, fmt.Errorf("%w: %w", ErrMissingConfigValue, ErrMissingRoutingNumber) - } + routingNum, rnExists := os.LookupEnv("FRB_ROUTING_NUMBER") + downloadcd, dcdExists := os.LookupEnv("FRB_DOWNLOAD_CODE") + downloadurltemp, urlExists := os.LookupEnv("FRB_DOWNLOAD_URL_TEMPLATE") - if opts.RoutingNumber == "" { - return nil, fmt.Errorf("%w: %w", ErrMissingConfigValue, ErrMissingDownloadCD) - } + if !urlExists { + if !rnExists { + return nil, fmt.Errorf("%w: %w", ErrMissingConfigValue, ErrMissingRoutingNumber) + } + + if !dcdExists { + return nil, fmt.Errorf("%w: %w", ErrMissingConfigValue, ErrMissingDownloadCD) + } - if opts.DownloadURL == "" { - opts.DownloadURL = DefaultFRBDownloadURLTemplate + downloadurltemp = DefaultFRBDownloadURLTemplate } return &Client{ httpClient: opts.HTTPClient, - routingNumber: opts.RoutingNumber, - downloadCode: opts.DownloadCode, - downloadURL: opts.DownloadURL, + routingNumber: routingNum, + downloadCode: downloadcd, + downloadURL: downloadurltemp, }, nil } diff --git a/pkg/download/download_test.go b/pkg/download/download_test.go index 7d0c0fe..38a1e56 100644 --- a/pkg/download/download_test.go +++ b/pkg/download/download_test.go @@ -95,11 +95,14 @@ func TestClient__fedwire(t *testing.T) { } func TestClient__wire_custom_url(t *testing.T) { - file, err := os.ReadFile(filepath.Join("..", "..", "data", "fedachdir.json")) + file, err := os.ReadFile(filepath.Join("..", "..", "data", "fedwiredir.json")) if err != nil { t.Fatal(err) } mockHTTPServer := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { + if request.URL.Path != "/fedwire" { + writer.WriteHeader(http.StatusNotFound) + } fmt.Fprint(writer, string(file)) })) defer mockHTTPServer.Close() @@ -110,19 +113,19 @@ func TestClient__wire_custom_url(t *testing.T) { client := setupClient(t) - fedach, err := client.GetList("fedwire") + fedwire, err := client.GetList("fedwire") if err != nil { t.Fatal(err) } - buf, ok := fedach.(*bytes.Buffer) + buf, ok := fedwire.(*bytes.Buffer) require.True(t, ok) if n := buf.Len(); n < 1024 { t.Errorf("unexpected size of %d bytes", n) } - bs, _ := io.ReadAll(io.LimitReader(fedach, 10024)) + bs, _ := io.ReadAll(io.LimitReader(fedwire, 10024)) if !bytes.Equal(bs, file) { t.Errorf("unexpected output:\n%s", string(bs)) } From 3e4771e073425d1ef875047ca0f493fdfdad1f2e Mon Sep 17 00:00:00 2001 From: Kristi Burnette Date: Tue, 5 Mar 2024 09:46:18 -0700 Subject: [PATCH 2/2] Improve logic that parses file as json or plaintext --- ACHDictionary.go | 9 +++++++-- WIREDictionary.go | 9 +++++++-- cmd/server/reader.go | 10 +--------- pkg/download/download.go | 6 +++--- pkg/download/download_test.go | 2 +- 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/ACHDictionary.go b/ACHDictionary.go index a82440a..9daf3e6 100644 --- a/ACHDictionary.go +++ b/ACHDictionary.go @@ -121,10 +121,15 @@ func (f *ACHDictionary) Read(r io.Reader) error { return err } - // Try reading the file as JSON and if that fails read as plaintext - if err := f.readJSON(bytes.NewReader(bs)); err == nil { + // Try validating the file as JSON and if that fails read as plaintext + if json.Valid(bs) { + err = f.readJSON(bytes.NewReader(bs)) + if err != nil { + return err + } return nil } + return f.readPlaintext(bytes.NewReader(bs)) } diff --git a/WIREDictionary.go b/WIREDictionary.go index f0c314c..83e75a1 100644 --- a/WIREDictionary.go +++ b/WIREDictionary.go @@ -119,10 +119,15 @@ func (f *WIREDictionary) Read(r io.Reader) error { return err } - // Try reading the file as JSON and if that fails read as plaintext - if err := f.readJSON(bytes.NewReader(bs)); err == nil { + // Try validating the file as JSON and if that fails read as plaintext + if json.Valid(bs) { + err = f.readJSON(bytes.NewReader(bs)) + if err != nil { + return err + } return nil } + return f.readPlaintext(bytes.NewReader(bs)) } diff --git a/cmd/server/reader.go b/cmd/server/reader.go index 6b822d2..d23f2fc 100644 --- a/cmd/server/reader.go +++ b/cmd/server/reader.go @@ -55,16 +55,8 @@ func fedWireDataFile(logger log.Logger) (io.Reader, error) { } func attemptFileDownload(logger log.Logger, listName string) (io.Reader, error) { - routingNumber := os.Getenv("FRB_ROUTING_NUMBER") - downloadCode := os.Getenv("FRB_DOWNLOAD_CODE") - downloadURL := os.Getenv("FRB_DOWNLOAD_URL_TEMPLATE") - logger.Logf("download: attempting %s", listName) - client, err := download.NewClient(&download.ClientOpts{ - RoutingNumber: routingNumber, - DownloadCode: downloadCode, - DownloadURL: downloadURL, - }) + client, err := download.NewClient(nil) if err != nil { return nil, fmt.Errorf("client setup: %w", err) } diff --git a/pkg/download/download.go b/pkg/download/download.go index 557b21c..6d45b85 100644 --- a/pkg/download/download.go +++ b/pkg/download/download.go @@ -53,12 +53,12 @@ func NewClient(opts *ClientOpts) (*Client, error) { downloadcd, dcdExists := os.LookupEnv("FRB_DOWNLOAD_CODE") downloadurltemp, urlExists := os.LookupEnv("FRB_DOWNLOAD_URL_TEMPLATE") - if !urlExists { - if !rnExists { + if !urlExists || downloadurltemp == "" { + if !rnExists || routingNum == "" { return nil, fmt.Errorf("%w: %w", ErrMissingConfigValue, ErrMissingRoutingNumber) } - if !dcdExists { + if !dcdExists || downloadcd == "" { return nil, fmt.Errorf("%w: %w", ErrMissingConfigValue, ErrMissingDownloadCD) } diff --git a/pkg/download/download_test.go b/pkg/download/download_test.go index 38a1e56..919f0d3 100644 --- a/pkg/download/download_test.go +++ b/pkg/download/download_test.go @@ -95,7 +95,7 @@ func TestClient__fedwire(t *testing.T) { } func TestClient__wire_custom_url(t *testing.T) { - file, err := os.ReadFile(filepath.Join("..", "..", "data", "fedwiredir.json")) + file, err := os.ReadFile(filepath.Join("..", "..", "data", "fpddir.json")) if err != nil { t.Fatal(err) }