1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import Axios from 'axios';
7 export const WORKBENCH_CONFIG_URL =
8 process.env.REACT_APP_ARVADOS_CONFIG_URL || '/config.json';
10 interface WorkbenchConfig {
12 VOCABULARY_URL?: string;
13 FILE_VIEWERS_CONFIG_URL?: string;
16 export interface ClusterConfigJSON {
18 UnfreezeProjectRequiresAdmin: boolean
19 MaxItemsPerResponse: number
24 ActivateUsers: boolean
32 SupportEmailAddress: string;
45 DisableSharingURLsUI: boolean;
46 ArvadosDocsite: string;
47 FileViewersConfigURL: string;
48 WelcomePageHTML: string;
49 InactivePageHTML: string;
50 SSHHelpPageHTML: string;
51 SSHHelpHostSuffix: string;
69 DisableSharingURLsUI: boolean;
70 ArvadosDocsite: string;
71 FileViewersConfigURL: string;
72 WelcomePageHTML: string;
73 InactivePageHTML: string;
74 SSHHelpPageHTML: string;
75 SSHHelpHostSuffix: string;
102 ForwardSlashNameSubstitution: string;
103 ManagedProperties?: {
110 TrustAllContent: boolean;
115 [key: string]: boolean;
121 export class Config {
123 keepWebServiceUrl!: string;
124 keepWebInlineServiceUrl!: string;
126 [key: string]: string;
130 websocketUrl!: string;
131 workbenchUrl!: string;
132 workbench2Url!: string;
133 vocabularyUrl!: string;
134 fileViewersConfigUrl!: string;
135 loginCluster!: string;
136 clusterConfig!: ClusterConfigJSON;
137 apiRevision!: number;
140 export const buildConfig = (clusterConfig: ClusterConfigJSON): Config => {
141 const clusterConfigJSON = removeTrailingSlashes(clusterConfig);
142 const config = new Config();
143 config.rootUrl = clusterConfigJSON.Services.Controller.ExternalURL;
144 config.baseUrl = `${config.rootUrl}/${ARVADOS_API_PATH}`;
145 config.uuidPrefix = clusterConfigJSON.ClusterID;
146 config.websocketUrl = clusterConfigJSON.Services.Websocket.ExternalURL;
147 config.workbench2Url = clusterConfigJSON.Services.Workbench2.ExternalURL;
148 config.workbenchUrl = clusterConfigJSON.Services.Workbench1.ExternalURL;
149 config.keepWebServiceUrl =
150 clusterConfigJSON.Services.WebDAVDownload.ExternalURL;
151 config.keepWebInlineServiceUrl =
152 clusterConfigJSON.Services.WebDAV.ExternalURL;
153 config.loginCluster = clusterConfigJSON.Login.LoginCluster;
154 config.clusterConfig = clusterConfigJSON;
155 config.apiRevision = 0;
156 mapRemoteHosts(clusterConfigJSON, config);
160 export const getStorageClasses = (config: Config): string[] => {
161 const classes: Set<string> = new Set(['default']);
162 const volumes = config.clusterConfig.Volumes;
163 Object.keys(volumes).forEach((v) => {
164 Object.keys(volumes[v].StorageClasses || {}).forEach((sc) => {
165 if (volumes[v].StorageClasses[sc]) {
170 return Array.from(classes);
173 const getApiRevision = async (apiUrl: string) => {
175 const dd = (await Axios.get<any>(`${apiUrl}/${DISCOVERY_DOC_PATH}`)).data;
176 return parseInt(dd.revision, 10) || 0;
179 'Unable to get API Revision number, defaulting to zero. Some features may not work properly.'
185 const removeTrailingSlashes = (
186 config: ClusterConfigJSON
187 ): ClusterConfigJSON => {
188 const svcs: any = {};
189 Object.keys(config.Services).forEach((s) => {
190 svcs[s] = config.Services[s];
191 if (svcs[s].hasOwnProperty('ExternalURL')) {
192 svcs[s].ExternalURL = svcs[s].ExternalURL.replace(/\/+$/, '');
195 return { ...config, Services: svcs };
198 export const fetchConfig = () => {
199 return Axios.get<WorkbenchConfig>(
200 WORKBENCH_CONFIG_URL + '?nocache=' + new Date().getTime()
202 .then((response) => response.data)
205 `There was an exception getting the Workbench config file at ${WORKBENCH_CONFIG_URL}. Using defaults instead.`
207 return Promise.resolve(getDefaultConfig());
209 .then((workbenchConfig) => {
210 if (workbenchConfig.API_HOST === undefined) {
212 `Unable to start Workbench. API_HOST is undefined in ${WORKBENCH_CONFIG_URL} or the environment.`
215 return Axios.get<ClusterConfigJSON>(
216 getClusterConfigURL(workbenchConfig.API_HOST)
217 ).then(async (response) => {
218 const apiRevision = await getApiRevision(
219 response.data.Services.Controller.ExternalURL.replace(/\/+$/, '')
221 const config = { ...buildConfig(response.data), apiRevision };
222 const warnLocalConfig = (varName: string) =>
224 `A value for ${varName} was found in ${WORKBENCH_CONFIG_URL}. To use the Arvados centralized configuration instead, \
225 remove the entire ${varName} entry from ${WORKBENCH_CONFIG_URL}`
228 // Check if the workbench config has an entry for vocabulary and file viewer URLs
229 // If so, use these values (even if it is an empty string), but print a console warning.
230 // Otherwise, use the cluster config.
231 let fileViewerConfigUrl;
232 if (workbenchConfig.FILE_VIEWERS_CONFIG_URL !== undefined) {
233 warnLocalConfig('FILE_VIEWERS_CONFIG_URL');
234 fileViewerConfigUrl = workbenchConfig.FILE_VIEWERS_CONFIG_URL;
236 fileViewerConfigUrl =
237 config.clusterConfig.Workbench.FileViewersConfigURL ||
238 '/file-viewers-example.json';
240 config.fileViewersConfigUrl = fileViewerConfigUrl;
242 if (workbenchConfig.VOCABULARY_URL !== undefined) {
244 `A value for VOCABULARY_URL was found in ${WORKBENCH_CONFIG_URL}. It will be ignored as the cluster already provides its own endpoint, you can safely remove it.`
247 config.vocabularyUrl = getVocabularyURL(workbenchConfig.API_HOST);
249 return { config, apiHost: workbenchConfig.API_HOST };
254 // Maps remote cluster hosts and removes the default RemoteCluster entry
255 export const mapRemoteHosts = (
256 clusterConfigJSON: ClusterConfigJSON,
259 config.remoteHosts = {};
260 Object.keys(clusterConfigJSON.RemoteClusters).forEach((k) => {
261 config.remoteHosts[k] = clusterConfigJSON.RemoteClusters[k].Host;
263 delete config.remoteHosts['*'];
266 export const mockClusterConfigJSON = (
267 config: Partial<ClusterConfigJSON>
268 ): ClusterConfigJSON => ({
270 UnfreezeProjectRequiresAdmin: false,
271 MaxItemsPerResponse: 1000,
276 Controller: { ExternalURL: '' },
277 Workbench1: { ExternalURL: '' },
278 Workbench2: { ExternalURL: '' },
279 Websocket: { ExternalURL: '' },
280 WebDAV: { ExternalURL: '' },
281 WebDAVDownload: { ExternalURL: '' },
282 WebShell: { ExternalURL: '' },
284 DisableSharingURLsUI: false,
286 FileViewersConfigURL: "",
288 InactivePageHTML: "",
290 SSHHelpHostSuffix: "",
296 DisableSharingURLsUI: false,
298 FileViewersConfigURL: '',
300 InactivePageHTML: '',
302 SSHHelpHostSuffix: '',
329 ForwardSlashNameSubstitution: '',
330 TrustAllContent: false,
336 export const mockConfig = (config: Partial<Config>): Config => ({
338 keepWebServiceUrl: '',
339 keepWebInlineServiceUrl: '',
347 fileViewersConfigUrl: '',
349 clusterConfig: mockClusterConfigJSON({}),
354 const getDefaultConfig = (): WorkbenchConfig => {
356 const envHost = process.env.REACT_APP_ARVADOS_API_HOST;
357 if (envHost !== undefined) {
358 console.warn(`Using default API host ${envHost}.`);
362 `No API host was found in the environment. Workbench may not be able to communicate with Arvados components.`
367 VOCABULARY_URL: undefined,
368 FILE_VIEWERS_CONFIG_URL: undefined,
372 export const ARVADOS_API_PATH = 'arvados/v1';
373 export const CLUSTER_CONFIG_PATH = 'arvados/v1/config';
374 export const VOCABULARY_PATH = 'arvados/v1/vocabulary';
375 export const DISCOVERY_DOC_PATH = 'discovery/v1/apis/arvados/v1/rest';
376 export const getClusterConfigURL = (apiHost: string) =>
377 `https://${apiHost}/${CLUSTER_CONFIG_PATH}?nocache=${new Date().getTime()}`;
378 export const getVocabularyURL = (apiHost: string) =>
379 `https://${apiHost}/${VOCABULARY_PATH}?nocache=${new Date().getTime()}`;