-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgit.go
More file actions
479 lines (417 loc) · 14.6 KB
/
git.go
File metadata and controls
479 lines (417 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// Package git implements a protocol-aware Git caching proxy strategy.
package git
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"io"
"log/slog"
"net/http"
"net/http/httputil"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"time"
"github.com/alecthomas/errors"
"github.com/block/cachew/internal/cache"
"github.com/block/cachew/internal/gitclone"
"github.com/block/cachew/internal/githubapp"
"github.com/block/cachew/internal/jobscheduler"
"github.com/block/cachew/internal/logging"
"github.com/block/cachew/internal/strategy"
)
func Register(r *strategy.Registry, scheduler jobscheduler.Provider, cloneManagerProvider gitclone.ManagerProvider, tokenManagerProvider githubapp.TokenManagerProvider) {
strategy.Register(r, "git", "Caches Git repositories, including tarball snapshots.", func(ctx context.Context, config Config, cache cache.Cache, mux strategy.Mux) (*Strategy, error) {
return New(ctx, config, scheduler, cache, mux, cloneManagerProvider, tokenManagerProvider)
})
}
type Config struct {
SnapshotInterval time.Duration `hcl:"snapshot-interval,optional" help:"How often to generate tar.zstd snapshots. 0 disables snapshots." default:"0"`
RepackInterval time.Duration `hcl:"repack-interval,optional" help:"How often to run full repack. 0 disables." default:"0"`
// ServerURL is embedded as remote.origin.url in snapshots so git pull goes through cachew.
ServerURL string `hcl:"server-url,optional" help:"Base URL of this cachew instance, embedded in snapshot remote URLs." default:"${CACHEW_URL}"`
ZstdThreads int `hcl:"zstd-threads,optional" help:"Threads for zstd compression/decompression (0 = all CPU cores)." default:"0"`
}
type Strategy struct {
config Config
cache cache.Cache
cloneManager *gitclone.Manager
httpClient *http.Client
proxy *httputil.ReverseProxy
ctx context.Context
scheduler jobscheduler.Scheduler
spoolsMu sync.Mutex
spools map[string]*RepoSpools
tokenManager *githubapp.TokenManager
}
func New(
ctx context.Context,
config Config,
schedulerProvider jobscheduler.Provider,
cache cache.Cache,
mux strategy.Mux,
cloneManagerProvider gitclone.ManagerProvider,
tokenManagerProvider githubapp.TokenManagerProvider,
) (*Strategy, error) {
if _, err := exec.LookPath("git"); err != nil {
return nil, errors.New("git is required but not found in PATH")
}
if config.SnapshotInterval > 0 {
for _, bin := range []string{"tar", "zstd"} {
if _, err := exec.LookPath(bin); err != nil {
return nil, errors.Errorf("%s is required for snapshots (snapshot-interval > 0) but not found in PATH", bin)
}
}
}
logger := logging.FromContext(ctx)
// Get GitHub App token manager if configured
tokenManager, err := tokenManagerProvider()
if err != nil {
return nil, errors.Wrap(err, "create token manager")
}
if tokenManager != nil {
logger.InfoContext(ctx, "Using GitHub App authentication for git strategy")
} else {
logger.WarnContext(ctx, "GitHub App not configured, using system git credentials")
}
cloneManager, err := cloneManagerProvider()
if err != nil {
return nil, errors.Wrap(err, "failed to create clone manager")
}
for _, dir := range []string{".spools", ".snapshots"} {
if err := os.RemoveAll(filepath.Join(cloneManager.Config().MirrorRoot, dir)); err != nil {
return nil, errors.Wrapf(err, "clean up stale %s", dir)
}
}
scheduler, err := schedulerProvider()
if err != nil {
return nil, errors.Wrap(err, "failed to create scheduler")
}
s := &Strategy{
config: config,
cache: cache,
cloneManager: cloneManager,
httpClient: http.DefaultClient,
ctx: ctx,
scheduler: scheduler.WithQueuePrefix("git"),
spools: make(map[string]*RepoSpools),
tokenManager: tokenManager,
}
s.config.ServerURL = strings.TrimRight(config.ServerURL, "/")
existing, err := s.cloneManager.DiscoverExisting(ctx)
if err != nil {
logger.WarnContext(ctx, "Failed to discover existing clones",
slog.String("error", err.Error()))
}
for _, repo := range existing {
if s.config.SnapshotInterval > 0 {
s.scheduleSnapshotJobs(repo)
}
if s.config.RepackInterval > 0 {
s.scheduleRepackJobs(repo)
}
}
s.proxy = &httputil.ReverseProxy{
Director: func(req *http.Request) {
req.URL.Scheme = "https"
req.URL.Host = req.PathValue("host")
req.URL.Path = "/" + req.PathValue("path")
req.Host = req.URL.Host
// Inject GitHub App authentication for github.com requests
if s.tokenManager != nil && req.URL.Host == "github.com" {
// Extract org from path (e.g., /squareup/blox.git/...)
parts := strings.Split(strings.TrimPrefix(req.URL.Path, "/"), "/")
if len(parts) >= 1 && parts[0] != "" {
org := parts[0]
token, err := s.tokenManager.GetTokenForOrg(req.Context(), org)
if err == nil && token != "" {
// Inject token as Basic auth with "x-access-token" username
req.SetBasicAuth("x-access-token", token)
logger.DebugContext(req.Context(), "Injecting GitHub App auth into upstream request",
slog.String("org", org))
}
}
}
},
Transport: s.httpClient.Transport,
ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
logging.FromContext(r.Context()).ErrorContext(r.Context(), "Upstream request failed", slog.String("error", err.Error()))
w.WriteHeader(http.StatusBadGateway)
},
}
mux.Handle("GET /git/{host}/{path...}", http.HandlerFunc(s.handleRequest))
mux.Handle("POST /git/{host}/{path...}", http.HandlerFunc(s.handleRequest))
logger.InfoContext(ctx, "Git strategy initialized",
"snapshot_interval", config.SnapshotInterval)
return s, nil
}
var _ strategy.Strategy = (*Strategy)(nil)
// SetHTTPTransport overrides the HTTP transport used for upstream requests.
// This is intended for testing.
func (s *Strategy) SetHTTPTransport(t http.RoundTripper) {
s.httpClient.Transport = t
s.proxy.Transport = t
}
func (s *Strategy) String() string { return "git" }
func (s *Strategy) handleRequest(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logger := logging.FromContext(ctx)
host := r.PathValue("host")
pathValue := r.PathValue("path")
logger.DebugContext(ctx, "Git request",
slog.String("method", r.Method),
slog.String("host", host),
slog.String("path", pathValue))
if strings.HasSuffix(pathValue, "/snapshot.tar.zst") {
s.handleSnapshotRequest(w, r, host, pathValue)
return
}
service := r.URL.Query().Get("service")
isReceivePack := service == "git-receive-pack" || strings.HasSuffix(pathValue, "/git-receive-pack")
if isReceivePack {
logger.DebugContext(ctx, "Forwarding write operation to upstream")
s.forwardToUpstream(w, r, host, pathValue)
return
}
repoPath := ExtractRepoPath(pathValue)
upstreamURL := "https://" + host + "/" + repoPath
repo, err := s.cloneManager.GetOrCreate(ctx, upstreamURL)
if err != nil {
logger.ErrorContext(ctx, "Failed to get or create clone",
slog.String("error", err.Error()))
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
state := repo.State()
isInfoRefs := strings.HasSuffix(pathValue, "/info/refs")
switch state {
case gitclone.StateReady:
if isInfoRefs {
if err := s.ensureRefsUpToDate(ctx, repo); err != nil {
logger.WarnContext(ctx, "Failed to ensure refs up to date",
slog.String("error", err.Error()))
}
}
s.maybeBackgroundFetch(repo)
s.serveFromBackend(w, r, repo)
case gitclone.StateCloning, gitclone.StateEmpty:
if state == gitclone.StateEmpty {
logger.DebugContext(ctx, "Starting background clone, forwarding to upstream")
s.scheduler.Submit(repo.UpstreamURL(), "clone", func(ctx context.Context) error {
s.startClone(ctx, repo)
return nil
})
}
s.serveWithSpool(w, r, host, pathValue, upstreamURL)
}
}
// SpoolKeyForRequest returns the spool key for a request, or empty string if the
// request is not spoolable. For POST requests, the body is hashed to differentiate
// protocol v2 commands (e.g. ls-refs vs fetch) that share the same URL. The request
// body is buffered and replaced so it can still be read by the caller.
func SpoolKeyForRequest(pathValue string, r *http.Request) (string, error) {
if !strings.HasSuffix(pathValue, "/git-upload-pack") {
return "", nil
}
if r.Method != http.MethodPost || r.Body == nil {
return "upload-pack", nil
}
body, err := io.ReadAll(r.Body)
if err != nil {
return "", errors.Wrap(err, "read request body for spool key")
}
r.Body = io.NopCloser(bytes.NewReader(body))
h := sha256.Sum256(body)
return "upload-pack-" + hex.EncodeToString(h[:8]), nil
}
func spoolDirForURL(mirrorRoot, upstreamURL string) (string, error) {
repoPath, err := gitclone.RepoPathFromURL(upstreamURL)
if err != nil {
return "", errors.Wrap(err, "resolve spool directory")
}
return filepath.Join(mirrorRoot, ".spools", repoPath), nil
}
func (s *Strategy) getOrCreateRepoSpools(upstreamURL string) (*RepoSpools, error) {
s.spoolsMu.Lock()
defer s.spoolsMu.Unlock()
rp, exists := s.spools[upstreamURL]
if exists {
return rp, nil
}
dir, err := spoolDirForURL(s.cloneManager.Config().MirrorRoot, upstreamURL)
if err != nil {
return nil, err
}
rp = NewRepoSpools(dir)
s.spools[upstreamURL] = rp
return rp, nil
}
func (s *Strategy) cleanupSpools(upstreamURL string) {
s.spoolsMu.Lock()
rp, exists := s.spools[upstreamURL]
if exists {
delete(s.spools, upstreamURL)
}
s.spoolsMu.Unlock()
if rp != nil {
if err := rp.Close(); err != nil {
logging.FromContext(s.ctx).WarnContext(s.ctx, "Failed to clean up spools",
slog.String("upstream", upstreamURL),
slog.String("error", err.Error()))
}
}
}
func (s *Strategy) serveWithSpool(w http.ResponseWriter, r *http.Request, host, pathValue, upstreamURL string) {
ctx := r.Context()
logger := logging.FromContext(ctx)
key, err := SpoolKeyForRequest(pathValue, r)
if err != nil {
logger.WarnContext(ctx, "Failed to compute spool key, forwarding to upstream",
slog.String("error", err.Error()))
s.forwardToUpstream(w, r, host, pathValue)
return
}
if key == "" {
s.forwardToUpstream(w, r, host, pathValue)
return
}
rp, err := s.getOrCreateRepoSpools(upstreamURL)
if err != nil {
logger.WarnContext(ctx, "Failed to resolve spool directory, forwarding to upstream",
slog.String("error", err.Error()))
s.forwardToUpstream(w, r, host, pathValue)
return
}
spool, isWriter, err := rp.GetOrCreate(key)
if err != nil {
logger.WarnContext(ctx, "Failed to create spool, forwarding to upstream",
slog.String("error", err.Error()))
s.forwardToUpstream(w, r, host, pathValue)
return
}
if isWriter {
logger.DebugContext(ctx, "Spooling upstream response",
slog.String("key", key),
slog.String("upstream", upstreamURL))
tw := NewSpoolTeeWriter(w, spool)
s.forwardToUpstream(tw, r, host, pathValue)
spool.MarkComplete()
return
}
if spool.Failed() {
logger.DebugContext(ctx, "Spool failed, forwarding to upstream",
slog.String("key", key))
s.forwardToUpstream(w, r, host, pathValue)
return
}
logger.DebugContext(ctx, "Serving from spool",
slog.String("key", key),
slog.String("upstream", upstreamURL))
if err := spool.ServeTo(w); err != nil {
if errors.Is(err, ErrSpoolFailed) {
logger.DebugContext(ctx, "Spool failed before response started, forwarding to upstream",
slog.String("key", key))
s.forwardToUpstream(w, r, host, pathValue)
return
}
logger.WarnContext(ctx, "Spool read failed mid-stream",
slog.String("key", key),
slog.String("error", err.Error()))
}
}
func ExtractRepoPath(pathValue string) string {
repoPath := pathValue
repoPath = strings.TrimSuffix(repoPath, "/info/refs")
repoPath = strings.TrimSuffix(repoPath, "/git-upload-pack")
repoPath = strings.TrimSuffix(repoPath, "/git-receive-pack")
repoPath = strings.TrimSuffix(repoPath, ".git")
return repoPath
}
func (s *Strategy) serveCachedArtifact(w http.ResponseWriter, r *http.Request, host, pathValue, urlSuffix, artifact string) {
ctx := r.Context()
logger := logging.FromContext(ctx)
logger.DebugContext(ctx, artifact+" request",
slog.String("host", host),
slog.String("path", pathValue))
pathValue = strings.TrimSuffix(pathValue, "/"+urlSuffix)
repoPath := ExtractRepoPath(pathValue)
upstreamURL := "https://" + host + "/" + repoPath
cacheKey := cache.NewKey(upstreamURL + "." + artifact)
reader, headers, err := s.cache.Open(ctx, cacheKey)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
logger.DebugContext(ctx, artifact+" not found in cache",
slog.String("upstream", upstreamURL))
http.NotFound(w, r)
return
}
logger.ErrorContext(ctx, "Failed to open "+artifact+" from cache",
slog.String("upstream", upstreamURL),
slog.String("error", err.Error()))
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
defer reader.Close()
for key, values := range headers {
for _, value := range values {
w.Header().Add(key, value)
}
}
_, err = io.Copy(w, reader)
if err != nil {
logger.ErrorContext(ctx, "Failed to stream "+artifact,
slog.String("upstream", upstreamURL),
slog.String("error", err.Error()))
}
}
func (s *Strategy) startClone(ctx context.Context, repo *gitclone.Repository) {
logger := logging.FromContext(ctx)
logger.InfoContext(ctx, "Starting clone",
slog.String("upstream", repo.UpstreamURL()),
slog.String("path", repo.Path()))
err := repo.Clone(ctx)
// Clean up spools regardless of clone success or failure, so that subsequent
// requests either serve from the local backend or go directly to upstream.
s.cleanupSpools(repo.UpstreamURL())
if err != nil {
logger.ErrorContext(ctx, "Clone failed",
slog.String("upstream", repo.UpstreamURL()),
slog.String("error", err.Error()))
return
}
logger.InfoContext(ctx, "Clone completed",
slog.String("upstream", repo.UpstreamURL()),
slog.String("path", repo.Path()))
if s.config.SnapshotInterval > 0 {
s.scheduleSnapshotJobs(repo)
}
if s.config.RepackInterval > 0 {
s.scheduleRepackJobs(repo)
}
}
func (s *Strategy) maybeBackgroundFetch(repo *gitclone.Repository) {
if !repo.NeedsFetch(s.cloneManager.Config().FetchInterval) {
return
}
s.scheduler.Submit(repo.UpstreamURL(), "fetch", func(ctx context.Context) error {
s.backgroundFetch(ctx, repo)
return nil
})
}
func (s *Strategy) backgroundFetch(ctx context.Context, repo *gitclone.Repository) {
logger := logging.FromContext(ctx)
if !repo.NeedsFetch(s.cloneManager.Config().FetchInterval) {
return
}
logger.DebugContext(ctx, "Fetching updates",
slog.String("upstream", repo.UpstreamURL()),
slog.String("path", repo.Path()))
if err := repo.Fetch(ctx); err != nil {
logger.ErrorContext(ctx, "Fetch failed",
slog.String("upstream", repo.UpstreamURL()),
slog.String("error", err.Error()))
}
}