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