Skip to content

feat: support defining security contexts for pods and containers#6614

Merged
gbartolini merged 15 commits intocloudnative-pg:mainfrom
x0ddf:feature/security-context
Nov 6, 2025
Merged

feat: support defining security contexts for pods and containers#6614
gbartolini merged 15 commits intocloudnative-pg:mainfrom
x0ddf:feature/security-context

Conversation

@x0ddf
Copy link
Contributor

@x0ddf x0ddf commented Jan 16, 2025

This commit introduces the ability to configure securityContext settings at both the pod and container level, enhancing control over runtime security policies and permissions.

Closes #2821

@x0ddf x0ddf requested a review from a team as a code owner January 16, 2025 12:24
@cnpg-bot cnpg-bot added backport-requested ◀️ This pull request should be backported to all supported releases release-1.22 release-1.24 release-1.25 labels Jan 16, 2025
@github-actions
Copy link
Contributor

❗ By default, the pull request is configured to backport to all release branches.

  • To stop backporting this pr, remove the label: backport-requested ◀️ or add the label 'do not backport'
  • To stop backporting this pr to a certain release branch, remove the specific branch label: release-x.y

@x0ddf x0ddf force-pushed the feature/security-context branch from 91460eb to cb3dd66 Compare January 16, 2025 19:51
@x0ddf x0ddf changed the title Add security context for pods and containers feat: add security context for pods and containers Jan 22, 2025
@sxd
Copy link
Member

sxd commented Jan 22, 2025

/test

@github-actions
Copy link
Contributor

@sxd, here's the link to the E2E on CNPG workflow run: https://github.com/cloudnative-pg/cloudnative-pg/actions/runs/12918936177

@cnpg-bot cnpg-bot added the ok to merge 👌 This PR can be merged label Jan 23, 2025
@x0ddf
Copy link
Contributor Author

x0ddf commented Jan 28, 2025

@sxd could you please review and merge this one?

@x0ddf x0ddf force-pushed the feature/security-context branch 2 times, most recently from 0d8270a to d7bb773 Compare January 30, 2025 06:34
@npgretz
Copy link

npgretz commented Jan 30, 2025

Thank you so much for this PR! I need this feature to be allowed to deploy CloudNative-PG at my org.

@cccsss01
Copy link

Very nice! Can we get this added?

@x0ddf x0ddf force-pushed the feature/security-context branch from d7bb773 to 3963998 Compare January 31, 2025 05:38
@sxd
Copy link
Member

sxd commented Jan 31, 2025

@x0ddf hi!

I haven't had time to look into this with full time but the first things that comes to my eyes is, where is the documentation and explanation for the usage of this and also all the risks that can come from a bad configuration of a security context? Something like that should explain what kind of things we can expect if something is badly configured, for example, PostgreSQL not starting because the security context define a different user from the one on the image, or that was defined previously ergo you'll see permissions issue.

Also, you're touching the pgbouncer one, that should be handled using the PodTemplate not using the options from the cluster.

On the proposal we allow to change the Security Context even on running pods, should we allow that?

Just to finish a quick review, please, this is lacking some test and E2E tests.

Regards,

@x0ddf x0ddf force-pushed the feature/security-context branch from 3963998 to e3611c7 Compare February 3, 2025 05:04
@x0ddf
Copy link
Contributor Author

x0ddf commented Feb 3, 2025

@sxd hi!

I haven't had time to look into this with full time but the first things that comes to my eyes is, where is the documentation and explanation for the usage of this and also all the risks that can come from a bad configuration of a security context? Something like that should explain what kind of things we can expect if something is badly configured, for example, PostgreSQL not starting because the security context define a different user from the one on the image, or that was defined previously ergo you'll see permissions issue.

I do agree with the statement that misconfigured SecurityContext(SC) is a risk. Still, the lack of opportunity to configure it is a risk also and could be a blocker to adopting it in some environments. For example, in the OpenShift/OKD environments, UID/GID could be randomized, and now you have to do tricks with AdmissionControllers/Kyverno and Cluster manifest simultaneously(just to set SC for the cnpg pods)! Also, at the moment, spec.postgres[UID, GID] is not propagated to the SC level, which is a big problem for me.

Also, you're touching the pgbouncer one, that should be handled using the PodTemplate not using the options from the cluster.

If you look at my change around the pgbouncer, you'll see that I'm not doing anything new (the seccomp profile was consumed from the Cluster spec before my changes).
After a deeper code evaluation, you'll find these two helpers used to create the deployment for pooler/pgbouncer:

It's already working as you want it to: if SC is defined in the PodTemplate, it will be utilized; otherwise, it will use the cnpg defaults.

@x0ddf x0ddf force-pushed the feature/security-context branch from e3611c7 to 265070f Compare February 5, 2025 08:23
@x0ddf x0ddf force-pushed the feature/security-context branch 2 times, most recently from 972300c to 5e5a98d Compare March 9, 2025 10:12
@x0ddf x0ddf force-pushed the feature/security-context branch from 5e5a98d to 9579476 Compare March 21, 2025 08:23
@x0ddf x0ddf force-pushed the feature/security-context branch from 9579476 to 9f9daa8 Compare April 23, 2025 05:31
@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Apr 23, 2025
@x0ddf x0ddf force-pushed the feature/security-context branch from 9f9daa8 to aa46fb0 Compare June 13, 2025 05:16
@sxd sxd force-pushed the feature/security-context branch from aa46fb0 to b3d585a Compare June 19, 2025 09:02
@mnencia mnencia force-pushed the feature/security-context branch from dc59ede to f67344f Compare November 4, 2025 10:51
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Nov 4, 2025
@mnencia mnencia force-pushed the feature/security-context branch from 7eaccfe to b055f89 Compare November 4, 2025 14:33
@mnencia
Copy link
Member

mnencia commented Nov 4, 2025

/test

@github-actions
Copy link
Contributor

github-actions bot commented Nov 4, 2025

@mnencia, here's the link to the E2E on CNPG workflow run: https://github.com/cloudnative-pg/cloudnative-pg/actions/runs/19075418581

@mnencia mnencia removed the backport-requested ◀️ This pull request should be backported to all supported releases label Nov 4, 2025
@mnencia mnencia force-pushed the feature/security-context branch from 187ea26 to 8140f85 Compare November 5, 2025 12:54
x0ddf and others added 15 commits November 6, 2025 15:36
- improve SecurityContext for containers

Signed-off-by: Sergey Yakovlev <noctus@proton.me>
Signed-off-by: Sergey Yakovlev <noctus@proton.me>
Signed-off-by: Armando Ruocco <armando.ruocco@enterprisedb.com>
Signed-off-by: Marco Nenciarini <marco.nenciarini@enterprisedb.com>
Signed-off-by: Marco Nenciarini <marco.nenciarini@enterprisedb.com>
Signed-off-by: Marco Nenciarini <marco.nenciarini@enterprisedb.com>
Signed-off-by: Marco Nenciarini <marco.nenciarini@enterprisedb.com>
Signed-off-by: Marco Nenciarini <marco.nenciarini@enterprisedb.com>
Signed-off-by: Marco Nenciarini <marco.nenciarini@enterprisedb.com>
Signed-off-by: Marco Nenciarini <marco.nenciarini@enterprisedb.com>
Signed-off-by: Armando Ruocco <armando.ruocco@enterprisedb.com>
Signed-off-by: Armando Ruocco <armando.ruocco@enterprisedb.com>
Signed-off-by: Armando Ruocco <armando.ruocco@enterprisedb.com>
Signed-off-by: Marco Nenciarini <marco.nenciarini@enterprisedb.com>
Signed-off-by: Gabriele Bartolini <gabriele.bartolini@enterprisedb.com>
@gbartolini gbartolini force-pushed the feature/security-context branch from 8095645 to 6e32f36 Compare November 6, 2025 14:36
@gbartolini gbartolini changed the title feat: add security context for pods and containers feat: support defining security contexts for pods and containers Nov 6, 2025
@gbartolini gbartolini merged commit 2928bdd into cloudnative-pg:main Nov 6, 2025
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer ok to merge 👌 This PR can be merged size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: add support for securityContext in postgres containers

8 participants