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