-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.go
105 lines (80 loc) · 2.7 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
package main
import (
"context"
"encoding/json"
"flag"
"time"
"database/sql"
_ "github.com/mattn/go-sqlite3"
"cloud.google.com/go/pubsub"
"github.com/censys/scan-takehome/pkg/scanning"
)
var (
services = []string{"HTTP", "SSH", "DNS"} // conider sharing with scanner and performing validation here
dbFile = flag.String("db", "/data/processor.db", "Database File")
)
// Interfaces to allow for mocking
// Consider using gomock to use *sql.DB instead of DBInterface and
// *pubsub.Subscription instead of SubscriptionInterface
type DBInterface interface {
Exec(query string, args ...interface{}) (sql.Result, error)
}
type SubscriptionInterface interface {
Receive(ctx context.Context, f func(ctx context.Context, msg *pubsub.Message)) error
}
// main loop that recieves messsges and inserts/updates them in database
func main_loop(ctx context.Context, subscription SubscriptionInterface, db DBInterface) {
err := subscription.Receive(ctx, func(ctx context.Context, msg *pubsub.Message) {
var scan scanning.Scan
if err := json.Unmarshal(msg.Data, &scan); err != nil {
panic(err)
}
var serviceResp string
switch scan.DataVersion {
case scanning.V1:
serviceResp = scan.ParseV1Data()
case scanning.V2:
serviceResp = scan.ParseV2Data()
default:
panic("Invalid DataVersion in scan")
}
// consider including timestamp in scan so retried messages don't overwrite new ones
_, err := db.Exec("INSERT OR REPLACE INTO scans (ip, port, service, response, scan_time) VALUES (?, ?, ?, ?, ?)",
scan.Ip, scan.Port, scan.Service, serviceResp, time.Now())
if err != nil {
panic("Database insert failed")
}
msg.Ack()
})
if err != nil {
panic(err)
}
}
func main() {
projectId := flag.String("project", "test-project", "GCP Project ID")
subscriptionId := flag.String("subscription", "scan-sub", "GCP PubSub Subscription ID")
flag.Parse()
ctx := context.Background()
client, err := pubsub.NewClient(ctx, *projectId)
if err != nil {
panic(err)
}
subscription := client.Subscription(*subscriptionId)
db, err := sql.Open("sqlite3", *dbFile)
if err != nil {
panic(err)
}
createTableSQL := `CREATE TABLE IF NOT EXISTS scans (
ip TEXT NOT NULL,
port INTEGER NOT NULL,
service TEXT NOT NULL,
response TEXT NOT NULL,
scan_time DATETIME NOT NULL,
PRIMARY KEY (ip, port, service)
);`
_, err = db.Exec(createTableSQL)
if err != nil {
panic(err)
}
main_loop(ctx, subscription, db)
}