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 = process.env.REACT_APP_ARVADOS_CONFIG_URL || "/config.json";
9 interface WorkbenchConfig {
11 VOCABULARY_URL?: string;
12 FILE_VIEWERS_CONFIG_URL?: string;
15 export interface ClusterConfigJSON {
17 UnfreezeProjectRequiresAdmin: boolean
22 ActivateUsers: boolean
30 SupportEmailAddress: string;
56 DisableSharingURLsUI: boolean;
57 ArvadosDocsite: string;
58 FileViewersConfigURL: string;
59 WelcomePageHTML: string;
60 InactivePageHTML: string;
61 SSHHelpPageHTML: string;
62 SSHHelpHostSuffix: string;
88 ForwardSlashNameSubstitution: string;
96 TrustAllContent: boolean
101 [key: string]: boolean;
107 export class Config {
109 keepWebServiceUrl!: string;
110 keepWebInlineServiceUrl!: string;
112 [key: string]: string
116 websocketUrl!: string;
117 workbenchUrl!: string;
118 workbench2Url!: string;
119 vocabularyUrl!: string;
120 fileViewersConfigUrl!: string;
121 loginCluster!: string;
122 clusterConfig!: ClusterConfigJSON;
123 apiRevision!: number;
126 export const buildConfig = (clusterConfig: ClusterConfigJSON): Config => {
127 const clusterConfigJSON = removeTrailingSlashes(clusterConfig);
128 const config = new Config();
129 config.rootUrl = clusterConfigJSON.Services.Controller.ExternalURL;
130 config.baseUrl = `${config.rootUrl}/${ARVADOS_API_PATH}`;
131 config.uuidPrefix = clusterConfigJSON.ClusterID;
132 config.websocketUrl = clusterConfigJSON.Services.Websocket.ExternalURL;
133 config.workbench2Url = clusterConfigJSON.Services.Workbench2.ExternalURL;
134 config.workbenchUrl = clusterConfigJSON.Services.Workbench1.ExternalURL;
135 config.keepWebServiceUrl = clusterConfigJSON.Services.WebDAVDownload.ExternalURL;
136 config.keepWebInlineServiceUrl = clusterConfigJSON.Services.WebDAV.ExternalURL;
137 config.loginCluster = clusterConfigJSON.Login.LoginCluster;
138 config.clusterConfig = clusterConfigJSON;
139 config.apiRevision = 0;
140 mapRemoteHosts(clusterConfigJSON, config);
144 export const getStorageClasses = (config: Config): string[] => {
145 const classes: Set<string> = new Set(['default']);
146 const volumes = config.clusterConfig.Volumes;
147 Object.keys(volumes).forEach(v => {
148 Object.keys(volumes[v].StorageClasses || {}).forEach(sc => {
149 if (volumes[v].StorageClasses[sc]) {
154 return Array.from(classes);
157 const getApiRevision = async (apiUrl: string) => {
159 const dd = (await Axios.get<any>(`${apiUrl}/${DISCOVERY_DOC_PATH}`)).data;
160 return parseInt(dd.revision, 10) || 0;
162 console.warn("Unable to get API Revision number, defaulting to zero. Some features may not work properly.");
167 const removeTrailingSlashes = (config: ClusterConfigJSON): ClusterConfigJSON => {
168 const svcs: any = {};
169 Object.keys(config.Services).forEach((s) => {
170 svcs[s] = config.Services[s];
171 if (svcs[s].hasOwnProperty('ExternalURL')) {
172 svcs[s].ExternalURL = svcs[s].ExternalURL.replace(/\/+$/, '');
175 return { ...config, Services: svcs };
178 export const fetchConfig = () => {
180 .get<WorkbenchConfig>(WORKBENCH_CONFIG_URL + "?nocache=" + (new Date()).getTime())
181 .then(response => response.data)
183 console.warn(`There was an exception getting the Workbench config file at ${WORKBENCH_CONFIG_URL}. Using defaults instead.`);
184 return Promise.resolve(getDefaultConfig());
186 .then(workbenchConfig => {
187 if (workbenchConfig.API_HOST === undefined) {
188 throw new Error(`Unable to start Workbench. API_HOST is undefined in ${WORKBENCH_CONFIG_URL} or the environment.`);
190 return Axios.get<ClusterConfigJSON>(getClusterConfigURL(workbenchConfig.API_HOST)).then(async response => {
191 const apiRevision = await getApiRevision(response.data.Services.Controller.ExternalURL.replace(/\/+$/, ''));
192 const config = { ...buildConfig(response.data), apiRevision };
193 const warnLocalConfig = (varName: string) => console.warn(
194 `A value for ${varName} was found in ${WORKBENCH_CONFIG_URL}. To use the Arvados centralized configuration instead, \
195 remove the entire ${varName} entry from ${WORKBENCH_CONFIG_URL}`);
197 // Check if the workbench config has an entry for vocabulary and file viewer URLs
198 // If so, use these values (even if it is an empty string), but print a console warning.
199 // Otherwise, use the cluster config.
200 let fileViewerConfigUrl;
201 if (workbenchConfig.FILE_VIEWERS_CONFIG_URL !== undefined) {
202 warnLocalConfig("FILE_VIEWERS_CONFIG_URL");
203 fileViewerConfigUrl = workbenchConfig.FILE_VIEWERS_CONFIG_URL;
206 fileViewerConfigUrl = config.clusterConfig.Workbench.FileViewersConfigURL || "/file-viewers-example.json";
208 config.fileViewersConfigUrl = fileViewerConfigUrl;
210 if (workbenchConfig.VOCABULARY_URL !== undefined) {
211 console.warn(`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.`)
213 config.vocabularyUrl = getVocabularyURL(workbenchConfig.API_HOST);
215 return { config, apiHost: workbenchConfig.API_HOST };
220 // Maps remote cluster hosts and removes the default RemoteCluster entry
221 export const mapRemoteHosts = (clusterConfigJSON: ClusterConfigJSON, config: Config) => {
222 config.remoteHosts = {};
223 Object.keys(clusterConfigJSON.RemoteClusters).forEach(k => { config.remoteHosts[k] = clusterConfigJSON.RemoteClusters[k].Host; });
224 delete config.remoteHosts["*"];
227 export const mockClusterConfigJSON = (config: Partial<ClusterConfigJSON>): ClusterConfigJSON => ({
229 UnfreezeProjectRequiresAdmin: false,
234 Controller: { ExternalURL: "" },
235 Workbench1: { ExternalURL: "" },
236 Workbench2: { ExternalURL: "" },
237 Websocket: { ExternalURL: "" },
238 WebDAV: { ExternalURL: "" },
239 WebDAVDownload: { ExternalURL: "" },
240 WebShell: { ExternalURL: "" },
243 DisableSharingURLsUI: false,
245 FileViewersConfigURL: "",
247 InactivePageHTML: "",
249 SSHHelpHostSuffix: "",
275 ForwardSlashNameSubstitution: "",
276 TrustAllContent: false,
282 export const mockConfig = (config: Partial<Config>): Config => ({
284 keepWebServiceUrl: "",
285 keepWebInlineServiceUrl: "",
293 fileViewersConfigUrl: "",
295 clusterConfig: mockClusterConfigJSON({}),
300 const getDefaultConfig = (): WorkbenchConfig => {
302 const envHost = process.env.REACT_APP_ARVADOS_API_HOST;
303 if (envHost !== undefined) {
304 console.warn(`Using default API host ${envHost}.`);
308 console.warn(`No API host was found in the environment. Workbench may not be able to communicate with Arvados components.`);
312 VOCABULARY_URL: undefined,
313 FILE_VIEWERS_CONFIG_URL: undefined,
317 export const ARVADOS_API_PATH = "arvados/v1";
318 export const CLUSTER_CONFIG_PATH = "arvados/v1/config";
319 export const VOCABULARY_PATH = "arvados/v1/vocabulary";
320 export const DISCOVERY_DOC_PATH = "discovery/v1/apis/arvados/v1/rest";
321 export const getClusterConfigURL = (apiHost: string) => `https://${apiHost}/${CLUSTER_CONFIG_PATH}?nocache=${(new Date()).getTime()}`;
322 export const getVocabularyURL = (apiHost: string) => `https://${apiHost}/${VOCABULARY_PATH}?nocache=${(new Date()).getTime()}`;