Skip to content

Commit 818510e

Browse files
wraixchristian-roggia
authored andcommitted
feat: Extended Root CA for upstream connections (ory#181)
This allows for appending a certificate file to the Root CA without altering the system Root CA. This is useful for allowing self-signed certificates on the upstream connections
1 parent 03c6bd0 commit 818510e

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

proxy/proxy.go

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ package proxy
2222

2323
import (
2424
"context"
25+
"crypto/tls"
26+
"crypto/x509"
2527
"io/ioutil"
2628
"net/http"
2729
"net/url"
@@ -88,7 +90,28 @@ func (d *Proxy) RoundTrip(r *http.Request) (*http.Response, error) {
8890
Header: rw.header,
8991
}, nil
9092
} 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)
92115
if err != nil {
93116
d.r.Logger().
94117
WithError(errors.WithStack(err)).
@@ -194,3 +217,33 @@ func ConfigureBackendURL(r *http.Request, rl *rule.Rule) error {
194217

195218
return nil
196219
}
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

Comments
 (0)