mirror of
https://gitea.com/gitea/act_runner
synced 2026-09-21 19:37:07 +02:00
Cache v2 makes the cache server the `ACTIONS_RESULTS_URL` origin, so artifact calls arrived there and were proxied on to Gitea, failing whenever it could not reach the instance. - Artifact calls are answered with a redirect, so the cache server opens no connection to Gitea. A scheme change or an untrusted instance is still proxied, but there the cache server is the runner itself, which already reaches Gitea. - Failures answer in twirp, not an empty `502` that clients report as `Unexpected end of JSON input`. - `cache.v2: false` really points artifacts at Gitea now. - Cache reservations are bound to the job that made them, so two jobs saving one key cannot commit against each other's upload, and a retry after a lost answer no longer fails a saved entry. - The toolkit patch, which edits the GitHub-host check out of an action's bundle, was left in the shared checkout where a job running with `runner.patch_actions: false` could inherit it. It is put back after the job's copy. - `exec` names an origin for the cache v2 it advertises, and masks its runtime token. Behaviour changes: `no_proxy` no longer exempts `cache.external_server`, and `cache.enabled: false` also stops external registration. Fixes https://gitea.com/gitea/runner/issues/1208 Fixes https://gitea.com/gitea/runner/issues/1211 Assisted by Claude (Opus 5). Reviewed-on: https://gitea.com/gitea/runner/pulls/1216 Reviewed-by: bircni <bircni@icloud.com> Co-authored-by: silverwind <me@silverwind.io>
102 lines
3.8 KiB
Go
102 lines
3.8 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package artifactcache
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httputil"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
// The results service is one origin serving every github.actions.results.api.v1 service, and
|
|
// Gitea implements only the artifact half of it. Forwarding that half from here makes this origin
|
|
// the whole service, so ACTIONS_RESULTS_URL can point at it truthfully, which is what the clients
|
|
// this runner cannot patch need, docker buildx among them.
|
|
//
|
|
// The instance to forward to travels with the job registration rather than with configuration, so
|
|
// a cache server shared between runners serves each of their instances.
|
|
const artifactServicePath = "/twirp/github.actions.results.api.v1.ArtifactService/"
|
|
|
|
// forwardOrNotFound is the router's fallback: the artifact service of the instance the job
|
|
// registered with, and the 404 the router would have written otherwise.
|
|
func (h *Handler) forwardOrNotFound(w http.ResponseWriter, r *http.Request) {
|
|
if !strings.HasPrefix(r.URL.Path, artifactServicePath) {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
cred, ok := h.lookupCredential(bearerToken(r))
|
|
if !ok {
|
|
h.twirpError(w, r, twirpUnauthenticated, errors.New("unknown bearer token"))
|
|
return
|
|
}
|
|
if cred.Results == "" {
|
|
h.twirpError(w, r, twirpInternal, errors.New("no instance is registered for this job"))
|
|
return
|
|
}
|
|
target, err := url.Parse(strings.TrimSuffix(cred.Results, "/"))
|
|
if err == nil && (target.Hostname() == "" || (target.Scheme != "http" && target.Scheme != "https")) {
|
|
err = errors.New("not an absolute http or https URL the job could use")
|
|
}
|
|
if err != nil {
|
|
h.logger.Errorf("artifact service forward to %q: %v", cred.Results, err)
|
|
h.twirpError(w, r, twirpInternal, fmt.Errorf("artifact service address %q is unusable: %w", cred.Results, err))
|
|
return
|
|
}
|
|
if !h.mustProxy(r, cred, target) {
|
|
redirect := target.JoinPath(r.URL.EscapedPath())
|
|
redirect.RawQuery = mergeQuery(target.RawQuery, r.URL.RawQuery)
|
|
h.logger.Debugf("%s %s: redirecting to %s", r.Method, r.URL.Path, target)
|
|
http.Redirect(w, r, redirect.String(), http.StatusTemporaryRedirect)
|
|
return
|
|
}
|
|
h.logger.Debugf("%s %s: forwarding to %s", r.Method, r.URL.Path, target)
|
|
|
|
proxy := &httputil.ReverseProxy{
|
|
Rewrite: func(r *httputil.ProxyRequest) {
|
|
r.SetURL(target)
|
|
// Gitea builds the URLs it hands back from this Host, and their scheme from the
|
|
// connection unless a forwarded header overrides it, so artifact bodies go to Gitea
|
|
// directly and never through here.
|
|
r.Out.Host = target.Host
|
|
},
|
|
ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
|
|
h.logger.Warnf("artifact service forward to %s: %v", target, err)
|
|
h.twirpError(w, r, twirpInternal, fmt.Errorf("cache server cannot reach the artifact service at %s: %w", target, err))
|
|
},
|
|
}
|
|
if cred.InsecureTLS {
|
|
proxy.Transport = insecureTransport
|
|
}
|
|
proxy.ServeHTTP(w, r)
|
|
}
|
|
|
|
// A protocol switch loses the bearer or the agent, and an untrusted instance needs skipped verification.
|
|
func (h *Handler) mustProxy(r *http.Request, cred JobCredential, target *url.URL) bool {
|
|
if cred.InsecureTLS && target.Scheme == "https" {
|
|
return true
|
|
}
|
|
scheme := r.Header.Get("X-Forwarded-Proto")
|
|
if base, err := url.Parse(cred.PublicURL); err == nil && base.Scheme != "" {
|
|
scheme = base.Scheme
|
|
}
|
|
if scheme == "" {
|
|
scheme = "http"
|
|
}
|
|
return scheme != target.Scheme
|
|
}
|
|
|
|
func mergeQuery(target, request string) string {
|
|
if target == "" || request == "" {
|
|
return target + request
|
|
}
|
|
return target + "&" + request
|
|
}
|
|
|
|
// insecureTransport is shared, because a transport per request would pool no connections.
|
|
var insecureTransport = &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}} //nolint:gosec // the runner reaches its instance on the operator's say-so
|