-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
376 lines (348 loc) · 13.3 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package main
import (
"context"
"fmt"
"log"
"time"
"net/http"
"net/mail"
"os"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/ses"
"github.com/google/uuid"
)
// Determine if an email exists with the given id.
func emailExistsWithId(email string, id string) (bool, error) {
table := os.Getenv("DB_TABLE_NAME")
svc := dynamodb.New(session.New())
input := &dynamodb.GetItemInput{
// Get an item that matches email
Key: map[string]*dynamodb.AttributeValue{
"email": {
S: aws.String(email),
},
},
TableName: aws.String(table),
}
result, err := svc.GetItem(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case dynamodb.ErrCodeProvisionedThroughputExceededException:
log.Print(dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error())
case dynamodb.ErrCodeResourceNotFoundException:
log.Print(dynamodb.ErrCodeResourceNotFoundException, aerr.Error())
case dynamodb.ErrCodeRequestLimitExceeded:
log.Print(dynamodb.ErrCodeRequestLimitExceeded, aerr.Error())
case dynamodb.ErrCodeInternalServerError:
log.Print(dynamodb.ErrCodeInternalServerError, aerr.Error())
default:
log.Print(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
log.Print(err.Error())
}
}
if result.Item == nil {
return false, err
}
// Double check that the resulting email and id matches the input, return emailExistsWithId == true
if (*result.Item["email"].S == email) && (*result.Item["id"].S == id) {
return true, nil
}
log.Printf("No match for email: %s with id: %s", email, id)
return false, err
}
// Edits an existing email's attributes. No authorization is performed here, so ensure you check that values of email and id match before calling this function.
func updateItemInDynamoDB(email string, id string, timestamp string, confirm bool) (*dynamodb.UpdateItemOutput, error) {
table := os.Getenv("DB_TABLE_NAME")
svc := dynamodb.New(session.New())
input := &dynamodb.UpdateItemInput{
// Provide the key to use for finding the right item.
// Only matching on email means that a duplicate subscription request will override the first id.
Key: map[string]*dynamodb.AttributeValue{
"email": {
S: aws.String(email),
},
},
// Give the keys to be updated a shorthand to reference
ExpressionAttributeNames: map[string]*string{
"#ID": aws.String("id"),
"#T": aws.String("timestamp"),
"#C": aws.String("confirm"),
},
// Give the incoming values a shorthand to reference
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":idval": {
S: aws.String(id),
},
":timeval": {
S: aws.String(timestamp),
},
// Always override existing bool
":confirmval": {
BOOL: aws.Bool(confirm),
},
},
// Use the shorthand references to update these keys
UpdateExpression: aws.String("SET #C = :confirmval, #T = :timeval, #ID = :idval"),
TableName: aws.String(table),
}
result, err := svc.UpdateItem(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case dynamodb.ErrCodeConditionalCheckFailedException:
log.Print(dynamodb.ErrCodeConditionalCheckFailedException, aerr.Error())
case dynamodb.ErrCodeProvisionedThroughputExceededException:
log.Print(dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error())
case dynamodb.ErrCodeResourceNotFoundException:
log.Print(dynamodb.ErrCodeResourceNotFoundException, aerr.Error())
case dynamodb.ErrCodeItemCollectionSizeLimitExceededException:
log.Print(dynamodb.ErrCodeItemCollectionSizeLimitExceededException, aerr.Error())
case dynamodb.ErrCodeTransactionConflictException:
log.Print(dynamodb.ErrCodeTransactionConflictException, aerr.Error())
case dynamodb.ErrCodeRequestLimitExceeded:
log.Print(dynamodb.ErrCodeRequestLimitExceeded, aerr.Error())
case dynamodb.ErrCodeInternalServerError:
log.Print(dynamodb.ErrCodeInternalServerError, aerr.Error())
default:
log.Print(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
log.Print(err.Error())
}
}
return result, err
}
// Delete an email from the table if the id matches.
func deleteEmailFromDynamoDb(email string, id string) (*dynamodb.DeleteItemOutput, error) {
table := os.Getenv("DB_TABLE_NAME")
svc := dynamodb.New(session.New())
input := &dynamodb.DeleteItemInput{
Key: map[string]*dynamodb.AttributeValue{
"email": {
S: aws.String(email),
},
},
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":emailval": {
S: aws.String(email),
},
":idval": {
S: aws.String(id),
},
},
// Find an item that matches both email and id
ConditionExpression: aws.String("email = :emailval AND id = :idval"),
TableName: aws.String(table),
}
result, err := svc.DeleteItem(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case dynamodb.ErrCodeConditionalCheckFailedException:
log.Println(dynamodb.ErrCodeConditionalCheckFailedException, aerr.Error())
case dynamodb.ErrCodeProvisionedThroughputExceededException:
log.Println(dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error())
case dynamodb.ErrCodeResourceNotFoundException:
log.Println(dynamodb.ErrCodeResourceNotFoundException, aerr.Error())
case dynamodb.ErrCodeItemCollectionSizeLimitExceededException:
log.Println(dynamodb.ErrCodeItemCollectionSizeLimitExceededException, aerr.Error())
case dynamodb.ErrCodeTransactionConflictException:
log.Println(dynamodb.ErrCodeTransactionConflictException, aerr.Error())
case dynamodb.ErrCodeRequestLimitExceeded:
log.Println(dynamodb.ErrCodeRequestLimitExceeded, aerr.Error())
case dynamodb.ErrCodeInternalServerError:
log.Println(dynamodb.ErrCodeInternalServerError, aerr.Error())
default:
log.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
log.Print(err.Error())
}
}
return result, err
}
// Send a confirmation email with a link to complete subscription.
func sendEmailWithSES(email string, id string) (*ses.SendEmailOutput, error) {
svc := ses.New(session.New())
log.Print("EMAIL: ", email)
// HTML format
msg := fmt.Sprintf("<p>Hello! You're receiving this email because you requested a subscription to my list.</p><p>To complete your subscription, please click this link to finish signing up:</p><p><a class=\"ulink\" href=\"%s%s/?email=%s&id=%s\" target=\"_blank\">Confirm subscription</a>.</p><p>If you did not request this email, you can safely ignore it. Your email address has not yet been added to my list.</p>", os.Getenv("API_URL"), os.Getenv("VERIFY_PATH"), email, id)
// Plain text format
txt := fmt.Sprintf("Hello! You're receiving this email because you requested a subscription to my list.\n\nTo complete your subscription, please visit this link to finish signing up.\n\n%s%s/?email=%s&id=%s\n\nIf you did not request this email, you can safely ignore it. Your email address has not yet been added to my list.", os.Getenv("API_URL"), os.Getenv("VERIFY_PATH"), email, id)
// Build the "from" value
source := fmt.Sprintf("\"%s\" <%s>", os.Getenv("SENDER_NAME"), os.Getenv("SENDER_EMAIL"))
input := &ses.SendEmailInput{
Destination: &ses.Destination{
ToAddresses: []*string{
aws.String(email),
},
},
Message: &ses.Message{
Body: &ses.Body{
Html: &ses.Content{
Charset: aws.String("UTF-8"),
Data: aws.String(msg),
},
Text: &ses.Content{
Charset: aws.String("UTF-8"),
Data: aws.String(txt),
},
},
Subject: &ses.Content{
Charset: aws.String("UTF-8"),
Data: aws.String("Confirm your subscription"),
},
},
ReturnPath: aws.String(os.Getenv("SENDER_EMAIL")),
Source: aws.String(source),
}
result, err := svc.SendEmail(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case ses.ErrCodeMessageRejected:
log.Print(ses.ErrCodeMessageRejected, aerr.Error())
case ses.ErrCodeMailFromDomainNotVerifiedException:
log.Print(ses.ErrCodeMailFromDomainNotVerifiedException, aerr.Error())
case ses.ErrCodeConfigurationSetDoesNotExistException:
log.Print(ses.ErrCodeConfigurationSetDoesNotExistException, aerr.Error())
case ses.ErrCodeConfigurationSetSendingPausedException:
log.Print(ses.ErrCodeConfigurationSetSendingPausedException, aerr.Error())
case ses.ErrCodeAccountSendingPausedException:
log.Print(ses.ErrCodeAccountSendingPausedException, aerr.Error())
default:
log.Print(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
log.Print(err.Error())
}
return result, err
}
log.Print(result)
return result, nil
}
func lambdaHandler(ctx context.Context, event events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
errorPage := fmt.Sprintf("%s%s", os.Getenv("BASE_URL"), os.Getenv("ERROR_PAGE"))
successPage := fmt.Sprintf("%s%s", os.Getenv("BASE_URL"), os.Getenv("SUCCESS_PAGE"))
confirmSubscribe := fmt.Sprintf("%s%s", os.Getenv("BASE_URL"), os.Getenv("CONFIRM_SUBSCRIBE_PAGE"))
confirmUnsubscribe := fmt.Sprintf("%s%s", os.Getenv("BASE_URL"), os.Getenv("CONFIRM_UNSUBSCRIBE_PAGE"))
resp := events.APIGatewayV2HTTPResponse{Headers: make(map[string]string)}
resp.Headers["Access-Control-Allow-Origin"] = "*"
resp.StatusCode = http.StatusSeeOther
// Request a new subscription.
if event.RawPath == fmt.Sprintf("/%s/", os.Getenv("SUBSCRIBE_PATH")) {
// Parse email from query string
email, err := mail.ParseAddress(event.QueryStringParameters["email"])
if err != nil {
log.Print("Could not get email: ", err)
resp.Headers["Location"] = errorPage
return resp, err
}
// Add requested email, new id, timestamp, and confirm == false to the table.
id := uuid.New().String()
timestamp := time.Now().Format("2006-01-02 15:04:05")
_, uerr := updateItemInDynamoDB(email.Address, id, timestamp, false)
if uerr != nil {
log.Print("Could not update database: ", uerr)
resp.Headers["Location"] = errorPage
return resp, uerr
}
// Send confirmation email.
_, serr := sendEmailWithSES(email.Address, id)
if serr != nil {
log.Print("Could not send confirmation email: ", serr)
resp.Headers["Location"] = errorPage
return resp, serr
}
// Sends requester to the SUCCESS_PATH in all cases that do not result in an error.
// This mitigates enumeration.
resp.Headers["Location"] = confirmSubscribe
return resp, nil
}
// Verify a subscription and add email to list.
if event.RawPath == fmt.Sprintf("/%s/", os.Getenv("VERIFY_PATH")) {
// Parse email and id from query string.
email, emailpresent := event.QueryStringParameters["email"]
id, idpresent := event.QueryStringParameters["id"]
if (emailpresent == false) || (idpresent == false) {
log.Printf("Missing parameters in query string: %s", event.RawQueryString)
resp.Headers["Location"] = errorPage
return resp, nil
}
// Query for matching item. Both email and id must match.
match, err := emailExistsWithId(email, id)
if match == true {
// Set confirm == true and update timestamp for when they subscribed.
timestamp := time.Now().Format("2006-01-02 15:04:05")
success, uerr := updateItemInDynamoDB(email, id, timestamp, true)
if success != nil {
resp.Headers["Location"] = successPage
return resp, nil
}
if uerr != nil {
log.Printf("Could not update item in database: %s\n with query string: %s", uerr, event.RawQueryString)
resp.Headers["Location"] = errorPage
return resp, uerr
}
}
// If details don't match, return error.
if err != nil {
log.Printf("Received a bad confirmation request: %s", event.RawQueryString)
resp.Headers["Location"] = errorPage
return resp, err
}
}
// Delete an item from the list. Both email and id must match.
if event.RawPath == fmt.Sprintf("/%s/", os.Getenv("UNSUBSCRIBE_PATH")) {
// Parse email and id from query string.
email, emailpresent := event.QueryStringParameters["email"]
id, idpresent := event.QueryStringParameters["id"]
if (emailpresent == false) || (idpresent == false) {
log.Printf("Missing parameters in query string: %s", event.RawQueryString)
resp.Headers["Location"] = errorPage
return resp, nil
}
// Try to find a match
match, err := emailExistsWithId(email, id)
if match == true {
// There's a matching item, so try to delete it
_, derr := deleteEmailFromDynamoDb(email, id)
if derr == nil {
resp.Headers["Location"] = confirmUnsubscribe
} else {
log.Printf("Could not delete item: %s", derr)
resp.Headers["Location"] = errorPage
}
return resp, derr
}
// If details don't match, return error
if (match == false) || (err != nil) {
log.Printf("Received a bad deletion request with no match or an error. Error: %s\n Query string: %s", err, event.RawQueryString)
resp.Headers["Location"] = errorPage
return resp, err
}
}
// No event.RawPath match
log.Printf("No path match for path: %s", event.RawPath)
resp.Headers["Location"] = errorPage
return resp, nil
}
func main() {
lambda.Start(lambdaHandler)
}