1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import Axios from 'axios';
7 export const WORKBENCH_CONFIG_URL =
8 process.env.REACT_APP_ARVADOS_CONFIG_URL || '/config.json';
10 interface WorkbenchConfig {
12 VOCABULARY_URL?: string;
13 FILE_VIEWERS_CONFIG_URL?: string;
16 export interface ClusterConfigJSON {
18 UnfreezeProjectRequiresAdmin: boolean
19 MaxItemsPerResponse: number
24 ActivateUsers: boolean
32 SupportEmailAddress: string;
45 DisableSharingURLsUI: boolean;
46 ArvadosDocsite: string;
47 FileViewersConfigURL: string;
48 WelcomePageHTML: string;
49 InactivePageHTML: string;
50 SSHHelpPageHTML: string;
51 SSHHelpHostSuffix: string;
69 DisableSharingURLsUI: boolean;
70 ArvadosDocsite: string;
71 FileViewersConfigURL: string;
72 WelcomePageHTML: string;
73 InactivePageHTML: string;
74 SSHHelpPageHTML: string;
75 SSHHelpHostSuffix: string;
79 UserProfileFormFields: {};
80 UserProfileFormMessage: string;
104 ForwardSlashNameSubstitution: string;
105 ManagedProperties?: {
112 TrustAllContent: boolean;
117 [key: string]: boolean;
122 AnonymousUserToken: string;
126 export class Config {
128 keepWebServiceUrl!: string;
129 keepWebInlineServiceUrl!: string;
131 [key: string]: string;
135 websocketUrl!: string;
136 workbenchUrl!: string;
137 workbench2Url!: string;
138 vocabularyUrl!: string;
139 fileViewersConfigUrl!: string;
140 loginCluster!: string;
141 clusterConfig!: ClusterConfigJSON;
142 apiRevision!: number;
145 export const buildConfig = (clusterConfig: ClusterConfigJSON): Config => {
146 const clusterConfigJSON = removeTrailingSlashes(clusterConfig);
147 const config = new Config();
148 config.rootUrl = clusterConfigJSON.Services.Controller.ExternalURL;
149 config.baseUrl = `${config.rootUrl}/${ARVADOS_API_PATH}`;
150 config.uuidPrefix = clusterConfigJSON.ClusterID;
151 config.websocketUrl = clusterConfigJSON.Services.Websocket.ExternalURL;
152 config.workbench2Url = clusterConfigJSON.Services.Workbench2.ExternalURL;
153 config.workbenchUrl = clusterConfigJSON.Services.Workbench1.ExternalURL;
154 config.keepWebServiceUrl =
155 clusterConfigJSON.Services.WebDAVDownload.ExternalURL;
156 config.keepWebInlineServiceUrl =
157 clusterConfigJSON.Services.WebDAV.ExternalURL;
158 config.loginCluster = clusterConfigJSON.Login.LoginCluster;
159 config.clusterConfig = clusterConfigJSON;
160 config.apiRevision = 0;
161 mapRemoteHosts(clusterConfigJSON, config);
165 export const getStorageClasses = (config: Config): string[] => {
166 const classes: Set<string> = new Set(['default']);
167 const volumes = config.clusterConfig.Volumes;
168 Object.keys(volumes).forEach((v) => {
169 Object.keys(volumes[v].StorageClasses || {}).forEach((sc) => {
170 if (volumes[v].StorageClasses[sc]) {
175 return Array.from(classes);
178 const getApiRevision = async (apiUrl: string) => {
180 const dd = (await Axios.get<any>(`${apiUrl}/${DISCOVERY_DOC_PATH}`)).data;
181 return parseInt(dd.revision, 10) || 0;
184 'Unable to get API Revision number, defaulting to zero. Some features may not work properly.'
190 const removeTrailingSlashes = (
191 config: ClusterConfigJSON
192 ): ClusterConfigJSON => {
193 const svcs: any = {};
194 Object.keys(config.Services).forEach((s) => {
195 svcs[s] = config.Services[s];
196 if (svcs[s].hasOwnProperty('ExternalURL')) {
197 svcs[s].ExternalURL = svcs[s].ExternalURL.replace(/\/+$/, '');
200 return { ...config, Services: svcs };
203 export const fetchConfig = () => {
204 return Axios.get<WorkbenchConfig>(
205 WORKBENCH_CONFIG_URL + '?nocache=' + new Date().getTime()
207 .then((response) => response.data)
210 `There was an exception getting the Workbench config file at ${WORKBENCH_CONFIG_URL}. Using defaults instead.`
212 return Promise.resolve(getDefaultConfig());
214 .then((workbenchConfig) => {
215 if (workbenchConfig.API_HOST === undefined) {
217 `Unable to start Workbench. API_HOST is undefined in ${WORKBENCH_CONFIG_URL} or the environment.`
220 return Axios.get<ClusterConfigJSON>(
221 getClusterConfigURL(workbenchConfig.API_HOST)
222 ).then(async (response) => {
223 const apiRevision = await getApiRevision(
224 response.data.Services.Controller.ExternalURL.replace(/\/+$/, '')
226 const config = { ...buildConfig(response.data), apiRevision };
227 const warnLocalConfig = (varName: string) =>
229 `A value for ${varName} was found in ${WORKBENCH_CONFIG_URL}. To use the Arvados centralized configuration instead, \
230 remove the entire ${varName} entry from ${WORKBENCH_CONFIG_URL}`
233 // Check if the workbench config has an entry for vocabulary and file viewer URLs
234 // If so, use these values (even if it is an empty string), but print a console warning.
235 // Otherwise, use the cluster config.
236 let fileViewerConfigUrl;
237 if (workbenchConfig.FILE_VIEWERS_CONFIG_URL !== undefined) {
238 warnLocalConfig('FILE_VIEWERS_CONFIG_URL');
239 fileViewerConfigUrl = workbenchConfig.FILE_VIEWERS_CONFIG_URL;
241 fileViewerConfigUrl =
242 config.clusterConfig.Workbench.FileViewersConfigURL ||
243 '/file-viewers-example.json';
245 config.fileViewersConfigUrl = fileViewerConfigUrl;
247 if (workbenchConfig.VOCABULARY_URL !== undefined) {
249 `A value for VOCABULARY_URL was found in ${WORKBENCH_CONFIG_URL}. It will be ignored as the cluster already provides its own endpoint, you can safely remove it.`
252 config.vocabularyUrl = getVocabularyURL(workbenchConfig.API_HOST);
254 return { config, apiHost: workbenchConfig.API_HOST };
259 // Maps remote cluster hosts and removes the default RemoteCluster entry
260 export const mapRemoteHosts = (
261 clusterConfigJSON: ClusterConfigJSON,
264 config.remoteHosts = {};
265 Object.keys(clusterConfigJSON.RemoteClusters).forEach((k) => {
266 config.remoteHosts[k] = clusterConfigJSON.RemoteClusters[k].Host;
268 delete config.remoteHosts['*'];
271 export const mockClusterConfigJSON = (
272 config: Partial<ClusterConfigJSON>
273 ): ClusterConfigJSON => ({
275 UnfreezeProjectRequiresAdmin: false,
276 MaxItemsPerResponse: 1000,
281 Controller: { ExternalURL: '' },
282 Workbench1: { ExternalURL: '' },
283 Workbench2: { ExternalURL: '' },
284 Websocket: { ExternalURL: '' },
285 WebDAV: { ExternalURL: '' },
286 WebDAVDownload: { ExternalURL: '' },
287 WebShell: { ExternalURL: '' },
289 DisableSharingURLsUI: false,
291 FileViewersConfigURL: "",
293 InactivePageHTML: "",
295 SSHHelpHostSuffix: "",
301 DisableSharingURLsUI: false,
303 FileViewersConfigURL: '',
305 InactivePageHTML: '',
307 SSHHelpHostSuffix: '',
311 UserProfileFormFields: {},
312 UserProfileFormMessage: '',
336 ForwardSlashNameSubstitution: '',
337 TrustAllContent: false,
341 AnonymousUserToken: ""
346 export const mockConfig = (config: Partial<Config>): Config => ({
348 keepWebServiceUrl: '',
349 keepWebInlineServiceUrl: '',
357 fileViewersConfigUrl: '',
359 clusterConfig: mockClusterConfigJSON({}),
364 const getDefaultConfig = (): WorkbenchConfig => {
366 const envHost = process.env.REACT_APP_ARVADOS_API_HOST;
367 if (envHost !== undefined) {
368 console.warn(`Using default API host ${envHost}.`);
372 `No API host was found in the environment. Workbench may not be able to communicate with Arvados components.`
377 VOCABULARY_URL: undefined,
378 FILE_VIEWERS_CONFIG_URL: undefined,
382 export const ARVADOS_API_PATH = 'arvados/v1';
383 export const CLUSTER_CONFIG_PATH = 'arvados/v1/config';
384 export const VOCABULARY_PATH = 'arvados/v1/vocabulary';
385 export const DISCOVERY_DOC_PATH = 'discovery/v1/apis/arvados/v1/rest';
386 export const getClusterConfigURL = (apiHost: string) =>
387 `https://${apiHost}/${CLUSTER_CONFIG_PATH}?nocache=${new Date().getTime()}`;
388 export const getVocabularyURL = (apiHost: string) =>
389 `https://${apiHost}/${VOCABULARY_PATH}?nocache=${new Date().getTime()}`;