This repository has been archived by the owner on Aug 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 385
/
mesh_gateway_test.go
298 lines (239 loc) · 12.4 KB
/
mesh_gateway_test.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
package meshgateway
import (
"context"
"fmt"
"testing"
"time"
"github.com/hashicorp/consul-helm/test/acceptance/framework/consul"
"github.com/hashicorp/consul-helm/test/acceptance/framework/environment"
"github.com/hashicorp/consul-helm/test/acceptance/framework/helpers"
"github.com/hashicorp/consul-helm/test/acceptance/framework/k8s"
"github.com/hashicorp/consul-helm/test/acceptance/framework/logger"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/sdk/testutil/retry"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const staticClientName = "static-client"
// Test that Connect and wan federation over mesh gateways work in a default installation
// i.e. without ACLs because TLS is required for WAN federation over mesh gateways
func TestMeshGatewayDefault(t *testing.T) {
env := suite.Environment()
cfg := suite.Config()
primaryContext := env.DefaultContext(t)
secondaryContext := env.Context(t, environment.SecondaryContextName)
primaryHelmValues := map[string]string{
"global.datacenter": "dc1",
"global.tls.enabled": "true",
"global.tls.httpsOnly": "false",
"global.federation.enabled": "true",
"global.federation.createFederationSecret": "true",
"connectInject.enabled": "true",
"controller.enabled": "true",
"meshGateway.enabled": "true",
"meshGateway.replicas": "1",
}
if cfg.UseKind {
primaryHelmValues["meshGateway.service.type"] = "NodePort"
primaryHelmValues["meshGateway.service.nodePort"] = "30000"
}
releaseName := helpers.RandomName()
// Install the primary consul cluster in the default kubernetes context
primaryConsulCluster := consul.NewHelmCluster(t, primaryHelmValues, primaryContext, cfg, releaseName)
primaryConsulCluster.Create(t)
// Get the federation secret from the primary cluster and apply it to secondary cluster
federationSecretName := fmt.Sprintf("%s-consul-federation", releaseName)
logger.Logf(t, "retrieving federation secret %s from the primary cluster and applying to the secondary", federationSecretName)
federationSecret, err := primaryContext.KubernetesClient(t).CoreV1().Secrets(primaryContext.KubectlOptions(t).Namespace).Get(context.Background(), federationSecretName, metav1.GetOptions{})
federationSecret.ResourceVersion = ""
require.NoError(t, err)
_, err = secondaryContext.KubernetesClient(t).CoreV1().Secrets(secondaryContext.KubectlOptions(t).Namespace).Create(context.Background(), federationSecret, metav1.CreateOptions{})
require.NoError(t, err)
// Create secondary cluster
secondaryHelmValues := map[string]string{
"global.datacenter": "dc2",
"global.tls.enabled": "true",
"global.tls.httpsOnly": "false",
"global.tls.caCert.secretName": federationSecretName,
"global.tls.caCert.secretKey": "caCert",
"global.tls.caKey.secretName": federationSecretName,
"global.tls.caKey.secretKey": "caKey",
"global.federation.enabled": "true",
"server.extraVolumes[0].type": "secret",
"server.extraVolumes[0].name": federationSecretName,
"server.extraVolumes[0].load": "true",
"server.extraVolumes[0].items[0].key": "serverConfigJSON",
"server.extraVolumes[0].items[0].path": "config.json",
"connectInject.enabled": "true",
"meshGateway.enabled": "true",
"meshGateway.replicas": "1",
}
if cfg.UseKind {
secondaryHelmValues["meshGateway.service.type"] = "NodePort"
secondaryHelmValues["meshGateway.service.nodePort"] = "30000"
}
// Install the secondary consul cluster in the secondary kubernetes context
secondaryConsulCluster := consul.NewHelmCluster(t, secondaryHelmValues, secondaryContext, cfg, releaseName)
secondaryConsulCluster.Create(t)
primaryClient := primaryConsulCluster.SetupConsulClient(t, false)
secondaryClient := secondaryConsulCluster.SetupConsulClient(t, false)
// Verify federation between servers
logger.Log(t, "verifying federation was successful")
verifyFederation(t, primaryClient, secondaryClient, releaseName, false)
// Create a ProxyDefaults resource to configure services to use the mesh
// gateways.
logger.Log(t, "creating proxy-defaults config")
kustomizeDir := "../fixtures/bases/mesh-gateway"
k8s.KubectlApplyK(t, primaryContext.KubectlOptions(t), kustomizeDir)
helpers.Cleanup(t, cfg.NoCleanupOnFailure, func() {
k8s.KubectlDeleteK(t, primaryContext.KubectlOptions(t), kustomizeDir)
})
// Check that we can connect services over the mesh gateways
logger.Log(t, "creating static-server in dc2")
k8s.DeployKustomize(t, secondaryContext.KubectlOptions(t), cfg.NoCleanupOnFailure, cfg.DebugDirectory, "../fixtures/cases/static-server-inject")
logger.Log(t, "creating static-client in dc1")
k8s.DeployKustomize(t, primaryContext.KubectlOptions(t), cfg.NoCleanupOnFailure, cfg.DebugDirectory, "../fixtures/cases/static-client-multi-dc")
logger.Log(t, "checking that connection is successful")
k8s.CheckStaticServerConnectionSuccessful(t, primaryContext.KubectlOptions(t), "http://localhost:1234")
}
// Test that Connect and wan federation over mesh gateways work in a secure installation,
// with ACLs and TLS with and without auto-encrypt enabled.
func TestMeshGatewaySecure(t *testing.T) {
cases := []struct {
name string
enableAutoEncrypt string
}{
{
"with ACLs and TLS without auto-encrypt",
"false",
},
{
"with ACLs and auto-encrypt",
"true",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
env := suite.Environment()
cfg := suite.Config()
primaryContext := env.DefaultContext(t)
secondaryContext := env.Context(t, environment.SecondaryContextName)
primaryHelmValues := map[string]string{
"global.datacenter": "dc1",
"global.tls.enabled": "true",
"global.tls.enableAutoEncrypt": c.enableAutoEncrypt,
"global.acls.manageSystemACLs": "true",
"global.acls.createReplicationToken": "true",
"global.federation.enabled": "true",
"global.federation.createFederationSecret": "true",
"connectInject.enabled": "true",
"controller.enabled": "true",
"meshGateway.enabled": "true",
"meshGateway.replicas": "1",
}
if cfg.UseKind {
primaryHelmValues["meshGateway.service.type"] = "NodePort"
primaryHelmValues["meshGateway.service.nodePort"] = "30000"
}
releaseName := helpers.RandomName()
// Install the primary consul cluster in the default kubernetes context
primaryConsulCluster := consul.NewHelmCluster(t, primaryHelmValues, primaryContext, cfg, releaseName)
primaryConsulCluster.Create(t)
// Get the federation secret from the primary cluster and apply it to secondary cluster
federationSecretName := fmt.Sprintf("%s-consul-federation", releaseName)
logger.Logf(t, "retrieving federation secret %s from the primary cluster and applying to the secondary", federationSecretName)
federationSecret, err := primaryContext.KubernetesClient(t).CoreV1().Secrets(primaryContext.KubectlOptions(t).Namespace).Get(context.Background(), federationSecretName, metav1.GetOptions{})
require.NoError(t, err)
federationSecret.ResourceVersion = ""
_, err = secondaryContext.KubernetesClient(t).CoreV1().Secrets(secondaryContext.KubectlOptions(t).Namespace).Create(context.Background(), federationSecret, metav1.CreateOptions{})
require.NoError(t, err)
// Create secondary cluster
secondaryHelmValues := map[string]string{
"global.datacenter": "dc2",
"global.tls.enabled": "true",
"global.tls.httpsOnly": "false",
"global.tls.enableAutoEncrypt": c.enableAutoEncrypt,
"global.tls.caCert.secretName": federationSecretName,
"global.tls.caCert.secretKey": "caCert",
"global.tls.caKey.secretName": federationSecretName,
"global.tls.caKey.secretKey": "caKey",
"global.acls.manageSystemACLs": "true",
"global.acls.replicationToken.secretName": federationSecretName,
"global.acls.replicationToken.secretKey": "replicationToken",
"global.federation.enabled": "true",
"server.extraVolumes[0].type": "secret",
"server.extraVolumes[0].name": federationSecretName,
"server.extraVolumes[0].load": "true",
"server.extraVolumes[0].items[0].key": "serverConfigJSON",
"server.extraVolumes[0].items[0].path": "config.json",
"connectInject.enabled": "true",
"meshGateway.enabled": "true",
"meshGateway.replicas": "1",
}
if cfg.UseKind {
secondaryHelmValues["meshGateway.service.type"] = "NodePort"
secondaryHelmValues["meshGateway.service.nodePort"] = "30000"
}
// Install the secondary consul cluster in the secondary kubernetes context
secondaryConsulCluster := consul.NewHelmCluster(t, secondaryHelmValues, secondaryContext, cfg, releaseName)
secondaryConsulCluster.Create(t)
primaryClient := primaryConsulCluster.SetupConsulClient(t, true)
secondaryClient := secondaryConsulCluster.SetupConsulClient(t, true)
// Verify federation between servers
logger.Log(t, "verifying federation was successful")
verifyFederation(t, primaryClient, secondaryClient, releaseName, true)
// Create a ProxyDefaults resource to configure services to use the mesh
// gateways.
logger.Log(t, "creating proxy-defaults config")
kustomizeDir := "../fixtures/bases/mesh-gateway"
k8s.KubectlApplyK(t, primaryContext.KubectlOptions(t), kustomizeDir)
helpers.Cleanup(t, cfg.NoCleanupOnFailure, func() {
k8s.KubectlDeleteK(t, primaryContext.KubectlOptions(t), kustomizeDir)
})
// Check that we can connect services over the mesh gateways
logger.Log(t, "creating static-server in dc2")
k8s.DeployKustomize(t, secondaryContext.KubectlOptions(t), cfg.NoCleanupOnFailure, cfg.DebugDirectory, "../fixtures/cases/static-server-inject")
logger.Log(t, "creating static-client in dc1")
k8s.DeployKustomize(t, primaryContext.KubectlOptions(t), cfg.NoCleanupOnFailure, cfg.DebugDirectory, "../fixtures/cases/static-client-multi-dc")
logger.Log(t, "creating intention")
_, err = primaryClient.Connect().IntentionUpsert(&api.Intention{
SourceName: staticClientName,
DestinationName: "static-server",
Action: api.IntentionActionAllow,
}, nil)
require.NoError(t, err)
logger.Log(t, "checking that connection is successful")
k8s.CheckStaticServerConnectionSuccessful(t, primaryContext.KubectlOptions(t), "http://localhost:1234")
})
}
}
// verifyFederation checks that the WAN federation between servers is successful
// by first checking members are alive from the perspective of both servers.
// If secure is true, it will also check that the ACL replication is running on the secondary server.
func verifyFederation(t *testing.T, primaryClient, secondaryClient *api.Client, releaseName string, secure bool) {
retrier := &retry.Timer{Timeout: 5 * time.Minute, Wait: 1 * time.Second}
start := time.Now()
// Check that server in dc1 is healthy from the perspective of the server in dc2, and vice versa.
// We're calling the Consul health API, as opposed to checking serf membership status,
// because we need to make sure that the federated servers can make API calls and forward requests
// from one server to another. From running tests in CI for a while and using serf membership status before,
// we've noticed that the status could be "alive" as soon as the server in the secondary cluster joins the primary
// and then switch to "failed". This would require us to check that the status is "alive" is showing consistently for
// some amount of time, which could be quite flakey. Calling the API in another datacenter allows us to check that
// each server can forward calls to another, which is what we need for connect.
retry.RunWith(retrier, t, func(r *retry.R) {
secondaryServerHealth, _, err := primaryClient.Health().Node(fmt.Sprintf("%s-consul-server-0", releaseName), &api.QueryOptions{Datacenter: "dc2"})
require.NoError(r, err)
require.Equal(r, secondaryServerHealth.AggregatedStatus(), api.HealthPassing)
primaryServerHealth, _, err := secondaryClient.Health().Node(fmt.Sprintf("%s-consul-server-0", releaseName), &api.QueryOptions{Datacenter: "dc1"})
require.NoError(r, err)
require.Equal(r, primaryServerHealth.AggregatedStatus(), api.HealthPassing)
if secure {
replicationStatus, _, err := secondaryClient.ACL().Replication(nil)
require.NoError(r, err)
require.True(r, replicationStatus.Enabled)
require.True(r, replicationStatus.Running)
}
})
logger.Logf(t, "Took %s to verify federation", time.Since(start))
}