mirror of
https://github.com/coder/coder.git
synced 2026-09-22 05:05:20 +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:
+1
-1
@@ -31,7 +31,7 @@ func (r *RootCmd) autoupdate() *serpent.Command {
|
||||
return xerrors.Errorf("validate policy: %w", err)
|
||||
}
|
||||
|
||||
workspace, err := namedWorkspace(inv.Context(), client, inv.Args[0])
|
||||
workspace, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
|
||||
if err != nil {
|
||||
return xerrors.Errorf("get workspace: %w", err)
|
||||
}
|
||||
|
||||
+2
-2
@@ -68,7 +68,7 @@ func (r *RootCmd) Create(opts CreateOptions) *serpent.Command {
|
||||
|
||||
workspaceOwner := codersdk.Me
|
||||
if len(inv.Args) >= 1 {
|
||||
workspaceOwner, workspaceName, err = splitNamedWorkspace(inv.Args[0])
|
||||
workspaceOwner, workspaceName, err = codersdk.SplitWorkspaceIdentifier(inv.Args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -104,7 +104,7 @@ func (r *RootCmd) Create(opts CreateOptions) *serpent.Command {
|
||||
|
||||
var sourceWorkspace codersdk.Workspace
|
||||
if copyParametersFrom != "" {
|
||||
sourceWorkspaceOwner, sourceWorkspaceName, err := splitNamedWorkspace(copyParametersFrom)
|
||||
sourceWorkspaceOwner, sourceWorkspaceName, err := codersdk.SplitWorkspaceIdentifier(copyParametersFrom)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+1
-1
@@ -35,7 +35,7 @@ func (r *RootCmd) deleteWorkspace() *serpent.Command {
|
||||
return err
|
||||
}
|
||||
|
||||
workspace, err := namedWorkspace(inv.Context(), client, inv.Args[0])
|
||||
workspace, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+1
-1
@@ -176,7 +176,7 @@ func TestDelete(t *testing.T) {
|
||||
go func() {
|
||||
defer close(doneChan)
|
||||
err := inv.Run()
|
||||
assert.ErrorContains(t, err, "invalid workspace name: \"a/b/c\"")
|
||||
assert.ErrorContains(t, err, "invalid workspace identifier: \"a/b/c\"")
|
||||
}()
|
||||
<-doneChan
|
||||
})
|
||||
|
||||
+1
-1
@@ -131,7 +131,7 @@ func (r *RootCmd) agentsCommand() *serpent.Command {
|
||||
|
||||
var workspaceID *uuid.UUID
|
||||
if workspaceFlag != "" {
|
||||
workspace, err := namedWorkspace(inv.Context(), client, workspaceFlag)
|
||||
workspace, err := client.ResolveWorkspace(inv.Context(), workspaceFlag)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("resolve workspace %q: %w", workspaceFlag, err)
|
||||
}
|
||||
|
||||
+2
-2
@@ -23,7 +23,7 @@ func (r *RootCmd) favorite() *serpent.Command {
|
||||
return err
|
||||
}
|
||||
|
||||
ws, err := namedWorkspace(inv.Context(), client, inv.Args[0])
|
||||
ws, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
|
||||
if err != nil {
|
||||
return xerrors.Errorf("get workspace: %w", err)
|
||||
}
|
||||
@@ -53,7 +53,7 @@ func (r *RootCmd) unfavorite() *serpent.Command {
|
||||
return err
|
||||
}
|
||||
|
||||
ws, err := namedWorkspace(inv.Context(), client, inv.Args[0])
|
||||
ws, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
|
||||
if err != nil {
|
||||
return xerrors.Errorf("get workspace: %w", err)
|
||||
}
|
||||
|
||||
+1
-1
@@ -52,7 +52,7 @@ func (r *RootCmd) logs() *serpent.Command {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ws, err := namedWorkspace(inv.Context(), client, inv.Args[0])
|
||||
ws, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
|
||||
if err != nil {
|
||||
return xerrors.Errorf("failed to get workspace: %w", err)
|
||||
}
|
||||
|
||||
+1
-1
@@ -26,7 +26,7 @@ func (r *RootCmd) rename() *serpent.Command {
|
||||
}
|
||||
appearanceConfig := initAppearance(inv.Context(), client)
|
||||
|
||||
workspace, err := namedWorkspace(inv.Context(), client, inv.Args[0])
|
||||
workspace, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
|
||||
if err != nil {
|
||||
return xerrors.Errorf("get workspace: %w", err)
|
||||
}
|
||||
|
||||
+1
-1
@@ -36,7 +36,7 @@ func (r *RootCmd) restart() *serpent.Command {
|
||||
ctx := inv.Context()
|
||||
out := inv.Stdout
|
||||
|
||||
workspace, err := namedWorkspace(inv.Context(), client, inv.Args[0])
|
||||
workspace, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
-31
@@ -27,7 +27,6 @@ import (
|
||||
"text/tabwriter"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/mattn/go-isatty"
|
||||
"github.com/mitchellh/go-wordwrap"
|
||||
"golang.org/x/mod/semver"
|
||||
@@ -1071,36 +1070,6 @@ func (o *OrganizationContext) Selected(inv *serpent.Invocation, client *codersdk
|
||||
return codersdk.Organization{}, xerrors.Errorf("Must select an organization with --org=<org_name>. Choose from: %s", strings.Join(validOrgs, ", "))
|
||||
}
|
||||
|
||||
func splitNamedWorkspace(identifier string) (owner string, workspaceName string, err error) {
|
||||
parts := strings.Split(identifier, "/")
|
||||
|
||||
switch len(parts) {
|
||||
case 1:
|
||||
owner = codersdk.Me
|
||||
workspaceName = parts[0]
|
||||
case 2:
|
||||
owner = parts[0]
|
||||
workspaceName = parts[1]
|
||||
default:
|
||||
return "", "", xerrors.Errorf("invalid workspace name: %q", identifier)
|
||||
}
|
||||
return owner, workspaceName, nil
|
||||
}
|
||||
|
||||
// namedWorkspace fetches and returns a workspace by an identifier, which may be either
|
||||
// a bare name (for a workspace owned by the current user) or a "user/workspace" combination,
|
||||
// where user is either a username or UUID.
|
||||
func namedWorkspace(ctx context.Context, client *codersdk.Client, identifier string) (codersdk.Workspace, error) {
|
||||
if uid, err := uuid.Parse(identifier); err == nil {
|
||||
return client.Workspace(ctx, uid)
|
||||
}
|
||||
owner, name, err := splitNamedWorkspace(identifier)
|
||||
if err != nil {
|
||||
return codersdk.Workspace{}, err
|
||||
}
|
||||
return client.WorkspaceByOwnerAndName(ctx, owner, name, codersdk.WorkspaceOptions{})
|
||||
}
|
||||
|
||||
func initAppearance(ctx context.Context, client *codersdk.Client) codersdk.AppearanceConfig {
|
||||
// best effort
|
||||
cfg, _ := client.Appearance(ctx)
|
||||
|
||||
+7
-7
@@ -109,7 +109,7 @@ func (r *RootCmd) scheduleShow() *serpent.Command {
|
||||
if len(inv.Args) == 1 {
|
||||
// If the argument contains a slash, we assume it's a full owner/name reference
|
||||
if strings.Contains(inv.Args[0], "/") {
|
||||
_, workspaceName, err := splitNamedWorkspace(inv.Args[0])
|
||||
_, workspaceName, err := codersdk.SplitWorkspaceIdentifier(inv.Args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -161,7 +161,7 @@ func (r *RootCmd) scheduleStart() *serpent.Command {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
workspace, err := namedWorkspace(inv.Context(), client, inv.Args[0])
|
||||
workspace, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -206,7 +206,7 @@ func (r *RootCmd) scheduleStart() *serpent.Command {
|
||||
return err
|
||||
}
|
||||
|
||||
updated, err := namedWorkspace(inv.Context(), client, inv.Args[0])
|
||||
updated, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -234,7 +234,7 @@ func (r *RootCmd) scheduleStop() *serpent.Command {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
workspace, err := namedWorkspace(inv.Context(), client, inv.Args[0])
|
||||
workspace, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -261,7 +261,7 @@ func (r *RootCmd) scheduleStop() *serpent.Command {
|
||||
return err
|
||||
}
|
||||
|
||||
updated, err := namedWorkspace(inv.Context(), client, inv.Args[0])
|
||||
updated, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -293,7 +293,7 @@ func (r *RootCmd) scheduleExtend() *serpent.Command {
|
||||
return err
|
||||
}
|
||||
|
||||
workspace, err := namedWorkspace(inv.Context(), client, inv.Args[0])
|
||||
workspace, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
|
||||
if err != nil {
|
||||
return xerrors.Errorf("get workspace: %w", err)
|
||||
}
|
||||
@@ -325,7 +325,7 @@ func (r *RootCmd) scheduleExtend() *serpent.Command {
|
||||
return err
|
||||
}
|
||||
|
||||
updated, err := namedWorkspace(inv.Context(), client, inv.Args[0])
|
||||
updated, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+3
-3
@@ -48,7 +48,7 @@ func (r *RootCmd) statusWorkspaceSharing() *serpent.Command {
|
||||
return err
|
||||
}
|
||||
|
||||
workspace, err := namedWorkspace(inv.Context(), client, inv.Args[0])
|
||||
workspace, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
|
||||
if err != nil {
|
||||
return xerrors.Errorf("unable to fetch Workspace %s: %w", inv.Args[0], err)
|
||||
}
|
||||
@@ -110,7 +110,7 @@ func (r *RootCmd) shareWorkspace() *serpent.Command {
|
||||
return xerrors.New("at least one user or group must be provided")
|
||||
}
|
||||
|
||||
workspace, err := namedWorkspace(inv.Context(), client, inv.Args[0])
|
||||
workspace, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
|
||||
if err != nil {
|
||||
return xerrors.Errorf("could not fetch the workspace %s: %w", inv.Args[0], err)
|
||||
}
|
||||
@@ -208,7 +208,7 @@ func (r *RootCmd) unshareWorkspace() *serpent.Command {
|
||||
return err
|
||||
}
|
||||
|
||||
workspace, err := namedWorkspace(inv.Context(), client, inv.Args[0])
|
||||
workspace, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
|
||||
if err != nil {
|
||||
return xerrors.Errorf("could not fetch the workspace %s: %w", inv.Args[0], err)
|
||||
}
|
||||
|
||||
+1
-1
@@ -41,7 +41,7 @@ func (r *RootCmd) show() *serpent.Command {
|
||||
if err != nil {
|
||||
return xerrors.Errorf("get server version: %w", err)
|
||||
}
|
||||
workspace, err := namedWorkspace(inv.Context(), client, inv.Args[0])
|
||||
workspace, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
|
||||
if err != nil {
|
||||
return xerrors.Errorf("get workspace: %w", err)
|
||||
}
|
||||
|
||||
@@ -65,6 +65,59 @@ func TestShow(t *testing.T) {
|
||||
}
|
||||
_ = testutil.TryReceive(ctx, t, doneChan)
|
||||
})
|
||||
|
||||
// Regression test: workspace names that are valid dashless UUIDs
|
||||
// (32 hex chars) should be looked up by name, not parsed as a
|
||||
// UUID and fetched by ID (which 404s).
|
||||
t.Run("WorkspaceWithUUIDLikeName", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
|
||||
owner := coderdtest.CreateFirstUser(t, client)
|
||||
member, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
|
||||
version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, completeWithAgent())
|
||||
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
|
||||
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID)
|
||||
|
||||
// This name is a valid 32-char hex string (dashless UUID).
|
||||
const wsName = "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6"
|
||||
workspace := coderdtest.CreateWorkspace(t, member, template.ID, func(cwr *codersdk.CreateWorkspaceRequest) {
|
||||
cwr.Name = wsName
|
||||
})
|
||||
build := coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID)
|
||||
|
||||
args := []string{
|
||||
"show",
|
||||
wsName,
|
||||
}
|
||||
inv, root := clitest.New(t, args...)
|
||||
clitest.SetupConfig(t, member, root)
|
||||
doneChan := make(chan struct{})
|
||||
pty := ptytest.New(t).Attach(inv)
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
go func() {
|
||||
defer close(doneChan)
|
||||
err := inv.WithContext(ctx).Run()
|
||||
assert.NoError(t, err)
|
||||
}()
|
||||
matches := []struct {
|
||||
match string
|
||||
write string
|
||||
}{
|
||||
{match: fmt.Sprintf("%s/%s", workspace.OwnerName, workspace.Name)},
|
||||
{match: fmt.Sprintf("(%s since ", build.Status)},
|
||||
{match: fmt.Sprintf("%s:%s", workspace.TemplateName, workspace.LatestBuild.TemplateVersionName)},
|
||||
{match: "compute.main"},
|
||||
{match: "smith (linux, i386)"},
|
||||
{match: "coder ssh " + workspace.Name},
|
||||
}
|
||||
for _, m := range matches {
|
||||
pty.ExpectMatchContext(ctx, m.match)
|
||||
if len(m.write) > 0 {
|
||||
pty.WriteLine(m.write)
|
||||
}
|
||||
}
|
||||
_ = testutil.TryReceive(ctx, t, doneChan)
|
||||
})
|
||||
}
|
||||
|
||||
func TestShowDevcontainers_Golden(t *testing.T) {
|
||||
|
||||
+2
-2
@@ -984,7 +984,7 @@ func GetWorkspaceAndAgent(ctx context.Context, inv *serpent.Invocation, client *
|
||||
err error
|
||||
)
|
||||
|
||||
workspace, err = namedWorkspace(ctx, client, workspaceParts[0])
|
||||
workspace, err = client.ResolveWorkspace(ctx, workspaceParts[0])
|
||||
if err != nil {
|
||||
return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, nil, err
|
||||
}
|
||||
@@ -1044,7 +1044,7 @@ func GetWorkspaceAndAgent(ctx context.Context, inv *serpent.Invocation, client *
|
||||
}
|
||||
|
||||
// Refresh workspace state so that `outdated`, `build`,`template_*` fields are up-to-date.
|
||||
workspace, err = namedWorkspace(ctx, client, workspaceParts[0])
|
||||
workspace, err = client.ResolveWorkspace(ctx, workspaceParts[0])
|
||||
if err != nil {
|
||||
return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, nil, err
|
||||
}
|
||||
|
||||
+2
-2
@@ -43,7 +43,7 @@ func (r *RootCmd) start() *serpent.Command {
|
||||
return err
|
||||
}
|
||||
|
||||
workspace, err := namedWorkspace(inv.Context(), client, inv.Args[0])
|
||||
workspace, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -97,7 +97,7 @@ func (r *RootCmd) start() *serpent.Command {
|
||||
}
|
||||
// Re-fetch workspace after stop completes so
|
||||
// startWorkspace sees the latest state.
|
||||
workspace, err = namedWorkspace(inv.Context(), client, inv.Args[0])
|
||||
workspace, err = client.ResolveWorkspace(inv.Context(), inv.Args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+4
-4
@@ -41,13 +41,13 @@ func (r *RootCmd) statePull() *serpent.Command {
|
||||
}
|
||||
var build codersdk.WorkspaceBuild
|
||||
if buildNumber == 0 {
|
||||
workspace, err := namedWorkspace(inv.Context(), client, inv.Args[0])
|
||||
workspace, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
build = workspace.LatestBuild
|
||||
} else {
|
||||
owner, workspace, err := splitNamedWorkspace(inv.Args[0])
|
||||
owner, workspace, err := codersdk.SplitWorkspaceIdentifier(inv.Args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -99,7 +99,7 @@ func (r *RootCmd) statePush() *serpent.Command {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
workspace, err := namedWorkspace(inv.Context(), client, inv.Args[0])
|
||||
workspace, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -107,7 +107,7 @@ func (r *RootCmd) statePush() *serpent.Command {
|
||||
if buildNumber == 0 {
|
||||
build = workspace.LatestBuild
|
||||
} else {
|
||||
owner, workspace, err := splitNamedWorkspace(inv.Args[0])
|
||||
owner, workspace, err := codersdk.SplitWorkspaceIdentifier(inv.Args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+1
-1
@@ -36,7 +36,7 @@ func (r *RootCmd) stop() *serpent.Command {
|
||||
return err
|
||||
}
|
||||
|
||||
workspace, err := namedWorkspace(inv.Context(), client, inv.Args[0])
|
||||
workspace, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+1
-1
@@ -185,7 +185,7 @@ func (r *RootCmd) supportBundle() *serpent.Command {
|
||||
cliLog.Warn(inv.Context(), "no workspace specified")
|
||||
cliui.Warn(inv.Stderr, "No workspace specified. This will result in incomplete information.")
|
||||
} else {
|
||||
ws, err := namedWorkspace(inv.Context(), client, inv.Args[0])
|
||||
ws, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
|
||||
if err != nil {
|
||||
return xerrors.Errorf("invalid workspace: %w", err)
|
||||
}
|
||||
|
||||
+1
-1
@@ -29,7 +29,7 @@ func (r *RootCmd) update() *serpent.Command {
|
||||
return err
|
||||
}
|
||||
|
||||
workspace, err := namedWorkspace(inv.Context(), client, inv.Args[0])
|
||||
workspace, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user