Skip to content

Commit 7c04ec5

Browse files
committed
Bug 1679272 - Validate console can talk to OAuth token URL
Make sure we can successfully talk to the OAuth token URL after discovering metadata before marking the console pod as ready. Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1679272
1 parent 9bfcbc6 commit 7c04ec5

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

auth/auth.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ type Config struct {
9393
ClientSecret string
9494
Scope []string
9595

96-
// DiscoveryCA is required for OpenShift OAuth metadata discovery. This is the CA
96+
// K8sCA is required for OpenShift OAuth metadata discovery. This is the CA
9797
// used to talk to the master, which might be different than the issuer CA.
98-
DiscoveryCA string
98+
K8sCA string
9999

100100
SuccessURL string
101101
ErrorURL string
@@ -140,33 +140,37 @@ func newHTTPClient(issuerCA string, includeSystemRoots bool) (*http.Client, erro
140140
// NewAuthenticator initializes an Authenticator struct. It blocks until the authenticator is
141141
// able to contact the provider.
142142
func NewAuthenticator(ctx context.Context, c *Config) (*Authenticator, error) {
143-
a, err := newUnstartedAuthenticator(c)
144-
if err != nil {
145-
return nil, err
146-
}
147-
148143
// Retry connecting to the identity provider a few times
149144
backoff := time.Second * 2
150-
maxSteps := 5
145+
maxSteps := 7
151146
steps := 0
152147

153148
for {
154149
var (
150+
a *Authenticator
155151
lm loginMethod
156152
endpoint oauth2.Endpoint
157153
err error
158154
)
155+
156+
a, err = newUnstartedAuthenticator(c)
157+
if err != nil {
158+
return nil, err
159+
}
160+
159161
switch c.AuthSource {
160162
case AuthSourceOpenShift:
161163
// Use the k8s CA for OAuth metadata discovery.
162-
var client *http.Client
163-
client, err = newHTTPClient(c.DiscoveryCA, false)
164+
var k8sClient *http.Client
165+
// Don't include system roots when talking to the API server.
166+
k8sClient, err = newHTTPClient(c.K8sCA, false)
164167
if err != nil {
165168
return nil, err
166169
}
167170

168171
endpoint, lm, err = newOpenShiftAuth(ctx, &openShiftConfig{
169-
client: client,
172+
k8sClient: k8sClient,
173+
oauthClient: a.client,
170174
issuerURL: c.IssuerURL,
171175
cookiePath: c.CookiePath,
172176
secureCookies: c.SecureCookies,
@@ -183,11 +187,11 @@ func NewAuthenticator(ctx context.Context, c *Config) (*Authenticator, error) {
183187
if err != nil {
184188
steps++
185189
if steps > maxSteps {
186-
log.Errorf("error contacting openid connect provider: %v", err)
190+
log.Errorf("error contacting auth provider: %v", err)
187191
return nil, err
188192
}
189193

190-
log.Errorf("error contacting openid connect provider (retrying in %s): %v", backoff, err)
194+
log.Errorf("error contacting auth provider (retrying in %s): %v", backoff, err)
191195

192196
time.Sleep(backoff)
193197
backoff *= 2

auth/auth_openshift.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ type openShiftAuth struct {
2323
}
2424

2525
type openShiftConfig struct {
26-
client *http.Client
26+
k8sClient *http.Client
27+
oauthClient *http.Client
2728
issuerURL string
2829
cookiePath string
2930
secureCookies bool
@@ -52,7 +53,7 @@ func newOpenShiftAuth(ctx context.Context, c *openShiftConfig) (oauth2.Endpoint,
5253
return oauth2.Endpoint{}, nil, err
5354
}
5455

55-
resp, err := c.client.Do(req.WithContext(ctx))
56+
resp, err := c.k8sClient.Do(req.WithContext(ctx))
5657
if err != nil {
5758
return oauth2.Endpoint{}, nil, err
5859
}
@@ -86,6 +87,19 @@ func newOpenShiftAuth(ctx context.Context, c *openShiftConfig) (oauth2.Endpoint,
8687
return oauth2.Endpoint{}, nil, err
8788
}
8889

90+
// Make sure we can talk to the token endpoint.
91+
req, err = http.NewRequest(http.MethodHead, metadata.Token, nil)
92+
if err != nil {
93+
return oauth2.Endpoint{}, nil, err
94+
}
95+
96+
resp, err = c.oauthClient.Do(req.WithContext(ctx))
97+
if err != nil {
98+
return oauth2.Endpoint{}, nil, fmt.Errorf("request to OAuth token endpoint %s failed: %v",
99+
metadata.Token, err)
100+
}
101+
defer resp.Body.Close()
102+
89103
kubeAdminLogoutURL := proxy.SingleJoiningSlash(metadata.Issuer, "/logout")
90104
return oauth2.Endpoint{
91105
AuthURL: metadata.Auth,

cmd/bridge/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ func main() {
366366

367367
// Use the k8s CA file for OpenShift OAuth metadata discovery.
368368
// This might be different than IssuerCA.
369-
DiscoveryCA: caCertFilePath,
369+
K8sCA: caCertFilePath,
370370

371371
ErrorURL: authLoginErrorEndpoint,
372372
SuccessURL: authLoginSuccessEndpoint,
@@ -394,7 +394,7 @@ func main() {
394394
}
395395

396396
if srv.Auther, err = auth.NewAuthenticator(context.Background(), oidcClientConfig); err != nil {
397-
log.Fatalf("Error initializing OIDC authenticator: %v", err)
397+
log.Fatalf("Error initializing authenticator: %v", err)
398398
}
399399
case "disabled":
400400
log.Warningf("running with AUTHENTICATION DISABLED!")

0 commit comments

Comments
 (0)