14990: added 404 page with wildcard route
[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     };
62     Login: {
63         LoginCluster: string;
64         Google: {
65             Enable: boolean;
66         }
67         LDAP: {
68             Enable: boolean;
69         }
70         OpenIDConnect: {
71             Enable: boolean;
72         }
73         PAM: {
74             Enable: boolean;
75         }
76         SSO: {
77             Enable: boolean;
78         }
79     };
80     Collections: {
81         ForwardSlashNameSubstitution: string;
82     };
83 }
84
85 export class Config {
86     baseUrl: string;
87     keepWebServiceUrl: string;
88     remoteHosts: {
89         [key: string]: string
90     };
91     rootUrl: string;
92     uuidPrefix: string;
93     websocketUrl: string;
94     workbenchUrl: string;
95     workbench2Url: string;
96     vocabularyUrl: string;
97     fileViewersConfigUrl: string;
98     loginCluster: string;
99     clusterConfig: ClusterConfigJSON;
100     apiRevision: number;
101 }
102
103 export const buildConfig = (clusterConfigJSON: ClusterConfigJSON): Config => {
104     const config = new Config();
105     config.rootUrl = clusterConfigJSON.Services.Controller.ExternalURL;
106     config.baseUrl = `${config.rootUrl}/${ARVADOS_API_PATH}`;
107     config.uuidPrefix = clusterConfigJSON.ClusterID;
108     config.websocketUrl = clusterConfigJSON.Services.Websocket.ExternalURL;
109     config.workbench2Url = clusterConfigJSON.Services.Workbench2.ExternalURL;
110     config.workbenchUrl = clusterConfigJSON.Services.Workbench1.ExternalURL;
111     config.keepWebServiceUrl = clusterConfigJSON.Services.WebDAVDownload.ExternalURL;
112     config.loginCluster = clusterConfigJSON.Login.LoginCluster;
113     config.clusterConfig = clusterConfigJSON;
114     config.apiRevision = 0;
115     mapRemoteHosts(clusterConfigJSON, config);
116     return config;
117 };
118
119 const getApiRevision = async (apiUrl: string) => {
120     try {
121         const dd = (await Axios.get<any>(`${apiUrl}/${DISCOVERY_DOC_PATH}`)).data;
122         return parseInt(dd.revision, 10) || 0;
123     } catch {
124         console.warn("Unable to get API Revision number, defaulting to zero. Some features may not work properly.");
125         return 0;
126     }
127 };
128
129 const removeTrailingSlashes = (config: ClusterConfigJSON): ClusterConfigJSON => {
130     const svcs: any = {};
131     Object.keys(config.Services).map((s) => {
132         svcs[s] = config.Services[s];
133         if (svcs[s].hasOwnProperty('ExternalURL')) {
134             svcs[s].ExternalURL = svcs[s].ExternalURL.replace(/\/+$/, '');
135         }
136     });
137     return {...config, Services: svcs};
138 };
139
140 export const fetchConfig = () => {
141     return Axios
142         .get<WorkbenchConfig>(WORKBENCH_CONFIG_URL + "?nocache=" + (new Date()).getTime())
143         .then(response => response.data)
144         .catch(() => {
145             console.warn(`There was an exception getting the Workbench config file at ${WORKBENCH_CONFIG_URL}. Using defaults instead.`);
146             return Promise.resolve(getDefaultConfig());
147         })
148         .then(workbenchConfig => {
149             if (workbenchConfig.API_HOST === undefined) {
150                 throw new Error(`Unable to start Workbench. API_HOST is undefined in ${WORKBENCH_CONFIG_URL} or the environment.`);
151             }
152             return Axios.get<ClusterConfigJSON>(getClusterConfigURL(workbenchConfig.API_HOST)).then(async response => {
153                 const clusterConfigJSON = removeTrailingSlashes(response.data);
154                 const apiRevision = await getApiRevision(clusterConfigJSON.Services.Controller.ExternalURL);
155                 const config = { ...buildConfig(clusterConfigJSON), apiRevision };
156                 const warnLocalConfig = (varName: string) => console.warn(
157                     `A value for ${varName} was found in ${WORKBENCH_CONFIG_URL}. To use the Arvados centralized configuration instead, \
158 remove the entire ${varName} entry from ${WORKBENCH_CONFIG_URL}`);
159
160                 // Check if the workbench config has an entry for vocabulary and file viewer URLs
161                 // If so, use these values (even if it is an empty string), but print a console warning.
162                 // Otherwise, use the cluster config.
163                 let fileViewerConfigUrl;
164                 if (workbenchConfig.FILE_VIEWERS_CONFIG_URL !== undefined) {
165                     warnLocalConfig("FILE_VIEWERS_CONFIG_URL");
166                     fileViewerConfigUrl = workbenchConfig.FILE_VIEWERS_CONFIG_URL;
167                 }
168                 else {
169                     fileViewerConfigUrl = clusterConfigJSON.Workbench.FileViewersConfigURL || "/file-viewers-example.json";
170                 }
171                 config.fileViewersConfigUrl = fileViewerConfigUrl;
172
173                 let vocabularyUrl;
174                 if (workbenchConfig.VOCABULARY_URL !== undefined) {
175                     warnLocalConfig("VOCABULARY_URL");
176                     vocabularyUrl = workbenchConfig.VOCABULARY_URL;
177                 }
178                 else {
179                     vocabularyUrl = clusterConfigJSON.Workbench.VocabularyURL || "/vocabulary-example.json";
180                 }
181                 config.vocabularyUrl = vocabularyUrl;
182
183                 return { config, apiHost: workbenchConfig.API_HOST };
184             });
185         });
186 };
187
188 // Maps remote cluster hosts and removes the default RemoteCluster entry
189 export const mapRemoteHosts = (clusterConfigJSON: ClusterConfigJSON, config: Config) => {
190     config.remoteHosts = {};
191     Object.keys(clusterConfigJSON.RemoteClusters).forEach(k => { config.remoteHosts[k] = clusterConfigJSON.RemoteClusters[k].Host; });
192     delete config.remoteHosts["*"];
193 };
194
195 export const mockClusterConfigJSON = (config: Partial<ClusterConfigJSON>): ClusterConfigJSON => ({
196     ClusterID: "",
197     RemoteClusters: {},
198     Services: {
199         Controller: { ExternalURL: "" },
200         Workbench1: { ExternalURL: "" },
201         Workbench2: { ExternalURL: "" },
202         Websocket: { ExternalURL: "" },
203         WebDAV: { ExternalURL: "" },
204         WebDAVDownload: { ExternalURL: "" },
205         WebShell: { ExternalURL: "" },
206     },
207     Workbench: {
208         ArvadosDocsite: "",
209         VocabularyURL: "",
210         FileViewersConfigURL: "",
211         WelcomePageHTML: "",
212         InactivePageHTML: "",
213         SSHHelpPageHTML: "",
214         SSHHelpHostSuffix: "",
215         SiteName: "",
216     },
217     Login: {
218         LoginCluster: "",
219         Google: {
220             Enable: false,
221         },
222         LDAP: {
223             Enable: false,
224         },
225         OpenIDConnect: {
226             Enable: false,
227         },
228         PAM: {
229             Enable: false,
230         },
231         SSO: {
232             Enable: false,
233         },
234     },
235     Collections: {
236         ForwardSlashNameSubstitution: "",
237     },
238     ...config
239 });
240
241 export const mockConfig = (config: Partial<Config>): Config => ({
242     baseUrl: "",
243     keepWebServiceUrl: "",
244     remoteHosts: {},
245     rootUrl: "",
246     uuidPrefix: "",
247     websocketUrl: "",
248     workbenchUrl: "",
249     workbench2Url: "",
250     vocabularyUrl: "",
251     fileViewersConfigUrl: "",
252     loginCluster: "",
253     clusterConfig: mockClusterConfigJSON({}),
254     apiRevision: 0,
255     ...config
256 });
257
258 const getDefaultConfig = (): WorkbenchConfig => {
259     let apiHost = "";
260     const envHost = process.env.REACT_APP_ARVADOS_API_HOST;
261     if (envHost !== undefined) {
262         console.warn(`Using default API host ${envHost}.`);
263         apiHost = envHost;
264     }
265     else {
266         console.warn(`No API host was found in the environment. Workbench may not be able to communicate with Arvados components.`);
267     }
268     return {
269         API_HOST: apiHost,
270         VOCABULARY_URL: undefined,
271         FILE_VIEWERS_CONFIG_URL: undefined,
272     };
273 };
274
275 export const ARVADOS_API_PATH = "arvados/v1";
276 export const CLUSTER_CONFIG_PATH = "arvados/v1/config";
277 export const DISCOVERY_DOC_PATH = "discovery/v1/apis/arvados/v1/rest";
278 export const getClusterConfigURL = (apiHost: string) => `${window.location.protocol}//${apiHost}/${CLUSTER_CONFIG_PATH}?nocache=${(new Date()).getTime()}`;