@@ -22,6 +22,8 @@ package proxy
22
22
23
23
import (
24
24
"context"
25
+ "crypto/tls"
26
+ "crypto/x509"
25
27
"io/ioutil"
26
28
"net/http"
27
29
"net/url"
@@ -88,7 +90,28 @@ func (d *Proxy) RoundTrip(r *http.Request) (*http.Response, error) {
88
90
Header : rw .header ,
89
91
}, nil
90
92
} else if err == nil {
91
- res , err := http .DefaultTransport .RoundTrip (r )
93
+
94
+ transport := http .DefaultTransport
95
+
96
+ // FIXME: Test config settings for extended Root CA cert file
97
+ if true {
98
+ d .r .Logger ().
99
+ WithFields (fields ).
100
+ WithField ("insecure-skip-verify" , false ).
101
+ Warn ("Using extended Root CA" )
102
+
103
+ transport , err = useTransportWithExtendedRootCa ("./config/certs/hydra/private.crt" ) // FIXME: Read from a config somehow...
104
+ if err != nil {
105
+ d .r .Logger ().
106
+ WithError (errors .WithStack (err )).
107
+ WithField ("granted" , false ).
108
+ WithFields (fields ).
109
+ Warn ("Access request denied because extended Root CA failed" )
110
+ return nil , err
111
+ }
112
+ }
113
+
114
+ res , err := transport .RoundTrip (r )
92
115
if err != nil {
93
116
d .r .Logger ().
94
117
WithError (errors .WithStack (err )).
@@ -194,3 +217,33 @@ func ConfigureBackendURL(r *http.Request, rl *rule.Rule) error {
194
217
195
218
return nil
196
219
}
220
+
221
+ // Allow for extending the Root CA chain
222
+ // Use to avoid the error: "http: proxy error: x509: certificate signed by unknown authority" for self-signed
223
+ // certificates upstream.
224
+ func useTransportWithExtendedRootCa (certFile string ) (transport * http.Transport , err error ) {
225
+ transport = & (* http .DefaultTransport .(* http.Transport )) // shallow copy
226
+
227
+ // Get the SystemCertPool or continue with an empty pool on error
228
+ rootCAs , err := x509 .SystemCertPool ()
229
+ if err != nil {
230
+ return nil , err
231
+ }
232
+
233
+ certs , err := ioutil .ReadFile (certFile )
234
+ if err != nil {
235
+ return nil , err
236
+ }
237
+
238
+ // Append our cert to the system pool
239
+ if ok := rootCAs .AppendCertsFromPEM (certs ); ! ok {
240
+ return nil , errors .New ("No certs appended, only system certs present, did you specifi the correct cert file?" )
241
+ }
242
+
243
+ transport .TLSClientConfig = & tls.Config {
244
+ InsecureSkipVerify : false ,
245
+ RootCAs : rootCAs ,
246
+ }
247
+
248
+ return transport , nil
249
+ }
0 commit comments