/
authentication.go
149 lines (127 loc) · 4.77 KB
/
authentication.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2022 ByteDance and its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package options
import (
"fmt"
"time"
"github.com/kubewharf/apiserver-runtime/pkg/server/authenticator"
"github.com/kubewharf/apiserver-runtime/pkg/server/options"
"github.com/spf13/pflag"
"k8s.io/apiserver/pkg/authentication/request/x509"
genericserver "k8s.io/apiserver/pkg/server"
openapicommon "k8s.io/kube-openapi/pkg/common"
"github.com/kubewharf/kubegateway/pkg/clusters"
proxyauthenticator "github.com/kubewharf/kubegateway/pkg/gateway/proxy/authenticator"
)
type AuthenticationOptions struct {
TokenSuccessCacheTTL time.Duration
TokenFailureCacheTTL time.Duration
}
func NewAuthenticationOptions() *AuthenticationOptions {
o := &AuthenticationOptions{
TokenSuccessCacheTTL: 600 * time.Second, // 10 minutes
TokenFailureCacheTTL: 10 * time.Second,
}
return o
}
func (o *AuthenticationOptions) Validate() []error {
return nil
}
func (o *AuthenticationOptions) AddFlags(fs *pflag.FlagSet) {
fs.DurationVar(&o.TokenSuccessCacheTTL, "proxy-authentication-token-success-cache-ttl", o.TokenSuccessCacheTTL,
"The duration to cache seccess responses from the upstream token request authenticator.")
fs.DurationVar(&o.TokenFailureCacheTTL, "proxy-authentication-token-failure-cache-ttl", o.TokenFailureCacheTTL,
"The duration to cache failure responses from the upstream token request authenticator.")
}
func (o *AuthenticationOptions) ToAuthenticationConfig(
controlplaneAutheNConfig authenticator.Config,
sniVerifyOptionsProvider x509.SNIVerifyOptionsProvider,
clientProvider clusters.ClientProvider,
) (*proxyauthenticator.AuthenricatorConfig, error) {
if o == nil {
return nil, nil
}
cfg := proxyauthenticator.AuthenricatorConfig{
TokenSuccessCacheTTL: o.TokenSuccessCacheTTL,
TokenFailureCacheTTL: o.TokenFailureCacheTTL,
APIAudiences: controlplaneAutheNConfig.GetAPIAudiences(),
Anonymous: true,
}
if clientCert := controlplaneAutheNConfig.GetClientCert(); clientCert != nil {
// cfg.ClientCertificateCAContentProvider = clientCert {}
cfg.ClientCert = &proxyauthenticator.ClientCertAuthenticationConfig{
CAContentProvider: clientCert.CAContentProvider,
}
}
if requestHeader := controlplaneAutheNConfig.GetRequestHeaderConfig(); requestHeader != nil {
cfg.RequestHeaderConfig = requestHeader
}
if sniVerifyOptionsProvider != nil {
// dynamic sni verify options provider
if cfg.ClientCert == nil {
cfg.ClientCert = &proxyauthenticator.ClientCertAuthenticationConfig{}
}
cfg.ClientCert.SNIVerifyOptionsPorvider = sniVerifyOptionsProvider
}
if clientProvider != nil {
cfg.TokenRequest = &proxyauthenticator.TokenAuthenticationConfig{
ClusterClientProvider: clientProvider,
}
}
return &cfg, nil
}
func (o *AuthenticationOptions) ApplyTo(
authenticationInfo *genericserver.AuthenticationInfo,
servingInfo *genericserver.SecureServingInfo,
openAPIConfig *openapicommon.Config,
sniVerifyOptionsProvider x509.SNIVerifyOptionsProvider,
clientProvider clusters.ClientProvider,
controlplaneauthnOptions *options.AuthenticationOptions,
) error {
if o == nil {
authenticationInfo.Authenticator = nil
return nil
}
controlplaneAutheNConfig, err := controlplaneauthnOptions.ToAuthenticationConfig()
if err != nil {
return err
}
cfg, err := o.ToAuthenticationConfig(controlplaneAutheNConfig, sniVerifyOptionsProvider, clientProvider)
if err != nil {
return err
}
// get the clientCA information
if cfg.ClientCert != nil && cfg.ClientCert.CAContentProvider != nil {
if err := authenticationInfo.ApplyClientCert(cfg.ClientCert.CAContentProvider, servingInfo); err != nil {
return fmt.Errorf("unable to assign client CA file: %v", err)
}
}
if cfg.RequestHeaderConfig != nil {
if err := authenticationInfo.ApplyClientCert(cfg.RequestHeaderConfig.CAContentProvider, servingInfo); err != nil {
return fmt.Errorf("unable to load request-header-client-ca-file: %v", err)
}
}
authenticationInfo.APIAudiences = cfg.APIAudiences
// create authenticator
authenticator, securityDefinitions, err := cfg.New()
if err != nil {
return err
}
authenticationInfo.Authenticator = authenticator
if openAPIConfig != nil {
openAPIConfig.SecurityDefinitions = securityDefinitions
}
authenticationInfo.SupportsBasicAuth = false
return nil
}