@@ -207,6 +207,16 @@ func Server(vip *viper.Viper, newAPI func(context.Context, *coderd.Options) (*co
207
207
listener = tls .NewListener (listener , tlsConfig )
208
208
}
209
209
210
+ ctx , httpClient , err := configureHTTPClient (
211
+ ctx ,
212
+ cfg .TLS .ClientCertFile .Value ,
213
+ cfg .TLS .ClientKeyFile .Value ,
214
+ cfg .TLS .ClientCAFile .Value ,
215
+ )
216
+ if err != nil {
217
+ return xerrors .Errorf ("configure http client: %w" , err )
218
+ }
219
+
210
220
tcpAddr , valid := listener .Addr ().(* net.TCPAddr )
211
221
if ! valid {
212
222
return xerrors .New ("must be listening on tcp" )
@@ -377,6 +387,7 @@ func Server(vip *viper.Viper, newAPI func(context.Context, *coderd.Options) (*co
377
387
DeploymentConfig : cfg ,
378
388
PrometheusRegistry : prometheus .NewRegistry (),
379
389
APIRateLimit : cfg .APIRateLimit .Value ,
390
+ HTTPClient : httpClient ,
380
391
}
381
392
if tlsConfig != nil {
382
393
options .TLSCertificates = tlsConfig .Certificates
@@ -424,11 +435,6 @@ func Server(vip *viper.Viper, newAPI func(context.Context, *coderd.Options) (*co
424
435
return xerrors .Errorf ("OIDC issuer URL must be set!" )
425
436
}
426
437
427
- ctx , err := handleOauth2ClientCertificates (ctx , cfg )
428
- if err != nil {
429
- return xerrors .Errorf ("configure oidc client certificates: %w" , err )
430
- }
431
-
432
438
if cfg .OIDC .IgnoreEmailVerified .Value {
433
439
logger .Warn (ctx , "coder will not check email_verified for OIDC logins" )
434
440
}
@@ -1088,19 +1094,27 @@ func configureTLS(tlsMinVersion, tlsClientAuth string, tlsCertFiles, tlsKeyFiles
1088
1094
return nil , nil //nolint:nilnil
1089
1095
}
1090
1096
1097
+ err = configureCAPool (tlsClientCAFile , tlsConfig )
1098
+ if err != nil {
1099
+ return nil , err
1100
+ }
1101
+
1102
+ return tlsConfig , nil
1103
+ }
1104
+
1105
+ func configureCAPool (tlsClientCAFile string , tlsConfig * tls.Config ) error {
1091
1106
if tlsClientCAFile != "" {
1092
1107
caPool := x509 .NewCertPool ()
1093
1108
data , err := os .ReadFile (tlsClientCAFile )
1094
1109
if err != nil {
1095
- return nil , xerrors .Errorf ("read %q: %w" , tlsClientCAFile , err )
1110
+ return xerrors .Errorf ("read %q: %w" , tlsClientCAFile , err )
1096
1111
}
1097
1112
if ! caPool .AppendCertsFromPEM (data ) {
1098
- return nil , xerrors .Errorf ("failed to parse CA certificate in tls-client-ca-file" )
1113
+ return xerrors .Errorf ("failed to parse CA certificate in tls-client-ca-file" )
1099
1114
}
1100
1115
tlsConfig .ClientCAs = caPool
1101
1116
}
1102
-
1103
- return tlsConfig , nil
1117
+ return nil
1104
1118
}
1105
1119
1106
1120
//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
1319
1333
return connectionURL , ep .Stop , nil
1320
1334
}
1321
1335
1322
- func handleOauth2ClientCertificates (ctx context.Context , cfg * codersdk. DeploymentConfig ) (context.Context , error ) {
1323
- if cfg . TLS . ClientCertFile . Value != "" && cfg . TLS . ClientKeyFile . Value != "" {
1324
- certificates , err := loadCertificates ([]string {cfg . TLS . ClientCertFile . Value }, []string {cfg . TLS . ClientKeyFile . Value })
1336
+ func configureHTTPClient (ctx context.Context , clientCertFile , clientKeyFile string , tlsClientCAFile string ) (context.Context , * http. Client , error ) {
1337
+ if clientCertFile != "" && clientKeyFile != "" {
1338
+ certificates , err := loadCertificates ([]string {clientCertFile }, []string {clientKeyFile })
1325
1339
if err != nil {
1326
- return nil , err
1340
+ return ctx , nil , err
1327
1341
}
1328
1342
1329
- return context .WithValue (ctx , oauth2 .HTTPClient , & http.Client {
1343
+ tlsClientConfig := & tls.Config { //nolint:gosec
1344
+ Certificates : certificates ,
1345
+ }
1346
+ err = configureCAPool (tlsClientCAFile , tlsClientConfig )
1347
+ if err != nil {
1348
+ return nil , nil , err
1349
+ }
1350
+
1351
+ httpClient := & http.Client {
1330
1352
Transport : & http.Transport {
1331
- TLSClientConfig : & tls.Config { //nolint:gosec
1332
- Certificates : certificates ,
1333
- },
1353
+ TLSClientConfig : tlsClientConfig ,
1334
1354
},
1335
- }), nil
1355
+ }
1356
+ return context .WithValue (ctx , oauth2 .HTTPClient , httpClient ), httpClient , nil
1336
1357
}
1337
- return ctx , nil
1358
+ return ctx , & http. Client {}, nil
1338
1359
}
0 commit comments