-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathgo_getter_custom_ca_test.go
More file actions
306 lines (262 loc) · 11.3 KB
/
go_getter_custom_ca_test.go
File metadata and controls
306 lines (262 loc) · 11.3 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
package singlecluster_test
// These test cases rely on a local git server, so that they can be run locally and against PRs.
// For tests monitoring external git hosting providers, see `e2e/require-secrets`.
import (
"encoding/base64"
"fmt"
"math/rand"
"os"
"path"
"strings"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/rancher/fleet/e2e/testenv"
"github.com/rancher/fleet/e2e/testenv/githelper"
"github.com/rancher/fleet/e2e/testenv/kubectl"
"sigs.k8s.io/yaml"
)
// These tests cover HTTPS cloning of git repositories via the `git::https://` URL prefix in
// `helm.chart`, including TLS certificate configuration (custom CA bundles and InsecureSkipVerify).
// The implementation uses go-git directly; the `git::` prefix is detected by Fleet's source parser
// and dispatched to the go-git–based fetcher. The contents fetched from those repositories are
// expected to be helm charts.
var _ = Describe("Testing CA bundles and insecureSkipVerify for cloning git repositories", Label("infra-setup"), func() {
const (
sleeper = "sleeper"
entrypoint = "entrypoint"
)
var (
tmpDir string
cloneDir string
k kubectl.Command
gh *githelper.Git
gitrepoName string
r = rand.New(rand.NewSource(GinkgoRandomSeed()))
targetNamespace string
// Build git repo URL reachable _within_ the cluster, for the GitRepo
host = githelper.BuildGitHostname()
)
getExternalRepoURL := func(repoName string) string {
GinkgoHelper()
addr, err := githelper.GetExternalRepoAddr(env, HTTPSPort, repoName)
Expect(err).ToNot(HaveOccurred())
addr = strings.Replace(addr, "http://", "https://", 1)
return addr
}
mustReadFileAsBase64 := func(filePath string) string {
GinkgoHelper()
data, err := os.ReadFile(filePath)
Expect(err).ToNot(HaveOccurred(), "failed to read file: %s", filePath)
return base64.StdEncoding.EncodeToString(data)
}
// getCertificateFilePath returns the path to the CA certificate file used to set up the local
// git server.
getCertificateFilePath := func() string {
GinkgoHelper()
certsDir := os.Getenv("CI_OCI_CERTS_DIR")
Expect(certsDir).ToNot(BeEmpty())
return path.Join(certsDir, "helm.crt")
}
createInvalidCACertFile := func() *os.File {
tmpFile, err := os.CreateTemp("", "invalid-ca-*.crt")
Expect(err).ToNot(HaveOccurred(), "failed to create temp file for invalid CA bundle")
_, err = tmpFile.Write([]byte("invalid-ca-bundle"))
Expect(err).ToNot(HaveOccurred(), "failed to write invalid CA bundle to temp file")
err = tmpFile.Close()
Expect(err).ToNot(HaveOccurred(), "failed to close temp file for invalid CA bundle")
return tmpFile
}
expectGitRepoToNotBeReady := func(g Gomega) {
out, err := k.Get("gitrepo", gitrepoName, `-o=jsonpath={.status.display.readyBundleDeployments}`)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(out).To(ContainSubstring("0/0"))
}
expectGitRepoToBeReady := func(g Gomega) {
out, err := k.Get("gitrepo", gitrepoName, `-o=jsonpath={.status.display.readyBundleDeployments}`)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(out).To(ContainSubstring("1/1"))
}
type Auth struct {
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
CABundle string `json:"caBundle,omitempty"`
SSHPrivateKey string `json:"sshPrivateKey,omitempty"`
InsecureSkipVerify bool `json:"insecureSkipVerify,omitempty"`
BasicHTTP bool `json:"basicHTTP,omitempty"`
}
type AuthConfigByPath map[string]Auth
createHelmSecretForPaths := func(secretName string, config AuthConfigByPath) {
GinkgoHelper()
data, err := yaml.Marshal(config)
Expect(err).ToNot(HaveOccurred())
file, err := os.CreateTemp("", "helm-auth-*.yaml")
Expect(err).ToNot(HaveOccurred(), "failed to create temp file for helm auth config")
defer os.Remove(file.Name())
_, err = file.Write(data)
Expect(err).ToNot(HaveOccurred(), "failed to write helm auth config to temp file")
_, err = k.Create("secret", "generic", secretName, "--from-file=secrets-path.yaml="+file.Name(), "-n", env.Namespace)
Expect(err).ToNot(HaveOccurred())
}
type gitRepoOptions struct {
CABundle string
InsecureSkipTLSVerify bool
HelmSecretName string
HelmSecretNameForPaths string
}
createGitRepo := func(options gitRepoOptions) {
GinkgoHelper()
values := gitRepoTestValues{
// Defaults
Name: gitrepoName,
Repo: gh.GetInClusterURL(host, HTTPSPort, "repo"),
Branch: gh.Branch,
PollingInterval: "15s", // default
TargetNamespace: targetNamespace, // to avoid conflicts with other tests
Path: entrypoint,
// Customizations
CABundle: options.CABundle,
InsecureSkipTLSVerify: options.InsecureSkipTLSVerify,
HelmSecretName: options.HelmSecretName,
HelmSecretNameForPaths: options.HelmSecretNameForPaths,
}
err := testenv.ApplyTemplate(k, testenv.AssetPath("gitrepo/gitrepo.yaml"), values)
Expect(err).ToNot(HaveOccurred())
}
BeforeEach(func() {
k = env.Kubectl.Namespace(env.Namespace)
})
JustBeforeEach(func() {
// Create the first repository
addr := getExternalRepoURL("repo")
gh = githelper.NewHTTP(addr)
tmpDir, err := os.MkdirTemp("", "fleet-")
Expect(err).ToNot(HaveOccurred())
cloneDir = path.Join(tmpDir, "repo") // Fixed and built into the container image.
gitrepoName = testenv.RandomFilename("gitjob-test", r)
// Creates the content in the sleeper directory
_, err = gh.Create(cloneDir, testenv.AssetPath("gitrepo/sleeper-chart"), sleeper)
Expect(err).ToNot(HaveOccurred())
// Create the second repository, referencing the Helm chart from the
// first repository through a fleet.yaml
tmpAssetDir := path.Join(tmpDir, "entryPoint")
err = os.Mkdir(tmpAssetDir, 0755)
Expect(err).ToNot(HaveOccurred())
url := "git::" + gh.GetInClusterURL(host, HTTPSPort, "repo//"+sleeper)
err = os.WriteFile(
path.Join(tmpAssetDir, "fleet.yaml"),
fmt.Appendf([]byte{}, "helm:\n chart: %s\n", url),
0755,
)
Expect(err).NotTo(HaveOccurred())
_, err = gh.Add(cloneDir, tmpAssetDir, entrypoint)
Expect(err).ToNot(HaveOccurred())
})
AfterEach(func() {
_ = os.RemoveAll(tmpDir)
_, _ = k.Delete("gitrepo", gitrepoName)
_, _ = k.Delete("ns", targetNamespace)
})
When("testing custom CA bundles for cloning git with HTTPS (fleet.yaml)", func() {
It("should succeed when not configuring any CA", func() {
// Create and apply GitRepo, don't configure CABundle, InsecureSkipTLSVerify,
// helmSecretName, or helmSecretNameForPath in GitRepo.spec which makes it fall back to
// Rancher certificates that work.
createGitRepo(gitRepoOptions{})
Eventually(expectGitRepoToBeReady).Should(Succeed())
})
It("should succeed when using the correct CA bundle provided in GitRepo's CABundle field", func() {
encodedCert := mustReadFileAsBase64(getCertificateFilePath())
createGitRepo(gitRepoOptions{CABundle: encodedCert})
Eventually(expectGitRepoToBeReady).Should(Succeed())
})
It("should fail when using the incorrect CA bundle provided in GitRepo's CABundle field", func() {
// But it should fail in gitcloner already, not later in fleet apply.
encodedCert := base64.StdEncoding.EncodeToString([]byte("invalid-ca-bundle"))
createGitRepo(gitRepoOptions{CABundle: encodedCert})
Eventually(expectGitRepoToNotBeReady).Should(Succeed())
Consistently(expectGitRepoToNotBeReady).Should(Succeed())
})
It("should succeed when using the correct CA bundle provided in helmSecretName", func() {
helmCrtFile := getCertificateFilePath()
// Create secret with CA bundle
secretName := testenv.RandomFilename("helm-ca-bundle", r)
out, err := k.Create("secret", "generic", secretName, "--from-file=cacerts="+helmCrtFile, "-n", env.Namespace)
Expect(err).ToNot(HaveOccurred(), out)
createGitRepo(gitRepoOptions{HelmSecretName: secretName})
Eventually(expectGitRepoToBeReady).Should(Succeed())
})
// That's the one that produces an error in the gitjob, but we provoked it by setting a
// certificate that it can't use. That's basically what the resulting error says.
It("should fail when using an incorrect CA bundle provided in helmSecretName", func() {
invalidCACertsFile := createInvalidCACertFile()
defer os.Remove(invalidCACertsFile.Name())
// Create secret with CA bundle
secretName := testenv.RandomFilename("helm-ca-bundle", r)
out, err := k.Create("secret", "generic", secretName, "--from-file=cacerts="+invalidCACertsFile.Name(), "-n", env.Namespace)
Expect(err).ToNot(HaveOccurred(), out)
createGitRepo(gitRepoOptions{HelmSecretName: secretName})
Eventually(expectGitRepoToNotBeReady).Should(Succeed())
Consistently(expectGitRepoToNotBeReady).Should(Succeed())
})
It("should succeed when using the correct CA bundle provided in helmSecretNameForPath", func() {
secretName := "helm-ca-bundle-by-path-" + gitrepoName
createHelmSecretForPaths(
secretName,
AuthConfigByPath{
entrypoint: {
CABundle: mustReadFileAsBase64(getCertificateFilePath()),
},
},
)
createGitRepo(gitRepoOptions{HelmSecretNameForPaths: secretName})
Eventually(expectGitRepoToBeReady).Should(Succeed())
})
It("should not succeed when using an incorrect CA bundle provided in helmSecretNameForPath", func() {
secretName := "helm-ca-bundle-by-path-" + gitrepoName
createHelmSecretForPaths(
secretName,
AuthConfigByPath{
entrypoint: {
CABundle: "asdf", // invalid
},
},
)
createGitRepo(gitRepoOptions{HelmSecretNameForPaths: secretName})
Consistently(expectGitRepoToNotBeReady).Should(Succeed())
})
})
When("testing ignoring insecure certificates for cloning git repositories with HTTPS (fleet.yaml)", func() {
It("should succeed when using the incorrect CA bundle provided in GitRepo's CABundle field but setting InsecureSkipTLSVerify to true", func() {
// But it should fail in gitcloner already, not later in fleet apply.
encodedCert := base64.StdEncoding.EncodeToString([]byte("invalid-ca-bundle"))
createGitRepo(gitRepoOptions{CABundle: encodedCert, InsecureSkipTLSVerify: true})
Eventually(expectGitRepoToBeReady).Should(Succeed())
})
It("should succeed when using the incorrect CA bundle provided in helmSecretName but setting InsecureSkipVerify to true", func() {
invalidCertFile := createInvalidCACertFile()
defer os.Remove(invalidCertFile.Name())
secretName := testenv.RandomFilename("helm-ca-bundle", r)
out, err := k.Create("secret", "generic", secretName,
"--from-file=cacerts="+invalidCertFile.Name(),
"--from-literal=insecureSkipVerify=true",
"-n", env.Namespace)
Expect(err).ToNot(HaveOccurred(), out)
createGitRepo(gitRepoOptions{HelmSecretName: secretName})
Eventually(expectGitRepoToBeReady).Should(Succeed())
})
It("should succeed when using the incorrect CA bundle provided in helmSecretNameForPath but setting InsecureSkipVerify to true", func() {
secretName := "helm-ca-bundle-by-path-" + gitrepoName
createHelmSecretForPaths(
secretName,
AuthConfigByPath{
entrypoint: {
CABundle: "asdf", // invalid
InsecureSkipVerify: true,
},
},
)
createGitRepo(gitRepoOptions{HelmSecretNameForPaths: secretName})
Eventually(expectGitRepoToBeReady).Should(Succeed())
})
})
})