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