Skip to content

Commit ff83eb2

Browse files
committed
Add option to pass nameID as a header
Serves /_health endpoint
1 parent 5442a51 commit ff83eb2

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ func init() {
3030
rootCmd.Flags().StringVar(&serverConfig.SpCertPath, "sp-cert-path", "saml-auth-proxy.cert", "Path to the X509 public certificate PEM file for this SP")
3131
rootCmd.Flags().StringToStringVar(&serverConfig.AttributeHeaderMappings, "attribute-header-mappings", nil,
3232
"Comma separated list of attribute=header pairs mapping SAML IdP response attributes to forwarded request header")
33+
rootCmd.Flags().StringVar(&serverConfig.NameIdHeaderMapping, "name-id-mapping", "",
34+
"Name of the request header to convey the SAML nameID/subject")
3335

3436
_ = rootCmd.MarkFlagRequired("base-url")
3537
_ = rootCmd.MarkFlagRequired("backend-url")

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ require (
77
github.com/inconshreveable/mousetrap v1.0.0 // indirect
88
github.com/jamiealquiza/envy v1.1.0
99
github.com/jonboulle/clockwork v0.1.0 // indirect
10+
github.com/kr/pretty v0.1.0 // indirect
1011
github.com/patrickmn/go-cache v2.1.0+incompatible
1112
github.com/pkg/errors v0.8.1
1213
github.com/russellhaering/goxmldsig v0.0.0-20180430223755-7acd5e4a6ef7 // indirect
1314
github.com/spf13/cobra v0.0.3
1415
github.com/spf13/pflag v1.0.3 // indirect
16+
github.com/stretchr/testify v1.3.0 // indirect
1517
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67 // indirect
18+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
1619
)

go.sum

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ github.com/beevik/etree v1.1.0 h1:T0xke/WvNtMoCqgzPhkX2r4rjY3GDZFi+FjpRZY2Jbs=
22
github.com/beevik/etree v1.1.0/go.mod h1:r8Aw8JqVegEf0w2fDnATrX9VpkMcyFeM0FhwO62wh+A=
33
github.com/crewjam/saml v0.0.0-20180831135026-ebc5f787b786 h1:8OVABJfT9iJh/uHeYlk1HWugxt7j50JPwW2uLOV9Yqs=
44
github.com/crewjam/saml v0.0.0-20180831135026-ebc5f787b786/go.mod h1:w5eu+HNtubx+kRpQL6QFT2F3yIFfYVe6+EzOFVU7Hko=
5+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
6+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
57
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
68
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
79
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
@@ -10,15 +12,27 @@ github.com/jamiealquiza/envy v1.1.0 h1:Nwh4wqTZ28gDA8zB+wFkhnUpz3CEcO12zotjeqqRo
1012
github.com/jamiealquiza/envy v1.1.0/go.mod h1:MP36BriGCLwEHhi1OU8E9569JNZrjWfCvzG7RsPnHus=
1113
github.com/jonboulle/clockwork v0.1.0 h1:VKV+ZcuP6l3yW9doeqz6ziZGgcynBVQO+obU0+0hcPo=
1214
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
15+
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
16+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
17+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
18+
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
19+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
1320
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
1421
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
1522
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
1623
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
24+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
25+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1726
github.com/russellhaering/goxmldsig v0.0.0-20180430223755-7acd5e4a6ef7 h1:J4AOUcOh/t1XbQcJfkEqhzgvMJ2tDxdCVvmHxW5QXao=
1827
github.com/russellhaering/goxmldsig v0.0.0-20180430223755-7acd5e4a6ef7/go.mod h1:Oz4y6ImuOQZxynhbSXk7btjEfNBtGlj2dcaOvXl2FSM=
1928
github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8=
2029
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
2130
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
2231
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
32+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
33+
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
34+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
2335
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67 h1:ng3VDlRp5/DHpSWl02R4rM9I+8M2rhmsuLwAMmkLQWE=
2436
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
37+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
38+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

server/proxy.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ func NewProxy(cfg *Config) (*proxy, error) {
4242
return proxy, nil
4343
}
4444

45+
func (p *proxy) health(respOutWriter http.ResponseWriter, reqIn *http.Request) {
46+
respOutWriter.Header().Set("Content-Type", "text/plain")
47+
respOutWriter.WriteHeader(200)
48+
respOutWriter.Write([]byte("OK"))
49+
}
50+
4551
func (p *proxy) handler(respOutWriter http.ResponseWriter, reqIn *http.Request) {
4652

4753
resolved, err := p.backendUrl.Parse(reqIn.URL.Path)
@@ -57,14 +63,20 @@ func (p *proxy) handler(respOutWriter http.ResponseWriter, reqIn *http.Request)
5763

5864
authToken := samlsp.Token(reqIn.Context())
5965

66+
copyHeaders(reqOut.Header, reqIn.Header)
67+
6068
p.checkForNewAuth(authToken)
6169

6270
if p.config.AttributeHeaderMappings != nil {
6371
for attr, hdr := range p.config.AttributeHeaderMappings {
6472
reqOut.Header.Set(hdr, authToken.Attributes.Get(attr))
6573
}
6674
}
67-
copyHeaders(reqOut.Header, reqIn.Header)
75+
if p.config.NameIdHeaderMapping != "" {
76+
reqOut.Header.Set(p.config.NameIdHeaderMapping,
77+
authToken.StandardClaims.Subject)
78+
}
79+
6880
reqOut.Header.Set("X-Forwarded-Host", reqIn.Host)
6981
remoteHost, _, err := net.SplitHostPort(reqIn.RemoteAddr)
7082
if err == nil {

server/server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Config struct {
1818
IdpMetadataUrl string
1919
SpKeyPath string
2020
SpCertPath string
21+
NameIdHeaderMapping string
2122
AttributeHeaderMappings map[string]string
2223
NewAuthWebhookUrl string
2324
}
@@ -48,6 +49,7 @@ func Start(cfg *Config) error {
4849
Key: keyPair.PrivateKey.(*rsa.PrivateKey),
4950
Certificate: keyPair.Leaf,
5051
IDPMetadataURL: idpMetadataUrl,
52+
CookieDomain: rootUrl.Hostname(),
5153
})
5254
if err != nil {
5355
return errors.Wrap(err, "Failed to initialize SP")
@@ -60,6 +62,7 @@ func Start(cfg *Config) error {
6062

6163
app := http.HandlerFunc(proxy.handler)
6264
http.Handle("/saml/", samlSP)
65+
http.Handle("/_health", http.HandlerFunc(proxy.health))
6366
http.Handle("/", samlSP.RequireAccount(app))
6467

6568
log.Printf("Serving requests for %s at %s", cfg.BaseUrl, cfg.Bind)

0 commit comments

Comments
 (0)