Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit 17871a4

Browse files
sourcegraph-release-botCraig Furman
andauthored
[Backport 5.5.x] fix(appliance): cache authorization status (#64219)
Backport 156aa5a from #64213 Co-authored-by: Craig Furman <[email protected]>
1 parent d24e8fe commit 17871a4

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

internal/appliance/auth.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package appliance
22

33
import (
44
"net/http"
5+
"sync"
56

67
"golang.org/x/crypto/bcrypt"
78
)
@@ -10,14 +11,25 @@ const (
1011
authHeaderName = "admin-password"
1112
)
1213

14+
// The bcrypt operation is expensive, and the frontend calls auth-gated
15+
// endpoints in a tight loop. Caching valid passwords in memory massively
16+
// improves performance.
17+
var authzCache = &sync.Map{}
18+
1319
func (a *Appliance) checkAuthorization(next http.Handler) http.Handler {
1420
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1521
userPass := r.Header.Get(authHeaderName)
22+
if _, ok := authzCache.Load(userPass); ok {
23+
next.ServeHTTP(w, r)
24+
return
25+
}
26+
1627
if err := bcrypt.CompareHashAndPassword(a.adminPasswordBcrypt, []byte(userPass)); err != nil {
1728
a.invalidAdminPasswordResponse(w, r)
1829
return
1930
}
2031

32+
authzCache.Store(userPass, struct{}{})
2133
next.ServeHTTP(w, r)
2234
})
2335
}

0 commit comments

Comments
 (0)