Skip to content

Commit 6ab4c68

Browse files
committed
surface authz webhook flags to configure an external authorization webhook
On-behalf-of: @SAP [email protected]
1 parent f8fe716 commit 6ab4c68

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

pkg/server/options/authorization.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ func (s *Authorization) AddFlags(fs *pflag.FlagSet) {
8484
func (s *Authorization) ApplyTo(config *genericapiserver.Config, kubeInformers, globalKubeInformers kcpkubernetesinformers.SharedInformerFactory, kcpInformers, globalKcpInformers kcpinformers.SharedInformerFactory) error {
8585
var authorizers []authorizer.Authorizer
8686

87+
// re-use the authorizer from the generic control plane (this is only set for webhooks)
88+
if authorizer := config.Authorization.Authorizer; authorizer != nil {
89+
authorizers = append(authorizers, authorizer)
90+
}
91+
8792
localLogicalClusterLister := kcpInformers.Core().V1alpha1().LogicalClusters().Lister()
8893
globalLogicalClusterLister := globalKcpInformers.Core().V1alpha1().LogicalClusters().Lister()
8994

pkg/server/options/flags.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ var (
5555
"audit-webhook-truncate-max-batch-size", // Maximum size of the batch sent to the underlying backend. Actual serialized size can be several hundreds of bytes greater. If a batch exceeds this limit, it is split into several batches of smaller size.
5656
"audit-webhook-truncate-max-event-size", // Maximum size of the audit event sent to the underlying backend. If the size of an event is greater than this number, first request and response are removed, and if this doesn't reduce the size enough, event is discarded.
5757
"audit-webhook-version", // API group and version used for serializing audit events written to webhook.
58-
5958
// authentication flags
59+
6060
"anonymous-auth", // Enables anonymous requests to the secure port of the API server. Requests that are not rejected by another authentication method are treated as anonymous requests. Anonymous requests have a username of system:anonymous, and a group name of system:unauthenticated.
6161
"api-audiences", // Identifiers of the API. The service account token authenticator will validate that tokens used against the API are bound to at least one of these audiences. If the --service-account-issuer flag is configured and this flag is not, this field defaults to a single element list containing the issuer URL.
6262
"authentication-token-webhook-cache-ttl", // The duration to cache responses from the webhook token authenticator.
@@ -80,6 +80,12 @@ var (
8080
"requestheader-username-headers", // List of request headers to inspect for usernames. X-Remote-User is common.
8181
"token-auth-file", // If set, the file that will be used to secure the secure port of the API server via token authentication.
8282

83+
// authorization
84+
"authorization-webhook-config-file", // File with webhook configuration in kubeconfig format, used with --authorization-mode=Webhook. The API server will query the remote service to determine access on the API server's secure port.
85+
"authorization-webhook-version", // The API version of the authorization.k8s.io SubjectAccessReview to send to and expect from the webhook.
86+
"authorization-webhook-cache-authorized-ttl", // The duration to cache 'authorized' responses from the webhook authorizer.
87+
"authorization-webhook-cache-unauthorized-ttl", // The duration to cache 'unauthorized' responses from the webhook authorizer.
88+
8389
// Kubernetes ServiceAccount Authentication flags
8490
"service-account-extend-token-expiration", // Turns on projected service account expiration extension during token generation, which helps safe transition from legacy token to bound service account token feature. If this flag is enabled, admission injected tokens would be extended up to 1 year to prevent unexpected failure during transition, ignoring value of service-account-max-token-expiration.
8591
"service-account-issuer", // Identifier of the service account token issuer. The issuer will assert this identifier in "iss" claim of issued tokens. This value is a string or URI. If this option is not a valid URI per the OpenID Discovery 1.0 spec, the ServiceAccountIssuerDiscovery feature will remain disabled, even if the feature gate is set to true. It is highly recommended that this value comply with the OpenID spec: https://openid.net/specs/openid-connect-discovery-1_0.html. In practice, this means that service-account-issuer must be an https URL. It is also highly recommended that this URL be capable of serving OpenID discovery documents at {service-account-issuer}/.well-known/openid-configuration. When this flag is specified multiple times, the first is used to generate tokens and all are used to determine which issuers are accepted.
@@ -204,5 +210,10 @@ var (
204210
// authentication flags
205211
// TODO(embik): look at enabling this feature.
206212
"authentication-config", // File with Authentication Configuration to configure the JWT Token authenticator. Note: This feature is in Alpha since v1.29.--feature-gate=StructuredAuthenticationConfiguration=true needs to be set for enabling this feature.This feature is mutually exclusive with the oidc-* flags.
213+
214+
// authorization flags
215+
"authorization-mode", // Ordered list of plug-ins to do authorization on secure port. Defaults to AlwaysAllow if --authorization-config is not used. Comma-delimited list of: Webhook
216+
"authorization-config", // File with Authorization Configuration to configure the authorizer chain.
217+
"authorization-policy-file", // File with authorization policy in json line by line format, used with --authorization-mode=ABAC, on the secure port.
207218
)
208219
)

pkg/server/options/options.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
genericapiserveroptions "k8s.io/apiserver/pkg/server/options"
3030
cliflag "k8s.io/component-base/cli/flag"
3131
controlplaneapiserver "k8s.io/kubernetes/pkg/controlplane/apiserver/options"
32-
kubeoptions "k8s.io/kubernetes/pkg/kubeapiserver/options"
32+
authzmodes "k8s.io/kubernetes/pkg/kubeapiserver/authorizer/modes"
3333

3434
kcpadmission "github.com/kcp-dev/kcp/pkg/admission"
3535
etcdoptions "github.com/kcp-dev/kcp/pkg/embeddedetcd/options"
@@ -114,18 +114,8 @@ func NewOptions(rootDir string) *Options {
114114

115115
// override all the stuff
116116
o.GenericControlPlane.SecureServing.ServerCert.CertDirectory = rootDir
117-
o.GenericControlPlane.Authentication = kubeoptions.NewBuiltInAuthenticationOptions().
118-
WithAnonymous().
119-
WithBootstrapToken().
120-
WithClientCert().
121-
WithOIDC().
122-
WithRequestHeader().
123-
WithServiceAccounts().
124-
WithTokenFile().
125-
WithWebHook()
126117
o.GenericControlPlane.Authentication.ServiceAccounts.Issuers = []string{"https://kcp.default.svc"}
127118
o.GenericControlPlane.Etcd.StorageConfig.Transport.ServerList = []string{"embedded"}
128-
o.GenericControlPlane.Authorization = nil // we have our own
129119

130120
// override set of admission plugins
131121
kcpadmission.RegisterAllKcpAdmissionPlugins(o.GenericControlPlane.Admission.GenericAdmission.Plugins)
@@ -313,6 +303,16 @@ func (o *Options) Complete(rootDir string) (*CompletedOptions, error) {
313303
o.GenericControlPlane.ServiceAccountSigningKeyFile = o.Controllers.SAController.ServiceAccountKeyFile
314304
}
315305

306+
// kcp uses the generic control plane's authorization flags, but only supports the webhook mode;
307+
// to prevent misconfigurations the --authorization-mode flag is not exposed, but set here
308+
// automatically based on the presence of the other webhook related flags.
309+
if o.GenericControlPlane.Authorization.WebhookConfigFile != "" {
310+
o.GenericControlPlane.Authorization.Modes = []string{authzmodes.ModeWebhook}
311+
} else {
312+
// if no webhook is used, completely disable upstream authz, as kcp has its own authorization logic
313+
o.GenericControlPlane.Authorization = nil
314+
}
315+
316316
completedGenericServerRunOptions, err := o.GenericControlPlane.Complete(nil, nil)
317317
if err != nil {
318318
return nil, err

0 commit comments

Comments
 (0)