-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yaml
300 lines (281 loc) · 9.01 KB
/
template.yaml
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
Lambda sending emails
Parameters:
CognitoAuthorizerArn:
Type: 'AWS::SSM::Parameter::Value<String>'
Description: Reference to Cognito UserPool for the stage
Default: CognitoAuthorizerArn
CustomDomain:
Type: 'AWS::SSM::Parameter::Value<String>'
Description: The default domain that is used as from_address
Default: CustomDomain
CustomDomainBasePath:
Type: String
Description: Base path mapping in CustomDomain
Default: email
ApiDomain:
Type: 'AWS::SSM::Parameter::Value<String>'
Description: The Api domain
Default: /NVA/ApiDomain
ScopusEmailBucketName:
Type: String
Description: Name of the bucket that receives the scopus emails
Default: scopus-emails
ScopusZipBucketName:
Type: String
Description: Name of the bucket that receives the scopus-zip files. It is created in the nva-publication stack
Default: "scopus-zip-files-v2"
SlackSnsArn:
Type: AWS::SSM::Parameter::Value<String>
Default: '/NVA/Monitoring/SlackSnsArn'
AllowedOrigins:
Type: String
Description: comma separated list of external clients that are allowed to contact the HTTP APIs, "*" indicates that all origins are allowed
Default: '*'
Globals:
Function:
Timeout: 900
MemorySize: 1798
Runtime: java17
Resources:
# Re-enable if Rest-api is needed
# DlrNvaEmailServiceRole:
# Type: AWS::IAM::Role
# Properties:
# AssumeRolePolicyDocument:
# Version: 2012-10-17
# Statement:
# - Effect: Allow
# Principal:
# Service: [ lambda.amazonaws.com ]
# Action: [ 'sts:AssumeRole' ]
# Policies:
# - PolicyName: writeLog
# PolicyDocument:
# Version: 2012-10-17
# Statement:
# - Effect: Allow
# Action:
# - logs:CreateLogGroup
# - logs:CreateLogStream
# - logs:PutLogEvents
# Resource: !Sub 'arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:*:*'
# - PolicyName: sendMail
# PolicyDocument:
# Version: 2012-10-17
# Statement:
# - Effect: Allow
# Action:
# - ses:SendEmail
# - ses:SendRawEmail
# Resource: "*"
DefaultLambdaRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: [ lambda.amazonaws.com ]
Action: [ 'sts:AssumeRole' ]
Policies:
- PolicyName: writeLog
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: !Sub 'arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:*:*'
- PolicyName: sendMail
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- s3:GetObject
- s3:ListBucket
- s3:ListBucketMultipartUploads
- s3:PutObject
- s3:PutObjectAcl
Resource: "*"
# Re-enable if Rest-api is needed
# EmailGatewayAPI:
# Type: AWS::Serverless::Api
# Properties:
# StageName: v1
# EndpointConfiguration:
# Type: REGIONAL
# MethodSettings:
# - ResourcePath: /email
# HttpMethod: POST
# DefinitionBody:
# 'Fn::Transform':
# Name: AWS::Include
# Parameters:
# Location: ./docs/openapi.yaml
# Re-enable if Rest-api is needed
# DlrNvaEmailServiceFunction:
# Type: AWS::Serverless::Function
# Properties:
# CodeUri: email-service
# Handler: no.sikt.nva.email.EmailRequestHandler::handleRequest
# Role: !GetAtt DlrNvaEmailServiceRole.Arn
# Runtime: java17
# MemorySize: 1798
# Environment:
# Variables:
# ALLOWED_ORIGIN: !Ref AllowedOrigins
# DEFAULT_FROM_ADDRESS: !Sub "no-reply@${CustomDomain}"
# Events:
# SendEmailRequest:
# Type: Api
# Properties:
# RestApiId: !Ref EmailGatewayAPI
# Method: post
# Path: /email
ReadScopusEmailFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: email-reader
Handler: no.sikt.nva.email.reader.handler.ScopusEmailReader::handleRequest
Role: !GetAtt DefaultLambdaRole.Arn
MemorySize: 8192
ReservedConcurrentExecutions: 1
Environment:
Variables:
SCOPUS_ZIP_BUCKET: !Sub "${ScopusZipBucketName}-${AWS::AccountId}"
Events:
Scopus:
Type: S3
Properties:
Bucket: !Ref ScopusEmailBucket
Events: 's3:ObjectCreated:*'
# Re-enable if Rest-api is needed
# EmailServiceBasePathMapping:
# Type: AWS::ApiGateway::BasePathMapping
# Properties:
# BasePath: !Sub ${CustomDomainBasePath}
# DomainName: !Ref ApiDomain
# RestApiId: !Ref EmailGatewayAPI
# Stage: !Ref EmailGatewayAPI.Stage
ScopusEmailBucket:
Type: AWS::S3::Bucket
Properties:
AccessControl: Private
BucketName: !Sub "${ScopusEmailBucketName}-${AWS::AccountId}"
LifecycleConfiguration:
Rules:
- Id: DeleteContentAfter20Days
Status: Enabled
ExpirationInDays: 20
# Set up automatically verified from_address domain: This will only work for Route 53 domains.
# Non route 53 domains needs manually updating certificates.
VerifiedFromSenderForSimpleEmail:
Type: AWS::SES::EmailIdentity
Properties:
DkimAttributes:
SigningEnabled: true
DkimSigningAttributes:
NextSigningKeyLength: 'RSA_2048_BIT'
EmailIdentity: !Ref CustomDomain
FeedbackAttributes:
EmailForwardingEnabled: true
ScopusImportEmailReceiver:
Type: AWS::SES::EmailIdentity
Properties:
EmailIdentity: !Sub 'scopus@${CustomDomain}'
AllowSesToPutToS3:
Type: AWS::S3::BucketPolicy
DependsOn: ScopusEmailBucket
Properties:
Bucket: !Ref ScopusEmailBucket
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- s3:PutObject
Resource: !Sub "arn:aws:s3:::scopus-emails-${AWS::AccountId}/*"
Principal:
Service: "ses.amazonaws.com"
Condition:
StringEquals:
'AWS:SourceAccount': !Ref AWS::AccountId
ScopusEmailRuleSet:
Type: AWS::SES::ReceiptRuleSet
Properties:
RuleSetName: !Sub 'email-receive-ruleset-${AWS::AccountId}'
MoveScopusEmailsToS3BucketRule:
Type: AWS::SES::ReceiptRule
DependsOn:
- ScopusEmailBucket
- AllowSesToPutToS3
- ScopusEmailRuleSet
Properties:
RuleSetName: !Ref ScopusEmailRuleSet
Rule:
Name: !Sub 'scopus-email-rule-${AWS::AccountId}'
Recipients:
- !Sub 'scopus@${CustomDomain}'
Enabled: true
ScanEnabled: true # when we get ip-address for
Actions:
- S3Action:
BucketName: !Sub "${ScopusEmailBucketName}-${AWS::AccountId}"
CheckThatScopusEmailWasReceivedFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: email-reader
Handler: no.sikt.nva.email.reader.handler.VerifyScopusEmailReceivedHandler::handleRequest
Role: !GetAtt DefaultLambdaRole.Arn
Environment:
Variables:
SCOPUS_EMAIL_BUCKET_NAME: !Sub "${ScopusEmailBucketName}-${AWS::AccountId}"
Events:
CheckForEmail:
Type: Schedule
Properties:
Schedule: 'cron(0 23 ? * WED *)'
VerifyScopusEmailReceivedAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: No scopus email received
AlarmDescription: No scopus emails younger than 24 hours found in s3 bucket
Namespace: AWS/Lambda
MetricName: Errors
Dimensions:
- Name: FunctionName
Value: !Ref CheckThatScopusEmailWasReceivedFunction
Statistic: Sum
Period: 300
EvaluationPeriods: 1
Threshold: 1
DatapointsToAlarm: 1
ComparisonOperator: GreaterThanOrEqualToThreshold
AlarmActions:
- !Ref SlackSnsArn
TooManyEmailsReceived:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: Too many scopus emails received in a day
AlarmDescription: If this alarm is triggered, then check that we are not being spammed
AlarmActions:
- !Ref SlackSnsArn
MetricName: Received
Namespace: AWS/SES
Statistic: Sum
Dimensions:
- Name: RuleSetName
Value: !Sub "email-receive-ruleset-${AWS::AccountId}"
Period: 86400
EvaluationPeriods: 1
DatapointsToAlarm: 1
Threshold: 1
ComparisonOperator: GreaterThanThreshold
TreatMissingData: missing