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