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

Expand run-as-non-root template to verify runAsGroup field is nonzero. #804

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

Conversation

ariyonaty
Copy link

@ariyonaty ariyonaty commented Jul 8, 2024

Closes #748

This PR introduces changes which expand the run-as-non-root template/check to verify runAsGroup field is set to a non-zero value.

The runAsGroup field specifies the group ID under which the container’s process should run.
If left unspecified, the GID defaults to 0.

Snippet of build in action where pod.yaml for first execution initially didn't set runAsGroup, second execution set the field to 0, and finally set to a non-zero value:
image

@ariyonaty ariyonaty requested a review from janisz as a code owner July 8, 2024 13:35
Copy link

codecov bot commented Jul 8, 2024

Codecov Report

Attention: Patch coverage is 52.38095% with 10 lines in your changes missing coverage. Please review.

Project coverage is 62.34%. Comparing base (dbd7529) to head (6331670).
Report is 29 commits behind head on main.

Files Patch % Lines
pkg/templates/runasnonroot/template.go 52.38% 10 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #804      +/-   ##
==========================================
- Coverage   62.36%   62.34%   -0.02%     
==========================================
  Files         197      197              
  Lines        4854     4871      +17     
==========================================
+ Hits         3027     3037      +10     
- Misses       1439     1447       +8     
+ Partials      388      387       -1     
Flag Coverage Δ
unit 62.34% <52.38%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@janisz janisz left a comment

Choose a reason for hiding this comment

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

could you add unit tests for new code?

if runAsUser != nil && *runAsUser > 0 {
runAsGroup := effectiveRunAsGroup(podSpec.SecurityContext, container.SecurityContext)
// runAsUser and runAsGroup explicitly set to non-root. All good.
if (runAsUser != nil && *runAsUser > 0) && (runAsGroup != nil && *runAsGroup > 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about extracting a function to check for non zero int pointer?

func isNonZero(number *int64) bool {
	return number != nil && *number > 0
}

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

Successfully merging this pull request may close these issues.

[FEATURE_REQUEST] Expand run-as-non-root template to verify runAsGroup field is nonzero.
2 participants