-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
142 lines (112 loc) · 3.01 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
132
133
134
135
136
137
138
139
140
141
142
// SPDX-License-Identifier: Apache-2.0
// Copyright 2021 Authors of KubeArmor
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/kubearmor/kubearmor-log-client/client"
)
// StopChan Channel
var StopChan chan struct{}
// init Function
func init() {
StopChan = make(chan struct{})
}
// ==================== //
// == Signal Handler == //
// ==================== //
// GetOSSigChannel Function
func GetOSSigChannel() chan os.Signal {
c := make(chan os.Signal, 1)
signal.Notify(c,
syscall.SIGKILL,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT,
os.Interrupt)
return c
}
// ========== //
// == Main == //
// ========== //
func main() {
// == //
// get arguments
gRPCPtr := flag.String("gRPC", "", "gRPC server information")
msgPathPtr := flag.String("msgPath", "none", "Output location for messages, {path|stdout|none}")
logPathPtr := flag.String("logPath", "stdout", "Output location for alerts and logs, {path|stdout|none}")
logFilterPtr := flag.String("logFilter", "policy", "Filter for what kinds of alerts and logs to receive, {policy|system|all}")
jsonPtr := flag.Bool("json", false, "Flag to print alerts and logs in the JSON format")
flag.Parse()
// == //
gRPC := ""
fmt.Println("== KubeArmor information ==")
if *gRPCPtr != "" {
gRPC = *gRPCPtr
} else {
if val, ok := os.LookupEnv("KUBEARMOR_SERVICE"); ok {
gRPC = val
} else {
gRPC = "localhost:32767"
}
}
fmt.Println(" gRPC server: " + gRPC)
// == //
if *msgPathPtr == "none" && *logPathPtr == "none" {
flag.PrintDefaults()
return
}
if *logFilterPtr != "all" && *logFilterPtr != "policy" && *logFilterPtr != "system" {
flag.PrintDefaults()
return
}
// == //
// create a client
logClient := client.NewClient(gRPC, *msgPathPtr, *logPathPtr, *logFilterPtr)
if logClient == nil {
fmt.Printf("Failed to connect to the gRPC server (%s)\n", gRPC)
return
}
fmt.Printf("Created a gRPC client (%s)\n", gRPC)
// do healthcheck
if ok := logClient.DoHealthCheck(); !ok {
fmt.Println("Failed to check the liveness of the gRPC server")
return
}
fmt.Println("Checked the liveness of the gRPC server")
if *msgPathPtr != "none" {
// watch messages
go logClient.WatchMessages(*msgPathPtr, *jsonPtr)
fmt.Println("Started to watch messages")
}
if *logPathPtr != "none" {
if *logFilterPtr == "all" || *logFilterPtr == "policy" {
// watch alerts
go logClient.WatchAlerts(*logPathPtr, *jsonPtr)
fmt.Println("Started to watch alerts")
}
if *logFilterPtr == "all" || *logFilterPtr == "system" {
// watch logs
go logClient.WatchLogs(*logPathPtr, *jsonPtr)
fmt.Println("Started to watch logs")
}
}
// listen for interrupt signals
sigChan := GetOSSigChannel()
<-sigChan
close(StopChan)
logClient.Running = false
time.Sleep(time.Second * 1)
// destroy the client
if err := logClient.DestroyClient(); err != nil {
fmt.Printf("Failed to destroy the gRPC client (%s)\n", err.Error())
return
}
fmt.Println("Destroyed the gRPC client")
// == //
}