|
4 | 4 | "crypto/sha256" |
5 | 5 | "crypto/subtle" |
6 | 6 | "crypto/tls" |
| 7 | + "encoding/pem" |
7 | 8 | "fmt" |
8 | 9 | "io" |
9 | 10 | "log" |
@@ -230,6 +231,26 @@ func checkBundleIsAsExpected(g Gomega, bundle fleet.Bundle, helmop fleet.HelmOp, |
230 | 231 | g.Expect(controllerutil.ContainsFinalizer(&bundle, finalize.BundleFinalizer)).To(BeTrue()) |
231 | 232 | } |
232 | 233 |
|
| 234 | +// createRancherCASecret creates a secret in cattle-system using the |
| 235 | +// certificate from svr and registers a DeferCleanup to delete it. |
| 236 | +func createRancherCASecret(svr *httptest.Server, secretName, dataKey string) { |
| 237 | + certPEM := pem.EncodeToMemory(&pem.Block{ |
| 238 | + Type: "CERTIFICATE", |
| 239 | + Bytes: svr.TLS.Certificates[0].Certificate[0], |
| 240 | + }) |
| 241 | + secret := &v1.Secret{ |
| 242 | + ObjectMeta: metav1.ObjectMeta{ |
| 243 | + Name: secretName, |
| 244 | + Namespace: "cattle-system", |
| 245 | + }, |
| 246 | + Data: map[string][]byte{dataKey: certPEM}, |
| 247 | + } |
| 248 | + Expect(k8sClient.Create(ctx, secret)).ToNot(HaveOccurred()) |
| 249 | + DeferCleanup(func() { |
| 250 | + _ = k8sClient.Delete(ctx, secret) |
| 251 | + }) |
| 252 | +} |
| 253 | + |
233 | 254 | func updateHelmOp(helmop fleet.HelmOp) error { |
234 | 255 | backoff := retry.DefaultBackoff |
235 | 256 | backoff.Steps = 10 |
@@ -1267,5 +1288,73 @@ var _ = Describe("HelmOps controller", func() { |
1267 | 1288 | }).Should(Succeed()) |
1268 | 1289 | }) |
1269 | 1290 | }) |
| 1291 | + |
| 1292 | + When("connecting to a https server with a CA bundle from Rancher tls-ca secret", func() { |
| 1293 | + BeforeEach(func() { |
| 1294 | + targets = []fleet.BundleTarget{} |
| 1295 | + helmop = getRandomHelmOpWithTargets("test-rancher-tlsca", targets) |
| 1296 | + helmop.Spec.Helm.Version = "" |
| 1297 | + helmop.Spec.HelmSecretName = "" |
| 1298 | + |
| 1299 | + svr := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 1300 | + w.WriteHeader(http.StatusOK) |
| 1301 | + fmt.Fprint(w, helmRepoIndex) |
| 1302 | + })) |
| 1303 | + DeferCleanup(svr.Close) |
| 1304 | + |
| 1305 | + helmop.Spec.Helm.Repo = svr.URL |
| 1306 | + helmop.Spec.Helm.Chart = "alpine" |
| 1307 | + helmop.Spec.InsecureSkipTLSverify = false |
| 1308 | + doAfterNamespaceCreated = func() { |
| 1309 | + createRancherCASecret(svr, "tls-ca", "cacerts.pem") |
| 1310 | + } |
| 1311 | + }) |
| 1312 | + |
| 1313 | + It("creates a bundle with the latest version it got from the index", func() { |
| 1314 | + Eventually(func(g Gomega) { |
| 1315 | + bundle := &fleet.Bundle{} |
| 1316 | + ns := types.NamespacedName{Name: helmop.Name, Namespace: helmop.Namespace} |
| 1317 | + err := k8sClient.Get(ctx, ns, bundle) |
| 1318 | + g.Expect(err).ToNot(HaveOccurred()) |
| 1319 | + t := []fleet.BundleTarget{{Name: "default", ClusterGroup: "default"}} |
| 1320 | + helmop.Spec.Helm.Version = "0.2.0" |
| 1321 | + checkBundleIsAsExpected(g, *bundle, helmop, t) |
| 1322 | + }).Should(Succeed()) |
| 1323 | + }) |
| 1324 | + }) |
| 1325 | + |
| 1326 | + When("connecting to a https server with a CA bundle from Rancher tls-ca-additional secret", func() { |
| 1327 | + BeforeEach(func() { |
| 1328 | + targets = []fleet.BundleTarget{} |
| 1329 | + helmop = getRandomHelmOpWithTargets("test-rancher-tlsca-additional", targets) |
| 1330 | + helmop.Spec.Helm.Version = "" |
| 1331 | + helmop.Spec.HelmSecretName = "" |
| 1332 | + |
| 1333 | + svr := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 1334 | + w.WriteHeader(http.StatusOK) |
| 1335 | + fmt.Fprint(w, helmRepoIndex) |
| 1336 | + })) |
| 1337 | + DeferCleanup(svr.Close) |
| 1338 | + |
| 1339 | + helmop.Spec.Helm.Repo = svr.URL |
| 1340 | + helmop.Spec.Helm.Chart = "alpine" |
| 1341 | + helmop.Spec.InsecureSkipTLSverify = false |
| 1342 | + doAfterNamespaceCreated = func() { |
| 1343 | + createRancherCASecret(svr, "tls-ca-additional", "ca-additional.pem") |
| 1344 | + } |
| 1345 | + }) |
| 1346 | + |
| 1347 | + It("creates a bundle with the latest version it got from the index", func() { |
| 1348 | + Eventually(func(g Gomega) { |
| 1349 | + bundle := &fleet.Bundle{} |
| 1350 | + ns := types.NamespacedName{Name: helmop.Name, Namespace: helmop.Namespace} |
| 1351 | + err := k8sClient.Get(ctx, ns, bundle) |
| 1352 | + g.Expect(err).ToNot(HaveOccurred()) |
| 1353 | + t := []fleet.BundleTarget{{Name: "default", ClusterGroup: "default"}} |
| 1354 | + helmop.Spec.Helm.Version = "0.2.0" |
| 1355 | + checkBundleIsAsExpected(g, *bundle, helmop, t) |
| 1356 | + }).Should(Succeed()) |
| 1357 | + }) |
| 1358 | + }) |
1270 | 1359 | }) |
1271 | 1360 | }) |
0 commit comments