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

[feature] Add more default securityContext #712

Merged
merged 4 commits into from Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion controllers/factory/builders.go
Expand Up @@ -528,13 +528,21 @@ func addStrictSecuritySettingsToPod(p *v1.PodSecurityContext, enableStrictSecuri
if !enableStrictSecurity || p != nil {
return p
}
return &v1.PodSecurityContext{
securityContext := v1.PodSecurityContext{
RunAsNonRoot: pointer.Bool(true),
// '65534' refers to 'nobody' in all the used default images like alpine, busybox
RunAsUser: pointer.Int64(65534),
RunAsGroup: pointer.Int64(65534),
FSGroup: pointer.Int64(65534),
SeccompProfile: &v1.SeccompProfile{
Type: v1.SeccompProfileTypeRuntimeDefault,
},
}
if k8stools.IsFSGroupChangePolicySupported() {
onRootMismatch := v1.FSGroupChangeOnRootMismatch
securityContext.FSGroupChangePolicy = &onRootMismatch
}
return &securityContext
}

func addStrictSecuritySettingsToContainers(containers []v1.Container, enableStrictSecurity bool) []v1.Container {
Expand All @@ -547,6 +555,11 @@ func addStrictSecuritySettingsToContainers(containers []v1.Container, enableStri
container.SecurityContext = &v1.SecurityContext{
ReadOnlyRootFilesystem: pointer.Bool(true),
AllowPrivilegeEscalation: pointer.Bool(false),
Capabilities: &v1.Capabilities{
Drop: []v1.Capability{
"ALL",
},
},
}
}
}
Expand Down
13 changes: 9 additions & 4 deletions controllers/factory/builders_test.go
Expand Up @@ -14,6 +14,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/version"
"k8s.io/utils/pointer"
)

Expand Down Expand Up @@ -638,6 +639,7 @@ func TestAddStrictSecuritySettingsToPod(t *testing.T) {
podSecurityPolicy *v1.PodSecurityContext
enableStrictSecurity bool
exp *v1.PodSecurityContext
kubeletVersion version.Info
}
tests := []struct {
name string
Expand All @@ -649,11 +651,13 @@ func TestAddStrictSecuritySettingsToPod(t *testing.T) {
args: args{
enableStrictSecurity: true,
exp: &v1.PodSecurityContext{
RunAsNonRoot: pointer.Bool(true),
RunAsUser: pointer.Int64(65534),
RunAsGroup: pointer.Int64(65534),
FSGroup: pointer.Int64(65534),
RunAsNonRoot: pointer.Bool(true),
RunAsUser: pointer.Int64(65534),
RunAsGroup: pointer.Int64(65534),
FSGroup: pointer.Int64(65534),
FSGroupChangePolicy: (*v1.PodFSGroupChangePolicy)(pointer.StringPtr("OnRootMismatch")),
},
kubeletVersion: version.Info{Major: "1", Minor: "27"},
},
},
{
Expand All @@ -677,6 +681,7 @@ func TestAddStrictSecuritySettingsToPod(t *testing.T) {
},
}
for _, tt := range tests {
k8stools.SetKubernetesVersionWithDefaults(&tt.args.kubeletVersion, 0, 0)
res := addStrictSecuritySettingsToPod(tt.args.podSecurityPolicy, tt.args.enableStrictSecurity)
if diff := deep.Equal(res, tt.args.exp); len(diff) > 0 {
t.Fatalf("got unexpected result: %v, expect: %v", res, tt.args.exp)
Expand Down
10 changes: 10 additions & 0 deletions controllers/factory/k8stools/version.go
Expand Up @@ -69,6 +69,16 @@ func IsHPAV2BetaSupported() bool {
return false
}

// IsFSGroupChangePolicySupported checks if `fsGroupChangePolicy` is supported,
// Supported since 1.20
// https://kubernetes.io/blog/2020/12/14/kubernetes-release-1.20-fsgroupchangepolicy-fsgrouppolicy/#allow-users-to-skip-recursive-permission-changes-on-mount
func IsFSGroupChangePolicySupported() bool {
if ServerMajorVersion == 1 && ServerMinorVersion >= 20 {
return true
}
return false
}

// NewHPAEmptyObject returns HorizontalPodAutoscaler object for given kubernetes version
func NewHPAEmptyObject(opts ...func(obj client.Object)) client.Object {
var hpa client.Object = &v2beta2.HorizontalPodAutoscaler{}
Expand Down
3 changes: 2 additions & 1 deletion docs/CHANGELOG.MD
Expand Up @@ -34,7 +34,8 @@
- [vmcluster](https://docs.victoriametrics.com/operator/api.html#vmagent): add [example config](https://github.com/VictoriaMetrics/operator/blob/master/config/examples/vmcluster_with_additional_claim.yaml) for cluster with custom storage claims.
- [vmrule](https://docs.victoriametrics.com/operator/api.html#vmrule): support `update_entries_limit` field in rules, refer to [alerting rules](https://docs.victoriametrics.com/vmalert.html#alerting-rules). See [this PR](https://github.com/VictoriaMetrics/operator/pull/691) for details.
- [vmrule](https://docs.victoriametrics.com/operator/api.html#vmrule): support `keep_firing_for` field in rules, refer to [alerting rules](https://docs.victoriametrics.com/vmalert.html#alerting-rules). See [this PR](https://github.com/VictoriaMetrics/operator/pull/711) for details.
- [vmoperator parameters](https://docs.victoriametrics.com/operator/vars.html): Add option `VM_ENABLESTRICTSECURITY` and enable strict security context by default. See [this issue](https://github.com/VictoriaMetrics/operator/issues/637) and [this PR](https://github.com/VictoriaMetrics/operator/pull/692/) for details.
- [vmoperator parameters](https://docs.victoriametrics.com/operator/vars.html): Add option `VM_ENABLESTRICTSECURITY` and enable strict security context by default. See [this issue](https://github.com/VictoriaMetrics/operator/issues/637), [this](https://github.com/VictoriaMetrics/operator/pull/692/) and [this](https://github.com/VictoriaMetrics/operator/pull/712) PR for details.


<a name="v0.35.1"></a>
## [v0.35.1](https://github.com/VictoriaMetrics/operator/releases/tag/v0.35.1) - 12 Jul 2023
Expand Down
11 changes: 11 additions & 0 deletions internal/config/config.go
Expand Up @@ -254,9 +254,20 @@ type BaseOperatorConf struct {
// 2. RunAsUser/RunAsGroup/FSGroup: 65534
// '65534' refers to 'nobody' in all the used default images like alpine, busybox.
// If you're using customize image, please make sure '65534' is a valid uid in there or specify SecurityContext.
// 3. FSGroupChangePolicy: &onRootMismatch
// If KubeVersion>=1.20, use `FSGroupChangePolicy="onRootMismatch"` to skip the recursive permission change
// when the root of the volume already has the correct permissions
// 4. SeccompProfile:
// type: RuntimeDefault
// Use `RuntimeDefault` seccomp profile by default, which is defined by the container runtime,
// instead of using the Unconfined (seccomp disabled) mode.
//
// Default container SecurityContext include:
// 1. AllowPrivilegeEscalation: false
// 2. ReadOnlyRootFilesystem: true
// 3. Capabilities:
// drop:
// - all
EnableStrictSecurity bool `default:"true"`
}

Expand Down