-
Notifications
You must be signed in to change notification settings - Fork 1.1k
cubeops: propagate database errors from the settings and user getters #1382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dwin-gharibi
wants to merge
9
commits into
TencentCloud:master
Choose a base branch
from
dwin-gharibi:cubeops-store-swallows-db-errors
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
02b5972
fix(CubeOps/internal/service/auth.go): propagate database errors from…
dwin-gharibi 24ecdf2
test(CubeOps/internal/service/auth_db_error_test.go): adding up some …
dwin-gharibi 75d003d
fix(CubeOps/internal/store/setting.go): use Row().Scan so sql.ErrNoRo…
dwin-gharibi 4d6bbd2
test(CubeOps/internal/store/setting_contract_test.go): pin the absent…
dwin-gharibi 78cb4b2
fix(CubeOps/internal/auth/handler.go): stop echoing database errors i…
dwin-gharibi ce0338d
test(CubeOps/internal/auth/handler_error_leak_test.go): assert 500 bo…
dwin-gharibi 5a6b6cc
fix(CubeOps/internal/auth/handler.go): stop the Refresh and agenthub …
dwin-gharibi 27b92a9
test(CubeOps/internal/auth/handler_error_leak_test.go): assert the Re…
dwin-gharibi 8023ff4
test(CubeOps/e2e/db_outage_test.go): kill the database under the runn…
dwin-gharibi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| //go:build e2e | ||
|
|
||
| // Copyright (c) 2026 Tencent Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package e2e | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| // dbDetailMarkers are fragments of a driver error that must never reach a | ||
| // client. The host/port come from the container the harness started. | ||
| func dbDetailMarkers(e *env) []string { | ||
| return []string{ | ||
| "dial tcp", "connection refused", "invalid connection", | ||
| "sql:", "mysql:", e.mysqlURL.port, | ||
| } | ||
| } | ||
|
|
||
| func assertNoDBDetail(t *testing.T, e *env, where, body string) { | ||
| t.Helper() | ||
| for _, marker := range dbDetailMarkers(e) { | ||
| if strings.Contains(strings.ToLower(body), strings.ToLower(marker)) { | ||
| t.Errorf("%s response leaks %q to the caller: %s", where, marker, body) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // TestDatabaseOutageEndToEnd is the wire-level guard for #1381. It logs in | ||
| // against a healthy database, then kills the database underneath the running | ||
| // server and asserts that: | ||
| // | ||
| // - login reports 500 (infrastructure), not 401 (invalid credentials) | ||
| // - none of the three public/authenticated 500 paths echo driver detail | ||
| func TestDatabaseOutageEndToEnd(t *testing.T) { | ||
| e := newEnv(t) | ||
| defer e.teardown() | ||
|
|
||
| inst := e.start(t) | ||
| defer inst.stop() | ||
|
|
||
| // Healthy baseline: a real login and a real refresh token to use later. | ||
| token := login(t, inst.baseURL, "admin", "admin") | ||
| code, body := do(t, http.MethodGet, inst.baseURL+"/api/v1/auth/session", token, "") | ||
| if code != http.StatusOK { | ||
| t.Fatalf("baseline session check failed: %d %s", code, body) | ||
| } | ||
|
|
||
| code, refreshBody := do(t, http.MethodPost, inst.baseURL+"/api/v1/auth/login", "", | ||
| `{"username":"admin","password":"admin"}`) | ||
| if code != http.StatusOK { | ||
| t.Fatalf("baseline login failed: %d %s", code, refreshBody) | ||
| } | ||
| refreshToken := extractRefreshToken(t, refreshBody) | ||
|
|
||
| // Pull the database out from under the running server. | ||
| e.teardown() | ||
|
|
||
| t.Run("login reports an outage rather than bad credentials", func(t *testing.T) { | ||
| code, body := do(t, http.MethodPost, inst.baseURL+"/api/v1/auth/login", "", | ||
| `{"username":"admin","password":"admin"}`) | ||
| if code == http.StatusUnauthorized { | ||
| t.Fatalf("a database outage was reported as invalid credentials: %d %s", code, body) | ||
| } | ||
| if code != http.StatusInternalServerError { | ||
| t.Fatalf("login during an outage returned %d, want 500: %s", code, body) | ||
| } | ||
| assertNoDBDetail(t, e, "login", body) | ||
| }) | ||
|
|
||
| t.Run("change-password does not leak driver detail", func(t *testing.T) { | ||
| code, body := do(t, http.MethodPost, inst.baseURL+"/api/v1/auth/change-password", token, | ||
| `{"old_password":"admin","new_password":"another-password"}`) | ||
| if code != http.StatusInternalServerError { | ||
| t.Logf("change-password returned %d during the outage: %s", code, body) | ||
| } | ||
| assertNoDBDetail(t, e, "change-password", body) | ||
| }) | ||
|
|
||
| t.Run("refresh does not leak driver detail", func(t *testing.T) { | ||
| code, body := do(t, http.MethodPost, inst.baseURL+"/api/v1/auth/refresh", "", | ||
| `{"refreshToken":"`+refreshToken+`"}`) | ||
| if code != http.StatusInternalServerError { | ||
| t.Logf("refresh returned %d during the outage: %s", code, body) | ||
| } | ||
| assertNoDBDetail(t, e, "refresh", body) | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // Copyright (c) 2026 Tencent Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Package e2e holds end-to-end tests that drive the real cubeops binary against | ||
| // a real database over HTTP. | ||
| // | ||
| // Every test file in this package carries the e2e build tag, so the default | ||
| // unit gate (go test ./...) compiles this file and reports "no test files" | ||
| // instead of failing on a package with no buildable sources. Run the suite with: | ||
| // | ||
| // go test -tags e2e ./e2e/... | ||
| package e2e |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two subtests only
t.Logfwhen the status isn't 500, so they pass even if change-password/refresh return 401 or 200 during the outage (as long as the body carries no driver detail). Both paths deterministically hit the DB while the server is up (GetUserPasswordfor change-password,IsRefreshTokenRevokedfor refresh) and the auth middleware is signature-only, so both should reliably return 500. For consistency with the login subtest above, assertingcode == http.StatusInternalServerErrorhere would make the guarantee uniform.