f44dc1680935596b14d3923b04b13b199e3abe84
[arvados-workbench2.git] / src / common / config.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import Axios from "axios";
6
7 export const WORKBENCH_CONFIG_URL = process.env.REACT_APP_ARVADOS_CONFIG_URL || "/config.json";
8
9 interface WorkbenchConfig {
10     API_HOST: string;
11     VOCABULARY_URL?: string;
12     FILE_VIEWERS_CONFIG_URL?: string;
13 }
14
15 export interface ClusterConfigJSON {
16     ClusterID: string;
17     RemoteClusters: {
18         [key: string]: {
19             ActivateUsers: boolean
20             Host: string
21             Insecure: boolean
22             Proxy: boolean
23             Scheme: string
24         }
25     };
26     Services: {
27         Controller: {
28             ExternalURL: string
29         }
30         Workbench1: {
31             ExternalURL: string
32         }
33         Workbench2: {
34             ExternalURL: string
35         }
36         Websocket: {
37             ExternalURL: string
38         }
39         WebDAV: {
40             ExternalURL: string
41         },
42         WebDAVDownload: {
43             ExternalURL: string
44         }
45     };
46     Workbench: {
47         ArvadosDocsite: string;
48         VocabularyURL: string;
49         FileViewersConfigURL: string;
50         WelcomePageHTML: string;
51         InactivePageHTML: string;
52         SiteName: string;
53     };
54     Login: {
55         LoginCluster: string;
56     };
57     Collections: {
58         ForwardSlashNameSubstitution: string;
59     };
60 }
61
62 export class Config {
63     baseUrl: string;
64     keepWebServiceUrl: string;
65     remoteHosts: {
66         [key: string]: string
67     };
68     rootUrl: string;
69     uuidPrefix: string;
70     websocketUrl: string;
71     workbenchUrl: string;
72     workbench2Url: string;
73     vocabularyUrl: string;
74     fileViewersConfigUrl: string;
75     loginCluster: string;
76     clusterConfig: ClusterConfigJSON;
77 }
78
79 export const buildConfig = (clusterConfigJSON: ClusterConfigJSON): Config => {
80     const config = new Config();
81     config.rootUrl = clusterConfigJSON.Services.Controller.ExternalURL;
82     config.baseUrl = `${config.rootUrl}/${ARVADOS_API_PATH}`;
83     config.uuidPrefix = clusterConfigJSON.ClusterID;
84     config.websocketUrl = clusterConfigJSON.Services.Websocket.ExternalURL;
85     config.workbench2Url = clusterConfigJSON.Services.Workbench2.ExternalURL;
86     config.workbenchUrl = clusterConfigJSON.Services.Workbench1.ExternalURL;
87     config.keepWebServiceUrl = clusterConfigJSON.Services.WebDAVDownload.ExternalURL;
88     config.loginCluster = clusterConfigJSON.Login.LoginCluster;
89     config.clusterConfig = clusterConfigJSON;
90     mapRemoteHosts(clusterConfigJSON, config);
91     return config;
92 };
93
94 export const fetchConfig = () => {
95     return Axios
96         .get<WorkbenchConfig>(WORKBENCH_CONFIG_URL + "?nocache=" + (new Date()).getTime())
97         .then(response => response.data)
98         .catch(() => {
99             console.warn(`There was an exception getting the Workbench config file at ${WORKBENCH_CONFIG_URL}. Using defaults instead.`);
100             return Promise.resolve(getDefaultConfig());
101         })
102         .then(workbenchConfig => {
103             if (workbenchConfig.API_HOST === undefined) {
104                 throw new Error(`Unable to start Workbench. API_HOST is undefined in ${WORKBENCH_CONFIG_URL} or the environment.`);
105             }
106             return Axios.get<ClusterConfigJSON>(getClusterConfigURL(workbenchConfig.API_HOST)).then(response => {
107                 const clusterConfigJSON = response.data;
108                 const config = buildConfig(clusterConfigJSON);
109                 const warnLocalConfig = (varName: string) => console.warn(
110                     `A value for ${varName} was found in ${WORKBENCH_CONFIG_URL}. To use the Arvados centralized configuration instead, \
111 remove the entire ${varName} entry from ${WORKBENCH_CONFIG_URL}`);
112
113                 // Check if the workbench config has an entry for vocabulary and file viewer URLs
114                 // If so, use these values (even if it is an empty string), but print a console warning.
115                 // Otherwise, use the cluster config.
116                 let fileViewerConfigUrl;
117                 if (workbenchConfig.FILE_VIEWERS_CONFIG_URL !== undefined) {
118                     warnLocalConfig("FILE_VIEWERS_CONFIG_URL");
119                     fileViewerConfigUrl = workbenchConfig.FILE_VIEWERS_CONFIG_URL;
120                 }
121                 else {
122                     fileViewerConfigUrl = clusterConfigJSON.Workbench.FileViewersConfigURL || "/file-viewers-example.json";
123                 }
124                 config.fileViewersConfigUrl = fileViewerConfigUrl;
125
126                 let vocabularyUrl;
127                 if (workbenchConfig.VOCABULARY_URL !== undefined) {
128                     warnLocalConfig("VOCABULARY_URL");
129                     vocabularyUrl = workbenchConfig.VOCABULARY_URL;
130                 }
131                 else {
132                     vocabularyUrl = clusterConfigJSON.Workbench.VocabularyURL || "/vocabulary-example.json";
133                 }
134                 config.vocabularyUrl = vocabularyUrl;
135
136                 return { config, apiHost: workbenchConfig.API_HOST };
137             });
138         });
139 };
140
141 // Maps remote cluster hosts and removes the default RemoteCluster entry
142 export const mapRemoteHosts = (clusterConfigJSON: ClusterConfigJSON, config: Config) => {
143     config.remoteHosts = {};
144     Object.keys(clusterConfigJSON.RemoteClusters).forEach(k => { config.remoteHosts[k] = clusterConfigJSON.RemoteClusters[k].Host; });
145     delete config.remoteHosts["*"];
146 };
147
148 export const mockClusterConfigJSON = (config: Partial<ClusterConfigJSON>): ClusterConfigJSON => ({
149     ClusterID: "",
150     RemoteClusters: {},
151     Services: {
152         Controller: { ExternalURL: "" },
153         Workbench1: { ExternalURL: "" },
154         Workbench2: { ExternalURL: "" },
155         Websocket: { ExternalURL: "" },
156         WebDAV: { ExternalURL: "" },
157         WebDAVDownload: { ExternalURL: "" },
158     },
159     Workbench: {
160         ArvadosDocsite: "",
161         VocabularyURL: "",
162         FileViewersConfigURL: "",
163         WelcomePageHTML: "",
164         InactivePageHTML: "",
165         SiteName: "",
166     },
167     Login: {
168         LoginCluster: "",
169     },
170     Collections: {
171         ForwardSlashNameSubstitution: "",
172     },
173     ...config
174 });
175
176 export const mockConfig = (config: Partial<Config>): Config => ({
177     baseUrl: "",
178     keepWebServiceUrl: "",
179     remoteHosts: {},
180     rootUrl: "",
181     uuidPrefix: "",
182     websocketUrl: "",
183     workbenchUrl: "",
184     workbench2Url: "",
185     vocabularyUrl: "",
186     fileViewersConfigUrl: "",
187     loginCluster: "",
188     clusterConfig: mockClusterConfigJSON({}),
189     ...config
190 });
191
192 const getDefaultConfig = (): WorkbenchConfig => {
193     let apiHost = "";
194     const envHost = process.env.REACT_APP_ARVADOS_API_HOST;
195     if (envHost !== undefined) {
196         console.warn(`Using default API host ${envHost}.`);
197         apiHost = envHost;
198     }
199     else {
200         console.warn(`No API host was found in the environment. Workbench may not be able to communicate with Arvados components.`);
201     }
202     return {
203         API_HOST: apiHost,
204         VOCABULARY_URL: undefined,
205         FILE_VIEWERS_CONFIG_URL: undefined,
206     };
207 };
208
209 export const ARVADOS_API_PATH = "arvados/v1";
210 export const CLUSTER_CONFIG_PATH = "arvados/v1/config";
211 export const DISCOVERY_DOC_PATH = "discovery/v1/apis/arvados/v1/rest";
212 export const getClusterConfigURL = (apiHost: string) => `${window.location.protocol}//${apiHost}/${CLUSTER_CONFIG_PATH}?nocache=${(new Date()).getTime()}`;