mirror of
https://github.com/coder/coder.git
synced 2025-07-13 21:36:50 +00:00
fix: Add client certs to OAuth HTTPClient context (#5126)
This commit is contained in:
@ -207,6 +207,16 @@ func Server(vip *viper.Viper, newAPI func(context.Context, *coderd.Options) (*co
|
||||
listener = tls.NewListener(listener, tlsConfig)
|
||||
}
|
||||
|
||||
ctx, httpClient, err := configureHTTPClient(
|
||||
ctx,
|
||||
cfg.TLS.ClientCertFile.Value,
|
||||
cfg.TLS.ClientKeyFile.Value,
|
||||
cfg.TLS.ClientCAFile.Value,
|
||||
)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("configure http client: %w", err)
|
||||
}
|
||||
|
||||
tcpAddr, valid := listener.Addr().(*net.TCPAddr)
|
||||
if !valid {
|
||||
return xerrors.New("must be listening on tcp")
|
||||
@ -377,6 +387,7 @@ func Server(vip *viper.Viper, newAPI func(context.Context, *coderd.Options) (*co
|
||||
DeploymentConfig: cfg,
|
||||
PrometheusRegistry: prometheus.NewRegistry(),
|
||||
APIRateLimit: cfg.APIRateLimit.Value,
|
||||
HTTPClient: httpClient,
|
||||
}
|
||||
if tlsConfig != nil {
|
||||
options.TLSCertificates = tlsConfig.Certificates
|
||||
@ -424,11 +435,6 @@ func Server(vip *viper.Viper, newAPI func(context.Context, *coderd.Options) (*co
|
||||
return xerrors.Errorf("OIDC issuer URL must be set!")
|
||||
}
|
||||
|
||||
ctx, err := handleOauth2ClientCertificates(ctx, cfg)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("configure oidc client certificates: %w", err)
|
||||
}
|
||||
|
||||
if cfg.OIDC.IgnoreEmailVerified.Value {
|
||||
logger.Warn(ctx, "coder will not check email_verified for OIDC logins")
|
||||
}
|
||||
@ -1088,19 +1094,27 @@ func configureTLS(tlsMinVersion, tlsClientAuth string, tlsCertFiles, tlsKeyFiles
|
||||
return nil, nil //nolint:nilnil
|
||||
}
|
||||
|
||||
err = configureCAPool(tlsClientCAFile, tlsConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return tlsConfig, nil
|
||||
}
|
||||
|
||||
func configureCAPool(tlsClientCAFile string, tlsConfig *tls.Config) error {
|
||||
if tlsClientCAFile != "" {
|
||||
caPool := x509.NewCertPool()
|
||||
data, err := os.ReadFile(tlsClientCAFile)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("read %q: %w", tlsClientCAFile, err)
|
||||
return xerrors.Errorf("read %q: %w", tlsClientCAFile, err)
|
||||
}
|
||||
if !caPool.AppendCertsFromPEM(data) {
|
||||
return nil, xerrors.Errorf("failed to parse CA certificate in tls-client-ca-file")
|
||||
return xerrors.Errorf("failed to parse CA certificate in tls-client-ca-file")
|
||||
}
|
||||
tlsConfig.ClientCAs = caPool
|
||||
}
|
||||
|
||||
return tlsConfig, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
//nolint:revive // Ignore flag-parameter: parameter 'allowEveryone' seems to be a control flag, avoid control coupling (revive)
|
||||
@ -1319,20 +1333,27 @@ func startBuiltinPostgres(ctx context.Context, cfg config.Root, logger slog.Logg
|
||||
return connectionURL, ep.Stop, nil
|
||||
}
|
||||
|
||||
func handleOauth2ClientCertificates(ctx context.Context, cfg *codersdk.DeploymentConfig) (context.Context, error) {
|
||||
if cfg.TLS.ClientCertFile.Value != "" && cfg.TLS.ClientKeyFile.Value != "" {
|
||||
certificates, err := loadCertificates([]string{cfg.TLS.ClientCertFile.Value}, []string{cfg.TLS.ClientKeyFile.Value})
|
||||
func configureHTTPClient(ctx context.Context, clientCertFile, clientKeyFile string, tlsClientCAFile string) (context.Context, *http.Client, error) {
|
||||
if clientCertFile != "" && clientKeyFile != "" {
|
||||
certificates, err := loadCertificates([]string{clientCertFile}, []string{clientKeyFile})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return ctx, nil, err
|
||||
}
|
||||
|
||||
return context.WithValue(ctx, oauth2.HTTPClient, &http.Client{
|
||||
tlsClientConfig := &tls.Config{ //nolint:gosec
|
||||
Certificates: certificates,
|
||||
}
|
||||
err = configureCAPool(tlsClientCAFile, tlsClientConfig)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
httpClient := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{ //nolint:gosec
|
||||
Certificates: certificates,
|
||||
},
|
||||
TLSClientConfig: tlsClientConfig,
|
||||
},
|
||||
}), nil
|
||||
}
|
||||
return context.WithValue(ctx, oauth2.HTTPClient, httpClient), httpClient, nil
|
||||
}
|
||||
return ctx, nil
|
||||
return ctx, &http.Client{}, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user