Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fall back to Rancher-configured CA bundles #3233

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

weyfonk
Copy link
Contributor

@weyfonk weyfonk commented Jan 22, 2025

This enables Fleet to rely on Rancher-configured CA bundle secrets in the following clients:

  • git remote lister and cloner: those CA bundles no longer need to be provided in a GitRepo's spec. Fleet will extract CA bundle data from Rancher secrets if (and only if) no CA bundle is provided for a GitRepo.
  • Helm client: Fleet will extract, populate and mount Rancher CA bundles in the following cases:
    • no HelmSecretName nor HelmSecretNameForPaths secret is provided on a GitRepo
    • one of those secrets is provided, but only specifies username(s) and password(s), without a CA bundle

This is tested through a combination of:

  • unit tests for common Rancher CA bundle data extraction logic
  • integration tests for mounting and use of that data by the gitOps controller
  • end-to-end tests for successful deployment of workloads with git repositories and Helm charts hosted on infrastructure using a custom CA. Those tests required adapting configuration and deployment of our test git server to support HTTPS beside HTTP and make use of already generated custom certificates.

Limitations

This does not cover imagescan (pending further discussion on the future of that feature), nor the HelmOps controller.

Falling back to Rancher CA bundles in HelmOps would require reading the HelmApp's HelmSecretName field, and populating a CA bundle if that field is empty or if the referenced secret does not contain any CA bundle.

How this should be done to support both version handling in the controller and chart downloads from the agent, is unclear for now: should an empty HelmSecretName be overwritten by the HelmOps controller, or an incomplete secret edited to add a Rancher CA bundle? This could be handled in a follow-up patch.

Refers to #2750
Related docs PR: rancher/fleet-docs#212

@weyfonk weyfonk requested a review from a team as a code owner January 22, 2025 08:33
@weyfonk weyfonk marked this pull request as draft January 22, 2025 08:38
@weyfonk weyfonk force-pushed the 2750-custom-ca-fallback branch from 13c1226 to 90bbe29 Compare January 22, 2025 09:32
This commit provides a new `cert` package, containing logic for
extraction of CA bundle information from known Rancher secrets.
That new package is expected to be used in Fleet clients which will need
to fall back to Rancher CA bundles when no custom bundle is provided.
This makes use of the new `cert` package to use a CA bundle from Rancher
secrets, if configured and if the `GitRepo` does not specify any such CA
bundle.
This makes use of the new `cert` package to use a CA bundle from Rancher
secrets, if configured and if the `GitRepo` does not already specify any
such CA bundle.
This does not alter the git cloner itself, but allows the CA bundle
secret built by the gitOps controller (and later mounted for use by the
git cloner) to use CA bundle information extracted from Rancher secrets
as an alternative source.
This makes use of the new `cert` package to use a CA bundle from Rancher
secrets, if configured and if the `GitRepo` does not already specify any
Helm secret. If those conditions are met, the gitOps controller creates
a new secret with CA bundle information from Rancher.

This does not alter the Helm client itself, and could be optimised to
avoid duplicating the CA bundle secret in case it already exists, with
identical contents, due to no CA bundle being configured on the
`GitRepo`.
Those expectations must take the `Eventually` function's `Gomega`
argument into account to properly work inside that block.
This had been omitted in a previous commit, leading to the git cloner
being unable to clone a git repository hosted on a server using HTTPS
with certificates signed by a private CA.
This covers fallback in git clients. It uses an updated version of the
test git server, which serves HTTPS, beside HTTP, with a custom CA
bundle.
This caters for an edge case whereby a `GitRepo` may specify a Helm
secret, for instance through `HelmSecretName`, which does not contain a
CA bundle.
When that happens, Fleet will fall back to Rancher-configured CA
bundles, if any, while any existing CA bundle in the `GitRepo`'s Helm
secret would, as before, be used with higher precedence.
This makes use of new Rancher-like secret [1] creation logic when
setting up Fleet for end-to-end testing purposes. This allows tests
validating fallback to Rancher-configured CA bundles to pass.

[1]: https://ranchermanager.docs.rancher.com/getting-started/installation-and-upgrade/installation-references/helm-chart-options#additional-trusted-cas
That logic was already close to covered in unit tests. This moves it
there, to remove clutter in integration tests, at the risk of adding
some to unit tests.
Copy link
Contributor

@0xavi0 0xavi0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

I wonder if we could get the Rancher secrets (names and values) from a configMap or something. (not sure if such configMap exists in Rancher or we could have that in fleet to centralise things)

And... in the same way....I would maybe set all the hardcoded strings for certs paths, secret values, keys, secret names, etc... as constants or functions. (So it is easier and faster to change them if needed)

@@ -50,5 +50,6 @@ k3d cluster create "$name" \
-p "$(( 8081 + offs )):8081@server:0" \
-p "$(( 8082 + offs )):8082@server:0" \
-p "$unique_tls_port:443@server:0" \
-p "8043:4343@server:0" \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Why is not using the offset like the rest?


addr, err := githelper.GetExternalRepoAddr(env, HTTPSPort, repoName)
Expect(err).ToNot(HaveOccurred())
addr = strings.Replace(addr, "http://", "https://", 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question:
Are these tests the same than the HTTP ones except for this line?
(I haven't checked 100% but both blocks look very similar)
If so... maybe better to just add the http/https as a variable and reuse the code?

@@ -62,9 +64,38 @@ var _ = Describe("Single Cluster Examples", Label("infra-setup"), func() {
}).Should(ContainSubstring("sleeper-"))
})
})
Context("containing a private HTTP-based helm chart with repo path and no configured Helm secret name", Label("helm-registry"), func() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It says "and no configured Helm secret" but it is passing a secret? is it a typo?

// Fall back to Rancher-configured secrets
// We need to copy secret data from Rancher, because Rancher secrets live in a different namespace and
// can therefore not be used as sources for a volume.
secretName := fmt.Sprintf("%s-rancher-cabundle", gitrepo.Name)
Copy link
Contributor

@0xavi0 0xavi0 Feb 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Maybe better to have these names in constants or a function? Similar to the caBundleName function.

(see the main comment for other hardcoded strings)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 👀 In review
Development

Successfully merging this pull request may close these issues.

2 participants