aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/git-lfs-server/main.go
blob: 3bb1173e1b72773cbd04fe827a96da6380dcba17 (plain) (blame)
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
package main

import (
	"bytes"
	"context"
	"crypto/ed25519"
	"crypto/sha256"
	"encoding/base64"
	"encoding/hex"
	"encoding/json"
	"errors"
	"fmt"
	"hash"
	"io"
	"mime"
	"net/http"
	"net/http/cgi"
	"net/url"
	"os"
	"os/exec"
	"path"
	"regexp"
	"slices"
	"strconv"
	"strings"
	"time"
	"unicode"

	"github.com/golang-jwt/jwt/v5"
	"github.com/minio/minio-go/v7"
	"github.com/minio/minio-go/v7/pkg/credentials"
	"github.com/rs/xid"
)

type operation string
type transferAdapter string
type hashAlgo string

const (
	operationDownload    operation       = "download"
	operationUpload      operation       = "upload"
	transferAdapterBasic transferAdapter = "basic"
	hashAlgoSHA256       hashAlgo        = "sha256"
)

const lfsMIME = "application/vnd.git-lfs+json"

type batchRef struct {
	Name string `json:"name"`
}

type batchRequestObject struct {
	OID  string `json:"oid"`
	Size int64  `json:"size"`
}

type batchRequest struct {
	Operation operation            `json:"operation"`
	Transfers []transferAdapter    `json:"transfers,omitempty"`
	Ref       *batchRef            `json:"ref,omitempty"`
	Objects   []batchRequestObject `json:"objects"`
	HashAlgo  hashAlgo             `json:"hash_algo,omitempty"`
}

type batchAction struct {
	HRef   string            `json:"href"`
	Header map[string]string `json:"header,omitempty"`
	// In seconds.
	ExpiresIn int64 `json:"expires_in,omitempty"`
	// expires_at (RFC3339) could also be used, but we leave it out since we
	// don't use it.
}

type batchError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

type batchResponseObject struct {
	OID           string                    `json:"oid"`
	Size          int64                     `json:"size"`
	Authenticated *bool                     `json:"authenticated"`
	Actions       map[operation]batchAction `json:"actions,omitempty"`
	Error         *batchError               `json:"error,omitempty"`
}

type batchResponse struct {
	Transfer transferAdapter       `json:"transfer,omitempty"`
	Objects  []batchResponseObject `json:"objects"`
	HashAlgo hashAlgo              `json:"hash_algo,omitempty"`
}

type handler struct {
	mc           *minio.Client
	bucket       string
	anonUser     string
	gitolitePath string
	privateKey   ed25519.PrivateKey
	baseURL      *url.URL
}

func isValidSHA256Hash(hash string) bool {
	if len(hash) != 64 {
		return false
	}
	for _, c := range hash {
		if !unicode.Is(unicode.ASCII_Hex_Digit, c) {
			return false
		}
	}
	return true
}

type lfsError struct {
	Message          string `json:"message"`
	DocumentationURL string `json:"documentation_url,omitempty"`
	RequestID        string `json:"request_id,omitempty"`
}

func makeRespError(ctx context.Context, w http.ResponseWriter, message string, code int) {
	err := lfsError{Message: message}
	if val := ctx.Value(requestIDKey); val != nil {
		err.RequestID = val.(string)
	}
	w.Header().Set("Content-Type", lfsMIME+"; charset=utf-8")
	w.WriteHeader(code)
	json.NewEncoder(w).Encode(err)
}

func makeObjError(obj parsedBatchObject, message string, code int) batchResponseObject {
	return batchResponseObject{
		OID:  obj.fullHash,
		Size: obj.size,
		Error: &batchError{
			Message: message,
			Code:    code,
		},
	}
}

func sha256AsBase64(hash string) string {
	raw, err := hex.DecodeString(hash)
	if err != nil {
		return ""
	}
	return base64.StdEncoding.EncodeToString(raw)
}

func (h *handler) handleDownloadObject(ctx context.Context, repo string, obj parsedBatchObject) batchResponseObject {
	fullPath := path.Join(repo+".git", "lfs/objects", obj.firstByte, obj.secondByte, obj.fullHash)
	expiresIn := time.Hour * 24

	info, err := h.mc.StatObject(ctx, h.bucket, fullPath, minio.StatObjectOptions{Checksum: true})
	if err != nil {
		var resp minio.ErrorResponse
		if errors.As(err, &resp) && resp.StatusCode == http.StatusNotFound {
			return makeObjError(obj, "Object does not exist", http.StatusNotFound)
		}
		// TODO: consider not making this an object-specific, but rather a
		// generic error such that the entire Batch API request fails.
		reqlog(ctx, "Failed to query object information (full path: %s): %s", fullPath, err)
		return makeObjError(obj, "Failed to query object information", http.StatusInternalServerError)
	}
	if info.ChecksumSHA256 != "" && strings.ToLower(info.ChecksumSHA256) != obj.fullHash {
		return makeObjError(obj, "Corrupted file", http.StatusUnprocessableEntity)
	}
	if info.Size != obj.size {
		return makeObjError(obj, "Incorrect size specified for object", http.StatusUnprocessableEntity)
	}

	presigned, err := h.mc.PresignedGetObject(ctx, h.bucket, fullPath, expiresIn, url.Values{})
	if err != nil {
		// TODO: consider not making this an object-specific, but rather a
		// generic error such that the entire Batch API request fails.
		reqlog(ctx, "Failed to generate action href (full path: %s): %s", fullPath, err)
		return makeObjError(obj, "Failed to generate action href", http.StatusInternalServerError)
	}

	authenticated := true
	return batchResponseObject{
		OID:           obj.fullHash,
		Size:          obj.size,
		Authenticated: &authenticated,
		Actions: map[operation]batchAction{
			operationDownload: {
				HRef:      presigned.String(),
				ExpiresIn: int64(expiresIn.Seconds()),
			},
		},
	}
}

type uploadObjectGitolfs3Claims struct {
	Repository string `json:"repository"`
	OID        string `json:"oid"`
	Size       int64  `json:"size"`
}

type uploadObjectCustomClaims struct {
	Gitolfs3 uploadObjectGitolfs3Claims `json:"gitolfs3"`
	*jwt.RegisteredClaims
}

// Return nil when the object already exists
func (h *handler) handleUploadObject(ctx context.Context, repo string, obj parsedBatchObject) *batchResponseObject {
	fullPath := path.Join(repo+".git", "lfs/objects", obj.firstByte, obj.secondByte, obj.fullHash)
	_, err := h.mc.StatObject(ctx, h.bucket, fullPath, minio.GetObjectOptions{})
	if err == nil {
		// The object exists
		return nil
	}

	var resp minio.ErrorResponse
	if !errors.As(err, &resp) || resp.StatusCode != http.StatusNotFound {
		// TODO: consider not making this an object-specific, but rather a
		// generic error such that the entire Batch API request fails.
		reqlog(ctx, "Failed to generate action href (full path: %s): %s", fullPath, err)
		objErr := makeObjError(obj, "Failed to generate action href", http.StatusInternalServerError)
		return &objErr
	}

	expiresIn := time.Hour * 24
	claims := uploadObjectCustomClaims{
		Gitolfs3: uploadObjectGitolfs3Claims{
			Repository: repo,
			OID:        obj.fullHash,
			Size:       obj.size,
		},
		RegisteredClaims: &jwt.RegisteredClaims{
			IssuedAt:  jwt.NewNumericDate(time.Now()),
			ExpiresAt: jwt.NewNumericDate(time.Now().Add(expiresIn)),
		},
	}

	token := jwt.NewWithClaims(jwt.SigningMethodEdDSA, claims)
	ss, err := token.SignedString(h.privateKey)
	if err != nil {
		// TODO: consider not making this an object-specific, but rather a
		// generic error such that the entire Batch API request fails.
		reqlog(ctx, "Fatal: failed to generate JWT: %s", err)
		objErr := makeObjError(obj, "Failed to generate token", http.StatusInternalServerError)
		return &objErr
	}

	uploadPath := path.Join(repo+".git", "info/lfs/objects", obj.firstByte, obj.secondByte, obj.fullHash)
	uploadHRef := h.baseURL.ResolveReference(&url.URL{Path: uploadPath}).String()
	// The object does not exist.
	authenticated := true
	return &batchResponseObject{
		OID:           obj.fullHash,
		Size:          obj.size,
		Authenticated: &authenticated,
		Actions: map[operation]batchAction{
			operationUpload: {
				Header: map[string]string{
					"Authorization": "Bearer " + ss,
				},
				HRef:      uploadHRef,
				ExpiresIn: int64(expiresIn.Seconds()),
			},
		},
	}
}

type validatingReader struct {
	promisedSize   int64
	promisedSha256 []byte

	reader    io.Reader
	bytesRead int64
	current   hash.Hash
	err       error
}

func newValidatingReader(promisedSize int64, promisedSha256 []byte, r io.Reader) *validatingReader {
	return &validatingReader{
		promisedSize:   promisedSize,
		promisedSha256: promisedSha256,
		reader:         r,
		current:        sha256.New(),
	}
}

var errTooBig = errors.New("validator: uploaded file bigger than indicated")
var errTooSmall = errors.New("validator: uploaded file smaller than indicated")
var errBadSum = errors.New("validator: bad checksum provided or file corrupted")

func (i *validatingReader) Read(b []byte) (int, error) {
	if i.err != nil {
		return 0, i.err
	}
	n, err := i.reader.Read(b)
	i.bytesRead += int64(n)
	if i.bytesRead > i.promisedSize {
		i.err = errTooBig
		return 0, i.err
	}
	if err != nil && errors.Is(err, io.EOF) {
		if i.bytesRead < i.promisedSize {
			i.err = errTooSmall
			return n, i.err
		}
	}
	// According to the documentation, Hash.Write never returns an error
	i.current.Write(b[:n])
	if i.bytesRead == i.promisedSize {
		if !bytes.Equal(i.promisedSha256, i.current.Sum(nil)) {
			i.err = errBadSum
			return 0, i.err
		}
	}
	return n, err
}

func (h *handler) handlePutObject(w http.ResponseWriter, r *http.Request, repo, oid string) {
	ctx := r.Context()

	authz := r.Header.Get("Authorization")
	if authz == "" {
		makeRespError(ctx, w, "Missing Authorization header", http.StatusBadRequest)
		return
	}
	if !strings.HasPrefix(authz, "Bearer ") {
		makeRespError(ctx, w, "Invalid Authorization header", http.StatusBadRequest)
		return
	}
	authz = strings.TrimPrefix(authz, "Bearer ")

	var claims uploadObjectCustomClaims
	_, err := jwt.ParseWithClaims(authz, &claims, func(token *jwt.Token) (any, error) {
		if _, ok := token.Method.(*jwt.SigningMethodEd25519); !ok {
			return nil, fmt.Errorf("expected signing method EdDSA, got %s", token.Header["alg"])
		}
		return h.privateKey.Public(), nil
	})
	if err != nil {
		makeRespError(ctx, w, "Invalid token", http.StatusUnauthorized)
		return
	}
	if claims.Gitolfs3.Repository != repo {
		makeRespError(ctx, w, "Invalid token", http.StatusUnauthorized)
		return
	}
	if claims.Gitolfs3.OID != oid {
		makeRespError(ctx, w, "Invalid token", http.StatusUnauthorized)
		return
	}

	// Check with claims
	if lengthStr := r.Header.Get("Content-Length"); lengthStr != "" {
		length, err := strconv.ParseInt(lengthStr, 10, 64)
		if err != nil {
			makeRespError(ctx, w, "Bad Content-Length format", http.StatusBadRequest)
			return
		}
		if length != claims.Gitolfs3.Size {
			makeRespError(ctx, w, "Invalid token", http.StatusUnauthorized)
			return
		}
	}

	sha256Raw, err := hex.DecodeString(oid)
	if err != nil || len(sha256Raw) != sha256.Size {
		makeRespError(ctx, w, "Invalid OID", http.StatusBadRequest)
		return
	}

	reader := newValidatingReader(claims.Gitolfs3.Size, sha256Raw, r.Body)

	fullPath := path.Join(repo+".git", "lfs/objects", oid[:2], oid[2:4], oid)
	_, err = h.mc.PutObject(ctx, h.bucket, fullPath, reader, int64(claims.Gitolfs3.Size), minio.PutObjectOptions{
		SendContentMd5: true,
	})
	if err != nil {
		makeRespError(ctx, w, "Failed to upload object", http.StatusInternalServerError)
		return
	}
}

type parsedBatchObject struct {
	firstByte  string
	secondByte string
	fullHash   string
	size       int64
}

func isLFSMediaType(t string) bool {
	if mediaType, params, err := mime.ParseMediaType(t); err == nil {
		if mediaType == lfsMIME {
			if params["charset"] == "" || strings.ToLower(params["charset"]) == "utf-8" {
				return true
			}
		}
	}
	return false
}

var reBatchAPI = regexp.MustCompile(`^([a-zA-Z0-9-_/]+)\.git/info/lfs/objects/batch$`)
var reObjUpload = regexp.MustCompile(`^([a-zA-Z0-9-_/]+)\.git/info/lfs/objects/([0-9a-f]{2})/([0-9a-f]{2})/([0-9a-f]{64})$`)

type requestID struct{}

var requestIDKey requestID

// TODO: make a shared package for this
type lfsAuthGitolfs3Claims struct {
	Repository string    `json:"repository"`
	Permission operation `json:"permission"`
}

type lfsAuthCustomClaims struct {
	Gitolfs3 lfsAuthGitolfs3Claims `json:"gitolfs3"`
	*jwt.RegisteredClaims
}

// Request to perform <operation> in <repository> [on reference <refspec>]
type operationRequest struct {
	operation  operation
	repository string
	refspec    *string
}

func (h *handler) getGitoliteAccess(repo, user, gitolitePerm string, refspec *string) (bool, error) {
	// gitolite access -q: returns only exit code
	gitoliteArgs := []string{"access", "-q", repo, user, gitolitePerm}
	if refspec != nil {
		gitoliteArgs = append(gitoliteArgs, *refspec)
	}
	cmd := exec.Command(h.gitolitePath, gitoliteArgs...)
	err := cmd.Run()
	if err != nil {
		var exitErr *exec.ExitError
		if !errors.As(err, &exitErr) {
			return false, fmt.Errorf("(running %s): %w", cmd, err)
		}
		return false, nil
	}
	return true, nil
}

func (h *handler) authorize(w http.ResponseWriter, r *http.Request, or operationRequest) bool {
	user := h.anonUser
	ctx := r.Context()

	if authz := r.Header.Get("Authorization"); authz != "" {
		if !strings.HasPrefix(authz, "Bearer ") {
			makeRespError(ctx, w, "Invalid Authorization header", http.StatusBadRequest)
			return false
		}
		authz = strings.TrimPrefix(authz, "Bearer ")

		var claims lfsAuthCustomClaims
		_, err := jwt.ParseWithClaims(authz, &claims, func(token *jwt.Token) (any, error) {
			if _, ok := token.Method.(*jwt.SigningMethodEd25519); !ok {
				return nil, fmt.Errorf("expected signing method EdDSA, got %s", token.Header["alg"])
			}
			return h.privateKey.Public(), nil
		})
		if err != nil {
			makeRespError(ctx, w, "Invalid token", http.StatusUnauthorized)
			return false
		}

		if claims.Gitolfs3.Repository != or.repository {
			makeRespError(ctx, w, "Invalid token", http.StatusUnauthorized)
			return false
		}
		if claims.Gitolfs3.Permission == operationDownload && or.operation == operationUpload {
			makeRespError(ctx, w, "Forbidden", http.StatusForbidden)
			return false
		}

		user = claims.Subject
	}

	readAccess, err := h.getGitoliteAccess(or.repository, user, "R", or.refspec)
	if err != nil {
		reqlog(ctx, "Error checking access info: %s", err)
		makeRespError(ctx, w, "Failed to query access information", http.StatusInternalServerError)
		return false
	}
	if !readAccess {
		makeRespError(ctx, w, "Repository not found", http.StatusNotFound)
		return false
	}
	if or.operation == operationUpload {
		writeAccess, err := h.getGitoliteAccess(or.repository, user, "W", or.refspec)
		if err != nil {
			reqlog(ctx, "Error checking access info: %s", err)
			makeRespError(ctx, w, "Failed to query access information", http.StatusInternalServerError)
			return false
		}
		// User has read access but no write access
		if !writeAccess {
			makeRespError(ctx, w, "Forbidden", http.StatusForbidden)
			return false
		}
	}

	return true
}

func (h *handler) handleBatchAPI(w http.ResponseWriter, r *http.Request, repo string) {
	ctx := r.Context()

	if !slices.ContainsFunc(r.Header.Values("Accept"), isLFSMediaType) {
		makeRespError(ctx, w, "Expected "+lfsMIME+" (with UTF-8 charset) in list of acceptable response media types", http.StatusNotAcceptable)
		return
	}
	if !isLFSMediaType(r.Header.Get("Content-Type")) {
		makeRespError(ctx, w, "Expected request Content-Type to be "+lfsMIME+" (with UTF-8 charset)", http.StatusUnsupportedMediaType)
		return
	}

	var body batchRequest
	if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
		makeRespError(ctx, w, "Failed to parse request body as JSON", http.StatusBadRequest)
		return
	}
	if body.Operation != operationDownload && body.Operation != operationUpload {
		makeRespError(ctx, w, "Invalid operation specified", http.StatusBadRequest)
		return
	}

	or := operationRequest{
		operation:  body.Operation,
		repository: repo,
	}
	if body.Ref != nil {
		or.refspec = &body.Ref.Name
	}
	if !h.authorize(w, r.WithContext(ctx), or) {
		return
	}

	if body.HashAlgo != hashAlgoSHA256 {
		makeRespError(ctx, w, "Unsupported hash algorithm specified", http.StatusConflict)
		return
	}

	if len(body.Transfers) != 0 && !slices.Contains(body.Transfers, transferAdapterBasic) {
		makeRespError(ctx, w, "Unsupported transfer adapter specified (supported: basic)", http.StatusConflict)
		return
	}

	var objects []parsedBatchObject
	for _, obj := range body.Objects {
		oid := strings.ToLower(obj.OID)
		if !isValidSHA256Hash(oid) {
			makeRespError(ctx, w, "Invalid hash format in object ID", http.StatusBadRequest)
			return
		}
		objects = append(objects, parsedBatchObject{
			firstByte:  oid[:2],
			secondByte: oid[2:4],
			fullHash:   oid,
			size:       obj.Size,
		})
	}

	resp := batchResponse{
		Transfer: transferAdapterBasic,
		HashAlgo: hashAlgoSHA256,
	}
	for _, obj := range objects {
		switch body.Operation {
		case operationDownload:
			resp.Objects = append(resp.Objects, h.handleDownloadObject(ctx, repo, obj))
		case operationUpload:
			if respObj := h.handleUploadObject(ctx, repo, obj); respObj != nil {
				resp.Objects = append(resp.Objects, *respObj)
			}
		}
	}

	w.Header().Set("Content-Type", lfsMIME)
	w.WriteHeader(http.StatusOK)
	json.NewEncoder(w).Encode(resp)
}

func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	ctx := context.WithValue(r.Context(), requestIDKey, xid.New().String())

	reqPath := os.Getenv("PATH_INFO")
	if reqPath == "" {
		reqPath = r.URL.Path
	}
	reqPath = strings.TrimPrefix(path.Clean(reqPath), "/")

	if submatches := reBatchAPI.FindStringSubmatch(reqPath); len(submatches) == 2 {
		repo := strings.TrimPrefix(path.Clean(submatches[1]), "/")
		reqlog(ctx, "Repository: %s", repo)

		if r.Method != http.MethodPost {
			makeRespError(ctx, w, "Method not allowed", http.StatusMethodNotAllowed)
			return
		}

		h.handleBatchAPI(w, r.WithContext(ctx), repo)
		return
	}

	if submatches := reObjUpload.FindStringSubmatch(reqPath); len(submatches) == 5 {
		repo := strings.TrimPrefix(path.Clean(submatches[1]), "/")
		oid0, oid1, oid := submatches[2], submatches[3], submatches[4]

		if !isValidSHA256Hash(oid) {
			panic("Regex should only allow valid SHA256 hashes")
		}
		if oid0 != oid[:2] || oid1 != oid[2:4] {
			makeRespError(ctx, w, "Bad URL format: malformed OID pattern", http.StatusBadRequest)
			return
		}
		reqlog(ctx, "Repository: %s; OID: %s", repo, oid)

		if r.Method != http.MethodPut {
			makeRespError(ctx, w, "Method not allowed", http.StatusMethodNotAllowed)
			return
		}

		h.handlePutObject(w, r.WithContext(ctx), repo, oid)
		return
	}

	makeRespError(ctx, w, "Not found", http.StatusNotFound)
}

func reqlog(ctx context.Context, msg string, args ...any) {
	fmt.Fprint(os.Stderr, "[gitolfs3] ")
	if val := ctx.Value(requestIDKey); val != nil {
		fmt.Fprintf(os.Stderr, "[%s] ", val.(string))
	}
	fmt.Fprintf(os.Stderr, msg, args...)
	fmt.Fprint(os.Stderr, "\n")
}

func log(msg string, args ...any) {
	fmt.Fprint(os.Stderr, "[gitolfs3] ")
	fmt.Fprintf(os.Stderr, msg, args...)
	fmt.Fprint(os.Stderr, "\n")
}

func die(msg string, args ...any) {
	log("Environment variables: (dying)")
	for _, s := range os.Environ() {
		log("  %s", s)
	}
	log(msg, args...)
	os.Exit(1)
}

func loadPrivateKey(path string) ed25519.PrivateKey {
	raw, err := os.ReadFile(path)
	if err != nil {
		die("Failed to open specified public key: %s", err)
	}
	raw = bytes.TrimSpace(raw)

	if hex.DecodedLen(len(raw)) != ed25519.SeedSize {
		die("Specified public key file does not contain key (seed) of appropriate length")
	}
	decoded := make([]byte, hex.DecodedLen(len(raw)))
	if _, err = hex.Decode(decoded, raw); err != nil {
		die("Failed to decode specified public key: %s", err)
	}
	return ed25519.NewKeyFromSeed(decoded)
}

func wipe(b []byte) {
	for i := range b {
		b[i] = 0
	}
}

func main() {
	anonUser := os.Getenv("ANON_USER")
	privateKeyPath := os.Getenv("GITOLFS3_PRIVATE_KEY_PATH")
	endpoint := os.Getenv("S3_ENDPOINT")
	bucket := os.Getenv("S3_BUCKET")
	accessKeyIDFile := os.Getenv("S3_ACCESS_KEY_ID_FILE")
	secretAccessKeyFile := os.Getenv("S3_SECRET_ACCESS_KEY_FILE")
	gitolitePath := os.Getenv("GITOLITE_PATH")
	baseURLStr := os.Getenv("BASE_URL")

	if gitolitePath == "" {
		gitolitePath = "gitolite"
	}

	if anonUser == "" {
		die("Fatal: expected environment variable ANON_USER to be set")
	}
	if privateKeyPath == "" {
		die("Fatal: expected environment variable GITOLFS3_PRIVATE_KEY_PATH to be set")
	}
	if baseURLStr == "" {
		die("Fatal: expected environment variable BASE_URL to be set")
	}
	if endpoint == "" {
		die("Fatal: expected environment variable S3_ENDPOINT to be set")
	}
	if bucket == "" {
		die("Fatal: expected environment variable S3_BUCKET to be set")
	}

	if accessKeyIDFile == "" {
		die("Fatal: expected environment variable S3_ACCESS_KEY_ID_FILE to be set")
	}
	if secretAccessKeyFile == "" {
		die("Fatal: expected environment variable S3_SECRET_ACCESS_KEY_FILE to be set")
	}

	accessKeyID, err := os.ReadFile(accessKeyIDFile)
	if err != nil {
		die("Fatal: failed to read access key ID from specified file: %s", err)
	}
	secretAccessKey, err := os.ReadFile(secretAccessKeyFile)
	if err != nil {
		die("Fatal: failed to read secret access key from specified file: %s", err)
	}

	privateKey := loadPrivateKey(privateKeyPath)
	defer wipe(privateKey)

	baseURL, err := url.Parse(baseURLStr)
	if err != nil {
		die("Fatal: provided BASE_URL has bad format: %s", err)
	}

	mc, err := minio.New(endpoint, &minio.Options{
		Creds:  credentials.NewStaticV4(string(accessKeyID), string(secretAccessKey), ""),
		Secure: true,
	})
	if err != nil {
		die("Fatal: failed to create S3 client: %s", err)
	}

	if err = cgi.Serve(&handler{mc, bucket, anonUser, gitolitePath, privateKey, baseURL}); err != nil {
		die("Fatal: failed to serve CGI: %s", err)
	}
}

// Directory stucture:
// - lfs/
//   - locks/
//   - objects/
//     - <1st OID byte>
//       - <2nd OID byte>
//         - <OID hash> <- this is the object