Skip to content

Commit 55ee6d3

Browse files
committed
address more review feedback; attempt to simplify cached workspace is
valid/present checks, plus ensure proper fallback (less noisy logging) when a cached workspace is not found/valid in dbauthz Signed-off-by: Callum Styan <callumstyan@gmail.com>
1 parent 089a5ef commit 55ee6d3

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

coderd/agentapi/cached_workspace.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,14 @@ func (cws *CachedWorkspaceFields) UpdateValues(ws database.Workspace) {
7373
cws.AutostartSchedule = ws.AutostartSchedule
7474
}
7575

76-
func (cws *CachedWorkspaceFields) AsDatabaseWorkspace() database.Workspace {
76+
// Returns the Workspace, true, unless the workspace has not been cached (nuked or was a prebuild).
77+
func (cws *CachedWorkspaceFields) AsDatabaseWorkspace() (database.Workspace, bool) {
7778
cws.lock.RLock()
7879
defer cws.lock.RUnlock()
80+
// Should we be more explicit about all fields being set to be valid?
81+
if cws.Equal(&CachedWorkspaceFields{}) {
82+
return database.Workspace{}, false
83+
}
7984
return database.Workspace{
8085
ID: cws.ID,
8186
OwnerID: cws.OwnerID,
@@ -85,5 +90,5 @@ func (cws *CachedWorkspaceFields) AsDatabaseWorkspace() database.Workspace {
8590
OwnerUsername: cws.OwnerUsername,
8691
TemplateName: cws.TemplateName,
8792
AutostartSchedule: cws.AutostartSchedule,
88-
}
93+
}, true
8994
}

coderd/agentapi/metadata.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,11 @@ func (a *MetadataAPI) BatchUpdateMetadata(ctx context.Context, req *agentproto.B
111111

112112
// Inject RBAC object into context for dbauthz fast path, avoid having to
113113
// call GetWorkspaceByAgentID on every metadata update.
114-
rbacCtx, err := dbauthz.WithWorkspaceRBAC(ctx, a.Workspace.AsDatabaseWorkspace().RBACObject())
115-
if err != nil {
116-
// Don't use error level here; that will fail the call but in reality an invalid object here is okay
117-
// since we will then just fallback to the actual GetWorkspaceByAgentID call in dbauthz.
118-
a.Log.Warn(ctx, "cached RBAC Workspace context wrapping was invalid", slog.Error(err))
114+
rbacCtx := ctx
115+
if dbws, ok := a.Workspace.AsDatabaseWorkspace(); ok {
116+
rbacCtx, err = dbauthz.WithWorkspaceRBAC(ctx, dbws.RBACObject())
119117
}
118+
120119
err = a.Database.UpdateWorkspaceAgentMetadata(rbacCtx, dbUpdate)
121120
if err != nil {
122121
return nil, xerrors.Errorf("update workspace agent metadata in database: %w", err)

coderd/agentapi/stats.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"time"
66

7-
"github.com/google/uuid"
87
"golang.org/x/xerrors"
98
"google.golang.org/protobuf/types/known/durationpb"
109

@@ -50,8 +49,9 @@ func (a *StatsAPI) UpdateStats(ctx context.Context, req *agentproto.UpdateStatsR
5049
}
5150

5251
// If cache is empty (prebuild or invalid), fall back to DB
53-
ws := a.Workspace.AsDatabaseWorkspace()
54-
if a.Workspace.ID == uuid.Nil {
52+
var ws database.Workspace
53+
var ok bool
54+
if ws, ok = a.Workspace.AsDatabaseWorkspace(); !ok {
5555
ws, err = a.Database.GetWorkspaceByAgentID(ctx, workspaceAgent.ID)
5656
if err != nil {
5757
return nil, xerrors.Errorf("get workspace by agent ID %q: %w", workspaceAgent.ID, err)

0 commit comments

Comments
 (0)