|
| 1 | +import { opens, receives } from "./support/streams"; |
| 2 | +import { |
| 3 | + ssh, |
| 4 | + startOutdatedWithCli, |
| 5 | + updateInOneBuild, |
| 6 | +} from "./support/workspaceActions"; |
| 7 | +import { currentAgentId } from "./support/workspaces"; |
| 8 | + |
| 9 | +import type { CoderApi } from "@/api/coderApi"; |
| 10 | + |
| 11 | +import type { Probe } from "./support/deployment"; |
| 12 | + |
| 13 | +type ApiMethod = keyof CoderApi; |
| 14 | + |
| 15 | +/** |
| 16 | + * One probe per Coder API method the extension calls, which |
| 17 | + * `oauthScopeProbes.test.ts` checks. Mutating probes leave `own` running with |
| 18 | + * no build in progress, so probes can run in any order. |
| 19 | + */ |
| 20 | +export const apiProbes = { |
| 21 | + getBuildInfo: ({ scoped }) => scoped.getBuildInfo(), |
| 22 | + getAuthenticatedUser: ({ scoped }) => scoped.getAuthenticatedUser(), |
| 23 | + getAppearance: ({ scoped }) => scoped.getAppearance(), |
| 24 | + getDeploymentSSHConfig: ({ scoped }) => scoped.getDeploymentSSHConfig(), |
| 25 | + getWorkspaces: async ({ scoped }) => |
| 26 | + assertNonEmpty((await scoped.getWorkspaces({ q: "owner:me" })).workspaces), |
| 27 | + "getWorkspaces (shared)": async ({ scoped, own }) => |
| 28 | + assertNonEmpty( |
| 29 | + (await scoped.getWorkspaces({ q: `shared_with_user:${own.owner_id}` })) |
| 30 | + .workspaces, |
| 31 | + ), |
| 32 | + getWorkspace: ({ scoped, own }) => scoped.getWorkspace(own.id), |
| 33 | + getWorkspaceByOwnerAndName: ({ scoped, own }) => |
| 34 | + scoped.getWorkspaceByOwnerAndName(own.owner_name, own.name), |
| 35 | + "getWorkspaceByOwnerAndName (shared)": ({ scoped, shared }) => |
| 36 | + scoped.getWorkspaceByOwnerAndName(shared.owner_name, shared.name), |
| 37 | + getWorkspaceBuildByNumber: ({ scoped, own }) => |
| 38 | + scoped.getWorkspaceBuildByNumber(own.owner_name, own.name, 1), |
| 39 | + getWorkspaceBuildParameters: async ({ scoped, own }) => |
| 40 | + assertNonEmpty( |
| 41 | + await scoped.getWorkspaceBuildParameters(own.latest_build.id), |
| 42 | + ), |
| 43 | + // No `assertNonEmpty`: the test agent never connects, so it has no logs. |
| 44 | + getWorkspaceAgentLogs: async ({ scoped, admin, own }) => |
| 45 | + scoped.getWorkspaceAgentLogs(await currentAgentId(admin, own)), |
| 46 | + getTemplate: ({ scoped, own }) => scoped.getTemplate(own.template_id), |
| 47 | + getTemplateVersion: ({ scoped, own }) => |
| 48 | + scoped.getTemplateVersion(own.template_active_version_id), |
| 49 | + getTemplateVersionResources: async ({ scoped, own }) => |
| 50 | + assertNonEmpty( |
| 51 | + await scoped.getTemplateVersionResources(own.template_active_version_id), |
| 52 | + ), |
| 53 | + getTemplateVersionRichParameters: async ({ scoped, own }) => |
| 54 | + assertNonEmpty( |
| 55 | + await scoped.getTemplateVersionRichParameters( |
| 56 | + own.template_active_version_id, |
| 57 | + ), |
| 58 | + ), |
| 59 | + getTemplateVersionPresets: async ({ scoped, own }) => |
| 60 | + assertNonEmpty( |
| 61 | + await scoped.getTemplateVersionPresets(own.template_active_version_id), |
| 62 | + ), |
| 63 | + watchWorkspace: receives((api, { own }) => api.watchWorkspace(own)), |
| 64 | + watchAgentMetadata: receives(async (api, { admin, own }) => |
| 65 | + api.watchAgentMetadata(await currentAgentId(admin, own)), |
| 66 | + ), |
| 67 | + watchBuildLogsByBuildId: opens((api, { own }) => |
| 68 | + api.watchBuildLogsByBuildId(own.latest_build.id, []), |
| 69 | + ), |
| 70 | + watchWorkspaceAgentLogs: opens(async (api, { admin, own }) => |
| 71 | + api.watchWorkspaceAgentLogs(await currentAgentId(admin, own), []), |
| 72 | + ), |
| 73 | + watchInboxNotifications: opens((api) => api.watchInboxNotifications([], [])), |
| 74 | + "postWorkspaceBuild (update)": updateInOneBuild, |
| 75 | +} satisfies Partial<Record<ApiMethod | `${ApiMethod} (${string})`, Probe>>; |
| 76 | + |
| 77 | +/** |
| 78 | + * CLI requests the source scan can't find. Unprobed: `ping` and `speedtest` |
| 79 | + * dial like `ssh`; `netcheck` calls `/workspaceagents/connection`, which has |
| 80 | + * no RBAC check; `support bundle` only reads what other probes cover. |
| 81 | + */ |
| 82 | +export const cliProbes: Record<string, Probe> = { |
| 83 | + "coder ssh": (d) => ssh(d, d.own), |
| 84 | + "coder ssh (shared)": (d) => ssh(d, d.shared), |
| 85 | + "coder start": startOutdatedWithCli, |
| 86 | +}; |
| 87 | + |
| 88 | +const TASKS_API = |
| 89 | + "Tasks API, only used on servers before 2.35, which ignore OAuth scopes"; |
| 90 | + |
| 91 | +/** Coder API methods the extension calls that need no probe, with the reason. */ |
| 92 | +export const unprobedMethods: Partial<Record<ApiMethod, string>> = { |
| 93 | + startWorkspace: TASKS_API, |
| 94 | + stopWorkspace: TASKS_API, |
| 95 | + createTask: TASKS_API, |
| 96 | + deleteTask: TASKS_API, |
| 97 | + getTask: TASKS_API, |
| 98 | + getTaskLogs: TASKS_API, |
| 99 | + getTasks: TASKS_API, |
| 100 | + getTemplates: TASKS_API, |
| 101 | + pauseTask: TASKS_API, |
| 102 | + resumeTask: TASKS_API, |
| 103 | + sendTaskInput: TASKS_API, |
| 104 | +}; |
| 105 | + |
| 106 | +function assertNonEmpty(items: readonly unknown[] | null): void { |
| 107 | + if (!items?.length) { |
| 108 | + throw new Error( |
| 109 | + "Expected at least one item; the server omits rows the token cannot read, so an empty list usually means a missing scope", |
| 110 | + ); |
| 111 | + } |
| 112 | +} |
0 commit comments