Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/envd/internal/api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,10 @@ func writeInitError(w http.ResponseWriter, logger zerolog.Logger, err error) {
switch {
case errors.Is(err, ErrAccessTokenMismatch), errors.Is(err, ErrAccessTokenResetNotAuthorized):
w.WriteHeader(http.StatusUnauthorized)
case errors.Is(err, host.ErrCAInstallInProgress):
// Not a failure, a concurrent install still holds the CA lock.
logger.Warn().Err(err).Msg("CA initialization still in progress, retrying")
w.WriteHeader(http.StatusServiceUnavailable)
default:
logger.Error().Msgf("Failed to set data: %v", err)
w.WriteHeader(http.StatusBadRequest)
Expand Down
7 changes: 6 additions & 1 deletion packages/envd/internal/host/cacerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package host

import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
Expand All @@ -12,6 +13,9 @@ import (
"golang.org/x/sync/semaphore"
)

// ErrCAInstallInProgress means the lock is held by a prior install's cleanup.
var ErrCAInstallInProgress = errors.New("CA install already in progress")

const (
CaBundlePath = "/etc/ssl/certs/ca-certificates.crt"

Expand Down Expand Up @@ -81,8 +85,9 @@ func (c *CACertInstaller) install(ctx context.Context, certPEM, bundlePath, extr
// consistent regardless of how the caller formatted the PEM.
normalized := strings.TrimRight(certPEM, "\n") + "\n"

// Cancellation here means a prior install's cleanup still holds mu (83ee89f9b).
if err := c.mu.Acquire(ctx, 1); err != nil {
return fmt.Errorf("acquire CA install lock: %w", err)
return fmt.Errorf("%w: %w", ErrCAInstallInProgress, err)
}
defer c.mu.Release(1)

Expand Down
20 changes: 20 additions & 0 deletions packages/envd/internal/host/cacerts_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package host

import (
"context"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -235,6 +236,25 @@ func TestInstallCACert_ConcurrentResume(t *testing.T) {
assert.Equal(t, 1, strings.Count(string(bundle), normalized), "cert should appear exactly once")
}

// A canceled acquire while a previous install's cleanup still holds mu must
// return ErrCAInstallInProgress, not a raw context error.
func TestInstallCACert_CanceledCtxUnderContention(t *testing.T) {
t.Parallel()
bundlePath, extraPath := testPaths(t)
c := newTestInstaller(t)

// Simulate the background cleanup goroutine still holding mu.
require.NoError(t, c.mu.Acquire(t.Context(), 1))
defer c.mu.Release(1)

canceledCtx, cancel := context.WithCancel(t.Context())
cancel()

err := c.install(canceledCtx, certA, bundlePath, extraPath)
require.ErrorIs(t, err, ErrCAInstallInProgress)
require.ErrorIs(t, err, context.Canceled)
}

func TestRemoveCertFromBundle(t *testing.T) {
t.Parallel()
dir := t.TempDir()
Expand Down
2 changes: 1 addition & 1 deletion packages/envd/pkg/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package pkg

const Version = "0.6.7"
const Version = "0.6.8"
Loading