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