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