Skip to content

Commit ddb4d59

Browse files
committed
fail fast in login / auth token when CI=true and AccessToken not set
1 parent 258b1ac commit ddb4d59

4 files changed

Lines changed: 41 additions & 0 deletions

File tree

cmd/src/auth_token.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ func init() {
5454
}
5555

5656
func resolveAuthToken(ctx context.Context, cfg *config) (string, error) {
57+
if err := cfg.requireCIAccessToken(); err != nil {
58+
return "", err
59+
}
60+
5761
if cfg.accessToken != "" {
5862
return cfg.accessToken, nil
5963
}

cmd/src/auth_token_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,28 @@ func TestResolveAuthToken(t *testing.T) {
3535
}
3636
})
3737

38+
t.Run("requires access token in CI", func(t *testing.T) {
39+
reset := stubAuthTokenDependencies(t)
40+
defer reset()
41+
42+
loadCalled := false
43+
loadOAuthToken = func(context.Context, *url.URL) (*oauth.Token, error) {
44+
loadCalled = true
45+
return nil, nil
46+
}
47+
48+
_, err := resolveAuthToken(context.Background(), &config{
49+
inCI: true,
50+
endpointURL: mustParseURL(t, "https://example.com"),
51+
})
52+
if err != errCIAccessTokenRequired {
53+
t.Fatalf("err = %v, want %v", err, errCIAccessTokenRequired)
54+
}
55+
if loadCalled {
56+
t.Fatal("expected OAuth token loader not to be called")
57+
}
58+
})
59+
3860
t.Run("uses stored oauth token", func(t *testing.T) {
3961
reset := stubAuthTokenDependencies(t)
4062
defer reset()

cmd/src/login.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ const (
100100
)
101101

102102
func loginCmd(ctx context.Context, p loginParams) error {
103+
if err := p.cfg.requireCIAccessToken(); err != nil {
104+
return err
105+
}
106+
103107
if p.cfg.configFilePath != "" {
104108
fmt.Fprintln(p.out)
105109
fmt.Fprintf(p.out, "⚠️ Warning: Configuring src with a JSON file is deprecated. Please migrate to using the env vars SRC_ENDPOINT, SRC_ACCESS_TOKEN, and SRC_PROXY instead, and then remove %s. See https://github.com/sourcegraph/src-cli#readme for more information.\n", p.cfg.configFilePath)

cmd/src/login_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ func TestLogin(t *testing.T) {
6161
}
6262
})
6363

64+
t.Run("CI requires access token", func(t *testing.T) {
65+
u := &url.URL{Scheme: "https", Host: "example.com"}
66+
out, err := check(t, &config{endpointURL: u, inCI: true}, u)
67+
if err != errCIAccessTokenRequired {
68+
t.Fatalf("err = %v, want %v", err, errCIAccessTokenRequired)
69+
}
70+
if out != "" {
71+
t.Fatalf("output = %q, want empty output", out)
72+
}
73+
})
74+
6475
t.Run("warning when using config file", func(t *testing.T) {
6576
endpoint := &url.URL{Scheme: "https", Host: "example.com"}
6677
out, err := check(t, &config{endpointURL: endpoint, configFilePath: "f"}, endpoint)

0 commit comments

Comments
 (0)