Skip to content

Commit 6f5fa2e

Browse files
authored
Merge pull request kubernetes#120985 from palnabarun/3221/fix-authorizer-name
[StructuredAuthorizationConfiguration] Fix the level at which authorizer name is surfaced
2 parents f936f69 + 3de0d9a commit 6f5fa2e

File tree

6 files changed

+122
-172
lines changed

6 files changed

+122
-172
lines changed

pkg/kubeapiserver/options/authorization.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ func (o *BuiltInAuthorizationOptions) buildAuthorizationConfiguration() (*authzc
167167
case authzmodes.ModeWebhook:
168168
authorizers = append(authorizers, authzconfig.AuthorizerConfiguration{
169169
Type: authzconfig.TypeWebhook,
170+
Name: defaultWebhookName,
170171
Webhook: &authzconfig.WebhookConfiguration{
171-
Name: defaultWebhookName,
172172
AuthorizedTTL: metav1.Duration{Duration: o.WebhookCacheAuthorizedTTL},
173173
UnauthorizedTTL: metav1.Duration{Duration: o.WebhookCacheUnauthorizedTTL},
174174
// Timeout and FailurePolicy are required for the new configuration.
@@ -183,9 +183,18 @@ func (o *BuiltInAuthorizationOptions) buildAuthorizationConfiguration() (*authzc
183183
},
184184
})
185185
default:
186-
authorizers = append(authorizers, authzconfig.AuthorizerConfiguration{Type: authzconfig.AuthorizerType(mode)})
186+
authorizers = append(authorizers, authzconfig.AuthorizerConfiguration{
187+
Type: authzconfig.AuthorizerType(mode),
188+
Name: getNameForAuthorizerMode(mode),
189+
})
187190
}
188191
}
189192

190193
return &authzconfig.AuthorizationConfiguration{Authorizers: authorizers}, nil
191194
}
195+
196+
// getNameForAuthorizerMode returns the name to be set for the mode in AuthorizationConfiguration
197+
// For now, lower cases the mode name
198+
func getNameForAuthorizerMode(mode string) string {
199+
return strings.ToLower(mode)
200+
}

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)