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