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

update the reverse proxy to read workflow id and inject new auth info

This commit is contained in:
Kwonunn 2024-11-24 18:38:27 +01:00 committed by Kwonunn
parent f81731e2d9
commit ce51735d7a

View file

@ -12,6 +12,7 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"regexp"
"time"
"github.com/julienschmidt/httprouter"
@ -40,20 +41,17 @@ type Handler struct {
}
type WorkflowData struct {
repositoryOwner string
repositoryName string
repositoryFullName string
runNumber string
timestamp string
repositoryMAC string
}
func (h *Handler) CreateWorkflowData(owner string, name string, runnumber string, timestamp string) WorkflowData {
repo := owner + "/" + name
mac := computeMac(h.cacheSecret, repo, runnumber, timestamp)
func (h *Handler) CreateWorkflowData(fullName string, runNumber string, timestamp string) WorkflowData {
mac := computeMac(h.cacheSecret, fullName, runNumber, timestamp)
return WorkflowData{
repositoryOwner: owner,
repositoryName: name,
runNumber: runnumber,
repositoryFullName: fullName,
runNumber: runNumber,
timestamp: timestamp,
repositoryMAC: mac,
}
@ -89,12 +87,12 @@ func StartHandler(targetHost string, outboundIP string, port uint16, cacheSecret
}
router := httprouter.New()
router.HandlerFunc("GET", urlBase+"/cache", proxyRequestHandler(proxy))
router.HandlerFunc("POST", urlBase+"/caches", proxyRequestHandler(proxy))
router.HandlerFunc("PATCH", urlBase+"/caches/:id", proxyRequestHandler(proxy))
router.HandlerFunc("POST", urlBase+"/caches/:id", proxyRequestHandler(proxy))
router.HandlerFunc("GET", urlBase+"/artifacts/:id", proxyRequestHandler(proxy))
router.HandlerFunc("POST", urlBase+"/clean", proxyRequestHandler(proxy))
router.HandlerFunc("GET", "/:workflowId"+urlBase+"/cache", proxyRequestHandler(proxy))
router.HandlerFunc("POST", "/:workflowId"+urlBase+"/caches", proxyRequestHandler(proxy))
router.HandlerFunc("PATCH", "/:workflowId"+urlBase+"/caches/:id", proxyRequestHandler(proxy))
router.HandlerFunc("POST", "/:workflowId"+urlBase+"/caches/:id", proxyRequestHandler(proxy))
router.HandlerFunc("GET", "/:workflowId"+urlBase+"/artifacts/:id", proxyRequestHandler(proxy))
router.HandlerFunc("POST", "/:workflowId"+urlBase+"/clean", proxyRequestHandler(proxy))
h.router = router
@ -133,16 +131,20 @@ func (h *Handler) newReverseProxy(targetHost string) (*httputil.ReverseProxy, er
Rewrite: func(r *httputil.ProxyRequest) {
r.SetURL(url)
r.Out.Host = r.In.Host // if desired
h.injectAuth(r)
re := regexp.MustCompile(`/(\w+)/_apis/artifactcache`)
matches := re.FindStringSubmatch(r.In.URL.Path)
id := matches[1]
data := h.workflows[id]
r.Out.Header.Add("Forgejo-Cache-Repo", data.repositoryFullName)
r.Out.Header.Add("Forgejo-Cache-RunNumber", data.runNumber)
r.Out.Header.Add("Forgejo-Cache-Timestamp", data.timestamp)
r.Out.Header.Add("Forgejo-Cache-MAC", data.repositoryMAC)
},
}
return proxy, nil
}
func (h *Handler) injectAuth(r *httputil.ProxyRequest) {
// TODO: re-implement this one
}
func (h *Handler) ExternalURL() string {
// TODO: make the external url configurable if necessary
return fmt.Sprintf("http://%s:%d",
@ -205,5 +207,5 @@ func computeMac(secret, repo, run, ts string) string {
mac.Write([]byte(repo))
mac.Write([]byte(run))
mac.Write([]byte(ts))
return string(mac.Sum(nil))
return hex.EncodeToString(mac.Sum(nil))
}