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 DisableSharingURLsUI: boolean;
54 ArvadosDocsite: string;
55 FileViewersConfigURL: string;
56 WelcomePageHTML: string;
57 InactivePageHTML: string;
58 SSHHelpPageHTML: string;
59 SSHHelpHostSuffix: string;
85 ForwardSlashNameSubstitution: string;
93 TrustAllContent: boolean
98 [key: string]: boolean;
104 export class Config {
106 keepWebServiceUrl!: string;
107 keepWebInlineServiceUrl!: string;
109 [key: string]: string
113 websocketUrl!: string;
114 workbenchUrl!: string;
115 workbench2Url!: string;
116 vocabularyUrl!: string;
117 fileViewersConfigUrl!: string;
118 loginCluster!: string;
119 clusterConfig!: ClusterConfigJSON;
120 apiRevision!: number;
123 export const buildConfig = (clusterConfig: ClusterConfigJSON): Config => {
124 const clusterConfigJSON = removeTrailingSlashes(clusterConfig);
125 const config = new Config();
126 config.rootUrl = clusterConfigJSON.Services.Controller.ExternalURL;
127 config.baseUrl = `${config.rootUrl}/${ARVADOS_API_PATH}`;
128 config.uuidPrefix = clusterConfigJSON.ClusterID;
129 config.websocketUrl = clusterConfigJSON.Services.Websocket.ExternalURL;
130 config.workbench2Url = clusterConfigJSON.Services.Workbench2.ExternalURL;
131 config.workbenchUrl = clusterConfigJSON.Services.Workbench1.ExternalURL;
132 config.keepWebServiceUrl = clusterConfigJSON.Services.WebDAVDownload.ExternalURL;
133 config.keepWebInlineServiceUrl = clusterConfigJSON.Services.WebDAV.ExternalURL;
134 config.loginCluster = clusterConfigJSON.Login.LoginCluster;
135 config.clusterConfig = clusterConfigJSON;
136 config.apiRevision = 0;
137 mapRemoteHosts(clusterConfigJSON, config);
141 export const getStorageClasses = (config: Config): string[] => {
142 const classes: Set<string> = new Set(['default']);
143 const volumes = config.clusterConfig.Volumes;
144 Object.keys(volumes).forEach(v => {
145 Object.keys(volumes[v].StorageClasses || {}).forEach(sc => {
146 if (volumes[v].StorageClasses[sc]) {
151 return Array.from(classes);
154 const getApiRevision = async (apiUrl: string) => {
156 const dd = (await Axios.get<any>(`${apiUrl}/${DISCOVERY_DOC_PATH}`)).data;
157 return parseInt(dd.revision, 10) || 0;
159 console.warn("Unable to get API Revision number, defaulting to zero. Some features may not work properly.");
164 const removeTrailingSlashes = (config: ClusterConfigJSON): ClusterConfigJSON => {
165 const svcs: any = {};
166 Object.keys(config.Services).forEach((s) => {
167 svcs[s] = config.Services[s];
168 if (svcs[s].hasOwnProperty('ExternalURL')) {
169 svcs[s].ExternalURL = svcs[s].ExternalURL.replace(/\/+$/, '');
172 return { ...config, Services: svcs };
175 export const fetchConfig = () => {
177 .get<WorkbenchConfig>(WORKBENCH_CONFIG_URL + "?nocache=" + (new Date()).getTime())
178 .then(response => response.data)
180 console.warn(`There was an exception getting the Workbench config file at ${WORKBENCH_CONFIG_URL}. Using defaults instead.`);
181 return Promise.resolve(getDefaultConfig());
183 .then(workbenchConfig => {
184 if (workbenchConfig.API_HOST === undefined) {
185 throw new Error(`Unable to start Workbench. API_HOST is undefined in ${WORKBENCH_CONFIG_URL} or the environment.`);
187 return Axios.get<ClusterConfigJSON>(getClusterConfigURL(workbenchConfig.API_HOST)).then(async response => {
188 const apiRevision = await getApiRevision(response.data.Services.Controller.ExternalURL.replace(/\/+$/, ''));
189 const config = { ...buildConfig(response.data), apiRevision };
190 const warnLocalConfig = (varName: string) => console.warn(
191 `A value for ${varName} was found in ${WORKBENCH_CONFIG_URL}. To use the Arvados centralized configuration instead, \
192 remove the entire ${varName} entry from ${WORKBENCH_CONFIG_URL}`);
194 // Check if the workbench config has an entry for vocabulary and file viewer URLs
195 // If so, use these values (even if it is an empty string), but print a console warning.
196 // Otherwise, use the cluster config.
197 let fileViewerConfigUrl;
198 if (workbenchConfig.FILE_VIEWERS_CONFIG_URL !== undefined) {
199 warnLocalConfig("FILE_VIEWERS_CONFIG_URL");
200 fileViewerConfigUrl = workbenchConfig.FILE_VIEWERS_CONFIG_URL;
203 fileViewerConfigUrl = config.clusterConfig.Workbench.FileViewersConfigURL || "/file-viewers-example.json";
205 config.fileViewersConfigUrl = fileViewerConfigUrl;
207 if (workbenchConfig.VOCABULARY_URL !== undefined) {
208 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.`)
210 config.vocabularyUrl = getVocabularyURL(workbenchConfig.API_HOST);
212 return { config, apiHost: workbenchConfig.API_HOST };
217 // Maps remote cluster hosts and removes the default RemoteCluster entry
218 export const mapRemoteHosts = (clusterConfigJSON: ClusterConfigJSON, config: Config) => {
219 config.remoteHosts = {};
220 Object.keys(clusterConfigJSON.RemoteClusters).forEach(k => { config.remoteHosts[k] = clusterConfigJSON.RemoteClusters[k].Host; });
221 delete config.remoteHosts["*"];
224 export const mockClusterConfigJSON = (config: Partial<ClusterConfigJSON>): ClusterConfigJSON => ({
228 Controller: { ExternalURL: "" },
229 Workbench1: { ExternalURL: "" },
230 Workbench2: { ExternalURL: "" },
231 Websocket: { ExternalURL: "" },
232 WebDAV: { ExternalURL: "" },
233 WebDAVDownload: { ExternalURL: "" },
234 WebShell: { ExternalURL: "" },
237 DisableSharingURLsUI: false,
239 FileViewersConfigURL: "",
241 InactivePageHTML: "",
243 SSHHelpHostSuffix: "",
269 ForwardSlashNameSubstitution: "",
270 TrustAllContent: false,
276 export const mockConfig = (config: Partial<Config>): Config => ({
278 keepWebServiceUrl: "",
279 keepWebInlineServiceUrl: "",
287 fileViewersConfigUrl: "",
289 clusterConfig: mockClusterConfigJSON({}),
294 const getDefaultConfig = (): WorkbenchConfig => {
296 const envHost = process.env.REACT_APP_ARVADOS_API_HOST;
297 if (envHost !== undefined) {
298 console.warn(`Using default API host ${envHost}.`);
302 console.warn(`No API host was found in the environment. Workbench may not be able to communicate with Arvados components.`);
306 VOCABULARY_URL: undefined,
307 FILE_VIEWERS_CONFIG_URL: undefined,
311 export const ARVADOS_API_PATH = "arvados/v1";
312 export const CLUSTER_CONFIG_PATH = "arvados/v1/config";
313 export const VOCABULARY_PATH = "arvados/v1/vocabulary";
314 export const DISCOVERY_DOC_PATH = "discovery/v1/apis/arvados/v1/rest";
315 export const getClusterConfigURL = (apiHost: string) => `https://${apiHost}/${CLUSTER_CONFIG_PATH}?nocache=${(new Date()).getTime()}`;
316 export const getVocabularyURL = (apiHost: string) => `https://${apiHost}/${VOCABULARY_PATH}?nocache=${(new Date()).getTime()}`;