sourcegraph/cmd/frontend/internal/auth/httpheader/testproxy.go
Erik Seliger 7a116a2a15
Merge enterprise and oss frontend (#56476)
This is a big one! Merged the enterprise frontend with the OSS one, to get yet another step closer to getting rid of the enterprise directory :)
2023-09-11 14:16:38 +02:00

54 lines
1.6 KiB
Go

// The testproxy command runs a simple HTTP proxy that wraps a Sourcegraph server running with the
// http-header auth provider to test the authentication HTTP proxy support.
//
// Also see dev/internal/cmd/auth-proxy-http-header for conveniently starting
// up a proxy for multiple users.
//go:build ignore
// +build ignore
package main
import (
"flag"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
)
var (
addr = flag.String("addr", ":4080", "HTTP listen address")
urlStr = flag.String("url", "http://localhost:3080", "proxy origin URL (Sourcegraph HTTP/HTTPS URL)") // CI:LOCALHOST_OK
username = flag.String("username", os.Getenv("USER"), "username to report to Sourcegraph")
usernamePrefix = flag.String("usernamePrefix", "", "prefix to place in front of username in the auth header value")
httpHeader = flag.String("header", "X-Forwarded-User", "name of HTTP header to add to request")
)
func main() {
flag.Parse()
log.SetFlags(0)
url, err := url.Parse(*urlStr)
if err != nil {
log.Fatalf("Error: Invalid -url: %s.", err)
}
if *username == "" {
log.Fatal("Error: No -username specified.")
}
if *httpHeader == "" {
log.Fatal("Error: No -header specified.")
}
headerVal := *usernamePrefix + *username
log.Printf(`Listening on %s, forwarding requests to %s with added header "%s: %s"`, *addr, url, *httpHeader, headerVal)
p := httputil.NewSingleHostReverseProxy(url)
log.Fatalf("Server error: %s.", http.ListenAndServe(*addr, &httputil.ReverseProxy{
Director: func(r *http.Request) {
r.Header.Set(*httpHeader, headerVal)
r.Host = url.Host
p.Director(r)
},
}))
}