Skip to content

Commit 1b78ea4

Browse files
harshitsinghbhandariclaude
andcommitted
feat(tracker): glab auth token fallback in EnvTokenSource
Zero-config auth per AO-26 directive: a user who has run `glab auth login` gets working credentials with no extra setup. Lookup order is now 1. configured EnvVars (first non-empty wins) 2. GITLAB_TOKEN 3. `glab auth token` 4. else ErrNoToken The glab fallback is on by default — no opt-in flag. Failure (binary missing, not logged in, empty stdout) falls through silently to ErrNoToken so the caller's failure mode stays uniform regardless of whether glab is installed. Stderr is discarded for the same reason: an unauthenticated glab shouldn't print noise during a silent fallthrough. Tests inject a GLAB hook on EnvTokenSource to assert lookup order without touching $PATH. The hook signature matches the parallel `gh auth token` work coming to the GitHub adapter so both auth.go shapes stay parallel. Co-Authored-By: Claude Opus 4.7 <[email protected]>
1 parent ee8c3fb commit 1b78ea4

2 files changed

Lines changed: 218 additions & 4 deletions

File tree

‎backend/internal/adapters/tracker/gitlab/auth.go‎

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package gitlab
33
import (
44
"context"
55
"errors"
6+
"io"
67
"os"
8+
"os/exec"
79
"strings"
810
)
911

@@ -29,14 +31,31 @@ func (s StaticTokenSource) Token(context.Context) (string, error) {
2931
return t, nil
3032
}
3133

32-
// EnvTokenSource reads the first non-empty value from the listed env vars,
33-
// falling back to GITLAB_TOKEN. The order matters: a project-configured token
34-
// (e.g. AO_GITLAB_TOKEN) should be preferred over the global default.
34+
// EnvTokenSource resolves a token from, in order:
35+
//
36+
// 1. The configured EnvVars (first non-empty wins).
37+
// 2. GITLAB_TOKEN.
38+
// 3. `glab auth token` (the GitLab CLI), if installed.
39+
//
40+
// The glab fallback is on by default — there is no opt-in flag. If glab is
41+
// not installed, fails, or returns nothing, the source falls through silently
42+
// to ErrNoToken; the exec error is NOT propagated, so the caller's failure
43+
// mode is uniform ("no token configured") regardless of whether the user has
44+
// glab installed. This matches the zero-friction directive AO-26: a user
45+
// who has already done `glab auth login` gets working credentials with no
46+
// extra setup, while CI environments using explicit env vars are unaffected.
47+
//
48+
// GLAB is the injection seam for tests. Production callers leave it nil and
49+
// the source uses the real `glab auth token` exec; tests replace it with a
50+
// fake to assert lookup order and contract behavior without touching $PATH.
51+
// The signature mirrors the GitHub adapter's parallel `gh auth token` hook
52+
// so both adapters' auth.go shapes stay parallel.
3553
type EnvTokenSource struct {
3654
EnvVars []string
55+
GLAB func(ctx context.Context) (string, error)
3756
}
3857

39-
func (s EnvTokenSource) Token(context.Context) (string, error) {
58+
func (s EnvTokenSource) Token(ctx context.Context) (string, error) {
4059
for _, name := range s.EnvVars {
4160
if v := strings.TrimSpace(os.Getenv(name)); v != "" {
4261
return v, nil
@@ -45,5 +64,34 @@ func (s EnvTokenSource) Token(context.Context) (string, error) {
4564
if v := strings.TrimSpace(os.Getenv("GITLAB_TOKEN")); v != "" {
4665
return v, nil
4766
}
67+
fn := s.GLAB
68+
if fn == nil {
69+
fn = defaultGlabToken
70+
}
71+
// Silent fallthrough: any error or empty output from glab maps to
72+
// ErrNoToken. We intentionally do NOT surface the exec error — a missing
73+
// CLI binary is the common case and shouldn't look like a configuration
74+
// problem to the caller.
75+
if tok, err := fn(ctx); err == nil {
76+
if trimmed := strings.TrimSpace(tok); trimmed != "" {
77+
return trimmed, nil
78+
}
79+
}
4880
return "", ErrNoToken
4981
}
82+
83+
// defaultGlabToken shells out to `glab auth token`. The ctx is threaded
84+
// through so a caller wiring a startup deadline gets honored. Any error
85+
// (binary missing, not logged in, glab unhappy for any reason) is returned
86+
// for the caller to discard — see EnvTokenSource.Token's silent-fallthrough
87+
// rule. Stderr is discarded so an unauthenticated glab doesn't print noise
88+
// during a fallthrough that the caller has chosen to handle silently.
89+
func defaultGlabToken(ctx context.Context) (string, error) {
90+
cmd := exec.CommandContext(ctx, "glab", "auth", "token")
91+
cmd.Stderr = io.Discard
92+
out, err := cmd.Output()
93+
if err != nil {
94+
return "", err
95+
}
96+
return string(out), nil
97+
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package gitlab
2+
3+
import (
4+
"context"
5+
"errors"
6+
"testing"
7+
)
8+
9+
// TestEnvTokenSource_EnvVarWins pins that a configured env var, when present,
10+
// short-circuits both GITLAB_TOKEN and the glab fallback. The glab fake is
11+
// installed to prove it is NOT consulted on the happy path.
12+
func TestEnvTokenSource_EnvVarWins(t *testing.T) {
13+
t.Setenv("AO_GITLAB_TOKEN", "from-env")
14+
t.Setenv("GITLAB_TOKEN", "from-default-env")
15+
src := EnvTokenSource{
16+
EnvVars: []string{"AO_GITLAB_TOKEN"},
17+
GLAB: func(context.Context) (string, error) {
18+
t.Fatalf("glab fallback called even though env var was set")
19+
return "", nil
20+
},
21+
}
22+
got, err := src.Token(context.Background())
23+
if err != nil {
24+
t.Fatalf("Token: %v", err)
25+
}
26+
if got != "from-env" {
27+
t.Fatalf("token = %q, want from-env", got)
28+
}
29+
}
30+
31+
// TestEnvTokenSource_DefaultEnvWins pins that GITLAB_TOKEN is used when the
32+
// configured EnvVars list is empty or yields nothing, before falling through
33+
// to glab.
34+
func TestEnvTokenSource_DefaultEnvWins(t *testing.T) {
35+
t.Setenv("GITLAB_TOKEN", "from-default-env")
36+
src := EnvTokenSource{
37+
GLAB: func(context.Context) (string, error) {
38+
t.Fatalf("glab fallback called even though GITLAB_TOKEN was set")
39+
return "", nil
40+
},
41+
}
42+
got, err := src.Token(context.Background())
43+
if err != nil {
44+
t.Fatalf("Token: %v", err)
45+
}
46+
if got != "from-default-env" {
47+
t.Fatalf("token = %q, want from-default-env", got)
48+
}
49+
}
50+
51+
// TestEnvTokenSource_GlabFallback pins that when no env var yields a token,
52+
// the glab shell-out is used.
53+
func TestEnvTokenSource_GlabFallback(t *testing.T) {
54+
t.Setenv("GITLAB_TOKEN", "")
55+
var called bool
56+
src := EnvTokenSource{
57+
GLAB: func(context.Context) (string, error) {
58+
called = true
59+
return "glpat-from-glab", nil
60+
},
61+
}
62+
got, err := src.Token(context.Background())
63+
if err != nil {
64+
t.Fatalf("Token: %v", err)
65+
}
66+
if !called {
67+
t.Fatalf("glab fallback was not called")
68+
}
69+
if got != "glpat-from-glab" {
70+
t.Fatalf("token = %q, want glpat-from-glab", got)
71+
}
72+
}
73+
74+
// TestEnvTokenSource_GlabFallbackTrimsWhitespace pins that the glab stdout is
75+
// trimmed (the real CLI ends with a newline).
76+
func TestEnvTokenSource_GlabFallbackTrimsWhitespace(t *testing.T) {
77+
t.Setenv("GITLAB_TOKEN", "")
78+
src := EnvTokenSource{
79+
GLAB: func(context.Context) (string, error) {
80+
return " glpat-padded\n", nil
81+
},
82+
}
83+
got, err := src.Token(context.Background())
84+
if err != nil {
85+
t.Fatalf("Token: %v", err)
86+
}
87+
if got != "glpat-padded" {
88+
t.Fatalf("token = %q, want glpat-padded", got)
89+
}
90+
}
91+
92+
// TestEnvTokenSource_GlabFailureFallsThrough pins the silent-fallthrough
93+
// contract: if glab errors (not installed, not logged in, etc.) the source
94+
// returns ErrNoToken instead of propagating the exec error. This keeps the
95+
// caller's failure mode uniform — "no token configured" — regardless of
96+
// whether the user has glab installed.
97+
func TestEnvTokenSource_GlabFailureFallsThrough(t *testing.T) {
98+
t.Setenv("GITLAB_TOKEN", "")
99+
src := EnvTokenSource{
100+
GLAB: func(context.Context) (string, error) {
101+
return "", errors.New("exec: glab: executable file not found in $PATH")
102+
},
103+
}
104+
_, err := src.Token(context.Background())
105+
if !errors.Is(err, ErrNoToken) {
106+
t.Fatalf("err = %v, want ErrNoToken (glab error should fall through silently)", err)
107+
}
108+
}
109+
110+
// TestEnvTokenSource_GlabEmptyFallsThrough pins that an empty (whitespace-only)
111+
// glab output is treated as "no token" rather than as a valid token of zero
112+
// length — otherwise StaticTokenSource-style downstream checks would yield
113+
// "no token configured" inconsistently.
114+
func TestEnvTokenSource_GlabEmptyFallsThrough(t *testing.T) {
115+
t.Setenv("GITLAB_TOKEN", "")
116+
src := EnvTokenSource{
117+
GLAB: func(context.Context) (string, error) {
118+
return " \n", nil
119+
},
120+
}
121+
_, err := src.Token(context.Background())
122+
if !errors.Is(err, ErrNoToken) {
123+
t.Fatalf("err = %v, want ErrNoToken (empty glab output should fall through)", err)
124+
}
125+
}
126+
127+
// TestEnvTokenSource_NilGlabUsesRealExec is a smoke test: with the GLAB field
128+
// nil and no env vars, the source attempts a real exec. We don't depend on
129+
// glab being installed on the test machine — we just assert that the
130+
// fallthrough produces ErrNoToken (either because glab isn't installed OR
131+
// because it has no token to give). Either way, the public contract holds:
132+
// "no token configured" surfaces as ErrNoToken, never as a raw exec error.
133+
func TestEnvTokenSource_NilGlabUsesRealExec(t *testing.T) {
134+
t.Setenv("GITLAB_TOKEN", "")
135+
src := EnvTokenSource{} // GLAB nil → defaults to real exec.
136+
// Either glab is installed and returns a real token (rare on CI), or it
137+
// fails / is missing. In both failure modes we must see ErrNoToken; in
138+
// the rare success case we get a non-empty token. Both are valid.
139+
tok, err := src.Token(context.Background())
140+
if err != nil && !errors.Is(err, ErrNoToken) {
141+
t.Fatalf("err = %v, want ErrNoToken or a real token", err)
142+
}
143+
if err == nil && tok == "" {
144+
t.Fatalf("got empty token with nil error")
145+
}
146+
}
147+
148+
// TestEnvTokenSource_ContextThreadedToGlab pins that the ctx is passed through
149+
// to the GLAB hook. The github adapter's parallel work uses the same shape,
150+
// so any future caller wiring a deadline at startup gets honored.
151+
func TestEnvTokenSource_ContextThreadedToGlab(t *testing.T) {
152+
t.Setenv("GITLAB_TOKEN", "")
153+
type ctxKey struct{}
154+
parent := context.WithValue(context.Background(), ctxKey{}, "sentinel")
155+
src := EnvTokenSource{
156+
GLAB: func(ctx context.Context) (string, error) {
157+
if v, _ := ctx.Value(ctxKey{}).(string); v != "sentinel" {
158+
t.Fatalf("ctx not threaded through to GLAB hook: got %v", v)
159+
}
160+
return "tok", nil
161+
},
162+
}
163+
if _, err := src.Token(parent); err != nil {
164+
t.Fatalf("Token: %v", err)
165+
}
166+
}

0 commit comments

Comments
 (0)