Skip to content

Commit

Permalink
Merge pull request #181 from moov-io/fix-audit-paths
Browse files Browse the repository at this point in the history
fix: build paths correctly for audittrails
  • Loading branch information
adamdecaf authored May 25, 2023
2 parents a3a8677 + 50aa2e6 commit fe50d97
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
17 changes: 5 additions & 12 deletions internal/audittrail/storage_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import (
"context"
"fmt"
"io"
"path/filepath"

"github.com/moov-io/achgateway/internal/service"
"github.com/moov-io/base/strx"
"github.com/moov-io/cryptfs"

"gocloud.dev/blob"
Expand All @@ -25,16 +23,14 @@ import (
// blobStorage implements Storage with gocloud.dev/blob which allows
// clients to use AWS S3, GCP Storage, and Azure Storage.
type blobStorage struct {
id string
bucket *blob.Bucket
basePath string
cryptor *cryptfs.FS
id string
bucket *blob.Bucket
cryptor *cryptfs.FS
}

func newBlobStorage(cfg *service.AuditTrail) (*blobStorage, error) {
storage := &blobStorage{
id: cfg.ID,
basePath: strx.Or(cfg.BasePath, "outbound"),
id: cfg.ID,
}

bucket, err := blob.OpenBucket(context.Background(), cfg.BucketURI)
Expand Down Expand Up @@ -77,9 +73,6 @@ func (bs *blobStorage) SaveFile(path string, data []byte) error {
return err
}

// Add the bucket's base path before out file
path = filepath.Join(bs.basePath, path)

exists, err := bs.bucket.Exists(context.Background(), path)
if exists {
return nil
Expand Down Expand Up @@ -110,7 +103,7 @@ func (bs *blobStorage) SaveFile(path string, data []byte) error {
}

func (bs *blobStorage) GetFile(path string) (io.ReadCloser, error) {
r, err := bs.bucket.NewReader(context.Background(), filepath.Join(bs.basePath, path), nil)
r, err := bs.bucket.NewReader(context.Background(), path, nil)
if err != nil {
return nil, fmt.Errorf("get file: %v", err)
}
Expand Down
15 changes: 10 additions & 5 deletions internal/audittrail/storage_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
package audittrail

import (
"bytes"
"io"
"strings"
)

type MockStorage struct {
Err error
FileContents string
Err error

SavedFilepath string
SavedContents []byte
}

func newMockStorage() *MockStorage {
Expand All @@ -26,11 +28,14 @@ func (s *MockStorage) Close() error {
return s.Err
}

func (s *MockStorage) SaveFile(_ string, _ []byte) error {
func (s *MockStorage) SaveFile(path string, data []byte) error {
if s.Err != nil {
uploadFilesErrors.With("type", "mock", "id", "mock").Add(1)
} else {
uploadedFilesCounter.With("type", "mock", "id", "mock").Add(1)

s.SavedFilepath = path
s.SavedContents = data
}
return s.Err
}
Expand All @@ -39,5 +44,5 @@ func (s *MockStorage) GetFile(_ string) (io.ReadCloser, error) {
if s.Err != nil {
return nil, s.Err
}
return io.NopCloser(strings.NewReader(s.FileContents)), nil
return io.NopCloser(bytes.NewReader(s.SavedContents)), nil
}
5 changes: 5 additions & 0 deletions internal/incoming/odfi/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ func TestProcessor(t *testing.T) {
if err != nil {
require.ErrorContains(t, err, "record:FileHeader *ach.FieldError FileCreationDate is a mandatory field")
}

// Verify saved file path
ms, ok := auditSaver.storage.(*audittrail.MockStorage)
require.True(t, ok)
require.Equal(t, "odfi/ftp.foo.com/testdata/2023-05-25/HMBRAD_ACHEXPORT_1001_08_19_2022_09_10", ms.SavedFilepath)
}

func TestProcessor_populateHashes(t *testing.T) {
Expand Down
6 changes: 5 additions & 1 deletion internal/pipeline/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,11 @@ func (xfagg *aggregator) uploadFile(index int, agent upload.Agent, res *transfor
}

// Record the file in our audit trail
path := fmt.Sprintf("%s/%s/%s", agent.Hostname(), time.Now().Format("2006-01-02"), filename)
basePath := "outbound"
if xfagg.shard.Audit != nil {
basePath = xfagg.shard.Audit.BasePath
}
path := fmt.Sprintf("%s/%s/%s/%s", basePath, agent.Hostname(), time.Now().Format("2006-01-02"), filename)
if err := xfagg.auditStorage.SaveFile(path, buf.Bytes()); err != nil {
recordFileUploadError(xfagg.shard.Name)
return fmt.Errorf("problem saving file in audit record: %v", err)
Expand Down

0 comments on commit fe50d97

Please sign in to comment.