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;
79 UserProfileFormFields: {};
103 ForwardSlashNameSubstitution: string;
104 ManagedProperties?: {
111 TrustAllContent: boolean;
116 [key: string]: boolean;
121 AnonymousUserToken: string;
125 export class Config {
127 keepWebServiceUrl!: string;
128 keepWebInlineServiceUrl!: string;
130 [key: string]: string;
134 websocketUrl!: string;
135 workbenchUrl!: string;
136 workbench2Url!: string;
137 vocabularyUrl!: string;
138 fileViewersConfigUrl!: string;
139 loginCluster!: string;
140 clusterConfig!: ClusterConfigJSON;
141 apiRevision!: number;
144 export const buildConfig = (clusterConfig: ClusterConfigJSON): Config => {
145 const clusterConfigJSON = removeTrailingSlashes(clusterConfig);
146 const config = new Config();
147 config.rootUrl = clusterConfigJSON.Services.Controller.ExternalURL;
148 config.baseUrl = `${config.rootUrl}/${ARVADOS_API_PATH}`;
149 config.uuidPrefix = clusterConfigJSON.ClusterID;
150 config.websocketUrl = clusterConfigJSON.Services.Websocket.ExternalURL;
151 config.workbench2Url = clusterConfigJSON.Services.Workbench2.ExternalURL;
152 config.workbenchUrl = clusterConfigJSON.Services.Workbench1.ExternalURL;
153 config.keepWebServiceUrl =
154 clusterConfigJSON.Services.WebDAVDownload.ExternalURL;
155 config.keepWebInlineServiceUrl =
156 clusterConfigJSON.Services.WebDAV.ExternalURL;
157 config.loginCluster = clusterConfigJSON.Login.LoginCluster;
158 config.clusterConfig = clusterConfigJSON;
159 config.apiRevision = 0;
160 mapRemoteHosts(clusterConfigJSON, config);
164 export const getStorageClasses = (config: Config): string[] => {
165 const classes: Set<string> = new Set(['default']);
166 const volumes = config.clusterConfig.Volumes;
167 Object.keys(volumes).forEach((v) => {
168 Object.keys(volumes[v].StorageClasses || {}).forEach((sc) => {
169 if (volumes[v].StorageClasses[sc]) {
174 return Array.from(classes);
177 const getApiRevision = async (apiUrl: string) => {
179 const dd = (await Axios.get<any>(`${apiUrl}/${DISCOVERY_DOC_PATH}`)).data;
180 return parseInt(dd.revision, 10) || 0;
183 'Unable to get API Revision number, defaulting to zero. Some features may not work properly.'
189 const removeTrailingSlashes = (
190 config: ClusterConfigJSON
191 ): ClusterConfigJSON => {
192 const svcs: any = {};
193 Object.keys(config.Services).forEach((s) => {
194 svcs[s] = config.Services[s];
195 if (svcs[s].hasOwnProperty('ExternalURL')) {
196 svcs[s].ExternalURL = svcs[s].ExternalURL.replace(/\/+$/, '');
199 return { ...config, Services: svcs };
202 export const fetchConfig = () => {
203 return Axios.get<WorkbenchConfig>(
204 WORKBENCH_CONFIG_URL + '?nocache=' + new Date().getTime()
206 .then((response) => response.data)
209 `There was an exception getting the Workbench config file at ${WORKBENCH_CONFIG_URL}. Using defaults instead.`
211 return Promise.resolve(getDefaultConfig());
213 .then((workbenchConfig) => {
214 if (workbenchConfig.API_HOST === undefined) {
216 `Unable to start Workbench. API_HOST is undefined in ${WORKBENCH_CONFIG_URL} or the environment.`
219 return Axios.get<ClusterConfigJSON>(
220 getClusterConfigURL(workbenchConfig.API_HOST)
221 ).then(async (response) => {
222 const apiRevision = await getApiRevision(
223 response.data.Services.Controller.ExternalURL.replace(/\/+$/, '')
225 const config = { ...buildConfig(response.data), apiRevision };
226 const warnLocalConfig = (varName: string) =>
228 `A value for ${varName} was found in ${WORKBENCH_CONFIG_URL}. To use the Arvados centralized configuration instead, \
229 remove the entire ${varName} entry from ${WORKBENCH_CONFIG_URL}`
232 // Check if the workbench config has an entry for vocabulary and file viewer URLs
233 // If so, use these values (even if it is an empty string), but print a console warning.
234 // Otherwise, use the cluster config.
235 let fileViewerConfigUrl;
236 if (workbenchConfig.FILE_VIEWERS_CONFIG_URL !== undefined) {
237 warnLocalConfig('FILE_VIEWERS_CONFIG_URL');
238 fileViewerConfigUrl = workbenchConfig.FILE_VIEWERS_CONFIG_URL;
240 fileViewerConfigUrl =
241 config.clusterConfig.Workbench.FileViewersConfigURL ||
242 '/file-viewers-example.json';
244 config.fileViewersConfigUrl = fileViewerConfigUrl;
246 if (workbenchConfig.VOCABULARY_URL !== undefined) {
248 `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.`
251 config.vocabularyUrl = getVocabularyURL(workbenchConfig.API_HOST);
253 return { config, apiHost: workbenchConfig.API_HOST };
258 // Maps remote cluster hosts and removes the default RemoteCluster entry
259 export const mapRemoteHosts = (
260 clusterConfigJSON: ClusterConfigJSON,
263 config.remoteHosts = {};
264 Object.keys(clusterConfigJSON.RemoteClusters).forEach((k) => {
265 config.remoteHosts[k] = clusterConfigJSON.RemoteClusters[k].Host;
267 delete config.remoteHosts['*'];
270 export const mockClusterConfigJSON = (
271 config: Partial<ClusterConfigJSON>
272 ): ClusterConfigJSON => ({
274 UnfreezeProjectRequiresAdmin: false,
275 MaxItemsPerResponse: 1000,
280 Controller: { ExternalURL: '' },
281 Workbench1: { ExternalURL: '' },
282 Workbench2: { ExternalURL: '' },
283 Websocket: { ExternalURL: '' },
284 WebDAV: { ExternalURL: '' },
285 WebDAVDownload: { ExternalURL: '' },
286 WebShell: { ExternalURL: '' },
288 DisableSharingURLsUI: false,
290 FileViewersConfigURL: "",
292 InactivePageHTML: "",
294 SSHHelpHostSuffix: "",
300 DisableSharingURLsUI: false,
302 FileViewersConfigURL: '',
304 InactivePageHTML: '',
306 SSHHelpHostSuffix: '',
310 UserProfileFormFields: {}
334 ForwardSlashNameSubstitution: '',
335 TrustAllContent: false,
339 AnonymousUserToken: ""
344 export const mockConfig = (config: Partial<Config>): Config => ({
346 keepWebServiceUrl: '',
347 keepWebInlineServiceUrl: '',
355 fileViewersConfigUrl: '',
357 clusterConfig: mockClusterConfigJSON({}),
362 const getDefaultConfig = (): WorkbenchConfig => {
364 const envHost = process.env.REACT_APP_ARVADOS_API_HOST;
365 if (envHost !== undefined) {
366 console.warn(`Using default API host ${envHost}.`);
370 `No API host was found in the environment. Workbench may not be able to communicate with Arvados components.`
375 VOCABULARY_URL: undefined,
376 FILE_VIEWERS_CONFIG_URL: undefined,
380 export const ARVADOS_API_PATH = 'arvados/v1';
381 export const CLUSTER_CONFIG_PATH = 'arvados/v1/config';
382 export const VOCABULARY_PATH = 'arvados/v1/vocabulary';
383 export const DISCOVERY_DOC_PATH = 'discovery/v1/apis/arvados/v1/rest';
384 export const getClusterConfigURL = (apiHost: string) =>
385 `https://${apiHost}/${CLUSTER_CONFIG_PATH}?nocache=${new Date().getTime()}`;
386 export const getVocabularyURL = (apiHost: string) =>
387 `https://${apiHost}/${VOCABULARY_PATH}?nocache=${new Date().getTime()}`;