Remove the Entire DB credential-store integration · Entire
Log in
Remove the Entire DB credential-store integration
44f774c→main·
Soph·4w ago·16 files·+18 added/-1,384 removed
git-sync carried a bespoke Entire credential path: auth.Resolve fell back to an active-user lookup in ~/.config/entire/hosts.json plus a file/keyring token store and OAuth refresh-token handling (client_id=entire-cli). Nothing in the product produces that layout anymore — the mirror-pipeline worker (the only library consumer that talks to entiredb) supplies credentials directly at the transport layer (GitHub installation tokens + per-request entire-core repo-scoped bearers), and client-side entire:// auth is owned by the separate git-remote-entire helper using the newer contexts.json model. So the lookup only ever read a store no current producer writes.
Drop entiredb.go and tokenstore.go and the LookupEntireDBCredential fallback; auth.Resolve now resolves explicit token/bearer credentials only and otherwise returns nil so the git credential helper is consulted on a 401, exactly as for any other remote. This also drops the github.com/zalando/go-keyring dependency and, with the file token store gone, removes the syscall.Flock usage that broke the Windows build (so the package now cross-compiles for windows cleanly).
Supersedes the entiredb token-refresh fix (#90) and the tokenstore Windows flock fix (#92), both of which were polishing this now-deleted code.
Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com
Sessions
de15fe82f1f3View transcript
?\ just remove .clawpatch from the commit but then just delete the folder, we don't need to keep itClaude Code·Opus 4.8[1m]·5 steps ?\ Remove the Entire DB credential-store integrationCodex·GPT-5.5·1 step ?\ Remove the Entire DB credential-store integrationCodex·GPT-5.5·1 step ?\ Remove the Entire DB credential-store integrationCodex·GPT-5.5·1 step ?\ Remove the Entire DB credential-store integrationCodex·GPT-5.5·1 step ?\ Remove the Entire DB credential-store integrationCodex·GPT-5.5·1 step ?\ Remove the Entire DB credential-store integrationCodex·GPT-5.5·1 step ?\ Remove the Entire DB credential-store integrationCodex·GPT-5.5·1 step ?\ Review the code changes against the base branch 'main'. The merge base commit for this comparison is 368750cee35f31935640a34ad199e889fc60f1a8. Run `git diff 368750cee35f31935640a34ad199e889fc60f1a8` tCodex·GPT-5.5·1 step
Changes
16
MCHANGELOG.md+4
cmd/git-sync/internal/sha256convert
Msha256convert.go+1/-4
docs
Marchitecture.md+2/-1
Mgo.mod+1/-4
Mgo.sum-8
internal
auth
Mauth.go+7/-16
Mauth_test.go+1/-404
Dentiredb.go-223
Dtokenstore.go-131
Dtokenstore_lock_unix.go-35
Dtokenstore_lock_windows.go-53
syncer
Mauth_test.go+1/-116
Dentire_local_smoke_test.go-337
Dentire_local_smoke_test_test.go-36
Msyncer.go+1/-4
Mmise.toml-12
6 unmodified lines
7
8
9
10
11
12
13
14
15
16
6 unmodified lines
## [Unreleased]
### Removed
- The built-in Entire DB credential store integration (`hosts.json` active-user lookup, the file/keyring token store, and OAuth refresh-token handling). `auth.Resolve` now resolves only explicit token/bearer credentials; everything else defers to the git credential helper on a 401, exactly as for any other remote. The Entire mirroring pipeline and the `git-remote-entire` helper already supply credentials directly (installation / repo-scoped tokens at the transport layer), so nothing produced the `hosts.json`/token-store layout this code read. This drops the `github.com/zalando/go-keyring` dependency and, with the file token store gone, the package now compiles on Windows without a `flock` shim.
### Fixed
- Concurrent **create** races on the target are now classified as `ErrTargetRefMoved`, matching the existing concurrent-update handling. entire-server rejects a create command (old = zero hash) for a ref that already exists with `already exists`; git-sync only plans a create for a ref it found absent at plan time, so that rejection is an unambiguous benign race — a second sync of the same repo created the ref first — exactly like the update-side `remote ref has changed`. Previously only the update reason was in `concurrentMoveMarkers`, so a create race fell through as a generic push failure and `errors.Is(err, ErrTargetRefMoved)` returned false; embedders that key redelivery/alerting off the sentinel (e.g. mirror-pipeline's worker) misclassified it as a hard sync failure. Both the create and update CAS rejections now satisfy `errors.Is(err, ErrTargetRefMoved)`.
MCHANGELOG.md+4
944 unmodified lines
945
946
947
948
948
949
950
951
952
953
954
955
956
954
955
956
944 unmodified lines
if ep.Scheme != "http" && ep.Scheme != "https" {
return nil, nil, nil, fmt.Errorf("convert-sha256 currently supports HTTP/HTTPS sources only; got %q", ep.Scheme)
}
authMethod, err := auth.Resolve(auth.Endpoint{
authMethod := auth.Resolve(auth.Endpoint{
Username: req.SourceAuth.Username,
Token: req.SourceAuth.Token,
BearerToken: req.SourceAuth.BearerToken,
SkipTLSVerify: req.SourceAuth.SkipTLSVerify,
}, ep)
if err != nil {
return nil, nil, nil, fmt.Errorf("resolve source auth: %w", err)
}
httpClient := &http.Client{Transport: gitproto.NewHTTPTransport(req.SourceAuth.SkipTLSVerify)}
conn := gitproto.NewHTTPConnWithClient(ep, "source", normalizeAuth(authMethod), httpClient)
conn.FollowInfoRefsRedirect = req.SourceFollowInfoRefsRedirect
Mcmd/git-sync/internal/sha256convert/sha256convert.go+1/-4
103 unmodified lines
104
105
106
107
107
108
109
110
111
103 unmodified lines
- `internal/validation`
- input normalization and front-loaded validation
- `internal/auth`
- credential lookup, Entire token handling, token store behavior
- explicit token/bearer auth and git credential-helper integration
(lookup deferred until the server returns 401)
- `internal/strategy/bootstrap`
- one-shot relay bootstrap and batched bootstrap
- `internal/strategy/incremental`
Mdocs/architecture.md+2/-1
8 unmodified lines
9
10
11
12
13
12
13
14
15
16
17
20
18
19
20
24
21
22
23
4 unmodified lines
28
29
30
31
32
33
8 unmodified lines
github.com/go-git/go-git/v6 v6.0.0-alpha.4.0.20260521151600-590487407c38
github.com/spf13/cobra v1.10.2
github.com/stretchr/testify v1.11.1
github.com/zalando/go-keyring v0.2.8
golang.org/x/sys v0.44.0
)
require (
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/ProtonMail/go-crypto v1.4.1 // indirect
github.com/cloudflare/circl v1.6.3 // indirect
github.com/danieljoos/wincred v1.2.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg/v2 v2.0.2 // indirect
github.com/godbus/dbus/v5 v5.2.2 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kevinburke/ssh_config v1.6.0 // indirect
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
4 unmodified lines
golang.org/x/crypto v0.51.0 // indirect
golang.org/x/net v0.54.0 // indirect
golang.org/x/sync v0.20.0 // indirect
golang.org/x/sys v0.44.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Mgo.mod+1/-4
8 unmodified lines
9
10
11
12
13
12
13
14
9 unmodified lines
24
25
26
29
30
27
28
29
17 unmodified lines
47
48
49
54
55
50
51
52
59
60
53
54
55
8 unmodified lines
github.com/cloudflare/circl v1.6.3 h1:9GPOhQGF9MCYUeXyMYlqTR6a5gTrgR/fBLXvUgtVcg8=
github.com/cloudflare/circl v1.6.3/go.mod h1:2eXP6Qfat4O/Yhh8BznvKnJ+uzEoTQ6jVKJRn81BiS4=
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/danieljoos/wincred v1.2.3 h1:v7dZC2x32Ut3nEfRH+vhoZGvN72+dQ/snVXo/vMFLdQ=
github.com/danieljoos/wincred v1.2.3/go.mod h1:6qqX0WNrS4RzPZ1tnroDzq9kY3fu1KwE7MRLQK4X0bs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
9 unmodified lines
github.com/go-git/go-git-fixtures/v6 v6.0.0-alpha.1/go.mod h1:ECf1MqJlBdYpKggBrOXjo/0EnvRZx6D++I86UYjPgAQ=
github.com/go-git/go-git/v6 v6.0.0-alpha.4.0.20260521151600-590487407c38 h1:uA2L2RZQTkmvHjzBqMNMFR+UWdjicJBc0UqhCrgodZs=
github.com/go-git/go-git/v6 v6.0.0-alpha.4.0.20260521151600-590487407c38/go.mod h1:4ODa/G7hPWrh4Y+7lmt59Ij3zW38IEfvRoAZxLYYBhc=
github.com/godbus/dbus/v5 v5.2.2 h1:TUR3TgtSVDmjiXOgAAyaZbYmIeP3DPkld3jgKGV8mXQ=
github.com/godbus/dbus/v5 v5.2.2/go.mod h1:3AAv2+hPq5rdnr5txxxRwiGjPXamgoIHgz9FPBfOp3c=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kevinburke/ssh_config v1.6.0 h1:J1FBfmuVosPHf5GRdltRLhPJtJpTlMdKTBjRgTaQBFY=
17 unmodified lines
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/zalando/go-keyring v0.2.8 h1:6sD/Ucpl7jNq10rM2pgqTs0sZ9V3qMrqfIIy5YPccHs=
github.com/zalando/go-keyring v0.2.8/go.mod h1:tsMo+VpRq5NGyKfxoBVjCuMrG47yj8cmakZDO5QGii0=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/crypto v0.51.0 h1:IBPXwPfKxY7cWQZ38ZCIRPI50YLeevDLlLnyC5wRGTI=
golang.org/x/crypto v0.51.0/go.mod h1:8AdwkbraGNABw2kOX6YFPs3WM22XqI4EXEd8g+x7Oc8=
Mgo.sum-8
27 unmodified lines
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
31
32
33
34
35
36
37
38
39
40
27 unmodified lines
SkipTLSVerify bool
}
// Resolve resolves the auth method for the given endpoint configuration.
// Order: explicit flags → Entire DB token → anonymous (with the git credential
// helper deferred until the server returns 401, matching git's own behaviour).
func Resolve(raw Endpoint, ep *url.URL) (Method, error) {
if auth := explicitAuth(raw); auth != nil {
return auth, nil
}
if !isHTTPEndpoint(ep) {
return nil, nil //nolint:nilnil // nil signals no auth method found at this stage
}
if username, password, ok, err := LookupEntireDBCredential(raw, ep); err != nil {
return nil, err // issue #7: surface refresh failure explicitly
} else if ok {
return &transporthttp.BasicAuth{Username: username, Password: password}, nil
}
return nil, nil //nolint:nilnil // nil signals no auth method found at this stage
// Resolve resolves the auth method for the given endpoint configuration:
// explicit token/bearer flags, or nil to proceed anonymously (the git
// credential helper is consulted later, deferred until the server returns 401,
// matching git's own behaviour). The endpoint is unused now but kept in the
// signature so callers needn't special-case it.
func Resolve(raw Endpoint, _ *url.URL) Method {
return explicitAuth(raw)
}
func explicitAuth(raw Endpoint) Method {
Minternal/auth/auth.go+7/-16
2 unmodified lines
3
4
5
6
6
7
9
10
8
9
13
10
11
12
17
13
14
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
15
16
17
156 unmodified lines
174
175
176
284
177
178
179
44 unmodified lines
224
225
226
335
336
337
338
339
340
341
342
343
344
345
346
347
348
227
228
229
230
317 unmodified lines
548
549
550
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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
2 unmodified lines
import (
"context"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
"github.com/go-git/go-git/v6/plumbing/transport"
transporthttp "github.com/go-git/go-git/v6/plumbing/transport/http"
"github.com/zalando/go-keyring"
)
func TestDecodeTokenWithExpiration(t *testing.T) {
tests := []struct {
name string
encoded string
wantToken string
wantZero bool // if true, expect time.Time zero value
wantUnix int64 // checked only when wantZero is false
}{
{
name: "token with pipe-separated unix timestamp",
encoded: "mytoken|12345",
wantToken: "mytoken",
wantUnix: 12345,
},
{
name: "plain token without pipe",
encoded: "plain-token",
wantToken: "plain-token",
wantZero: true,
},
{
name: "empty string",
encoded: "",
wantToken: "",
wantZero: true,
},
{
name: "pipe with non-numeric suffix falls back to full string",
encoded: "tok|notanumber",
wantToken: "tok|notanumber",
wantZero: true,
},
{
name: "multiple pipes uses last one",
encoded: "a|b|99999",
wantToken: "a|b",
wantUnix: 99999,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
token, ts := decodeTokenWithExpiration(tt.encoded)
if token != tt.wantToken {
t.Errorf("token = %q, want %q", token, tt.wantToken)
}
if tt.wantZero {
if !ts.IsZero() {
t.Errorf("expected zero time, got %v", ts)
}
} else {
if ts.Unix() != tt.wantUnix {
t.Errorf("timestamp = %d, want %d", ts.Unix(), tt.wantUnix)
}
}
})
}
}
func TestTokenExpiredOrExpiring(t *testing.T) {
tests := []struct {
name string
expiresAt time.Time
want bool
}{
{
name: "zero time is treated as expired",
expiresAt: time.Time{},
want: true,
},
{
name: "far future is not expired",
expiresAt: time.Now().Add(1 * time.Hour),
want: false,
},
{
name: "past time is expired",
expiresAt: time.Now().Add(-1 * time.Hour),
want: true,
},
{
name: "expiring within 5 minute window is treated as expired",
expiresAt: time.Now().Add(2 * time.Minute),
want: true,
},
{
name: "just beyond 5 minute window is not expired",
expiresAt: time.Now().Add(10 * time.Minute),
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tokenExpiredOrExpiring(tt.expiresAt)
if got != tt.want {
t.Errorf("tokenExpiredOrExpiring(%v) = %v, want %v", tt.expiresAt, got, tt.want)
}
})
}
}
func TestCredentialInput_FillQueryWithEmbeddedUser(t *testing.T) {
ep := &url.URL{
Scheme: "https",
156 unmodified lines
wantType string // "token", "basic", "nil"
wantUser string
wantPass string
wantErr bool
}{
{
name: "bearer token set returns TokenAuth",
44 unmodified lines
return nil, nil
}
// Also ensure ENTIRE_CONFIG_DIR points nowhere so EntireDB lookup
// doesn't find anything.
t.Setenv("ENTIRE_CONFIG_DIR", t.TempDir())
got, err := Resolve(tt.raw, tt.ep)
if tt.wantErr {
if err == nil {
t.Fatal("expected error, got nil")
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
got := Resolve(tt.raw, tt.ep)
switch tt.wantType {
case "nil":
317 unmodified lines
}
}
}
func TestEndpointBaseURL(t *testing.T) {
tests := []struct {
name string
ep *url.URL
want string
}{
{
name: "https host",
ep: &url.URL{Scheme: "https", Host: "example.com"},
want: "https://example.com",
},
{
name: "http host with port",
ep: &url.URL{Scheme: "http", Host: "example.com:8080"},
want: "http://example.com:8080",
},
{
name: "nil endpoint",
ep: nil,
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := endpointBaseURL(tt.ep)
if got != tt.want {
t.Errorf("endpointBaseURL() = %q, want %q", got, tt.want)
}
})
}
}
func TestEndpointCredentialHost(t *testing.T) {
tests := []struct {
name string
ep *url.URL
want string
}{
{
name: "host without port",
ep: &url.URL{Host: "example.com"},
want: "example.com",
},
{
name: "host with port",
ep: &url.URL{Host: "example.com:8080"},
want: "example.com:8080",
},
{
name: "nil endpoint",
ep: nil,
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := endpointCredentialHost(tt.ep)
if got != tt.want {
t.Errorf("endpointCredentialHost() = %q, want %q", got, tt.want)
}
})
}
}
func TestReadWriteFileToken(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "tokens.json")
// Write a token and read it back.
if err := writeFileToken(path, "svc1", "user1", "pass1"); err != nil {
t.Fatalf("writeFileToken: %v", err)
}
got, err := readFileToken(path, "svc1", "user1")
if err != nil {
t.Fatalf("readFileToken: %v", err)
}
if got != "pass1" {
t.Errorf("readFileToken = %q, want %q", got, "pass1")
}
// Read missing service returns ErrNotFound.
_, err = readFileToken(path, "missing-svc", "user1")
if !errors.Is(err, keyring.ErrNotFound) {
t.Errorf("expected ErrNotFound for missing service, got %v", err)
}
// Write a second service and read each back.
if err := writeFileToken(path, "svc2", "user2", "pass2"); err != nil {
t.Fatalf("writeFileToken svc2: %v", err)
}
got1, err := readFileToken(path, "svc1", "user1")
if err != nil {
t.Fatalf("readFileToken svc1 after second write: %v", err)
}
if got1 != "pass1" {
t.Errorf("svc1 token = %q, want %q", got1, "pass1")
}
got2, err := readFileToken(path, "svc2", "user2")
if err != nil {
t.Fatalf("readFileToken svc2: %v", err)
}
if got2 != "pass2" {
t.Errorf("svc2 token = %q, want %q", got2, "pass2")
}
// Read file that doesn't exist returns ErrNotFound.
_, err = readFileToken(filepath.Join(dir, "nonexistent.json"), "svc1", "user1")
if !errors.Is(err, keyring.ErrNotFound) {
t.Errorf("expected ErrNotFound for missing file, got %v", err)
}
}
func TestIsNotFound(t *testing.T) {
if !isNotFound(keyring.ErrNotFound) {
t.Error("expected isNotFound(keyring.ErrNotFound) = true")
}
if isNotFound(errors.New("some other error")) {
t.Error("expected isNotFound(other error) = false")
}
// Wrapped ErrNotFound should also be detected.
wrapped := fmt.Errorf("wrapped: %w", keyring.ErrNotFound)
if !isNotFound(wrapped) {
t.Error("expected isNotFound(wrapped ErrNotFound) = true")
}
}
func TestReadFileTokenEmptyPath(t *testing.T) {
_, err := readFileToken("", "svc", "user")
if !errors.Is(err, keyring.ErrNotFound) {
t.Errorf("expected ErrNotFound for empty path, got %v", err)
}
}
func TestWriteFileTokenEmptyPath(t *testing.T) {
err := writeFileToken("", "svc", "user", "pass")
if err == nil {
t.Error("expected error for empty path, got nil")
}
if !errors.Is(err, os.ErrInvalid) {
t.Errorf("expected os.ErrInvalid, got %v", err)
}
}
func TestGetTokenWithRefresh(t *testing.T) {
t.Run("non-expired token returned without refresh", func(t *testing.T) {
dir := t.TempDir()
tokenPath := filepath.Join(dir, "tokens.json")
t.Setenv("ENTIRE_TOKEN_STORE", "file")
t.Setenv("ENTIRE_TOKEN_STORE_PATH", tokenPath)
// Set up a hosts.json so lookupEntireDBToken would find a user.
configDir := t.TempDir()
t.Setenv("ENTIRE_CONFIG_DIR", configDir)
hostsJSON := `{"example.com":{"activeUser":"alice","users":["alice"]}}`
if err := os.WriteFile(filepath.Join(configDir, "hosts.json"), []byte(hostsJSON), 0o644); err != nil {
t.Fatal(err)
}
// Write a non-expired token (expires 1 hour from now).
futureExpiry := time.Now().Add(1 * time.Hour).Unix()
encoded := fmt.Sprintf("my-access-token|%d", futureExpiry)
if err := WriteStoredToken(credentialService("example.com"), "alice", encoded); err != nil {
t.Fatalf("WriteStoredToken: %v", err)
}
// getTokenWithRefresh should return the token without attempting refresh.
got, err := getTokenWithRefresh(context.Background(), "example.com", "alice", "https://example.com", false)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != "my-access-token" {
t.Errorf("got %q, want %q", got, "my-access-token")
}
})
t.Run("expired token with no refresh token returns error", func(t *testing.T) {
dir := t.TempDir()
tokenPath := filepath.Join(dir, "tokens.json")
t.Setenv("ENTIRE_TOKEN_STORE", "file")
t.Setenv("ENTIRE_TOKEN_STORE_PATH", tokenPath)
configDir := t.TempDir()
t.Setenv("ENTIRE_CONFIG_DIR", configDir)
// Write an expired token (expired 1 hour ago).
pastExpiry := time.Now().Add(-1 * time.Hour).Unix()
encoded := fmt.Sprintf("stale-token|%d", pastExpiry)
if err := WriteStoredToken(credentialService("example.com"), "bob", encoded); err != nil {
t.Fatalf("WriteStoredToken: %v", err)
}
// No refresh token stored — refresh should fail.
_, err := getTokenWithRefresh(context.Background(), "example.com", "bob", "https://example.com", false)
if err == nil {
t.Fatal("expected error for expired token with no refresh token, got nil")
}
// Issue #7: error should mention refresh failure.
if !strings.Contains(err.Error(), "refresh failed") {
t.Errorf("error should mention refresh failure, got: %v", err)
}
})
}
func TestReadWriteStoredTokenFileStore(t *testing.T) {
dir := t.TempDir()
tokenPath := filepath.Join(dir, "tokens.json")
t.Setenv("ENTIRE_TOKEN_STORE", "file")
t.Setenv("ENTIRE_TOKEN_STORE_PATH", tokenPath)
// Write and read back — round-trip.
if err := WriteStoredToken("svc:test", "user1", "secret-value"); err != nil {
t.Fatalf("WriteStoredToken: %v", err)
}
got, err := ReadStoredToken("svc:test", "user1")
if err != nil {
t.Fatalf("ReadStoredToken: %v", err)
}
if got != "secret-value" {
t.Errorf("ReadStoredToken = %q, want %q", got, "secret-value")
}
// Read a missing key returns ErrNotFound.
_, err = ReadStoredToken("svc:missing", "nobody")
if !isNotFound(err) {
t.Errorf("expected ErrNotFound for missing key, got %v", err)
}
// Read from a non-existent file path returns ErrNotFound.
t.Setenv("ENTIRE_TOKEN_STORE_PATH", filepath.Join(dir, "nonexistent", "tokens.json"))
_, err = ReadStoredToken("svc:test", "user1")
if !isNotFound(err) {
t.Errorf("expected ErrNotFound for missing file, got %v", err)
}
}
func TestEncodeTokenWithExpiration(t *testing.T) {
before := time.Now().Unix()
encoded := encodeTokenWithExpiration("mytoken", 3600)
after := time.Now().Unix()
// Format should be "token|unixtime".
parts := strings.SplitN(encoded, "|", 2)
if len(parts) != 2 {
t.Fatalf("expected format 'token|unixtime', got %q", encoded)
}
if parts[0] != "mytoken" {
t.Errorf("token part = %q, want %q", parts[0], "mytoken")
}
ts, err := strconv.ParseInt(parts[1], 10, 64)
if err != nil {
t.Fatalf("failed to parse timestamp: %v", err)
}
// The timestamp should be now + 3600 (within the before/after window).
if ts < before+3600 || ts > after+3600 {
t.Errorf("timestamp %d not in expected range [%d, %d]", ts, before+3600, after+3600)
}
}
func TestCredentialService(t *testing.T) {
got := credentialService("example.com")
want := "entire:example.com"
if got != want {
t.Errorf("credentialService(%q) = %q, want %q", "example.com", got, want)
}
}
func TestLookupEntireDBTokenNotConfigured(t *testing.T) {
// Set ENTIRE_CONFIG_DIR to an empty temp dir (no hosts.json).
configDir := t.TempDir()
t.Setenv("ENTIRE_CONFIG_DIR", configDir)
got, err := lookupEntireDBToken("example.com", "https://example.com", false)
if err != nil {
t.Fatalf("expected nil error, got %v", err)
}
if got != "" {
t.Errorf("expected empty string, got %q", got)
}
}
Minternal/auth/auth_test.go+1/-404
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
package auth
import (
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/zalando/go-keyring"
)
func isNotFound(err error) bool {
return errors.Is(err, keyring.ErrNotFound)
}
const entireCLIClientID = "entire-cli"
type entireAuthHostInfo struct {
ActiveUser string `json:"activeUser"`
Users []string `json:"users"`
}
type oauthTokenResponse struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
ExpiresIn int64 `json:"expires_in"`
}
// LookupEntireDBCredential looks up credentials from the Entire token store.
// Returns (username, password, true, nil) on success, ("", "", false, nil) when
// no credential is configured, or ("", "", false, err) when a credential exists
// but refresh failed (issue #7).
func LookupEntireDBCredential(raw Endpoint, ep *url.URL) (string, string, bool, error) {
if ep == nil || ep.Host == "" {
return "", "", false, nil
}
credHost := endpointCredentialHost(ep)
token, err := lookupEntireDBToken(credHost, endpointBaseURL(ep), raw.SkipTLSVerify)
if err != nil {
return "", "", false, err
}
if token == "" {
return "", "", false, nil
}
username := raw.Username
if username == "" {
username = defaultGitUsername
}
return username, token, true, nil
}
func endpointBaseURL(ep *url.URL) string {
if ep == nil || ep.Hostname() == "" {
return ""
}
scheme := ep.Scheme
if scheme == "" {
scheme = "https"
}
host := ep.Host // includes port if present in url.URL
return scheme + "://" + host
}
func endpointCredentialHost(ep *url.URL) string {
if ep == nil {
return ""
}
return ep.Host // includes port if present in url.URL
}
func lookupEntireDBToken(host, baseURL string, skipTLS bool) (string, error) {
configDir := os.Getenv("ENTIRE_CONFIG_DIR")
if configDir == "" {
home, err := os.UserHomeDir()
if err != nil {
return "", nil //nolint:nilerr // missing config dir means no stored credentials, not an error
}
configDir = filepath.Join(home, ".config", "entire")
}
username, ok := loadEntireDBActiveUser(host, configDir)
if !ok || username == "" {
return "", nil
}
return getTokenWithRefresh(context.Background(), host, username, baseURL, skipTLS)
}
func loadEntireDBActiveUser(host, configDir string) (string, bool) {
data, err := os.ReadFile(filepath.Join(configDir, "hosts.json"))
if err != nil {
return "", false
}
var hosts map[string]*entireAuthHostInfo
if err := json.Unmarshal(data, &hosts); err != nil {
return "", false
}
info := hosts[host]
if info == nil || info.ActiveUser == "" {
return "", false
}
return info.ActiveUser, true
}
// getTokenWithRefresh retrieves a token, refreshing it if expired.
// On refresh failure, returns the stale token with a nil error rather than
// propagating the refresh error silently (issue #7).
func getTokenWithRefresh(ctx context.Context, host, username, baseURL string, skipTLS bool) (string, error) {
encoded, err := ReadStoredToken(credentialService(host), username)
if err != nil {
// "Not found" means no credential is configured — not an error.
// Only propagate actual storage failures.
if isNotFound(err) {
return "", nil
}
return "", err
}
token, expiresAt := decodeTokenWithExpiration(encoded)
if token == "" {
return "", nil
}
if !tokenExpiredOrExpiring(expiresAt) {
return token, nil
}
refreshed, err := refreshAccessToken(ctx, host, username, baseURL, skipTLS)
if err != nil {
// Issue #7: surface refresh failure explicitly instead of silently reusing stale token.
return "", fmt.Errorf("token expired and refresh failed for %s@%s: %w", username, host, err)
}
return refreshed, nil
}
func decodeTokenWithExpiration(encoded string) (string, time.Time) {
idx := strings.LastIndex(encoded, "|")
if idx == -1 {
return encoded, time.Time{}
}
token := encoded[:idx]
ts, err := strconv.ParseInt(encoded[idx+1:], 10, 64)
if err != nil {
return encoded, time.Time{}
}
return token, time.Unix(ts, 0)
}
func tokenExpiredOrExpiring(expiresAt time.Time) bool {
if expiresAt.IsZero() {
return true
}
return time.Now().Add(5 * time.Minute).After(expiresAt)
}
func refreshAccessToken(ctx context.Context, host, username, baseURL string, skipTLS bool) (string, error) {
refreshToken, err := ReadStoredToken(credentialService(host)+":refresh", username)
if err != nil {
return "", err
}
if refreshToken == "" || baseURL == "" {
return "", errors.New("missing refresh token or base url")
}
form := url.Values{}
form.Set("grant_type", "refresh_token")
form.Set("refresh_token", refreshToken)
form.Set("client_id", entireCLIClientID)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimRight(baseURL, "/")+"/oauth/token", strings.NewReader(form.Encode()))
if err != nil {
return "", fmt.Errorf("create token refresh request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
client := &http.Client{
Timeout: 30 * time.Second,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: skipTLS}, //nolint:gosec // InsecureSkipVerify is controlled by user flag
},
}
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("execute token refresh request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("refresh failed with status %d", resp.StatusCode)
}
var tokenResp oauthTokenResponse
if err := json.NewDecoder(resp.Body).Decode(&tokenResp); err != nil {
return "", fmt.Errorf("decode token refresh response: %w", err)
}
if tokenResp.AccessToken == "" {
return "", errors.New("empty access token in refresh response")
}
if err := WriteStoredToken(
credentialService(host),
username,
encodeTokenWithExpiration(tokenResp.AccessToken, tokenResp.ExpiresIn),
); err != nil {
return "", err
}
if tokenResp.RefreshToken != "" {
//nolint:errcheck // best-effort refresh token storage; access token already saved successfully
WriteStoredToken(credentialService(host)+":refresh", username, tokenResp.RefreshToken)
}
return tokenResp.AccessToken, nil
}
func encodeTokenWithExpiration(token string, expiresIn int64) string {
return fmt.Sprintf("%s|%d", token, time.Now().Unix()+expiresIn)
}
func credentialService(host string) string {
return "entire:" + host
}
Dinternal/auth/entiredb.go-223
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
package auth
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/zalando/go-keyring"
)
// ReadStoredToken reads a token from the configured store (keyring or file).
func ReadStoredToken(service, username string) (string, error) {
if os.Getenv("ENTIRE_TOKEN_STORE") == "file" {
return readFileToken(fileTokenPath(), service, username)
}
token, err := keyring.Get(service, username)
if err != nil {
return "", fmt.Errorf("read keyring token: %w", err)
}
return token, nil
}
// WriteStoredToken writes a token to the configured store.
func WriteStoredToken(service, username, password string) error {
if os.Getenv("ENTIRE_TOKEN_STORE") == "file" {
return writeFileToken(fileTokenPath(), service, username, password)
}
if err := keyring.Set(service, username, password); err != nil {
return fmt.Errorf("write keyring token: %w", err)
}
return nil
}
func fileTokenPath() string {
path := os.Getenv("ENTIRE_TOKEN_STORE_PATH")
if path != "" {
return path
}
home, err := os.UserHomeDir()
if err != nil {
return ""
}
return filepath.Join(home, ".config", "entiredb", "tokens.json")
}
func readFileToken(path, service, username string) (string, error) {
if path == "" {
return "", keyring.ErrNotFound
}
// Acquire shared lock for concurrent read safety (issue #10).
unlock, err := flockShared(path)
if err != nil {
// If we can't lock (e.g., file doesn't exist yet), fall through to direct read.
return readFileTokenDirect(path, service, username)
}
defer unlock()
return readFileTokenDirect(path, service, username)
}
func readFileTokenDirect(path, service, username string) (string, error) {
data, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return "", keyring.ErrNotFound
}
return "", fmt.Errorf("read token store file: %w", err)
}
var store map[string]map[string]string
if err := json.Unmarshal(data, &store); err != nil {
return "", fmt.Errorf("unmarshal token store: %w", err)
}
users := store[service]
if users == nil {
return "", keyring.ErrNotFound
}
password, ok := users[username]
if !ok {
return "", keyring.ErrNotFound
}
return password, nil
}
// writeFileToken writes a token to the file store with exclusive file locking
// to prevent corruption from concurrent processes (issue #10).
func writeFileToken(path, service, username, password string) error {
if path == "" {
return os.ErrInvalid
}
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
return fmt.Errorf("create config directory: %w", err)
}
// Acquire exclusive lock for the write.
unlock, err := flockExclusive(path)
if err != nil {
return err
}
defer unlock()
// Re-read under lock to avoid lost updates.
store := map[string]map[string]string{}
if data, err := os.ReadFile(path); err == nil {
if err := json.Unmarshal(data, &store); err != nil {
return fmt.Errorf("unmarshal token store: %w", err)
}
} else if !os.IsNotExist(err) {
return fmt.Errorf("read token store file: %w", err)
}
if store[service] == nil {
store[service] = map[string]string{}
}
store[service][username] = password
data, err := json.Marshal(store)
if err != nil {
return fmt.Errorf("marshal token store: %w", err)
}
// Atomic write: write to temp file then rename to prevent corruption on crash.
tmp := path + ".tmp"
if err := os.WriteFile(tmp, data, 0o600); err != nil {
return fmt.Errorf("write token store temp file: %w", err)
}
if err := os.Rename(tmp, path); err != nil {
return fmt.Errorf("rename token store temp file: %w", err)
}
return nil
}
// flockShared / flockExclusive are defined per-platform: tokenstore_lock_unix.go
// uses syscall.Flock, tokenstore_lock_windows.go provides a compiling fallback
// (syscall.Flock and the LOCK_* constants do not exist on Windows).
Dinternal/auth/tokenstore.go-131
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
//go:build !windows
package auth
import (
"fmt"
"os"
"syscall"
)
// flockShared acquires a shared (read) lock on path+".lock".
func flockShared(path string) (func(), error) {
return flockOpen(path+".lock", syscall.LOCK_SH)
}
// flockExclusive acquires an exclusive (write) lock on path+".lock".
func flockExclusive(path string) (func(), error) {
return flockOpen(path+".lock", syscall.LOCK_EX)
}
func flockOpen(lockPath string, how int) (func(), error) {
f, err := os.OpenFile(lockPath, os.O_CREATE|os.O_RDWR, 0o600)
if err != nil {
return nil, fmt.Errorf("open lock file: %w", err)
}
if err := syscall.Flock(int(f.Fd()), how); err != nil {
f.Close()
return nil, fmt.Errorf("acquire file lock: %w", err)
}
return func() {
//nolint:errcheck // unlock errors on close are not actionable
syscall.Flock(int(f.Fd()), syscall.LOCK_UN)
f.Close()
}, nil
}
Dinternal/auth/tokenstore_lock_unix.go-35
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
//go:build windows
package auth
import (
"fmt"
"os"
"golang.org/x/sys/windows"
)
// Windows has no flock(2); use LockFileEx on a dedicated ".lock" file for the
// same advisory, interprocess mutual exclusion the Unix path gets from flock.
// writeFileToken relies on this to serialize its read-modify-write (and the
// shared temp-file write that precedes the rename), so a no-op would let
// concurrent logins/refreshes lose an update.
// flockShared acquires a shared (read) lock on path+".lock".
func flockShared(path string) (func(), error) {
return flockOpen(path+".lock", 0)
}
// flockExclusive acquires an exclusive (write) lock on path+".lock".
func flockExclusive(path string) (func(), error) {
return flockOpen(path+".lock", windows.LOCKFILE_EXCLUSIVE_LOCK)
}
func flockOpen(lockPath string, flags uint32) (func(), error) {
f, err := os.OpenFile(lockPath, os.O_CREATE|os.O_RDWR, 0o600)
if err != nil {
return nil, fmt.Errorf("open lock file: %w", err)
}
// Lock the entire file range, blocking until the lock is available
// (no LOCKFILE_FAIL_IMMEDIATELY), matching flock's blocking semantics.
//
// os.OpenFile yields a synchronous handle (Go does not pass
// FILE_FLAG_OVERLAPPED), so LockFileEx blocks until the lock is granted and
// never returns ERROR_IO_PENDING — that pending/GetOverlappedResult path
// only applies to handles opened for asynchronous I/O. Treating any error
// as failure is therefore correct here; this matches the long-standing
// github.com/gofrs/flock implementation.
if err := windows.LockFileEx(windows.Handle(f.Fd()), flags, 0, maxUint32, maxUint32, new(windows.Overlapped)); err != nil {
f.Close()
return nil, fmt.Errorf("acquire file lock: %w", err)
}
return func() {
//nolint:errcheck // unlock errors on close are not actionable
windows.UnlockFileEx(windows.Handle(f.Fd()), 0, maxUint32, maxUint32, new(windows.Overlapped))
f.Close()
}, nil
}
const maxUint32 = ^uint32(0)
Dinternal/auth/tokenstore_lock_windows.go-53
1 unmodified line
2
3
4
5
6
5
8
9
10
6
12
7
8
9
15 unmodified lines
25
26
27
34
28
29
30
31
38
39
40
32
33
34
54 unmodified lines
89
90
91
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
1 unmodified line
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"
"github.com/go-git/go-git/v6/plumbing/transport"
transporthttp "github.com/go-git/go-git/v6/plumbing/transport/http"
15 unmodified lines
return nil, nil
}
resolved, err := auth.Resolve(auth.Endpoint{
resolved := auth.Resolve(auth.Endpoint{
Username: "git",
Token: "explicit-token",
}, ep)
if err != nil {
t.Fatalf("resolve auth: %v", err)
}
basic, ok := resolved.(*transporthttp.BasicAuth)
if !ok {
54 unmodified lines
t.Fatalf("wrapped base transport = %T, want %T", rt.base, baseTransport)
}
}
func TestResolveAuthMethodUsesEntireDBStoredToken(t *testing.T) {
configDir := t.TempDir()
tokenStorePath := filepath.Join(t.TempDir(), "tokens.json")
t.Setenv("ENTIRE_CONFIG_DIR", configDir)
t.Setenv("ENTIRE_TOKEN_STORE", "file")
t.Setenv("ENTIRE_TOKEN_STORE_PATH", tokenStorePath)
ep, err := transport.ParseURL("https://localhost:8080/git/test/repo")
if err != nil {
t.Fatalf("new endpoint: %v", err)
}
credHost := ep.Host
writeEntireDBHostsFile(t, configDir, credHost, "test-user")
// Store token with a future expiration so it's not treated as expired (issue #7).
futureExpiry := time.Now().Unix() + 3600
if err := auth.WriteStoredToken("entire:"+credHost, "test-user", fmt.Sprintf("stored-token|%d", futureExpiry)); err != nil {
t.Fatalf("write token: %v", err)
}
originalCred := auth.GitCredentialCommand
t.Cleanup(func() { auth.GitCredentialCommand = originalCred })
auth.GitCredentialCommand = func(_ context.Context, op auth.CredentialOp, input string) ([]byte, error) {
t.Fatalf("unexpected git credential %s call with input %q", op, input)
return nil, nil
}
resolved, err := auth.Resolve(auth.Endpoint{}, ep)
if err != nil {
t.Fatalf("resolve auth: %v", err)
}
basic, ok := resolved.(*transporthttp.BasicAuth)
if !ok {
t.Fatalf("expected basic auth, got %T", resolved)
}
if basic.Username != "git" || basic.Password != "stored-token" {
t.Fatalf("unexpected auth: %+v", basic)
}
}
func TestResolveAuthMethodRefreshesExpiredEntireDBToken(t *testing.T) {
configDir := t.TempDir()
tokenStorePath := filepath.Join(t.TempDir(), "tokens.json")
t.Setenv("ENTIRE_CONFIG_DIR", configDir)
t.Setenv("ENTIRE_TOKEN_STORE", "file")
t.Setenv("ENTIRE_TOKEN_STORE_PATH", tokenStorePath)
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/oauth/token" {
t.Fatalf("unexpected path: %s", r.URL.Path)
}
if err := r.ParseForm(); err != nil {
t.Fatalf("parse form: %v", err)
}
if got := r.Form.Get("refresh_token"); got != "refresh-token" {
t.Fatalf("unexpected refresh token: %s", got)
}
w.Header().Set("Content-Type", "application/json")
if _, err := w.Write([]byte(`{"access_token":"new-token","refresh_token":"new-refresh","expires_in":3600}`)); err != nil {
t.Errorf("write response: %v", err)
}
}))
defer server.Close()
ep, err := transport.ParseURL(server.URL + "/git/test/repo")
if err != nil {
t.Fatalf("new endpoint: %v", err)
}
credHost := ep.Host
writeEntireDBHostsFile(t, configDir, credHost, "test-user")
// Expired token: expiration in the past
if err := auth.WriteStoredToken("entire:"+credHost, "test-user", "expired-token|1"); err != nil {
t.Fatalf("write expired token: %v", err)
}
if err := auth.WriteStoredToken("entire:"+credHost+":refresh", "test-user", "refresh-token"); err != nil {
t.Fatalf("write refresh token: %v", err)
}
resolved, err := auth.Resolve(auth.Endpoint{SkipTLSVerify: true}, ep)
if err != nil {
t.Fatalf("resolve auth: %v", err)
}
basic, ok := resolved.(*transporthttp.BasicAuth)
if !ok {
t.Fatalf("expected basic auth, got %T", resolved)
}
if basic.Password != "new-token" {
t.Fatalf("unexpected password: %q", basic.Password)
}
}
func writeEntireDBHostsFile(t *testing.T, configDir, host, username string) {
t.Helper()
hosts := map[string]map[string]any{
host: {"activeUser": username, "users": []string{username}},
}
data, err := json.Marshal(hosts)
if err != nil {
t.Fatalf("marshal hosts: %v", err)
}
if err := os.WriteFile(filepath.Join(configDir, "hosts.json"), data, 0o600); err != nil {
t.Fatalf("write hosts: %v", err)
}
}
Minternal/syncer/auth_test.go+1/-116
package syncer
import ( "context" "encoding/json" "errors" "fmt" "net/url" "os" "os/exec" "path/filepath" "strconv" "strings" "testing"
"github.com/zalando/go-keyring" )
const entireLocalSmokeEnv = "GITSYNC_E2E_ENTIRE"
type entireHosts map[string]entireHostInfo
type entireHostInfo struct {
ActiveUser string json:"activeUser"
Users []string json:"users"
}
type entireHostsFile struct {
ActiveHost string json:"activeHost"
Hosts entireHosts json:"hosts"
}
func TestRun_EntireLocalPublicRepoSmoke(t *testing.T) { if os.Getenv(entireLocalSmokeEnv) == "" { t.Skip("set GITSYNC_E2E_ENTIRE=1 to run the Entire local smoke test") }
baseURL := firstNonEmpty( os.Getenv("GITSYNC_E2E_ENTIRE_BASE_URL"), os.Getenv("ENTIRE_BASE_URL"), ) if baseURL == "" { t.Skip("set GITSYNC_E2E_ENTIRE_BASE_URL or ENTIRE_BASE_URL to your local Entire base URL") }
sourceURL := firstNonEmpty( os.Getenv("GITSYNC_E2E_ENTIRE_SOURCE_URL"), "https://github.com/entireio/cli.git", ) branch := firstNonEmpty( os.Getenv("GITSYNC_E2E_ENTIRE_BRANCH"), "main", ) maxPackBytes, err := envInt64Default("GITSYNC_E2E_ENTIRE_MAX_PACK_BYTES", 0) if err != nil { t.Fatalf("parse GITSYNC_E2E_ENTIRE_MAX_PACK_BYTES: %v", err) } batchMaxPackBytes, err := envInt64Default("GITSYNC_E2E_ENTIRE_BATCH_MAX_PACK_BYTES", 0) if err != nil { t.Fatalf("parse GITSYNC_E2E_ENTIRE_BATCH_MAX_PACK_BYTES: %v", err) } protocolMode := firstNonEmpty( os.Getenv("GITSYNC_E2E_ENTIRE_PROTOCOL"), protocolModeAuto, ) repoName := firstNonEmpty( os.Getenv("GITSYNC_E2E_ENTIRE_REPO"), "git-sync-smoke", ) username := os.Getenv("GITSYNC_E2E_ENTIRE_USERNAME") token := os.Getenv("GITSYNC_E2E_ENTIRE_TOKEN") skipTLSVerify := envBoolDefault("GITSYNC_E2E_ENTIRE_SKIP_TLS_VERIFY", true)
host, err := credentialHost(baseURL) if err != nil { t.Fatalf("parse entire base URL: %v", err) } resolvedHost, err := resolveEntireCredentialHost(host) if err != nil { t.Fatalf("resolve Entire credential host: %v", err) } host = resolvedHost if username == "" { username, err = activeEntireUser(host) if err != nil { t.Fatalf("determine active Entire user: %v", err) } } if token == "" { token, err = lookupEntireToken(host, username) if err != nil { t.Fatalf("lookup Entire token: %v", err) } }
if err := ensureEntireRepo(t, baseURL, repoName, skipTLSVerify); err != nil { t.Fatalf("ensure Entire repo exists: %v", err) }
targetURL := strings.TrimRight(baseURL, "/") + "/git/" + username + "/" + repoName t.Logf( "Entire local smoke config: source=%s branch=%s target=%s repo=%s protocol=%s max_pack_bytes=%d target_max_pack_bytes=%d", sourceURL, branch, targetURL, repoName, protocolMode, maxPackBytes, batchMaxPackBytes, ) result, err := Run(context.Background(), Config{ Source: Endpoint{URL: sourceURL}, Target: Endpoint{ URL: targetURL, Username: "git", Token: token, SkipTLSVerify: skipTLSVerify, }, Branches: []string{branch}, Verbose: true, MaxPackBytes: maxPackBytes, TargetMaxPackBytes: batchMaxPackBytes, ProtocolMode: protocolMode, }) if err != nil { t.Fatalf("sync public source into Entire failed: %v", err) } if result.Blocked != 0 { t.Fatalf("expected no blocked refs, got %+v", result) }
sourceProbe, err := Probe(context.Background(), Config{ Source: Endpoint{URL: sourceURL}, }) if err != nil { t.Fatalf("probe source refs: %v", err) } targetProbe, err := Probe(context.Background(), Config{ Source: Endpoint{ URL: targetURL, Username: "git", Token: token, SkipTLSVerify: skipTLSVerify, }, }) if err != nil { t.Fatalf("probe target refs: %v", err) }
wantRef := "refs/heads/" + branch sourceHash, ok := refHashFromInfos(sourceProbe.Refs, wantRef) if !ok { t.Fatalf("source ref %s not found in probe response", wantRef) } targetHash, ok := refHashFromInfos(targetProbe.Refs, wantRef) if !ok { t.Fatalf("target ref %s not found in probe response", wantRef) } if sourceHash != targetHash { t.Fatalf("target ref %s mismatch: source=%s target=%s", wantRef, sourceHash, targetHash) } }
func ensureEntireRepo(t *testing.T, baseURL, repoName string, skipTLSVerify bool) error { t.Helper()
bin, err := entireCLIBinary() if err != nil { return err }
listCmd := exec.CommandContext(t.Context(), bin, "repo", "list") listCmd.Env = append(os.Environ(), entireCLIEnv(baseURL, skipTLSVerify)...) output, err := listCmd.CombinedOutput() if err != nil { return fmt.Errorf("list repos with entiredb: %w\n%s", err, strings.TrimSpace(string(output))) } for _, line := range strings.Split(string(output), "\n") { if strings.TrimSpace(line) == repoName { return nil } }
createCmd := exec.CommandContext(t.Context(), bin, "repo", "create", repoName) createCmd.Env = append(os.Environ(), entireCLIEnv(baseURL, skipTLSVerify)...) output, err = createCmd.CombinedOutput() if err != nil { return fmt.Errorf("create repo with entiredb: %w\n%s", err, strings.TrimSpace(string(output))) } return nil }
func entireCLIBinary() (string, error) { if path := os.Getenv("GITSYNC_E2E_ENTIREDB_BIN"); path != "" { return path, nil } if path, err := exec.LookPath("entiredb"); err == nil { return path, nil } return "", errors.New("could not find entiredb; set GITSYNC_E2E_ENTIREDB_BIN or put entiredb on PATH") }
func entireCLIEnv(baseURL string, skipTLSVerify bool) []string { env := []string{"ENTIRE_BASE_URL=" + baseURL} if configDir := os.Getenv("ENTIRE_CONFIG_DIR"); configDir != "" { env = append(env, "ENTIRE_CONFIG_DIR="+configDir) } if skipTLSVerify { env = append(env, "ENTIRE_TLS_SKIP_VERIFY=true") } return env }
func credentialHost(baseURL string) (string, error) { parsed, err := url.Parse(baseURL) if err != nil { return "", err } if parsed.Host == "" { return "", fmt.Errorf("missing host in %q", baseURL) } return parsed.Host, nil }
func activeEntireUser(host string) (string, error) { hosts, _, err := loadEntireHosts() if err != nil { return "", err } info, ok := hosts[host] if !ok || info.ActiveUser == "" { return "", fmt.Errorf("no active Entire user recorded for host %s", host) } return info.ActiveUser, nil }
func resolveEntireCredentialHost(requestedHost string) (string, error) { hosts, activeHost, err := loadEntireHosts() if err != nil { return "", err } if requestedHost != "" { if _, ok := hosts[requestedHost]; ok { return requestedHost, nil } } if activeHost != "" { if _, ok := hosts[activeHost]; ok { return activeHost, nil } } return requestedHost, nil }
func lookupEntireToken(host, username string) (string, error) { encoded, err := keyring.Get("entire:"+host, username) if err != nil { return "", err } if idx := strings.LastIndex(encoded, "|"); idx != -1 { return encoded[:idx], nil } return encoded, nil }
func entireConfigDir() string { if dir := os.Getenv("ENTIRE_CONFIG_DIR"); dir != "" { return dir } home, err := os.UserHomeDir() if err != nil { return "." } return filepath.Join(home, ".config", "entire") }
func loadEntireHosts() (entireHosts, string, error) { data, err := os.ReadFile(filepath.Join(entireConfigDir(), "hosts.json")) if err != nil { return nil, "", fmt.Errorf("read hosts.json: %w", err) }
var wrapped entireHostsFile if err := json.Unmarshal(data, &wrapped); err == nil && len(wrapped.Hosts) > 0 { return wrapped.Hosts, wrapped.ActiveHost, nil }
var hosts entireHosts if err := json.Unmarshal(data, &hosts); err != nil { return nil, "", fmt.Errorf("decode hosts.json: %w", err) } return hosts, "", nil }
func envBoolDefault(key string, defaultValue bool) bool { value := strings.TrimSpace(strings.ToLower(os.Getenv(key))) switch value { case "1", "true", "yes", "on": return true case "0", "false", "no", "off": return false case "": return defaultValue default: return defaultValue } }
func envInt64Default(key string, defaultValue int64) (int64, error) { value := strings.TrimSpace(os.Getenv(key)) if value == "" { return defaultValue, nil } parsed, err := strconv.ParseInt(value, 10, 64) if err != nil { return 0, err } return parsed, nil }
func firstNonEmpty(values ...string) string { for _, value := range values { if strings.TrimSpace(value) != "" { return value } } return "" }
func refHashFromInfos(refs []RefInfo, name string) (string, bool) { for _, ref := range refs { if ref.Name == name { return ref.Hash.String(), true } } return "", false }
Dinternal/syncer/entire\_local\_smoke\_test.go-337
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
package syncer
import ( "os" "path/filepath" "testing" )
func TestLoadEntireHostsWrappedFormat(t *testing.T) { configDir := t.TempDir() t.Setenv("ENTIRE_CONFIG_DIR", configDir)
data := []byte({ "activeHost": "127.0.0.1:8080", "hosts": { "127.0.0.1:8080": { "activeUser": "soph", "users": ["soph"] } } })
if err := os.WriteFile(filepath.Join(configDir, "hosts.json"), data, 0o600); err != nil {
t.Fatalf("write hosts.json: %v", err)
}
hosts, activeHost, err := loadEntireHosts() if err != nil { t.Fatalf("load hosts: %v", err) } if activeHost != "127.0.0.1:8080" { t.Fatalf("unexpected active host: %q", activeHost) } if hosts["127.0.0.1:8080"].ActiveUser != "soph" { t.Fatalf("unexpected active user: %+v", hosts["127.0.0.1:8080"]) } }
Dinternal/syncer/entire\_local\_smoke\_test\_test.go-36
368 unmodified lines
369 370 371 372 373 374 375 372 373 374 375
368 unmodified lines
BearerToken: raw.BearerToken, SkipTLSVerify: raw.SkipTLSVerify, } authMethod, err := auth.Resolve(authEp, ep) if err != nil { return nil, fmt.Errorf("resolve auth: %w", err) } authMethod := auth.Resolve(authEp, ep) stats.setSideDisplay(label, hostnameFromURL(raw.URL)) client := instrumentHTTPClient(httpClient, raw.SkipTLSVerify, label, stats) conn := gitproto.NewHTTPConnWithClient(ep, label, authMethod, client)
Minternal/syncer/syncer.go+1/-4
33 unmodified lines
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 37 38 39
33 unmodified lines
description = "Run optional live linux batched bootstrap smoke test" run = "GITSYNC_E2E_LIVE_LINUX=1 go test ./internal/syncer -run TestBootstrap_LiveLinuxSourceBatched -timeout 60m -v"
[tasks."test:entire-local-smoke"] description = "Run optional smoke syncing a public repo into a running Entire local instance" run = "GITSYNC_E2E_ENTIRE=1 go test ./internal/syncer -run TestRun_EntireLocalPublicRepoSmoke -timeout 30m -v"
[tasks."test:entire-local-smoke:linux"] description = "Run optional batched smoke syncing the Linux repo into a running Entire local instance" run = "GITSYNC_E2E_ENTIRE=1 GITSYNC_E2E_ENTIRE_SOURCE_URL=https://github.com/torvalds/linux.git GITSYNC_E2E_ENTIRE_BRANCH=master GITSYNC_E2E_ENTIRE_PROTOCOL=v2 GITSYNC_E2E_ENTIRE_BATCH_MAX_PACK_BYTES=536870912 go test ./internal/syncer -run TestRun_EntireLocalPublicRepoSmoke -timeout 60m -v"
[tasks."test:entire-local-smoke:linux:single"] description = "Run optional single-pack smoke syncing the Linux repo into a running Entire local instance" run = "GITSYNC_E2E_ENTIRE=1 GITSYNC_E2E_ENTIRE_SOURCE_URL=https://github.com/torvalds/linux.git GITSYNC_E2E_ENTIRE_BRANCH=master go test ./internal/syncer -run TestRun_EntireLocalPublicRepoSmoke -timeout 60m -v"
[tasks.build] description = "Build git-sync" run = "go build ./cmd/git-sync"
Mmise.toml-12