Skip to content

Commit 11ce6d2

Browse files
committed
k8s.io/apiserver: fix levelling of the name field in AuthorizationConfiguration
Signed-off-by: Nabarun Pal <[email protected]>
1 parent 3c94af7 commit 11ce6d2

File tree

5 files changed

+111
-170
lines changed

5 files changed

+111
-170
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,18 +228,19 @@ type AuthorizerConfiguration struct {
228228
// types like Node, RBAC, ABAC, etc.
229229
Type AuthorizerType
230230

231+
// Name used to describe the webhook
232+
// This is explicitly used in monitoring machinery for metrics
233+
// Note: Names must be DNS1123 labels like `myauthorizername` or
234+
// subdomains like `myauthorizer.example.domain`
235+
// Required, with no default
236+
Name string
237+
231238
// Webhook defines the configuration for a Webhook authorizer
232239
// Must be defined when Type=Webhook
233240
Webhook *WebhookConfiguration
234241
}
235242

236243
type WebhookConfiguration struct {
237-
// Name used to describe the webhook
238-
// This is explicitly used in monitoring machinery for metrics
239-
// Note: Names must be DNS1123 labels like `mywebhookname` or
240-
// subdomains like `webhookname.example.domain`
241-
// Required, with no default
242-
Name string
243244
// The duration to cache 'authorized' responses from the webhook
244245
// authorizer.
245246
// Same as setting `--authorization-webhook-cache-authorized-ttl` flag

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,19 +298,20 @@ type AuthorizerConfiguration struct {
298298
// types like Node, RBAC, ABAC, etc.
299299
Type string `json:"type"`
300300

301+
// Name used to describe the webhook
302+
// This is explicitly used in monitoring machinery for metrics
303+
// Note: Names must be DNS1123 labels like `myauthorizername` or
304+
// subdomains like `myauthorizer.example.domain`
305+
// Required, with no default
306+
Name string `json:"name"`
307+
301308
// Webhook defines the configuration for a Webhook authorizer
302309
// Must be defined when Type=Webhook
303310
// Must not be defined when Type!=Webhook
304311
Webhook *WebhookConfiguration `json:"webhook,omitempty"`
305312
}
306313

307314
type WebhookConfiguration struct {
308-
// Name used to describe the webhook
309-
// This is explicitly used in monitoring machinery for metrics
310-
// Note: Names must be DNS1123 labels like `mywebhookname` or
311-
// subdomains like `webhookname.example.domain`
312-
// Required, with no default
313-
Name string `json:"name"`
314315
// The duration to cache 'authorized' responses from the webhook
315316
// authorizer.
316317
// Same as setting `--authorization-webhook-cache-authorized-ttl` flag

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

Lines changed: 2 additions & 2 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/validation/validation.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package validation
1818

1919
import (
2020
"fmt"
21+
utilvalidation "k8s.io/apimachinery/pkg/util/validation"
2122
"net/url"
2223
"os"
2324
"path/filepath"
@@ -28,7 +29,6 @@ import (
2829
"k8s.io/api/authorization/v1beta1"
2930
"k8s.io/apimachinery/pkg/runtime"
3031
"k8s.io/apimachinery/pkg/util/sets"
31-
utilvalidation "k8s.io/apimachinery/pkg/util/validation"
3232
"k8s.io/apimachinery/pkg/util/validation/field"
3333
api "k8s.io/apiserver/pkg/apis/apiserver"
3434
"k8s.io/client-go/util/cert"
@@ -220,7 +220,7 @@ func ValidateAuthorizationConfiguration(fldPath *field.Path, c *api.Authorizatio
220220
}
221221

222222
seenAuthorizerTypes := sets.NewString()
223-
seenWebhookNames := sets.NewString()
223+
seenAuthorizerNames := sets.NewString()
224224
for i, a := range c.Authorizers {
225225
fldPath := fldPath.Child("authorizers").Index(i)
226226
aType := string(a.Type)
@@ -238,13 +238,22 @@ func ValidateAuthorizationConfiguration(fldPath *field.Path, c *api.Authorizatio
238238
}
239239
seenAuthorizerTypes.Insert(aType)
240240

241+
if len(a.Name) == 0 {
242+
allErrs = append(allErrs, field.Required(fldPath.Child("name"), ""))
243+
} else if seenAuthorizerNames.Has(a.Name) {
244+
allErrs = append(allErrs, field.Duplicate(fldPath.Child("name"), a.Name))
245+
} else if errs := utilvalidation.IsDNS1123Subdomain(a.Name); len(errs) != 0 {
246+
allErrs = append(allErrs, field.Invalid(fldPath.Child("name"), a.Name, fmt.Sprintf("authorizer name is invalid: %s", strings.Join(errs, ", "))))
247+
}
248+
seenAuthorizerNames.Insert(a.Name)
249+
241250
switch a.Type {
242251
case api.TypeWebhook:
243252
if a.Webhook == nil {
244253
allErrs = append(allErrs, field.Required(fldPath.Child("webhook"), "required when type=Webhook"))
245254
continue
246255
}
247-
allErrs = append(allErrs, ValidateWebhookConfiguration(fldPath, a.Webhook, seenWebhookNames)...)
256+
allErrs = append(allErrs, ValidateWebhookConfiguration(fldPath, a.Webhook)...)
248257
default:
249258
if a.Webhook != nil {
250259
allErrs = append(allErrs, field.Invalid(fldPath.Child("webhook"), "non-null", "may only be specified when type=Webhook"))
@@ -255,16 +264,8 @@ func ValidateAuthorizationConfiguration(fldPath *field.Path, c *api.Authorizatio
255264
return allErrs
256265
}
257266

258-
func ValidateWebhookConfiguration(fldPath *field.Path, c *api.WebhookConfiguration, seenNames sets.String) field.ErrorList {
267+
func ValidateWebhookConfiguration(fldPath *field.Path, c *api.WebhookConfiguration) field.ErrorList {
259268
allErrs := field.ErrorList{}
260-
if len(c.Name) == 0 {
261-
allErrs = append(allErrs, field.Required(fldPath.Child("name"), ""))
262-
} else if seenNames.Has(c.Name) {
263-
allErrs = append(allErrs, field.Duplicate(fldPath.Child("name"), c.Name))
264-
} else if errs := utilvalidation.IsDNS1123Subdomain(c.Name); len(errs) != 0 {
265-
allErrs = append(allErrs, field.Invalid(fldPath.Child("name"), c.Name, fmt.Sprintf("webhook name is invalid: %s", strings.Join(errs, ", "))))
266-
}
267-
seenNames.Insert(c.Name)
268269

269270
if c.Timeout.Duration == 0 {
270271
allErrs = append(allErrs, field.Required(fldPath.Child("timeout"), ""))

0 commit comments

Comments
 (0)