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;
120 AnonymousUserToken: string;
124 export class Config {
126 keepWebServiceUrl!: string;
127 keepWebInlineServiceUrl!: string;
129 [key: string]: string;
133 websocketUrl!: string;
134 workbenchUrl!: string;
135 workbench2Url!: string;
136 vocabularyUrl!: string;
137 fileViewersConfigUrl!: string;
138 loginCluster!: string;
139 clusterConfig!: ClusterConfigJSON;
140 apiRevision!: number;
143 export const buildConfig = (clusterConfig: ClusterConfigJSON): Config => {
144 const clusterConfigJSON = removeTrailingSlashes(clusterConfig);
145 const config = new Config();
146 config.rootUrl = clusterConfigJSON.Services.Controller.ExternalURL;
147 config.baseUrl = `${config.rootUrl}/${ARVADOS_API_PATH}`;
148 config.uuidPrefix = clusterConfigJSON.ClusterID;
149 config.websocketUrl = clusterConfigJSON.Services.Websocket.ExternalURL;
150 config.workbench2Url = clusterConfigJSON.Services.Workbench2.ExternalURL;
151 config.workbenchUrl = clusterConfigJSON.Services.Workbench1.ExternalURL;
152 config.keepWebServiceUrl =
153 clusterConfigJSON.Services.WebDAVDownload.ExternalURL;
154 config.keepWebInlineServiceUrl =
155 clusterConfigJSON.Services.WebDAV.ExternalURL;
156 config.loginCluster = clusterConfigJSON.Login.LoginCluster;
157 config.clusterConfig = clusterConfigJSON;
158 config.apiRevision = 0;
159 mapRemoteHosts(clusterConfigJSON, config);
163 export const getStorageClasses = (config: Config): string[] => {
164 const classes: Set<string> = new Set(['default']);
165 const volumes = config.clusterConfig.Volumes;
166 Object.keys(volumes).forEach((v) => {
167 Object.keys(volumes[v].StorageClasses || {}).forEach((sc) => {
168 if (volumes[v].StorageClasses[sc]) {
173 return Array.from(classes);
176 const getApiRevision = async (apiUrl: string) => {
178 const dd = (await Axios.get<any>(`${apiUrl}/${DISCOVERY_DOC_PATH}`)).data;
179 return parseInt(dd.revision, 10) || 0;
182 'Unable to get API Revision number, defaulting to zero. Some features may not work properly.'
188 const removeTrailingSlashes = (
189 config: ClusterConfigJSON
190 ): ClusterConfigJSON => {
191 const svcs: any = {};
192 Object.keys(config.Services).forEach((s) => {
193 svcs[s] = config.Services[s];
194 if (svcs[s].hasOwnProperty('ExternalURL')) {
195 svcs[s].ExternalURL = svcs[s].ExternalURL.replace(/\/+$/, '');
198 return { ...config, Services: svcs };
201 export const fetchConfig = () => {
202 return Axios.get<WorkbenchConfig>(
203 WORKBENCH_CONFIG_URL + '?nocache=' + new Date().getTime()
205 .then((response) => response.data)
208 `There was an exception getting the Workbench config file at ${WORKBENCH_CONFIG_URL}. Using defaults instead.`
210 return Promise.resolve(getDefaultConfig());
212 .then((workbenchConfig) => {
213 if (workbenchConfig.API_HOST === undefined) {
215 `Unable to start Workbench. API_HOST is undefined in ${WORKBENCH_CONFIG_URL} or the environment.`
218 return Axios.get<ClusterConfigJSON>(
219 getClusterConfigURL(workbenchConfig.API_HOST)
220 ).then(async (response) => {
221 const apiRevision = await getApiRevision(
222 response.data.Services.Controller.ExternalURL.replace(/\/+$/, '')
224 const config = { ...buildConfig(response.data), apiRevision };
225 const warnLocalConfig = (varName: string) =>
227 `A value for ${varName} was found in ${WORKBENCH_CONFIG_URL}. To use the Arvados centralized configuration instead, \
228 remove the entire ${varName} entry from ${WORKBENCH_CONFIG_URL}`
231 // Check if the workbench config has an entry for vocabulary and file viewer URLs
232 // If so, use these values (even if it is an empty string), but print a console warning.
233 // Otherwise, use the cluster config.
234 let fileViewerConfigUrl;
235 if (workbenchConfig.FILE_VIEWERS_CONFIG_URL !== undefined) {
236 warnLocalConfig('FILE_VIEWERS_CONFIG_URL');
237 fileViewerConfigUrl = workbenchConfig.FILE_VIEWERS_CONFIG_URL;
239 fileViewerConfigUrl =
240 config.clusterConfig.Workbench.FileViewersConfigURL ||
241 '/file-viewers-example.json';
243 config.fileViewersConfigUrl = fileViewerConfigUrl;
245 if (workbenchConfig.VOCABULARY_URL !== undefined) {
247 `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.`
250 config.vocabularyUrl = getVocabularyURL(workbenchConfig.API_HOST);
252 return { config, apiHost: workbenchConfig.API_HOST };
257 // Maps remote cluster hosts and removes the default RemoteCluster entry
258 export const mapRemoteHosts = (
259 clusterConfigJSON: ClusterConfigJSON,
262 config.remoteHosts = {};
263 Object.keys(clusterConfigJSON.RemoteClusters).forEach((k) => {
264 config.remoteHosts[k] = clusterConfigJSON.RemoteClusters[k].Host;
266 delete config.remoteHosts['*'];
269 export const mockClusterConfigJSON = (
270 config: Partial<ClusterConfigJSON>
271 ): ClusterConfigJSON => ({
273 UnfreezeProjectRequiresAdmin: false,
274 MaxItemsPerResponse: 1000,
279 Controller: { ExternalURL: '' },
280 Workbench1: { ExternalURL: '' },
281 Workbench2: { ExternalURL: '' },
282 Websocket: { ExternalURL: '' },
283 WebDAV: { ExternalURL: '' },
284 WebDAVDownload: { ExternalURL: '' },
285 WebShell: { ExternalURL: '' },
287 DisableSharingURLsUI: false,
289 FileViewersConfigURL: "",
291 InactivePageHTML: "",
293 SSHHelpHostSuffix: "",
299 DisableSharingURLsUI: false,
301 FileViewersConfigURL: '',
303 InactivePageHTML: '',
305 SSHHelpHostSuffix: '',
332 ForwardSlashNameSubstitution: '',
333 TrustAllContent: false,
337 AnonymousUserToken: ""
342 export const mockConfig = (config: Partial<Config>): Config => ({
344 keepWebServiceUrl: '',
345 keepWebInlineServiceUrl: '',
353 fileViewersConfigUrl: '',
355 clusterConfig: mockClusterConfigJSON({}),
360 const getDefaultConfig = (): WorkbenchConfig => {
362 const envHost = process.env.REACT_APP_ARVADOS_API_HOST;
363 if (envHost !== undefined) {
364 console.warn(`Using default API host ${envHost}.`);
368 `No API host was found in the environment. Workbench may not be able to communicate with Arvados components.`
373 VOCABULARY_URL: undefined,
374 FILE_VIEWERS_CONFIG_URL: undefined,
378 export const ARVADOS_API_PATH = 'arvados/v1';
379 export const CLUSTER_CONFIG_PATH = 'arvados/v1/config';
380 export const VOCABULARY_PATH = 'arvados/v1/vocabulary';
381 export const DISCOVERY_DOC_PATH = 'discovery/v1/apis/arvados/v1/rest';
382 export const getClusterConfigURL = (apiHost: string) =>
383 `https://${apiHost}/${CLUSTER_CONFIG_PATH}?nocache=${new Date().getTime()}`;
384 export const getVocabularyURL = (apiHost: string) =>
385 `https://${apiHost}/${VOCABULARY_PATH}?nocache=${new Date().getTime()}`;