Skip to content

Commit

Permalink
Support setting USER_HEADER_NAME to tell Faces which header to get a …
Browse files Browse the repository at this point in the history
…username from

Signed-off-by: Flynn <flynn@buoyant.io>
  • Loading branch information
kflynn committed May 2, 2024
1 parent 79f416b commit 7b328d8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion assets/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ <H1>Faces</H1>
let now = new Date().toISOString()
xhr.open("GET", `${this.fetchURL}?row=${this.row}&col=${this.col}&now=${now}`);
xhr.setRequestHeader("Cache-Control", "no-cache, no-store, max-age=0");
xhr.setRequestHeader("X-Faces-User", this.sw.userControl.user);
xhr.setRequestHeader("%%{user_header}", this.sw.userControl.user);

// We must send credentials...
xhr.withCredentials = true
Expand Down
5 changes: 4 additions & 1 deletion pkg/faces/baseserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ type BaseServer struct {
server *http.ServeMux
preHook Hook
postHook Hook
userHeaderName string
}

func NewBaseServer(serverName string) *BaseServer {
Expand Down Expand Up @@ -153,6 +154,7 @@ func (srv *BaseServer) SetupFromEnvironment() {

srv.RegisterCustom("/rl", srv.rlGetHandler)

srv.userHeaderName = utils.StringFromEnv("USER_HEADER_NAME", "X-Faces-User")
srv.hostIP = utils.StringFromEnv("HOST_IP", utils.StringFromEnv("HOSTNAME", "unknown"))

delayBucketsStr := utils.StringFromEnv("DELAY_BUCKETS", "")
Expand Down Expand Up @@ -188,6 +190,7 @@ func (srv *BaseServer) SetupFromEnvironment() {
fmt.Printf("%s %s: latch_fraction %d\n", time.Now().Format(time.RFC3339), srv.Name, srv.latchFraction)
fmt.Printf("%s %s: debug_enabled %v\n", time.Now().Format(time.RFC3339), srv.Name, srv.debugEnabled)
fmt.Printf("%s %s: max_rate %f\n", time.Now().Format(time.RFC3339), srv.Name, srv.maxRate)
fmt.Printf("%s %s: userHeaderName %v\n", time.Now().Format(time.RFC3339), srv.Name, srv.userHeaderName)
}

func (srv *BaseServer) ListenAndServe(port string) {
Expand Down Expand Up @@ -430,7 +433,7 @@ func (srv *BaseServer) standardHeaders(w http.ResponseWriter, r *http.Request, s
}

w.Header().Set("Content-Type", contentType)
w.Header().Set("X-Faces-User", r.Header.Get("x-faces-user"))
w.Header().Set(srv.userHeaderName, r.Header.Get(srv.userHeaderName))
w.Header().Set("User-Agent", r.Header.Get("User-Agent"))
w.Header().Set("X-Faces-Pod", srv.hostIP)
w.WriteHeader(statusCode)
Expand Down
6 changes: 3 additions & 3 deletions pkg/faces/faceserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (srv *FaceServer) makeRequest(user string, userAgent string, service string
}

if !failed {
req.Header.Set("X-Faces-User", user)
req.Header.Set(srv.userHeaderName, user)
req.Header.Set("User-Agent", userAgent)

response, err = http.DefaultClient.Do(req)
Expand Down Expand Up @@ -190,7 +190,7 @@ func (srv *FaceServer) faceGetHandler(r *http.Request, rstat *BaseRequestStatus)
smiley, smileyOK = Smileys.Lookup(Defaults["smiley-ratelimit"])
color = Colors.Lookup(Defaults["color-ratelimit"])
} else {
user := r.Header.Get("X-Faces-User")
user := r.Header.Get(srv.userHeaderName)

if user == "" {
user = "unknown"
Expand Down Expand Up @@ -224,7 +224,7 @@ func (srv *FaceServer) faceGetHandler(r *http.Request, rstat *BaseRequestStatus)

if srv.debugEnabled {
fmt.Printf("%s %s: mapped smiley %d to %s (%s, %v)\n",
time.Now().Format(time.RFC3339), srv.Name, smileyResp.statusCode, mapped, smiley, smileyOK)
time.Now().Format(time.RFC3339), srv.Name, smileyResp.statusCode, mapped, smiley, smileyOK)
}
} else {
smiley = smileyResp.data
Expand Down
14 changes: 10 additions & 4 deletions pkg/faces/guiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ func (srv *GUIServer) guiGetHandler(w http.ResponseWriter, r *http.Request) {
start := time.Now()

fmt.Printf("%s %s: GET %s\n", time.Now().Format(time.RFC3339), srv.Name, r.URL.Path)
fmt.Printf(" userHeaderName: %s\n", srv.userHeaderName)
fmt.Printf(" headers: %s\n", r.Header)

user := r.Header.Get("x-faces-user")
user := r.Header.Get(srv.userHeaderName)
if user == "" {
user = "unknown"
}
Expand All @@ -81,6 +83,9 @@ func (srv *GUIServer) guiGetHandler(w http.ResponseWriter, r *http.Request) {
userAgent = "unknown"
}

fmt.Printf(" user: %s\n", user)
fmt.Printf(" userAgent: %s\n", userAgent)

podID := srv.hostIP
rcode := http.StatusNotFound
rtext := fmt.Sprintf("%s not found", r.URL.Path)
Expand All @@ -92,7 +97,7 @@ func (srv *GUIServer) guiGetHandler(w http.ResponseWriter, r *http.Request) {
latencyMs := end.Sub(start).Milliseconds()

w.Header().Set("Content-Type", "text/plain")
w.Header().Set("X-Faces-User", user)
w.Header().Set(srv.userHeaderName, user)
w.Header().Set("X-Faces-User-Agent", userAgent)
w.Header().Set("X-Faces-Latency", strconv.FormatInt(latencyMs, 10))
w.Header().Set("X-Faces-Pod", podID)
Expand All @@ -118,6 +123,7 @@ func (srv *GUIServer) guiGetHandler(w http.ResponseWriter, r *http.Request) {
rtext = strings.ReplaceAll(rtext, "%%{hide_key}", fmt.Sprintf("%v", srv.hideKey))
rtext = strings.ReplaceAll(rtext, "%%{show_pods}", fmt.Sprintf("%v", srv.showPods))
rtext = strings.ReplaceAll(rtext, "%%{user}", user)
rtext = strings.ReplaceAll(rtext, "%%{user_header}", fmt.Sprintf("%v", srv.userHeaderName))
rtext = strings.ReplaceAll(rtext, "%%{user_agent}", userAgent)
}
} else if strings.HasPrefix(r.URL.Path, "/face/") {
Expand All @@ -127,7 +133,7 @@ func (srv *GUIServer) guiGetHandler(w http.ResponseWriter, r *http.Request) {
reqStart := time.Now()

url := fmt.Sprintf("http://face/%s", r.URL.Path[6:])
user := r.Header.Get("x-faces-user")
user := r.Header.Get(srv.userHeaderName)
if user == "" {
user = "unknown"
}
Expand Down Expand Up @@ -202,7 +208,7 @@ func (srv *GUIServer) guiGetHandler(w http.ResponseWriter, r *http.Request) {
latencyMs := end.Sub(start).Milliseconds()

w.Header().Set("Content-Type", rtype)
w.Header().Set("X-Faces-User", user)
w.Header().Set(srv.userHeaderName, user)
w.Header().Set("X-Faces-User-Agent", userAgent)
w.Header().Set("X-Faces-Latency", strconv.FormatInt(latencyMs, 10))
w.Header().Set("X-Faces-Pod", podID)
Expand Down

0 comments on commit 7b328d8

Please sign in to comment.