Skip to content

Commit 8708d81

Browse files
chore: cherry-pick #17934 into 2.22 (#17952)
Cherry-pick of #17934 --------- Signed-off-by: Thomas Kosiewski <tk@coder.com> Signed-off-by: Danny Kopping <dannykopping@gmail.com> Co-authored-by: Thomas Kosiewski <tk@coder.com>
1 parent 32f093e commit 8708d81

File tree

6 files changed

+192
-115
lines changed

6 files changed

+192
-115
lines changed

coderd/agentapi/manifest.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ func (a *ManifestAPI) GetManifest(ctx context.Context, _ *agentproto.GetManifest
4747
scripts []database.WorkspaceAgentScript
4848
metadata []database.WorkspaceAgentMetadatum
4949
workspace database.Workspace
50-
owner database.User
5150
devcontainers []database.WorkspaceAgentDevcontainer
5251
)
5352

@@ -76,10 +75,6 @@ func (a *ManifestAPI) GetManifest(ctx context.Context, _ *agentproto.GetManifest
7675
if err != nil {
7776
return xerrors.Errorf("getting workspace by id: %w", err)
7877
}
79-
owner, err = a.Database.GetUserByID(ctx, workspace.OwnerID)
80-
if err != nil {
81-
return xerrors.Errorf("getting workspace owner by id: %w", err)
82-
}
8378
return err
8479
})
8580
eg.Go(func() (err error) {
@@ -98,7 +93,7 @@ func (a *ManifestAPI) GetManifest(ctx context.Context, _ *agentproto.GetManifest
9893
AppSlugOrPort: "{{port}}",
9994
AgentName: workspaceAgent.Name,
10095
WorkspaceName: workspace.Name,
101-
Username: owner.Username,
96+
Username: workspace.OwnerUsername,
10297
}
10398

10499
vscodeProxyURI := vscodeProxyURI(appSlug, a.AccessURL, a.AppHostname)
@@ -115,15 +110,15 @@ func (a *ManifestAPI) GetManifest(ctx context.Context, _ *agentproto.GetManifest
115110
}
116111
}
117112

118-
apps, err := dbAppsToProto(dbApps, workspaceAgent, owner.Username, workspace)
113+
apps, err := dbAppsToProto(dbApps, workspaceAgent, workspace.OwnerUsername, workspace)
119114
if err != nil {
120115
return nil, xerrors.Errorf("converting workspace apps: %w", err)
121116
}
122117

123118
return &agentproto.Manifest{
124119
AgentId: workspaceAgent.ID[:],
125120
AgentName: workspaceAgent.Name,
126-
OwnerUsername: owner.Username,
121+
OwnerUsername: workspace.OwnerUsername,
127122
WorkspaceId: workspace.ID[:],
128123
WorkspaceName: workspace.Name,
129124
GitAuthConfigs: gitAuthConfigs,

coderd/agentapi/manifest_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ func TestGetManifest(t *testing.T) {
4646
Username: "cool-user",
4747
}
4848
workspace = database.Workspace{
49-
ID: uuid.New(),
50-
OwnerID: owner.ID,
51-
Name: "cool-workspace",
49+
ID: uuid.New(),
50+
OwnerID: owner.ID,
51+
OwnerUsername: owner.Username,
52+
Name: "cool-workspace",
5253
}
5354
agent = database.WorkspaceAgent{
5455
ID: uuid.New(),
@@ -329,7 +330,6 @@ func TestGetManifest(t *testing.T) {
329330
}).Return(metadata, nil)
330331
mDB.EXPECT().GetWorkspaceAgentDevcontainersByAgentID(gomock.Any(), agent.ID).Return(devcontainers, nil)
331332
mDB.EXPECT().GetWorkspaceByID(gomock.Any(), workspace.ID).Return(workspace, nil)
332-
mDB.EXPECT().GetUserByID(gomock.Any(), workspace.OwnerID).Return(owner, nil)
333333

334334
got, err := api.GetManifest(context.Background(), &agentproto.GetManifestRequest{})
335335
require.NoError(t, err)
@@ -396,7 +396,6 @@ func TestGetManifest(t *testing.T) {
396396
}).Return(metadata, nil)
397397
mDB.EXPECT().GetWorkspaceAgentDevcontainersByAgentID(gomock.Any(), agent.ID).Return(devcontainers, nil)
398398
mDB.EXPECT().GetWorkspaceByID(gomock.Any(), workspace.ID).Return(workspace, nil)
399-
mDB.EXPECT().GetUserByID(gomock.Any(), workspace.OwnerID).Return(owner, nil)
400399

401400
got, err := api.GetManifest(context.Background(), &agentproto.GetManifestRequest{})
402401
require.NoError(t, err)

coderd/workspaceagents_test.go

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -437,25 +437,55 @@ func TestWorkspaceAgentConnectRPC(t *testing.T) {
437437
t.Run("Connect", func(t *testing.T) {
438438
t.Parallel()
439439

440-
client, db := coderdtest.NewWithDatabase(t, nil)
441-
user := coderdtest.CreateFirstUser(t, client)
442-
r := dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{
443-
OrganizationID: user.OrganizationID,
444-
OwnerID: user.UserID,
445-
}).WithAgent().Do()
446-
_ = agenttest.New(t, client.URL, r.AgentToken)
447-
resources := coderdtest.AwaitWorkspaceAgents(t, client, r.Workspace.ID)
440+
for _, tc := range []struct {
441+
name string
442+
apiKeyScope rbac.ScopeName
443+
}{
444+
{
445+
name: "empty (backwards compat)",
446+
apiKeyScope: "",
447+
},
448+
{
449+
name: "all",
450+
apiKeyScope: rbac.ScopeAll,
451+
},
452+
{
453+
name: "no_user_data",
454+
apiKeyScope: rbac.ScopeNoUserData,
455+
},
456+
{
457+
name: "application_connect",
458+
apiKeyScope: rbac.ScopeApplicationConnect,
459+
},
460+
} {
461+
t.Run(tc.name, func(t *testing.T) {
462+
client, db := coderdtest.NewWithDatabase(t, nil)
463+
user := coderdtest.CreateFirstUser(t, client)
464+
r := dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{
465+
OrganizationID: user.OrganizationID,
466+
OwnerID: user.UserID,
467+
}).WithAgent(func(agents []*proto.Agent) []*proto.Agent {
468+
for _, agent := range agents {
469+
agent.ApiKeyScope = string(tc.apiKeyScope)
470+
}
448471

449-
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
450-
defer cancel()
472+
return agents
473+
}).Do()
474+
_ = agenttest.New(t, client.URL, r.AgentToken)
475+
resources := coderdtest.NewWorkspaceAgentWaiter(t, client, r.Workspace.ID).AgentNames([]string{}).Wait()
451476

452-
conn, err := workspacesdk.New(client).
453-
DialAgent(ctx, resources[0].Agents[0].ID, nil)
454-
require.NoError(t, err)
455-
defer func() {
456-
_ = conn.Close()
457-
}()
458-
conn.AwaitReachable(ctx)
477+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
478+
defer cancel()
479+
480+
conn, err := workspacesdk.New(client).
481+
DialAgent(ctx, resources[0].Agents[0].ID, nil)
482+
require.NoError(t, err)
483+
defer func() {
484+
_ = conn.Close()
485+
}()
486+
conn.AwaitReachable(ctx)
487+
})
488+
}
459489
})
460490

461491
t.Run("FailNonLatestBuild", func(t *testing.T) {

coderd/workspaceagentsrpc.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,8 @@ func (api *API) workspaceAgentRPC(rw http.ResponseWriter, r *http.Request) {
7676
return
7777
}
7878

79-
owner, err := api.Database.GetUserByID(ctx, workspace.OwnerID)
80-
if err != nil {
81-
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
82-
Message: "Internal error fetching user.",
83-
Detail: err.Error(),
84-
})
85-
return
86-
}
87-
8879
logger = logger.With(
89-
slog.F("owner", owner.Username),
80+
slog.F("owner", workspace.OwnerUsername),
9081
slog.F("workspace_name", workspace.Name),
9182
slog.F("agent_name", workspaceAgent.Name),
9283
)
@@ -170,7 +161,7 @@ func (api *API) workspaceAgentRPC(rw http.ResponseWriter, r *http.Request) {
170161
})
171162

172163
streamID := tailnet.StreamID{
173-
Name: fmt.Sprintf("%s-%s-%s", owner.Username, workspace.Name, workspaceAgent.Name),
164+
Name: fmt.Sprintf("%s-%s-%s", workspace.OwnerUsername, workspace.Name, workspaceAgent.Name),
174165
ID: workspaceAgent.ID,
175166
Auth: tailnet.AgentCoordinateeAuth{ID: workspaceAgent.ID},
176167
}

0 commit comments

Comments
 (0)