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 {
19 ActivateUsers: boolean
27 SupportEmailAddress: string;
53 ArvadosDocsite: string;
54 FileViewersConfigURL: string;
55 WelcomePageHTML: string;
56 InactivePageHTML: string;
57 SSHHelpPageHTML: string;
58 SSHHelpHostSuffix: string;
84 ForwardSlashNameSubstitution: string;
92 TrustAllContent: boolean
97 [key: string]: boolean;
103 export class Config {
105 keepWebServiceUrl!: string;
106 keepWebInlineServiceUrl!: string;
108 [key: string]: string
112 websocketUrl!: string;
113 workbenchUrl!: string;
114 workbench2Url!: string;
115 vocabularyUrl!: string;
116 fileViewersConfigUrl!: string;
117 loginCluster!: string;
118 clusterConfig!: ClusterConfigJSON;
119 apiRevision!: number;
122 export const buildConfig = (clusterConfig: ClusterConfigJSON): Config => {
123 const clusterConfigJSON = removeTrailingSlashes(clusterConfig);
124 const config = new Config();
125 config.rootUrl = clusterConfigJSON.Services.Controller.ExternalURL;
126 config.baseUrl = `${config.rootUrl}/${ARVADOS_API_PATH}`;
127 config.uuidPrefix = clusterConfigJSON.ClusterID;
128 config.websocketUrl = clusterConfigJSON.Services.Websocket.ExternalURL;
129 config.workbench2Url = clusterConfigJSON.Services.Workbench2.ExternalURL;
130 config.workbenchUrl = clusterConfigJSON.Services.Workbench1.ExternalURL;
131 config.keepWebServiceUrl = clusterConfigJSON.Services.WebDAVDownload.ExternalURL;
132 config.keepWebInlineServiceUrl = clusterConfigJSON.Services.WebDAV.ExternalURL;
133 config.loginCluster = clusterConfigJSON.Login.LoginCluster;
134 config.clusterConfig = clusterConfigJSON;
135 config.apiRevision = 0;
136 mapRemoteHosts(clusterConfigJSON, config);
140 export const getStorageClasses = (config: Config): string[] => {
141 const classes: Set<string> = new Set(['default']);
142 const volumes = config.clusterConfig.Volumes;
143 Object.keys(volumes).forEach(v => {
144 Object.keys(volumes[v].StorageClasses || {}).forEach(sc => {
145 if (volumes[v].StorageClasses[sc]) {
150 return Array.from(classes);
153 const getApiRevision = async (apiUrl: string) => {
155 const dd = (await Axios.get<any>(`${apiUrl}/${DISCOVERY_DOC_PATH}`)).data;
156 return parseInt(dd.revision, 10) || 0;
158 console.warn("Unable to get API Revision number, defaulting to zero. Some features may not work properly.");
163 const removeTrailingSlashes = (config: ClusterConfigJSON): ClusterConfigJSON => {
164 const svcs: any = {};
165 Object.keys(config.Services).forEach((s) => {
166 svcs[s] = config.Services[s];
167 if (svcs[s].hasOwnProperty('ExternalURL')) {
168 svcs[s].ExternalURL = svcs[s].ExternalURL.replace(/\/+$/, '');
171 return { ...config, Services: svcs };
174 export const fetchConfig = () => {
176 .get<WorkbenchConfig>(WORKBENCH_CONFIG_URL + "?nocache=" + (new Date()).getTime())
177 .then(response => response.data)
179 console.warn(`There was an exception getting the Workbench config file at ${WORKBENCH_CONFIG_URL}. Using defaults instead.`);
180 return Promise.resolve(getDefaultConfig());
182 .then(workbenchConfig => {
183 if (workbenchConfig.API_HOST === undefined) {
184 throw new Error(`Unable to start Workbench. API_HOST is undefined in ${WORKBENCH_CONFIG_URL} or the environment.`);
186 return Axios.get<ClusterConfigJSON>(getClusterConfigURL(workbenchConfig.API_HOST)).then(async response => {
187 const apiRevision = await getApiRevision(response.data.Services.Controller.ExternalURL.replace(/\/+$/, ''));
188 const config = { ...buildConfig(response.data), apiRevision };
189 const warnLocalConfig = (varName: string) => console.warn(
190 `A value for ${varName} was found in ${WORKBENCH_CONFIG_URL}. To use the Arvados centralized configuration instead, \
191 remove the entire ${varName} entry from ${WORKBENCH_CONFIG_URL}`);
193 // Check if the workbench config has an entry for vocabulary and file viewer URLs
194 // If so, use these values (even if it is an empty string), but print a console warning.
195 // Otherwise, use the cluster config.
196 let fileViewerConfigUrl;
197 if (workbenchConfig.FILE_VIEWERS_CONFIG_URL !== undefined) {
198 warnLocalConfig("FILE_VIEWERS_CONFIG_URL");
199 fileViewerConfigUrl = workbenchConfig.FILE_VIEWERS_CONFIG_URL;
202 fileViewerConfigUrl = config.clusterConfig.Workbench.FileViewersConfigURL || "/file-viewers-example.json";
204 config.fileViewersConfigUrl = fileViewerConfigUrl;
206 if (workbenchConfig.VOCABULARY_URL !== undefined) {
207 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.`)
209 config.vocabularyUrl = getVocabularyURL(workbenchConfig.API_HOST);
211 return { config, apiHost: workbenchConfig.API_HOST };
216 // Maps remote cluster hosts and removes the default RemoteCluster entry
217 export const mapRemoteHosts = (clusterConfigJSON: ClusterConfigJSON, config: Config) => {
218 config.remoteHosts = {};
219 Object.keys(clusterConfigJSON.RemoteClusters).forEach(k => { config.remoteHosts[k] = clusterConfigJSON.RemoteClusters[k].Host; });
220 delete config.remoteHosts["*"];
223 export const mockClusterConfigJSON = (config: Partial<ClusterConfigJSON>): ClusterConfigJSON => ({
227 Controller: { ExternalURL: "" },
228 Workbench1: { ExternalURL: "" },
229 Workbench2: { ExternalURL: "" },
230 Websocket: { ExternalURL: "" },
231 WebDAV: { ExternalURL: "" },
232 WebDAVDownload: { ExternalURL: "" },
233 WebShell: { ExternalURL: "" },
237 FileViewersConfigURL: "",
239 InactivePageHTML: "",
241 SSHHelpHostSuffix: "",
267 ForwardSlashNameSubstitution: "",
268 TrustAllContent: false,
274 export const mockConfig = (config: Partial<Config>): Config => ({
276 keepWebServiceUrl: "",
277 keepWebInlineServiceUrl: "",
285 fileViewersConfigUrl: "",
287 clusterConfig: mockClusterConfigJSON({}),
292 const getDefaultConfig = (): WorkbenchConfig => {
294 const envHost = process.env.REACT_APP_ARVADOS_API_HOST;
295 if (envHost !== undefined) {
296 console.warn(`Using default API host ${envHost}.`);
300 console.warn(`No API host was found in the environment. Workbench may not be able to communicate with Arvados components.`);
304 VOCABULARY_URL: undefined,
305 FILE_VIEWERS_CONFIG_URL: undefined,
309 export const ARVADOS_API_PATH = "arvados/v1";
310 export const CLUSTER_CONFIG_PATH = "arvados/v1/config";
311 export const VOCABULARY_PATH = "arvados/v1/vocabulary";
312 export const DISCOVERY_DOC_PATH = "discovery/v1/apis/arvados/v1/rest";
313 export const getClusterConfigURL = (apiHost: string) => `https://${apiHost}/${CLUSTER_CONFIG_PATH}?nocache=${(new Date()).getTime()}`;
314 export const getVocabularyURL = (apiHost: string) => `https://${apiHost}/${VOCABULARY_PATH}?nocache=${(new Date()).getTime()}`;