56f7c4884b19542ee057b937b5ff420809477a9e
[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         ArvadosDocsite: string;
54         VocabularyURL: 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                 let vocabularyUrl;
208                 if (workbenchConfig.VOCABULARY_URL !== undefined) {
209                     warnLocalConfig("VOCABULARY_URL");
210                     vocabularyUrl = workbenchConfig.VOCABULARY_URL;
211                 }
212                 else {
213                     vocabularyUrl = config.clusterConfig.Workbench.VocabularyURL || "/vocabulary-example.json";
214                 }
215                 config.vocabularyUrl = vocabularyUrl;
216
217                 return { config, apiHost: workbenchConfig.API_HOST };
218             });
219         });
220 };
221
222 // Maps remote cluster hosts and removes the default RemoteCluster entry
223 export const mapRemoteHosts = (clusterConfigJSON: ClusterConfigJSON, config: Config) => {
224     config.remoteHosts = {};
225     Object.keys(clusterConfigJSON.RemoteClusters).forEach(k => { config.remoteHosts[k] = clusterConfigJSON.RemoteClusters[k].Host; });
226     delete config.remoteHosts["*"];
227 };
228
229 export const mockClusterConfigJSON = (config: Partial<ClusterConfigJSON>): ClusterConfigJSON => ({
230     ClusterID: "",
231     RemoteClusters: {},
232     Services: {
233         Controller: { ExternalURL: "" },
234         Workbench1: { ExternalURL: "" },
235         Workbench2: { ExternalURL: "" },
236         Websocket: { ExternalURL: "" },
237         WebDAV: { ExternalURL: "" },
238         WebDAVDownload: { ExternalURL: "" },
239         WebShell: { ExternalURL: "" },
240     },
241     Workbench: {
242         ArvadosDocsite: "",
243         VocabularyURL: "",
244         FileViewersConfigURL: "",
245         WelcomePageHTML: "",
246         InactivePageHTML: "",
247         SSHHelpPageHTML: "",
248         SSHHelpHostSuffix: "",
249         SiteName: "",
250         IdleTimeout: "0s",
251     },
252     Login: {
253         LoginCluster: "",
254         Google: {
255             Enable: false,
256         },
257         LDAP: {
258             Enable: false,
259         },
260         OpenIDConnect: {
261             Enable: false,
262         },
263         PAM: {
264             Enable: false,
265         },
266         SSO: {
267             Enable: false,
268         },
269         Test: {
270             Enable: false,
271         },
272     },
273     Collections: {
274         ForwardSlashNameSubstitution: "",
275         TrustAllContent: false,
276     },
277     Volumes: {},
278     ...config
279 });
280
281 export const mockConfig = (config: Partial<Config>): Config => ({
282     baseUrl: "",
283     keepWebServiceUrl: "",
284     keepWebInlineServiceUrl: "",
285     remoteHosts: {},
286     rootUrl: "",
287     uuidPrefix: "",
288     websocketUrl: "",
289     workbenchUrl: "",
290     workbench2Url: "",
291     vocabularyUrl: "",
292     fileViewersConfigUrl: "",
293     loginCluster: "",
294     clusterConfig: mockClusterConfigJSON({}),
295     apiRevision: 0,
296     ...config
297 });
298
299 const getDefaultConfig = (): WorkbenchConfig => {
300     let apiHost = "";
301     const envHost = process.env.REACT_APP_ARVADOS_API_HOST;
302     if (envHost !== undefined) {
303         console.warn(`Using default API host ${envHost}.`);
304         apiHost = envHost;
305     }
306     else {
307         console.warn(`No API host was found in the environment. Workbench may not be able to communicate with Arvados components.`);
308     }
309     return {
310         API_HOST: apiHost,
311         VOCABULARY_URL: undefined,
312         FILE_VIEWERS_CONFIG_URL: undefined,
313     };
314 };
315
316 export const ARVADOS_API_PATH = "arvados/v1";
317 export const CLUSTER_CONFIG_PATH = "arvados/v1/config";
318 export const DISCOVERY_DOC_PATH = "discovery/v1/apis/arvados/v1/rest";
319 export const getClusterConfigURL = (apiHost: string) => `${window.location.protocol}//${apiHost}/${CLUSTER_CONFIG_PATH}?nocache=${(new Date()).getTime()}`;