Skip to content

Commit

Permalink
Refactor S3 client to make it unit-testable
Browse files Browse the repository at this point in the history
  • Loading branch information
Elliot Smith committed Feb 19, 2024
1 parent 6d1fe8b commit 6e82be8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 31 deletions.
42 changes: 42 additions & 0 deletions internal/objectstore/static_lpa_storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package objectstore

import (
"errors"
"fmt"

"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/ministryofjustice/opg-data-lpa-store/internal/shared"
)

/**
* For saving static copy of original LPA to S3
*/
type StaticLpaStorage struct {
client *S3Client
}

func (sls *StaticLpaStorage) Save(lpa *shared.Lpa) error {
// save a copy of the original to permanent storage,
// but only if the key doesn't already exist
objectKey := fmt.Sprintf("%s/donor-executed-lpa.json", lpa.Uid)
_, err := sls.client.Get(objectKey)

if err == nil {
// no error and 200 => bad (object already exists)
return fmt.Errorf("Could not save donor executed LPA as key %s already exists", objectKey)
}

// error which is a 404 => good (object should not already exist)
var nsk *types.NoSuchKey
if errors.As(err, &nsk) {
_, err = sls.client.Put(objectKey, lpa)
}

return err
}

func NewStaticLpaStorage(client *S3Client) *StaticLpaStorage {
return &StaticLpaStorage{
client: client,
}
}
45 changes: 14 additions & 31 deletions lambda/create/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"time"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/ministryofjustice/opg-data-lpa-store/internal/ddb"
"github.com/ministryofjustice/opg-data-lpa-store/internal/objectstore"
"github.com/ministryofjustice/opg-data-lpa-store/internal/shared"
Expand All @@ -27,20 +23,19 @@ type Store interface {
Get(ctx context.Context, uid string) (shared.Lpa, error)
}

type S3Client interface {
Put(objectKey string, obj any) (*s3.PutObjectOutput, error)
Get(objectKey string) (*s3.GetObjectOutput, error)
type StaticLpaStorage interface {
Save(lpa *shared.Lpa) error
}

type Verifier interface {
VerifyHeader(events.APIGatewayProxyRequest) (*shared.LpaStoreClaims, error)
}

type Lambda struct {
store Store
s3client S3Client
store Store
staticLpaStorage StaticLpaStorage
verifier Verifier
logger Logger
logger Logger
}

func (l *Lambda) HandleEvent(ctx context.Context, event events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
Expand Down Expand Up @@ -101,22 +96,8 @@ func (l *Lambda) HandleEvent(ctx context.Context, event events.APIGatewayProxyRe
return shared.ProblemInternalServerError.Respond()
}

// save a copy of the original to permanent storage,
// but only if the key doesn't already exist
objectKey := fmt.Sprintf("%s/donor-executed-lpa.json", data.Uid)
_, err = l.s3client.Get(objectKey)
if err == nil {
// 200 => bad (object already exists)
err = fmt.Errorf("Could not save donor executed LPA as key %s already exists", objectKey)
l.logger.Print(err)
return shared.ProblemInvalidRequest.Respond()
}

// 404 => good (object should not already exist)
var nsk *types.NoSuchKey
if errors.As(err, &nsk) {
_, err = l.s3client.Put(objectKey, data)
}
// save to static storage as JSON
err = l.staticLpaStorage.Save(&data)

if err != nil {
l.logger.Print(err)
Expand All @@ -132,17 +113,19 @@ func (l *Lambda) HandleEvent(ctx context.Context, event events.APIGatewayProxyRe

func main() {
l := &Lambda{
store: ddb.New(
store: ddb.New(
os.Getenv("AWS_DYNAMODB_ENDPOINT"),
os.Getenv("DDB_TABLE_NAME_DEEDS"),
os.Getenv("DDB_TABLE_NAME_CHANGES"),
),
s3client: objectstore.NewS3Client(
os.Getenv("S3_BUCKET_NAME_ORIGINAL"),
os.Getenv("AWS_S3_ENDPOINT"),
staticLpaStorage: objectstore.NewStaticLpaStorage(
objectstore.NewS3Client(
os.Getenv("S3_BUCKET_NAME_ORIGINAL"),
os.Getenv("AWS_S3_ENDPOINT"),
),
),
verifier: shared.NewJWTVerifier(),
logger: logging.New(os.Stdout, "opg-data-lpa-store"),
logger: logging.New(os.Stdout, "opg-data-lpa-store"),
}

lambda.Start(l.HandleEvent)
Expand Down

0 comments on commit 6e82be8

Please sign in to comment.