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