2323package route
2424
2525import (
26+ "crypto/tls"
2627 "encoding/base64"
2728 "encoding/json"
2829 "errors"
@@ -37,6 +38,7 @@ import (
3738 "github.com/labstack/echo/v4"
3839 "github.com/temporalio/ui-server/v2/server/auth"
3940 "github.com/temporalio/ui-server/v2/server/config"
41+ "github.com/temporalio/ui-server/v2/server/rpc"
4042 "golang.org/x/net/context"
4143 "golang.org/x/oauth2"
4244)
@@ -46,22 +48,40 @@ func SetAuthRoutes(e *echo.Echo, cfgProvider *config.ConfigProviderWithRefresh)
4648 ctx := context .Background ()
4749 serverCfg , err := cfgProvider .GetConfig ()
4850 if err != nil {
49- fmt .Printf ("unable to get auth config: %s\n " , err )
51+ log .Printf ("unable to get auth config: %s\n " , err )
52+ return
5053 }
5154
5255 if ! serverCfg .Auth .Enabled {
5356 return
5457 }
5558
56- if len (serverCfg .Auth .Providers ) == 0 {
57- log .Fatal (`auth providers configuration is empty. Configure an auth provider or disable auth` )
59+ err = validateAuthConfig (& serverCfg .Auth )
60+ if err != nil {
61+ log .Fatalf ("invalid auth config: %s\n " , err )
5862 }
5963
6064 providerCfg := serverCfg .Auth .Providers [0 ] // only single provider is currently supported
6165
6266 if len (providerCfg .IssuerUrl ) > 0 {
6367 ctx = oidc .InsecureIssuerURLContext (ctx , providerCfg .IssuerUrl )
6468 }
69+
70+ // Configure HTTP client (with timeout) and optional custom CA if provided via caFile or caData
71+ httpClient := & http.Client {
72+ Timeout : 30 * time .Second ,
73+ }
74+ if providerCfg .CaFile != "" || providerCfg .CaData != "" {
75+ caCertPool , err := rpc .LoadCACert (providerCfg .CaFile , providerCfg .CaData )
76+ if err != nil {
77+ log .Fatalf ("Unable to load auth CA certificate: %s\n " , err )
78+ }
79+ httpClient .Transport = & http.Transport {
80+ TLSClientConfig : & tls.Config {RootCAs : caCertPool },
81+ }
82+ }
83+ ctx = oidc .ClientContext (ctx , httpClient )
84+
6585 provider , err := oidc .NewProvider (ctx , providerCfg .ProviderURL )
6686 if err != nil {
6787 log .Fatal (err )
@@ -231,3 +251,16 @@ type Nonce struct {
231251 Nonce string `json:"nonce"`
232252 ReturnURL string `json:"return_url"`
233253}
254+
255+ func validateAuthConfig (cfg * config.Auth ) error {
256+ if len (cfg .Providers ) == 0 {
257+ return fmt .Errorf (`auth providers configuration is empty. Configure an auth provider or disable auth` )
258+ }
259+ for _ , providerCfg := range cfg .Providers {
260+ if providerCfg .CaFile != "" && providerCfg .CaData != "" {
261+ return fmt .Errorf ("cannot specify Auth CA file and CA data at the same time" )
262+ }
263+ }
264+
265+ return nil
266+ }
0 commit comments