-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
140 lines (115 loc) · 3.92 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"time"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
dynamodbtypes "github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/app"
"github.com/ministryofjustice/opg-modernising-lpa/internal/dynamo"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/s3"
"github.com/ministryofjustice/opg-modernising-lpa/internal/uid"
)
const (
virusFound = "infected"
)
type uidEvent struct {
UID string `json:"uid"`
}
type dynamodbClient interface {
One(ctx context.Context, pk, sk string, v interface{}) error
OneByUID(ctx context.Context, uid string, v interface{}) error
Put(ctx context.Context, v interface{}) error
Update(ctx context.Context, pk, sk string, values map[string]dynamodbtypes.AttributeValue, expression string) error
}
type s3Client interface {
GetObjectTags(ctx context.Context, key string) ([]types.Tag, error)
}
type shareCodeSender interface {
SendCertificateProviderPrompt(context.Context, page.AppData, *actor.DonorProvidedDetails) error
}
type DocumentStore interface {
UpdateScanResults(ctx context.Context, lpaID, objectKey string, virusDetected bool) error
}
type UidStore interface {
Set(ctx context.Context, lpaID, sessionID, uid string) error
}
type UidClient interface {
CreateCase(context.Context, *uid.CreateCaseRequestBody) (string, error)
}
type Event struct {
events.S3Event
events.CloudWatchEvent
}
func (e Event) isS3Event() bool {
return len(e.Records) > 0
}
func (e Event) isCloudWatchEvent() bool {
return e.Source == "aws.cloudwatch" || e.Source == "opg.poas.makeregister" || e.Source == "opg.poas.sirius"
}
func handler(ctx context.Context, event Event) error {
var (
tableName = os.Getenv("LPAS_TABLE")
notifyIsProduction = os.Getenv("GOVUK_NOTIFY_IS_PRODUCTION") == "1"
appPublicURL = os.Getenv("APP_PUBLIC_URL")
awsBaseURL = os.Getenv("AWS_BASE_URL")
notifyBaseURL = os.Getenv("GOVUK_NOTIFY_BASE_URL")
evidenceBucketName = os.Getenv("UPLOADS_S3_BUCKET_NAME")
uidBaseURL = os.Getenv("UID_BASE_URL")
)
cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
return fmt.Errorf("failed to load default config: %w", err)
}
if len(awsBaseURL) > 0 {
cfg.EndpointResolverWithOptions = aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{
PartitionID: "aws",
URL: awsBaseURL,
SigningRegion: "eu-west-1",
}, nil
})
}
dynamoClient, err := dynamo.NewClient(cfg, tableName)
if err != nil {
return fmt.Errorf("failed to create dynamodb client: %w", err)
}
if event.isS3Event() {
s3Client := s3.NewClient(cfg, evidenceBucketName)
documentStore := app.NewDocumentStore(dynamoClient, nil, nil, nil, nil)
if err := handleObjectTagsAdded(ctx, dynamoClient, event.S3Event, s3Client, documentStore); err != nil {
return fmt.Errorf("ObjectTagging:Put: %w", err)
}
return nil
}
if event.isCloudWatchEvent() {
handler := &cloudWatchEventHandler{
dynamoClient: dynamoClient,
now: time.Now,
uidBaseURL: uidBaseURL,
cfg: cfg,
notifyIsProduction: notifyIsProduction,
notifyBaseURL: notifyBaseURL,
appPublicURL: appPublicURL,
}
if err := handler.Handle(ctx, event.CloudWatchEvent); err != nil {
return fmt.Errorf("%s: %w", event.DetailType, err)
}
log.Println("successfully handled ", event.DetailType)
return nil
}
eJson, _ := json.Marshal(event)
return fmt.Errorf("unknown event type received: %s", string(eJson))
}
func main() {
lambda.Start(handler)
}