Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8757e3f

Browse files
committed
WIP
Signed-off-by: Callum Styan <callumstyan@gmail.com>
1 parent5fc6219 commit8757e3f

File tree

5 files changed

+46
-15
lines changed

5 files changed

+46
-15
lines changed

‎coderd/agentapi/api.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ func New(opts Options, workspace database.Workspace) *API {
197197
AgentFn:api.agent,
198198
ConnectionLogger:opts.ConnectionLogger,
199199
Database:opts.Database,
200+
Workspace:api.cachedWorkspaceFields,
200201
Log:opts.Log,
201202
}
202203

‎coderd/agentapi/connectionlog.go‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ import (
1212
"cdr.dev/slog"
1313
agentproto"github.com/coder/coder/v2/agent/proto"
1414
"github.com/coder/coder/v2/coderd/connectionlog"
15+
"github.com/coder/coder/v2/coderd/database/dbauthz"
1516
"github.com/coder/coder/v2/coderd/database"
1617
"github.com/coder/coder/v2/coderd/database/db2sdk"
1718
)
1819

1920
typeConnLogAPIstruct {
2021
AgentFnfunc(context.Context) (database.WorkspaceAgent,error)
2122
ConnectionLogger*atomic.Pointer[connectionlog.ConnectionLogger]
23+
Workspace*CachedWorkspaceFields
2224
Database database.Store
2325
Log slog.Logger
2426
}
@@ -51,8 +53,20 @@ func (a *ConnLogAPI) ReportConnection(ctx context.Context, req *agentproto.Repor
5153
}
5254
}
5355

56+
// Inject RBAC object into context for dbauthz fast path, avoid having to
57+
// call GetWorkspaceByAgentID on every metadata update.
58+
rbacCtx:=ctx
59+
ifdbws,ok:=a.Workspace.AsWorkspaceIdentity();ok {
60+
rbacCtx,err=dbauthz.WithWorkspaceRBAC(ctx,dbws.RBACObject())
61+
iferr!=nil {
62+
// Don't error level log here, will exit the function. We want to fall back to GetWorkspaceByAgentID.
63+
//nolint:gocritic
64+
a.Log.Debug(ctx,"Cached workspace was present but RBAC object was invalid",slog.F("err",err))
65+
}
66+
}
67+
5468
// Fetch contextual data for this connection log event.
55-
workspaceAgent,err:=a.AgentFn(ctx)
69+
workspaceAgent,err:=a.AgentFn(rbacCtx)
5670
iferr!=nil {
5771
returnnil,xerrors.Errorf("get agent: %w",err)
5872
}

‎coderd/agentapi/connectionlog_test.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ func TestConnectionLog(t *testing.T) {
117117
AgentFn:func(context.Context) (database.WorkspaceAgent,error) {
118118
returnagent,nil
119119
},
120+
Workspace:&agentapi.CachedWorkspaceFields{},
120121
}
121122
api.ReportConnection(context.Background(),&agentproto.ReportConnectionRequest{
122123
Connection:&agentproto.Connection{

‎coderd/agentapi/metadata.go‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,20 @@ func (a *MetadataAPI) BatchUpdateMetadata(ctx context.Context, req *agentproto.B
4747
maxErrorLen=maxValueLen
4848
)
4949

50-
workspaceAgent,err:=a.AgentFn(ctx)
50+
// Inject RBAC object into context for dbauthz fast path, avoid having to
51+
// call GetWorkspaceByAgentID on every metadata update.
52+
varerrerror
53+
rbacCtx:=ctx
54+
ifdbws,ok:=a.Workspace.AsWorkspaceIdentity();ok {
55+
rbacCtx,err=dbauthz.WithWorkspaceRBAC(ctx,dbws.RBACObject())
56+
iferr!=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+
}
61+
}
62+
63+
workspaceAgent,err:=a.AgentFn(rbacCtx)
5164
iferr!=nil {
5265
returnnil,err
5366
}
@@ -109,18 +122,6 @@ func (a *MetadataAPI) BatchUpdateMetadata(ctx context.Context, req *agentproto.B
109122
)
110123
}
111124

112-
// Inject RBAC object into context for dbauthz fast path, avoid having to
113-
// call GetWorkspaceByAgentID on every metadata update.
114-
rbacCtx:=ctx
115-
ifdbws,ok:=a.Workspace.AsWorkspaceIdentity();ok {
116-
rbacCtx,err=dbauthz.WithWorkspaceRBAC(ctx,dbws.RBACObject())
117-
iferr!=nil {
118-
// Don't error level log here, will exit the function. We want to fall back to GetWorkspaceByAgentID.
119-
//nolint:gocritic
120-
a.Log.Debug(ctx,"Cached workspace was present but RBAC object was invalid",slog.F("err",err))
121-
}
122-
}
123-
124125
err=a.Database.UpdateWorkspaceAgentMetadata(rbacCtx,dbUpdate)
125126
iferr!=nil {
126127
returnnil,xerrors.Errorf("update workspace agent metadata in database: %w",err)

‎coderd/agentapi/stats.go‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ 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"
1314
"github.com/coder/coder/v2/coderd/database/dbtime"
1415
"github.com/coder/coder/v2/coderd/workspacestats"
1516
"github.com/coder/coder/v2/codersdk"
@@ -43,7 +44,20 @@ func (a *StatsAPI) UpdateStats(ctx context.Context, req *agentproto.UpdateStatsR
4344
returnres,nil
4445
}
4546

46-
workspaceAgent,err:=a.AgentFn(ctx)
47+
// Inject RBAC object into context for dbauthz fast path, avoid having to
48+
// call GetWorkspaceByAgentID on every metadata update.
49+
varerrerror
50+
rbacCtx:=ctx
51+
ifdbws,ok:=a.Workspace.AsWorkspaceIdentity();ok {
52+
rbacCtx,err=dbauthz.WithWorkspaceRBAC(ctx,dbws.RBACObject())
53+
iferr!=nil {
54+
// Don't error level log here, will exit the function. We want to fall back to GetWorkspaceByAgentID.
55+
//nolint:gocritic
56+
a.Log.Debug(ctx,"Cached workspace was present but RBAC object was invalid",slog.F("err",err))
57+
}
58+
}
59+
60+
workspaceAgent,err:=a.AgentFn(rbacCtx)
4761
iferr!=nil {
4862
returnnil,err
4963
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp