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