forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcell_test.go
More file actions
332 lines (288 loc) · 10.8 KB
/
Copy pathcell_test.go
File metadata and controls
332 lines (288 loc) · 10.8 KB
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
328
329
330
331
332
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package ipsec
import (
"log/slog"
"net"
"os"
"path/filepath"
"testing"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/cilium/hive/cell"
"github.com/cilium/hive/hivetest"
"github.com/cilium/statedb"
cnicell "github.com/cilium/cilium/daemon/cmd/cni"
fakecni "github.com/cilium/cilium/daemon/cmd/cni/fake"
"github.com/cilium/cilium/daemon/k8s"
"github.com/cilium/cilium/pkg/clustermesh"
cmtypes "github.com/cilium/cilium/pkg/clustermesh/types"
fakeTypes "github.com/cilium/cilium/pkg/datapath/fake/types"
"github.com/cilium/cilium/pkg/datapath/iptables/ipset"
"github.com/cilium/cilium/pkg/datapath/linux/sysctl"
"github.com/cilium/cilium/pkg/datapath/tables"
"github.com/cilium/cilium/pkg/datapath/tunnel"
"github.com/cilium/cilium/pkg/datapath/types"
"github.com/cilium/cilium/pkg/dial"
"github.com/cilium/cilium/pkg/endpoint/regeneration"
envoy "github.com/cilium/cilium/pkg/envoy/config"
"github.com/cilium/cilium/pkg/hive"
"github.com/cilium/cilium/pkg/ipcache"
k8sClient "github.com/cilium/cilium/pkg/k8s/client/testutils"
k8sSynced "github.com/cilium/cilium/pkg/k8s/synced"
"github.com/cilium/cilium/pkg/k8s/watchers"
"github.com/cilium/cilium/pkg/kvstore"
"github.com/cilium/cilium/pkg/kvstore/store"
"github.com/cilium/cilium/pkg/loadbalancer"
"github.com/cilium/cilium/pkg/loadbalancer/reflectors"
"github.com/cilium/cilium/pkg/loadbalancer/writer"
"github.com/cilium/cilium/pkg/maps/encrypt"
"github.com/cilium/cilium/pkg/mtu"
"github.com/cilium/cilium/pkg/node"
"github.com/cilium/cilium/pkg/node/addressing"
nodeManager "github.com/cilium/cilium/pkg/node/manager"
nodeTypes "github.com/cilium/cilium/pkg/node/types"
"github.com/cilium/cilium/pkg/nodediscovery"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/source"
"github.com/cilium/cilium/pkg/testutils"
"github.com/cilium/cilium/pkg/testutils/netns"
"github.com/cilium/cilium/pkg/time"
wgTypes "github.com/cilium/cilium/pkg/wireguard/types"
)
var (
TestTimeout = 5 * time.Second
k8s1NodeIPv4 = net.ParseIP("192.168.60.11")
k8s1NodeName = "k8s1"
)
// paramsOut holds the output parameters needed for running cells in the test.
type paramsOut struct {
cell.Out
IPSecConfig Config
WireguardConfig wgTypes.WireguardConfig
TunnelConfig tunnel.Config
DaemonConfig *option.DaemonConfig
LBConfig loadbalancer.Config
LBExternalConfig loadbalancer.ExternalConfig
LocalNode node.LocalNode
IPCache *ipcache.IPCache
CNIConfigManager cnicell.CNIConfigManager
K8SAPIGroups *k8sSynced.APIGroups
K8SResources *k8sSynced.Resources
Sysctl sysctl.Sysctl
StoreFactory store.Factory
ClusterMeshMetrics clustermesh.ClusterMeshMetrics
RemoteIdentityWatcher clustermesh.RemoteIdentityWatcher
CacheStatus k8sSynced.CacheStatus
ClusterInfo cmtypes.ClusterInfo
NodeHandler types.NodeHandler
SecretSyncConfig envoy.SecretSyncConfig
}
func TestPrivileged_TestIPSecCell(t *testing.T) {
// Needed to create the test namespace, bpf maps and XFRM.
testutils.PrivilegedTest(t)
// Create a temporary directory, used also by the agent for the ipsec key.
testRunDir := t.TempDir()
// Use the temporary directory in daemon config.
oldRunDir, oldStateDir := option.Config.RunDir, option.Config.StateDir
defer func() {
option.Config.RunDir = oldRunDir
option.Config.StateDir = oldStateDir
}()
option.Config.RunDir = testRunDir
option.Config.StateDir = testRunDir
var (
// Local references are updated when starting the Hive.
ipsecAgent *Agent
nodeStore *node.LocalNodeStore
mtuConfig mtu.MTU
encryptMap encrypt.EncryptMap
nodeHandler types.NodeHandler
ctx = t.Context()
ns = netns.NewNetNS(t)
log = hivetest.Logger(t, hivetest.LogLevel(slog.LevelError))
validKey = []byte("4 rfc4106(gcm(aes)) 44434241343332312423222114131211f4f3f2f1 128\n")
anotherValidKey = []byte("5 rfc4106(gcm(aes)) 44434241343332312423222114131211f4f3f2f1 128\n")
invalidKey = []byte("6 test abcdefghijklmnopqrstuvwzyzABCDEF test abcdefghijklmnopqrstuvwzyzABCDEF\n")
keyFile = filepath.Join(testRunDir, "cilium_ipsec.key")
zeroKey = encrypt.EncryptKey{Key: 0}
)
// getHive returns a new hive with IPSec enabled/disabled.
getHive := func(ipsecEnabled bool) *hive.Hive {
return hive.New(
mtu.Cell,
encrypt.Cell,
nodeManager.Cell,
nodediscovery.Cell,
source.Cell,
watchers.Cell,
dial.ServiceResolverCell,
reflectors.K8sReflectorCell,
clustermesh.Cell,
writer.Cell,
ipset.Cell,
k8s.ResourcesCell,
k8s.PodTableCell,
node.LocalNodeStoreTestCell,
k8sClient.FakeClientCell(),
kvstore.Cell(kvstore.DisabledBackendName),
cell.Provide(
reflectors.NetnsCookieSupportFunc,
newIPsecAgent,
newIPsecConfig,
regeneration.NewFence,
tables.NewDeviceTable,
tables.NewNodeAddressTable,
ipcache.NewLocalIPIdentityWatcher,
ipcache.NewIPIdentitySynchronizer,
statedb.RWTable[*tables.Device].ToTable,
statedb.RWTable[tables.NodeAddress].ToTable,
func() paramsOut {
return paramsOut{
IPSecConfig: Config{
UserConfig: UserConfig{
EnableIPsec: ipsecEnabled,
EnableIPsecKeyWatcher: true,
EnableIPsecXfrmStateCaching: true,
UseCiliumInternalIPForIPsec: false,
DNSProxyInsecureSkipTransparentModeCheck: false,
IPsecKeyFile: keyFile,
IPsecKeyRotationDuration: 1 * time.Second,
},
EncryptNode: false,
},
WireguardConfig: fakeTypes.WireguardConfig{},
TunnelConfig: tunnel.Config{},
DaemonConfig: option.Config,
LBConfig: loadbalancer.Config{},
LBExternalConfig: loadbalancer.ExternalConfig{},
LocalNode: node.LocalNode{
Node: nodeTypes.Node{
Name: k8s1NodeName,
IPAddresses: []nodeTypes.Address{
{
Type: addressing.NodeInternalIP,
IP: k8s1NodeIPv4,
},
},
Annotations: map[string]string{},
},
},
IPCache: ipcache.NewIPCache(&ipcache.Configuration{
Context: ctx,
Logger: hivetest.Logger(t),
}),
K8SAPIGroups: &k8sSynced.APIGroups{},
K8SResources: &k8sSynced.Resources{},
CNIConfigManager: &fakecni.FakeCNIConfigManager{},
Sysctl: sysctl.NewDirectSysctl(afero.NewOsFs(), "/proc"),
StoreFactory: store.NewFactory(hivetest.Logger(t), store.MetricsProvider()),
ClusterMeshMetrics: nil,
RemoteIdentityWatcher: nil,
CacheStatus: make(k8sSynced.CacheStatus),
ClusterInfo: cmtypes.DefaultClusterInfo,
NodeHandler: fakeTypes.NewNodeHandler(),
SecretSyncConfig: envoy.SecretSyncConfig{},
}
},
),
cell.Invoke(
func(a types.IPsecAgent, s *node.LocalNodeStore, m mtu.MTU, e encrypt.EncryptMap, n types.NodeHandler) {
ipsecAgent = a.(*Agent)
nodeStore = s
mtuConfig = m
nodeHandler = n
if a.Enabled() {
encryptMap = e
}
}),
)
}
// Remove pinned encryption map upon finishing.
t.Cleanup(func() {
if encryptMap != nil {
encryptMap.UnpinIfExists()
}
})
t.Run("IPSecEnabled", func(t *testing.T) {
ns.Do(func() error {
// 0. Dump a valid IPSec key to file.
require.NoError(t, os.WriteFile(keyFile, validKey, 0644))
// 1. Create a hive with IPSec enabled.
hive := getHive(true)
// 2. Start the hive.
require.NoError(t, hive.Start(log, ctx))
// 3. Ensure the ipsec agent is enabled.
require.True(t, ipsecAgent.Enabled())
// 4. Ensure the MTU returns the correct value.
require.EventuallyWithT(t, func(c *assert.CollectT) {
overhead := mtu.EncryptionIPsecOverhead + (ipsecAgent.authKeySize - mtu.EncryptionDefaultAuthKeyLength)
assert.Equal(c, mtuConfig.GetDeviceMTU(), mtuConfig.GetRouteMTU()+overhead)
}, TestTimeout, 50*time.Millisecond)
// 5. Ensure local node has been updated.
localNode, err := nodeStore.Get(ctx)
require.NoError(t, err)
assert.Equal(t, localNode.EncryptionKey, ipsecAgent.spi)
// 6. Ensure encrypt map is updated accordingly.
v, err := encryptMap.Lookup(zeroKey)
require.NoError(t, err)
assert.Equal(t, ipsecAgent.spi, v.KeyID)
// 6. Start background ipsec jobs.
require.NoError(t, ipsecAgent.StartBackgroundJobs(nodeHandler))
// 7. Dump another valid IPSec key to file.
require.NoError(t, os.WriteFile(keyFile, anotherValidKey, 0644))
// 8. Ensure the ipsec agent updated the spi accordingly.
require.EventuallyWithT(t, func(c *assert.CollectT) {
assert.Equal(c, uint8(5), ipsecAgent.spi)
v, err := encryptMap.Lookup(zeroKey)
assert.NoError(c, err)
assert.Equal(c, ipsecAgent.spi, v.KeyID)
assert.NotEmpty(c, ipsecAgent.ipSecKeysRemovalTime)
}, TestTimeout, 50*time.Millisecond)
// 8. Dump an invalid IPSec key to file.
require.NoError(t, os.WriteFile(keyFile, invalidKey, 0644))
// 9. Ensure the ipsec agent rejected the new key.
require.EventuallyWithT(t, func(c *assert.CollectT) {
assert.Equal(c, uint8(5), ipsecAgent.spi)
v, err := encryptMap.Lookup(zeroKey)
assert.NoError(c, err)
assert.Equal(c, ipsecAgent.spi, v.KeyID)
}, TestTimeout, 50*time.Millisecond)
// 10. Stop the hive.
require.NoError(t, hive.Stop(log, ctx))
return nil
})
})
t.Run("IPSecDisabled", func(t *testing.T) {
ns.Do(func() error {
// 0. Dump a valid IPSec key to file.
require.NoError(t, os.WriteFile(keyFile, validKey, 0644))
// 1. Create a hive with ipsec disabled.
hive := getHive(false)
// 2. Start the hive.
require.NoError(t, hive.Start(log, ctx))
// 3. Ensure the ipsec Agent is disabled.
require.False(t, ipsecAgent.Enabled())
// 4. Ensure the MTU is not changed.
require.EventuallyWithT(t, func(c *assert.CollectT) {
assert.Equal(c, mtuConfig.GetDeviceMTU(), mtuConfig.GetRouteMTU())
}, TestTimeout, 50*time.Millisecond)
// 5. Ensure local node has not been updated.
localNode, err := nodeStore.Get(ctx)
require.NoError(t, err)
assert.Zero(t, localNode.EncryptionKey)
// 6. Ensure the ipsec agent did not update the key.
// Current key SPI would've been 4, but encryptMap still has 5 from previous test.
require.EventuallyWithT(t, func(c *assert.CollectT) {
assert.Equal(c, uint8(0), ipsecAgent.spi)
v, err := encryptMap.Lookup(zeroKey)
assert.NoError(c, err)
assert.NotEqual(c, uint8(4), v.KeyID)
}, TestTimeout, 50*time.Millisecond)
// 7. Stop the hive.
require.NoError(t, hive.Stop(log, ctx))
return nil
})
})
}