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