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;
58 DisableSharingURLsUI: boolean;
59 ArvadosDocsite: string;
60 FileViewersConfigURL: string;
61 WelcomePageHTML: string;
62 InactivePageHTML: string;
63 SSHHelpPageHTML: string;
64 SSHHelpHostSuffix: string;
90 ForwardSlashNameSubstitution: string;
98 TrustAllContent: boolean;
103 [key: string]: boolean;
109 export class Config {
111 keepWebServiceUrl!: string;
112 keepWebInlineServiceUrl!: string;
114 [key: string]: string;
118 websocketUrl!: string;
119 workbenchUrl!: string;
120 workbench2Url!: string;
121 vocabularyUrl!: string;
122 fileViewersConfigUrl!: string;
123 loginCluster!: string;
124 clusterConfig!: ClusterConfigJSON;
125 apiRevision!: number;
128 export const buildConfig = (clusterConfig: ClusterConfigJSON): Config => {
129 const clusterConfigJSON = removeTrailingSlashes(clusterConfig);
130 const config = new Config();
131 config.rootUrl = clusterConfigJSON.Services.Controller.ExternalURL;
132 config.baseUrl = `${config.rootUrl}/${ARVADOS_API_PATH}`;
133 config.uuidPrefix = clusterConfigJSON.ClusterID;
134 config.websocketUrl = clusterConfigJSON.Services.Websocket.ExternalURL;
135 config.workbench2Url = clusterConfigJSON.Services.Workbench2.ExternalURL;
136 config.workbenchUrl = clusterConfigJSON.Services.Workbench1.ExternalURL;
137 config.keepWebServiceUrl =
138 clusterConfigJSON.Services.WebDAVDownload.ExternalURL;
139 config.keepWebInlineServiceUrl =
140 clusterConfigJSON.Services.WebDAV.ExternalURL;
141 config.loginCluster = clusterConfigJSON.Login.LoginCluster;
142 config.clusterConfig = clusterConfigJSON;
143 config.apiRevision = 0;
144 mapRemoteHosts(clusterConfigJSON, config);
148 export const getStorageClasses = (config: Config): string[] => {
149 const classes: Set<string> = new Set(['default']);
150 const volumes = config.clusterConfig.Volumes;
151 Object.keys(volumes).forEach((v) => {
152 Object.keys(volumes[v].StorageClasses || {}).forEach((sc) => {
153 if (volumes[v].StorageClasses[sc]) {
158 return Array.from(classes);
161 const getApiRevision = async (apiUrl: string) => {
163 const dd = (await Axios.get<any>(`${apiUrl}/${DISCOVERY_DOC_PATH}`)).data;
164 return parseInt(dd.revision, 10) || 0;
167 'Unable to get API Revision number, defaulting to zero. Some features may not work properly.'
173 const removeTrailingSlashes = (
174 config: ClusterConfigJSON
175 ): ClusterConfigJSON => {
176 const svcs: any = {};
177 Object.keys(config.Services).forEach((s) => {
178 svcs[s] = config.Services[s];
179 if (svcs[s].hasOwnProperty('ExternalURL')) {
180 svcs[s].ExternalURL = svcs[s].ExternalURL.replace(/\/+$/, '');
183 return { ...config, Services: svcs };
186 export const fetchConfig = () => {
187 return Axios.get<WorkbenchConfig>(
188 WORKBENCH_CONFIG_URL + '?nocache=' + new Date().getTime()
190 .then((response) => response.data)
193 `There was an exception getting the Workbench config file at ${WORKBENCH_CONFIG_URL}. Using defaults instead.`
195 return Promise.resolve(getDefaultConfig());
197 .then((workbenchConfig) => {
198 if (workbenchConfig.API_HOST === undefined) {
200 `Unable to start Workbench. API_HOST is undefined in ${WORKBENCH_CONFIG_URL} or the environment.`
203 return Axios.get<ClusterConfigJSON>(
204 getClusterConfigURL(workbenchConfig.API_HOST)
205 ).then(async (response) => {
206 const apiRevision = await getApiRevision(
207 response.data.Services.Controller.ExternalURL.replace(/\/+$/, '')
209 const config = { ...buildConfig(response.data), apiRevision };
210 const warnLocalConfig = (varName: string) =>
212 `A value for ${varName} was found in ${WORKBENCH_CONFIG_URL}. To use the Arvados centralized configuration instead, \
213 remove the entire ${varName} entry from ${WORKBENCH_CONFIG_URL}`
216 // Check if the workbench config has an entry for vocabulary and file viewer URLs
217 // If so, use these values (even if it is an empty string), but print a console warning.
218 // Otherwise, use the cluster config.
219 let fileViewerConfigUrl;
220 if (workbenchConfig.FILE_VIEWERS_CONFIG_URL !== undefined) {
221 warnLocalConfig('FILE_VIEWERS_CONFIG_URL');
222 fileViewerConfigUrl = workbenchConfig.FILE_VIEWERS_CONFIG_URL;
224 fileViewerConfigUrl =
225 config.clusterConfig.Workbench.FileViewersConfigURL ||
226 '/file-viewers-example.json';
228 config.fileViewersConfigUrl = fileViewerConfigUrl;
230 if (workbenchConfig.VOCABULARY_URL !== undefined) {
232 `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.`
235 config.vocabularyUrl = getVocabularyURL(workbenchConfig.API_HOST);
237 return { config, apiHost: workbenchConfig.API_HOST };
242 // Maps remote cluster hosts and removes the default RemoteCluster entry
243 export const mapRemoteHosts = (
244 clusterConfigJSON: ClusterConfigJSON,
247 config.remoteHosts = {};
248 Object.keys(clusterConfigJSON.RemoteClusters).forEach((k) => {
249 config.remoteHosts[k] = clusterConfigJSON.RemoteClusters[k].Host;
251 delete config.remoteHosts['*'];
254 export const mockClusterConfigJSON = (
255 config: Partial<ClusterConfigJSON>
256 ): ClusterConfigJSON => ({
258 UnfreezeProjectRequiresAdmin: false,
259 MaxItemsPerResponse: 1000,
264 Controller: { ExternalURL: '' },
265 Workbench1: { ExternalURL: '' },
266 Workbench2: { ExternalURL: '' },
267 Websocket: { ExternalURL: '' },
268 WebDAV: { ExternalURL: '' },
269 WebDAVDownload: { ExternalURL: '' },
270 WebShell: { ExternalURL: '' },
273 DisableSharingURLsUI: false,
275 FileViewersConfigURL: '',
277 InactivePageHTML: '',
279 SSHHelpHostSuffix: '',
305 ForwardSlashNameSubstitution: '',
306 TrustAllContent: false,
312 export const mockConfig = (config: Partial<Config>): Config => ({
314 keepWebServiceUrl: '',
315 keepWebInlineServiceUrl: '',
323 fileViewersConfigUrl: '',
325 clusterConfig: mockClusterConfigJSON({}),
330 const getDefaultConfig = (): WorkbenchConfig => {
332 const envHost = process.env.REACT_APP_ARVADOS_API_HOST;
333 if (envHost !== undefined) {
334 console.warn(`Using default API host ${envHost}.`);
338 `No API host was found in the environment. Workbench may not be able to communicate with Arvados components.`
343 VOCABULARY_URL: undefined,
344 FILE_VIEWERS_CONFIG_URL: undefined,
348 export const ARVADOS_API_PATH = 'arvados/v1';
349 export const CLUSTER_CONFIG_PATH = 'arvados/v1/config';
350 export const VOCABULARY_PATH = 'arvados/v1/vocabulary';
351 export const DISCOVERY_DOC_PATH = 'discovery/v1/apis/arvados/v1/rest';
352 export const getClusterConfigURL = (apiHost: string) =>
353 `https://${apiHost}/${CLUSTER_CONFIG_PATH}?nocache=${new Date().getTime()}`;
354 export const getVocabularyURL = (apiHost: string) =>
355 `https://${apiHost}/${VOCABULARY_PATH}?nocache=${new Date().getTime()}`;