2323package route
2424
2525import (
26+ "crypto/tls"
2627 "encoding/base64"
2728 "encoding/json"
2829 "errors"
@@ -38,6 +39,7 @@ import (
3839 "github.com/labstack/echo/v4"
3940 "github.com/temporalio/ui-server/v2/server/auth"
4041 "github.com/temporalio/ui-server/v2/server/config"
42+ "github.com/temporalio/ui-server/v2/server/rpc"
4143 "golang.org/x/net/context"
4244 "golang.org/x/oauth2"
4345)
@@ -75,22 +77,40 @@ func SetAuthRoutes(e *echo.Echo, cfgProvider *config.ConfigProviderWithRefresh)
7577 ctx := context .Background ()
7678 serverCfg , err := cfgProvider .GetConfig ()
7779 if err != nil {
78- fmt .Printf ("unable to get auth config: %s\n " , err )
80+ log .Printf ("unable to get auth config: %s\n " , err )
81+ return
7982 }
8083
8184 if ! serverCfg .Auth .Enabled {
8285 return
8386 }
8487
85- if len (serverCfg .Auth .Providers ) == 0 {
86- log .Fatal (`auth providers configuration is empty. Configure an auth provider or disable auth` )
88+ err = validateAuthConfig (& serverCfg .Auth )
89+ if err != nil {
90+ log .Fatalf ("invalid auth config: %s\n " , err )
8791 }
8892
8993 providerCfg := serverCfg .Auth .Providers [0 ] // only single provider is currently supported
9094
9195 if len (providerCfg .IssuerUrl ) > 0 {
9296 ctx = oidc .InsecureIssuerURLContext (ctx , providerCfg .IssuerUrl )
9397 }
98+
99+ // Configure HTTP client (with timeout) and optional custom CA if provided via caFile or caData
100+ httpClient := & http.Client {
101+ Timeout : 30 * time .Second ,
102+ }
103+ if providerCfg .CaFile != "" || providerCfg .CaData != "" {
104+ caCertPool , err := rpc .LoadCACert (providerCfg .CaFile , providerCfg .CaData )
105+ if err != nil {
106+ log .Fatalf ("Unable to load auth CA certificate: %s\n " , err )
107+ }
108+ transport := http .DefaultTransport .(* http.Transport ).Clone ()
109+ transport .TLSClientConfig = & tls.Config {RootCAs : caCertPool }
110+ httpClient .Transport = transport
111+ }
112+ ctx = oidc .ClientContext (ctx , httpClient )
113+
94114 provider , err := oidc .NewProvider (ctx , providerCfg .ProviderURL )
95115 if err != nil {
96116 log .Fatal (err )
@@ -368,3 +388,16 @@ type Nonce struct {
368388 Nonce string `json:"nonce"`
369389 ReturnURL string `json:"return_url"`
370390}
391+
392+ func validateAuthConfig (cfg * config.Auth ) error {
393+ if len (cfg .Providers ) == 0 {
394+ return fmt .Errorf (`auth providers configuration is empty. Configure an auth provider or disable auth` )
395+ }
396+ for _ , providerCfg := range cfg .Providers {
397+ if providerCfg .CaFile != "" && providerCfg .CaData != "" {
398+ return fmt .Errorf ("cannot specify Auth CA file and CA data at the same time" )
399+ }
400+ }
401+
402+ return nil
403+ }
0 commit comments