Skip to content

fix: avoid race between replicas on start #12344

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

Merged
merged 3 commits into from
Feb 28, 2024
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
1 change: 1 addition & 0 deletions coderd/database/dbauthz/dbauthz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1874,6 +1874,7 @@ func (s *MethodTestSuite) TestSystemFunctions() {
check.Args(u.ID).Asserts(rbac.ResourceSystem, rbac.ActionRead)
}))
s.Run("GetDERPMeshKey", s.Subtest(func(db database.Store, check *expects) {
db.InsertDERPMeshKey(context.Background(), "testing")
check.Args().Asserts(rbac.ResourceSystem, rbac.ActionRead)
}))
s.Run("InsertDERPMeshKey", s.Subtest(func(db database.Store, check *expects) {
Expand Down
3 changes: 3 additions & 0 deletions coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -1761,6 +1761,9 @@ func (q *FakeQuerier) GetDERPMeshKey(_ context.Context) (string, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()

if q.derpMeshKey == "" {
return "", sql.ErrNoRows
}
Comment on lines +1764 to +1766
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦 #justdbmemthings

return q.derpMeshKey, nil
}

Expand Down
1 change: 1 addition & 0 deletions coderd/database/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const (
// Keep the unused iota here so we don't need + 1 every time
lockIDUnused = iota
LockIDDeploymentSetup
LockIDEnterpriseDeploymentSetup
)

// GenLockID generates a unique and consistent lock ID from a given string.
Expand Down
33 changes: 27 additions & 6 deletions enterprise/cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"tailscale.com/types/key"

"github.com/coder/coder/v2/cli/clibase"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/cryptorand"
"github.com/coder/coder/v2/enterprise/audit"
"github.com/coder/coder/v2/enterprise/audit/backends"
Expand All @@ -37,21 +38,41 @@ func (r *RootCmd) Server(_ func()) *clibase.Cmd {
}

options.DERPServer = derp.NewServer(key.NewNode(), tailnet.Logger(options.Logger.Named("derp")))
meshKey, err := options.Database.GetDERPMeshKey(ctx)
if err != nil {

var meshKey string
err := options.Database.InTx(func(tx database.Store) error {
// This will block until the lock is acquired, and will be
// automatically released when the transaction ends.
err := tx.AcquireLock(ctx, database.LockIDEnterpriseDeploymentSetup)
if err != nil {
return xerrors.Errorf("acquire lock: %w", err)
}

meshKey, err = tx.GetDERPMeshKey(ctx)
if err == nil {
return nil
}
if !errors.Is(err, sql.ErrNoRows) {
return nil, nil, xerrors.Errorf("get mesh key: %w", err)
return xerrors.Errorf("get DERP mesh key: %w", err)
}
meshKey, err = cryptorand.String(32)
if err != nil {
return nil, nil, xerrors.Errorf("generate mesh key: %w", err)
return xerrors.Errorf("generate DERP mesh key: %w", err)
}
err = options.Database.InsertDERPMeshKey(ctx, meshKey)
err = tx.InsertDERPMeshKey(ctx, meshKey)
if err != nil {
return nil, nil, xerrors.Errorf("insert mesh key: %w", err)
return xerrors.Errorf("insert DERP mesh key: %w", err)
}
return nil
}, nil)
if err != nil {
return nil, nil, err
}
if meshKey == "" {
return nil, nil, xerrors.New("mesh key is empty")
}
options.DERPServer.SetMeshKey(meshKey)

options.Auditor = audit.NewAuditor(
options.Database,
audit.DefaultFilter,
Expand Down