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