Skip to content

Commit

Permalink
added support for adding a bearer token from a given file for proxy r…
Browse files Browse the repository at this point in the history
…equests
  • Loading branch information
jstrachan committed Jun 28, 2016
1 parent 00dae03 commit 59c7d82
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion kuisp.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Options struct {
TlsKeyFile string
AccessLogging bool
CompressHandler bool
BearerTokenFile string
ServeWww bool
}

Expand All @@ -70,6 +71,7 @@ func initFlags() {
flag.BoolVar(&options.CompressHandler, "compress", false, "Enable gzip/deflate response compression")
flag.BoolVar(&options.FailOnUnknownServices, "fail-on-unknown-services", false, "Fail on unknown services in DNS")
flag.BoolVar(&options.ServeWww, "serve-www", true, "Whether to serve static content")
flag.StringVar(&options.BearerTokenFile, "bearer-token", "", "Specify the file to use as the Bearer token for Authorization header")
flag.Parse()
}

Expand Down Expand Up @@ -119,7 +121,21 @@ func main() {
log.Printf("Creating service proxy: %v => %v\n", serviceDef.prefix, serviceDef.url.String())
rp := httputil.NewSingleHostReverseProxy(serviceDef.url)
rp.Transport = transport
http.Handle(serviceDef.prefix, http.StripPrefix(serviceDef.prefix, rp))
handler := http.StripPrefix(serviceDef.prefix, rp)
if len(options.BearerTokenFile) > 0 {
data, err := ioutil.ReadFile(options.BearerTokenFile)
if err != nil {
log.Fatalf("Could not load Bearer token file %s due to %v", options.BearerTokenFile, err)
}
authHeader := "Bearer " + string(data)
oldHandler := handler
newHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Header.Set("Authorization", authHeader)
oldHandler.ServeHTTP(w, r)
})
handler = newHandler
}
http.Handle(serviceDef.prefix, handler)
}
log.Println()
}
Expand Down

0 comments on commit 59c7d82

Please sign in to comment.