Skip to content

Commit

Permalink
fix: conflict between enable_ac_key_instance_mangling and disable_htt…
Browse files Browse the repository at this point in the history
…p_ac_validation

Signed-off-by: hyphennn <[email protected]>
  • Loading branch information
hyphennn committed Nov 18, 2024
1 parent a563ac2 commit df86f7e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (h *httpCache) CacheHandler(w http.ResponseWriter, r *http.Request) {
return
}

if h.mangleACKeys && kind == cache.AC {
if h.mangleACKeys && (kind == cache.AC || kind == cache.RAW) {
hash = cache.TransformActionCacheKey(hash, instance, h.accessLogger)
}

Expand Down
57 changes: 57 additions & 0 deletions server/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
Expand Down Expand Up @@ -506,3 +507,59 @@ func TestRemoteReturnsNotFound(t *testing.T) {
t.Errorf("Wrong status code, expected %d, got %d", http.StatusNotFound, statusCode)
}
}

func TestManglingACKeys(t *testing.T) {
cacheDir, err := os.MkdirTemp("", "bazel-remote")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(cacheDir)

blobSize := int64(1024)
cacheSize := blobSize*2 + disk.BlockSize
diskCache, err := disk.New(cacheDir, cacheSize, disk.WithAccessLogger(testutils.NewSilentLogger()))
if err != nil {
t.Fatal(err)
}

h := NewHTTPCache(diskCache, testutils.NewSilentLogger(), testutils.NewSilentLogger(), false, true, false, false, "")
// create a fake http.Request
data, hash := testutils.RandomDataAndHash(blobSize)
err = diskCache.Put(context.Background(), cache.RAW, hash, int64(len(data)), bytes.NewReader(data))
if err != nil {
t.Fatal(err)
}

url, _ := url.Parse(fmt.Sprintf("http://localhost:8080/ac/%s", hash))
reader := bytes.NewReader([]byte{})
body := io.NopCloser(reader)
req := &http.Request{
Method: "GET",
URL: url,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Body: body,
}
statusCode := 0
respWriter := &fakeResponseWriter{
statusCode: &statusCode,
}
h.CacheHandler(respWriter, req)
if statusCode != 0 {
t.Errorf("Wrong status code, expected %d, got %d", 0, statusCode)
}

url, _ = url.Parse(fmt.Sprintf("http://localhost:8080/test-instance/ac/%s", hash))
reader.Reset([]byte{})
body.Close()
body = io.NopCloser(reader)
req.URL = url
req.Body = body
statusCode = 0

h.CacheHandler(respWriter, req)
if statusCode != http.StatusNotFound {
t.Errorf("Wrong status code, expected %d, got %d", http.StatusNotFound, statusCode)
}
}

0 comments on commit df86f7e

Please sign in to comment.