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