Skip to content

Commit a85c6ac

Browse files
authored
Merge pull request #843 from kelos-dev/ghproxy-drop-auth-cache-key
Remove Authorization from ghproxy cache key for cross-pod deduplication
2 parents 4c7ba61 + 698a7fb commit a85c6ac

File tree

2 files changed

+15
-32
lines changed

2 files changed

+15
-32
lines changed

cmd/ghproxy/main.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package main
22

33
import (
4-
"crypto/sha256"
5-
"encoding/hex"
64
"flag"
75
"fmt"
86
"io"
@@ -67,23 +65,13 @@ func newProxy(allowed []string, cacheTTL time.Duration) *proxy {
6765
}
6866
}
6967

70-
// cacheKey returns a key that includes the upstream and the request path+query
71-
// and varies by headers that can affect the upstream response.
72-
func cacheKey(upstream, pathAndQuery, accept, authorization string) string {
73-
return strings.Join([]string{
74-
upstream,
75-
pathAndQuery,
76-
accept,
77-
authorizationKey(authorization),
78-
}, "|")
79-
}
80-
81-
func authorizationKey(authorization string) string {
82-
if authorization == "" {
83-
return ""
84-
}
85-
sum := sha256.Sum256([]byte(authorization))
86-
return hex.EncodeToString(sum[:])
68+
// cacheKey returns a key that includes the upstream, request path+query, and
69+
// Accept header so that the same path on different upstreams or with different
70+
// content types is cached separately. Authorization is intentionally excluded
71+
// so that spawners with different tokens share cached responses for the same
72+
// repo, enabling cross-pod deduplication.
73+
func cacheKey(upstream, pathAndQuery, accept string) string {
74+
return upstream + "|" + pathAndQuery + "|" + accept
8775
}
8876

8977
// rewriteLinkHeader rewrites absolute URLs in a Link header, replacing the
@@ -143,7 +131,7 @@ func (p *proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
143131
scheme = "https"
144132
}
145133
proxyBase := scheme + "://" + r.Host
146-
key := cacheKey(upstream, r.URL.RequestURI(), r.Header.Get("Accept"), r.Header.Get("Authorization"))
134+
key := cacheKey(upstream, r.URL.RequestURI(), r.Header.Get("Accept"))
147135
var entry *cacheEntry
148136
if r.Method == http.MethodGet {
149137
p.mu.RLock()

cmd/ghproxy/main_test.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ func TestProxy_PassesThroughNonGET(t *testing.T) {
190190

191191
func TestProxy_DefaultUpstream(t *testing.T) {
192192
// Verify that the cache key includes the default upstream.
193-
key := cacheKey(defaultUpstream, "/repos/owner/repo", "", "")
194-
if key != "https://api.github.com|/repos/owner/repo||" {
193+
key := cacheKey(defaultUpstream, "/repos/owner/repo", "")
194+
if key != "https://api.github.com|/repos/owner/repo|" {
195195
t.Fatalf("unexpected cache key: %s", key)
196196
}
197197
}
@@ -319,21 +319,16 @@ func TestProxy_AllowsConfiguredUpstream(t *testing.T) {
319319
}
320320
}
321321

322-
func TestCacheKeyVariesByAcceptAndAuthorization(t *testing.T) {
323-
key1 := cacheKey(defaultUpstream, "/repos/o/r/issues", "application/json", "token one")
324-
key2 := cacheKey(defaultUpstream, "/repos/o/r/issues", "application/vnd.github.raw+json", "token one")
325-
key3 := cacheKey(defaultUpstream, "/repos/o/r/issues", "application/json", "token two")
322+
func TestCacheKeyVariesByAcceptNotAuthorization(t *testing.T) {
323+
key1 := cacheKey(defaultUpstream, "/repos/o/r/issues", "application/json")
324+
key2 := cacheKey(defaultUpstream, "/repos/o/r/issues", "application/vnd.github.raw+json")
326325

327326
if key1 == key2 {
328327
t.Fatal("expected cache key to vary by Accept header")
329328
}
330-
if key1 == key3 {
331-
t.Fatal("expected cache key to vary by Authorization header")
329+
if key1 != cacheKey(defaultUpstream, "/repos/o/r/issues", "application/json") {
330+
t.Fatal("expected cache key to be stable for identical inputs")
332331
}
333-
if key1 == cacheKey(defaultUpstream, "/repos/o/r/issues", "application/json", "token one") {
334-
return
335-
}
336-
t.Fatal("expected cache key to be stable for identical inputs")
337332
}
338333

339334
func TestRewriteLinkHeader(t *testing.T) {

0 commit comments

Comments
 (0)