-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #279 from ahrtmn/ach-driver-put-file-check
filedrive: add ach FTP driver with PutFile checks
- Loading branch information
Showing
4 changed files
with
132 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package filedrive | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/moov-io/ach" | ||
"github.com/moov-io/base/log" | ||
"github.com/moov-io/base/telemetry" | ||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/trace" | ||
"goftp.io/server/core" | ||
) | ||
|
||
// ACHDriver wraps the goftp driver to add additional logic and error checking. | ||
type ACHDriver struct { | ||
core.Driver | ||
|
||
logger log.Logger | ||
validateOpts *ach.ValidateOpts | ||
} | ||
|
||
func NewACHDriver(logger log.Logger, validateOpts *ach.ValidateOpts, driver core.Driver) *ACHDriver { | ||
return &ACHDriver{ | ||
Driver: driver, | ||
logger: logger, | ||
validateOpts: validateOpts, | ||
} | ||
} | ||
|
||
// PutFile overrides the existing method to prevent erroneous ACH files from being uploaded. | ||
func (d *ACHDriver) PutFile(path string, r io.Reader, appendData bool) (int64, error) { | ||
_, span := telemetry.StartSpan(context.Background(), "put-file", trace.WithAttributes( | ||
attribute.String("ftp.destination", path), | ||
)) | ||
defer span.End() | ||
|
||
d.logger.Info().Log(fmt.Sprintf("receiving file for %s", path)) | ||
|
||
// Read the file that was uploaded | ||
var buf bytes.Buffer | ||
tee := io.TeeReader(r, &buf) | ||
|
||
reader := ach.NewReader(tee) | ||
reader.SetValidation(d.validateOpts) | ||
|
||
file, err := reader.Read() | ||
if err != nil { | ||
span.RecordError(err) | ||
d.logger.Error().Log(fmt.Sprintf("ftp: error reading ACH file %s: %v", path, err)) | ||
return 0, err | ||
} | ||
|
||
if err := file.Create(); err != nil { | ||
d.logger.Error().Log(fmt.Sprintf("ftp: error creating file %s: %v", path, err)) | ||
return 0, err | ||
} | ||
|
||
span.SetAttributes(attribute.Int("ftp.file_size_bytes", buf.Len())) | ||
d.logger.Info().Log(fmt.Sprintf("accepting file at %s", path)) | ||
|
||
// Call the original PutFile method with a reset reader. | ||
return d.Driver.PutFile(path, &buf, appendData) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package filedrive | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/moov-io/base/log" | ||
"github.com/stretchr/testify/require" | ||
"goftp.io/server/core" | ||
) | ||
|
||
// MockDriver is a simple mock implementation of core.Driver for testing purposes. | ||
type MockDriver struct { | ||
core.Driver | ||
} | ||
|
||
func (m *MockDriver) PutFile(path string, r io.Reader, appendData bool) (int64, error) { | ||
// Mock implementation, just return success | ||
return 0, nil | ||
} | ||
|
||
func TestACHDriver_PutFile_InvalidACH(t *testing.T) { | ||
mockDriver := &MockDriver{} | ||
customDriver := NewACHDriver(log.NewDefaultLogger(), nil, mockDriver) | ||
|
||
// Create an invalid ACH file (e.g., missing required fields) | ||
var invalidACH bytes.Buffer | ||
invalidACH.WriteString("invalid ACH content") | ||
|
||
// Attempt to upload the invalid ACH file | ||
_, err := customDriver.PutFile("invalid.ach", &invalidACH, false) | ||
|
||
// Verify that an error is returned | ||
require.Error(t, err) | ||
} | ||
|
||
func TestACHDriver_PutFile(t *testing.T) { | ||
mockDriver := &MockDriver{} | ||
customDriver := NewACHDriver(log.NewDefaultLogger(), nil, mockDriver) | ||
|
||
achFile, err := os.Open(filepath.Join("..", "..", "testdata", "20230809-144155-102000021C.ach")) | ||
require.NoError(t, err) | ||
defer achFile.Close() | ||
|
||
// Attempt to upload the valid ACH file | ||
_, err = customDriver.PutFile("valid.ach", achFile, false) | ||
|
||
// Verify that no error is returned | ||
require.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters