Skip to content

Commit 4bc6a88

Browse files
Callum Styanclaude
andcommitted
refactor: use ContextInject helper to reduce code duplication
Replaced direct WithWorkspaceRBAC calls with the existing CachedWorkspaceFields.ContextInject() helper in connectionlog.go, metadata.go, and stats.go. This reduces code duplication and ensures consistent error handling when injecting workspace RBAC into context for the dbauthz fast path. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 067ab1d commit 4bc6a88

File tree

3 files changed

+19
-29
lines changed

3 files changed

+19
-29
lines changed

coderd/agentapi/connectionlog.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/coder/coder/v2/coderd/connectionlog"
1515
"github.com/coder/coder/v2/coderd/database"
1616
"github.com/coder/coder/v2/coderd/database/db2sdk"
17-
"github.com/coder/coder/v2/coderd/database/dbauthz"
1817
)
1918

2019
type ConnLogAPI struct {
@@ -55,16 +54,16 @@ func (a *ConnLogAPI) ReportConnection(ctx context.Context, req *agentproto.Repor
5554

5655
// Inject RBAC object into context for dbauthz fast path, avoid having to
5756
// call GetWorkspaceByAgentID on every metadata update.
58-
rbacCtx := ctx
57+
rbacCtx, err := a.Workspace.ContextInject(ctx)
58+
if err != nil {
59+
// Don't error level log here, will exit the function. We want to fall back to GetWorkspaceByAgentID.
60+
//nolint:gocritic
61+
a.Log.Debug(ctx, "Cached workspace was present but RBAC object was invalid", slog.F("err", err))
62+
rbacCtx = ctx
63+
}
5964
var ws database.WorkspaceIdentity
6065
if dbws, ok := a.Workspace.AsWorkspaceIdentity(); ok {
6166
ws = dbws
62-
rbacCtx, err = dbauthz.WithWorkspaceRBAC(ctx, dbws.RBACObject())
63-
if err != nil {
64-
// Don't error level log here, will exit the function. We want to fall back to GetWorkspaceByAgentID.
65-
//nolint:gocritic
66-
a.Log.Debug(ctx, "Cached workspace was present but RBAC object was invalid", slog.F("err", err))
67-
}
6867
}
6968

7069
// Fetch contextual data for this connection log event.

coderd/agentapi/metadata.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"cdr.dev/slog"
1313
agentproto "github.com/coder/coder/v2/agent/proto"
1414
"github.com/coder/coder/v2/coderd/database"
15-
"github.com/coder/coder/v2/coderd/database/dbauthz"
1615
"github.com/coder/coder/v2/coderd/database/dbtime"
1716
"github.com/coder/coder/v2/coderd/database/pubsub"
1817
)
@@ -49,15 +48,12 @@ func (a *MetadataAPI) BatchUpdateMetadata(ctx context.Context, req *agentproto.B
4948

5049
// Inject RBAC object into context for dbauthz fast path, avoid having to
5150
// call GetWorkspaceByAgentID on every metadata update.
52-
var err error
53-
rbacCtx := ctx
54-
if dbws, ok := a.Workspace.AsWorkspaceIdentity(); ok {
55-
rbacCtx, err = dbauthz.WithWorkspaceRBAC(ctx, dbws.RBACObject())
56-
if err != nil {
57-
// Don't error level log here, will exit the function. We want to fall back to GetWorkspaceByAgentID.
58-
//nolint:gocritic
59-
a.Log.Debug(ctx, "Cached workspace was present but RBAC object was invalid", slog.F("err", err))
60-
}
51+
rbacCtx, err := a.Workspace.ContextInject(ctx)
52+
if err != nil {
53+
// Don't error level log here, will exit the function. We want to fall back to GetWorkspaceByAgentID.
54+
//nolint:gocritic
55+
a.Log.Debug(ctx, "Cached workspace was present but RBAC object was invalid", slog.F("err", err))
56+
rbacCtx = ctx
6157
}
6258

6359
workspaceAgent, err := a.AgentFn(rbacCtx)

coderd/agentapi/stats.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"cdr.dev/slog"
1111
agentproto "github.com/coder/coder/v2/agent/proto"
1212
"github.com/coder/coder/v2/coderd/database"
13-
"github.com/coder/coder/v2/coderd/database/dbauthz"
1413
"github.com/coder/coder/v2/coderd/database/dbtime"
1514
"github.com/coder/coder/v2/coderd/workspacestats"
1615
"github.com/coder/coder/v2/codersdk"
@@ -46,16 +45,12 @@ func (a *StatsAPI) UpdateStats(ctx context.Context, req *agentproto.UpdateStatsR
4645

4746
// Inject RBAC object into context for dbauthz fast path, avoid having to
4847
// call GetWorkspaceAgentByID on every stats update.
49-
50-
rbacCtx := ctx
51-
if dbws, ok := a.Workspace.AsWorkspaceIdentity(); ok {
52-
var err error
53-
rbacCtx, err = dbauthz.WithWorkspaceRBAC(ctx, dbws.RBACObject())
54-
if err != nil {
55-
// Don't error level log here, will exit the function. We want to fall back to GetWorkspaceByAgentID.
56-
//nolint:gocritic
57-
a.Log.Debug(ctx, "Cached workspace was present but RBAC object was invalid", slog.F("err", err))
58-
}
48+
rbacCtx, err := a.Workspace.ContextInject(ctx)
49+
if err != nil {
50+
// Don't error level log here, will exit the function. We want to fall back to GetWorkspaceByAgentID.
51+
//nolint:gocritic
52+
a.Log.Debug(ctx, "Cached workspace was present but RBAC object was invalid", slog.F("err", err))
53+
rbacCtx = ctx
5954
}
6055

6156
workspaceAgent, err := a.AgentFn(rbacCtx)

0 commit comments

Comments
 (0)