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"
"net/http/httputil" "net/http/httputil"
"net/url" "net/url"
"regexp"
"time" "time"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
@ -40,22 +41,19 @@ type Handler struct {
} }
type WorkflowData struct { type WorkflowData struct {
repositoryOwner string repositoryFullName string
repositoryName string runNumber string
runNumber string timestamp string
timestamp string repositoryMAC string
repositoryMAC string
} }
func (h *Handler) CreateWorkflowData(owner string, name string, runnumber string, timestamp string) WorkflowData { func (h *Handler) CreateWorkflowData(fullName string, runNumber string, timestamp string) WorkflowData {
repo := owner + "/" + name mac := computeMac(h.cacheSecret, fullName, runNumber, timestamp)
mac := computeMac(h.cacheSecret, repo, runnumber, timestamp)
return WorkflowData{ return WorkflowData{
repositoryOwner: owner, repositoryFullName: fullName,
repositoryName: name, runNumber: runNumber,
runNumber: runnumber, timestamp: timestamp,
timestamp: timestamp, repositoryMAC: mac,
repositoryMAC: mac,
} }
} }
@ -89,12 +87,12 @@ func StartHandler(targetHost string, outboundIP string, port uint16, cacheSecret
} }
router := httprouter.New() router := httprouter.New()
router.HandlerFunc("GET", urlBase+"/cache", proxyRequestHandler(proxy)) router.HandlerFunc("GET", "/:workflowId"+urlBase+"/cache", proxyRequestHandler(proxy))
router.HandlerFunc("POST", urlBase+"/caches", proxyRequestHandler(proxy)) router.HandlerFunc("POST", "/:workflowId"+urlBase+"/caches", proxyRequestHandler(proxy))
router.HandlerFunc("PATCH", urlBase+"/caches/:id", proxyRequestHandler(proxy)) router.HandlerFunc("PATCH", "/:workflowId"+urlBase+"/caches/:id", proxyRequestHandler(proxy))
router.HandlerFunc("POST", urlBase+"/caches/:id", proxyRequestHandler(proxy)) router.HandlerFunc("POST", "/:workflowId"+urlBase+"/caches/:id", proxyRequestHandler(proxy))
router.HandlerFunc("GET", urlBase+"/artifacts/:id", proxyRequestHandler(proxy)) router.HandlerFunc("GET", "/:workflowId"+urlBase+"/artifacts/:id", proxyRequestHandler(proxy))
router.HandlerFunc("POST", urlBase+"/clean", proxyRequestHandler(proxy)) router.HandlerFunc("POST", "/:workflowId"+urlBase+"/clean", proxyRequestHandler(proxy))
h.router = router h.router = router
@ -133,16 +131,20 @@ func (h *Handler) newReverseProxy(targetHost string) (*httputil.ReverseProxy, er
Rewrite: func(r *httputil.ProxyRequest) { Rewrite: func(r *httputil.ProxyRequest) {
r.SetURL(url) r.SetURL(url)
r.Out.Host = r.In.Host // if desired 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 return proxy, nil
} }
func (h *Handler) injectAuth(r *httputil.ProxyRequest) {
// TODO: re-implement this one
}
func (h *Handler) ExternalURL() string { func (h *Handler) ExternalURL() string {
// TODO: make the external url configurable if necessary // TODO: make the external url configurable if necessary
return fmt.Sprintf("http://%s:%d", 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(repo))
mac.Write([]byte(run)) mac.Write([]byte(run))
mac.Write([]byte(ts)) mac.Write([]byte(ts))
return string(mac.Sum(nil)) return hex.EncodeToString(mac.Sum(nil))
} }