forked from neuvector/neuvector
/
grpc.go
270 lines (220 loc) · 7.45 KB
/
grpc.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
package main
import (
"encoding/json"
"fmt"
"sync"
"time"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
"google.golang.org/grpc"
"github.com/neuvector/neuvector/agent/workerlet"
"github.com/neuvector/neuvector/share"
"github.com/neuvector/neuvector/share/cluster"
"github.com/neuvector/neuvector/share/global"
"github.com/neuvector/neuvector/share/utils"
)
type ScanService struct {
scanning utils.Set
scanMutex sync.Mutex
}
func newScanService() *ScanService {
return &ScanService{
scanning: utils.NewSet(),
}
}
func (ss *ScanService) setScanStart(id string) bool {
ss.scanMutex.Lock()
defer ss.scanMutex.Unlock()
// In case a scan takes a long time to finish, ctrl will retry the request
// Avoid triggering the same scan for such case
if ss.scanning.Contains(id) {
return true
}
ss.scanning.Add(id)
return false
}
func (ss *ScanService) setScanDone(id string) {
ss.scanMutex.Lock()
defer ss.scanMutex.Unlock()
ss.scanning.Remove(id)
}
func (ss *ScanService) ScanGetFiles(ctx context.Context, req *share.ScanRunningRequest) (*share.ScanData, error) {
// Use Info log level so we by default log it's scanning
log.WithFields(log.Fields{"id": req.ID}).Info("")
if ss.setScanStart(req.ID) {
log.WithFields(log.Fields{"id": req.ID}).Info("scan in propress")
return &share.ScanData{Error: share.ScanErrorCode_ScanErrInProgress}, nil
}
defer ss.setScanDone(req.ID)
var pid int
var data share.ScanData
gInfoRLock()
if req.Type == share.ScanObjectType_HOST {
pid = 1
if gInfo.hostScanCache != nil {
data.Buffer = gInfo.hostScanCache
data.Error = share.ScanErrorCode_ScanErrNone
}
} else if c, ok := gInfo.activeContainers[req.ID]; ok {
pid = c.pid
if c.scanCache != nil {
data.Buffer = c.scanCache
data.Error = share.ScanErrorCode_ScanErrNone
}
}
gInfoRUnlock()
// Use the cached buffer if it's valid
if data.Buffer != nil {
log.WithFields(log.Fields{"id": req.ID}).Info("return cached data")
return &data, nil
}
if pid == 0 {
log.WithFields(log.Fields{"id": req.ID}).Info("container not running")
return &share.ScanData{Error: share.ScanErrorCode_ScanErrContainerExit}, nil
}
global.SYS.ReCalculateMemoryMetrics(memStatsEnforcerResetMark)
taskReq := workerlet.WalkGetPackageRequest{
Pid: pid,
Id: req.ID,
Kernel: Host.Kernel,
ObjType: req.Type,
}
bytesValue, _, err := walkerTask.Run(taskReq)
if err == nil {
if err = json.Unmarshal(bytesValue, &data); err != nil {
log.WithFields(log.Fields{"id": req.ID, "error": err}).Error()
}
} else {
log.WithFields(log.Fields{"id": req.ID, "error": err}).Error()
}
if data.Error == share.ScanErrorCode_ScanErrNone {
gInfoLock()
if req.Type == share.ScanObjectType_HOST {
gInfo.hostScanCache = data.Buffer
} else if c, ok := gInfo.activeContainers[req.ID]; ok {
c.scanCache = data.Buffer
}
gInfoUnlock()
}
if err := ctx.Err(); err != nil {
log.WithFields(log.Fields{"id": req.ID, "error": err}).Error("gRPC: Failed")
}
log.WithFields(log.Fields{"id": req.ID}).Info("return data for scanning")
return &data, nil
}
type CapService struct {
}
func (s *CapService) IsGRPCCompressed(ctx context.Context, v *share.RPCVoid) (*share.CLUSBoolean, error) {
return &share.CLUSBoolean{Value: true}, nil
}
func startGRPCServer(port uint16) (*cluster.GRPCServer, uint16) {
var grpc *cluster.GRPCServer
var err error
if port == 0 {
port = cluster.DefaultAgentGRPCPort
}
log.WithFields(log.Fields{"port": port}).Info("")
for {
grpc, err = cluster.NewGRPCServerTCP(fmt.Sprintf(":%d", port))
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("Fail to create GRPC server")
time.Sleep(time.Second * 5)
} else {
break
}
}
share.RegisterEnforcerCapServiceServer(grpc.GetServer(), new(CapService))
share.RegisterEnforcerServiceServer(grpc.GetServer(), new(RPCService))
share.RegisterEnforcerScanServiceServer(grpc.GetServer(), newScanService())
go grpc.Start()
log.Info("GRPC server started")
return grpc, port
}
func createControllerAgentServiceWrapper(conn *grpc.ClientConn) cluster.Service {
return share.NewControllerAgentServiceClient(conn)
}
func getControllerServiceClient() (share.ControllerAgentServiceClient, error) {
ctrlEndpoint := getLeadGRPCEndpoint()
log.WithFields(log.Fields{"endpoint": ctrlEndpoint}).Debug("")
if ctrlEndpoint == "" {
log.WithFields(log.Fields{"endpoint": ctrlEndpoint}).Error("Controller endpoint is not ready")
return nil, fmt.Errorf("Controller endpoint is not ready")
}
if cluster.GetGRPCClientEndpoint(ctrlEndpoint) == "" {
cluster.CreateGRPCClient(ctrlEndpoint, ctrlEndpoint, true,
createControllerAgentServiceWrapper)
}
c, err := cluster.GetGRPCClient(ctrlEndpoint, cluster.IsControllerGRPCCommpressed, nil)
if err == nil {
return c.(share.ControllerAgentServiceClient), nil
} else {
log.WithFields(log.Fields{"err": err}).Error("Failed to connect to grpc server")
return nil, err
}
}
func requestAdmission(req *share.CLUSAdmissionRequest, timeout time.Duration) (*share.CLUSAdmissionResponse, error) {
client, err := getControllerServiceClient()
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("Failed to find ctrl client")
return nil, fmt.Errorf("Fail to find controller client")
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return client.RequestAdmission(ctx, req)
}
func sendLearnedProcess(procs []*share.CLUSProcProfileReq) error {
log.WithFields(log.Fields{"processes": len(procs)}).Debug("")
client, err := getControllerServiceClient()
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("Failed to find ctrl client")
return fmt.Errorf("Fail to find controller client")
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
procArray := &share.CLUSProcProfileArray{
Processes: procs,
}
_, err = client.ReportProcProfile(ctx, procArray)
if err != nil {
log.WithFields(log.Fields{"error": err}).Debug("Fail to report process profile to controller")
return fmt.Errorf("Fail to report process profile to controller")
}
return nil
}
func sendLearnedFileAccessRule(rules []*share.CLUSFileAccessRuleReq) error {
log.WithFields(log.Fields{"rules": len(rules)}).Debug("")
client, err := getControllerServiceClient()
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("Failed to find ctrl client")
return fmt.Errorf("Fail to find controller client")
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
ruleArray := &share.CLUSFileAccessRuleArray{
Rules: rules,
}
_, err = client.ReportFileAccessRule(ctx, ruleArray)
if err != nil {
log.WithFields(log.Fields{"error": err}).Debug("Fail to report file rule to controller")
return fmt.Errorf("Fail to report file rule to controller")
}
return nil
}
func sendConnections(conns []*share.CLUSConnection) (*share.CLUSReportResponse, error) {
client, err := getControllerServiceClient()
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("Failed to find ctrl client")
return nil, fmt.Errorf("Fail to find controller client")
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
connArray := &share.CLUSConnectionArray{
Connections: conns,
}
resp, err := client.ReportConnections(ctx, connArray)
if err != nil {
log.WithFields(log.Fields{"error": err}).Debug("Fail to report connections to controller")
return resp, fmt.Errorf("Fail to report connections to controller")
}
return resp, nil
}