From a10cfcadb6099aee93b05cf7ea425cddeddbeb09 Mon Sep 17 00:00:00 2001 From: Sebastian Beyvers Date: Tue, 3 Nov 2026 14:59:48 +0100 Subject: [PATCH 1/3] feat: add the local folder storage kind --- .../groups/StorageBackendDialog.test.ts | 118 +++++++++++++++++- .../groups/StorageBackendDialog.vue | 73 ++++++++++- src/composables/useImportRoots.test.ts | 100 +++++++++++++++ src/composables/useImportRoots.ts | 49 ++++++++ src/lib/api/groupBackends.ts | 14 ++- src/lib/linkedFolders.ts | 3 +- src/lib/storage.test.ts | 30 +++++ src/lib/storage.ts | 23 +++- 8 files changed, 400 insertions(+), 10 deletions(-) create mode 100644 src/composables/useImportRoots.test.ts create mode 100644 src/composables/useImportRoots.ts diff --git a/src/components/groups/StorageBackendDialog.test.ts b/src/components/groups/StorageBackendDialog.test.ts index ea08b395a..4613ac6ea 100644 --- a/src/components/groups/StorageBackendDialog.test.ts +++ b/src/components/groups/StorageBackendDialog.test.ts @@ -14,7 +14,8 @@ import { } from '@/test/clientRender' import * as Storage from '@/lib/storage' import * as Utils from '@/lib/utils' -import type { GroupBackendRequest, GroupBackendResponse } from '@/lib/api' +import type { GroupBackendRequest, GroupBackendResponse, ImportRoot } from '@/lib/api' +import type { RootsState } from '@/composables/useImportRoots' const saved: GroupBackendResponse = { backend_id: 'b1', @@ -28,6 +29,12 @@ const replaceGroupBackend = vi.fn(async (_group: string, _id: string, _body: Gro const disableGroupBackend = vi.fn(async (_group: string, _id: string) => {}) const enableGroupBackend = vi.fn(async (_group: string, _id: string) => saved) const saving = ref(false) +const folders = { + roots: ref([]), + state: ref('ready'), + error: ref(null), + load: vi.fn(), +} const Passthrough = defineComponent((_, { slots }) => () => h('div', slots.default?.())) const Empty = defineComponent(() => () => null) @@ -69,6 +76,7 @@ const StorageBackendDialog = compileClientComponent(new URL('./StorageBackendDia '@/lib/storage': Storage, '@/lib/utils': Utils, '@/lib/connectivity': { OFFLINE_WRITE_HINT: 'offline', useConnectivity: () => ({ writesDisabled: ref(false) }) }, + '@/composables/useImportRoots': { useImportRoots: () => folders }, '@/composables/useAruna': { useAruna: () => ({ createGroupBackend, replaceGroupBackend, disableGroupBackend, enableGroupBackend, saving }), }, @@ -124,6 +132,10 @@ describe('Storage dialog connection', () => { replaceGroupBackend.mockClear() disableGroupBackend.mockClear() saving.value = false + folders.roots.value = [] + folders.state.value = 'ready' + folders.error.value = null + folders.load.mockClear() }) it('saves a setting without keys, so the stored keys stay', async () => { @@ -240,4 +252,108 @@ describe('Storage dialog connection', () => { await click(buttonWith(root, 'Remove')) expect(disableGroupBackend).toHaveBeenCalledWith('g1', 'b1') }) + + describe('local folder', () => { + const pick = async (root: Root) => typeValue(byLabel(root, 'Type'), 'fs') + const approved = [ + { name: 'projects', writable: false }, + { name: 'scratch', writable: true }, + ] + + it('has no credentials and sends the root and folder it was given', async () => { + folders.roots.value = approved + const { root } = await mount() + await pick(root) + expect(nodes(root).some((node) => node.tag === 'fieldset')).toBe(false) + expect(content(root)).not.toContain('Aruna checks the keys') + await typeValue(byId(root, '-name'), 'raw') + expect(save(root).props.disabled).toBe(true) + await typeValue(byLabel(root, 'Approved folder'), 'scratch') + await typeValue(byId(root, '-pub-folder'), 'runs/2026/') + await submit(root) + expect(createGroupBackend.mock.calls[0][1]).toEqual({ + name: 'raw', + kind: 'fs', + public_config: { root: 'scratch', folder: 'runs/2026/' }, + secret_config: {}, + }) + }) + + it('says whether the chosen folder is read-only or writable', async () => { + folders.roots.value = approved + const { root } = await mount() + await pick(root) + await typeValue(byLabel(root, 'Approved folder'), 'projects') + expect(content(root)).toContain('Read-only: Aruna can read files here but not write them.') + await typeValue(byLabel(root, 'Approved folder'), 'scratch') + expect(content(root)).toContain('Writable: Aruna can read and write files here.') + }) + + it('shows loading, failure with a retry, and the empty list', async () => { + folders.state.value = 'loading' + const { root } = await mount() + await pick(root) + expect(content(root)).toContain('Loading approved folders…') + folders.state.value = 'failed' + folders.error.value = 'node down' + await flush() + expect(content(root)).toContain('node down') + expect(content(root)).not.toContain('has not approved any folders') + await click(buttonWith(root, 'Try again')) + expect(folders.load).toHaveBeenCalledTimes(1) + folders.state.value = 'ready' + await flush() + expect(content(root)).toContain('The node operator has not approved any folders for this group.') + }) + + it('refuses a folder the node would refuse', async () => { + folders.roots.value = approved + const { root } = await mount() + await pick(root) + await typeValue(byId(root, '-name'), 'raw') + await typeValue(byLabel(root, 'Approved folder'), 'scratch') + for (const bad of ['runs', '/runs/', 'a//b/', './a/', 'a/../b/']) { + await typeValue(byId(root, '-pub-folder'), bad) + expect(save(root).props.disabled).toBe(true) + expect(content(root)).toMatch(/End the folder with a slash\.|Use a relative folder/) + } + await typeValue(byId(root, '-pub-folder'), 'runs/') + expect(save(root).props.disabled).toBe(false) + }) + + it('does not save a root the list no longer approves', async () => { + folders.roots.value = approved + const { root } = await mount() + await pick(root) + await typeValue(byId(root, '-name'), 'raw') + await typeValue(byLabel(root, 'Approved folder'), 'scratch') + expect(save(root).props.disabled).toBe(false) + folders.roots.value = [approved[0]] + await flush() + expect(save(root).props.disabled).toBe(true) + }) + + it('forgets an S3 root prefix when the type changes to a local folder', async () => { + folders.roots.value = [{ name: 'aruna/', writable: true }] + const { root } = await mount() + await typeValue(byId(root, '-pub-root'), 'aruna/') + await pick(root) + expect(byLabel(root, 'Approved folder').props.value).toBe('') + await typeValue(byId(root, '-name'), 'raw') + expect(save(root).props.disabled).toBe(true) + }) + + it('keeps the stored root of an edit fixed and keyless', async () => { + const { root } = await mount({ ...saved, kind: 'fs', public_config: { root: 'gone' } }) + expect(byLabel(root, 'Approved folder').props.disabled).toBe(true) + expect(content(root)).not.toContain('Replace keys') + await typeValue(byId(root, '-name'), 'renamed') + await submit(root) + expect(replaceGroupBackend.mock.calls[0][2]).toMatchObject({ + kind: 'fs', + public_config: { root: 'gone' }, + secret_config: {}, + }) + }) + }) }) diff --git a/src/components/groups/StorageBackendDialog.vue b/src/components/groups/StorageBackendDialog.vue index 9db79ae14..57d61aad3 100644 --- a/src/components/groups/StorageBackendDialog.vue +++ b/src/components/groups/StorageBackendDialog.vue @@ -13,8 +13,9 @@ import Switch from '@/components/ui/Switch.vue' import { computed, ref, shallowRef, useId, watch } from 'vue' import { Database, Lock } from '@lucide/vue' import { useAruna } from '@/composables/useAruna' +import { useImportRoots } from '@/composables/useImportRoots' import { OFFLINE_WRITE_HINT, useConnectivity } from '@/lib/connectivity' -import { BACKEND_KINDS, BACKEND_KIND_SCHEMAS, backendSchema, shortError } from '@/lib/storage' +import { BACKEND_KINDS, BACKEND_KIND_SCHEMAS, backendSchema, folderProblem, shortError } from '@/lib/storage' import type { GroupBackendKind, GroupBackendResponse } from '@/lib/api' import { errorMessage } from '@/lib/utils' import Notice from '@/components/ui/Notice.vue' @@ -47,6 +48,21 @@ const removing = ref(false) const toggling = shallowRef<{ backendId: string; remove: boolean } | null>(null) const submitError = ref(null) +const folderMode = computed(() => kind.value === 'fs') +const { roots, state: rootsState, error: rootsError, load: reloadRoots } = useImportRoots( + computed(() => props.groupId), + computed(() => props.open && folderMode.value), +) +const rootChoice = computed(() => roots.value.find((root) => root.name === publicValues.value.root)) +// A stored folder keeps its name in the list even when the node no longer approves it. +const rootOptions = computed(() => { + const list = roots.value.map((root) => ({ value: root.name, label: root.name })) + const stored = publicValues.value.root + if (isEdit.value && stored && !rootChoice.value) list.push({ value: stored, label: stored }) + return list +}) +const folderError = computed(() => (folderMode.value ? folderProblem(publicValues.value.folder ?? '') : null)) + const submitSummary = computed(() => (submitError.value ? shortError(submitError.value) : '')) const isEdit = computed(() => Boolean(props.backend)) @@ -98,9 +114,19 @@ const submitDisabled = computed(() => { // Nothing typed and nothing changed: there is nothing to save. if (isEdit.value && !settingsChanged.value && !credentialsEntered.value) return true if (missingRequired.value || !name.value.trim()) return true + if (folderMode.value && (folderError.value || (!isEdit.value && !rootChoice.value))) return true return needsCredentials.value && credentialsIncomplete.value }) +// A root name and an S3 root prefix share a key but mean different things. +function pickKind(next: GroupBackendKind) { + if ((next === 'fs') !== folderMode.value) { + delete publicValues.value.root + delete publicValues.value.folder + } + kind.value = next +} + watch( () => props.open, (open) => { @@ -194,7 +220,13 @@ async function setRemoved(remove: boolean) { {{ backend ? backend.name : 'Add storage' }} - {{ isEdit ? 'Keys saved.' : 'Storage your group runs, like your own S3.' }} + {{ + schema.keyless + ? 'A folder on the node that the node operator approved for your group.' + : isEdit + ? 'Keys saved.' + : 'Storage your group runs, like your own S3.' + }} @@ -227,7 +259,7 @@ async function setRemoved(remove: boolean) { :disabled="isEdit" aria-label="Type" class="mt-1" - @update:model-value="(v: string) => (kind = v as GroupBackendKind)" + @update:model-value="(v: string) => pickKind(v as GroupBackendKind)" /> @@ -246,15 +278,44 @@ async function setRemoved(remove: boolean) { + +

+ {{ folderError }} +

@@ -276,14 +337,14 @@ async function setRemoved(remove: boolean) { -
+
{{ isEdit ? 'Replace keys' : 'Credentials' }}

Stored write-only: the node never shows them again. @@ -310,7 +371,7 @@ async function setRemoved(remove: boolean) {

{{ submitError }}

-

+

Aruna checks the keys before it saves them.

diff --git a/src/composables/useImportRoots.test.ts b/src/composables/useImportRoots.test.ts new file mode 100644 index 000000000..5796ab2e7 --- /dev/null +++ b/src/composables/useImportRoots.test.ts @@ -0,0 +1,100 @@ +import { effectScope, nextTick, ref } from 'vue' +import { beforeEach, describe, expect, it, vi } from 'vitest' +import type { ImportRoot } from '@/lib/api' + +const aruna = await vi.hoisted(async () => { + const { ref } = await import('vue') + return { sessionEpoch: ref(1), apiBaseUrl: ref('https://api'), authToken: ref('t') } +}) +const getImportRoots = vi.hoisted(() => vi.fn()) +vi.mock('@/composables/useAruna', () => ({ useAruna: () => aruna })) +vi.mock('@/lib/api', async (original) => ({ ...(await original()), getImportRoots })) + +const { useImportRoots } = await import('@/composables/useImportRoots') + +const projects: ImportRoot = { name: 'projects', writable: false } +const scratch: ImportRoot = { name: 'scratch', writable: true } + +function start(group = 'g1', active = true) { + const groupId = ref(group) + const open = ref(active) + const scope = effectScope() + return { groupId, open, roots: scope.run(() => useImportRoots(groupId, open))!, scope } +} + +function later() { + let resolve: (value: T) => void = () => {} + let reject: (error: Error) => void = () => {} + const promise = new Promise((done, fail) => { + resolve = done + reject = fail + }) + return { promise, resolve, reject } +} + +describe('import roots', () => { + beforeEach(() => { + getImportRoots.mockReset() + aruna.sessionEpoch.value = 1 + }) + + it('lists the approved folders of the group', async () => { + getImportRoots.mockResolvedValue({ roots: [projects, scratch] }) + const { roots, scope } = start() + expect(roots.state.value).toBe('loading') + await vi.waitFor(() => expect(roots.state.value).toBe('ready')) + expect(getImportRoots).toHaveBeenCalledWith('g1', { baseUrl: 'https://api', token: 't' }) + expect(roots.roots.value).toEqual([projects, scratch]) + scope.stop() + }) + + it('reports a failed read instead of an empty list, and reads again on request', async () => { + getImportRoots.mockRejectedValueOnce(new Error('node down')).mockResolvedValueOnce({ roots: [] }) + const { roots, scope } = start() + await vi.waitFor(() => expect(roots.state.value).toBe('failed')) + expect(roots.error.value).toBe('node down') + await roots.load() + expect(roots.state.value).toBe('ready') + expect(roots.roots.value).toEqual([]) + scope.stop() + }) + + it('drops the answer for a group that is no longer shown', async () => { + const first = later<{ roots: ImportRoot[] }>() + getImportRoots.mockReturnValueOnce(first.promise).mockResolvedValueOnce({ roots: [scratch] }) + const { groupId, roots, scope } = start() + groupId.value = 'g2' + await vi.waitFor(() => expect(roots.roots.value).toEqual([scratch])) + first.resolve({ roots: [projects] }) + await nextTick() + expect(roots.roots.value).toEqual([scratch]) + expect(roots.state.value).toBe('ready') + scope.stop() + }) + + it('drops a late failure after the session changed', async () => { + const first = later<{ roots: ImportRoot[] }>() + getImportRoots.mockReturnValueOnce(first.promise).mockResolvedValueOnce({ roots: [scratch] }) + const { roots, scope } = start() + aruna.sessionEpoch.value = 2 + await vi.waitFor(() => expect(roots.state.value).toBe('ready')) + first.reject(new Error('old session')) + await nextTick() + expect(roots.state.value).toBe('ready') + expect(roots.error.value).toBeNull() + scope.stop() + }) + + it('reads nothing while inactive and forgets the list when it closes', async () => { + getImportRoots.mockResolvedValue({ roots: [projects] }) + const { open, roots, scope } = start('g1', false) + expect(getImportRoots).not.toHaveBeenCalled() + open.value = true + await vi.waitFor(() => expect(roots.state.value).toBe('ready')) + open.value = false + await nextTick() + expect(roots.state.value).toBe('idle') + expect(roots.roots.value).toEqual([]) + scope.stop() + }) +}) diff --git a/src/composables/useImportRoots.ts b/src/composables/useImportRoots.ts new file mode 100644 index 000000000..c2748a7c6 --- /dev/null +++ b/src/composables/useImportRoots.ts @@ -0,0 +1,49 @@ +// The folders the node operator approved for a group. Unknown stays unknown: a failed read +// is never an empty list, and an answer for another group or session is dropped. +import { ref, watch, type Ref } from 'vue' +import { useAruna } from '@/composables/useAruna' +import { getImportRoots, type ImportRoot } from '@/lib/api' +import { errorMessage } from '@/lib/utils' + +export type RootsState = 'idle' | 'loading' | 'ready' | 'failed' + +export function useImportRoots(groupId: Ref, active: Ref) { + const { apiBaseUrl, authToken, sessionEpoch } = useAruna() + const roots = ref([]) + const state = ref('idle') + const error = ref(null) + let runs = 0 + + async function load() { + const run = ++runs + const epoch = sessionEpoch.value + roots.value = [] + error.value = null + state.value = 'loading' + try { + const answer = await getImportRoots(groupId.value, { baseUrl: apiBaseUrl.value, token: authToken.value }) + if (run !== runs || epoch !== sessionEpoch.value) return + roots.value = answer.roots + state.value = 'ready' + } catch (cause) { + if (run !== runs || epoch !== sessionEpoch.value) return + error.value = errorMessage(cause) + state.value = 'failed' + } + } + + watch( + [groupId, active, () => sessionEpoch.value], + () => { + if (active.value) void load() + else { + runs++ + roots.value = [] + state.value = 'idle' + } + }, + { immediate: true }, + ) + + return { roots, state, error, load } +} diff --git a/src/lib/api/groupBackends.ts b/src/lib/api/groupBackends.ts index 9e88df175..ef9786471 100644 --- a/src/lib/api/groupBackends.ts +++ b/src/lib/api/groupBackends.ts @@ -10,7 +10,7 @@ import { apiRequest, type ApiClientOptions } from './client' // nodes), the enable route and the credentials route (both 404 there). DELETE // disables the backend once that lands; before it, DELETE is a hard delete and // can answer 409 while the backend still holds data. -export type GroupBackendKind = 's3' | 'gcs' | 'azblob' | 'azdls' | 'b2' +export type GroupBackendKind = 's3' | 'gcs' | 'azblob' | 'azdls' | 'b2' | 'fs' export interface GroupBackendResponse { backend_id: string @@ -51,6 +51,18 @@ export function getGroupBackends(groupId: string, client: ApiClientOptions): Pro return apiRequest(`/data/groups/${encodeURIComponent(groupId)}/storage/backends`, {}, client) } +// GET /data/groups/{gid}/storage/import-roots: the folders the node operator approved for the +// group, by name and never by path. Same authorization as the backend list. A `fs` backend names +// one root; a read-only root cannot receive writes. +export interface ImportRoot { + name: string + writable: boolean +} + +export function getImportRoots(groupId: string, client: ApiClientOptions): Promise<{ roots: ImportRoot[] }> { + return apiRequest(`/data/groups/${encodeURIComponent(groupId)}/storage/import-roots`, {}, client) +} + // GET /data/groups/{gid}/storage/buckets: one page of the group's buckets on this node, in name // order, each with the storage that keeps its files. Group ADMIN; older nodes answer 404. /** A step running or paused on a bucket. */ diff --git a/src/lib/linkedFolders.ts b/src/lib/linkedFolders.ts index 0bad2bf83..a264450f1 100644 --- a/src/lib/linkedFolders.ts +++ b/src/lib/linkedFolders.ts @@ -9,8 +9,9 @@ export function folderPath(value: string): string { const STORE_KEYS = ['bucket', 'container', 'filesystem'] -/** The bucket, container or filesystem a backend points at. */ +/** The bucket, container, filesystem or approved folder a backend points at. */ export function storeName(backend: GroupBackendResponse): string { + if (backend.kind === 'fs') return backend.public_config.root ?? backend.name const key = STORE_KEYS.find((name) => backend.public_config[name]) return key ? backend.public_config[key] : backend.name } diff --git a/src/lib/storage.test.ts b/src/lib/storage.test.ts index 52209dfce..9cef5ba2e 100644 --- a/src/lib/storage.test.ts +++ b/src/lib/storage.test.ts @@ -1,10 +1,12 @@ import { describe, expect, it } from 'vitest' import { + BACKEND_KIND_SCHEMAS, backendQuota, backendSummary, copyHeldBack, copyOrigin, copyState, + folderProblem, shortError, targetLabel, tenantClasses, @@ -153,3 +155,31 @@ describe('shortError', () => { expect(shortError(`backend error ${'x'.repeat(400)}`)).toBe('The node could not save the backend.') }) }) + +describe('local folder kind', () => { + it('needs a root, takes an optional folder and no credentials', () => { + const schema = BACKEND_KIND_SCHEMAS.fs + expect(schema.label).toBe('Local folder') + expect(schema.public.map((field) => [field.key, Boolean(field.required)])).toEqual([ + ['root', true], + ['folder', false], + ]) + expect(schema.secret).toEqual([]) + expect(schema.identity).toEqual(['root', 'folder']) + }) + + it('accepts an empty folder or a relative one ending with a slash', () => { + for (const folder of ['', ' ', 'raw/', 'raw/2026/', 'a b/c.d/']) expect(folderProblem(folder)).toBeNull() + }) + + it('refuses what the node refuses', () => { + expect(folderProblem('raw')).toMatch(/slash/) + for (const folder of ['/raw/', 'a//b/', './a/', 'a/../b/', '../', '/']) { + expect(folderProblem(folder)).toMatch(/relative/) + } + }) + + it('summarizes a folder by its root', () => { + expect(backendSummary(backend('fs', { root: 'projects', folder: 'raw/' }))).toBe('projects · raw/') + }) +}) diff --git a/src/lib/storage.ts b/src/lib/storage.ts index 7462537da..c4ba18417 100644 --- a/src/lib/storage.ts +++ b/src/lib/storage.ts @@ -24,6 +24,8 @@ export interface BackendKindSchema { secret: BackendField[] /** When set, exactly one of these secrets suffices; otherwise all are needed. */ secretOneOf?: string[] + /** Hides the credentials form for a kind that needs none. */ + keyless?: boolean /** Optional boolean public key, written as "true" or omitted. */ toggle?: { key: string; label: string; hint: string } } @@ -106,6 +108,25 @@ export const BACKEND_KIND_SCHEMAS: Record = { key: 'application_key', label: 'Application key', required: true }, ], }, + fs: { + label: 'Local folder', + identity: ['root', 'folder'], + public: [ + { key: 'root', label: 'Approved folder', required: true }, + { key: 'folder', label: 'Folder inside it', placeholder: 'raw/2026/' }, + ], + secret: [], + keyless: true, + }, +} + +/** Why a folder inside an approved root is not accepted; null when it is fine or empty. */ +export function folderProblem(folder: string): string | null { + const value = folder.trim() + if (!value) return null + if (!value.endsWith('/')) return 'End the folder with a slash.' + const bad = value.slice(0, -1).split('/').some((name) => name === '' || name === '.' || name === '..') + return value.startsWith('/') || bad ? 'Use a relative folder without empty names, "." or "..".' : null } export const BACKEND_KINDS = Object.keys(BACKEND_KIND_SCHEMAS) as GroupBackendKind[] @@ -120,7 +141,7 @@ export function backendSummary(backend: GroupBackendResponse): string { const keys = schema ? schema.identity : Object.keys(backend.public_config) const parts = keys.map((key) => backend.public_config[key]).filter(Boolean) const root = backend.public_config.root - if (root) parts.push(root) + if (root && !keys.includes('root')) parts.push(root) return parts.join(' · ') } From d6cb93e7a9e8aef363d63ef765a5778bfebf7796 Mon Sep 17 00:00:00 2001 From: Sebastian Beyvers Date: Tue, 3 Nov 2026 15:00:56 +0100 Subject: [PATCH 2/3] feat: keep local folders out of the routing targets --- .../groups/NewBucketsDialog.test.ts | 20 +++++++-- src/components/groups/NewBucketsDialog.vue | 4 +- .../groups/RoutingTargetPicker.test.ts | 45 +++++++++++++++++++ src/components/groups/RoutingTargetPicker.vue | 3 +- src/lib/storageRules.ts | 5 +++ 5 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 src/components/groups/RoutingTargetPicker.test.ts diff --git a/src/components/groups/NewBucketsDialog.test.ts b/src/components/groups/NewBucketsDialog.test.ts index 87ce3cb52..e063363c3 100644 --- a/src/components/groups/NewBucketsDialog.test.ts +++ b/src/components/groups/NewBucketsDialog.test.ts @@ -19,10 +19,14 @@ const InputStub = defineComponent({ h('input', { ...attrs, value: props.modelValue, onInput: (event: { target: { value: string } }) => emit('update:modelValue', event.target.value) }), }) const SelectStub = defineComponent({ - props: { modelValue: { type: String, default: '' }, ariaLabel: String }, + props: { modelValue: { type: String, default: '' }, ariaLabel: String, options: { type: Array, default: () => [] } }, emits: ['update:modelValue'], setup: (props, { emit }) => () => - h('select', { 'aria-label': props.ariaLabel, value: props.modelValue, onInput: (event: { target: { value: string } }) => emit('update:modelValue', event.target.value) }), + h( + 'select', + { 'aria-label': props.ariaLabel, value: props.modelValue, onInput: (event: { target: { value: string } }) => emit('update:modelValue', event.target.value) }, + (props.options as { value: string; label: string }[]).map((option) => h('option', { value: option.value }, option.label)), + ), }) const Dialog = compileClientComponent(new URL('./NewBucketsDialog.vue', import.meta.url), { @@ -50,11 +54,11 @@ type Root = Parameters[0] const field = (root: Root, label: string) => element(root, (node) => node.props['aria-label'] === label) const save = (root: Root) => element(root, (node) => node.tag === 'button' && node.props.type === 'submit') -async function mount(routing: Record) { +async function mount(routing: Record, backends: unknown[] = [SPIRE]) { const open = ref(false) const mounted = await mountApp( defineComponent(() => () => - h(Dialog, { open: open.value, groupId: 'g1', groupName: 'spire', backends: [SPIRE], routing: { group_id: 'g1', warnings: [], ...routing }, links: [] }), + h(Dialog, { open: open.value, groupId: 'g1', groupName: 'spire', backends, routing: { group_id: 'g1', warnings: [], ...routing }, links: [] }), ), ) open.value = true @@ -102,4 +106,12 @@ describe('NewBucketsDialog', () => { await submit(root) expect(putGroupRouting).toHaveBeenCalledWith('g1', null) }) + + it('does not offer a local folder as the storage for new buckets', async () => { + const folder = { backend_id: 'f1', group_id: 'g1', kind: 'fs', name: 'raw-folder', public_config: { root: 'projects' } } + const { root } = await mount({}, [SPIRE, folder]) + const options = content(field(root, 'Storage')) + expect(options).toContain('spire') + expect(options).not.toContain('raw-folder') + }) }) diff --git a/src/components/groups/NewBucketsDialog.vue b/src/components/groups/NewBucketsDialog.vue index 696f85826..61fe21dea 100644 --- a/src/components/groups/NewBucketsDialog.vue +++ b/src/components/groups/NewBucketsDialog.vue @@ -17,7 +17,7 @@ import { useAruna } from '@/composables/useAruna' import { OFFLINE_WRITE_HINT, useConnectivity } from '@/lib/connectivity' import type { BackendImport, GroupBackendResponse, GroupRoutingResponse } from '@/lib/api' import { fillTemplate, templateProblem } from '@/lib/bucketNaming' -import { defaultBucket, defaultConflict, splitsBuckets, suggestBucket } from '@/lib/storageRules' +import { canRoute, defaultBucket, defaultConflict, splitsBuckets, suggestBucket } from '@/lib/storageRules' import { errorMessage } from '@/lib/utils' const props = defineProps<{ @@ -46,7 +46,7 @@ const ARUNA = 'aruna' const storeOptions = computed(() => [ { value: ARUNA, label: 'Aruna storage' }, ...props.backends - .filter((backend) => !backend.disabled || backend.backend_id === store.value) + .filter((backend) => canRoute(backend) && (!backend.disabled || backend.backend_id === store.value)) .map((backend) => ({ value: backend.backend_id, label: backend.name })), ]) const backend = computed(() => props.backends.find((entry) => entry.backend_id === store.value) ?? null) diff --git a/src/components/groups/RoutingTargetPicker.test.ts b/src/components/groups/RoutingTargetPicker.test.ts new file mode 100644 index 000000000..f1748dcb7 --- /dev/null +++ b/src/components/groups/RoutingTargetPicker.test.ts @@ -0,0 +1,45 @@ +import * as VueRuntime from 'vue' +import { defineComponent, h, ref } from 'vue' +import { describe, expect, it } from 'vitest' +import { compileClientComponent, content, element, moduleDefault, mountApp } from '@/test/clientRender' +import * as Storage from '@/lib/storage' +import * as StorageRules from '@/lib/storageRules' +import type { GroupBackendResponse } from '@/lib/api' + +const SelectStub = defineComponent({ + props: { modelValue: { type: String, default: '' }, ariaLabel: String, options: { type: Array, default: () => [] } }, + setup: (props) => () => + h( + 'select', + { 'aria-label': props.ariaLabel }, + (props.options as { value: string; label: string }[]).map((option) => h('option', { value: option.value }, option.label)), + ), +}) +const Picker = compileClientComponent(new URL('./RoutingTargetPicker.vue', import.meta.url), { + vue: VueRuntime, + '@/components/ui/Select.vue': moduleDefault(SelectStub), + '@/composables/useAruna': { useAruna: () => ({ nodeInfo: ref(null) }) }, + '@/lib/storage': Storage, + '@/lib/storageRules': StorageRules, +}) + +const backend = (kind: string, name: string): GroupBackendResponse => ({ + backend_id: name, + group_id: 'g1', + kind, + name, + public_config: {}, +}) + +describe('RoutingTargetPicker', () => { + it('lists storages that take new files and leaves out local folders', async () => { + const { root } = await mountApp( + defineComponent(() => () => + h(Picker, { modelValue: null, backends: [backend('s3', 'spire'), backend('fs', 'raw-folder')] }), + ), + ) + const options = content(element(root, (node) => node.tag === 'select')) + expect(options).toContain('spire') + expect(options).not.toContain('raw-folder') + }) +}) diff --git a/src/components/groups/RoutingTargetPicker.vue b/src/components/groups/RoutingTargetPicker.vue index 40b5e3f47..085bd8d46 100644 --- a/src/components/groups/RoutingTargetPicker.vue +++ b/src/components/groups/RoutingTargetPicker.vue @@ -3,6 +3,7 @@ import Select from '@/components/ui/Select.vue' import { computed } from 'vue' import { useAruna } from '@/composables/useAruna' import { tenantClasses } from '@/lib/storage' +import { canRoute } from '@/lib/storageRules' import type { GroupBackendResponse, RoutingTarget } from '@/lib/api' const props = withDefaults( @@ -32,7 +33,7 @@ const current = computed(() => { const options = computed(() => { const list: { value: string; label: string }[] = [] if (props.allowDefault) list.push({ value: DEFAULT_VALUE, label: 'Node default' }) - for (const backend of props.backends) { + for (const backend of props.backends.filter(canRoute)) { const value = `backend:${backend.backend_id}` // A disabled backend refuses new uploads; keep it listed only while stored. if (backend.disabled && current.value !== value) continue diff --git a/src/lib/storageRules.ts b/src/lib/storageRules.ts index 726511e85..94fa84a82 100644 --- a/src/lib/storageRules.ts +++ b/src/lib/storageRules.ts @@ -14,6 +14,11 @@ export function splitsBuckets(backend: GroupBackendResponse): boolean { return backend.kind === 's3' && !backend.public_config.bucket } +/** Whether new files may be routed to the storage; a local folder serves imports only. */ +export function canRoute(backend: GroupBackendResponse): boolean { + return backend.kind !== 'fs' +} + /** The bucket and the folder of a path on a storage. */ export function splitPath(backend: GroupBackendResponse, path: string): { bucket: string; folder: string } { if (!splitsBuckets(backend)) return { bucket: storeName(backend), folder: path } From aff1a30fe45d4bfb140197ebce2ae80dcc918914 Mon Sep 17 00:00:00 2001 From: Sebastian Beyvers Date: Tue, 3 Nov 2026 15:00:56 +0100 Subject: [PATCH 3/3] feat: link a local folder and require a writable storage for a read-only one --- .../groups/LinkBucketDialog.test.ts | 117 +++++++++++++++++- src/components/groups/LinkBucketDialog.vue | 53 +++++++- src/components/groups/StorageSection.vue | 11 +- 3 files changed, 170 insertions(+), 11 deletions(-) diff --git a/src/components/groups/LinkBucketDialog.test.ts b/src/components/groups/LinkBucketDialog.test.ts index 2e0d562b6..988d66753 100644 --- a/src/components/groups/LinkBucketDialog.test.ts +++ b/src/components/groups/LinkBucketDialog.test.ts @@ -1,14 +1,22 @@ import * as VueRuntime from 'vue' import { defineComponent, h, ref } from 'vue' import { beforeEach, describe, expect, it, vi } from 'vitest' -import { compileClientComponent, content, element, flush, moduleDefault, mountApp, typeValue } from '@/test/clientRender' +import { click, compileClientComponent, content, element, flush, moduleDefault, mountApp, typeValue } from '@/test/clientRender' import * as Api from '@/lib/api' import * as BucketNaming from '@/lib/bucketNaming' import * as LinkedFolders from '@/lib/linkedFolders' +import * as Storage from '@/lib/storage' import * as StorageRules from '@/lib/storageRules' import * as Utils from '@/lib/utils' +import type { RootsState } from '@/composables/useImportRoots' const createBackendImport = vi.fn() +const folders = { + roots: ref([]), + state: ref('ready'), + error: ref(null), + load: vi.fn(), +} const runBackendImport = vi.fn() const Slot = defineComponent((_, { slots }) => () => h('div', slots.default?.())) const ButtonStub = defineComponent({ @@ -22,10 +30,14 @@ const InputStub = defineComponent({ h('input', { ...attrs, value: props.modelValue, onInput: (event: { target: { value: string } }) => emit('update:modelValue', event.target.value) }), }) const SelectStub = defineComponent({ - props: { modelValue: { type: String, default: '' }, ariaLabel: String }, + props: { modelValue: { type: String, default: '' }, ariaLabel: String, options: { type: Array, default: () => [] } }, emits: ['update:modelValue'], setup: (props, { emit }) => () => - h('select', { 'aria-label': props.ariaLabel, value: props.modelValue, onInput: (event: { target: { value: string } }) => emit('update:modelValue', event.target.value) }), + h( + 'select', + { 'aria-label': props.ariaLabel, value: props.modelValue, onInput: (event: { target: { value: string } }) => emit('update:modelValue', event.target.value) }, + (props.options as { value: string; label: string }[]).map((option) => h('option', { value: option.value }, option.label)), + ), }) const Dialog = compileClientComponent(new URL('./LinkBucketDialog.vue', import.meta.url), { @@ -42,28 +54,31 @@ const Dialog = compileClientComponent(new URL('./LinkBucketDialog.vue', import.m '@/components/ui/Input.vue': moduleDefault(InputStub), '@/components/ui/Notice.vue': moduleDefault(Slot), '@/components/ui/Select.vue': moduleDefault(SelectStub), + '@/composables/useImportRoots': { useImportRoots: () => folders }, '@/composables/useAruna': { useAruna: () => ({ apiBaseUrl: ref('https://api'), authToken: ref('t'), sessionEpoch: ref(0) }) }, '@/lib/connectivity': { OFFLINE_WRITE_HINT: 'offline', useConnectivity: () => ({ writesDisabled: ref(false) }) }, '@/lib/api': { ...Api, createBackendImport, runBackendImport }, '@/lib/bucketNaming': BucketNaming, '@/lib/linkedFolders': LinkedFolders, + '@/lib/storage': Storage, '@/lib/storageRules': StorageRules, '@/lib/utils': Utils, }) -const SPIRE = { backend_id: 's1', group_id: 'g1', kind: 's3', name: 'spire', public_config: { endpoint: 'https://s3' } } +const SPIRE: Api.GroupBackendResponse = { backend_id: 's1', group_id: 'g1', kind: 's3', name: 'spire', public_config: { endpoint: 'https://s3' } } const LINKED = { import_id: 'i1', backend_id: 's1', backend_prefix: 'lab-old/', bucket: 'old', key_prefix: '', created_at: '' } type Root = Parameters[0] const byId = (root: Root, suffix: string) => element(root, (node) => node.tag === 'input' && String(node.props.id ?? '').endsWith(suffix)) +const byLabel = (root: Root, label: string) => element(root, (node) => node.props['aria-label'] === label) const save = (root: Root) => element(root, (node) => node.tag === 'button' && node.props.type === 'submit') -async function mount() { +async function mount(backend: Api.GroupBackendResponse = SPIRE, backends: Api.GroupBackendResponse[] = [SPIRE]) { const open = ref(false) const events: unknown[] = [] const mounted = await mountApp( defineComponent(() => () => - h(Dialog, { open: open.value, groupId: 'g1', backend: SPIRE, links: [LINKED], onSaved: (error?: string) => events.push(error) }), + h(Dialog, { open: open.value, groupId: 'g1', backend, backends, links: [LINKED], onSaved: (error?: string) => events.push(error) }), ), ) open.value = true @@ -80,6 +95,10 @@ async function submit(root: Root) { beforeEach(() => { createBackendImport.mockReset().mockResolvedValue({ import_id: 'i2' }) runBackendImport.mockReset().mockResolvedValue({ job_id: 'j' }) + folders.roots.value = [] + folders.state.value = 'ready' + folders.error.value = null + folders.load.mockClear() }) describe('LinkBucketDialog', () => { @@ -114,4 +133,90 @@ describe('LinkBucketDialog', () => { await submit(root) expect(String(events[0])).toContain('Linked, but the first sync did not start') }) + + describe('local folder', () => { + const folder = (name: string, root: string): Api.GroupBackendResponse => ({ + backend_id: name, + group_id: 'g1', + kind: 'fs', + name, + public_config: { root }, + }) + const readOnly = folder('raw', 'projects') + const others = [ + readOnly, + SPIRE, + { ...SPIRE, backend_id: 's2', name: 'removed-s3', disabled: true }, + folder('frozen', 'archive'), + folder('open-folder', 'scratch'), + folder('unlisted', 'gone'), + ] + const approved = [ + { name: 'projects', writable: false }, + { name: 'archive', writable: false }, + { name: 'scratch', writable: true }, + ] + + it('links a writable folder without asking where to write', async () => { + folders.roots.value = approved + const { root } = await mount(folder('open-folder', 'scratch'), others) + expect(content(root)).toContain('Link a folder') + expect(() => byLabel(root, 'Write new uploads to')).toThrow() + await typeValue(byId(root, '-aruna'), 'runs') + await submit(root) + expect(createBackendImport.mock.calls[0][1]).not.toHaveProperty('write_backend_id') + }) + + it('needs a writable storage for a read-only folder and offers only those', async () => { + folders.roots.value = approved + const { root } = await mount(readOnly, others) + await typeValue(byId(root, '-aruna'), 'runs') + expect(content(root)).toContain('This folder is read-only') + expect(save(root).props.disabled).toBe(true) + const options = content(byLabel(root, 'Write new uploads to')) + expect(options).toContain('spire') + expect(options).toContain('open-folder') + for (const refused of ['removed-s3', 'frozen', 'unlisted', 'raw']) expect(options).not.toContain(refused) + await typeValue(byLabel(root, 'Write new uploads to'), 's1') + expect(save(root).props.disabled).toBe(false) + await submit(root) + expect(createBackendImport.mock.calls[0][1]).toMatchObject({ backend_id: 'raw', write_backend_id: 's1' }) + }) + + it('says so when no other storage can take the writes', async () => { + folders.roots.value = approved + const { root } = await mount(readOnly, [readOnly]) + await typeValue(byId(root, '-aruna'), 'runs') + expect(content(root)).toContain('Add another storage that takes them first.') + expect(save(root).props.disabled).toBe(true) + }) + + it('waits for the approved folders and reports a failed read', async () => { + folders.state.value = 'loading' + const { root } = await mount(readOnly, others) + await typeValue(byId(root, '-aruna'), 'runs') + expect(content(root)).toContain('Loading approved folders…') + expect(save(root).props.disabled).toBe(true) + folders.state.value = 'failed' + folders.error.value = 'node down' + await flush() + expect(content(root)).toContain('node down') + expect(save(root).props.disabled).toBe(true) + await click(element(root, (node) => node.tag === 'button' && content(node) === 'Try again')) + expect(folders.load).toHaveBeenCalledTimes(1) + }) + + it('refuses a folder that is no longer approved or has odd names', async () => { + folders.roots.value = approved + const { root } = await mount(folder('unlisted', 'gone'), others) + await typeValue(byId(root, '-aruna'), 'runs') + expect(content(root)).toContain('The node operator no longer approves this folder.') + expect(save(root).props.disabled).toBe(true) + const open = await mount(folder('open-folder', 'scratch'), others) + await typeValue(byId(open.root, '-aruna'), 'runs') + await typeValue(byId(open.root, '-folder'), 'a/../b') + expect(content(open.root)).toContain('Use a relative folder') + expect(save(open.root).props.disabled).toBe(true) + }) + }) }) diff --git a/src/components/groups/LinkBucketDialog.vue b/src/components/groups/LinkBucketDialog.vue index dd3eb2c6f..0340cc735 100644 --- a/src/components/groups/LinkBucketDialog.vue +++ b/src/components/groups/LinkBucketDialog.vue @@ -1,5 +1,5 @@