-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitscan.go
358 lines (309 loc) · 10.3 KB
/
bitscan.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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"path"
"strings"
"time"
"github.com/whats-this/cdn-origin/weed"
log "github.com/Sirupsen/logrus"
"github.com/buaazp/fasthttprouter"
"github.com/dchest/uniuri"
"github.com/sheenobu/go-clamscan"
"github.com/spf13/viper"
"github.com/valyala/fasthttp"
)
// currentAPIVersion represents the current API base path.
const currentAPIBasePath = "/v1"
// version is the current version of bitscan.
const version = "0.1.0"
// folderFileMode is the default file mode for the temporary file directory.
const folderFileMode os.FileMode = os.ModeDir | 0770
// objectFileMode is the default file mode for objects stored in the temporary file directory.
const objectFileMode os.FileMode = 0660
// applicationJSON is the JSON header prefix expected on routes that consume JSON data.
const applicationJSON = "application/json"
// contentType is the content type header to read request content type from.
const contentType = "Content-Type"
// attachment represents a Slack webhook attachment.
type attachment struct {
Fallback string `json:"fallback"`
Color string `json:"color"`
Title string `json:"title"`
Text string `json:"text"`
}
// object represents a bitbin object.
type object struct {
BucketKey string `json:"bucket_key"`
Bucket string `json:"bucket"`
Key string `json:"key"`
Dir string `json:"dir"`
Type uint8 `json:"type"`
BackendFileID *string `json:"backend_file_id"`
DestURL *string `json:"dest_url"`
ContentType *string `json:"content_type"`
ContentLength *uint `json:"content_length"`
AuthHash *string `json:"auth_hash"`
CreatedAt *string `json:"created_at"`
MD5Hash *string `json:"md5_hash"`
}
func init() {
// http.listenAddress (string=":8080"): TCP address to listen to for HTTP requests
viper.SetDefault("http.listenAddress", ":8080")
viper.BindEnv("HTTP_LISTEN_ADDRESS", "http.listenAddress")
// log.debug (bool=false): enable debug mode (logs requests and prints other information)
viper.SetDefault("log.debug", false)
viper.BindEnv("log.debug", "DEBUG")
// notifications.slackWebhookURL (string=""): optional webhook URL for Slack-compatible notification messages
// (error + positive files)
viper.SetDefault("notifications.slackWebhookURL", "")
viper.BindEnv("SLACK_WEBHOOK_URL", "notifications.slackWebhookURL")
// seaweed.masterURL* (string): SeaweedFS master URL
viper.SetDefault("seaweed.masterURL", "http://localhost:9333")
viper.BindEnv("SEAWEED_MASTER_URL", "seaweed.masterURL")
// Configuration file settings
viper.SetConfigType("toml")
viper.SetConfigName("bitscan")
viper.AddConfigPath(".")
viper.AddConfigPath("/etc/bitscan/")
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
log.WithField("err", err).Fatal("failed to read in configuration")
}
}
// Enable debug mode
if viper.GetBool("log.debug") {
log.SetLevel(log.DebugLevel)
}
}
// httpClient to use for webhook requests.
var httpClient = &http.Client{
Jar: nil,
Timeout: time.Minute * 5,
}
// seaweed client to use for fetching files from the SeaweedFS cluster.
var seaweed *weed.Seaweed
// tmpDir is the directory used for storing downloaded files before they are scanned.
var tmpDir string
func main() {
var err error
// Attempt to connect to SeaweedFS master
seaweed = weed.New(viper.GetString("seaweed.masterURL"), time.Second*5)
err = seaweed.Ping()
if err != nil {
log.WithField("err", err).Fatal("failed to ping SeaweedFS master")
return
}
// Create router
router := &fasthttprouter.Router{
RedirectTrailingSlash: true,
RedirectFixedPath: true,
HandleMethodNotAllowed: true,
HandleOPTIONS: true,
NotFound: func(ctx *fasthttp.RequestCtx) {
ctx.SetStatusCode(fasthttp.StatusNotFound)
ctx.SetContentType("application/json; charset=utf-8")
fmt.Fprint(ctx, `{"code":404,"message":"Not Found"}`)
},
MethodNotAllowed: func(ctx *fasthttp.RequestCtx) {
ctx.SetStatusCode(fasthttp.StatusMethodNotAllowed)
ctx.SetContentType("application/json; charset=utf-8")
fmt.Fprint(ctx, `{"code":405,"message":"Method Not Allowed"}`)
},
PanicHandler: func(ctx *fasthttp.RequestCtx, err interface{}) {
log.WithField("err", err).Error("recovered panic while serving request")
ctx.ResetBody()
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
ctx.SetContentType("application/json; charset=utf-8")
fmt.Fprint(ctx, `{"code":500,"message":"Internal Server Error"}`)
},
}
// Apply routes
// > GET / (index)
router.GET("/", func(ctx *fasthttp.RequestCtx) {
ctx.SetStatusCode(fasthttp.StatusOK)
ctx.SetContentType("application/json; charset=utf-8")
ctx.Response.Header.Set("Location", currentAPIBasePath)
fmt.Fprintf(ctx, `{"code":200,"message":"index","current":"%s"}`, currentAPIBasePath)
})
// > POST /scanObject.async (scanObject.async)
// Scan a file, expects a file object to be supplied.
// Scans are run asynchronously on this route, and 201 Accepted responses are returned immediately.
// If a virus is detected, a Slack webhook is fired.
// TODO: automatically delete files and clear Varnish cache (?)
router.POST("/v1/scanObject.async", func(ctx *fasthttp.RequestCtx) {
ctx.SetContentType("application/json; charset=utf-8")
// Check Content-Type
if !strings.HasPrefix(string(ctx.Request.Header.Peek(contentType)), applicationJSON) {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
fmt.Fprint(ctx, `{"code":400,"message":"Bad Request (Content-Type not JSON)"}`)
return
}
// Parse request body
data := &object{}
err := json.Unmarshal(ctx.PostBody(), data)
if err != nil {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
fmt.Fprint(ctx, `{"code":400,"message":"Bad Request (could not parse JSON body)"}`)
return
}
// Verify request body
if data.Type != 0 {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
fmt.Fprint(ctx, `{"code":400,"message":"Bad Request (invalid object type)"}`)
return
}
// Scan asynchronously and return response
go processScan(data)
ctx.SetStatusCode(fasthttp.StatusAccepted)
fmt.Fprint(ctx, `{"code":201,"message":"Accepted (processing asynchronously)"}`)
})
// Serve
server := &fasthttp.Server{
Handler: router.Handler,
Name: "bitscan/" + version,
ReadTimeout: time.Minute * 3,
WriteTimeout: time.Minute * 30,
LogAllErrors: log.GetLevel() == log.DebugLevel,
Logger: log.New(),
}
log.Info("Attempting to listen on " + viper.GetString("http.listenAddress"))
if err := server.ListenAndServe(viper.GetString("http.listenAddress")); err != nil {
log.WithField("err", err).Fatal("error in ListenAndServe")
}
}
// getTempFilename returns a filename with the supplied extension (ext should have period).
func getTempFilename(ext string) (string, error) {
if tmpDir == "" {
tmpDir = os.TempDir() + "/bitscan_" + uniuri.New()
}
// Check if directory exists, otherwise create new directory
info, err := os.Stat(tmpDir)
if err != nil || !info.IsDir() {
tmpDir = os.TempDir() + "/bitscan_" + uniuri.New()
err = os.Mkdir(tmpDir, folderFileMode)
if err != nil {
log.WithField("err", err).Error("failed to generate temporary directory")
return "", err
}
}
return tmpDir + "/" + uniuri.New() + ext, nil
}
// sendWebhook to the Slack endpoint in the configuration.
func sendWebhook(title, text, color string) error {
if viper.GetString("notifications.slackWebhookURL") == "" {
return nil
}
d, err := json.Marshal(map[string][]attachment{
"attachments": {
{
Fallback: fmt.Sprintf("**%s**\n%s", title, text),
Color: color,
Title: title,
Text: text,
},
},
})
if err != nil {
return err
}
_, err = httpClient.Post(viper.GetString("notifications.slackWebhookURL"), applicationJSON, bytes.NewBuffer(d))
return err
}
func processScan(object *object) error {
res, err := scan(object)
if res == nil {
return nil
}
if err != nil {
err = sendWebhook(
fmt.Sprintf("Error scanning `%s`", object.BucketKey),
fmt.Sprintf("```\n%s```", err),
"danger")
if err != nil {
log.WithField("err", err).Error("failed to invoke webhook")
}
return err
}
if res.Error != nil {
err = sendWebhook(
fmt.Sprintf("Error scanning `%s`", object.BucketKey),
fmt.Sprintf("```\n%s```", res.Error),
"danger")
if err != nil {
log.WithField("err", err).Error("failed to invoke webhook")
}
return err
}
if res.Found {
log.WithFields(log.Fields{
"bucket_key": object.BucketKey,
"virus": res.Virus,
"md5_hash": object.MD5Hash,
}).Info("found virus in a file")
err = sendWebhook(fmt.Sprintf("Positive file found: `%s`", object.BucketKey),
fmt.Sprintf("`%s` (`%s`) returned positive during scan with virus `%s`.\n\n"+
"It has not been deleted from storage backend.", object.BucketKey, *object.MD5Hash,
res.Virus),
"#439FE0")
if err != nil {
log.WithField("err", err).Error("failed to invoke webhook")
}
return err
}
return nil
}
func scan(object *object) (*clamscan.Result, error) {
path, err := getTempFilename(path.Ext(object.Key))
if err != nil {
return nil, err
}
// Create temporary file
file, err := os.Create(path)
if err != nil {
log.WithFields(log.Fields{
"err": err,
"path": path,
}).Error("failed to create temporary file")
return nil, errors.New("failed to create temporary file: " + err.Error())
}
file.Chmod(objectFileMode)
// Get file from SeaweedFS
_, _, err = seaweed.Get(file, *object.BackendFileID, nil, "")
if err != nil {
log.WithFields(log.Fields{
"err": err,
"path": path,
}).Error("failed to get file from SeaweedFS backend")
return nil, errors.New("failed to get file from SeaweedFS backend: " + err.Error())
}
// Close the file
if err = file.Close(); err != nil {
log.WithFields(log.Fields{
"err": err,
"path": path,
}).Error("failed to close file")
}
// Scan the file using clamscan
res, err := clamscan.Scan(&clamscan.Options{}, path)
if err != nil {
log.WithFields(log.Fields{
"err": err,
"path": path,
}).Error("failed to scan file")
return nil, errors.New("failed to scan file: " + err.Error())
}
result := <-res
// Remove the file from disk
if err = os.Remove(path); err != nil {
log.WithFields(log.Fields{
"err": err,
"path": path,
}).Error("failed to delete file from disk")
}
return result, nil
}