Skip to content

Conversation

@osmman
Copy link
Collaborator

@osmman osmman commented Aug 28, 2025

Summary by Sourcery

Standardize security contexts for operator-managed pods by introducing a generic PodSecurityContext ensure function, removing legacy OpenShift-specific SCC logic, and applying consistent security defaults across Trillian, TUF, and Rekor workloads.

Enhancements:

  • Add ensure.PodSecurityContext to apply non-root, FSGroup, and seccomp defaults to any PodSpec
  • Remove GetOpenshiftPodSecurityContextRestricted and related OpenShift-specific imports and fallbacks
  • Apply the new PodSecurityContext mutator to Trillian DB deployments, TUF deployments and init jobs, and Rekor statefulsets and deployments

Tests:

  • Update TUF e2e test to assert PodSecurityContext is set on the init job

@sourcery-ai
Copy link

sourcery-ai bot commented Aug 28, 2025

Reviewer's Guide

This PR centralizes pod security context configuration by introducing a generic helper that enforces run-as-non-root, disables privilege escalation, applies a default FSGroup for non-OpenShift clusters, and sets the runtime-default seccomp profile; it removes the legacy OpenShift-specific logic and updates all operator-managed workloads (Trillian DB, TUF deployments and init jobs, Rekor server and statefulset) to use the new helper, as well as adjusts the e2e TUF test to verify the security context.

Sequence diagram for applying PodSecurityContext to operator-managed workloads

sequenceDiagram
    participant Controller
    participant Workload (e.g. Deployment/StatefulSet/Job)
    participant Ensure
    Controller->>Workload: Create or update workload
    Workload->>Ensure: Call PodSecurityContext helper
    Ensure-->>Workload: Apply security context (runAsNonRoot, no privilege escalation, FSGroup, seccomp)
    Workload-->>Controller: Workload created/updated with enforced security context
Loading

Class diagram for PodSecurityContext enforcement changes

classDiagram
    class Ensure {
        +PodSecurityContext(podSpec)
    }
    class Deployment {
        +PodSecurityContext()
    }
    Ensure <|-- Deployment
    Deployment : +PodSecurityContext() calls Ensure.PodSecurityContext
    class TrillianDbDeployment {
        -removed: GetOpenshiftPodSecurityContextRestricted
        +uses: Deployment.PodSecurityContext
    }
    class TufDeployment {
        +uses: Deployment.PodSecurityContext
    }
    class TufInitJob {
        +uses: Ensure.PodSecurityContext
    }
    class RekorServerDeployment {
        +uses: Deployment.PodSecurityContext
    }
    class RekorMonitorStatefulSet {
        +uses: Ensure.PodSecurityContext
    }
    TrillianDbDeployment --> Deployment
    TufDeployment --> Deployment
    TufInitJob --> Ensure
    RekorServerDeployment --> Deployment
    RekorMonitorStatefulSet --> Ensure
Loading

File-Level Changes

Change Details Files
Introduce a universal PodSecurityContext helper
  • Add PodSecurityContext function setting RunAsNonRoot, FSGroup, SeccompProfile, and container security defaults
  • Respect OpenShift by skipping FSGroup and RunAsUser defaults
  • Reuse helper across init containers and main containers
internal/utils/kubernetes/ensure/pod_spec.go
Remove legacy OpenShift-specific security context logic
  • Delete GetOpenshiftPodSecurityContextRestricted function and related imports
  • Eliminate fallback FSGroup code in common utilities
internal/utils/kubernetes/common.go
Apply generic PodSecurityContext to all operator resources
  • Replace calls to the removed OpenShift helper with deployment.PodSecurityContext
  • Inject PodSecurityContext for Trillian DB, TUF Deployment, TUF init job, Rekor server Deployment, and Rekor StatefulSet
internal/controller/trillian/actions/db/deployment.go
internal/controller/tuf/actions/deployment.go
internal/controller/tuf/actions/tuf_init_job.go
internal/controller/rekor/actions/server/deployment.go
internal/controller/rekor/actions/monitor/statefulset.go
Add wrapper in deployment ensure for PodSecurityContext
  • Introduce PodSecurityContext helper in ensure/deployment/deployment.go
internal/utils/kubernetes/ensure/deployment/deployment.go
Enhance e2e TUF test to assert security context
  • Add Expect call to verify PodSecurityContext on TUF init job spec
test/e2e/support/tas/tuf/tuf.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes - here's some feedback:

  • ensure.PodSecurityContext only applies defaults to spec.Containers—consider extending it to initContainers (and other container lists) so all pod containers get consistent security settings.
  • Hard-coding runAsUser/runAsGroup to 1001 might clash with some images—consider making these values configurable or deriving them from the pod spec rather than a fixed constant.
  • Review the choice to skip setting FSGroup on OpenShift clusters, as PVC-backed pods often need an explicit FSGroup on all platforms to mount volumes correctly.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- ensure.PodSecurityContext only applies defaults to spec.Containers—consider extending it to initContainers (and other container lists) so all pod containers get consistent security settings.
- Hard-coding runAsUser/runAsGroup to 1001 might clash with some images—consider making these values configurable or deriving them from the pod spec rather than a fixed constant.
- Review the choice to skip setting FSGroup on OpenShift clusters, as PVC-backed pods often need an explicit FSGroup on all platforms to mount volumes correctly.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@osmman osmman added the bug Something isn't working label Aug 28, 2025
@osmman osmman force-pushed the tturek/securitycontext branch 6 times, most recently from 7214ec5 to 33be352 Compare August 28, 2025 12:43
@osmman osmman marked this pull request as draft August 28, 2025 14:58
@osmman osmman force-pushed the tturek/securitycontext branch 3 times, most recently from ba49996 to 8dd4ae0 Compare August 28, 2025 19:54
@osmman osmman marked this pull request as ready for review August 28, 2025 19:55
sourcery-ai[bot]

This comment was marked as outdated.

sourcery-ai[bot]

This comment was marked as outdated.

@osmman osmman force-pushed the tturek/securitycontext branch 2 times, most recently from eea3bd3 to 52618e2 Compare August 29, 2025 09:00
Signed-off-by: Tomas Turek <tturek@redhat.com>
@osmman osmman force-pushed the tturek/securitycontext branch 2 times, most recently from 1994f8a to 25b0ce0 Compare August 29, 2025 09:18
@osmman osmman requested review from JasonPowr, bouskaJ and knrc August 29, 2025 10:20
@osmman osmman merged commit 01df2a0 into main Aug 29, 2025
19 checks passed
@osmman osmman deleted the tturek/securitycontext branch August 29, 2025 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants