-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
66 lines (52 loc) · 1.88 KB
/
main.go
File metadata and controls
66 lines (52 loc) · 1.88 KB
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
package main
import (
"context"
"encoding/json"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/rs/zerolog/log"
)
type Response struct {
Message string `json:"message"`
}
var (
headers = map[string]string{
"Access-Control-Allow-Origin": "https://app.joinformal.com",
"Access-Control-Allow-Methods": "OPTIONS,POST",
}
)
func handler(ctx context.Context, event events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
log.Debug().Msgf("Received event: %s", event.Body)
parsed, err := parseFormalEncryptedData(event.Body)
if err != nil {
log.Error().Err(err).Msg("Error happen in parseFormalEncryptedData")
return events.APIGatewayProxyResponse{StatusCode: 400, Body: err.Error(), Headers: headers}, nil
}
log.Debug().Msgf("Parsed body: %+v", parsed)
dataKey, err := decryptDataKey(parsed.KmsKeyRegion, parsed.KmsKeyId, []byte(parsed.EncryptedKey))
if err != nil {
log.Error().Err(err).Msg("Error happen in decryptDataKey")
return events.APIGatewayProxyResponse{StatusCode: 500, Body: err.Error(), Headers: headers}, nil
}
decrypted, err := decryptString(parsed.EncryptedData, dataKey)
if err != nil {
log.Error().Err(err).Msg("Error happen in decryptString")
return events.APIGatewayProxyResponse{StatusCode: 500, Body: err.Error(), Headers: headers}, nil
}
log.Debug().Msgf("Decrypted data: %s", decrypted)
response := Response{Message: decrypted}
responseBody, err := json.Marshal(&response)
if err != nil {
log.Error().Err(err).Msg("Error happen in json.Marshal")
return events.APIGatewayProxyResponse{StatusCode: 500, Body: "Error marshalling the response body", Headers: headers}, nil
}
log.Debug().Msgf("Response body: %s", responseBody)
return events.APIGatewayProxyResponse{
StatusCode: 200,
Body: string(responseBody),
Headers: headers,
}, nil
}
func main() {
lambda.Start(handler)
}