20251: Fix flaky collection file browser by using race-free state update callback
[arvados-workbench2.git] / src / common / config.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import Axios from 'axios';
6
7 export const WORKBENCH_CONFIG_URL =
8   process.env.REACT_APP_ARVADOS_CONFIG_URL || '/config.json';
9
10 interface WorkbenchConfig {
11   API_HOST: string;
12   VOCABULARY_URL?: string;
13   FILE_VIEWERS_CONFIG_URL?: string;
14 }
15
16 export interface ClusterConfigJSON {
17     API: {
18         UnfreezeProjectRequiresAdmin: boolean
19         MaxItemsPerResponse: number
20     },
21     ClusterID: string;
22     RemoteClusters: {
23         [key: string]: {
24             ActivateUsers: boolean
25             Host: string
26             Insecure: boolean
27             Proxy: boolean
28             Scheme: string
29         }
30     };
31   Mail?: {
32     SupportEmailAddress: string;
33   };
34   Services: {
35     Controller: {
36       ExternalURL: string;
37     };
38     Workbench1: {
39       ExternalURL: string;
40     };
41     Workbench2: {
42       ExternalURL: string;
43     };
44     Workbench: {
45         DisableSharingURLsUI: boolean;
46         ArvadosDocsite: string;
47         FileViewersConfigURL: string;
48         WelcomePageHTML: string;
49         InactivePageHTML: string;
50         SSHHelpPageHTML: string;
51         SSHHelpHostSuffix: string;
52         SiteName: string;
53         IdleTimeout: string;
54     };
55     Websocket: {
56       ExternalURL: string;
57     };
58     WebDAV: {
59       ExternalURL: string;
60     };
61     WebDAVDownload: {
62       ExternalURL: string;
63     };
64     WebShell: {
65       ExternalURL: string;
66     };
67   };
68   Workbench: {
69     DisableSharingURLsUI: boolean;
70     ArvadosDocsite: string;
71     FileViewersConfigURL: string;
72     WelcomePageHTML: string;
73     InactivePageHTML: string;
74     SSHHelpPageHTML: string;
75     SSHHelpHostSuffix: string;
76     SiteName: string;
77     IdleTimeout: string;
78     BannerUUID: string;
79   };
80   Login: {
81     LoginCluster: string;
82     Google: {
83       Enable: boolean;
84     };
85     LDAP: {
86       Enable: boolean;
87     };
88     OpenIDConnect: {
89       Enable: boolean;
90     };
91     PAM: {
92       Enable: boolean;
93     };
94     SSO: {
95       Enable: boolean;
96     };
97     Test: {
98       Enable: boolean;
99     };
100   };
101   Collections: {
102     ForwardSlashNameSubstitution: string;
103     ManagedProperties?: {
104       [key: string]: {
105         Function: string;
106         Value: string;
107         Protected?: boolean;
108       };
109     };
110     TrustAllContent: boolean;
111   };
112   Volumes: {
113     [key: string]: {
114       StorageClasses: {
115         [key: string]: boolean;
116       };
117     };
118   };
119 }
120
121 export class Config {
122   baseUrl!: string;
123   keepWebServiceUrl!: string;
124   keepWebInlineServiceUrl!: string;
125   remoteHosts!: {
126     [key: string]: string;
127   };
128   rootUrl!: string;
129   uuidPrefix!: 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;
138 }
139
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);
157   return config;
158 };
159
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]) {
166         classes.add(sc);
167       }
168     });
169   });
170   return Array.from(classes);
171 };
172
173 const getApiRevision = async (apiUrl: string) => {
174   try {
175     const dd = (await Axios.get<any>(`${apiUrl}/${DISCOVERY_DOC_PATH}`)).data;
176     return parseInt(dd.revision, 10) || 0;
177   } catch {
178     console.warn(
179       'Unable to get API Revision number, defaulting to zero. Some features may not work properly.'
180     );
181     return 0;
182   }
183 };
184
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(/\/+$/, '');
193     }
194   });
195   return { ...config, Services: svcs };
196 };
197
198 export const fetchConfig = () => {
199   return Axios.get<WorkbenchConfig>(
200     WORKBENCH_CONFIG_URL + '?nocache=' + new Date().getTime()
201   )
202     .then((response) => response.data)
203     .catch(() => {
204       console.warn(
205         `There was an exception getting the Workbench config file at ${WORKBENCH_CONFIG_URL}. Using defaults instead.`
206       );
207       return Promise.resolve(getDefaultConfig());
208     })
209     .then((workbenchConfig) => {
210       if (workbenchConfig.API_HOST === undefined) {
211         throw new Error(
212           `Unable to start Workbench. API_HOST is undefined in ${WORKBENCH_CONFIG_URL} or the environment.`
213         );
214       }
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(/\/+$/, '')
220         );
221         const config = { ...buildConfig(response.data), apiRevision };
222         const warnLocalConfig = (varName: string) =>
223           console.warn(
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}`
226           );
227
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;
235         } else {
236           fileViewerConfigUrl =
237             config.clusterConfig.Workbench.FileViewersConfigURL ||
238             '/file-viewers-example.json';
239         }
240         config.fileViewersConfigUrl = fileViewerConfigUrl;
241
242         if (workbenchConfig.VOCABULARY_URL !== undefined) {
243           console.warn(
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.`
245           );
246         }
247         config.vocabularyUrl = getVocabularyURL(workbenchConfig.API_HOST);
248
249         return { config, apiHost: workbenchConfig.API_HOST };
250       });
251     });
252 };
253
254 // Maps remote cluster hosts and removes the default RemoteCluster entry
255 export const mapRemoteHosts = (
256   clusterConfigJSON: ClusterConfigJSON,
257   config: Config
258 ) => {
259   config.remoteHosts = {};
260   Object.keys(clusterConfigJSON.RemoteClusters).forEach((k) => {
261     config.remoteHosts[k] = clusterConfigJSON.RemoteClusters[k].Host;
262   });
263   delete config.remoteHosts['*'];
264 };
265
266 export const mockClusterConfigJSON = (
267   config: Partial<ClusterConfigJSON>
268 ): ClusterConfigJSON => ({
269   API: {
270     UnfreezeProjectRequiresAdmin: false,
271     MaxItemsPerResponse: 1000,
272   },
273   ClusterID: '',
274   RemoteClusters: {},
275   Services: {
276     Controller: { ExternalURL: '' },
277     Workbench1: { ExternalURL: '' },
278     Workbench2: { ExternalURL: '' },
279     Websocket: { ExternalURL: '' },
280     WebDAV: { ExternalURL: '' },
281     WebDAVDownload: { ExternalURL: '' },
282     WebShell: { ExternalURL: '' },
283     Workbench: {
284       DisableSharingURLsUI: false,
285       ArvadosDocsite: "",
286       FileViewersConfigURL: "",
287       WelcomePageHTML: "",
288       InactivePageHTML: "",
289       SSHHelpPageHTML: "",
290       SSHHelpHostSuffix: "",
291       SiteName: "",
292       IdleTimeout: "0s"
293     },
294   },
295   Workbench: {
296     DisableSharingURLsUI: false,
297     ArvadosDocsite: '',
298     FileViewersConfigURL: '',
299     WelcomePageHTML: '',
300     InactivePageHTML: '',
301     SSHHelpPageHTML: '',
302     SSHHelpHostSuffix: '',
303     SiteName: '',
304     IdleTimeout: '0s',
305     BannerUUID: ""
306   },
307   Login: {
308     LoginCluster: '',
309     Google: {
310       Enable: false,
311     },
312     LDAP: {
313       Enable: false,
314     },
315     OpenIDConnect: {
316       Enable: false,
317     },
318     PAM: {
319       Enable: false,
320     },
321     SSO: {
322       Enable: false,
323     },
324     Test: {
325       Enable: false,
326     },
327   },
328   Collections: {
329     ForwardSlashNameSubstitution: '',
330     TrustAllContent: false,
331   },
332   Volumes: {},
333   ...config,
334 });
335
336 export const mockConfig = (config: Partial<Config>): Config => ({
337   baseUrl: '',
338   keepWebServiceUrl: '',
339   keepWebInlineServiceUrl: '',
340   remoteHosts: {},
341   rootUrl: '',
342   uuidPrefix: '',
343   websocketUrl: '',
344   workbenchUrl: '',
345   workbench2Url: '',
346   vocabularyUrl: '',
347   fileViewersConfigUrl: '',
348   loginCluster: '',
349   clusterConfig: mockClusterConfigJSON({}),
350   apiRevision: 0,
351   ...config,
352 });
353
354 const getDefaultConfig = (): WorkbenchConfig => {
355   let apiHost = '';
356   const envHost = process.env.REACT_APP_ARVADOS_API_HOST;
357   if (envHost !== undefined) {
358     console.warn(`Using default API host ${envHost}.`);
359     apiHost = envHost;
360   } else {
361     console.warn(
362       `No API host was found in the environment. Workbench may not be able to communicate with Arvados components.`
363     );
364   }
365   return {
366     API_HOST: apiHost,
367     VOCABULARY_URL: undefined,
368     FILE_VIEWERS_CONFIG_URL: undefined,
369   };
370 };
371
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()}`;