Skip to content

Commit 04bd52f

Browse files
fix(console): address review on the VMDisk upload panel
Five review findings on the image-upload panel, all of them cases where the UI drew a conclusion the underlying data did not support. The placeholder warning read CDIConfig.status.uploadProxyURL raw while the command builder trimmed it, so a whitespace-only URL suppressed the warning and still produced a placeholder command. Both sides now read the URL through one usableProxyURL() predicate so they cannot drift apart again. useSelfSubjectAccessReview absorbs errors as allowed=false, which is fail-closed-to-hide. The panel inverted that into fail-closed-to-accuse and told a fully authorized admin their RBAC forbids the upload whenever the access check itself failed. The banner is now suppressed on an errored SSAR; the command is shown either way. navigator.clipboard is undefined on an insecure origin, so the optional chain resolved to undefined and the copy button flashed a checkmark having copied nothing. It is now disabled without a clipboard, matching the pattern already used in SecretsTab, and the command stays selectable in the <pre>. failureMessage used ??, so a Running condition carrying an empty message shadowed a meaningful Bound one and a failed disk rendered with no reason at all. The DiskUploadPanel fixture resolved the DataVolume name through the singular fallback, a path production never takes: the shipped ApplicationDefinition carries singular "vmdisk" and release.prefix "vm-disk-", so production would have computed vmdisk-demo, 404'd on the GET and rendered nothing behind a green test. Assisted-By: Claude <[email protected]> Signed-off-by: Myasnikov Daniil <[email protected]>
1 parent fec41e0 commit 04bd52f

4 files changed

Lines changed: 106 additions & 14 deletions

File tree

‎packages/system/dashboard/images/console/apps/console/src/lib/vm-disk-upload.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, it, expect } from "vitest"
22
import {
33
isUploadSource,
44
uploadState,
5+
usableProxyURL,
56
virtctlUploadCommand,
67
UPLOAD_PROXY_URL_PLACEHOLDER,
78
type DataVolume,
@@ -89,6 +90,19 @@ describe("uploadState", () => {
8990
expect(state.message).toBe("no capacity")
9091
})
9192

93+
it("falls back to the Bound condition when Running carries an empty message", () => {
94+
const state = uploadState(
95+
dv({ upload: {} }, {
96+
phase: "Failed",
97+
conditions: [
98+
{ type: "Bound", message: "no capacity" },
99+
{ type: "Running", message: "" },
100+
],
101+
}),
102+
)
103+
expect(state.message).toBe("no capacity")
104+
})
105+
92106
it("carries progress through at UploadReady", () => {
93107
expect(
94108
uploadState(dv({ upload: {} }, { phase: "UploadReady", progress: "42.0%" })).progress,
@@ -108,6 +122,17 @@ describe("uploadState", () => {
108122
})
109123
})
110124

125+
describe("usableProxyURL", () => {
126+
it("treats a blank or whitespace-only URL as no URL at all", () => {
127+
expect(usableProxyURL(undefined)).toBeUndefined()
128+
expect(usableProxyURL("")).toBeUndefined()
129+
expect(usableProxyURL(" ")).toBeUndefined()
130+
expect(usableProxyURL(" https://cdi-uploadproxy.example.org ")).toBe(
131+
"https://cdi-uploadproxy.example.org",
132+
)
133+
})
134+
})
135+
111136
describe("virtctlUploadCommand", () => {
112137
it("reuses the chart-created DataVolume and skips certificate verification", () => {
113138
const cmd = virtctlUploadCommand({

‎packages/system/dashboard/images/console/apps/console/src/lib/vm-disk-upload.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ export function isUploadSource(dv: DataVolume | undefined): boolean {
6161
function failureMessage(dv: DataVolume): string | undefined {
6262
const running = dv.status?.conditions?.find((c) => c.type === "Running")
6363
const bound = dv.status?.conditions?.find((c) => c.type === "Bound")
64-
return running?.message ?? bound?.message
64+
// `||`, not `??`: an empty Running message must not shadow the Bound one.
65+
return running?.message || bound?.message
6566
}
6667

6768
export function uploadState(dv: DataVolume | undefined): UploadState {
@@ -87,14 +88,24 @@ export interface UploadCommandOptions {
8788

8889
export const UPLOAD_PROXY_URL_PLACEHOLDER = "https://cdi-uploadproxy.<your-cozystack-domain>"
8990

91+
/**
92+
* A whitespace-only `CDIConfig.status.uploadProxyURL` is as unusable as an
93+
* absent one. The command builder and the panel's placeholder warning both
94+
* read the URL through this, so they cannot disagree over which values need
95+
* the placeholder.
96+
*/
97+
export function usableProxyURL(uploadProxyURL: string | undefined): string | undefined {
98+
return uploadProxyURL?.trim() || undefined
99+
}
100+
90101
/**
91102
* The disk's DataVolume is created by the vm-disk Helm release, so the upload
92103
* has to reuse it (`--no-create`); `--insecure` is required because Cozystack
93104
* publishes cdi-uploadproxy through TLS passthrough, leaving CDI's internal
94105
* self-signed certificate on the wire.
95106
*/
96107
export function virtctlUploadCommand(opts: UploadCommandOptions): string {
97-
const proxy = opts.uploadProxyURL?.trim() || UPLOAD_PROXY_URL_PLACEHOLDER
108+
const proxy = usableProxyURL(opts.uploadProxyURL) ?? UPLOAD_PROXY_URL_PLACEHOLDER
98109
return [
99110
"virtctl image-upload dv",
100111
opts.name,

‎packages/system/dashboard/images/console/apps/console/src/routes/detail/DiskUploadPanel.test.tsx‎

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect, vi, afterEach } from "vitest"
2-
import { screen, waitFor, cleanup } from "@testing-library/react"
2+
import { screen, waitFor, cleanup, fireEvent } from "@testing-library/react"
33
import { K8sClient } from "@cozystack/k8s-client"
44
import { renderWithK8sProvider } from "../../test-utils/render.tsx"
55
import { DiskUploadPanel } from "./DiskUploadPanel.tsx"
@@ -13,9 +13,10 @@ const ad: ApplicationDefinition = {
1313
application: {
1414
kind: "VMDisk",
1515
plural: "vmdisks",
16-
singular: "vm-disk",
16+
singular: "vmdisk",
1717
openAPISchema: "{}",
1818
},
19+
release: { prefix: "vm-disk-" },
1920
},
2021
}
2122

@@ -31,6 +32,7 @@ interface ClusterFixture {
3132
dvError?: Error
3233
uploadProxyURL?: string
3334
canUpload?: boolean
35+
ssarError?: Error
3436
}
3537

3638
function makeClient(fixture: ClusterFixture) {
@@ -57,11 +59,14 @@ function makeClient(fixture: ClusterFixture) {
5759
}
5860
},
5961
)
60-
vi.spyOn(client, "create").mockResolvedValue({
61-
apiVersion: "authorization.k8s.io/v1",
62-
kind: "SelfSubjectAccessReview",
63-
metadata: { name: "" },
64-
status: { allowed: fixture.canUpload ?? true },
62+
vi.spyOn(client, "create").mockImplementation(async () => {
63+
if (fixture.ssarError) throw fixture.ssarError
64+
return {
65+
apiVersion: "authorization.k8s.io/v1",
66+
kind: "SelfSubjectAccessReview",
67+
metadata: { name: "" },
68+
status: { allowed: fixture.canUpload ?? true },
69+
}
6570
})
6671
vi.spyOn(client, "watch").mockReturnValue(() => {})
6772
return client
@@ -70,6 +75,8 @@ function makeClient(fixture: ClusterFixture) {
7075
afterEach(() => {
7176
cleanup()
7277
vi.restoreAllMocks()
78+
// jsdom exposes no clipboard; drop the one the copy test installs.
79+
Reflect.deleteProperty(navigator, "clipboard")
7380
})
7481

7582
describe("DiskUploadPanel visibility", () => {
@@ -125,6 +132,14 @@ describe("DiskUploadPanel states", () => {
125132
expect(await screen.findByText(/publishes no upload proxy URL/)).toBeInTheDocument()
126133
})
127134

135+
it("flags the placeholder when the published proxy URL is only whitespace", async () => {
136+
const client = makeClient({ uploadProxyURL: " " })
137+
renderWithK8sProvider(<DiskUploadPanel ad={ad} instance={instance} />, { client })
138+
expect(await screen.findByText(/publishes no upload proxy URL/)).toBeInTheDocument()
139+
const cmd = await screen.findByText(/virtctl image-upload dv vm-disk-demo/)
140+
expect(cmd.textContent).toContain("cdi-uploadproxy.<your-cozystack-domain>")
141+
})
142+
128143
it("withholds the command before the upload target exists", async () => {
129144
const client = makeClient({ phase: "Pending" })
130145
renderWithK8sProvider(<DiskUploadPanel ad={ad} instance={instance} />, { client })
@@ -169,6 +184,14 @@ describe("DiskUploadPanel RBAC warning", () => {
169184
)
170185
})
171186

187+
it("stays quiet when the access check itself failed", async () => {
188+
const client = makeClient({ ssarError: new Error("SSAR unavailable") })
189+
renderWithK8sProvider(<DiskUploadPanel ad={ad} instance={instance} />, { client })
190+
expect(await screen.findByText(/virtctl image-upload/)).toBeInTheDocument()
191+
await waitFor(() => expect(client.create).toHaveBeenCalled())
192+
await expect(screen.findByText(/will fail with Forbidden/)).rejects.toThrow()
193+
})
194+
172195
it("stays quiet when the user is allowed to upload", async () => {
173196
const client = makeClient({ canUpload: true })
174197
renderWithK8sProvider(<DiskUploadPanel ad={ad} instance={instance} />, { client })
@@ -177,3 +200,24 @@ describe("DiskUploadPanel RBAC warning", () => {
177200
expect(screen.queryByText(/will fail with Forbidden/)).not.toBeInTheDocument()
178201
})
179202
})
203+
204+
describe("DiskUploadPanel copy button", () => {
205+
it("does not offer a copy the browser cannot perform", async () => {
206+
const client = makeClient({})
207+
renderWithK8sProvider(<DiskUploadPanel ad={ad} instance={instance} />, { client })
208+
expect(await screen.findByLabelText("Copy command")).toBeDisabled()
209+
})
210+
211+
it("copies the command where a clipboard exists", async () => {
212+
const writeText = vi.fn().mockResolvedValue(undefined)
213+
Object.defineProperty(navigator, "clipboard", { value: { writeText }, configurable: true })
214+
const client = makeClient({})
215+
renderWithK8sProvider(<DiskUploadPanel ad={ad} instance={instance} />, { client })
216+
fireEvent.click(await screen.findByLabelText("Copy command"))
217+
await waitFor(() =>
218+
expect(writeText).toHaveBeenCalledWith(
219+
expect.stringContaining("virtctl image-upload dv vm-disk-demo"),
220+
),
221+
)
222+
})
223+
})

‎packages/system/dashboard/images/console/apps/console/src/routes/detail/DiskUploadPanel.tsx‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { releasePrefix } from "../../lib/app-definitions.ts"
77
import {
88
isUploadSource,
99
uploadState,
10+
usableProxyURL,
1011
virtctlUploadCommand,
1112
type CDIConfig,
1213
type DataVolume,
@@ -35,9 +36,12 @@ const STAGE_TONE = {
3536

3637
function CopyableCommand({ command }: { command: string }) {
3738
const [copied, setCopied] = useState(false)
39+
// navigator.clipboard is absent on insecure origins; the <pre> stays selectable.
40+
const canCopy = !!navigator.clipboard
3841
const copy = async () => {
42+
if (!canCopy) return
3943
try {
40-
await navigator.clipboard?.writeText(command)
44+
await navigator.clipboard.writeText(command)
4145
setCopied(true)
4246
setTimeout(() => setCopied(false), 1500)
4347
} catch {
@@ -49,7 +53,13 @@ function CopyableCommand({ command }: { command: string }) {
4953
<pre className="flex-1 overflow-x-auto rounded-md bg-slate-900 px-3 py-2 text-xs leading-relaxed text-slate-100">
5054
{command}
5155
</pre>
52-
<Button variant="outline" size="sm" onClick={copy} aria-label="Copy command">
56+
<Button
57+
variant="outline"
58+
size="sm"
59+
onClick={copy}
60+
disabled={!canCopy}
61+
aria-label="Copy command"
62+
>
5363
{copied ? <Check className="size-3.5" /> : <Copy className="size-3.5" />}
5464
</Button>
5565
</div>
@@ -116,10 +126,11 @@ export function DiskUploadPanel({
116126
if (!isUploadSource(dv)) return null
117127

118128
const state = uploadState(dv)
129+
const proxyURL = usableProxyURL(cdiConfig.data?.status?.uploadProxyURL)
119130
const command = virtctlUploadCommand({
120131
name: dvName,
121132
namespace: ns,
122-
uploadProxyURL: cdiConfig.data?.status?.uploadProxyURL,
133+
uploadProxyURL: proxyURL,
123134
})
124135
const showCommand = state.stage === "awaiting-upload" || state.stage === "failed"
125136

@@ -177,14 +188,15 @@ export function DiskUploadPanel({
177188
at your local image and run:
178189
</p>
179190
<CopyableCommand command={command} />
180-
{!cdiConfig.data?.status?.uploadProxyURL && (
191+
{!proxyURL && (
181192
<p className="text-xs text-slate-500">
182193
This cluster publishes no upload proxy URL, so the address above is a
183194
placeholder. Ask your platform administrator for the{" "}
184195
<span className="font-mono">cdi-uploadproxy</span> hostname.
185196
</p>
186197
)}
187-
{!canUpload.isLoading && !canUpload.allowed && (
198+
{/* An errored SSAR surfaces as allowed=false, which is not a denial. */}
199+
{!canUpload.isLoading && !canUpload.error && !canUpload.allowed && (
188200
<p className="rounded-md border border-amber-200 bg-amber-50 p-3 text-xs text-amber-800">
189201
Your account cannot create{" "}
190202
<span className="font-mono">uploadtokenrequests</span> in this

0 commit comments

Comments
 (0)