Skip to content

Commit d544935

Browse files
committed
Add option for adding CA cert for the IdP
1 parent c9b5a49 commit d544935

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Flags:
1616
--base-url string External URL of this proxy [SAML_PROXY_BASE_URL]
1717
--bind string host:port to bind for serving HTTP [SAML_PROXY_BIND] (default ":8080")
1818
-h, --help help for saml-auth-proxy
19+
--idp-ca-path string Optional path to a CA certificate PEM file for the IdP [SAML_PROXY_IDP_CA_PATH]
1920
--idp-metadata-url string URL of the IdP's metadata XML [SAML_PROXY_IDP_METADATA_URL]
2021
--name-id-mapping string Name of the request header to convey the SAML nameID/subject [SAML_PROXY_NAME_ID_MAPPING]
2122
--new-auth-webhook-url string URL of webhook that will get POST'ed when a new authentication is processed [SAML_PROXY_NEW_AUTH_WEBHOOK_URL]
@@ -24,6 +25,9 @@ Flags:
2425
--version version for saml-auth-proxy
2526
```
2627

28+
The snake-case values, such as `SAML_PROXY_BACKEND_URL`, are the equivalent environment variables
29+
that can be set instead of passing configuration via the command-line.
30+
2731
## Building
2832

2933
With Go 1.11 or newer:

cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ func init() {
2626
rootCmd.Flags().StringVar(&serverConfig.BackendUrl, "backend-url", "", "URL of the backend being proxied")
2727
rootCmd.Flags().StringVar(&serverConfig.NewAuthWebhookUrl, "new-auth-webhook-url", "", "URL of webhook that will get POST'ed when a new authentication is processed")
2828
rootCmd.Flags().StringVar(&serverConfig.IdpMetadataUrl, "idp-metadata-url", "", "URL of the IdP's metadata XML")
29+
rootCmd.Flags().StringVar(&serverConfig.IdpCaFile, "idp-ca-path", "",
30+
"Optional path to a CA certificate PEM file for the IdP")
2931
rootCmd.Flags().StringVar(&serverConfig.SpKeyPath, "sp-key-path", "saml-auth-proxy.key", "Path to the X509 private key PEM file for this SP")
3032
rootCmd.Flags().StringVar(&serverConfig.SpCertPath, "sp-cert-path", "saml-auth-proxy.cert", "Path to the X509 public certificate PEM file for this SP")
3133
rootCmd.Flags().StringToStringVar(&serverConfig.AttributeHeaderMappings, "attribute-header-mappings", nil,

server/server.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"crypto/x509"
77
"github.com/crewjam/saml/samlsp"
88
"github.com/pkg/errors"
9+
"io/ioutil"
910
"log"
1011
"net/http"
1112
"net/url"
@@ -16,6 +17,7 @@ type Config struct {
1617
BaseUrl string
1718
BackendUrl string
1819
IdpMetadataUrl string
20+
IdpCaFile string
1921
SpKeyPath string
2022
SpCertPath string
2123
NameIdHeaderMapping string
@@ -44,12 +46,18 @@ func Start(cfg *Config) error {
4446
return errors.Wrap(err, "Failed to parse base URL")
4547
}
4648

49+
httpClient, err := setupHttpClient(cfg.IdpCaFile)
50+
if err != nil {
51+
return errors.Wrap(err, "Failed to setup HTTP client")
52+
}
53+
4754
samlSP, err := samlsp.New(samlsp.Options{
4855
URL: *rootUrl,
4956
Key: keyPair.PrivateKey.(*rsa.PrivateKey),
5057
Certificate: keyPair.Leaf,
5158
IDPMetadataURL: idpMetadataUrl,
5259
CookieDomain: rootUrl.Hostname(),
60+
HTTPClient: httpClient,
5361
})
5462
if err != nil {
5563
return errors.Wrap(err, "Failed to initialize SP")
@@ -68,3 +76,32 @@ func Start(cfg *Config) error {
6876
log.Printf("Serving requests for %s at %s", cfg.BaseUrl, cfg.Bind)
6977
return http.ListenAndServe(cfg.Bind, nil)
7078
}
79+
80+
func setupHttpClient(idpCaFile string) (*http.Client, error) {
81+
if idpCaFile == "" {
82+
return nil, nil
83+
}
84+
85+
rootCAs, _ := x509.SystemCertPool()
86+
if rootCAs == nil {
87+
rootCAs = x509.NewCertPool()
88+
}
89+
90+
certs, err := ioutil.ReadFile(idpCaFile)
91+
if err != nil {
92+
return nil, errors.Wrap(err, "Failed to read IdP CA file")
93+
}
94+
95+
if ok := rootCAs.AppendCertsFromPEM(certs); !ok {
96+
log.Println("INF No certs appended, using system certs only")
97+
}
98+
99+
config := &tls.Config{
100+
RootCAs: rootCAs,
101+
}
102+
103+
tr := &http.Transport{TLSClientConfig: config}
104+
client := &http.Client{Transport: tr}
105+
106+
return client, nil
107+
}

0 commit comments

Comments
 (0)