Skip to content

Commit 2fe30bd

Browse files
committed
Fix requested changes
1 parent 236e94a commit 2fe30bd

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

coderd/workspaceagents.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,14 @@ func (api *API) workspaceAgentsGitAuth(rw http.ResponseWriter, r *http.Request)
11601160
continue
11611161
}
11621162
if gitAuthConfig.ValidateURL != "" {
1163-
valid, _ := validateGitToken(ctx, gitAuthConfig.ValidateURL, gitAuthLink.OAuthAccessToken)
1163+
valid, err := validateGitToken(ctx, gitAuthConfig.ValidateURL, gitAuthLink.OAuthAccessToken)
1164+
if err != nil {
1165+
api.Logger.Warn(ctx, "failed to validate git auth token",
1166+
slog.F("workspace_owner_id", workspace.OwnerID.String()),
1167+
slog.F("validate_url", gitAuthConfig.ValidateURL),
1168+
slog.Error(err),
1169+
)
1170+
}
11641171
if !valid {
11651172
continue
11661173
}
@@ -1221,9 +1228,9 @@ func (api *API) workspaceAgentsGitAuth(rw http.ResponseWriter, r *http.Request)
12211228
}
12221229

12231230
if gitAuthConfig.ValidateURL != "" {
1224-
valid, err := validateGitToken(r.Context(), gitAuthConfig.ValidateURL, token.AccessToken)
1231+
valid, err := validateGitToken(ctx, gitAuthConfig.ValidateURL, token.AccessToken)
12251232
if err != nil {
1226-
httpapi.Write(r.Context(), rw, http.StatusInternalServerError, codersdk.Response{
1233+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
12271234
Message: "Failed to validate Git authentication token.",
12281235
Detail: err.Error(),
12291236
})
@@ -1278,7 +1285,7 @@ func validateGitToken(ctx context.Context, validateURL, token string) (bool, err
12781285
}
12791286
if res.StatusCode != http.StatusOK {
12801287
data, _ := io.ReadAll(res.Body)
1281-
return false, xerrors.Errorf("body: %s", data)
1288+
return false, xerrors.Errorf("git token validation failed: status %d: body: %s", res.StatusCode, data)
12821289
}
12831290
return true, nil
12841291
}

coderd/workspaceagents_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,9 @@ func TestWorkspaceAgentsGitAuth(t *testing.T) {
937937
})
938938
t.Run("ValidateURL", func(t *testing.T) {
939939
t.Parallel()
940+
ctx, cancelFunc := testutil.Context(t)
941+
defer cancelFunc()
942+
940943
srv := httptest.NewServer(nil)
941944
defer srv.Close()
942945
client := coderdtest.New(t, &coderdtest.Options{
@@ -987,7 +990,7 @@ func TestWorkspaceAgentsGitAuth(t *testing.T) {
987990
srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
988991
w.WriteHeader(http.StatusUnauthorized)
989992
})
990-
res, err := agentClient.WorkspaceAgentGitAuth(context.Background(), "github.com/asd/asd", false)
993+
res, err := agentClient.WorkspaceAgentGitAuth(ctx, "github.com/asd/asd", false)
991994
require.NoError(t, err)
992995
require.NotEmpty(t, res.URL)
993996

@@ -997,7 +1000,7 @@ func TestWorkspaceAgentsGitAuth(t *testing.T) {
9971000
w.WriteHeader(http.StatusForbidden)
9981001
w.Write([]byte("Something went wrong!"))
9991002
})
1000-
_, err = agentClient.WorkspaceAgentGitAuth(context.Background(), "github.com/asd/asd", false)
1003+
_, err = agentClient.WorkspaceAgentGitAuth(ctx, "github.com/asd/asd", false)
10011004
var apiError *codersdk.Error
10021005
require.ErrorAs(t, err, &apiError)
10031006
require.Equal(t, http.StatusInternalServerError, apiError.StatusCode())

0 commit comments

Comments
 (0)