WIP: feat: add support for TLS and extra Certificate Authorities #657

Draft
joshsizer wants to merge 5 commits from joshsizer/add-tls-and-extra-ca-support into main
joshsizer commented 2024-05-21 15:54:31 +00:00 (Migrated from gitea.com)

Description of the change

This change has two features associated:

  • allows a user to pass in a TLS secret to Gitea chart. When TLS is enabled and a TLS secret is passed, the secret will be mounted to the Gitea container and used by Gitea to serve HTTPS
  • allows a user to pass in multiple CA cert secrets to the Gitea chart. When CA is enabled and CA secret(s) are passed in, they will be copied to the OS's trust store and made available to trust external services, like Postgres or Redis.

Benefits

The benefits of this change is that if a user is using PKI, with a private Certificate Authority, they can utilize the PKI with Gitea and its dependencies

Possible drawbacks

This will not generate the PKI for you - you must provide it (using cert-manager makes this easy)

Checklist

  • Parameters are documented in the values.yaml and added to the README.md using readme-generator-for-helm
  • Breaking changes are documented in the README.md (not available here - no breaking changes)
  • Templating unittests are added
<!-- Before you open the request please review the following guidelines and tips to help it be more easily integrated: - Describe the scope of your change - i.e. what the change does. - Describe any known limitations with your change. - Please run any tests or examples that can exercise your modified code. Thank you for contributing! We will try to review, test and integrate the change as soon as we can. --> ### Description of the change This change has two features associated: - allows a user to pass in a TLS secret to Gitea chart. When TLS is enabled and a TLS secret is passed, the secret will be mounted to the Gitea container and used by Gitea to serve HTTPS - allows a user to pass in multiple CA cert secrets to the Gitea chart. When CA is enabled and CA secret(s) are passed in, they will be copied to the OS's trust store and made available to trust external services, like Postgres or Redis. ### Benefits The benefits of this change is that if a user is using PKI, with a private Certificate Authority, they can utilize the PKI with Gitea and its dependencies ### Possible drawbacks This will not generate the PKI for you - you must provide it (using cert-manager makes this easy) ### Checklist <!-- [Place an '[X]' (no spaces) in all applicable fields. Please remove unrelated fields.] --> - [x] Parameters are documented in the `values.yaml` and added to the `README.md` using [readme-generator-for-helm](https://github.com/bitnami-labs/readme-generator-for-helm) - [ ] Breaking changes are documented in the `README.md` (not available here - no breaking changes) - [x] Templating unittests are added
justusbunsi (Migrated from gitea.com) reviewed 2024-05-21 15:54:32 +00:00
pat-s (Migrated from gitea.com) reviewed 2024-05-21 15:54:32 +00:00
pat-s commented 2024-05-23 09:11:55 +00:00 (Migrated from gitea.com)

Hey @joshsizer, thanks for contributing!

I think both of your suggested additions are already possible right now:

Did you see/try these upfront and if so, why weren't they sufficient for your use case?

Hey @joshsizer, thanks for contributing! I think both of your suggested additions are already possible right now: - Reference a secret for TLS: https://gitea.com/gitea/helm-chart/src/commit/b13063ad7a9a0364820bd6a23a7232806abf0523/values.yaml#L174 - Mounting additional CA bundles via `extraVolumeMounts` and `extraVolumes` Did you see/try these upfront and if so, why weren't they sufficient for your use case?
joshsizer commented 2024-05-23 15:33:56 +00:00 (Migrated from gitea.com)

Hey @joshsizer, thanks for contributing!

I think both of your suggested additions are already possible right now:

Did you see/try these upfront and if so, why weren't they sufficient for your use case?

Hey @pat-s, thanks for taking a look at this MR!

I'll address both of points you brought up:

  1. The secret for TLS you referenced is for an Ingress, which is a different situation than what this MR is attempting to solve for. Currently, this chart doesn't support in a first-class way enabling TLS on the Gitea server itself. If a user of the chart wants to implement Pod to Pod TLS, without using something like Istio, they have to do so through extraVolumes and extraVolumeMounts. While this is possible, it's not a user-friendly interface - I took a lot of inspiration for the TLS changes from bitnami charts like Redis and MongoDB, which support setting TLS on the servers themselves, which is different than at the ingress level. Happy to expand on this if needed.

  2. Unfortunately, simply adding additional CA bundles via extraVolumes and extraVolumeMounts does not work. You need to place the CA bundles on the correct place in the file system, and call the update-ca-certificates command. This MR helps the user mount the CAs in the correct place, as well as add some initContainers that call the update-ca-certificates command. Additionally, this was tested with the rootless image, which is why there is an extra initContainer to copy the default system certs & additional certs to an emptyDir volume with permissions that respect the securityContexts.

So, I think this MR does add a lot of value to the chart, since it gives users an easy interface to enabling TLS on the Gitea server (for inter-pod TLS), as well as adds the ability to trust additional CAs.

> Hey @joshsizer, thanks for contributing! > > I think both of your suggested additions are already possible right now: > > - Reference a secret for TLS: https://gitea.com/gitea/helm-chart/src/commit/b13063ad7a9a0364820bd6a23a7232806abf0523/values.yaml#L174 > - Mounting additional CA bundles via `extraVolumeMounts` and `extraVolumes` > > Did you see/try these upfront and if so, why weren't they sufficient for your use case? Hey @pat-s, thanks for taking a look at this MR! I'll address both of points you brought up: 1. The secret for TLS you referenced is for an Ingress, which is a different situation than what this MR is attempting to solve for. Currently, this chart doesn't support in a first-class way enabling TLS on the Gitea server itself. If a user of the chart wants to implement Pod to Pod TLS, without using something like Istio, they have to do so through `extraVolumes` and `extraVolumeMounts`. While this is possible, it's not a user-friendly interface - I took a lot of inspiration for the TLS changes from bitnami charts like Redis and MongoDB, which support setting TLS on the servers themselves, which is different than at the ingress level. Happy to expand on this if needed. 2. Unfortunately, simply adding additional CA bundles via `extraVolumes` and `extraVolumeMounts` does not work. You need to place the CA bundles on the correct place in the file system, _and_ call the `update-ca-certificates` command. This MR helps the user mount the CAs in the correct place, as well as add some initContainers that call the `update-ca-certificates` command. Additionally, this was tested with the rootless image, which is why there is an extra initContainer to copy the default system certs & additional certs to an emptyDir volume with permissions that respect the securityContexts. So, I think this MR does add a lot of value to the chart, since it gives users an easy interface to enabling TLS on the Gitea server (for inter-pod TLS), as well as adds the ability to trust additional CAs.
justusbunsi commented 2024-05-23 17:53:41 +00:00 (Migrated from gitea.com)

There is a way to properly mount custom CAs into Gitea containers. https://github.com/go-gitea/gitea/issues/14102#issuecomment-1305973019
The referenced values are now located at https://github.com/cmu-sei/foundry-appliance/blob/main/foundry%2Fgitea.values.yaml

But I am open to adding a value setting to simplify mounting custom CAs into the correct place. @pat-s, you too?

There is a way to properly mount custom CAs into Gitea containers. https://github.com/go-gitea/gitea/issues/14102#issuecomment-1305973019 The referenced values are now located at https://github.com/cmu-sei/foundry-appliance/blob/main/foundry%2Fgitea.values.yaml But I am open to adding a value setting to simplify mounting custom CAs into the correct place. @pat-s, you too?
joshsizer commented 2024-05-23 19:04:40 +00:00 (Migrated from gitea.com)

There is a way to properly mount custom CAs into Gitea containers. https://github.com/go-gitea/gitea/issues/14102#issuecomment-1305973019
The referenced values are now located at https://github.com/cmu-sei/foundry-appliance/blob/main/foundry%2Fgitea.values.yaml

I just tried this locally, without calling update-ca-certificates and it worked! (mounting the certs to /etc/ssl/certs) Thanks for the pointer.

I can go ahead and update the MR to simplify this.

But I am open to adding a value setting to simplify mounting custom CAs into the correct place. @pat-s, you too?

@justusbunsi What do you think about simplifying setting Gitea up for serving TLS (the other set of changes in this MR)?

> There is a way to properly mount custom CAs into Gitea containers. https://github.com/go-gitea/gitea/issues/14102#issuecomment-1305973019 > The referenced values are now located at https://github.com/cmu-sei/foundry-appliance/blob/main/foundry%2Fgitea.values.yaml I just tried this locally, without calling update-ca-certificates and it worked! (mounting the certs to /etc/ssl/certs) Thanks for the pointer. I can go ahead and update the MR to simplify this. > But I am open to adding a value setting to simplify mounting custom CAs into the correct place. @pat-s, you too? @justusbunsi What do you think about simplifying setting Gitea up for serving TLS (the other set of changes in this MR)?
justusbunsi commented 2024-05-23 20:14:04 +00:00 (Migrated from gitea.com)

What do you think about simplifying setting Gitea up for serving TLS (the other set of changes in this MR)?

Hrm. On the one hand I see the benefit and understand your use case. But I also agree with @pat-s that this is already possible using the extraVolumes/extraVolumeMounts option.
Like with custom CAs, I personally think a dedicated option helps setting up Gitea in a consistent way. However, the current approach looks like it would somehow conflict with ingress.tls. Having tls.enabled: true but not setting ingress.tls while ingress.enabled: true results in an ingress routing from a non-https port to a https port. So there has to be some sort of failsafe between both options. And right now I am not sure how to handle that conflict.

But before continuing this part, let's wait for @pat-s thoughts on it. I am open to both, as long as it simplifies the overall configuration by adding new options.

And a heads up: In case we agree on adding both options, please split this PR into two. One for CAs and one for tls.

> What do you think about simplifying setting Gitea up for serving TLS (the other set of changes in this MR)? Hrm. On the one hand I see the benefit and understand your use case. But I also agree with @pat-s that this is already possible using the extraVolumes/extraVolumeMounts option. Like with custom CAs, I personally think a dedicated option helps setting up Gitea in a consistent way. However, the current approach looks like it would somehow conflict with `ingress.tls`. Having `tls.enabled: true` but not setting `ingress.tls` while `ingress.enabled: true` results in an ingress routing from a non-https port to a https port. So there has to be some sort of failsafe between both options. And right now I am not sure how to handle that conflict. But before continuing this part, let's wait for @pat-s thoughts on it. I am open to both, as long as it simplifies the overall configuration by adding new options. And a heads up: In case we agree on adding both options, please split this PR into two. One for CAs and one for tls.
pat-s commented 2024-05-24 11:55:16 +00:00 (Migrated from gitea.com)

There is a way to properly mount custom CAs into Gitea containers. https://github.com/go-gitea/gitea/issues/14102#issuecomment-1305973019

I wanted to point out this general approach as well, but I wouldn't have had a link that even references a blog post.
I also know the pain of calling update-ca-certificates and update-ca-trust (RHEL) but I've also found out in the last year that every distro has their specific locations which "auto-add" the certs in them to the trust-store. Not sure if this is something that existed forever or has been added recently but maybe it is also just that the websearch is still dominated by the call "update-ca-xy" approach.

But I am open to adding a value setting to simplify mounting custom CAs into the correct place. @pat-s, you too?

Yes and no.
Yes: Sure, simplifying the workflow to add/mount custom resources is always great from a user PoV.
No: But we also need to make sure to follow "best practices" WRT to helm chart templating/structuring to make it easy to use the chart.
I.e. I am not aware of other charts right now that have a dedicated config option to mount custom CA's. The "canonical" way to add such is by using extraVolumeMounts and friends. These exist in every chart. And if one has to do this once, chances are high one needs to do this multiple times. And if this holds true, one can just c/p the YAML which is .
I am managing a lot of chart-based deployments myself and every time I come across a "custom" chart which does everything different in terms of naming and overall structure, I am a bit 🥴

A good overall reference are the bitnami charts as they represent a quasi standard in the field due to their popularity.
In the end I like the idea of generic extraVolumeMounts and extraVolumes as they allow doing pretty much anything that is needed in private environments WRT to custom assets. It is important that they allow both referencing k8s-secrets and other input types.

So for your use case, you should be able to do both the internal TLS communication handling and CA cert parsing using the existing extraContainerVolumeMounts. If not, we can certainly help/find a way to get this going for you.

@justusbunsi Let me know what your thoughts are on the topic of "custom chart additions" and "best practices" I shared above. This is of course off-topic here somewhat but also an important point overall.

> There is a way to properly mount custom CAs into Gitea containers. https://github.com/go-gitea/gitea/issues/14102#issuecomment-1305973019 I wanted to point out this general approach as well, but I wouldn't have had a link that even references a blog post. I also know the pain of calling `update-ca-certificates` and `update-ca-trust` (RHEL) but I've also found out in the last year that every distro has their specific locations which "auto-add" the certs in them to the trust-store. Not sure if this is something that existed forever or has been added recently but maybe it is also just that the websearch is still dominated by the call "update-ca-xy" approach. > But I am open to adding a value setting to simplify mounting custom CAs into the correct place. @pat-s, you too? Yes and no. Yes: Sure, simplifying the workflow to add/mount custom resources is always great from a user PoV. No: But we also need to make sure to follow "best practices" WRT to helm chart templating/structuring to make it easy to use the chart. I.e. I am not aware of other charts right now that have a dedicated config option to mount custom CA's. The "canonical" way to add such is by using `extraVolumeMounts` and friends. These exist in every chart. And if one has to do this once, chances are high one needs to do this multiple times. And if this holds true, one can just c/p the YAML which is . I am managing a lot of chart-based deployments myself and every time I come across a "custom" chart which does everything different in terms of naming and overall structure, I am a bit 🥴️ A good overall reference are the bitnami charts as they represent a quasi standard in the field due to their popularity. In the end I like the idea of generic `extraVolumeMounts` and `extraVolumes` as they allow doing pretty much anything that is needed in private environments WRT to custom assets. It is important that they allow both referencing k8s-secrets and other input types. So for your use case, you should be able to do both the internal TLS communication handling and CA cert parsing using the existing `extraContainerVolumeMounts`. If not, we can certainly help/find a way to get this going for you. @justusbunsi Let me know what your thoughts are on the topic of "custom chart additions" and "best practices" I shared above. This is of course off-topic here somewhat but also an important point overall.
joshsizer commented 2024-05-24 15:12:26 +00:00 (Migrated from gitea.com)

I'm very sympathetic to following best practices - helm charts can become unruly very quickly.

I would like to mention that some bitnami charts, like Redis, MongoDB, and PostgreSQL support enabling TLS on the server (they will mount the secrets to the pods for you).

On the CA side, I actually feel like this is a feature that is missing from these charts. While I understand you can accomplish adding extra CAs through extraVolumes and extraVolumesMounts, I've always perceived these as escape hatches for the user - for when the chart doesn't handle something they need in a first class way. It would always be preferable if the chart handled it in a first class way.

Of course, a lot of the ways people use these escape hatches in charts are pretty unique to them, but I don't think the ability to enable TLS or to add CAs is all that esoteric.

I'm very sympathetic to following best practices - helm charts can become unruly very quickly. I would like to mention that some bitnami charts, like [Redis](https://github.com/bitnami/charts/blob/main/bitnami/redis/values.yaml#L1640), [MongoDB](https://github.com/bitnami/charts/blob/main/bitnami/mongodb/values.yaml#L196), and [PostgreSQL](https://github.com/bitnami/charts/blob/main/bitnami/postgresql/values.yaml#L269) support enabling TLS on the server (they will mount the secrets to the pods for you). On the CA side, I actually feel like this is a feature that is _missing_ from these charts. While I understand you can accomplish adding extra CAs through `extraVolumes` and `extraVolumesMounts`, I've always perceived these as escape hatches for the user - for when the chart doesn't handle something they need in a first class way. It would always be preferable if the chart handled it in a first class way. Of course, a lot of the ways people use these escape hatches in charts are pretty unique to them, but I don't think the ability to enable TLS or to add CAs is all that esoteric.
justusbunsi commented 2024-05-27 14:35:47 +00:00 (Migrated from gitea.com)

I'm very sympathetic to following best practices - helm charts can become unruly very quickly.

@justusbunsi Let me know what your thoughts are on the topic of "custom chart additions" and "best practices" I shared above.

It seems we all aim for a rock-solid solution and are fine with following best-practices. So here's my suggestion:

> I'm very sympathetic to following best practices - helm charts can become unruly very quickly. > @justusbunsi Let me know what your thoughts are on the topic of "custom chart additions" and "best practices" I shared above. It seems we all aim for a rock-solid solution and are fine with following best-practices. So here's my suggestion: - Having https://github.com/go-gitea/gitea/issues/14102#issuecomment-1305973019 in mind: Simply mounting custom CA to the right place via `extraVolumes` and `extraVolumeMounts` would be a great example for the Chart documentation. Either at `docs/` or within the README. That should probably eliminate the implementation needs. - For the TLS part of this PR I suggest we improve the `ingress.tls` handling when configuring Gitea. As mentioned in https://gitea.com/gitea/helm-chart/pulls/657#issuecomment-824334, that is missing and might be the initial intention to add the new settings. @joshsizer please correct me if I'm wrong here.
pat-s commented 2024-05-30 07:13:57 +00:00 (Migrated from gitea.com)

On the CA side, I actually feel like this is a feature that is missing from these charts. While I understand you can accomplish adding extra CAs through extraVolumes and extraVolumesMounts, I've always perceived these as escape hatches for the user - for when the chart doesn't handle something they need in a first class way. It would always be preferable if the chart handled it in a first class way.

I understand where you're coming from. I also don't know why things are as they are, but what I can think of is:

  • Mount paths differ depending on the OS used in the image. And yes, I know that one could just say "we only guarantee that it works with the official images".
  • Custom CAs are often "optional" and people (chart creators) just focus on the "important" parts
  • The widespread use of extraVolumeMounts/extraVolumes

Ingress TLS: Agree with @justusbunsi

> On the CA side, I actually feel like this is a feature that is missing from these charts. While I understand you can accomplish adding extra CAs through extraVolumes and extraVolumesMounts, I've always perceived these as escape hatches for the user - for when the chart doesn't handle something they need in a first class way. It would always be preferable if the chart handled it in a first class way. I understand where you're coming from. I also don't know why things are as they are, but what I can think of is: - Mount paths differ depending on the OS used in the image. And yes, I know that one could just say "we only guarantee that it works with the official images". - Custom CAs are often "optional" and people (chart creators) just focus on the "important" parts - The widespread use of `extraVolumeMounts`/`extraVolumes` Ingress TLS: Agree with @justusbunsi
joshsizer commented 2024-05-30 18:25:10 +00:00 (Migrated from gitea.com)

On the CA side, I actually feel like this is a feature that is missing from these charts. While I understand you can accomplish adding extra CAs through extraVolumes and extraVolumesMounts, I've always perceived these as escape hatches for the user - for when the chart doesn't handle something they need in a first class way. It would always be preferable if the chart handled it in a first class way.

I understand where you're coming from. I also don't know why things are as they are, but what I can think of is:

  • Mount paths differ depending on the OS used in the image. And yes, I know that one could just say "we only guarantee that it works with the official images".
  • Custom CAs are often "optional" and people (chart creators) just focus on the "important" parts
  • The widespread use of extraVolumeMounts/extraVolumes

Ingress TLS: Agree with @justusbunsi

Hey folks, actually just ran into an issue with the way this MR sets up CAs - first off it only handles secrets - what if a CA is passed in as a config map? What if it has a different key (ca.pem vs ca.crt)? So I am totally onboard with ya'll, let's just let users configure the CAs through extraVolumes and extraVolumeMounts.

I will look into how to better check for ingress TLS vs Gitea TLS conflicts. With NGINX, you can add an annotation:

nginx.ingress.kubernetes.io/backend-protocol: HTTPS, but I'm not sure if there is an implementation agnostic way to configure this. Not sure if you guys have any input here?

> > On the CA side, I actually feel like this is a feature that is missing from these charts. While I understand you can accomplish adding extra CAs through extraVolumes and extraVolumesMounts, I've always perceived these as escape hatches for the user - for when the chart doesn't handle something they need in a first class way. It would always be preferable if the chart handled it in a first class way. > > I understand where you're coming from. I also don't know why things are as they are, but what I can think of is: > > - Mount paths differ depending on the OS used in the image. And yes, I know that one could just say "we only guarantee that it works with the official images". > - Custom CAs are often "optional" and people (chart creators) just focus on the "important" parts > - The widespread use of `extraVolumeMounts`/`extraVolumes` > > Ingress TLS: Agree with @justusbunsi > Hey folks, actually just ran into an issue with the way this MR sets up CAs - first off it only handles secrets - what if a CA is passed in as a config map? What if it has a different key (ca.pem vs ca.crt)? So I am totally onboard with ya'll, let's just let users configure the CAs through extraVolumes and extraVolumeMounts. I will look into how to better check for ingress TLS vs Gitea TLS conflicts. With NGINX, you can add an annotation: `nginx.ingress.kubernetes.io/backend-protocol: HTTPS`, but I'm not sure if there is an implementation agnostic way to configure this. Not sure if you guys have any input here?
pat-s commented 2024-05-31 12:32:55 +00:00 (Migrated from gitea.com)

Due to the variety of backends, ingress often works through annotations, like the one shown for NGINX. I don't have much experience with this specific one, though.

I didn't check in detail all comments above again, but in my case, I usually use ingress.tls[0].secretName and reference to a secret named <domain>-tls (which in my case is generated by certmanager). This is combined with the ingress-nginx chart. AFAIU though you want to run Gitea itself with HTTPS directly rather than HTTP so the connection between the ingress controller and the service endpoint is also encrypted.

I've done this a few times in some environments (but not for Gitea) and always found it quite a hassle. But it might/should be possible.

Due to the variety of backends, ingress often works through annotations, like the one shown for NGINX. I don't have much experience with this specific one, though. I didn't check in detail all comments above again, but in my case, I usually use `ingress.tls[0].secretName` and reference to a secret named `<domain>-tls` (which in my case is generated by `certmanager`). This is combined with the `ingress-nginx` chart. AFAIU though you want to run Gitea itself with HTTPS directly rather than HTTP so the connection between the ingress controller and the service endpoint is also encrypted. I've done this a few times in some environments (but not for Gitea) and always found it quite a hassle. But it might/should be possible.
joshsizer commented 2024-06-12 17:06:47 +00:00 (Migrated from gitea.com)

Sorry for the hiatus, I am still interested in working on this. Hopefully will get a chance to work on this in the next few weeks

Sorry for the hiatus, I am still interested in working on this. Hopefully will get a chance to work on this in the next few weeks
This pull request is marked as a work in progress.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin joshsizer/add-tls-and-extra-ca-support:joshsizer/add-tls-and-extra-ca-support
git checkout joshsizer/add-tls-and-extra-ca-support
Sign in to join this conversation.
No description provided.