Skip to content

Commit 48b61be

Browse files
committed
Wire in WebhookTokenAutenticator support
Signed-off-by: Simo Sorce <[email protected]>
1 parent 3addbb0 commit 48b61be

File tree

8 files changed

+104
-1
lines changed

8 files changed

+104
-1
lines changed

pkg/cmd/server/apis/config/helpers.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ func GetMasterFileReferences(config *MasterConfig) []*string {
238238
refs = append(refs, &config.AuthConfig.RequestHeader.ClientCA)
239239
}
240240

241+
for _, wta := range config.AuthConfig.WebhookTokenAuthenticators {
242+
refs = append(refs, &wta.WebhookTokenAuthnConfigFile)
243+
}
244+
241245
refs = append(refs, &config.AggregatorConfig.ProxyClientInfo.CertFile)
242246
refs = append(refs, &config.AggregatorConfig.ProxyClientInfo.KeyFile)
243247

pkg/cmd/server/apis/config/types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ type MasterConfig struct {
399399
EtcdConfig *EtcdConfig
400400
// OAuthConfig, if present start the /oauth endpoint in this process
401401
OAuthConfig *OAuthConfig
402+
402403
// DNSConfig, if present start the DNS server in this process
403404
DNSConfig *DNSConfig
404405

@@ -446,6 +447,8 @@ type MasterConfig struct {
446447
type MasterAuthConfig struct {
447448
// RequestHeader holds options for setting up a front proxy against the the API. It is optional.
448449
RequestHeader *RequestHeaderAuthenticationOptions
450+
// WebhookTokenAuthnConfig, if present configures remote token reviewers
451+
WebhookTokenAuthenticators []WebhookTokenAuthenticator
449452
}
450453

451454
// RequestHeaderAuthenticationOptions provides options for setting up a front proxy against the entire
@@ -828,6 +831,15 @@ type DNSConfig struct {
828831
AllowRecursiveQueries bool
829832
}
830833

834+
type WebhookTokenAuthenticator struct {
835+
// WebhookTokenAuthnConfigFile is a path to a Kubeconfig file with the webhook configuration
836+
WebhookTokenAuthnConfigFile string
837+
// WebhookTokenAuthnCacheTTL indicates how long an authentication result should be cached.
838+
// It takes a valid time duration string (e.g. "5m").
839+
// If empty, you get the default timeout. If zero (e.g. "0m"), caching is disabled
840+
WebhookTokenAuthnCacheTTL string
841+
}
842+
831843
type OAuthConfig struct {
832844
// MasterCA is the CA for verifying the TLS connection back to the MasterURL.
833845
// "" to use system roots, set to use custom roots, never nil (guaranteed by conversion defaults)

pkg/cmd/server/apis/config/v1/types.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ type MasterConfig struct {
247247
EtcdConfig *EtcdConfig `json:"etcdConfig"`
248248
// OAuthConfig, if present start the /oauth endpoint in this process
249249
OAuthConfig *OAuthConfig `json:"oauthConfig"`
250+
250251
// DNSConfig, if present start the DNS server in this process
251252
DNSConfig *DNSConfig `json:"dnsConfig"`
252253

@@ -294,6 +295,8 @@ type MasterConfig struct {
294295
type MasterAuthConfig struct {
295296
// RequestHeader holds options for setting up a front proxy against the the API. It is optional.
296297
RequestHeader *RequestHeaderAuthenticationOptions `json:"requestHeader"`
298+
// WebhookTokenAuthnConfig, if present configures remote token reviewers
299+
WebhookTokenAuthenticators []WebhookTokenAuthenticator `json:"webhookTokenAuthenticators"`
297300
}
298301

299302
// RequestHeaderAuthenticationOptions provides options for setting up a front proxy against the entire
@@ -711,6 +714,17 @@ type DNSConfig struct {
711714
AllowRecursiveQueries bool `json:"allowRecursiveQueries"`
712715
}
713716

717+
// WebhookTokenAuthenticators holds the necessary configuation options for
718+
// external token authenticators
719+
type WebhookTokenAuthenticator struct {
720+
// WebhookTokenAuthnConfigFile is a path to a Kubeconfig file with the webhook configuration
721+
WebhookTokenAuthnConfigFile string `json:"webhookTokenAuthnConfigFile"`
722+
// WebhookTokenAuthnCacheTTL indicates how long an authentication result should be cached.
723+
// It takes a valid time duration string (e.g. "5m").
724+
// If empty, you get the default timeout. If zero (e.g. "0m"), caching is disabled
725+
WebhookTokenAuthnCacheTTL string `json:"webhookTokenAuthnCacheTTL"`
726+
}
727+
714728
// OAuthConfig holds the necessary configuration options for OAuth authentication
715729
type OAuthConfig struct {
716730
// MasterCA is the CA for verifying the TLS connection back to the MasterURL.

pkg/cmd/server/apis/config/v1/zz_generated.deepcopy.go

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

pkg/cmd/server/apis/config/validation/master.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,24 @@ func ValidateMasterAuthConfig(config configapi.MasterAuthConfig, fldPath *field.
192192
validationResults.AddErrors(field.Required(fldPath.Child("requestHeader.extraHeaderPrefixes"), "must be specified for a secure connection"))
193193
}
194194

195+
for _, wta := range config.WebhookTokenAuthenticators {
196+
webhookTokenAuthnConfigFile := fldPath.Child("webhookTokenAuthenticators", "webhookTokenAuthnConfigFile")
197+
if len(wta.WebhookTokenAuthnConfigFile) == 0 {
198+
validationResults.AddErrors(field.Required(webhookTokenAuthnConfigFile, ""))
199+
} else {
200+
validationResults.AddErrors(ValidateFile(wta.WebhookTokenAuthnConfigFile, webhookTokenAuthnConfigFile)...)
201+
}
202+
203+
webhookTokenAuthnCacheTTL := fldPath.Child("webhookTokenAuthenticators", "webhookTokenAuthnCacheTTL")
204+
if len(wta.WebhookTokenAuthnCacheTTL) == 0 {
205+
validationResults.AddErrors(field.Required(webhookTokenAuthnCacheTTL, ""))
206+
} else if ttl, err := time.ParseDuration(wta.WebhookTokenAuthnCacheTTL); err != nil {
207+
validationResults.AddErrors(field.Invalid(webhookTokenAuthnCacheTTL, wta.WebhookTokenAuthnCacheTTL, fmt.Sprintf("%v", err)))
208+
} else if ttl < 0 {
209+
validationResults.AddErrors(field.Invalid(webhookTokenAuthnCacheTTL, wta.WebhookTokenAuthnCacheTTL, "cannot be less than zero"))
210+
}
211+
}
212+
195213
return validationResults
196214
}
197215

pkg/cmd/server/apis/config/zz_generated.deepcopy.go

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

pkg/cmd/server/kubernetes/master/master_config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ func BuildKubernetesMasterConfig(
588588

589589
func defaultOpenAPIConfig(config configapi.MasterConfig) *openapicommon.Config {
590590
securityDefinitions := spec.SecurityDefinitions{}
591-
if len(config.ServiceAccountConfig.PublicKeyFiles) > 0 {
591+
if len(config.ServiceAccountConfig.PublicKeyFiles) > 0 || len(config.AuthConfig.WebhookTokenAuthenticators) > 0 {
592592
securityDefinitions["BearerToken"] = &spec.SecurityScheme{
593593
SecuritySchemeProps: spec.SecuritySchemeProps{
594594
Type: "apiKey",

pkg/cmd/server/origin/authenticator.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
tokencache "k8s.io/apiserver/pkg/authentication/token/cache"
1717
tokenunion "k8s.io/apiserver/pkg/authentication/token/union"
1818
genericapiserver "k8s.io/apiserver/pkg/server"
19+
webhooktoken "k8s.io/apiserver/plugin/pkg/authenticator/token/webhook"
1920
kclientsetexternal "k8s.io/client-go/kubernetes"
2021
"k8s.io/client-go/rest"
2122
"k8s.io/client-go/util/cert"
@@ -108,6 +109,18 @@ func newAuthenticator(config configapi.MasterConfig, accessTokenGetter oauthclie
108109
group.NewTokenGroupAdder(oauthTokenAuthenticator, []string{bootstrappolicy.AuthenticatedOAuthGroup}))
109110
}
110111

112+
for _, wta := range config.AuthConfig.WebhookTokenAuthenticators {
113+
ttl, err := time.ParseDuration(wta.WebhookTokenAuthnCacheTTL)
114+
if err != nil {
115+
return nil, nil, fmt.Errorf("Error converting WebhookTokenAuthnCacheTTL='%s' to duration", wta.WebhookTokenAuthnCacheTTL)
116+
}
117+
webhookTokenAuthenticator, err := webhooktoken.New(wta.WebhookTokenAuthnConfigFile, ttl)
118+
if err != nil {
119+
return nil, nil, fmt.Errorf("Failed to create authenticator for WebhookTokenAuthnConfigFile='%s'", wta.WebhookTokenAuthnConfigFile)
120+
}
121+
tokenAuthenticators = append(tokenAuthenticators, webhookTokenAuthenticator)
122+
}
123+
111124
if len(tokenAuthenticators) > 0 {
112125
// Combine all token authenticators
113126
tokenAuth := tokenunion.New(tokenAuthenticators...)

0 commit comments

Comments
 (0)