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