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