mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix: fall back to name lookup for UUID-shaped workspace names (#24340)
`namedWorkspace` in `cli/root.go` parsed workspace identifiers with `uuid.Parse` first and returned immediately on success, even when no workspace had that UUID as its actual ID. This caused 404 errors for any workspace whose name was a valid 32-char hex string (dashless UUID). - Add `codersdk.ResolveWorkspace`: tries UUID lookup first, falls back to name lookup on 404. `NameValid` guard skips the fallback for standard dashed UUIDs (36 chars > 32-char name limit). - Export `codersdk.SplitWorkspaceIdentifier`, replacing the duplicate `splitNamedWorkspace` in `cli/root.go` (uses `strings.Cut`). - Delete `namedWorkspace` from `cli/root.go`; all 28 call sites now use `client.ResolveWorkspace` directly. - Delete `namedWorkspace` and `splitNameAndOwner` from `codersdk/toolsdk/bash.go`; inline `client.ResolveWorkspace`. - Simplify `GetWorkspace` tool handler to a single `ResolveWorkspace` call. - Unit tests via httptest mock cover UUID, name, owner/name, UUID-like fallback, not-found, server error, transport error, and invalid identifier paths. - Integration tests in `cli/show_test.go` and `codersdk/toolsdk` for workspaces with UUID-like names. > Generated with Coder Agents
This commit is contained in:
@@ -190,7 +190,7 @@ func findWorkspaceAndAgent(ctx context.Context, client *codersdk.Client, workspa
|
||||
}
|
||||
|
||||
// Get workspace
|
||||
workspace, err := namedWorkspace(ctx, client, workspaceName)
|
||||
workspace, err := client.ResolveWorkspace(ctx, workspaceName)
|
||||
if err != nil {
|
||||
return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, err
|
||||
}
|
||||
@@ -274,37 +274,6 @@ func getWorkspaceAgent(workspace codersdk.Workspace, agentName string) (codersdk
|
||||
return codersdk.WorkspaceAgent{}, xerrors.Errorf("multiple agents found, please specify the agent name, available agents: %v", availableNames)
|
||||
}
|
||||
|
||||
func splitNameAndOwner(identifier string) (name string, owner string) {
|
||||
// Parse owner and name (workspace, task).
|
||||
parts := strings.SplitN(identifier, "/", 2)
|
||||
|
||||
if len(parts) == 2 {
|
||||
owner = parts[0]
|
||||
name = parts[1]
|
||||
} else {
|
||||
owner = "me"
|
||||
name = identifier
|
||||
}
|
||||
|
||||
return name, owner
|
||||
}
|
||||
|
||||
// namedWorkspace gets a workspace by owner/name or just name
|
||||
func namedWorkspace(ctx context.Context, client *codersdk.Client, identifier string) (codersdk.Workspace, error) {
|
||||
workspaceName, owner := splitNameAndOwner(identifier)
|
||||
|
||||
// Handle -- separator format (convert to / format)
|
||||
if strings.Contains(identifier, "--") && !strings.Contains(identifier, "/") {
|
||||
dashParts := strings.SplitN(identifier, "--", 2)
|
||||
if len(dashParts) == 2 {
|
||||
owner = dashParts[0]
|
||||
workspaceName = dashParts[1]
|
||||
}
|
||||
}
|
||||
|
||||
return client.WorkspaceByOwnerAndName(ctx, owner, workspaceName, codersdk.WorkspaceOptions{})
|
||||
}
|
||||
|
||||
// executeCommandWithTimeout executes a command with timeout support
|
||||
func executeCommandWithTimeout(ctx context.Context, session *gossh.Session, command string) ([]byte, error) {
|
||||
// Set up pipes to capture output
|
||||
|
||||
@@ -432,11 +432,7 @@ This returns more data than list_workspaces to reduce token usage.`,
|
||||
},
|
||||
MCPAnnotations: mcpReadOnlyAnnotations,
|
||||
Handler: func(ctx context.Context, deps Deps, args GetWorkspaceArgs) (codersdk.Workspace, error) {
|
||||
wsID, err := uuid.Parse(args.WorkspaceID)
|
||||
if err != nil {
|
||||
return namedWorkspace(ctx, deps.coderClient, NormalizeWorkspaceInput(args.WorkspaceID))
|
||||
}
|
||||
return deps.coderClient.Workspace(ctx, wsID)
|
||||
return deps.coderClient.ResolveWorkspace(ctx, NormalizeWorkspaceInput(args.WorkspaceID))
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -45,6 +45,14 @@ import (
|
||||
// nolint:gocritic // This is in a test package and does not end up in the build
|
||||
func setupWorkspaceForAgent(t *testing.T, opts *coderdtest.Options) (*codersdk.Client, database.WorkspaceTable, string) {
|
||||
t.Helper()
|
||||
return setupWorkspaceForAgentWithName(t, opts, "myworkspace")
|
||||
}
|
||||
|
||||
// setupWorkspaceForAgentWithName creates a workspace setup exactly like main
|
||||
// SSH tests, but with a caller-provided workspace name.
|
||||
// nolint:gocritic // This is in a test package and does not end up in the build
|
||||
func setupWorkspaceForAgentWithName(t *testing.T, opts *coderdtest.Options, workspaceName string) (*codersdk.Client, database.WorkspaceTable, string) {
|
||||
t.Helper()
|
||||
|
||||
client, store := coderdtest.NewWithDatabase(t, opts)
|
||||
client.SetLogger(testutil.Logger(t).Named("client"))
|
||||
@@ -54,7 +62,7 @@ func setupWorkspaceForAgent(t *testing.T, opts *coderdtest.Options) (*codersdk.C
|
||||
})
|
||||
// nolint:gocritic // This is in a test package and does not end up in the build
|
||||
r := dbfake.WorkspaceBuild(t, store, database.WorkspaceTable{
|
||||
Name: "myworkspace",
|
||||
Name: workspaceName,
|
||||
OrganizationID: first.OrganizationID,
|
||||
OwnerID: user.ID,
|
||||
}).WithAgent().Do()
|
||||
@@ -241,6 +249,31 @@ func TestTools(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("GetWorkspace_ByUUIDLikeName", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// Regression test: a workspace whose name is a valid dashless
|
||||
// UUID should resolve correctly. Previously, the handler would
|
||||
// parse the name as a UUID, get a 404 from the ID-based lookup,
|
||||
// and never fall back to name-based lookup.
|
||||
const uuidLikeName = "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6"
|
||||
// nolint:gocritic // This is in a test package and does not end up in the build
|
||||
uuidWorkspace := dbfake.WorkspaceBuild(t, store, database.WorkspaceTable{
|
||||
OrganizationID: owner.OrganizationID,
|
||||
OwnerID: member.ID,
|
||||
Name: uuidLikeName,
|
||||
}).Do()
|
||||
|
||||
tb, err := toolsdk.NewDeps(memberClient)
|
||||
require.NoError(t, err)
|
||||
|
||||
result, err := testTool(t, toolsdk.GetWorkspace, tb, toolsdk.GetWorkspaceArgs{
|
||||
WorkspaceID: uuidLikeName,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, uuidWorkspace.Workspace.ID, result.ID)
|
||||
})
|
||||
|
||||
t.Run("ListTemplates", func(t *testing.T) {
|
||||
tb, err := toolsdk.NewDeps(memberClient)
|
||||
require.NoError(t, err)
|
||||
@@ -566,6 +599,24 @@ func TestTools(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 0, result.ExitCode)
|
||||
require.Equal(t, "owner format works", result.Output)
|
||||
|
||||
// Regression test: agent-backed tools should also work when the
|
||||
// workspace name is a valid dashless UUID.
|
||||
const uuidLikeName = "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6"
|
||||
uuidClient, uuidWorkspace, uuidAgentToken := setupWorkspaceForAgentWithName(t, nil, uuidLikeName)
|
||||
_ = agenttest.New(t, uuidClient.URL, uuidAgentToken)
|
||||
coderdtest.NewWorkspaceAgentWaiter(t, uuidClient, uuidWorkspace.ID).Wait()
|
||||
|
||||
uuidTB, err := toolsdk.NewDeps(uuidClient)
|
||||
require.NoError(t, err)
|
||||
|
||||
result, err = testTool(t, toolsdk.WorkspaceBash, uuidTB, toolsdk.WorkspaceBashArgs{
|
||||
Workspace: uuidWorkspace.Name,
|
||||
Command: "echo 'uuid-like name works'",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 0, result.ExitCode)
|
||||
require.Equal(t, "uuid-like name works", result.Output)
|
||||
})
|
||||
|
||||
t.Run("WorkspaceLS", func(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user