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;
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 = clusterConfigJSON.Services.WebDAVDownload.ExternalURL;
137 config.keepWebInlineServiceUrl = clusterConfigJSON.Services.WebDAV.ExternalURL;
138 config.loginCluster = clusterConfigJSON.Login.LoginCluster;
139 config.clusterConfig = clusterConfigJSON;
140 config.apiRevision = 0;
141 mapRemoteHosts(clusterConfigJSON, config);
145 export const getStorageClasses = (config: Config): string[] => {
146 const classes: Set<string> = new Set(['default']);
147 const volumes = config.clusterConfig.Volumes;
148 Object.keys(volumes).forEach(v => {
149 Object.keys(volumes[v].StorageClasses || {}).forEach(sc => {
150 if (volumes[v].StorageClasses[sc]) {
155 return Array.from(classes);
158 const getApiRevision = async (apiUrl: string) => {
160 const dd = (await Axios.get<any>(`${apiUrl}/${DISCOVERY_DOC_PATH}`)).data;
161 return parseInt(dd.revision, 10) || 0;
163 console.warn("Unable to get API Revision number, defaulting to zero. Some features may not work properly.");
168 const removeTrailingSlashes = (config: ClusterConfigJSON): ClusterConfigJSON => {
169 const svcs: any = {};
170 Object.keys(config.Services).forEach((s) => {
171 svcs[s] = config.Services[s];
172 if (svcs[s].hasOwnProperty('ExternalURL')) {
173 svcs[s].ExternalURL = svcs[s].ExternalURL.replace(/\/+$/, '');
176 return { ...config, Services: svcs };
179 export const fetchConfig = () => {
181 .get<WorkbenchConfig>(WORKBENCH_CONFIG_URL + "?nocache=" + (new Date()).getTime())
182 .then(response => response.data)
184 console.warn(`There was an exception getting the Workbench config file at ${WORKBENCH_CONFIG_URL}. Using defaults instead.`);
185 return Promise.resolve(getDefaultConfig());
187 .then(workbenchConfig => {
188 if (workbenchConfig.API_HOST === undefined) {
189 throw new Error(`Unable to start Workbench. API_HOST is undefined in ${WORKBENCH_CONFIG_URL} or the environment.`);
191 return Axios.get<ClusterConfigJSON>(getClusterConfigURL(workbenchConfig.API_HOST)).then(async response => {
192 const apiRevision = await getApiRevision(response.data.Services.Controller.ExternalURL.replace(/\/+$/, ''));
193 const config = { ...buildConfig(response.data), apiRevision };
194 const warnLocalConfig = (varName: string) => console.warn(
195 `A value for ${varName} was found in ${WORKBENCH_CONFIG_URL}. To use the Arvados centralized configuration instead, \
196 remove the entire ${varName} entry from ${WORKBENCH_CONFIG_URL}`);
198 // Check if the workbench config has an entry for vocabulary and file viewer URLs
199 // If so, use these values (even if it is an empty string), but print a console warning.
200 // Otherwise, use the cluster config.
201 let fileViewerConfigUrl;
202 if (workbenchConfig.FILE_VIEWERS_CONFIG_URL !== undefined) {
203 warnLocalConfig("FILE_VIEWERS_CONFIG_URL");
204 fileViewerConfigUrl = workbenchConfig.FILE_VIEWERS_CONFIG_URL;
207 fileViewerConfigUrl = config.clusterConfig.Workbench.FileViewersConfigURL || "/file-viewers-example.json";
209 config.fileViewersConfigUrl = fileViewerConfigUrl;
211 if (workbenchConfig.VOCABULARY_URL !== undefined) {
212 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.`)
214 config.vocabularyUrl = getVocabularyURL(workbenchConfig.API_HOST);
216 return { config, apiHost: workbenchConfig.API_HOST };
221 // Maps remote cluster hosts and removes the default RemoteCluster entry
222 export const mapRemoteHosts = (clusterConfigJSON: ClusterConfigJSON, config: Config) => {
223 config.remoteHosts = {};
224 Object.keys(clusterConfigJSON.RemoteClusters).forEach(k => { config.remoteHosts[k] = clusterConfigJSON.RemoteClusters[k].Host; });
225 delete config.remoteHosts["*"];
228 export const mockClusterConfigJSON = (config: Partial<ClusterConfigJSON>): ClusterConfigJSON => ({
230 UnfreezeProjectRequiresAdmin: false,
235 Controller: { ExternalURL: "" },
236 Workbench1: { ExternalURL: "" },
237 Workbench2: { ExternalURL: "" },
238 Websocket: { ExternalURL: "" },
239 WebDAV: { ExternalURL: "" },
240 WebDAVDownload: { ExternalURL: "" },
241 WebShell: { ExternalURL: "" },
244 DisableSharingURLsUI: false,
246 FileViewersConfigURL: "",
248 InactivePageHTML: "",
250 SSHHelpHostSuffix: "",
277 ForwardSlashNameSubstitution: "",
278 TrustAllContent: false,
284 export const mockConfig = (config: Partial<Config>): Config => ({
286 keepWebServiceUrl: "",
287 keepWebInlineServiceUrl: "",
295 fileViewersConfigUrl: "",
297 clusterConfig: mockClusterConfigJSON({}),
302 const getDefaultConfig = (): WorkbenchConfig => {
304 const envHost = process.env.REACT_APP_ARVADOS_API_HOST;
305 if (envHost !== undefined) {
306 console.warn(`Using default API host ${envHost}.`);
310 console.warn(`No API host was found in the environment. Workbench may not be able to communicate with Arvados components.`);
314 VOCABULARY_URL: undefined,
315 FILE_VIEWERS_CONFIG_URL: undefined,
319 export const ARVADOS_API_PATH = "arvados/v1";
320 export const CLUSTER_CONFIG_PATH = "arvados/v1/config";
321 export const VOCABULARY_PATH = "arvados/v1/vocabulary";
322 export const DISCOVERY_DOC_PATH = "discovery/v1/apis/arvados/v1/rest";
323 export const getClusterConfigURL = (apiHost: string) => `https://${apiHost}/${CLUSTER_CONFIG_PATH}?nocache=${(new Date()).getTime()}`;
324 export const getVocabularyURL = (apiHost: string) => `https://${apiHost}/${VOCABULARY_PATH}?nocache=${(new Date()).getTime()}`;