1
0
Fork 0
mirror of https://code.forgejo.org/forgejo/runner.git synced 2025-08-06 17:40:58 +00:00

review: Gusted review

This commit is contained in:
Kwonunn 2025-03-21 13:57:25 +01:00 committed by Kwonunn
parent 9150081892
commit ef94958cd5
4 changed files with 33 additions and 13 deletions

View file

@ -369,6 +369,33 @@ func TestHandler(t *testing.T) {
require.Equal(t, 404, resp.StatusCode)
})
t.Run("get with bad MAC", func(t *testing.T) {
key := strings.ToLower(t.Name())
version := "c19da02a2bd7e77277f1ac29ab45c09b7d46b4ee758284e26bb3045ad11d9d20"
content := make([]byte, 100)
_, err := rand.Read(content)
require.NoError(t, err)
uploadCacheNormally(t, base, key, version, content)
// Perform the request with the custom `httpClient` which will send correct MAC data
resp, err := httpClient.Get(fmt.Sprintf("%s/cache?keys=%s&version=%s", base, key, version))
require.NoError(t, err)
require.Equal(t, 200, resp.StatusCode)
// Perform the same request with incorrect MAC data
req, err := http.NewRequest("GET", fmt.Sprintf("%s/cache?keys=%s&version=%s", base, key, version), nil)
require.NoError(t, err)
req.Header.Set("Forgejo-Cache-Repo", cache_repo)
req.Header.Set("Forgejo-Cache-RunNumber", cache_runnum)
req.Header.Set("Forgejo-Cache-Timestamp", cache_timestamp)
req.Header.Set("Forgejo-Cache-MAC", "33f0e850ba0bdfd2f3e66ff79c1f8004b8226114e3b2e65c229222bb59df0f9d") // ! This is not the correct MAC
req.Header.Set("Forgejo-Cache-Host", handlerExternalUrl)
resp, err = http.Get(fmt.Sprintf("%s/cache?keys=%s&version=%s", base, key, version))
require.NoError(t, err)
require.Equal(t, 403, resp.StatusCode)
})
t.Run("get with multiple keys", func(t *testing.T) {
version := "c19da02a2bd7e77277f1ac29ab45c09b7d46a4ee758284e26bb3045ad11d9d20"
key := strings.ToLower(t.Name())

View file

@ -28,7 +28,7 @@ func (h *Handler) validateMac(rundata cacheproxy.RunData) (string, error) {
if hmac.Equal([]byte(expectedMAC), []byte(rundata.RepositoryMAC)) {
return rundata.RepositoryFullName, nil
}
return rundata.RepositoryFullName, ErrValidation
return "", ErrValidation
}
func validateAge(ts string) bool {

View file

@ -63,7 +63,7 @@ func TestMac(t *testing.T) {
repoName, err := handler.validateMac(rundata)
require.Error(t, err)
require.Equal(t, name, repoName)
require.Equal(t, "", repoName)
})
t.Run("compute correct mac", func(t *testing.T) {

View file

@ -78,7 +78,6 @@ func StartHandler(targetHost string, outboundIP string, port uint16, cacheSecret
h.logger = logger
h.cacheSecret = cacheSecret
// h.runs = make(map[string]RunData)
if outboundIP != "" {
h.outboundIP = outboundIP
@ -125,9 +124,7 @@ func StartHandler(targetHost string, outboundIP string, port uint16, cacheSecret
}
func proxyRequestHandler(proxy *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
proxy.ServeHTTP(w, r)
}
return proxy.ServeHTTP
}
func (h *Handler) newReverseProxy(targetHost string) (*httputil.ReverseProxy, error) {
@ -141,13 +138,12 @@ func (h *Handler) newReverseProxy(targetHost string) (*httputil.ReverseProxy, er
matches := urlRegex.FindStringSubmatch(r.In.URL.Path)
id := matches[1]
data, ok := h.runs.Load(id)
var runData = data.(RunData)
if !ok {
// The ID doesn't exist.
// ! this should probably be handled more gracefully but i can't figure out how
// ! it really shouldn't happen anyway so it's fine for now
h.logger.Warn(fmt.Sprintf("Tried starting a cache proxy with id %s, which does not exist.", id))
return
}
var runData = data.(RunData)
uri := matches[2]
r.SetURL(targetURL)
@ -212,10 +208,7 @@ func (h *Handler) Close() error {
}
if h.listener != nil {
err := h.listener.Close()
if errors.Is(err, net.ErrClosed) {
err = nil
}
if err != nil {
if !errors.Is(err, net.ErrClosed) {
retErr = err
}
h.listener = nil