-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
131 lines (116 loc) · 2.79 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
package main
import (
"compress/gzip"
"context"
"io"
"log"
"net/http"
"os"
"path"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
s3Config "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/gin-gonic/gin"
)
func newS3Client(cfg *config) (*s3.Client, error) {
s3Cfg, err := s3Config.LoadDefaultConfig(context.TODO(),
s3Config.WithEndpointResolverWithOptions(
aws.EndpointResolverWithOptionsFunc(
func(service, region string, options ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{URL: cfg.S3.Endpoint}, nil
})),
s3Config.WithRegion(cfg.S3.Region),
s3Config.WithCredentialsProvider(
credentials.NewStaticCredentialsProvider(cfg.S3.Credentials.KeyID, cfg.S3.Credentials.SecretKey, ""),
),
)
if err != nil {
return nil, err
}
return s3.NewFromConfig(s3Cfg), err
}
func main() {
cfg, err := newConfig()
if err != nil {
log.Fatalf("error loading config: %v", err)
}
ercr, err := newEnricher(
enricherWithCRS("crs/coraza.conf", "crs/crs-setup.conf", "crs/rules/*.conf"),
enricherWithOptionalGeoIP(cfg.GeoIP.CityDBPath),
)
if err != nil {
log.Fatalf("error initializing enricher: %v", err)
}
s3Client, err := newS3Client(cfg)
if err != nil {
log.Fatalf("error initializing s3 client: %v", err)
}
r := gin.Default()
r.UseRawPath = true
r.UnescapePathValues = true
r.GET("/ecs/s3/:object_key", func(c *gin.Context) {
getInput := &s3.GetObjectInput{
Bucket: aws.String(cfg.S3.Bucket),
Key: aws.String(c.Param("object_key")),
}
resp, err := s3Client.GetObject(context.TODO(), getInput)
if err != nil {
c.String(500, err.Error())
return
}
defer resp.Body.Close()
body := resp.Body
if strings.Contains(*resp.ContentType, "application/gzip") {
body, err = gzip.NewReader(body)
if err != nil {
c.String(500, err.Error())
return
}
defer body.Close()
}
erc, err := ercr.EnrichRecord(body)
if err != nil {
c.String(500, err.Error())
return
}
defer erc.Close()
ecs, err := erc.toECS()
if err != nil {
c.String(500, err.Error())
return
}
c.JSON(http.StatusOK, ecs)
})
r.GET("/ecs/files/:filename", func(c *gin.Context) {
f, err := os.Open("./" + path.Clean(c.Param("filename")))
if err != nil {
c.String(500, err.Error())
return
}
defer f.Close()
body := io.ReadCloser(f)
if strings.HasSuffix(c.Param("filename"), ".gz") {
body, err = gzip.NewReader(body)
if err != nil {
c.String(500, err.Error())
return
}
defer body.Close()
}
er, err := ercr.EnrichRecord(f)
if err != nil {
c.String(500, err.Error())
return
}
defer er.Close()
ecs, err := er.toECS()
if err != nil {
c.String(500, err.Error())
return
}
c.JSON(http.StatusOK, ecs)
})
r.Run()
}