-
Notifications
You must be signed in to change notification settings - Fork 9
/
forwarder.go
292 lines (241 loc) · 6.9 KB
/
forwarder.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
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"regexp"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/aws/aws-sdk-go/service/secretsmanager"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
var logger = logrus.New()
func main() {
logger.SetFormatter(&logrus.JSONFormatter{})
logger.SetLevel(logrus.InfoLevel)
lambda.Start(handleRequest)
}
func handleRequest(ctx context.Context, event struct{}) error {
args, err := BuildArgs()
if err != nil {
return err
}
return Handler(args)
}
type awsCredential struct {
key string
secret string
}
type S3Ptr struct {
Region string
Bucket string
Key string
credential *awsCredential
}
func Handler(args Args) error {
forwardMessage := func(msg *FalconMessage) error {
t := time.Unix(int64(msg.Timestamp/1000), 0)
for _, f := range msg.Files {
logger.WithField("f", f).Info("forwarding")
src := S3Ptr{
Region: falconAwsRegion,
Bucket: msg.Bucket,
Key: f.Path,
credential: &awsCredential{
key: args.FalconAwsKey,
secret: args.FalconAwsSecret,
},
}
dst := S3Ptr{
Region: args.S3Region,
Bucket: args.S3Bucket,
Key: strings.Join([]string{
args.S3Prefix,
t.Format("2006/01/02/15/"),
f.Path,
}, ""),
}
err := ForwardS3File(src, dst)
if err != nil {
return err
}
}
return nil
}
err := ReceiveMessages(args.SqsURL, args.FalconAwsKey, args.FalconAwsSecret,
forwardMessage)
return err
}
var falconAwsRegion = "us-west-1"
func getSecretValues(secretArn string, values interface{}) error {
// sample: arn:aws:secretsmanager:ap-northeast-1:1234567890:secret:mytest
arn := strings.Split(secretArn, ":")
if len(arn) != 7 {
return errors.New(fmt.Sprintf("Invalid SecretsManager ARN format: %s", secretArn))
}
region := arn[3]
ssn := session.Must(session.NewSession(&aws.Config{
Region: aws.String(region),
}))
mgr := secretsmanager.New(ssn)
result, err := mgr.GetSecretValue(&secretsmanager.GetSecretValueInput{
SecretId: aws.String(secretArn),
})
if err != nil {
return errors.Wrap(err, "Fail to retrieve secret values")
}
err = json.Unmarshal([]byte(*result.SecretString), values)
if err != nil {
return errors.Wrap(err, "Fail to parse secret values as JSON")
}
return nil
}
// BuildArgs builds argument of receiver from environment variables.
func BuildArgs() (Args, error) {
args := Args{
S3Bucket: os.Getenv("S3_BUCKET"),
S3Prefix: os.Getenv("S3_PREFIX"),
S3Region: os.Getenv("S3_REGION"),
SqsURL: os.Getenv("SQS_URL"),
}
err := getSecretValues(os.Getenv("SECRET_ARN"), &args)
if err != nil {
return args, errors.Wrap(err, "Fail to retrieve secret values")
}
return args, nil
}
type Args struct {
S3Bucket string
S3Prefix string
S3Region string
SqsURL string
FalconAwsKey string `json:"falcon_aws_key"`
FalconAwsSecret string `json:"falcon_aws_secret"`
}
type FalconMessage struct {
CID string `json:"cid"`
Timestamp uint `json:"timestamp"`
FileCount int `json:"fileCount"`
TotalSize int `json:"totalSize"`
Bucket string `json:"bucket"`
PathPrefix string `json:"pathPrefix"`
Files []FalconLogFiles `json:"files"`
}
type FalconLogFiles struct {
Path string `json:"path"`
Size int `json:"size"`
CheckSum string `json:"checksum"`
}
func sqsURLtoRegion(url string) (string, error) {
urlPattern := []string{
// https://sqs.ap-northeast-1.amazonaws.com/21xxxxxxxxxxx/test-queue
`https://sqs\.([a-z0-9\-]+)\.amazonaws\.com`,
// https://us-west-1.queue.amazonaws.com/2xxxxxxxxxx/test-queue
`https://([a-z0-9\-]+)\.queue\.amazonaws\.com`,
}
for _, ptn := range urlPattern {
re := regexp.MustCompile(ptn)
group := re.FindSubmatch([]byte(url))
if len(group) == 2 {
return string(group[1]), nil
}
}
return "", errors.New("unsupported SQS URL syntax")
}
// ReceiveMessages receives SQS message from Falcon side and invokes msgHandler per message.
// In this method, not use channel because SQS queue deletion must be after handling messages
// to keep idempotence.
func ReceiveMessages(sqsURL, awsKey, awsSecret string, msgHandler func(msg *FalconMessage) error) error {
sqsRegion, err := sqsURLtoRegion(sqsURL)
if err != nil {
return err
}
cfg := aws.Config{Region: aws.String(sqsRegion)}
if awsKey != "" && awsSecret != "" {
cfg.Credentials = credentials.NewStaticCredentials(awsKey, awsSecret, "")
} else {
logger.Warn("AWS Key and secret are not set, use role permission")
}
queue := sqs.New(session.Must(session.NewSession(&cfg)))
for {
result, err := queue.ReceiveMessage(&sqs.ReceiveMessageInput{
AttributeNames: []*string{
aws.String(sqs.MessageSystemAttributeNameSentTimestamp),
},
MessageAttributeNames: []*string{
aws.String(sqs.QueueAttributeNameAll),
},
QueueUrl: &sqsURL,
MaxNumberOfMessages: aws.Int64(1),
VisibilityTimeout: aws.Int64(36000), // 10 hours
WaitTimeSeconds: aws.Int64(0),
})
if err != nil {
return errors.Wrap(err, "SQS recv error")
}
logger.WithField("result", result).Info("recv queue")
if len(result.Messages) == 0 {
break
}
for _, msg := range result.Messages {
fmsg := FalconMessage{}
err = json.Unmarshal([]byte(*msg.Body), &fmsg)
if err != nil {
return errors.Wrap(err, "Fail to parse Falcon SNS error")
}
if err = msgHandler(&fmsg); err != nil {
return err
}
}
_, err = queue.DeleteMessage(&sqs.DeleteMessageInput{
QueueUrl: &sqsURL,
ReceiptHandle: result.Messages[0].ReceiptHandle,
})
if err != nil {
return errors.Wrap(err, "SQS queue delete error")
}
}
return nil
}
func ForwardS3File(src, dst S3Ptr) error {
cfg := aws.Config{Region: aws.String(src.Region)}
if src.credential != nil {
cfg.Credentials = credentials.NewStaticCredentials(src.credential.key,
src.credential.secret, "")
} else {
logger.Warn("AWS Key and secret are not set, use role permission")
}
// Download
downSrv := s3.New(session.Must(session.NewSession(&cfg)))
getInput := &s3.GetObjectInput{
Bucket: aws.String(src.Bucket),
Key: aws.String(src.Key),
}
getResult, err := downSrv.GetObject(getInput)
if err != nil {
return errors.Wrap(err, "Fail to download data from Falcon")
}
// Upload
dstSsn := session.Must(session.NewSession(&aws.Config{
Region: aws.String(dst.Region),
}))
uploader := s3manager.NewUploader(dstSsn)
_, err = uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(dst.Bucket),
Key: aws.String(dst.Key),
Body: getResult.Body,
})
if err != nil {
return errors.Wrap(err, "Fail to upload data to your bucket")
}
return nil
}