Skip to content

Commit fa8e37f

Browse files
authored
Allow disabling caching for webhook authorizers when using apiserver.config.k8s.io/v1{alpha1,beta1}.AuthorizationConfiguration (kubernetes#129237)
* Introduce new boolean `cache{Una,A}uthorizedRequests` field * Run `hack/update-codegen.sh` * Respect legacy flags values for caching With the legacy `--authorization-webhook-cache-{un}authorized-ttl` flags, caching was disabled when the TTL was set to `0`, so let's continue doing so when building the authz configuration struct. * Pass TTL=0 to webhook authz plugin when cache disabled
1 parent 04b1ef3 commit fa8e37f

File tree

16 files changed

+279
-23
lines changed

16 files changed

+279
-23
lines changed

pkg/kubeapiserver/authorizer/reload.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,18 @@ func (r *reloadableAuthorizerResolver) newForConfig(authzConfig *authzconfig.Aut
141141
default:
142142
return nil, nil, fmt.Errorf("unknown failurePolicy %q", configuredAuthorizer.Webhook.FailurePolicy)
143143
}
144+
145+
authorizedTTL, unauthorizedTTL := configuredAuthorizer.Webhook.AuthorizedTTL.Duration, configuredAuthorizer.Webhook.UnauthorizedTTL.Duration
146+
if !configuredAuthorizer.Webhook.CacheAuthorizedRequests {
147+
authorizedTTL = 0
148+
}
149+
if !configuredAuthorizer.Webhook.CacheUnauthorizedRequests {
150+
unauthorizedTTL = 0
151+
}
144152
webhookAuthorizer, err := webhook.New(clientConfig,
145153
configuredAuthorizer.Webhook.SubjectAccessReviewVersion,
146-
configuredAuthorizer.Webhook.AuthorizedTTL.Duration,
147-
configuredAuthorizer.Webhook.UnauthorizedTTL.Duration,
154+
authorizedTTL,
155+
unauthorizedTTL,
148156
*r.initialConfig.WebhookRetryBackoff,
149157
decisionOnError,
150158
configuredAuthorizer.Webhook.MatchConditions,

pkg/kubeapiserver/options/authorization.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
authzconfig "k8s.io/apiserver/pkg/apis/apiserver"
3434
genericoptions "k8s.io/apiserver/pkg/server/options"
3535
versionedinformers "k8s.io/client-go/informers"
36-
3736
"k8s.io/kubernetes/pkg/kubeapiserver/authorizer"
3837
authzmodes "k8s.io/kubernetes/pkg/kubeapiserver/authorizer/modes"
3938
)
@@ -273,8 +272,10 @@ func (o *BuiltInAuthorizationOptions) buildAuthorizationConfiguration() (*authzc
273272
Type: authzconfig.TypeWebhook,
274273
Name: defaultWebhookName,
275274
Webhook: &authzconfig.WebhookConfiguration{
276-
AuthorizedTTL: metav1.Duration{Duration: o.WebhookCacheAuthorizedTTL},
277-
UnauthorizedTTL: metav1.Duration{Duration: o.WebhookCacheUnauthorizedTTL},
275+
AuthorizedTTL: metav1.Duration{Duration: o.WebhookCacheAuthorizedTTL},
276+
CacheAuthorizedRequests: o.WebhookCacheAuthorizedTTL != 0,
277+
UnauthorizedTTL: metav1.Duration{Duration: o.WebhookCacheUnauthorizedTTL},
278+
CacheUnauthorizedRequests: o.WebhookCacheUnauthorizedTTL != 0,
278279
// Timeout and FailurePolicy are required for the new configuration.
279280
// Setting these two implicitly to preserve backward compatibility.
280281
Timeout: metav1.Duration{Duration: 30 * time.Second},

staging/src/k8s.io/apiserver/pkg/apis/apiserver/load/load_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,10 @@ func TestLoadFromData(t *testing.T) {
213213
Type: "Webhook",
214214
Name: "default",
215215
Webhook: &api.WebhookConfiguration{
216-
AuthorizedTTL: metav1.Duration{Duration: 5 * time.Minute},
217-
UnauthorizedTTL: metav1.Duration{Duration: 30 * time.Second},
216+
AuthorizedTTL: metav1.Duration{Duration: 5 * time.Minute},
217+
CacheAuthorizedRequests: true,
218+
UnauthorizedTTL: metav1.Duration{Duration: 30 * time.Second},
219+
CacheUnauthorizedRequests: true,
218220
},
219221
}},
220222
},
@@ -252,8 +254,10 @@ authorizers:
252254
Type: "Webhook",
253255
Name: "default",
254256
Webhook: &api.WebhookConfiguration{
255-
AuthorizedTTL: metav1.Duration{Duration: 5 * time.Minute},
256-
UnauthorizedTTL: metav1.Duration{Duration: 30 * time.Second},
257+
AuthorizedTTL: metav1.Duration{Duration: 5 * time.Minute},
258+
CacheAuthorizedRequests: true,
259+
UnauthorizedTTL: metav1.Duration{Duration: 30 * time.Second},
260+
CacheUnauthorizedRequests: true,
257261
},
258262
}},
259263
},
@@ -291,8 +295,10 @@ authorizers:
291295
Type: "Webhook",
292296
Name: "default",
293297
Webhook: &api.WebhookConfiguration{
294-
AuthorizedTTL: metav1.Duration{Duration: 5 * time.Minute},
295-
UnauthorizedTTL: metav1.Duration{Duration: 30 * time.Second},
298+
AuthorizedTTL: metav1.Duration{Duration: 5 * time.Minute},
299+
CacheAuthorizedRequests: true,
300+
UnauthorizedTTL: metav1.Duration{Duration: 30 * time.Second},
301+
CacheUnauthorizedRequests: true,
296302
},
297303
}},
298304
},

staging/src/k8s.io/apiserver/pkg/apis/apiserver/types.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,21 @@ type WebhookConfiguration struct {
334334
// Same as setting `--authorization-webhook-cache-authorized-ttl` flag
335335
// Default: 5m0s
336336
AuthorizedTTL metav1.Duration
337+
// CacheAuthorizedRequests specifies whether authorized requests should be cached.
338+
// If set to true, the TTL for cached decisions can be configured via the
339+
// AuthorizedTTL field.
340+
// Default: true
341+
CacheAuthorizedRequests bool
337342
// The duration to cache 'unauthorized' responses from the webhook
338343
// authorizer.
339344
// Same as setting `--authorization-webhook-cache-unauthorized-ttl` flag
340345
// Default: 30s
341346
UnauthorizedTTL metav1.Duration
347+
// CacheUnauthorizedRequests specifies whether unauthorized requests should be cached.
348+
// If set to true, the TTL for cached decisions can be configured via the
349+
// UnauthorizedTTL field.
350+
// Default: true
351+
CacheUnauthorizedRequests bool
342352
// Timeout for the webhook request
343353
// Maximum allowed value is 30s.
344354
// Required, no default value.

staging/src/k8s.io/apiserver/pkg/apis/apiserver/v1/defaults.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2323
"k8s.io/apimachinery/pkg/runtime"
24+
"k8s.io/utils/ptr"
2425
)
2526

2627
var (
@@ -53,7 +54,13 @@ func SetDefaults_WebhookConfiguration(obj *WebhookConfiguration) {
5354
if obj.AuthorizedTTL.Duration == 0 {
5455
obj.AuthorizedTTL.Duration = 5 * time.Minute
5556
}
57+
if obj.CacheAuthorizedRequests == nil {
58+
obj.CacheAuthorizedRequests = ptr.To(true)
59+
}
5660
if obj.UnauthorizedTTL.Duration == 0 {
5761
obj.UnauthorizedTTL.Duration = 30 * time.Second
5862
}
63+
if obj.CacheUnauthorizedRequests == nil {
64+
obj.CacheUnauthorizedRequests = ptr.To(true)
65+
}
5966
}

staging/src/k8s.io/apiserver/pkg/apis/apiserver/v1/types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,23 @@ type WebhookConfiguration struct {
9797
// Same as setting `--authorization-webhook-cache-authorized-ttl` flag
9898
// Default: 5m0s
9999
AuthorizedTTL metav1.Duration `json:"authorizedTTL"`
100+
// CacheAuthorizedRequests specifies whether authorized requests should be cached.
101+
// If set to true, the TTL for cached decisions can be configured via the
102+
// AuthorizedTTL field.
103+
// Default: true
104+
// +optional
105+
CacheAuthorizedRequests *bool `json:"cacheAuthorizedRequests,omitempty"`
100106
// The duration to cache 'unauthorized' responses from the webhook
101107
// authorizer.
102108
// Same as setting `--authorization-webhook-cache-unauthorized-ttl` flag
103109
// Default: 30s
104110
UnauthorizedTTL metav1.Duration `json:"unauthorizedTTL"`
111+
// CacheUnauthorizedRequests specifies whether unauthorized requests should be cached.
112+
// If set to true, the TTL for cached decisions can be configured via the
113+
// UnauthorizedTTL field.
114+
// Default: true
115+
// +optional
116+
CacheUnauthorizedRequests *bool `json:"cacheUnauthorizedRequests,omitempty"`
105117
// Timeout for the webhook request
106118
// Maximum allowed value is 30s.
107119
// Required, no default value.

staging/src/k8s.io/apiserver/pkg/apis/apiserver/v1/zz_generated.conversion.go

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

staging/src/k8s.io/apiserver/pkg/apis/apiserver/v1/zz_generated.deepcopy.go

Lines changed: 10 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/apiserver/pkg/apis/apiserver/v1alpha1/defaults.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"time"
2121

2222
"k8s.io/apimachinery/pkg/runtime"
23+
"k8s.io/utils/ptr"
2324
)
2425

2526
func addDefaultingFuncs(scheme *runtime.Scheme) error {
@@ -30,7 +31,13 @@ func SetDefaults_WebhookConfiguration(obj *WebhookConfiguration) {
3031
if obj.AuthorizedTTL.Duration == 0 {
3132
obj.AuthorizedTTL.Duration = 5 * time.Minute
3233
}
34+
if obj.CacheAuthorizedRequests == nil {
35+
obj.CacheAuthorizedRequests = ptr.To(true)
36+
}
3337
if obj.UnauthorizedTTL.Duration == 0 {
3438
obj.UnauthorizedTTL.Duration = 30 * time.Second
3539
}
40+
if obj.CacheUnauthorizedRequests == nil {
41+
obj.CacheUnauthorizedRequests = ptr.To(true)
42+
}
3643
}

staging/src/k8s.io/apiserver/pkg/apis/apiserver/v1alpha1/types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,11 +550,23 @@ type WebhookConfiguration struct {
550550
// Same as setting `--authorization-webhook-cache-authorized-ttl` flag
551551
// Default: 5m0s
552552
AuthorizedTTL metav1.Duration `json:"authorizedTTL"`
553+
// CacheAuthorizedRequests specifies whether authorized requests should be cached.
554+
// If set to true, the TTL for cached decisions can be configured via the
555+
// AuthorizedTTL field.
556+
// Default: true
557+
// +optional
558+
CacheAuthorizedRequests *bool `json:"cacheAuthorizedRequests,omitempty"`
553559
// The duration to cache 'unauthorized' responses from the webhook
554560
// authorizer.
555561
// Same as setting `--authorization-webhook-cache-unauthorized-ttl` flag
556562
// Default: 30s
557563
UnauthorizedTTL metav1.Duration `json:"unauthorizedTTL"`
564+
// CacheUnauthorizedRequests specifies whether unauthorized requests should be cached.
565+
// If set to true, the TTL for cached decisions can be configured via the
566+
// UnauthorizedTTL field.
567+
// Default: true
568+
// +optional
569+
CacheUnauthorizedRequests *bool `json:"cacheUnauthorizedRequests,omitempty"`
558570
// Timeout for the webhook request
559571
// Maximum allowed value is 30s.
560572
// Required, no default value.

0 commit comments

Comments
 (0)