-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
93 lines (75 loc) · 2.5 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
package main
import (
"context"
"log"
"net/http"
"os"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/trufflesecurity/logwarden/internal/engine"
"github.com/trufflesecurity/logwarden/internal/outputs"
"github.com/trufflesecurity/logwarden/internal/outputs/human"
"github.com/trufflesecurity/logwarden/internal/outputs/json"
"github.com/trufflesecurity/logwarden/internal/outputs/slack"
"github.com/trufflesecurity/logwarden/internal/outputs/webhook"
"github.com/trufflesecurity/logwarden/internal/secret"
)
var (
app = kingpin.New("logwarden", "Logwarden is a tool to audit GCP logs against a set of rego policies.")
// required
policies = app.Flag("policies", "Path to policies folder.").Default("policies").String()
project = app.Flag("project", "GCP Project ID.").Required().String()
subscription = app.Flag("subscription", "Pub/Sub subscription to audit.").Required().String()
secretName = app.Flag("secret-name", "GCP Secret name to use for GCP Auditor.").Default("logwarden").String()
// options
jsonOut = app.Flag("json", "Output results as JSON.").Bool()
printAll = app.Flag("print-all", "Output all logs that are processed.").Bool()
// outputs
slackWebhookOut = app.Flag("slack-webhook", "Enable Slack webhook.").Bool()
webhookOut = app.Flag("webhook", "Enable JSON HTTP POST webhook output.").Bool()
)
func main() {
kingpin.MustParse(app.Parse(os.Args[1:]))
ctx := context.TODO()
secret, err := secret.GetSecret(ctx, *project, *secretName)
if err != nil {
log.Fatal(err)
}
enabledOutputs := []outputs.Output{}
if *jsonOut {
enabledOutputs = append(enabledOutputs, json.JSON{})
} else {
enabledOutputs = append(enabledOutputs, human.Human{})
}
if *slackWebhookOut {
slackWebhookURL := secret.MustGetField("SLACK_WEBHOOK")
enabledOutputs = append(enabledOutputs, slack.Slack{WebhookURL: slackWebhookURL})
}
if *webhookOut {
webhookURL := secret.MustGetField("WEBHOOK_URL")
enabledOutputs = append(enabledOutputs, webhook.Webhook{PostURL: webhookURL})
}
eng, err := engine.New(ctx, *policies, enabledOutputs, *printAll)
if err != nil {
log.Fatal(err)
}
go func() {
err := eng.Alert(ctx)
if err != nil {
log.Fatal(err)
}
}()
go func() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
log.Fatal(http.ListenAndServe(":"+port, nil))
}()
err = eng.Subscribe(ctx, *project, *subscription)
if err != nil {
log.Fatal(err)
}
}