diff --git a/pkg/gpg/gpg_forwarding.go b/pkg/gpg/gpg_forwarding.go index 9a4e181245..744e6b4deb 100644 --- a/pkg/gpg/gpg_forwarding.go +++ b/pkg/gpg/gpg_forwarding.go @@ -182,8 +182,17 @@ func (g *GPGConf) SetupRemoteSocketLink(ctx context.Context) error { // binds as root; a non-root user needs write access to connect. The socket // is bound asynchronously by the ssh server, so this waits for it to appear. func (g *GPGConf) claimForwardedSocket(ctx context.Context) error { + if err := g.waitForForwardedSocket(ctx); err != nil { + return err + } + owner := strconv.Itoa(os.Getuid()) + ":" + strconv.Itoa(os.Getgid()) + //nolint:gosec // g.SocketPath is the fixed forwarded socket path + return exec.Command("sudo", "chown", owner, g.SocketPath).Run() +} + +func (g *GPGConf) waitForForwardedSocket(ctx context.Context) error { backoff := wait.Backoff{ Duration: 200 * time.Millisecond, Factor: 1.5, @@ -216,8 +225,7 @@ func (g *GPGConf) claimForwardedSocket(ctx context.Context) error { return err } - //nolint:gosec // g.SocketPath is the fixed forwarded socket path - return exec.Command("sudo", "chown", owner, g.SocketPath).Run() + return nil } func gpgConfigPath() string { diff --git a/pkg/gpg/gpg_forwarding_test.go b/pkg/gpg/gpg_forwarding_test.go index 3f9bce8921..421246714a 100644 --- a/pkg/gpg/gpg_forwarding_test.go +++ b/pkg/gpg/gpg_forwarding_test.go @@ -7,7 +7,6 @@ import ( "errors" "net" "os" - "os/exec" "path/filepath" "strings" "testing" @@ -85,7 +84,7 @@ func TestSetupGpgConf_ExistingFileWithoutTrailingNewline(t *testing.T) { } } -func TestClaimForwardedSocket_StopsPollingAsSoonAsSocketAppears(t *testing.T) { +func TestWaitForForwardedSocket_StopsPollingAsSoonAsSocketAppears(t *testing.T) { socketPath := filepath.Join(shortSocketDir(t), "S.gpg-agent") g := &GPGConf{SocketPath: socketPath} @@ -97,56 +96,43 @@ func TestClaimForwardedSocket_StopsPollingAsSoonAsSocketAppears(t *testing.T) { time.Sleep(50 * time.Millisecond) listener, listenErr = listenUnixSocket(socketPath) }() - defer func() { + t.Cleanup(func() { <-listenerReady require.NoError(t, listenErr) - _ = listener.Close() - }() + if listener != nil { + require.NoError(t, listener.Close()) + } + }) - start := time.Now() - err := g.claimForwardedSocket(context.Background()) - elapsed := time.Since(start) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() - assert.Less(t, elapsed, 2*time.Second, "must return shortly after the socket appears") - if err != nil { - var exitErr *exec.ExitError - var execErr *exec.Error - assert.True(t, errors.As(err, &exitErr) || errors.As(err, &execErr), - "only a sudo chown failure is tolerated here, got: %v", err) - } + require.NoError(t, g.waitForForwardedSocket(ctx)) } -func TestClaimForwardedSocket_TimesOutWhenSocketNeverAppears(t *testing.T) { +func TestWaitForForwardedSocket_TimesOutWhenSocketNeverAppears(t *testing.T) { g := &GPGConf{SocketPath: filepath.Join(t.TempDir(), "S.gpg-agent")} ctx, cancel := context.WithTimeout(context.Background(), 35*time.Second) defer cancel() - err := g.claimForwardedSocket(ctx) + err := g.waitForForwardedSocket(ctx) require.Error(t, err) assert.Contains(t, err.Error(), "did not appear") } -func TestClaimForwardedSocket_RejectsNonSocketPath(t *testing.T) { +func TestWaitForForwardedSocket_RejectsNonSocketPath(t *testing.T) { socketPath := filepath.Join(t.TempDir(), "S.gpg-agent") require.NoError(t, os.WriteFile(socketPath, []byte("not a socket"), 0o600)) g := &GPGConf{SocketPath: socketPath} - start := time.Now() - err := g.claimForwardedSocket(context.Background()) - elapsed := time.Since(start) + err := g.waitForForwardedSocket(context.Background()) require.Error(t, err) assert.Contains(t, err.Error(), "not a unix socket") - assert.Less( - t, - elapsed, - time.Second, - "must fail immediately, not retry against a non-socket path", - ) } -func TestClaimForwardedSocket_FailsFastOnPermissionError(t *testing.T) { +func TestWaitForForwardedSocket_FailsFastOnPermissionError(t *testing.T) { if os.Getuid() == 0 { t.Skip("root bypasses directory permission checks") } @@ -159,35 +145,24 @@ func TestClaimForwardedSocket_FailsFastOnPermissionError(t *testing.T) { g := &GPGConf{SocketPath: socketPath} - start := time.Now() - err := g.claimForwardedSocket(context.Background()) - elapsed := time.Since(start) + err := g.waitForForwardedSocket(context.Background()) require.Error(t, err) assert.NotContains(t, err.Error(), "did not appear", "a permission error must not be reported as the socket never appearing") - assert.Less( - t, - elapsed, - time.Second, - "must fail immediately, not retry against a permission error", - ) } -func TestClaimForwardedSocket_RespectsContextCancellation(t *testing.T) { +func TestWaitForForwardedSocket_RespectsContextCancellation(t *testing.T) { g := &GPGConf{SocketPath: filepath.Join(t.TempDir(), "S.gpg-agent")} ctx, cancel := context.WithCancel(context.Background()) cancel() - start := time.Now() - err := g.claimForwardedSocket(ctx) - elapsed := time.Since(start) + err := g.waitForForwardedSocket(ctx) require.Error(t, err) assert.True(t, errors.Is(err, context.Canceled), "error must wrap context.Canceled, not just any error: got %v", err) - assert.Less(t, elapsed, time.Second, "cancelled context must not wait out the full backoff") } func readConf(t *testing.T, path string) string {