Skip to content

Commit bc79d3b

Browse files
authored
Merge pull request kubernetes#128396 from ritazh/deprecate-EnforceMountableSecretsAnnotation
deprecate EnforceMountableSecretsAnnotation in 1.32
2 parents 4965a7a + e7cdc59 commit bc79d3b

File tree

14 files changed

+152
-10
lines changed

14 files changed

+152
-10
lines changed

api/openapi-spec/swagger.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/openapi-spec/v3/api__v1_openapi.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/core/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4822,6 +4822,8 @@ type ServiceAccount struct {
48224822

48234823
// Secrets is a list of the secrets in the same namespace that pods running using this ServiceAccount are allowed to use.
48244824
// Pods are only limited to this list if this service account has a "kubernetes.io/enforce-mountable-secrets" annotation set to "true".
4825+
// The "kubernetes.io/enforce-mountable-secrets" annotation is deprecated since v1.32.
4826+
// Prefer separate namespaces to isolate access to mounted secrets.
48254827
// This field should not be used to find auto-generated service account token secrets for use outside of pods.
48264828
// Instead, tokens can be requested directly using the TokenRequest API, or service account token secrets can be manually created.
48274829
Secrets []ObjectReference

pkg/generated/openapi/zz_generated.openapi.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/registry/core/serviceaccount/strategy.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ package serviceaccount
1818

1919
import (
2020
"context"
21+
"fmt"
2122

2223
"k8s.io/apimachinery/pkg/runtime"
2324
"k8s.io/apimachinery/pkg/util/validation/field"
2425
"k8s.io/apiserver/pkg/storage/names"
2526
"k8s.io/kubernetes/pkg/api/legacyscheme"
2627
api "k8s.io/kubernetes/pkg/apis/core"
2728
"k8s.io/kubernetes/pkg/apis/core/validation"
29+
sa "k8s.io/kubernetes/plugin/pkg/admission/serviceaccount"
2830
)
2931

3032
// strategy implements behavior for ServiceAccount objects
@@ -50,7 +52,9 @@ func (strategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorLis
5052
}
5153

5254
// WarningsOnCreate returns warnings for the creation of the given object.
53-
func (strategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string { return nil }
55+
func (strategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string {
56+
return warnIfHasEnforceMountableSecretsAnnotation(obj.(*api.ServiceAccount), nil)
57+
}
5458

5559
// Canonicalize normalizes the object after validation.
5660
func (strategy) Canonicalize(obj runtime.Object) {
@@ -76,9 +80,24 @@ func (strategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) fie
7680

7781
// WarningsOnUpdate returns warnings for the given update.
7882
func (strategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
79-
return nil
83+
return warnIfHasEnforceMountableSecretsAnnotation(obj.(*api.ServiceAccount), old.(*api.ServiceAccount))
8084
}
8185

8286
func (strategy) AllowUnconditionalUpdate() bool {
8387
return true
8488
}
89+
90+
func warnIfHasEnforceMountableSecretsAnnotation(serviceAccount, oldServiceAccount *api.ServiceAccount) []string {
91+
if oldServiceAccount != nil {
92+
_, ok := oldServiceAccount.Annotations[sa.EnforceMountableSecretsAnnotation]
93+
if ok {
94+
// skip warning if request isn't newly setting the annotation
95+
return nil
96+
}
97+
}
98+
_, ok := serviceAccount.Annotations[sa.EnforceMountableSecretsAnnotation]
99+
if ok {
100+
return []string{fmt.Sprintf("metadata.annotations[%s]: deprecated in v1.32+; prefer separate namespaces to isolate access to mounted secrets", sa.EnforceMountableSecretsAnnotation)}
101+
}
102+
return nil
103+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package serviceaccount
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"testing"
23+
24+
api "k8s.io/kubernetes/pkg/apis/core"
25+
sa "k8s.io/kubernetes/plugin/pkg/admission/serviceaccount"
26+
)
27+
28+
func TestWarningsOnCreate(t *testing.T) {
29+
ctx := context.TODO()
30+
serviceAccount := &api.ServiceAccount{}
31+
32+
warnings := Strategy.WarningsOnCreate(ctx, serviceAccount)
33+
if len(warnings) != 0 {
34+
t.Errorf("expected no warnings, got %v", warnings)
35+
}
36+
serviceAccount.Annotations = map[string]string{sa.EnforceMountableSecretsAnnotation: "true"}
37+
warnings = Strategy.WarningsOnCreate(ctx, serviceAccount)
38+
if len(warnings) != 1 || warnings[0] != fmt.Sprintf("metadata.annotations[%s]: deprecated in v1.32+; prefer separate namespaces to isolate access to mounted secrets", sa.EnforceMountableSecretsAnnotation) {
39+
t.Errorf("expected warnings, got %v", warnings)
40+
}
41+
}
42+
43+
func TestWarningsOnUpdate(t *testing.T) {
44+
ctx := context.TODO()
45+
serviceAccount := &api.ServiceAccount{}
46+
oldServiceAccount := &api.ServiceAccount{}
47+
48+
warnings := Strategy.WarningsOnUpdate(ctx, serviceAccount, oldServiceAccount)
49+
if len(warnings) != 0 {
50+
t.Errorf("expected no warnings, got %v", warnings)
51+
}
52+
serviceAccount.Annotations = map[string]string{sa.EnforceMountableSecretsAnnotation: "true"}
53+
warnings = Strategy.WarningsOnUpdate(ctx, serviceAccount, oldServiceAccount)
54+
if len(warnings) != 1 || warnings[0] != fmt.Sprintf("metadata.annotations[%s]: deprecated in v1.32+; prefer separate namespaces to isolate access to mounted secrets", sa.EnforceMountableSecretsAnnotation) {
55+
t.Errorf("expected warnings, got %v", warnings)
56+
}
57+
58+
oldServiceAccount.Annotations = map[string]string{sa.EnforceMountableSecretsAnnotation: "true"}
59+
warnings = Strategy.WarningsOnUpdate(ctx, serviceAccount, oldServiceAccount)
60+
if len(warnings) != 0 {
61+
t.Errorf("expected no warnings if request isn't newly setting the annotation, got %v", warnings)
62+
}
63+
}

staging/src/k8s.io/api/core/v1/generated.proto

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staging/src/k8s.io/api/core/v1/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5736,6 +5736,8 @@ type ServiceAccount struct {
57365736

57375737
// Secrets is a list of the secrets in the same namespace that pods running using this ServiceAccount are allowed to use.
57385738
// Pods are only limited to this list if this service account has a "kubernetes.io/enforce-mountable-secrets" annotation set to "true".
5739+
// The "kubernetes.io/enforce-mountable-secrets" annotation is deprecated since v1.32.
5740+
// Prefer separate namespaces to isolate access to mounted secrets.
57395741
// This field should not be used to find auto-generated service account token secrets for use outside of pods.
57405742
// Instead, tokens can be requested directly using the TokenRequest API, or service account token secrets can be manually created.
57415743
// More info: https://kubernetes.io/docs/concepts/configuration/secret

staging/src/k8s.io/api/core/v1/types_swagger_doc_generated.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staging/src/k8s.io/cli-runtime/artifacts/openapi/swagger-with-shared-parameters.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9630,7 +9630,7 @@
96309630
"description": "Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata"
96319631
},
96329632
"secrets": {
9633-
"description": "Secrets is a list of the secrets in the same namespace that pods running using this ServiceAccount are allowed to use. Pods are only limited to this list if this service account has a \"kubernetes.io/enforce-mountable-secrets\" annotation set to \"true\". This field should not be used to find auto-generated service account token secrets for use outside of pods. Instead, tokens can be requested directly using the TokenRequest API, or service account token secrets can be manually created. More info: https://kubernetes.io/docs/concepts/configuration/secret",
9633+
"description": "Secrets is a list of the secrets in the same namespace that pods running using this ServiceAccount are allowed to use. Pods are only limited to this list if this service account has a \"kubernetes.io/enforce-mountable-secrets\" annotation set to \"true\". The \"kubernetes.io/enforce-mountable-secrets\" annotation is deprecated since v1.32. Prefer separate namespaces to isolate access to mounted secrets. This field should not be used to find auto-generated service account token secrets for use outside of pods. Instead, tokens can be requested directly using the TokenRequest API, or service account token secrets can be manually created. More info: https://kubernetes.io/docs/concepts/configuration/secret",
96349634
"items": {
96359635
"$ref": "#/definitions/io.k8s.api.core.v1.ObjectReference"
96369636
},

0 commit comments

Comments
 (0)