This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.go
327 lines (251 loc) · 9.78 KB
/
monitor.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
// Copyright 2017 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package RuntimeTest
import (
"context"
"fmt"
"math/rand"
"strings"
"time"
. "github.com/cilium/cilium/test/ginkgo-ext"
"github.com/cilium/cilium/test/helpers"
. "github.com/onsi/gomega"
)
func init() {
// ensure that our random numbers are seeded differently on each run
rand.Seed(time.Now().UnixNano())
}
const (
// MonitorDropNotification represents the DropNotification configuration
// value for the Cilium monitor
MonitorDropNotification = "DropNotification"
// MonitorTraceNotification represents the TraceNotification configuration
// value for the Cilium monitor
MonitorTraceNotification = "TraceNotification"
// MonitorDebug represents the Debug configuration value for
// the Cilium monitor
MonitorDebug = "Debug"
)
var _ = Describe("RuntimeMonitorTest", func() {
var vm *helpers.SSHMeta
BeforeAll(func() {
vm = helpers.InitRuntimeHelper(helpers.Runtime, logger)
ExpectCiliumReady(vm)
areEndpointsReady := vm.WaitEndpointsReady()
Expect(areEndpointsReady).Should(BeTrue())
})
JustAfterEach(func() {
vm.ValidateNoErrorsInLogs(CurrentGinkgoTestDescription().Duration)
})
AfterFailed(func() {
vm.ReportFailed()
})
BeforeEach(func() {
ExpectPolicyEnforcementUpdated(vm, helpers.PolicyEnforcementDefault)
})
Context("With Sample Containers", func() {
BeforeAll(func() {
vm.SampleContainersActions(helpers.Create, helpers.CiliumDockerNetwork)
})
AfterEach(func() {
_ = vm.PolicyDelAll()
})
AfterAll(func() {
vm.SampleContainersActions(helpers.Delete, helpers.CiliumDockerNetwork)
})
monitorConfig := func() {
res := vm.ExecCilium(fmt.Sprintf("config %s=true %s=true %s=true",
MonitorDebug, MonitorDropNotification, MonitorTraceNotification))
ExpectWithOffset(1, res.WasSuccessful()).To(BeTrue(), "cannot update monitor config")
}
It("Cilium monitor verbose mode", func() {
monitorConfig()
ctx, cancel := context.WithCancel(context.Background())
res := vm.ExecInBackground(ctx, "cilium monitor -v")
defer cancel()
areEndpointsReady := vm.WaitEndpointsReady()
Expect(areEndpointsReady).Should(BeTrue())
endpoints, err := vm.GetEndpointsIds()
Expect(err).Should(BeNil())
for k, v := range endpoints {
filter := fmt.Sprintf("FROM %s DEBUG:", v)
vm.ContainerExec(k, helpers.Ping(helpers.Httpd1))
Expect(res.WaitUntilMatch(filter)).To(BeNil(),
"%q is not in the output after timeout", filter)
Expect(res.Output().String()).Should(ContainSubstring(filter))
}
})
It("Cilium monitor event types", func() {
monitorConfig()
_, err := vm.PolicyImportAndWait(vm.GetFullPath(policiesL3JSON), helpers.HelperTimeout)
Expect(err).Should(BeNil())
areEndpointsReady := vm.WaitEndpointsReady()
Expect(areEndpointsReady).Should(BeTrue(), "Endpoints are not ready after timeout")
eventTypes := map[string]string{
"drop": "DROP:",
"debug": "DEBUG:",
"capture": "DEBUG:",
}
for k, v := range eventTypes {
By("Type %s", k)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
res := vm.ExecInBackground(ctx, fmt.Sprintf("cilium monitor --type %s -v", k))
vm.ContainerExec(helpers.App1, helpers.Ping(helpers.Httpd1))
vm.ContainerExec(helpers.App3, helpers.Ping(helpers.Httpd1))
cancel()
Expect(res.WaitUntilMatch(v)).To(BeNil(),
"%q is not in the output after timeout", v)
Expect(res.CountLines()).Should(BeNumerically(">", 3))
Expect(res.Output().String()).Should(ContainSubstring(v))
}
By("all types together")
command := "cilium monitor -v"
for k := range eventTypes {
command = command + " --type " + k
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
By(command)
res := vm.ExecInBackground(ctx, command)
areEndpointsReady = vm.WaitEndpointsReady()
Expect(areEndpointsReady).Should(BeTrue())
vm.ContainerExec(helpers.App3, helpers.Ping(helpers.Httpd1))
vm.ContainerExec(helpers.App1, helpers.Ping(helpers.Httpd1))
for _, v := range eventTypes {
Expect(res.WaitUntilMatch(v)).To(BeNil(),
"%q is not in the output after timeout", v)
Expect(res.Output().String()).Should(ContainSubstring(v))
}
Expect(res.CountLines()).Should(BeNumerically(">", 3))
})
It("cilium monitor check --from", func() {
monitorConfig()
areEndpointsReady := vm.WaitEndpointsReady()
Expect(areEndpointsReady).Should(BeTrue())
endpoints, err := vm.GetEndpointsIds()
Expect(err).Should(BeNil())
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
res := vm.ExecInBackground(ctx, fmt.Sprintf(
"cilium monitor --type debug --from %s -v", endpoints[helpers.App1]))
vm.ContainerExec(helpers.App1, helpers.Ping(helpers.Httpd1))
filter := fmt.Sprintf("FROM %s DEBUG:", endpoints[helpers.App1])
Expect(res.WaitUntilMatch(filter)).To(BeNil(),
"%q is not in the output after timeout", filter)
Expect(res.CountLines()).Should(BeNumerically(">", 3))
Expect(res.Output().String()).Should(ContainSubstring(filter))
//MonitorDebug mode shouldn't have DROP lines
Expect(res.Output().String()).ShouldNot(ContainSubstring("DROP"))
})
It("cilium monitor check --to", func() {
monitorConfig()
areEndpointsReady := vm.WaitEndpointsReady()
Expect(areEndpointsReady).Should(BeTrue())
endpoints, err := vm.GetEndpointsIds()
Expect(err).Should(BeNil())
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
res := vm.ExecInBackground(ctx, fmt.Sprintf(
"cilium monitor -v --to %s", endpoints[helpers.Httpd1]))
vm.ContainerExec(helpers.App1, helpers.Ping(helpers.Httpd1))
vm.ContainerExec(helpers.App2, helpers.Ping(helpers.Httpd1))
filter := fmt.Sprintf("to endpoint %s", endpoints[helpers.Httpd1])
Expect(res.WaitUntilMatch(filter)).To(BeNil(),
"%q is not in the output after timeout", filter)
Expect(res.CountLines()).Should(BeNumerically(">=", 3))
Expect(res.Output().String()).Should(ContainSubstring(filter))
})
It("cilium monitor check --related-to", func() {
monitorConfig()
areEndpointsReady := vm.WaitEndpointsReady()
Expect(areEndpointsReady).Should(BeTrue())
endpoints, err := vm.GetEndpointsIds()
Expect(err).Should(BeNil())
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
res := vm.ExecInBackground(ctx, fmt.Sprintf(
"cilium monitor -v --related-to %s", endpoints[helpers.Httpd1]))
vm.WaitEndpointsReady()
vm.ContainerExec(helpers.App1, helpers.CurlFail("http://httpd1/public"))
filter := fmt.Sprintf("FROM %s DEBUG:", endpoints[helpers.Httpd1])
Expect(res.WaitUntilMatch(filter)).To(BeNil(),
"%q is not in the output after timeout", filter)
Expect(res.CountLines()).Should(BeNumerically(">=", 3))
Expect(res.Output().String()).Should(ContainSubstring(filter))
})
It("multiple monitors", func() {
monitorConfig()
areEndpointsReady := vm.WaitEndpointsReady()
Expect(areEndpointsReady).Should(BeTrue(), "Endpoints are not ready after timeout")
var monitorRes []*helpers.CmdRes
ctx, cancelfn := context.WithCancel(context.Background())
for i := 1; i <= 3; i++ {
monitorRes = append(monitorRes, vm.ExecInBackground(ctx, "cilium monitor"))
}
vm.ContainerExec(helpers.App1, helpers.Ping(helpers.Httpd1))
cancelfn()
for _, res := range monitorRes {
res.WaitUntilFinish()
}
Expect(monitorRes[0].CountLines()).Should(BeNumerically(">", 2))
//Due to the ssh connection, sometimes the result has one line more in
//any output. So we check at least 5 lines are in the all outputs.
for i := 0; i < 5; i++ {
//ln: return a random number in the array len upper than 5 (First 5 lines)
ln := rand.Intn((len(monitorRes[0].ByLines())-1)-5) + 5
str := monitorRes[0].ByLines()[ln]
Expect(monitorRes[1].Output().String()).Should(ContainSubstring(str))
Expect(monitorRes[2].Output().String()).Should(ContainSubstring(str))
}
})
It("checks container ids match monitor output", func() {
res := vm.ExecCilium(fmt.Sprintf("config %s=true", MonitorDebug))
res.ExpectSuccess()
ExpectPolicyEnforcementUpdated(vm, helpers.PolicyEnforcementAlways)
ctx, cancel := context.WithCancel(context.Background())
res = vm.ExecInBackground(ctx, "cilium monitor -v")
vm.ContainerExec(helpers.App1, helpers.Ping(helpers.Httpd1))
vm.ContainerExec(helpers.Httpd1, helpers.Ping(helpers.App1))
endpoints, err := vm.GetEndpointsIDMap()
Expect(err).Should(BeNil())
helpers.Sleep(10)
cancel()
// Expected full example output:
// CPU 01: MARK 0x3de3947b FROM 48896 DEBUG: Attempting local delivery for container id 29381 from seclabel 263
// ^ ^
for _, line := range res.ByLines() {
var toID, fromID string
fields := strings.Split(line, " ")
for i := range fields {
switch fields[i] {
case "FROM":
fromID = fields[i+1]
break
case "id":
toID = fields[i+1]
break
}
}
if fromID == "" || toID == "" {
continue
}
By("checking endpoints in monitor line:\n%q", line)
Expect(toID).Should(Not(Equal(fromID)))
Expect(endpoints[toID]).Should(Not(BeNil()))
Expect(endpoints[fromID]).Should(Not(BeNil()))
}
})
})
})