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