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