-
Notifications
You must be signed in to change notification settings - Fork 131
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
Run as non-privilege and non-root user #2037
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright (c) 2022 Tigera, Inc. All rights reserved. | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package render | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
batchv1 "k8s.io/api/batch/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
|
||
operatorv1 "github.com/tigera/operator/api/v1" | ||
rtest "github.com/tigera/operator/pkg/render/common/test" | ||
) | ||
|
||
var _ = Describe("AWS SecurityGroup Setup rendering tests", func() { | ||
var cfg *AWSSGSetupConfiguration | ||
|
||
BeforeEach(func() { | ||
cfg = &AWSSGSetupConfiguration{ | ||
PullSecrets: []corev1.LocalObjectReference{}, | ||
Installation: &operatorv1.InstallationSpec{}, | ||
} | ||
}) | ||
|
||
It("should render AWS SecurityGroup Setup resources", func() { | ||
component, err := AWSSecurityGroupSetup(cfg) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(component.ResolveImages(nil)).NotTo(HaveOccurred()) | ||
|
||
toCreate, toDelete := component.Objects() | ||
|
||
expectedResources := []struct { | ||
name string | ||
ns string | ||
group string | ||
version string | ||
kind string | ||
}{ | ||
{"tigera-aws-security-group-setup", "tigera-operator", "", "v1", "ServiceAccount"}, | ||
{"tigera-aws-security-group-setup", "kube-system", "rbac.authorization.k8s.io", "v1", "Role"}, | ||
{"tigera-aws-security-group-setup", "kube-system", "rbac.authorization.k8s.io", "v1", "RoleBinding"}, | ||
{"aws-security-group-setup-1", "tigera-operator", "batch", "v1", "Job"}, | ||
} | ||
|
||
Expect(len(toCreate)).To(Equal(len(expectedResources))) | ||
|
||
for i, expectedRes := range expectedResources { | ||
obj := toCreate[i] | ||
rtest.ExpectResource(obj, expectedRes.name, expectedRes.ns, expectedRes.group, expectedRes.version, expectedRes.kind) | ||
} | ||
|
||
Expect(toDelete).To(BeNil()) | ||
}) | ||
|
||
It("should render Setup Job specs correctly", func() { | ||
component, err := AWSSecurityGroupSetup(cfg) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(component.ResolveImages(nil)).NotTo(HaveOccurred()) | ||
toCreate, _ := component.Objects() | ||
|
||
job, ok := rtest.GetResource(toCreate, "aws-security-group-setup-1", "tigera-operator", "batch", "v1", "Job").(*batchv1.Job) | ||
Expect(ok).To(BeTrue()) | ||
|
||
Expect(job.Spec.Template.Spec.Containers).To(HaveLen(1)) | ||
|
||
Expect(*job.Spec.Template.Spec.Containers[0].SecurityContext.AllowPrivilegeEscalation).To(BeFalse()) | ||
Expect(*job.Spec.Template.Spec.Containers[0].SecurityContext.Privileged).To(BeFalse()) | ||
Expect(*job.Spec.Template.Spec.Containers[0].SecurityContext.RunAsGroup).To(BeEquivalentTo(0)) | ||
Expect(*job.Spec.Template.Spec.Containers[0].SecurityContext.RunAsNonRoot).To(BeTrue()) | ||
Expect(*job.Spec.Template.Spec.Containers[0].SecurityContext.RunAsUser).To(BeEquivalentTo(1001)) | ||
}) | ||
}) |
15 changes: 0 additions & 15 deletions
15
pkg/render/common/podsecuritycontext/pod_security_context.go
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) 2021-2022 Tigera, Inc. All rights reserved. | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package securitycontext | ||
|
||
import ( | ||
corev1 "k8s.io/api/core/v1" | ||
|
||
"github.com/tigera/operator/pkg/ptr" | ||
) | ||
|
||
var ( | ||
// It is recommended to choose UID and GID that don't collide with existing system users and groups. | ||
// Non-system UID and GID range is normally from 1000 to 60000 (Debian derived systems define this | ||
// in /etc/login.defs). On a normal Linux host, it is unlikely to have more than 10k non-system users. | ||
// 10001 is chosen based on this assumption. | ||
RunAsUserID int64 = 10001 | ||
RunAsGroupID int64 = 10001 | ||
) | ||
|
||
// NewBaseContext returns the non root non privileged security context that most of the containers running should | ||
// be using. | ||
func NewBaseContext(uid, gid int64) *corev1.SecurityContext { | ||
return &corev1.SecurityContext{ | ||
AllowPrivilegeEscalation: ptr.BoolToPtr(false), | ||
Privileged: ptr.BoolToPtr(false), | ||
RunAsGroup: &gid, | ||
RunAsNonRoot: ptr.BoolToPtr(true), | ||
RunAsUser: &uid, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,11 +34,13 @@ import ( | |
operatorv1 "github.com/tigera/operator/api/v1" | ||
"github.com/tigera/operator/pkg/components" | ||
"github.com/tigera/operator/pkg/dns" | ||
"github.com/tigera/operator/pkg/ptr" | ||
relasticsearch "github.com/tigera/operator/pkg/render/common/elasticsearch" | ||
rkibana "github.com/tigera/operator/pkg/render/common/kibana" | ||
rmeta "github.com/tigera/operator/pkg/render/common/meta" | ||
"github.com/tigera/operator/pkg/render/common/podsecuritypolicy" | ||
"github.com/tigera/operator/pkg/render/common/secret" | ||
"github.com/tigera/operator/pkg/render/common/securitycontext" | ||
"github.com/tigera/operator/pkg/tls/certificatemanagement" | ||
"github.com/tigera/operator/pkg/url" | ||
) | ||
|
@@ -543,7 +545,7 @@ func (c *intrusionDetectionComponent) intrusionDetectionControllerContainer() co | |
}, | ||
} | ||
|
||
privileged := false | ||
sc := securitycontext.NewBaseContext(securitycontext.RunAsUserID, securitycontext.RunAsGroupID) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nothing is set in IDS controller |
||
|
||
// If syslog forwarding is enabled then set the necessary ENV var and volume mount to | ||
// write logs for Fluentd. | ||
|
@@ -558,7 +560,7 @@ func (c *intrusionDetectionComponent) intrusionDetectionControllerContainer() co | |
// On OpenShift, if we need the volume mount to hostpath volume for syslog forwarding, | ||
// then ID controller needs privileged access to write event logs to that volume | ||
if c.cfg.Openshift { | ||
privileged = true | ||
sc.Privileged = ptr.BoolToPtr(true) | ||
} | ||
} | ||
|
||
|
@@ -578,10 +580,8 @@ func (c *intrusionDetectionComponent) intrusionDetectionControllerContainer() co | |
}, | ||
InitialDelaySeconds: 5, | ||
}, | ||
SecurityContext: &corev1.SecurityContext{ | ||
Privileged: &privileged, | ||
}, | ||
VolumeMounts: volumeMounts, | ||
SecurityContext: sc, | ||
VolumeMounts: volumeMounts, | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preferably, I want this UID and GID consistent across all components. It is not possible to make the Operator only change because some components
chown
folders to a hard-coded UID inDockerfile
. When running as a different user, I got permission errors. 10001 is chosen for the reason mentioned in the comments. The UIDs we are currently using (999 or 1001 etc.) may not be desired because they will collide with system or non-system users. Do we want to make them consistent? @caseydavenport or @tmjd WDYT?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consistency would be nice, but like you point out it would require some extra work. I don't think we have any special attachment to 999 or 1001, but there are probably other places where we (or users) make assumptions based on those, so I might be inclined to leave them as-is for now. I don't think we've had any reports of those values causing issues.
Maybe just make these configurable for now and each component can pass in the user / group that it expects to be run as, and we can then do a follow-up series of PRs to standardize?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made uid and gid configurable in
NewBaseContext
. The UID and GID are taken from Dockerfiles (unchanged) for each component.