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

feat: secure-pod-defaults is enabled by default #14168

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions config/core/configmaps/features.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ metadata:
app.kubernetes.io/component: controller
app.kubernetes.io/version: devel
annotations:
knative.dev/example-checksum: "f2fc138e"
knative.dev/example-checksum: "3c7d91f6"
data:
_example: |-
################################
Expand All @@ -40,12 +40,11 @@ data:
# this example block and unindented to be in the data block
# to actually change the configuration.

# Default SecurityContext settings to secure-by-default values
# if unset.
# Indicates whether secure-pod-defaults support is enabled
#
# This value will default to "enabled" in a future release,
# probably Knative 1.10
secure-pod-defaults: "disabled"
# WARNING: Cannot safely be disabled once enabled.
# See: https://knative.dev/docs/serving/feature-flags/#secure-pod-defaults
secure-pod-defaults: "enabled"

# Indicates whether multi container support is enabled
#
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/config/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func defaultFeaturesConfig() *Features {
PodSpecInitContainers: Disabled,
PodSpecDNSPolicy: Disabled,
PodSpecDNSConfig: Disabled,
SecurePodDefaults: Disabled,
SecurePodDefaults: Enabled,
TagHeaderBasedRouting: Disabled,
AutoDetectHTTP2: Disabled,
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/apis/serving/fieldmask_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,11 @@ func TestPodSecurityContextMask(t *testing.T) {
},
}

want := &corev1.PodSecurityContext{}
want := &corev1.PodSecurityContext{
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
},
}
ctx := context.Background()

got := PodSecurityContextMask(ctx, in)
Expand Down
75 changes: 65 additions & 10 deletions pkg/apis/serving/k8s_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1265,16 +1265,6 @@ func TestPodSpecFeatureValidation(t *testing.T) {
Paths: []string{"runtimeClassName"},
},
cfgOpts: []configOption{withPodSpecRuntimeClassNameEnabled()},
}, {
name: "PodSpecSecurityContext",
featureSpec: corev1.PodSpec{
SecurityContext: &corev1.PodSecurityContext{},
},
err: &apis.FieldError{
Message: "must not set the field(s)",
Paths: []string{"securityContext"},
},
cfgOpts: []configOption{withPodSpecSecurityContextEnabled()},
}, {
name: "PriorityClassName",
featureSpec: corev1.PodSpec{
Expand Down Expand Up @@ -3015,3 +3005,68 @@ func TestPodSpecSecurityContextValidation(t *testing.T) {
})
}
}

func TestSecurityContextSecurePodDefaultsFeatureValidation(t *testing.T) {
tests := []struct {
name string
sc *corev1.PodSecurityContext
err *apis.FieldError
errLevel apis.DiagnosticLevel
cfgOpts *config.Config
}{{
name: "SecurePodDefaults off, PodSpecSecurityContext off",
sc: &corev1.PodSecurityContext{},
cfgOpts: &config.Config{
Features: &config.Features{
SecurePodDefaults: config.Disabled,
PodSpecSecurityContext: config.Disabled,
},
},
}, {
name: "SecurePodDefaults off, PodSpecSecurityContext on",
sc: &corev1.PodSecurityContext{
RunAsNonRoot: ptr.Bool(false),
},
cfgOpts: &config.Config{
Features: &config.Features{
SecurePodDefaults: config.Disabled,
PodSpecSecurityContext: config.Enabled,
},
},
}, {
name: "SecurePodDefaults on, PodSpecSecurityContext off",
sc: &corev1.PodSecurityContext{
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
},
},
cfgOpts: &config.Config{
Features: &config.Features{
SecurePodDefaults: config.Enabled,
PodSpecSecurityContext: config.Disabled,
},
},
}, {
name: "SecurePodDefaults on, PodSpecSecurityContext on",
sc: &corev1.PodSecurityContext{},
cfgOpts: &config.Config{
Features: &config.Features{
SecurePodDefaults: config.Enabled,
PodSpecSecurityContext: config.Enabled,
},
},
}}

for _, test := range tests {
ctx := config.ToContext(context.Background(), test.cfgOpts)

t.Run(test.name, func(t *testing.T) {
got := ValidatePodSecurityContext(ctx, test.sc)
got.Filter(test.errLevel)
if diff := cmp.Diff(test.err.Error(), got.Error()); diff != "" {
t.Errorf("ValidatePodSecurityContext(-want, +got): \n%s", diff)
}
})
}

}
30 changes: 30 additions & 0 deletions pkg/apis/serving/v1/configuration_defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ func TestConfigurationDefaulting(t *testing.T) {
Image: "busybox",
Resources: defaultResources,
ReadinessProbe: defaultProbe,
SecurityContext: &corev1.SecurityContext{
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{"ALL"},
},
RunAsNonRoot: ptr.Bool(true),
AllowPrivilegeEscalation: ptr.Bool(false),
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
},
},
}},
},
TimeoutSeconds: ptr.Int64(config.DefaultRevisionTimeoutSeconds),
Expand Down Expand Up @@ -111,6 +121,16 @@ func TestConfigurationDefaulting(t *testing.T) {
Image: "busybox",
Resources: defaultResources,
ReadinessProbe: defaultProbe,
SecurityContext: &corev1.SecurityContext{
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{"ALL"},
},
RunAsNonRoot: ptr.Bool(true),
AllowPrivilegeEscalation: ptr.Bool(false),
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
},
},
}},
},
TimeoutSeconds: ptr.Int64(config.DefaultRevisionTimeoutSeconds),
Expand Down Expand Up @@ -148,6 +168,16 @@ func TestConfigurationDefaulting(t *testing.T) {
Image: "busybox",
Resources: defaultResources,
ReadinessProbe: defaultProbe,
SecurityContext: &corev1.SecurityContext{
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{"ALL"},
},
RunAsNonRoot: ptr.Bool(true),
AllowPrivilegeEscalation: ptr.Bool(false),
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
},
},
}},
},
TimeoutSeconds: ptr.Int64(60),
Expand Down
Loading