Merge branch 'master' into 15067-tag-editing-by-ids
[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     };
43     Workbench: {
44         ArvadosDocsite: string;
45         VocabularyURL: string;
46         FileViewersConfigURL: string;
47         WelcomePageHTML: string;
48         InactivePageHTML: string;
49         SiteName: string;
50     };
51     Login: {
52         LoginCluster: string;
53     };
54 }
55
56 export class Config {
57     baseUrl: string;
58     keepWebServiceUrl: string;
59     remoteHosts: {
60         [key: string]: string
61     };
62     rootUrl: string;
63     uuidPrefix: string;
64     websocketUrl: string;
65     workbenchUrl: string;
66     workbench2Url: string;
67     vocabularyUrl: string;
68     fileViewersConfigUrl: string;
69     loginCluster: string;
70     clusterConfig: ClusterConfigJSON;
71 }
72
73 export const buildConfig = (clusterConfigJSON: ClusterConfigJSON): Config => {
74     const config = new Config();
75     config.rootUrl = clusterConfigJSON.Services.Controller.ExternalURL;
76     config.baseUrl = `${config.rootUrl}/${ARVADOS_API_PATH}`;
77     config.uuidPrefix = clusterConfigJSON.ClusterID;
78     config.websocketUrl = clusterConfigJSON.Services.Websocket.ExternalURL;
79     config.workbench2Url = clusterConfigJSON.Services.Workbench2.ExternalURL;
80     config.workbenchUrl = clusterConfigJSON.Services.Workbench1.ExternalURL;
81     config.keepWebServiceUrl = clusterConfigJSON.Services.WebDAV.ExternalURL;
82     config.loginCluster = clusterConfigJSON.Login.LoginCluster;
83     config.clusterConfig = clusterConfigJSON;
84     mapRemoteHosts(clusterConfigJSON, config);
85     return config;
86 };
87
88 export const fetchConfig = () => {
89     return Axios
90         .get<WorkbenchConfig>(WORKBENCH_CONFIG_URL + "?nocache=" + (new Date()).getTime())
91         .then(response => response.data)
92         .catch(() => {
93             console.warn(`There was an exception getting the Workbench config file at ${WORKBENCH_CONFIG_URL}. Using defaults instead.`);
94             return Promise.resolve(getDefaultConfig());
95         })
96         .then(workbenchConfig => {
97             if (workbenchConfig.API_HOST === undefined) {
98                 throw new Error(`Unable to start Workbench. API_HOST is undefined in ${WORKBENCH_CONFIG_URL} or the environment.`);
99             }
100             return Axios.get<ClusterConfigJSON>(getClusterConfigURL(workbenchConfig.API_HOST)).then(response => {
101                 const clusterConfigJSON = response.data;
102                 const config = buildConfig(clusterConfigJSON);
103                 const warnLocalConfig = (varName: string) => console.warn(
104                     `A value for ${varName} was found in ${WORKBENCH_CONFIG_URL}. To use the Arvados centralized configuration instead, \
105 remove the entire ${varName} entry from ${WORKBENCH_CONFIG_URL}`);
106
107                 // Check if the workbench config has an entry for vocabulary and file viewer URLs
108                 // If so, use these values (even if it is an empty string), but print a console warning.
109                 // Otherwise, use the cluster config.
110                 let fileViewerConfigUrl;
111                 if (workbenchConfig.FILE_VIEWERS_CONFIG_URL !== undefined) {
112                     warnLocalConfig("FILE_VIEWERS_CONFIG_URL");
113                     fileViewerConfigUrl = workbenchConfig.FILE_VIEWERS_CONFIG_URL;
114                 }
115                 else {
116                     fileViewerConfigUrl = clusterConfigJSON.Workbench.FileViewersConfigURL || "/file-viewers-example.json";
117                 }
118                 config.fileViewersConfigUrl = fileViewerConfigUrl;
119
120                 let vocabularyUrl;
121                 if (workbenchConfig.VOCABULARY_URL !== undefined) {
122                     warnLocalConfig("VOCABULARY_URL");
123                     vocabularyUrl = workbenchConfig.VOCABULARY_URL;
124                 }
125                 else {
126                     vocabularyUrl = clusterConfigJSON.Workbench.VocabularyURL || "/vocabulary-example.json";
127                 }
128                 // FIXME: The following line is for dev testing purposes
129                 vocabularyUrl = "/vocabulary-example.json";
130
131                 config.vocabularyUrl = vocabularyUrl;
132
133                 return { config, apiHost: workbenchConfig.API_HOST };
134             });
135         });
136 };
137
138 // Maps remote cluster hosts and removes the default RemoteCluster entry
139 export const mapRemoteHosts = (clusterConfigJSON: ClusterConfigJSON, config: Config) => {
140     config.remoteHosts = {};
141     Object.keys(clusterConfigJSON.RemoteClusters).forEach(k => { config.remoteHosts[k] = clusterConfigJSON.RemoteClusters[k].Host; });
142     delete config.remoteHosts["*"];
143 };
144
145 export const mockClusterConfigJSON = (config: Partial<ClusterConfigJSON>): ClusterConfigJSON => ({
146     ClusterID: "",
147     RemoteClusters: {},
148     Services: {
149         Controller: { ExternalURL: "" },
150         Workbench1: { ExternalURL: "" },
151         Workbench2: { ExternalURL: "" },
152         Websocket: { ExternalURL: "" },
153         WebDAV: { ExternalURL: "" },
154     },
155     Workbench: {
156         ArvadosDocsite: "",
157         VocabularyURL: "",
158         FileViewersConfigURL: "",
159         WelcomePageHTML: "",
160         InactivePageHTML: "",
161         SiteName: "",
162     },
163     Login: {
164         LoginCluster: "",
165     },
166     ...config
167 });
168
169 export const mockConfig = (config: Partial<Config>): Config => ({
170     baseUrl: "",
171     keepWebServiceUrl: "",
172     remoteHosts: {},
173     rootUrl: "",
174     uuidPrefix: "",
175     websocketUrl: "",
176     workbenchUrl: "",
177     workbench2Url: "",
178     vocabularyUrl: "",
179     fileViewersConfigUrl: "",
180     loginCluster: "",
181     clusterConfig: mockClusterConfigJSON({}),
182     ...config
183 });
184
185 const getDefaultConfig = (): WorkbenchConfig => {
186     let apiHost = "";
187     const envHost = process.env.REACT_APP_ARVADOS_API_HOST;
188     if (envHost !== undefined) {
189         console.warn(`Using default API host ${envHost}.`);
190         apiHost = envHost;
191     }
192     else {
193         console.warn(`No API host was found in the environment. Workbench may not be able to communicate with Arvados components.`);
194     }
195     return {
196         API_HOST: apiHost,
197         VOCABULARY_URL: undefined,
198         FILE_VIEWERS_CONFIG_URL: undefined,
199     };
200 };
201
202 export const ARVADOS_API_PATH = "arvados/v1";
203 export const CLUSTER_CONFIG_PATH = "arvados/v1/config";
204 export const DISCOVERY_DOC_PATH = "discovery/v1/apis/arvados/v1/rest";
205 export const getClusterConfigURL = (apiHost: string) => `${window.location.protocol}//${apiHost}/${CLUSTER_CONFIG_PATH}?nocache=${(new Date()).getTime()}`;