Skip to content

Commit c78805d

Browse files
committed
cmd/buildlet: add localhost proxy to farmer.golang.org's module proxy
Now that farmer.golang.org is an authenticated GOPROXY server (CL 165779), this adds the unauthenticated localhost server on the buildlet that adds the auth headers to farmer.golang.org. The buildlet only does this if the build includes env GO_BUILDER_SET_GOPROXY=coordinator, in which case it listens on an emphemeral localhost port per exec and sets GOPROXY to an HTTP server running on that port. This way we don't need to deal with any port management or conflicts. Updates golang/go#14594 Change-Id: Iae6d2deda9d5e88ab659d94aaccc43e01fcf4a7c Reviewed-on: https://go-review.googlesource.com/c/build/+/165782 Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent 8fa933c commit c78805d

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

cmd/buildlet/buildlet.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ var (
7373
// 16: make macstadium builders always haltEntireOS
7474
// 17: make macstadium halts use sudo
7575
// 18: set TMPDIR and GOCACHE
76-
const buildletVersion = 18
76+
// 21: GO_BUILDER_SET_GOPROXY=coordinator support
77+
const buildletVersion = 21
7778

7879
func defaultListenAddr() string {
7980
if runtime.GOOS == "darwin" {
@@ -926,6 +927,18 @@ func handleExec(w http.ResponseWriter, r *http.Request) {
926927

927928
env := append(baseEnv(goarch), postEnv...)
928929

930+
// Setup an localhost HTTP server to proxy module cache, if requested by environment.
931+
if goproxyHandler != nil && getEnv(postEnv, "GO_BUILDER_SET_GOPROXY") == "coordinator" {
932+
ln, err := net.Listen("tcp", "localhost:0")
933+
if err != nil {
934+
http.Error(w, "failed to listen on localhost for GOPROXY=coordinator: "+err.Error(), http.StatusInternalServerError)
935+
return
936+
}
937+
defer ln.Close()
938+
srv := &http.Server{Handler: goproxyHandler}
939+
go srv.Serve(ln)
940+
env = append(env, fmt.Sprintf("GOPROXY=http://localhost:%d", ln.Addr().(*net.TCPAddr).Port))
941+
}
929942
if v := processTmpDirEnv; v != "" {
930943
env = append(env, "TMPDIR="+v)
931944
}

cmd/buildlet/reverse.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"log"
1818
"net"
1919
"net/http"
20+
"net/http/httputil"
2021
"net/url"
2122
"os"
2223
"path/filepath"
@@ -69,6 +70,34 @@ func isDevReverseMode() bool {
6970
return !strings.HasPrefix(*coordinator, "farmer.golang.org")
7071
}
7172

73+
// proxyToCoordinatorHandler is a GOPROXY proxy, proxying to
74+
// https://farmer.golang.org while adding HTTP basic auth (of the
75+
// reverse buildlet type & its key) and the
76+
// X-Proxy-Service:module-cache header.
77+
type proxyToCoordinatorHandler struct {
78+
user, pass string
79+
rp *httputil.ReverseProxy
80+
}
81+
82+
func (h *proxyToCoordinatorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
83+
outReq := r.WithContext(r.Context())
84+
outReq.SetBasicAuth(h.user, h.pass)
85+
outReq.Header.Set("X-Proxy-Service", "module-cache")
86+
h.rp.ServeHTTP(w, outReq)
87+
}
88+
89+
// goproxyHandler is non-nil for reverse buildlets.
90+
var goproxyHandler *proxyToCoordinatorHandler
91+
92+
func newProxyToCoordinatorHandler(user, pass string) *proxyToCoordinatorHandler {
93+
u, _ := url.Parse("https://farmer.golang.org")
94+
return &proxyToCoordinatorHandler{
95+
user: user,
96+
pass: pass,
97+
rp: httputil.NewSingleHostReverseProxy(u),
98+
}
99+
}
100+
72101
func dialCoordinator() error {
73102
devMode := isDevReverseMode()
74103

@@ -87,13 +116,15 @@ func dialCoordinator() error {
87116
}
88117
keys = append(keys, key)
89118
}
119+
goproxyHandler = newProxyToCoordinatorHandler(modes[0], keys[0])
90120
} else {
91121
// New way.
92122
key, err := keyForMode(*reverseType)
93123
if err != nil {
94124
log.Fatalf("failed to find key for %s: %v", *reverseType, err)
95125
}
96126
keys = append(keys, key)
127+
goproxyHandler = newProxyToCoordinatorHandler(*reverseType, key)
97128
}
98129

99130
caCert := build.ProdCoordinatorCA

0 commit comments

Comments
 (0)