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;
23 ActivateUsers: boolean;
31 SupportEmailAddress: string;
57 DisableSharingURLsUI: boolean;
58 ArvadosDocsite: string;
59 FileViewersConfigURL: string;
60 WelcomePageHTML: string;
61 InactivePageHTML: string;
62 SSHHelpPageHTML: string;
63 SSHHelpHostSuffix: string;
89 ForwardSlashNameSubstitution: string;
97 TrustAllContent: boolean;
102 [key: string]: boolean;
108 export class Config {
110 keepWebServiceUrl!: string;
111 keepWebInlineServiceUrl!: string;
113 [key: string]: string;
117 websocketUrl!: string;
118 workbenchUrl!: string;
119 workbench2Url!: string;
120 vocabularyUrl!: string;
121 fileViewersConfigUrl!: string;
122 loginCluster!: string;
123 clusterConfig!: ClusterConfigJSON;
124 apiRevision!: number;
127 export const buildConfig = (clusterConfig: ClusterConfigJSON): Config => {
128 const clusterConfigJSON = removeTrailingSlashes(clusterConfig);
129 const config = new Config();
130 config.rootUrl = clusterConfigJSON.Services.Controller.ExternalURL;
131 config.baseUrl = `${config.rootUrl}/${ARVADOS_API_PATH}`;
132 config.uuidPrefix = clusterConfigJSON.ClusterID;
133 config.websocketUrl = clusterConfigJSON.Services.Websocket.ExternalURL;
134 config.workbench2Url = clusterConfigJSON.Services.Workbench2.ExternalURL;
135 config.workbenchUrl = clusterConfigJSON.Services.Workbench1.ExternalURL;
136 config.keepWebServiceUrl =
137 clusterConfigJSON.Services.WebDAVDownload.ExternalURL;
138 config.keepWebInlineServiceUrl =
139 clusterConfigJSON.Services.WebDAV.ExternalURL;
140 config.loginCluster = clusterConfigJSON.Login.LoginCluster;
141 config.clusterConfig = clusterConfigJSON;
142 config.apiRevision = 0;
143 mapRemoteHosts(clusterConfigJSON, config);
147 export const getStorageClasses = (config: Config): string[] => {
148 const classes: Set<string> = new Set(['default']);
149 const volumes = config.clusterConfig.Volumes;
150 Object.keys(volumes).forEach((v) => {
151 Object.keys(volumes[v].StorageClasses || {}).forEach((sc) => {
152 if (volumes[v].StorageClasses[sc]) {
157 return Array.from(classes);
160 const getApiRevision = async (apiUrl: string) => {
162 const dd = (await Axios.get<any>(`${apiUrl}/${DISCOVERY_DOC_PATH}`)).data;
163 return parseInt(dd.revision, 10) || 0;
166 'Unable to get API Revision number, defaulting to zero. Some features may not work properly.'
172 const removeTrailingSlashes = (
173 config: ClusterConfigJSON
174 ): ClusterConfigJSON => {
175 const svcs: any = {};
176 Object.keys(config.Services).forEach((s) => {
177 svcs[s] = config.Services[s];
178 if (svcs[s].hasOwnProperty('ExternalURL')) {
179 svcs[s].ExternalURL = svcs[s].ExternalURL.replace(/\/+$/, '');
182 return { ...config, Services: svcs };
185 export const fetchConfig = () => {
186 return Axios.get<WorkbenchConfig>(
187 WORKBENCH_CONFIG_URL + '?nocache=' + new Date().getTime()
189 .then((response) => response.data)
192 `There was an exception getting the Workbench config file at ${WORKBENCH_CONFIG_URL}. Using defaults instead.`
194 return Promise.resolve(getDefaultConfig());
196 .then((workbenchConfig) => {
197 if (workbenchConfig.API_HOST === undefined) {
199 `Unable to start Workbench. API_HOST is undefined in ${WORKBENCH_CONFIG_URL} or the environment.`
202 return Axios.get<ClusterConfigJSON>(
203 getClusterConfigURL(workbenchConfig.API_HOST)
204 ).then(async (response) => {
205 const apiRevision = await getApiRevision(
206 response.data.Services.Controller.ExternalURL.replace(/\/+$/, '')
208 const config = { ...buildConfig(response.data), apiRevision };
209 const warnLocalConfig = (varName: string) =>
211 `A value for ${varName} was found in ${WORKBENCH_CONFIG_URL}. To use the Arvados centralized configuration instead, \
212 remove the entire ${varName} entry from ${WORKBENCH_CONFIG_URL}`
215 // Check if the workbench config has an entry for vocabulary and file viewer URLs
216 // If so, use these values (even if it is an empty string), but print a console warning.
217 // Otherwise, use the cluster config.
218 let fileViewerConfigUrl;
219 if (workbenchConfig.FILE_VIEWERS_CONFIG_URL !== undefined) {
220 warnLocalConfig('FILE_VIEWERS_CONFIG_URL');
221 fileViewerConfigUrl = workbenchConfig.FILE_VIEWERS_CONFIG_URL;
223 fileViewerConfigUrl =
224 config.clusterConfig.Workbench.FileViewersConfigURL ||
225 '/file-viewers-example.json';
227 config.fileViewersConfigUrl = fileViewerConfigUrl;
229 if (workbenchConfig.VOCABULARY_URL !== undefined) {
231 `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.`
234 config.vocabularyUrl = getVocabularyURL(workbenchConfig.API_HOST);
236 return { config, apiHost: workbenchConfig.API_HOST };
241 // Maps remote cluster hosts and removes the default RemoteCluster entry
242 export const mapRemoteHosts = (
243 clusterConfigJSON: ClusterConfigJSON,
246 config.remoteHosts = {};
247 Object.keys(clusterConfigJSON.RemoteClusters).forEach((k) => {
248 config.remoteHosts[k] = clusterConfigJSON.RemoteClusters[k].Host;
250 delete config.remoteHosts['*'];
253 export const mockClusterConfigJSON = (
254 config: Partial<ClusterConfigJSON>
255 ): ClusterConfigJSON => ({
257 UnfreezeProjectRequiresAdmin: false,
262 Controller: { ExternalURL: '' },
263 Workbench1: { ExternalURL: '' },
264 Workbench2: { ExternalURL: '' },
265 Websocket: { ExternalURL: '' },
266 WebDAV: { ExternalURL: '' },
267 WebDAVDownload: { ExternalURL: '' },
268 WebShell: { ExternalURL: '' },
271 DisableSharingURLsUI: false,
273 FileViewersConfigURL: '',
275 InactivePageHTML: '',
277 SSHHelpHostSuffix: '',
303 ForwardSlashNameSubstitution: '',
304 TrustAllContent: false,
310 export const mockConfig = (config: Partial<Config>): Config => ({
312 keepWebServiceUrl: '',
313 keepWebInlineServiceUrl: '',
321 fileViewersConfigUrl: '',
323 clusterConfig: mockClusterConfigJSON({}),
328 const getDefaultConfig = (): WorkbenchConfig => {
330 const envHost = process.env.REACT_APP_ARVADOS_API_HOST;
331 if (envHost !== undefined) {
332 console.warn(`Using default API host ${envHost}.`);
336 `No API host was found in the environment. Workbench may not be able to communicate with Arvados components.`
341 VOCABULARY_URL: undefined,
342 FILE_VIEWERS_CONFIG_URL: undefined,
346 export const ARVADOS_API_PATH = 'arvados/v1';
347 export const CLUSTER_CONFIG_PATH = 'arvados/v1/config';
348 export const VOCABULARY_PATH = 'arvados/v1/vocabulary';
349 export const DISCOVERY_DOC_PATH = 'discovery/v1/apis/arvados/v1/rest';
350 export const getClusterConfigURL = (apiHost: string) =>
351 `https://${apiHost}/${CLUSTER_CONFIG_PATH}?nocache=${new Date().getTime()}`;
352 export const getVocabularyURL = (apiHost: string) =>
353 `https://${apiHost}/${VOCABULARY_PATH}?nocache=${new Date().getTime()}`;