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