Skip to content

Bug 1679272 - Validate console can talk to OAuth token URL #1206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ type Config struct {
ClientSecret string
Scope []string

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

SuccessURL string
ErrorURL string
Expand Down Expand Up @@ -140,33 +140,37 @@ func newHTTPClient(issuerCA string, includeSystemRoots bool) (*http.Client, erro
// NewAuthenticator initializes an Authenticator struct. It blocks until the authenticator is
// able to contact the provider.
func NewAuthenticator(ctx context.Context, c *Config) (*Authenticator, error) {
a, err := newUnstartedAuthenticator(c)
if err != nil {
return nil, err
}

// Retry connecting to the identity provider a few times
backoff := time.Second * 2
maxSteps := 5
maxSteps := 7
steps := 0

for {
var (
a *Authenticator
lm loginMethod
endpoint oauth2.Endpoint
err error
)

a, err = newUnstartedAuthenticator(c)
if err != nil {
return nil, err
}

switch c.AuthSource {
case AuthSourceOpenShift:
// Use the k8s CA for OAuth metadata discovery.
var client *http.Client
client, err = newHTTPClient(c.DiscoveryCA, false)
var k8sClient *http.Client
// Don't include system roots when talking to the API server.
k8sClient, err = newHTTPClient(c.K8sCA, false)
if err != nil {
return nil, err
}

endpoint, lm, err = newOpenShiftAuth(ctx, &openShiftConfig{
client: client,
k8sClient: k8sClient,
oauthClient: a.client,
issuerURL: c.IssuerURL,
cookiePath: c.CookiePath,
secureCookies: c.SecureCookies,
Expand All @@ -183,11 +187,11 @@ func NewAuthenticator(ctx context.Context, c *Config) (*Authenticator, error) {
if err != nil {
steps++
if steps > maxSteps {
log.Errorf("error contacting openid connect provider: %v", err)
log.Errorf("error contacting auth provider: %v", err)
return nil, err
}

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

time.Sleep(backoff)
backoff *= 2
Expand Down
18 changes: 16 additions & 2 deletions auth/auth_openshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ type openShiftAuth struct {
}

type openShiftConfig struct {
client *http.Client
k8sClient *http.Client
oauthClient *http.Client
issuerURL string
cookiePath string
secureCookies bool
Expand Down Expand Up @@ -52,7 +53,7 @@ func newOpenShiftAuth(ctx context.Context, c *openShiftConfig) (oauth2.Endpoint,
return oauth2.Endpoint{}, nil, err
}

resp, err := c.client.Do(req.WithContext(ctx))
resp, err := c.k8sClient.Do(req.WithContext(ctx))
if err != nil {
return oauth2.Endpoint{}, nil, err
}
Expand Down Expand Up @@ -86,6 +87,19 @@ func newOpenShiftAuth(ctx context.Context, c *openShiftConfig) (oauth2.Endpoint,
return oauth2.Endpoint{}, nil, err
}

// Make sure we can talk to the token endpoint.
req, err = http.NewRequest(http.MethodHead, metadata.Token, nil)
if err != nil {
return oauth2.Endpoint{}, nil, err
}

resp, err = c.oauthClient.Do(req.WithContext(ctx))
if err != nil {
return oauth2.Endpoint{}, nil, fmt.Errorf("request to OAuth token endpoint %s failed: %v",
metadata.Token, err)
}
defer resp.Body.Close()

kubeAdminLogoutURL := proxy.SingleJoiningSlash(metadata.Issuer, "/logout")
return oauth2.Endpoint{
AuthURL: metadata.Auth,
Expand Down
4 changes: 2 additions & 2 deletions cmd/bridge/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func main() {

// Use the k8s CA file for OpenShift OAuth metadata discovery.
// This might be different than IssuerCA.
DiscoveryCA: caCertFilePath,
K8sCA: caCertFilePath,

ErrorURL: authLoginErrorEndpoint,
SuccessURL: authLoginSuccessEndpoint,
Expand Down Expand Up @@ -394,7 +394,7 @@ func main() {
}

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