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