Merge branch 'main' into 19462-colorscheme refs #19462
[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     Websocket: {
45       ExternalURL: string;
46     };
47     WebDAV: {
48       ExternalURL: string;
49     };
50     WebDAVDownload: {
51       ExternalURL: string;
52     };
53     WebShell: {
54       ExternalURL: string;
55     };
56   };
57   Workbench: {
58     DisableSharingURLsUI: boolean;
59     ArvadosDocsite: string;
60     FileViewersConfigURL: string;
61     WelcomePageHTML: string;
62     InactivePageHTML: string;
63     SSHHelpPageHTML: string;
64     SSHHelpHostSuffix: string;
65     SiteName: string;
66     IdleTimeout: string;
67   };
68   Login: {
69     LoginCluster: string;
70     Google: {
71       Enable: boolean;
72     };
73     LDAP: {
74       Enable: boolean;
75     };
76     OpenIDConnect: {
77       Enable: boolean;
78     };
79     PAM: {
80       Enable: boolean;
81     };
82     SSO: {
83       Enable: boolean;
84     };
85     Test: {
86       Enable: boolean;
87     };
88   };
89   Collections: {
90     ForwardSlashNameSubstitution: string;
91     ManagedProperties?: {
92       [key: string]: {
93         Function: string;
94         Value: string;
95         Protected?: boolean;
96       };
97     };
98     TrustAllContent: boolean;
99   };
100   Volumes: {
101     [key: string]: {
102       StorageClasses: {
103         [key: string]: boolean;
104       };
105     };
106   };
107 }
108
109 export class Config {
110   baseUrl!: string;
111   keepWebServiceUrl!: string;
112   keepWebInlineServiceUrl!: string;
113   remoteHosts!: {
114     [key: string]: string;
115   };
116   rootUrl!: string;
117   uuidPrefix!: string;
118   websocketUrl!: string;
119   workbenchUrl!: string;
120   workbench2Url!: string;
121   vocabularyUrl!: string;
122   fileViewersConfigUrl!: string;
123   loginCluster!: string;
124   clusterConfig!: ClusterConfigJSON;
125   apiRevision!: number;
126 }
127
128 export const buildConfig = (clusterConfig: ClusterConfigJSON): Config => {
129   const clusterConfigJSON = removeTrailingSlashes(clusterConfig);
130   const config = new Config();
131   config.rootUrl = clusterConfigJSON.Services.Controller.ExternalURL;
132   config.baseUrl = `${config.rootUrl}/${ARVADOS_API_PATH}`;
133   config.uuidPrefix = clusterConfigJSON.ClusterID;
134   config.websocketUrl = clusterConfigJSON.Services.Websocket.ExternalURL;
135   config.workbench2Url = clusterConfigJSON.Services.Workbench2.ExternalURL;
136   config.workbenchUrl = clusterConfigJSON.Services.Workbench1.ExternalURL;
137   config.keepWebServiceUrl =
138     clusterConfigJSON.Services.WebDAVDownload.ExternalURL;
139   config.keepWebInlineServiceUrl =
140     clusterConfigJSON.Services.WebDAV.ExternalURL;
141   config.loginCluster = clusterConfigJSON.Login.LoginCluster;
142   config.clusterConfig = clusterConfigJSON;
143   config.apiRevision = 0;
144   mapRemoteHosts(clusterConfigJSON, config);
145   return config;
146 };
147
148 export const getStorageClasses = (config: Config): string[] => {
149   const classes: Set<string> = new Set(['default']);
150   const volumes = config.clusterConfig.Volumes;
151   Object.keys(volumes).forEach((v) => {
152     Object.keys(volumes[v].StorageClasses || {}).forEach((sc) => {
153       if (volumes[v].StorageClasses[sc]) {
154         classes.add(sc);
155       }
156     });
157   });
158   return Array.from(classes);
159 };
160
161 const getApiRevision = async (apiUrl: string) => {
162   try {
163     const dd = (await Axios.get<any>(`${apiUrl}/${DISCOVERY_DOC_PATH}`)).data;
164     return parseInt(dd.revision, 10) || 0;
165   } catch {
166     console.warn(
167       'Unable to get API Revision number, defaulting to zero. Some features may not work properly.'
168     );
169     return 0;
170   }
171 };
172
173 const removeTrailingSlashes = (
174   config: ClusterConfigJSON
175 ): ClusterConfigJSON => {
176   const svcs: any = {};
177   Object.keys(config.Services).forEach((s) => {
178     svcs[s] = config.Services[s];
179     if (svcs[s].hasOwnProperty('ExternalURL')) {
180       svcs[s].ExternalURL = svcs[s].ExternalURL.replace(/\/+$/, '');
181     }
182   });
183   return { ...config, Services: svcs };
184 };
185
186 export const fetchConfig = () => {
187   return Axios.get<WorkbenchConfig>(
188     WORKBENCH_CONFIG_URL + '?nocache=' + new Date().getTime()
189   )
190     .then((response) => response.data)
191     .catch(() => {
192       console.warn(
193         `There was an exception getting the Workbench config file at ${WORKBENCH_CONFIG_URL}. Using defaults instead.`
194       );
195       return Promise.resolve(getDefaultConfig());
196     })
197     .then((workbenchConfig) => {
198       if (workbenchConfig.API_HOST === undefined) {
199         throw new Error(
200           `Unable to start Workbench. API_HOST is undefined in ${WORKBENCH_CONFIG_URL} or the environment.`
201         );
202       }
203       return Axios.get<ClusterConfigJSON>(
204         getClusterConfigURL(workbenchConfig.API_HOST)
205       ).then(async (response) => {
206         const apiRevision = await getApiRevision(
207           response.data.Services.Controller.ExternalURL.replace(/\/+$/, '')
208         );
209         const config = { ...buildConfig(response.data), apiRevision };
210         const warnLocalConfig = (varName: string) =>
211           console.warn(
212             `A value for ${varName} was found in ${WORKBENCH_CONFIG_URL}. To use the Arvados centralized configuration instead, \
213 remove the entire ${varName} entry from ${WORKBENCH_CONFIG_URL}`
214           );
215
216         // Check if the workbench config has an entry for vocabulary and file viewer URLs
217         // If so, use these values (even if it is an empty string), but print a console warning.
218         // Otherwise, use the cluster config.
219         let fileViewerConfigUrl;
220         if (workbenchConfig.FILE_VIEWERS_CONFIG_URL !== undefined) {
221           warnLocalConfig('FILE_VIEWERS_CONFIG_URL');
222           fileViewerConfigUrl = workbenchConfig.FILE_VIEWERS_CONFIG_URL;
223         } else {
224           fileViewerConfigUrl =
225             config.clusterConfig.Workbench.FileViewersConfigURL ||
226             '/file-viewers-example.json';
227         }
228         config.fileViewersConfigUrl = fileViewerConfigUrl;
229
230         if (workbenchConfig.VOCABULARY_URL !== undefined) {
231           console.warn(
232             `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.`
233           );
234         }
235         config.vocabularyUrl = getVocabularyURL(workbenchConfig.API_HOST);
236
237         return { config, apiHost: workbenchConfig.API_HOST };
238       });
239     });
240 };
241
242 // Maps remote cluster hosts and removes the default RemoteCluster entry
243 export const mapRemoteHosts = (
244   clusterConfigJSON: ClusterConfigJSON,
245   config: Config
246 ) => {
247   config.remoteHosts = {};
248   Object.keys(clusterConfigJSON.RemoteClusters).forEach((k) => {
249     config.remoteHosts[k] = clusterConfigJSON.RemoteClusters[k].Host;
250   });
251   delete config.remoteHosts['*'];
252 };
253
254 export const mockClusterConfigJSON = (
255   config: Partial<ClusterConfigJSON>
256 ): ClusterConfigJSON => ({
257   API: {
258     UnfreezeProjectRequiresAdmin: false,
259     MaxItemsPerResponse: 1000,
260   },
261   ClusterID: '',
262   RemoteClusters: {},
263   Services: {
264     Controller: { ExternalURL: '' },
265     Workbench1: { ExternalURL: '' },
266     Workbench2: { ExternalURL: '' },
267     Websocket: { ExternalURL: '' },
268     WebDAV: { ExternalURL: '' },
269     WebDAVDownload: { ExternalURL: '' },
270     WebShell: { ExternalURL: '' },
271   },
272   Workbench: {
273     DisableSharingURLsUI: false,
274     ArvadosDocsite: '',
275     FileViewersConfigURL: '',
276     WelcomePageHTML: '',
277     InactivePageHTML: '',
278     SSHHelpPageHTML: '',
279     SSHHelpHostSuffix: '',
280     SiteName: '',
281     IdleTimeout: '0s',
282   },
283   Login: {
284     LoginCluster: '',
285     Google: {
286       Enable: false,
287     },
288     LDAP: {
289       Enable: false,
290     },
291     OpenIDConnect: {
292       Enable: false,
293     },
294     PAM: {
295       Enable: false,
296     },
297     SSO: {
298       Enable: false,
299     },
300     Test: {
301       Enable: false,
302     },
303   },
304   Collections: {
305     ForwardSlashNameSubstitution: '',
306     TrustAllContent: false,
307   },
308   Volumes: {},
309   ...config,
310 });
311
312 export const mockConfig = (config: Partial<Config>): Config => ({
313   baseUrl: '',
314   keepWebServiceUrl: '',
315   keepWebInlineServiceUrl: '',
316   remoteHosts: {},
317   rootUrl: '',
318   uuidPrefix: '',
319   websocketUrl: '',
320   workbenchUrl: '',
321   workbench2Url: '',
322   vocabularyUrl: '',
323   fileViewersConfigUrl: '',
324   loginCluster: '',
325   clusterConfig: mockClusterConfigJSON({}),
326   apiRevision: 0,
327   ...config,
328 });
329
330 const getDefaultConfig = (): WorkbenchConfig => {
331   let apiHost = '';
332   const envHost = process.env.REACT_APP_ARVADOS_API_HOST;
333   if (envHost !== undefined) {
334     console.warn(`Using default API host ${envHost}.`);
335     apiHost = envHost;
336   } else {
337     console.warn(
338       `No API host was found in the environment. Workbench may not be able to communicate with Arvados components.`
339     );
340   }
341   return {
342     API_HOST: apiHost,
343     VOCABULARY_URL: undefined,
344     FILE_VIEWERS_CONFIG_URL: undefined,
345   };
346 };
347
348 export const ARVADOS_API_PATH = 'arvados/v1';
349 export const CLUSTER_CONFIG_PATH = 'arvados/v1/config';
350 export const VOCABULARY_PATH = 'arvados/v1/vocabulary';
351 export const DISCOVERY_DOC_PATH = 'discovery/v1/apis/arvados/v1/rest';
352 export const getClusterConfigURL = (apiHost: string) =>
353   `https://${apiHost}/${CLUSTER_CONFIG_PATH}?nocache=${new Date().getTime()}`;
354 export const getVocabularyURL = (apiHost: string) =>
355   `https://${apiHost}/${VOCABULARY_PATH}?nocache=${new Date().getTime()}`;