@@ -10,8 +10,6 @@ import (
10
10
"strings"
11
11
"time"
12
12
13
- "gopkg.in/square/go-jose.v2"
14
- "gopkg.in/square/go-jose.v2/jwt"
15
13
utilnet "k8s.io/apimachinery/pkg/util/net"
16
14
"k8s.io/apiserver/pkg/authentication/request/bearertoken"
17
15
authuser "k8s.io/apiserver/pkg/authentication/user"
@@ -22,7 +20,6 @@ import (
22
20
"k8s.io/klog"
23
21
24
22
"github.com/jetstack/kube-oidc-proxy/cmd/app/options"
25
- "github.com/jetstack/kube-oidc-proxy/pkg/probe"
26
23
"github.com/jetstack/kube-oidc-proxy/pkg/proxy/tokenreview"
27
24
)
28
25
@@ -44,49 +41,49 @@ type Options struct {
44
41
45
42
type Proxy struct {
46
43
oidcAuther * bearertoken.Authenticator
47
- oidcOptions * options.OIDCAuthenticationOptions
48
44
tokenReviewer * tokenreview.TokenReview
49
45
secureServingInfo * server.SecureServingInfo
50
46
51
47
restConfig * rest.Config
52
48
clientTransport http.RoundTripper
53
49
noAuthClientTransport http.RoundTripper
54
- healthCheck * probe.HealthCheck
55
50
56
51
options * Options
57
52
}
58
53
59
54
func New (restConfig * rest.Config , oidcOptions * options.OIDCAuthenticationOptions ,
60
- tokenReviewer * tokenreview.TokenReview , ssinfo * server.SecureServingInfo , healthCheck * probe.HealthCheck , options * Options ) * Proxy {
55
+ tokenReviewer * tokenreview.TokenReview , ssinfo * server.SecureServingInfo ,
56
+ options * Options ) (* Proxy , error ) {
57
+
58
+ // generate tokenAuther from oidc config
59
+ tokenAuther , err := oidc .New (oidc.Options {
60
+ APIAudiences : oidcOptions .APIAudiences ,
61
+ CAFile : oidcOptions .CAFile ,
62
+ ClientID : oidcOptions .ClientID ,
63
+ GroupsClaim : oidcOptions .GroupsClaim ,
64
+ GroupsPrefix : oidcOptions .GroupsPrefix ,
65
+ IssuerURL : oidcOptions .IssuerURL ,
66
+ RequiredClaims : oidcOptions .RequiredClaims ,
67
+ SupportedSigningAlgs : oidcOptions .SigningAlgs ,
68
+ UsernameClaim : oidcOptions .UsernameClaim ,
69
+ UsernamePrefix : oidcOptions .UsernamePrefix ,
70
+ })
71
+ if err != nil {
72
+ return nil , err
73
+ }
74
+
75
+ oidcAuther := bearertoken .New (tokenAuther )
76
+
61
77
return & Proxy {
62
78
restConfig : restConfig ,
63
- oidcOptions : oidcOptions ,
64
79
tokenReviewer : tokenReviewer ,
65
80
secureServingInfo : ssinfo ,
66
- healthCheck : healthCheck ,
67
81
options : options ,
68
- }
82
+ oidcAuther : oidcAuther ,
83
+ }, nil
69
84
}
70
85
71
86
func (p * Proxy ) Run (stopCh <- chan struct {}) (<- chan struct {}, error ) {
72
- // generate oidcAuther from oidc config
73
- oidcAuther , err := oidc .New (oidc.Options {
74
- APIAudiences : p .oidcOptions .APIAudiences ,
75
- CAFile : p .oidcOptions .CAFile ,
76
- ClientID : p .oidcOptions .ClientID ,
77
- GroupsClaim : p .oidcOptions .GroupsClaim ,
78
- GroupsPrefix : p .oidcOptions .GroupsPrefix ,
79
- IssuerURL : p .oidcOptions .IssuerURL ,
80
- RequiredClaims : p .oidcOptions .RequiredClaims ,
81
- SupportedSigningAlgs : p .oidcOptions .SigningAlgs ,
82
- UsernameClaim : p .oidcOptions .UsernameClaim ,
83
- UsernamePrefix : p .oidcOptions .UsernamePrefix ,
84
- })
85
- if err != nil {
86
- return nil , err
87
- }
88
- p .oidcAuther = bearertoken .New (oidcAuther )
89
-
90
87
// standard round tripper for proxy to API Server
91
88
clientRT , err := p .roundTripperForRestConfig (p .restConfig )
92
89
if err != nil {
@@ -123,37 +120,6 @@ func (p *Proxy) Run(stopCh <-chan struct{}) (<-chan struct{}, error) {
123
120
proxyHandler .Transport = p
124
121
proxyHandler .ErrorHandler = p .Error
125
122
126
- // probe for readiness
127
- go func () {
128
- ticker := time .NewTicker (500 * time .Millisecond )
129
- for {
130
- <- ticker .C
131
- fr , err := http .NewRequest ("GET" , "http://fake" , nil )
132
- if err != nil {
133
- klog .Infof ("error during readiness check: %v" , err )
134
- continue
135
- }
136
- jwt , err := p .fakeJWT ()
137
- if err != nil {
138
- klog .Infof ("error during readiness check: %v" , err )
139
- continue
140
- }
141
- fr .Header .Set ("Authorization" , fmt .Sprintf ("Bearer %s" , jwt ))
142
-
143
- _ , _ , err = p .oidcAuther .AuthenticateRequest (fr )
144
- if strings .HasSuffix (err .Error (), "authenticator not initialized" ) {
145
- klog .V (4 ).Infof ("OIDC provider not yet initialized" )
146
- continue
147
- }
148
-
149
- p .healthCheck .SetReady ()
150
- klog .Info ("OIDC provider initialized, proxy ready" )
151
- klog .V (4 ).Infof ("OIDC provider initialized, readiness check returned error: %+v" , err )
152
- return
153
- }
154
-
155
- }()
156
-
157
123
waitCh , err := p .serve (proxyHandler , stopCh )
158
124
if err != nil {
159
125
return nil , err
@@ -242,24 +208,6 @@ func (p *Proxy) RoundTrip(req *http.Request) (*http.Response, error) {
242
208
return rt .RoundTrip (reqCpy )
243
209
}
244
210
245
- // fakeJWT generates a JWT that passes the first offline validity checks. It is
246
- // used to test if the OIDC provider is initialised
247
- func (p * Proxy ) fakeJWT () (string , error ) {
248
- key := []byte ("secret" )
249
- sig , err := jose .NewSigner (jose.SigningKey {Algorithm : jose .HS256 , Key : key }, (& jose.SignerOptions {}).WithType ("JWT" ))
250
- if err != nil {
251
- return "" , err
252
- }
253
-
254
- cl := jwt.Claims {
255
- Subject : "readiness" ,
256
- Issuer : p .oidcOptions .IssuerURL ,
257
- NotBefore : jwt .NewNumericDate (time .Date (2016 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC )),
258
- Audience : jwt .Audience (p .oidcOptions .APIAudiences ),
259
- }
260
- return jwt .Signed (sig ).Claims (cl ).CompactSerialize ()
261
- }
262
-
263
211
func (p * Proxy ) tokenReview (req * http.Request ) (* http.Response , error ) {
264
212
klog .V (4 ).Infof ("attempting to validate a token in request using TokenReview endpoint(%s)" ,
265
213
req .RemoteAddr )
@@ -356,3 +304,8 @@ func (p *Proxy) roundTripperForRestConfig(config *rest.Config) (http.RoundTrippe
356
304
357
305
return clientRT , nil
358
306
}
307
+
308
+ // Return the proxy OIDC token authenticator
309
+ func (p * Proxy ) OIDCAuthenticator () * bearertoken.Authenticator {
310
+ return p .oidcAuther
311
+ }
0 commit comments