fix(embeddeddolt): use stored peer credentials for federation sync (#5085)
* fix(embeddeddolt): use stored peer credentials for federation sync bd federation add-peer promises stored credentials are used automatically when syncing, but the embedded Fetch/PushTo/PullFrom paths read only the DOLT_REMOTE_USER environment variable, so first sync against any authenticated Dolt server presented the wrong identity. This is the peer-path remainder of the class fixed for default remotes in #2543. withPeerAuth mirrors server mode's withPeerCredentials: peer-store lookup first, environment fallback for unknown remotes and credential-free peers (federation sync iterates every dolt remote, so non-peer remotes must keep their behavior). Stored credentials override the process-wide DOLT_REMOTE_USER/DOLT_REMOTE_PASSWORD pair as a unit under a federationEnvMutex, mirroring package dolt: the in-process Dolt engine reads them from the process environment, so the override is serialized and both variables are restored (or re-unset) afterward, including on callback failure. A stored empty field unsets its variable rather than inheriting an ambient value. Fixes #5080 Agent-Signature: claude-code-claude-fable-5-unknown-reasoning on behalf of Michael Pursifull * docs(embeddeddolt): clarify federationEnvMutex is separate from package dolt's lock Review nit on #5085: the 'mirroring' phrasing read as if the two packages shared one mutex. State explicitly that they are separate locks and why per-package serialization suffices. * fix(embeddeddolt): bind stored credentials to the remote URL; serialize env fallbacks Two fixes to the shared withPeerAuth seam from the consolidation review: - Stored peer credentials are now bound to the peer's canonical remote URL, not only to a matching remote name. AddRemoteIfNotExists keeps an existing same-name remote regardless of its URL, so a redirected remote could receive the stored password. withPeerAuth now verifies the live dolt_remotes URL against the peer row before any credential is installed, failing closed on divergence and on a missing remote. - The plain-remote and empty-credential fallback callbacks now run under federationEnvMutex too. They previously called remoteAuthUser() and the operation itself outside the lock, so a fallback operation could observe a concurrent stored-peer operation's temporarily installed DOLT_REMOTE_USER/DOLT_REMOTE_PASSWORD pair. Adds three regressions: diverged-URL fail-closed, missing-remote fail-closed, and env-fallback-does-not-observe-peer-credentials. Refs: gastownhall/beads#5085 (review) --------- Co-authored-by: Steve Yegge <steve.yegge@gmail.com>
This commit is contained in:
parent
041f1d64d4
commit
ce7bf9d8e4
3 changed files with 505 additions and 17 deletions
|
|
@ -8,11 +8,13 @@ import (
|
|||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
|
|
@ -145,6 +147,98 @@ func (s *EmbeddedDoltStore) GetFederationPeer(ctx context.Context, name string)
|
|||
return &row.Peer, nil
|
||||
}
|
||||
|
||||
// federationEnvMutex serializes mutation of the process-wide
|
||||
// DOLT_REMOTE_USER/DOLT_REMOTE_PASSWORD pair: the in-process Dolt engine
|
||||
// reads them from the process environment, so concurrent peer operations
|
||||
// would otherwise observe each other's credentials. This is a separate
|
||||
// lock from package dolt's federationEnvMutex, not the same lock; the two
|
||||
// stores never run remote operations concurrently, so serializing within
|
||||
// each package suffices.
|
||||
var federationEnvMutex sync.Mutex
|
||||
|
||||
// withPeerAuth runs fn with the remote-auth username for peer. Credentials
|
||||
// stored by add-peer win and override the environment pair as a unit, so an
|
||||
// ambient DOLT_REMOTE_PASSWORD never mixes with a stored username (or vice
|
||||
// versa); remotes without a stored peer keep the environment fallback.
|
||||
//
|
||||
// Every callback path holds federationEnvMutex, including the environment
|
||||
// fallbacks: the in-process Dolt engine reads the pair from the process
|
||||
// environment, so an unserialized plain-remote operation could observe
|
||||
// another peer operation's temporarily installed credentials.
|
||||
//
|
||||
// Stored credentials are bound to the peer's canonical remote URL, not only
|
||||
// to its name (see verifyPeerRemoteURL).
|
||||
func (s *EmbeddedDoltStore) withPeerAuth(ctx context.Context, peer string, fn func(user string) error) error {
|
||||
p, err := s.GetFederationPeer(ctx, peer)
|
||||
if err != nil && !errors.Is(err, storage.ErrNotFound) {
|
||||
return fmt.Errorf("resolve peer credentials: %w", err)
|
||||
}
|
||||
if err != nil || (p.Username == "" && p.Password == "") {
|
||||
federationEnvMutex.Lock()
|
||||
defer federationEnvMutex.Unlock()
|
||||
return fn(remoteAuthUser())
|
||||
}
|
||||
|
||||
if err := s.verifyPeerRemoteURL(ctx, peer, p.RemoteURL); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
federationEnvMutex.Lock()
|
||||
defer federationEnvMutex.Unlock()
|
||||
|
||||
restoreUser := overrideEnv("DOLT_REMOTE_USER", p.Username)
|
||||
restorePassword := overrideEnv("DOLT_REMOTE_PASSWORD", p.Password)
|
||||
defer func() {
|
||||
restorePassword()
|
||||
restoreUser()
|
||||
}()
|
||||
|
||||
return fn(p.Username)
|
||||
}
|
||||
|
||||
// verifyPeerRemoteURL fails closed when the live remote named peer does not
|
||||
// carry the URL stored on the federation peer row. AddRemoteIfNotExists
|
||||
// preserves an existing same-name remote regardless of its URL, so the name
|
||||
// alone does not prove the destination; installing the stored password for a
|
||||
// diverged URL would disclose it to an unrelated host. A missing remote fails
|
||||
// the same way: there is no verified destination to authenticate against.
|
||||
// Runs before any credential is installed, outside federationEnvMutex.
|
||||
func (s *EmbeddedDoltStore) verifyPeerRemoteURL(ctx context.Context, peer, storedURL string) error {
|
||||
var liveURL string
|
||||
err := s.withConn(ctx, false, func(tx *sql.Tx) error {
|
||||
return tx.QueryRowContext(ctx, "SELECT url FROM dolt_remotes WHERE name = ?", peer).Scan(&liveURL)
|
||||
})
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return fmt.Errorf("federation peer %s has stored credentials but no remote named %s exists; re-run 'bd federation add-peer %s <url> --user <user>' to restore it", peer, peer, peer)
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("resolve remote URL for peer %s: %w", peer, err)
|
||||
}
|
||||
if liveURL != storedURL {
|
||||
return fmt.Errorf("remote %s points at %q but federation peer %s stored its credentials for %q; refusing to send stored credentials to a diverged URL. Remove and re-add the peer to rebind it", peer, liveURL, peer, storedURL)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// overrideEnv sets key to value (unsetting it when value is empty, so an
|
||||
// ambient value cannot leak into an operation that stored an empty field)
|
||||
// and returns a function restoring the prior state.
|
||||
func overrideEnv(key, value string) func() {
|
||||
prev, had := os.LookupEnv(key)
|
||||
if value == "" {
|
||||
_ = os.Unsetenv(key)
|
||||
} else {
|
||||
_ = os.Setenv(key, value)
|
||||
}
|
||||
return func() {
|
||||
if had {
|
||||
_ = os.Setenv(key, prev)
|
||||
} else {
|
||||
_ = os.Unsetenv(key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *EmbeddedDoltStore) ListFederationPeers(ctx context.Context) ([]*storage.FederationPeer, error) {
|
||||
var rows []*issueops.FederationPeerRow
|
||||
err := s.withConn(ctx, false, func(tx *sql.Tx) error {
|
||||
|
|
|
|||
388
internal/storage/embeddeddolt/federation_credentials_test.go
Normal file
388
internal/storage/embeddeddolt/federation_credentials_test.go
Normal file
|
|
@ -0,0 +1,388 @@
|
|||
//go:build cgo
|
||||
|
||||
package embeddeddolt
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
)
|
||||
|
||||
func newPeerAuthTestStore(t *testing.T) *EmbeddedDoltStore {
|
||||
t.Helper()
|
||||
ctx := t.Context()
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
store, err := Open(ctx, beadsDir, "fedauth", "main")
|
||||
if err != nil {
|
||||
t.Fatalf("Open: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { store.Close() })
|
||||
return store
|
||||
}
|
||||
|
||||
// Regression test for GH#5080: credentials stored by add-peer must be the
|
||||
// ones presented when syncing with that peer, overriding the environment
|
||||
// pair as a unit; remotes without a stored peer keep the environment
|
||||
// fallback.
|
||||
func TestWithPeerAuth(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
peer *storage.FederationPeer
|
||||
remote string
|
||||
wantUser string
|
||||
wantPwd string
|
||||
wantPwdSet bool
|
||||
wantUsrEnv string
|
||||
wantUsrSet bool
|
||||
}{
|
||||
{
|
||||
name: "stored peer credentials win",
|
||||
peer: &storage.FederationPeer{
|
||||
Name: "team",
|
||||
RemoteURL: "https://peer.example/peerdb",
|
||||
Username: "peeruser",
|
||||
Password: "peerpass",
|
||||
},
|
||||
remote: "team",
|
||||
wantUser: "peeruser",
|
||||
wantPwd: "peerpass", wantPwdSet: true,
|
||||
wantUsrEnv: "peeruser", wantUsrSet: true,
|
||||
},
|
||||
{
|
||||
name: "stored empty password does not inherit ambient password",
|
||||
peer: &storage.FederationPeer{
|
||||
Name: "open-pwd",
|
||||
RemoteURL: "https://peer.example/peerdb",
|
||||
Username: "peeruser",
|
||||
},
|
||||
remote: "open-pwd",
|
||||
wantUser: "peeruser",
|
||||
wantPwd: "", wantPwdSet: false,
|
||||
wantUsrEnv: "peeruser", wantUsrSet: true,
|
||||
},
|
||||
{
|
||||
name: "stored password with empty username does not inherit ambient user",
|
||||
peer: &storage.FederationPeer{
|
||||
Name: "open-usr",
|
||||
RemoteURL: "https://peer.example/peerdb",
|
||||
Password: "peerpass",
|
||||
},
|
||||
remote: "open-usr",
|
||||
wantUser: "",
|
||||
wantPwd: "peerpass", wantPwdSet: true,
|
||||
wantUsrEnv: "", wantUsrSet: false,
|
||||
},
|
||||
{
|
||||
name: "unknown remote falls back to env",
|
||||
remote: "not-a-peer",
|
||||
wantUser: "envuser",
|
||||
wantPwd: "envpass", wantPwdSet: true,
|
||||
wantUsrEnv: "envuser", wantUsrSet: true,
|
||||
},
|
||||
{
|
||||
name: "credential-free peer falls back to env",
|
||||
peer: &storage.FederationPeer{
|
||||
Name: "open-peer",
|
||||
RemoteURL: "https://peer.example/peerdb",
|
||||
},
|
||||
remote: "open-peer",
|
||||
wantUser: "envuser",
|
||||
wantPwd: "envpass", wantPwdSet: true,
|
||||
wantUsrEnv: "envuser", wantUsrSet: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
store := newPeerAuthTestStore(t)
|
||||
|
||||
t.Setenv("DOLT_REMOTE_USER", "envuser")
|
||||
t.Setenv("DOLT_REMOTE_PASSWORD", "envpass")
|
||||
|
||||
if tc.peer != nil {
|
||||
if err := store.AddFederationPeer(ctx, tc.peer); err != nil {
|
||||
t.Fatalf("AddFederationPeer: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
var gotUser, gotPwd, gotUsrEnv string
|
||||
var gotPwdSet, gotUsrSet bool
|
||||
err := store.withPeerAuth(ctx, tc.remote, func(user string) error {
|
||||
gotUser = user
|
||||
gotPwd, gotPwdSet = os.LookupEnv("DOLT_REMOTE_PASSWORD")
|
||||
gotUsrEnv, gotUsrSet = os.LookupEnv("DOLT_REMOTE_USER")
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("withPeerAuth: %v", err)
|
||||
}
|
||||
if gotUser != tc.wantUser {
|
||||
t.Errorf("user = %q, want %q", gotUser, tc.wantUser)
|
||||
}
|
||||
if gotPwdSet != tc.wantPwdSet || gotPwd != tc.wantPwd {
|
||||
t.Errorf("DOLT_REMOTE_PASSWORD during fn = %q (set=%v), want %q (set=%v)", gotPwd, gotPwdSet, tc.wantPwd, tc.wantPwdSet)
|
||||
}
|
||||
if gotUsrSet != tc.wantUsrSet || gotUsrEnv != tc.wantUsrEnv {
|
||||
t.Errorf("DOLT_REMOTE_USER during fn = %q (set=%v), want %q (set=%v)", gotUsrEnv, gotUsrSet, tc.wantUsrEnv, tc.wantUsrSet)
|
||||
}
|
||||
if got := os.Getenv("DOLT_REMOTE_PASSWORD"); got != "envpass" {
|
||||
t.Errorf("DOLT_REMOTE_PASSWORD after fn = %q, want restored %q", got, "envpass")
|
||||
}
|
||||
if got := os.Getenv("DOLT_REMOTE_USER"); got != "envuser" {
|
||||
t.Errorf("DOLT_REMOTE_USER after fn = %q, want restored %q", got, "envuser")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithPeerAuth_RestoresEnvWhenCallbackFails(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
store := newPeerAuthTestStore(t)
|
||||
|
||||
t.Setenv("DOLT_REMOTE_PASSWORD", "envpass")
|
||||
if err := store.AddFederationPeer(ctx, &storage.FederationPeer{
|
||||
Name: "team", RemoteURL: "https://peer.example/peerdb",
|
||||
Username: "peeruser", Password: "peerpass",
|
||||
}); err != nil {
|
||||
t.Fatalf("AddFederationPeer: %v", err)
|
||||
}
|
||||
|
||||
sentinel := errors.New("boom")
|
||||
err := store.withPeerAuth(ctx, "team", func(string) error { return sentinel })
|
||||
if !errors.Is(err, sentinel) {
|
||||
t.Fatalf("withPeerAuth error = %v, want sentinel", err)
|
||||
}
|
||||
if got := os.Getenv("DOLT_REMOTE_PASSWORD"); got != "envpass" {
|
||||
t.Errorf("DOLT_REMOTE_PASSWORD after failed fn = %q, want restored %q", got, "envpass")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithPeerAuth_RestoresAbsentEnv(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
store := newPeerAuthTestStore(t)
|
||||
|
||||
t.Setenv("DOLT_REMOTE_PASSWORD", "scratch")
|
||||
_ = os.Unsetenv("DOLT_REMOTE_PASSWORD")
|
||||
t.Setenv("DOLT_REMOTE_USER", "scratch")
|
||||
_ = os.Unsetenv("DOLT_REMOTE_USER")
|
||||
|
||||
if err := store.AddFederationPeer(ctx, &storage.FederationPeer{
|
||||
Name: "team", RemoteURL: "https://peer.example/peerdb",
|
||||
Username: "peeruser", Password: "peerpass",
|
||||
}); err != nil {
|
||||
t.Fatalf("AddFederationPeer: %v", err)
|
||||
}
|
||||
|
||||
if err := store.withPeerAuth(ctx, "team", func(string) error { return nil }); err != nil {
|
||||
t.Fatalf("withPeerAuth: %v", err)
|
||||
}
|
||||
if v, set := os.LookupEnv("DOLT_REMOTE_PASSWORD"); set {
|
||||
t.Errorf("DOLT_REMOTE_PASSWORD after fn = %q, want unset", v)
|
||||
}
|
||||
if v, set := os.LookupEnv("DOLT_REMOTE_USER"); set {
|
||||
t.Errorf("DOLT_REMOTE_USER after fn = %q, want unset", v)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithPeerAuth_ConcurrentPeersDoNotMixCredentials(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
store := newPeerAuthTestStore(t)
|
||||
|
||||
peers := map[string]string{"alpha": "alphapass", "beta": "betapass"}
|
||||
for name, pwd := range peers {
|
||||
if err := store.AddFederationPeer(ctx, &storage.FederationPeer{
|
||||
Name: name, RemoteURL: "https://peer.example/" + name,
|
||||
Username: name + "-user", Password: pwd,
|
||||
}); err != nil {
|
||||
t.Fatalf("AddFederationPeer(%s): %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
var wg sync.WaitGroup
|
||||
errs := make(chan error, 40)
|
||||
for range 10 {
|
||||
for name, pwd := range peers {
|
||||
wg.Add(1)
|
||||
go func(name, pwd string) {
|
||||
defer wg.Done()
|
||||
errs <- store.withPeerAuth(ctx, name, func(user string) error {
|
||||
if got := os.Getenv("DOLT_REMOTE_PASSWORD"); got != pwd {
|
||||
t.Errorf("peer %s observed DOLT_REMOTE_PASSWORD %q, want %q", name, got, pwd)
|
||||
}
|
||||
if want := name + "-user"; user != want {
|
||||
t.Errorf("peer %s got user %q, want %q", name, user, want)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}(name, pwd)
|
||||
}
|
||||
}
|
||||
wg.Wait()
|
||||
close(errs)
|
||||
for err := range errs {
|
||||
if err != nil {
|
||||
t.Errorf("withPeerAuth: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// rebindRemote repoints the Dolt remote name at url without touching the
|
||||
// federation peer row, simulating a same-name remote that no longer matches
|
||||
// the URL the credentials were stored for (GH#5085 review).
|
||||
func rebindRemote(t *testing.T, store *EmbeddedDoltStore, name, url string) {
|
||||
t.Helper()
|
||||
ctx := t.Context()
|
||||
if err := store.withConn(ctx, true, func(tx *sql.Tx) error {
|
||||
if _, err := tx.ExecContext(ctx, "CALL DOLT_REMOTE('remove', ?)", name); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := tx.ExecContext(ctx, "CALL DOLT_REMOTE('add', ?, ?)", name, url)
|
||||
return err
|
||||
}); err != nil {
|
||||
t.Fatalf("rebind remote %s: %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Stored credentials are bound to the peer's canonical remote URL, not only
|
||||
// to its name: a same-name remote redirected to another URL must fail closed
|
||||
// before any credential is installed (GH#5085 review).
|
||||
func TestWithPeerAuth_DivergedRemoteURLFailsClosed(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
store := newPeerAuthTestStore(t)
|
||||
|
||||
t.Setenv("DOLT_REMOTE_USER", "envuser")
|
||||
t.Setenv("DOLT_REMOTE_PASSWORD", "envpass")
|
||||
|
||||
if err := store.AddFederationPeer(ctx, &storage.FederationPeer{
|
||||
Name: "team", RemoteURL: "https://peer.example/peerdb",
|
||||
Username: "peeruser", Password: "peerpass",
|
||||
}); err != nil {
|
||||
t.Fatalf("AddFederationPeer: %v", err)
|
||||
}
|
||||
rebindRemote(t, store, "team", "https://attacker.example/peerdb")
|
||||
|
||||
called := false
|
||||
err := store.withPeerAuth(ctx, "team", func(string) error {
|
||||
called = true
|
||||
return nil
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("withPeerAuth succeeded, want diverged-URL refusal")
|
||||
}
|
||||
if called {
|
||||
t.Error("withPeerAuth ran the operation, want fail-closed")
|
||||
}
|
||||
for _, want := range []string{"https://attacker.example/peerdb", "https://peer.example/peerdb", "diverged"} {
|
||||
if !strings.Contains(err.Error(), want) {
|
||||
t.Errorf("error %q does not mention %q", err, want)
|
||||
}
|
||||
}
|
||||
if got := os.Getenv("DOLT_REMOTE_PASSWORD"); got != "envpass" {
|
||||
t.Errorf("DOLT_REMOTE_PASSWORD after refusal = %q, want untouched %q", got, "envpass")
|
||||
}
|
||||
}
|
||||
|
||||
// A peer row with stored credentials but no live remote has no verified
|
||||
// destination to authenticate against, so it fails closed the same way.
|
||||
func TestWithPeerAuth_MissingRemoteFailsClosed(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
store := newPeerAuthTestStore(t)
|
||||
|
||||
if err := store.AddFederationPeer(ctx, &storage.FederationPeer{
|
||||
Name: "team", RemoteURL: "https://peer.example/peerdb",
|
||||
Username: "peeruser", Password: "peerpass",
|
||||
}); err != nil {
|
||||
t.Fatalf("AddFederationPeer: %v", err)
|
||||
}
|
||||
if err := store.withConn(ctx, true, func(tx *sql.Tx) error {
|
||||
_, err := tx.ExecContext(ctx, "CALL DOLT_REMOTE('remove', ?)", "team")
|
||||
return err
|
||||
}); err != nil {
|
||||
t.Fatalf("remove remote: %v", err)
|
||||
}
|
||||
|
||||
called := false
|
||||
err := store.withPeerAuth(ctx, "team", func(string) error {
|
||||
called = true
|
||||
return nil
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("withPeerAuth succeeded, want missing-remote refusal")
|
||||
}
|
||||
if called {
|
||||
t.Error("withPeerAuth ran the operation, want fail-closed")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "no remote named team") {
|
||||
t.Errorf("error %q does not name the missing remote", err)
|
||||
}
|
||||
}
|
||||
|
||||
// The environment fallback serializes with stored-peer operations (GH#5085
|
||||
// review): the in-process Dolt engine reads DOLT_REMOTE_PASSWORD from the
|
||||
// process environment, so a plain-remote operation running concurrently with
|
||||
// a stored-peer operation would otherwise observe the peer's temporarily
|
||||
// installed password.
|
||||
func TestWithPeerAuth_EnvFallbackDoesNotObservePeerCredentials(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
store := newPeerAuthTestStore(t)
|
||||
|
||||
t.Setenv("DOLT_REMOTE_USER", "envuser")
|
||||
t.Setenv("DOLT_REMOTE_PASSWORD", "envpass")
|
||||
|
||||
if err := store.AddFederationPeer(ctx, &storage.FederationPeer{
|
||||
Name: "team", RemoteURL: "https://peer.example/peerdb",
|
||||
Username: "peeruser", Password: "peerpass",
|
||||
}); err != nil {
|
||||
t.Fatalf("AddFederationPeer: %v", err)
|
||||
}
|
||||
|
||||
peerEntered := make(chan struct{})
|
||||
releasePeer := make(chan struct{})
|
||||
peerDone := make(chan error, 1)
|
||||
go func() {
|
||||
peerDone <- store.withPeerAuth(ctx, "team", func(string) error {
|
||||
close(peerEntered)
|
||||
<-releasePeer
|
||||
return nil
|
||||
})
|
||||
}()
|
||||
<-peerEntered
|
||||
|
||||
var gotUser, gotPwd string
|
||||
fallbackDone := make(chan error, 1)
|
||||
go func() {
|
||||
fallbackDone <- store.withPeerAuth(ctx, "not-a-peer", func(user string) error {
|
||||
gotUser = user
|
||||
gotPwd = os.Getenv("DOLT_REMOTE_PASSWORD")
|
||||
return nil
|
||||
})
|
||||
}()
|
||||
|
||||
select {
|
||||
case err := <-fallbackDone:
|
||||
t.Fatalf("fallback ran while a stored-peer operation held the credentials (err=%v), want it to wait", err)
|
||||
case <-time.After(50 * time.Millisecond):
|
||||
}
|
||||
|
||||
close(releasePeer)
|
||||
if err := <-peerDone; err != nil {
|
||||
t.Fatalf("stored-peer withPeerAuth: %v", err)
|
||||
}
|
||||
if err := <-fallbackDone; err != nil {
|
||||
t.Fatalf("fallback withPeerAuth: %v", err)
|
||||
}
|
||||
if gotUser != "envuser" {
|
||||
t.Errorf("fallback user = %q, want ambient %q", gotUser, "envuser")
|
||||
}
|
||||
if gotPwd != "envpass" {
|
||||
t.Errorf("fallback observed DOLT_REMOTE_PASSWORD %q, want ambient %q", gotPwd, "envpass")
|
||||
}
|
||||
}
|
||||
|
|
@ -561,14 +561,18 @@ func (s *EmbeddedDoltStore) PullRemote(ctx context.Context, remote string) error
|
|||
}
|
||||
|
||||
func (s *EmbeddedDoltStore) Fetch(ctx context.Context, peer string) error {
|
||||
return s.withMutatingDBConn(ctx, func(db versioncontrolops.DBConn) error {
|
||||
return versioncontrolops.Fetch(ctx, db, peer, remoteAuthUser())
|
||||
return s.withPeerAuth(ctx, peer, func(user string) error {
|
||||
return s.withMutatingDBConn(ctx, func(db versioncontrolops.DBConn) error {
|
||||
return versioncontrolops.Fetch(ctx, db, peer, user)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *EmbeddedDoltStore) PushTo(ctx context.Context, peer string) error {
|
||||
return s.withMutatingDBConn(ctx, func(db versioncontrolops.DBConn) error {
|
||||
return versioncontrolops.Push(ctx, db, peer, s.branch, remoteAuthUser())
|
||||
return s.withPeerAuth(ctx, peer, func(user string) error {
|
||||
return s.withMutatingDBConn(ctx, func(db versioncontrolops.DBConn) error {
|
||||
return versioncontrolops.Push(ctx, db, peer, s.branch, user)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -581,20 +585,22 @@ func (s *EmbeddedDoltStore) PullFrom(ctx context.Context, peer string) ([]storag
|
|||
|
||||
preHead := s.preMergeHead(ctx)
|
||||
var conflicts []storage.Conflict
|
||||
err := s.withMutatingPinnedDBConn(ctx, func(db versioncontrolops.DBConn) error {
|
||||
if pullErr := versioncontrolops.Pull(ctx, db, peer, s.branch, remoteAuthUser()); pullErr != nil {
|
||||
// bd-578h9.15: the settle machinery aborts a merge it cannot
|
||||
// auto-resolve before returning, so dolt_conflicts is already
|
||||
// empty here; the conflicts arrive captured pre-abort inside
|
||||
// MergeConflictsError instead.
|
||||
var mce *versioncontrolops.MergeConflictsError
|
||||
if errors.As(pullErr, &mce) {
|
||||
conflicts = mce.Conflicts
|
||||
return nil
|
||||
err := s.withPeerAuth(ctx, peer, func(user string) error {
|
||||
return s.withMutatingPinnedDBConn(ctx, func(db versioncontrolops.DBConn) error {
|
||||
if pullErr := versioncontrolops.Pull(ctx, db, peer, s.branch, user); pullErr != nil {
|
||||
// bd-578h9.15: the settle machinery aborts a merge it cannot
|
||||
// auto-resolve before returning, so dolt_conflicts is already
|
||||
// empty here; the conflicts arrive captured pre-abort inside
|
||||
// MergeConflictsError instead.
|
||||
var mce *versioncontrolops.MergeConflictsError
|
||||
if errors.As(pullErr, &mce) {
|
||||
conflicts = mce.Conflicts
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("pull from %s: %w", peer, pullErr)
|
||||
}
|
||||
return fmt.Errorf("pull from %s: %w", peer, pullErr)
|
||||
}
|
||||
return nil
|
||||
return nil
|
||||
})
|
||||
})
|
||||
if err != nil || len(conflicts) > 0 {
|
||||
// Conflicted pulls skip the recompute: the operator resolves first,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue