[go: up one dir, main page]

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

Add default SecurityContext to every new ksvc #1821

Merged
merged 4 commits into from
Jul 13, 2023

Conversation

dsimansk
Copy link
Contributor

Description

This's handling a warning from API-server validation about SecurityContext being empty for kn service create and related cmds.

I'll look into a few further improvements. E.g. either adding SecurityContext builder pattern or at least plain --security-context seccon.yaml flag to make it customizable.

/cc @rhuss @vyasgun

Changes

  • Add default SecurityContext to every new ksvc

Reference

Fixes #

Release Note

Add default SecurityContext to every new ksvc

@knative-prow knative-prow bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jun 26, 2023
@knative-prow knative-prow bot requested review from rhuss and vyasgun June 26, 2023 11:30
Copy link
@knative-prow knative-prow bot left a comment

Choose a reason for hiding this comment

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

@dsimansk: 2 warnings.

In response to this:

Description

This's handling a warning from API-server validation about SecurityContext being empty for kn service create and related cmds.

I'll look into a few further improvements. E.g. either adding SecurityContext builder pattern or at least plain --security-context seccon.yaml flag to make it customizable.

/cc @rhuss @vyasgun

Changes

  • Add default SecurityContext to every new ksvc

Reference

Fixes #

Release Note

Add default SecurityContext to every new ksvc

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@@ -378,6 +380,26 @@ func UpdateImagePullPolicy(spec *corev1.PodSpec, imagePullPolicy string) error {
return nil
}

func UpdateDefaultSecurityContext(spec *corev1.PodSpec) {
Copy link

Choose a reason for hiding this comment

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

Golint comments: exported function UpdateDefaultSecurityContext should have comment or be unexported. More info.

}
}

func DefaultSecCon() *corev1.SecurityContext {
Copy link

Choose a reason for hiding this comment

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

Golint comments: exported function DefaultSecCon should have comment or be unexported. More info.

@knative-prow knative-prow bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jun 26, 2023
@codecov
Copy link
codecov bot commented Jun 26, 2023

Codecov Report

Patch coverage: 82.92% and project coverage change: +0.03 🎉

Comparison is base (00e38dc) 79.70% compared to head (009b2e7) 79.74%.

❗ Current head 009b2e7 differs from pull request most recent head c96dc6c. Consider uploading reports for the commit c96dc6c to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1821      +/-   ##
==========================================
+ Coverage   79.70%   79.74%   +0.03%     
==========================================
  Files         179      179              
  Lines       13850    13891      +41     
==========================================
+ Hits        11039    11077      +38     
- Misses       2050     2052       +2     
- Partials      761      762       +1     
Impacted Files Coverage Δ
pkg/kn/flags/podspec.go 74.33% <46.15%> (-1.28%) ⬇️
pkg/kn/flags/podspec_helper.go 85.53% <100.00%> (+1.03%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

func DefaultSecCon() *corev1.SecurityContext {
return &corev1.SecurityContext{
AllowPrivilegeEscalation: pointer.Bool(false),
RunAsNonRoot: pointer.Bool(true),
Copy link
Contributor

Choose a reason for hiding this comment

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

My question would be if this does not break the existing setup and whether this is not too restrictive. I mean, there are still tons of container images that can only run as root and/or need certain capabilities. Having such a strict default (that currently can't be changed), is quite drastic. I think we should only add this if there is at least an option to get back the previous behaviour, i.e. having no SecurityContext at all. This then could be documented as a change (which is still not backwards compatible). This option could be called --no-security-context or maybe even better something like --security-context=none (with the alternatives like --security-context=strict (the default) and --security-context=privilegeEscalation:allow,runAsNonRoot:true,dropCapabilities:NET for fine grained security context configuration (this we can add later but strict and none should be already along with this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, I was actually thinking about --no-security-context to be added immediately. I like strict & none combination, with the further enhancement to towards fine grained options.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's at least ready with on/off switch. 0fcf800

@rhuss
Copy link
Contributor
rhuss commented Jun 26, 2023

Please see my comment https://github.com/knative/client/pull/1821/files#r1242163943 that we should already add basic option handling here. Otherwise, I think it would break too much.

Also note the linter warnings.

@knative-prow knative-prow bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jun 27, 2023
@dsimansk
Copy link
Contributor Author

/retest

Copy link
Contributor
@rhuss rhuss left a comment

Choose a reason for hiding this comment

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

Looks good to me, with some minor nits.

@@ -234,6 +236,10 @@ func (p *PodSpecFlags) AddFlags(flagset *pflag.FlagSet) []string {
flagNames = append(flagNames, "pull-secret")
flagset.Int64VarP(&p.User, "user", "", 0, "The user ID to run the container (e.g., 1001).")
flagNames = append(flagNames, "user")

flagset.StringVar(&p.SecurityContext, "security-context", "strict", "Security Context definition to be added the service. Accepted values: strict | none.")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
flagset.StringVar(&p.SecurityContext, "security-context", "strict", "Security Context definition to be added the service. Accepted values: strict | none.")
flagset.StringVar(&p.SecurityContext, "security-context", "strict", "Predefined security context for the service. Accepted values: 'none' for no security context and 'strict' for dropping all capabilities, running as non-root, and no privilege escalation.")

// Add or update Security Context to default strict
container.SecurityContext = DefaultStrictSecCon()
case "":
// Add default strict SC flag is not used, hence empty value
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this be the case if a default value is set to "strict" ?

@knative-prow knative-prow bot added the lgtm Indicates that a PR is ready to be merged. label Jul 13, 2023
@knative-prow
Copy link
knative-prow bot commented Jul 13, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: dsimansk, rhuss

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow knative-prow bot merged commit 75d62a3 into knative:main Jul 13, 2023
@dsimansk dsimansk linked an issue Jul 26, 2023 that may be closed by this pull request
@dsimansk dsimansk added the kind/feature New feature or request label Jul 26, 2023
@dsimansk dsimansk added this to the Knative 1.11 milestone Aug 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. kind/feature New feature or request lgtm Indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature suggestions for offline mode (kn service --target)
2 participants