fix(sync): distinguish SSH dial timeout from auth failure (#6677)
* fix(sync): distinguish SSH dial timeout from auth failure * fix(sync): sanitize discarded pull errors
This commit is contained in:
parent
6e36c3f19c
commit
61ec1d1c38
2 changed files with 58 additions and 5 deletions
|
|
@ -9,6 +9,7 @@ import (
|
|||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
|
@ -294,7 +295,7 @@ func runSyncLoop(ctx context.Context, ops syncOps, maxAttempts int) (*syncOutcom
|
|||
// shared sql-server — and returning nil below would silently
|
||||
// drop it. Surface it instead of discarding it.
|
||||
if pullErr != nil && len(merged) == 0 {
|
||||
out.DiscardedPullError = pullErr.Error()
|
||||
out.DiscardedPullError = syncFailureText(pullErr)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
|
@ -870,7 +871,7 @@ func runSyncCommand(cmd *cobra.Command, _ []string) error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
exitErr := HandleErrorRespectJSON("sync failed: %v", err)
|
||||
exitErr := HandleErrorRespectJSON("sync failed: %s", syncFailureText(err))
|
||||
if !jsonOutput {
|
||||
printSyncErrorGuidance(remote, err)
|
||||
}
|
||||
|
|
@ -901,6 +902,24 @@ func runSyncCommand(cmd *cobra.Command, _ []string) error {
|
|||
}
|
||||
}
|
||||
|
||||
// sshDialTimeoutLine is deliberately narrower than a generic timeout: Dolt's
|
||||
// git auth wrapper appends credential hints to all git failures, including a
|
||||
// transport-level SSH dial timeout. Only that positively identified stderr
|
||||
// line warrants replacing the inherited, misleading auth advice. Match the
|
||||
// stderr line rather than importing Dolt's error type across the storage
|
||||
// boundary; sync only has the wrapped error by the time it renders the result.
|
||||
var sshDialTimeoutLine = regexp.MustCompile(`(?i)ssh: connect to host [^\r\n]+ port [0-9]+: (?:Operation timed out|Connection timed out)`)
|
||||
|
||||
func syncFailureText(err error) string {
|
||||
if err == nil {
|
||||
return ""
|
||||
}
|
||||
if line := sshDialTimeoutLine.FindString(err.Error()); line != "" {
|
||||
return "SSH remote could not be reached (dial timed out): " + scrubURLCredentials(strings.TrimSpace(line))
|
||||
}
|
||||
return err.Error()
|
||||
}
|
||||
|
||||
// mergeBlockersOp builds the loop's positive constraint-violation hook, or nil
|
||||
// when this store cannot answer the question — an unimplemented interface
|
||||
// must leave the detector silent, never guessing. Bound to the same st the
|
||||
|
|
|
|||
|
|
@ -18,6 +18,33 @@ import (
|
|||
"github.com/steveyegge/beads/internal/storage/versioncontrolops"
|
||||
)
|
||||
|
||||
func TestSyncFailureTextDistinguishesSSHDialTimeoutFromAuth(t *testing.T) {
|
||||
const timeoutLine = "ssh: connect to host blackhole.example port 22: Operation timed out"
|
||||
gitText := timeoutLine + "\nfatal: Could not read from remote repository.\nPlease make sure you have the correct access rights and the repository exists.\nhint: run `ssh-add <key>` to pre-load your key for SSH remotes"
|
||||
timeout := fmt.Errorf("pull: %w", errors.New(gitText))
|
||||
if !strings.Contains(timeout.Error(), "ssh-add") {
|
||||
t.Fatal("fixture did not reproduce Dolt's misleading auth hint")
|
||||
}
|
||||
got := syncFailureText(timeout)
|
||||
if !strings.Contains(got, timeoutLine) || !strings.Contains(got, "could not be reached") {
|
||||
t.Fatalf("dial timeout not explained: %q", got)
|
||||
}
|
||||
for _, misleading := range []string{"access rights", "ssh-add", "fatal: Could not read"} {
|
||||
if strings.Contains(got, misleading) {
|
||||
t.Errorf("dial timeout retained auth hint %q: %q", misleading, got)
|
||||
}
|
||||
}
|
||||
|
||||
auth := fmt.Errorf("pull: %w", errors.New("git@host: Permission denied (publickey).\nhint: run `ssh-add <key>` to pre-load your key for SSH remotes"))
|
||||
if got := syncFailureText(auth); got != auth.Error() {
|
||||
t.Errorf("real auth failure changed: got %q, want %q", got, auth.Error())
|
||||
}
|
||||
plainTimeout := errors.New("database query: Operation timed out")
|
||||
if got := syncFailureText(plainTimeout); got != plainTimeout.Error() {
|
||||
t.Errorf("non-SSH timeout changed: got %q, want %q", got, plainTimeout.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// syncOpsRecorder builds a syncOps whose steps are scripted per attempt and
|
||||
// which records how many times each step ran. Scripts are indexed by call
|
||||
// number; a call past the end of a script reuses the script's last entry, so a
|
||||
|
|
@ -302,8 +329,10 @@ func TestRunSyncLoopConflictWinsOverPullError(t *testing.T) {
|
|||
// `merged` empty — the conflict branch still fires (from the live check), and
|
||||
// the unrelated error must not vanish silently.
|
||||
func TestRunSyncLoopDiscardedPullErrorOnLiveConflict(t *testing.T) {
|
||||
const timeoutLine = "ssh: connect to host blackhole.example port 22: Operation timed out"
|
||||
pullErr := errors.New(timeoutLine + "\nfatal: Could not read from remote repository.\nPlease make sure you have the correct access rights and the repository exists.\nhint: run `ssh-add <key>` to pre-load your key for SSH remotes")
|
||||
r := &syncOpsRecorder{
|
||||
pullErrs: []error{errors.New("dial tcp: connection refused")},
|
||||
pullErrs: []error{pullErr},
|
||||
conflictsSeq: [][]string{nil, {"issues"}},
|
||||
}
|
||||
out, err := runSyncLoop(context.Background(), r.ops(), defaultSyncAttempts)
|
||||
|
|
@ -313,8 +342,13 @@ func TestRunSyncLoopDiscardedPullErrorOnLiveConflict(t *testing.T) {
|
|||
if out.Status != syncStatusConflict {
|
||||
t.Fatalf("status = %q, want %q", out.Status, syncStatusConflict)
|
||||
}
|
||||
if out.DiscardedPullError != "dial tcp: connection refused" {
|
||||
t.Errorf("DiscardedPullError = %q, want the unrelated pull error preserved", out.DiscardedPullError)
|
||||
if !strings.Contains(out.DiscardedPullError, "could not be reached") || !strings.Contains(out.DiscardedPullError, timeoutLine) {
|
||||
t.Errorf("DiscardedPullError does not explain the dial timeout: %q", out.DiscardedPullError)
|
||||
}
|
||||
for _, misleading := range []string{"access rights", "ssh-add", "fatal: Could not read"} {
|
||||
if strings.Contains(out.DiscardedPullError, misleading) {
|
||||
t.Errorf("DiscardedPullError retained auth hint %q: %q", misleading, out.DiscardedPullError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue