Merge branch '15736-site-mgr' refs #15736
authorPeter Amstutz <pamstutz@veritasgenetics.com>
Tue, 5 Nov 2019 21:21:37 +0000 (16:21 -0500)
committerPeter Amstutz <pamstutz@veritasgenetics.com>
Tue, 5 Nov 2019 21:21:37 +0000 (16:21 -0500)
Arvados-DCO-1.1-Signed-off-by: Peter Amstutz <pamstutz@veritasgenetics.com>

16 files changed:
src/common/config.ts
src/common/url.ts
src/index.tsx
src/models/session.ts
src/routes/routes.ts
src/services/auth-service/auth-service.ts
src/services/groups-service/groups-service.ts
src/store/auth/auth-action-session.ts
src/store/auth/auth-action.test.ts
src/store/auth/auth-action.ts
src/store/search-results-panel/search-results-middleware-service.ts
src/views-components/add-session/add-session.tsx [new file with mode: 0644]
src/views/search-results-panel/search-results-panel-view.tsx
src/views/site-manager-panel/site-manager-panel-root.tsx
src/views/site-manager-panel/site-manager-panel.tsx
src/views/workbench/fed-login.tsx

index e5f7b1c6d70e2a163c6dd29a11e393d9c6c04b28..7d974342a704198686b8e20a959e537c0e03a73c 100644 (file)
@@ -70,6 +70,21 @@ export class Config {
     clusterConfig: ClusterConfigJSON;
 }
 
+export const buildConfig = (clusterConfigJSON: ClusterConfigJSON): Config => {
+    const config = new Config();
+    config.rootUrl = clusterConfigJSON.Services.Controller.ExternalURL;
+    config.baseUrl = `${config.rootUrl}/${ARVADOS_API_PATH}`;
+    config.uuidPrefix = clusterConfigJSON.ClusterID;
+    config.websocketUrl = clusterConfigJSON.Services.Websocket.ExternalURL;
+    config.workbench2Url = clusterConfigJSON.Services.Workbench2.ExternalURL;
+    config.workbenchUrl = clusterConfigJSON.Services.Workbench1.ExternalURL;
+    config.keepWebServiceUrl = clusterConfigJSON.Services.WebDAV.ExternalURL;
+    config.loginCluster = clusterConfigJSON.Login.LoginCluster;
+    config.clusterConfig = clusterConfigJSON;
+    mapRemoteHosts(clusterConfigJSON, config);
+    return config;
+};
+
 export const fetchConfig = () => {
     return Axios
         .get<WorkbenchConfig>(WORKBENCH_CONFIG_URL + "?nocache=" + (new Date()).getTime())
@@ -83,8 +98,8 @@ export const fetchConfig = () => {
                 throw new Error(`Unable to start Workbench. API_HOST is undefined in ${WORKBENCH_CONFIG_URL} or the environment.`);
             }
             return Axios.get<ClusterConfigJSON>(getClusterConfigURL(workbenchConfig.API_HOST)).then(response => {
-                const config = new Config();
                 const clusterConfigJSON = response.data;
+                const config = buildConfig(clusterConfigJSON);
                 const warnLocalConfig = (varName: string) => console.warn(
                     `A value for ${varName} was found in ${WORKBENCH_CONFIG_URL}. To use the Arvados centralized configuration instead, \
 remove the entire ${varName} entry from ${WORKBENCH_CONFIG_URL}`);
@@ -112,17 +127,6 @@ remove the entire ${varName} entry from ${WORKBENCH_CONFIG_URL}`);
                 }
                 config.vocabularyUrl = vocabularyUrl;
 
-                config.rootUrl = clusterConfigJSON.Services.Controller.ExternalURL;
-                config.baseUrl = `${config.rootUrl}/${ARVADOS_API_PATH}`;
-                config.uuidPrefix = clusterConfigJSON.ClusterID;
-                config.websocketUrl = clusterConfigJSON.Services.Websocket.ExternalURL;
-                config.workbench2Url = clusterConfigJSON.Services.Workbench2.ExternalURL;
-                config.workbenchUrl = clusterConfigJSON.Services.Workbench1.ExternalURL;
-                config.keepWebServiceUrl = clusterConfigJSON.Services.WebDAV.ExternalURL;
-                config.loginCluster = clusterConfigJSON.Login.LoginCluster;
-                config.clusterConfig = clusterConfigJSON;
-                mapRemoteHosts(clusterConfigJSON, config);
-
                 return { config, apiHost: workbenchConfig.API_HOST };
             });
         });
@@ -193,5 +197,6 @@ const getDefaultConfig = (): WorkbenchConfig => {
 };
 
 export const ARVADOS_API_PATH = "arvados/v1";
-export const CLUSTER_CONFIG_URL = "arvados/v1/config";
-export const getClusterConfigURL = (apiHost: string) => `${window.location.protocol}//${apiHost}/${CLUSTER_CONFIG_URL}?nocache=${(new Date()).getTime()}`;
+export const CLUSTER_CONFIG_PATH = "arvados/v1/config";
+export const DISCOVERY_DOC_PATH = "discovery/v1/apis/arvados/v1/rest";
+export const getClusterConfigURL = (apiHost: string) => `${window.location.protocol}//${apiHost}/${CLUSTER_CONFIG_PATH}?nocache=${(new Date()).getTime()}`;
index 1824f26a942426c29f544f9d77decd4ce8b58002..9789b65effb6b47d6ed14c0589cb39b2b032c2a4 100644 (file)
@@ -4,3 +4,12 @@ export function getUrlParameter(search: string, name: string) {
     const results = regex.exec(search);
     return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
 }
+
+export function normalizeURLPath(url: string) {
+    const u = new URL(url);
+    u.pathname = u.pathname.replace(/\/\//, '/');
+    if (u.pathname[u.pathname.length - 1] === '/') {
+        u.pathname = u.pathname.substr(0, u.pathname.length - 1);
+    }
+    return u.toString();
+}
index f286b7be2ff96f1e26785464f0ae5f89e4f4b247..5a941638e5c5227fd4014ff63bb10701fb2b17d8 100644 (file)
@@ -13,6 +13,7 @@ import { History } from "history";
 import { configureStore, RootStore } from '~/store/store';
 import { ConnectedRouter } from "react-router-redux";
 import { ApiToken } from "~/views-components/api-token/api-token";
+import { AddSession } from "~/views-components/add-session/add-session";
 import { initAuth } from "~/store/auth/auth-action";
 import { createServices } from "~/services/services";
 import { MuiThemeProvider } from '@material-ui/core/styles';
@@ -112,6 +113,7 @@ fetchConfig()
         store.dispatch(loadFileViewersConfig);
 
         const TokenComponent = (props: any) => <ApiToken authService={services.authService} config={config} loadMainApp={true} {...props} />;
+        const AddSessionComponent = (props: any) => <AddSession {...props} />;
         const FedTokenComponent = (props: any) => <ApiToken authService={services.authService} config={config} loadMainApp={false} {...props} />;
         const MainPanelComponent = (props: any) => <MainPanel {...props} />;
 
@@ -123,6 +125,7 @@ fetchConfig()
                             <Switch>
                                 <Route path={Routes.TOKEN} component={TokenComponent} />
                                 <Route path={Routes.FED_LOGIN} component={FedTokenComponent} />
+                                <Route path={Routes.ADD_SESSION} component={AddSessionComponent} />
                                 <Route path={Routes.ROOT} component={MainPanelComponent} />
                             </Switch>
                         </ConnectedRouter>
index 9a942967370f1241de198007359d6582ac594a2a..91a0d997606257391e0958f595dc3696475b9402 100644 (file)
@@ -12,9 +12,10 @@ export interface Session {
     clusterId: string;
     remoteHost: string;
     baseUrl: string;
-    username: string;
+    name: string;
     email: string;
     token: string;
+    uuid: string;
     loggedIn: boolean;
     status: SessionStatus;
     active: boolean;
index 08e0a03d058a0030bef3f864e640e8bf40b2a733..bb518d3fcfe803f109c56ac379646edc7b0f9c14 100644 (file)
@@ -19,6 +19,7 @@ export const Routes = {
     ROOT: '/',
     TOKEN: '/token',
     FED_LOGIN: '/fedtoken',
+    ADD_SESSION: '/add-session',
     PROJECTS: `/projects/:id(${RESOURCE_UUID_PATTERN})`,
     COLLECTIONS: `/collections/:id(${RESOURCE_UUID_PATTERN})`,
     PROCESSES: `/processes/:id(${RESOURCE_UUID_PATTERN})`,
@@ -72,7 +73,13 @@ export const getNavUrl = (uuid: string, config: FederationConfig) => {
     } else if (config.remoteHostsConfig[cls]) {
         let u: URL;
         if (config.remoteHostsConfig[cls].workbench2Url) {
-            u = new URL(config.remoteHostsConfig[cls].workbench2Url || "");
+           /* NOTE: wb2 presently doesn't support passing api_token
+              to arbitrary page to set credentials, only through
+              api-token route.  So for navigation to work, user needs
+              to already be logged in.  In the future we want to just
+              request the records and display in the current
+              workbench instance making this redirect unnecessary. */
+            u = new URL(config.remoteHostsConfig[cls].workbench2Url);
         } else {
             u = new URL(config.remoteHostsConfig[cls].workbenchUrl);
             u.search = "api_token=" + config.sessions.filter((s) => s.clusterId === cls)[0].token;
@@ -159,7 +166,7 @@ export const matchTokenRoute = (route: string) =>
     matchPath(route, { path: Routes.TOKEN });
 
 export const matchFedTokenRoute = (route: string) =>
-    matchPath(route, {path: Routes.FED_LOGIN});
+    matchPath(route, { path: Routes.FED_LOGIN });
 
 export const matchUsersRoute = (route: string) =>
     matchPath(route, { path: Routes.USERS });
index da96f1629b2fc9e3cf76ba91c3215df43bd17666..d5cb4ec205c36cdfc9d0545a167218a454b3103a 100644 (file)
@@ -176,11 +176,12 @@ export class AuthService {
             clusterId: cfg.uuidPrefix,
             remoteHost: cfg.rootUrl,
             baseUrl: cfg.baseUrl,
-            username: getUserFullname(user),
+            name: getUserFullname(user),
             email: user ? user.email : '',
             token: this.getApiToken(),
             loggedIn: true,
             active: true,
+            uuid: user ? user.uuid : '',
             status: SessionStatus.VALIDATED
         } as Session;
         const localSessions = this.getSessions().map(s => ({
@@ -195,11 +196,12 @@ export class AuthService {
                 clusterId,
                 remoteHost,
                 baseUrl: '',
-                username: '',
+                name: '',
                 email: '',
                 token: '',
                 loggedIn: false,
                 active: false,
+                uuid: '',
                 status: SessionStatus.INVALIDATED
             } as Session;
         });
index 9517e2cb3427dd462ef4b31ca5f822babca67e2c..691ab8f7ccdb670affa90374cead8f4a783ca25a 100644 (file)
@@ -52,6 +52,7 @@ export class GroupsService<T extends GroupResource = GroupResource> extends Tras
         const cfg: AxiosRequestConfig = { params: CommonResourceService.mapKeys(_.snakeCase)(params) };
         if (session) {
             cfg.baseURL = session.baseUrl;
+            cfg.headers = { 'Authorization': 'Bearer ' + session.token };
         }
 
         const response = await CommonResourceService.defaultResponse(
index 6af72e0c7e413bf5f5be384fe9e605a93e62016a..5b8acf9aeb061651b656590cd496c4a2d9e8bad8 100644 (file)
@@ -9,36 +9,76 @@ import { ServiceRepository } from "~/services/services";
 import Axios from "axios";
 import { getUserFullname, User } from "~/models/user";
 import { authActions } from "~/store/auth/auth-action";
-import { Config, ClusterConfigJSON, CLUSTER_CONFIG_URL, ARVADOS_API_PATH } from "~/common/config";
+import {
+    Config, ClusterConfigJSON, CLUSTER_CONFIG_PATH, DISCOVERY_DOC_PATH,
+    buildConfig, mockClusterConfigJSON
+} from "~/common/config";
+import { normalizeURLPath } from "~/common/url";
 import { Session, SessionStatus } from "~/models/session";
 import { progressIndicatorActions } from "~/store/progress-indicator/progress-indicator-actions";
 import { AuthService, UserDetailsResponse } from "~/services/auth-service/auth-service";
+import { snackbarActions, SnackbarKind } from "~/store/snackbar/snackbar-actions";
 import * as jsSHA from "jssha";
 
-const getRemoteHostBaseUrl = async (remoteHost: string): Promise<string | null> => {
+const getClusterConfig = async (origin: string): Promise<Config | null> => {
+    // Try the new public config endpoint
+    try {
+        const config = (await Axios.get<ClusterConfigJSON>(`${origin}/${CLUSTER_CONFIG_PATH}`)).data;
+        return buildConfig(config);
+    } catch { }
+
+    // Fall back to discovery document
+    try {
+        const config = (await Axios.get<any>(`${origin}/${DISCOVERY_DOC_PATH}`)).data;
+        return {
+            baseUrl: normalizeURLPath(config.baseUrl),
+            keepWebServiceUrl: config.keepWebServiceUrl,
+            remoteHosts: config.remoteHosts,
+            rootUrl: config.rootUrl,
+            uuidPrefix: config.uuidPrefix,
+            websocketUrl: config.websocketUrl,
+            workbenchUrl: config.workbenchUrl,
+            workbench2Url: config.workbench2Url,
+            loginCluster: "",
+            vocabularyUrl: "",
+            fileViewersConfigUrl: "",
+            clusterConfig: mockClusterConfigJSON({})
+        };
+    } catch { }
+
+    return null;
+};
+
+const getRemoteHostConfig = async (remoteHost: string): Promise<Config | null> => {
     let url = remoteHost;
     if (url.indexOf('://') < 0) {
         url = 'https://' + url;
     }
     const origin = new URL(url).origin;
-    let baseUrl: string | null = null;
 
+    // Maybe it is an API server URL, try fetching config and discovery doc
+    let r = await getClusterConfig(origin);
+    if (r !== null) {
+        return r;
+    }
+
+    // Maybe it is a Workbench2 URL, try getting config.json
     try {
-        const resp = await Axios.get<ClusterConfigJSON>(`${origin}/${CLUSTER_CONFIG_URL}`);
-        baseUrl = `${resp.data.Services.Controller.ExternalURL}/${ARVADOS_API_PATH}`;
-    } catch (err) {
-        try {
-            const resp = await Axios.get<any>(`${origin}/status.json`);
-            baseUrl = resp.data.apiBaseURL;
-        } catch (err) {
+        r = await getClusterConfig((await Axios.get<any>(`${origin}/config.json`)).data.API_HOST);
+        if (r !== null) {
+            return r;
         }
-    }
+    } catch { }
 
-    if (baseUrl && baseUrl[baseUrl.length - 1] === '/') {
-        baseUrl = baseUrl.substr(0, baseUrl.length - 1);
-    }
+    // Maybe it is a Workbench1 URL, try getting status.json
+    try {
+        r = await getClusterConfig((await Axios.get<any>(`${origin}/status.json`)).data.apiBaseURL);
+        if (r !== null) {
+            return r;
+        }
+    } catch { }
 
-    return baseUrl;
+    return null;
 };
 
 const getUserDetails = async (baseUrl: string, token: string): Promise<UserDetailsResponse> => {
@@ -50,40 +90,30 @@ const getUserDetails = async (baseUrl: string, token: string): Promise<UserDetai
     return resp.data;
 };
 
-const getTokenUuid = async (baseUrl: string, token: string): Promise<string> => {
-    if (token.startsWith("v2/")) {
-        const uuid = token.split("/")[1];
-        return Promise.resolve(uuid);
-    }
+const invalidV2Token = "Must be a v2 token";
 
-    const resp = await Axios.get(`${baseUrl}api_client_authorizations`, {
-        headers: {
-            Authorization: `OAuth2 ${token}`
-        },
-        data: {
-            filters: JSON.stringify([['api_token', '=', token]])
-        }
-    });
-
-    return resp.data.items[0].uuid;
-};
-
-export const getSaltedToken = (clusterId: string, tokenUuid: string, token: string) => {
+export const getSaltedToken = (clusterId: string, token: string) => {
     const shaObj = new jsSHA("SHA-1", "TEXT");
-    let secret = token;
-    if (token.startsWith("v2/")) {
-        secret = token.split("/")[2];
+    const [ver, uuid, secret] = token.split("/");
+    if (ver !== "v2") {
+        throw new Error(invalidV2Token);
     }
-    shaObj.setHMACKey(secret, "TEXT");
-    shaObj.update(clusterId);
-    const hmac = shaObj.getHMAC("HEX");
-    return `v2/${tokenUuid}/${hmac}`;
+    let salted = secret;
+    if (uuid.substr(0, 5) !== clusterId) {
+        shaObj.setHMACKey(secret, "TEXT");
+        shaObj.update(clusterId);
+        salted = shaObj.getHMAC("HEX");
+    }
+    return `v2/${uuid}/${salted}`;
 };
 
-const clusterLogin = async (clusterId: string, baseUrl: string, activeSession: Session): Promise<{ user: User, token: string }> => {
-    const tokenUuid = await getTokenUuid(activeSession.baseUrl, activeSession.token);
-    const saltedToken = getSaltedToken(clusterId, tokenUuid, activeSession.token);
-    const user = await getUserDetails(baseUrl, saltedToken);
+export const getActiveSession = (sessions: Session[]): Session | undefined => sessions.find(s => s.active);
+
+export const validateCluster = async (config: Config, useToken: string):
+    Promise<{ user: User; token: string }> => {
+
+    const saltedToken = getSaltedToken(config.uuidPrefix, useToken);
+    const user = await getUserDetails(config.baseUrl, saltedToken);
     return {
         user: {
             firstName: user.first_name,
@@ -96,38 +126,53 @@ const clusterLogin = async (clusterId: string, baseUrl: string, activeSession: S
             username: user.username,
             prefs: user.prefs
         },
-        token: saltedToken
+        token: saltedToken,
     };
 };
 
-export const getActiveSession = (sessions: Session[]): Session | undefined => sessions.find(s => s.active);
-
-export const validateCluster = async (remoteHost: string, clusterId: string, activeSession: Session): Promise<{ user: User; token: string, baseUrl: string }> => {
-    const baseUrl = await getRemoteHostBaseUrl(remoteHost);
-    if (!baseUrl) {
-        return Promise.reject(`Could not find base url for ${remoteHost}`);
-    }
-    const { user, token } = await clusterLogin(clusterId, baseUrl, activeSession);
-    return { baseUrl, user, token };
-};
-
 export const validateSession = (session: Session, activeSession: Session) =>
     async (dispatch: Dispatch): Promise<Session> => {
         dispatch(authActions.UPDATE_SESSION({ ...session, status: SessionStatus.BEING_VALIDATED }));
         session.loggedIn = false;
-        try {
-            const { baseUrl, user, token } = await validateCluster(session.remoteHost, session.clusterId, activeSession);
+
+        const setupSession = (baseUrl: string, user: User, token: string) => {
             session.baseUrl = baseUrl;
             session.token = token;
             session.email = user.email;
-            session.username = getUserFullname(user);
+            session.uuid = user.uuid;
+            session.name = getUserFullname(user);
             session.loggedIn = true;
-        } catch {
-            session.loggedIn = false;
-        } finally {
-            session.status = SessionStatus.VALIDATED;
-            dispatch(authActions.UPDATE_SESSION(session));
+        };
+
+        let fail: Error | null = null;
+        const config = await getRemoteHostConfig(session.remoteHost);
+        if (config !== null) {
+            dispatch(authActions.REMOTE_CLUSTER_CONFIG({ config }));
+            try {
+                const { user, token } = await validateCluster(config, session.token);
+                setupSession(config.baseUrl, user, token);
+            } catch (e) {
+                fail = new Error(`Getting current user for ${session.remoteHost}: ${e.message}`);
+                try {
+                    const { user, token } = await validateCluster(config, activeSession.token);
+                    setupSession(config.baseUrl, user, token);
+                    fail = null;
+                } catch (e2) {
+                    if (e.message === invalidV2Token) {
+                        fail = new Error(`Getting current user for ${session.remoteHost}: ${e2.message}`);
+                    }
+                }
+            }
+        } else {
+            fail = new Error(`Could not get config for ${session.remoteHost}`);
         }
+        session.status = SessionStatus.VALIDATED;
+        dispatch(authActions.UPDATE_SESSION(session));
+
+        if (fail) {
+            throw fail;
+        }
+
         return session;
     };
 
@@ -139,69 +184,124 @@ export const validateSessions = () =>
             dispatch(progressIndicatorActions.START_WORKING("sessionsValidation"));
             for (const session of sessions) {
                 if (session.status === SessionStatus.INVALIDATED) {
-                    await dispatch(validateSession(session, activeSession));
+                    try {
+                       /* Here we are dispatching a function, not an
+                          action.  This is legal (it calls the
+                          function with a 'Dispatch' object as the
+                          first parameter) but the typescript
+                          annotations don't understand this case, so
+                          we get an error from typescript unless
+                          override it using Dispatch<any>.  This
+                          pattern is used in a bunch of different
+                          places in Workbench2. */
+                        await dispatch(validateSession(session, activeSession));
+                    } catch (e) {
+                        dispatch(snackbarActions.OPEN_SNACKBAR({
+                            message: e.message,
+                            kind: SnackbarKind.ERROR
+                        }));
+                    }
                 }
             }
-            services.authService.saveSessions(sessions);
+            services.authService.saveSessions(getState().auth.sessions);
             dispatch(progressIndicatorActions.STOP_WORKING("sessionsValidation"));
         }
     };
 
-export const addSession = (remoteHost: string) =>
+export const addSession = (remoteHost: string, token?: string, sendToLogin?: boolean) =>
     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
         const sessions = getState().auth.sessions;
         const activeSession = getActiveSession(sessions);
-        if (activeSession) {
-            const clusterId = remoteHost.match(/^(\w+)\./)![1];
-            if (sessions.find(s => s.clusterId === clusterId)) {
-                return Promise.reject("Cluster already exists");
+        let useToken: string | null = null;
+        if (token) {
+            useToken = token;
+        } else if (activeSession) {
+            useToken = activeSession.token;
+        }
+
+        if (useToken) {
+            const config = await getRemoteHostConfig(remoteHost);
+            if (!config) {
+                dispatch(snackbarActions.OPEN_SNACKBAR({
+                    message: `Could not get config for ${remoteHost}`,
+                    kind: SnackbarKind.ERROR
+                }));
+                return;
             }
+
             try {
-                const { baseUrl, user, token } = await validateCluster(remoteHost, clusterId, activeSession);
+                dispatch(authActions.REMOTE_CLUSTER_CONFIG({ config }));
+                const { user, token } = await validateCluster(config, useToken);
                 const session = {
                     loggedIn: true,
                     status: SessionStatus.VALIDATED,
                     active: false,
                     email: user.email,
-                    username: getUserFullname(user),
+                    name: getUserFullname(user),
+                    uuid: user.uuid,
+                    baseUrl: config.baseUrl,
+                    clusterId: config.uuidPrefix,
                     remoteHost,
-                    baseUrl,
-                    clusterId,
                     token
                 };
 
-                dispatch(authActions.ADD_SESSION(session));
+                if (sessions.find(s => s.clusterId === config.uuidPrefix)) {
+                    await dispatch(authActions.UPDATE_SESSION(session));
+                } else {
+                    await dispatch(authActions.ADD_SESSION(session));
+                }
                 services.authService.saveSessions(getState().auth.sessions);
 
                 return session;
-            } catch (e) {
+            } catch {
+                if (sendToLogin) {
+                    const rootUrl = new URL(config.baseUrl);
+                    rootUrl.pathname = "";
+                    window.location.href = `${rootUrl.toString()}/login?return_to=` + encodeURI(`${window.location.protocol}//${window.location.host}/add-session?baseURL=` + encodeURI(rootUrl.toString()));
+                    return;
+                }
             }
         }
-        return Promise.reject("Could not validate cluster");
+        return Promise.reject(new Error("Could not validate cluster"));
     };
 
-export const toggleSession = (session: Session) =>
+
+export const removeSession = (clusterId: string) =>
     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
-        let s = { ...session };
+        await dispatch(authActions.REMOVE_SESSION(clusterId));
+        services.authService.saveSessions(getState().auth.sessions);
+    };
+
+export const toggleSession = (session: Session) =>
+    async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
+        const s: Session = { ...session };
 
         if (session.loggedIn) {
             s.loggedIn = false;
+            dispatch(authActions.UPDATE_SESSION(s));
         } else {
             const sessions = getState().auth.sessions;
             const activeSession = getActiveSession(sessions);
             if (activeSession) {
-                s = await dispatch<any>(validateSession(s, activeSession)) as Session;
+                try {
+                    await dispatch(validateSession(s, activeSession));
+                } catch (e) {
+                    dispatch(snackbarActions.OPEN_SNACKBAR({
+                        message: e.message,
+                        kind: SnackbarKind.ERROR
+                    }));
+                    s.loggedIn = false;
+                    dispatch(authActions.UPDATE_SESSION(s));
+                }
             }
         }
 
-        dispatch(authActions.UPDATE_SESSION(s));
         services.authService.saveSessions(getState().auth.sessions);
     };
 
 export const initSessions = (authService: AuthService, config: Config, user: User) =>
     (dispatch: Dispatch<any>) => {
         const sessions = authService.buildSessions(config, user);
-        authService.saveSessions(sessions);
         dispatch(authActions.SET_SESSIONS(sessions));
         dispatch(validateSessions());
     };
index 801d9e33ee9cb9d3e8a266298712fc7bdfe1b956..a543fc18dccadd57c31043d8ecd1f99c84fe0330 100644 (file)
@@ -90,7 +90,8 @@ describe('auth-actions', () => {
                 "remoteHost": "https://zzzzz.arvadosapi.com",
                 "status": 2,
                 "token": "token",
-                "username": "John Doe"
+                "name": "John Doe"
+               "uuid": "zzzzz-tpzed-abcefg",
             }, {
                 "active": false,
                 "baseUrl": "",
@@ -100,7 +101,8 @@ describe('auth-actions', () => {
                 "remoteHost": "xc59z.arvadosapi.com",
                 "status": 1,
                 "token": "",
-                "username": ""
+                "name": "",
+                "uuid": "",
             }],
             user: {
                 email: "test@test.com",
index e273d18c1b4327bb82c2c8ec4e4b170b9ed9c8d5..9f18b5b029ea30e153fb9b59ec911630d1f5d722 100644 (file)
@@ -10,11 +10,10 @@ import { ServiceRepository } from "~/services/services";
 import { SshKeyResource } from '~/models/ssh-key';
 import { User, UserResource } from "~/models/user";
 import { Session } from "~/models/session";
-import { getClusterConfigURL, Config, ClusterConfigJSON, mapRemoteHosts } from '~/common/config';
+import { Config } from '~/common/config';
 import { initSessions } from "~/store/auth/auth-action-session";
 import { cancelLinking } from '~/store/link-account-panel/link-account-panel-actions';
 import { matchTokenRoute, matchFedTokenRoute } from '~/routes/routes';
-import Axios from "axios";
 import { AxiosError } from "axios";
 
 export const authActions = unionize({
@@ -67,12 +66,16 @@ export const initAuth = (config: Config) => (dispatch: Dispatch, getState: () =>
 const init = (config: Config) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
     const user = services.authService.getUser();
     const token = services.authService.getApiToken();
-    const homeCluster = services.authService.getHomeCluster();
+    let homeCluster = services.authService.getHomeCluster();
     if (token) {
         setAuthorizationHeader(services, token);
     }
+    if (homeCluster && !config.remoteHosts[homeCluster]) {
+        homeCluster = undefined;
+    }
     dispatch(authActions.CONFIG({ config }));
     dispatch(authActions.SET_HOME_CLUSTER(config.loginCluster || homeCluster || config.uuidPrefix));
+    document.title = `Arvados Workbench (${config.uuidPrefix})`;
     if (token && user) {
         dispatch(authActions.INIT({ user, token }));
         dispatch<any>(initSessions(services.authService, config, user));
@@ -82,22 +85,11 @@ const init = (config: Config) => (dispatch: Dispatch, getState: () => RootState,
             if (err.response) {
                 // Bad token
                 if (err.response.status === 401) {
-                    logout()(dispatch, getState, services);
+                    dispatch<any>(logout());
                 }
             }
         });
     }
-    Object.keys(config.remoteHosts).map((k) => {
-        Axios.get<ClusterConfigJSON>(getClusterConfigURL(config.remoteHosts[k]))
-            .then(response => {
-                const remoteConfig = new Config();
-                remoteConfig.uuidPrefix = response.data.ClusterID;
-                remoteConfig.workbench2Url = response.data.Services.Workbench2.ExternalURL;
-                remoteConfig.loginCluster = response.data.Login.LoginCluster;
-                mapRemoteHosts(response.data, remoteConfig);
-                dispatch(authActions.REMOTE_CLUSTER_CONFIG({ config: remoteConfig }));
-            });
-    });
 };
 
 export const saveApiToken = (token: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
index 3233525a15fa7ce47def6793b1018c4224496b18..84e68ab0d07d7a4ed2efacd39e287772f375a279 100644 (file)
@@ -42,36 +42,29 @@ export class SearchResultsMiddlewareService extends DataExplorerMiddlewareServic
             return;
         }
 
-        try {
-            const params = getParams(dataExplorer, searchValue);
-
-            const responses = await Promise.all(sessions.map(session =>
-                this.services.groupsService.contents('', params, session)
-            ));
-
-            const initial = {
-                itemsAvailable: 0,
-                items: [] as GroupContentsResource[],
-                kind: '',
-                offset: 0,
-                limit: 10
-            };
-
-            const mergedResponse = responses.reduce((merged, current) => ({
-                ...merged,
-                itemsAvailable: merged.itemsAvailable + current.itemsAvailable,
-                items: merged.items.concat(current.items)
-            }), initial);
-
-            api.dispatch(updateResources(mergedResponse.items));
-
-            api.dispatch(criteriaChanged
-                ? setItems(mergedResponse)
-                : appendItems(mergedResponse));
-
-        } catch {
-            api.dispatch(couldNotFetchSearchResults());
+        const params = getParams(dataExplorer, searchValue);
+
+        const initial = {
+            itemsAvailable: 0,
+            items: [] as GroupContentsResource[],
+            kind: '',
+            offset: 0,
+            limit: 10
+        };
+
+        if (criteriaChanged) {
+            api.dispatch(setItems(initial));
         }
+
+        sessions.map(session =>
+            this.services.groupsService.contents('', params, session)
+                .then((response) => {
+                    api.dispatch(updateResources(response.items));
+                    api.dispatch(appendItems(response));
+                }).catch(() => {
+                    api.dispatch(couldNotFetchSearchResults(session.clusterId));
+                })
+        );
     }
 }
 
@@ -117,8 +110,8 @@ export const appendItems = (listResults: ListResults<GroupContentsResource>) =>
         items: listResults.items.map(resource => resource.uuid),
     });
 
-const couldNotFetchSearchResults = () =>
+const couldNotFetchSearchResults = (cluster: string) =>
     snackbarActions.OPEN_SNACKBAR({
-        message: `Could not fetch search results for some sessions.`,
+        message: `Could not fetch search results from ${cluster}.`,
         kind: SnackbarKind.ERROR
     });
diff --git a/src/views-components/add-session/add-session.tsx b/src/views-components/add-session/add-session.tsx
new file mode 100644 (file)
index 0000000..4628e1c
--- /dev/null
@@ -0,0 +1,26 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+import { RouteProps } from "react-router";
+import * as React from "react";
+import { connect, DispatchProp } from "react-redux";
+import { getUrlParameter } from "~/common/url";
+import { navigateToSiteManager } from "~/store/navigation/navigation-action";
+import { addSession } from "~/store/auth/auth-action-session";
+
+export const AddSession = connect()(
+    class extends React.Component<RouteProps & DispatchProp<any>, {}> {
+        componentDidMount() {
+            const search = this.props.location ? this.props.location.search : "";
+            const apiToken = getUrlParameter(search, 'api_token');
+            const baseURL = getUrlParameter(search, 'baseURL');
+
+            this.props.dispatch(addSession(baseURL, apiToken));
+            this.props.dispatch(navigateToSiteManager);
+        }
+        render() {
+            return <div />;
+        }
+    }
+);
index 6eac09fa0b08bd1d8191dafaa5ef9ec35c65b651..8bc5419b854fce5091401316b6d68795ce235c7f 100644 (file)
@@ -127,7 +127,8 @@ export const SearchResultsPanelView = withStyles(styles, { withTheme: true })(
                     {loggedIn.length === 1 ?
                         <span>Searching local cluster <ResourceCluster uuid={props.localCluster} /></span>
                         : <span>Searching clusters: {loggedIn.map((ss) => <span key={ss.clusterId}>
-                            <a href={props.remoteHostsConfig[ss.clusterId].workbench2Url} style={{ textDecoration: 'none' }}> <ResourceCluster uuid={ss.clusterId} /></a></span>)}</span>}
+                            <a href={props.remoteHostsConfig[ss.clusterId] && props.remoteHostsConfig[ss.clusterId].workbench2Url} style={{ textDecoration: 'none' }}> <ResourceCluster uuid={ss.clusterId} /></a>
+                        </span>)}</span>}
                     {loggedIn.length === 1 && props.localCluster !== homeCluster ?
                         <span>To search multiple clusters, <a href={props.remoteHostsConfig[homeCluster] && props.remoteHostsConfig[homeCluster].workbench2Url}> start from your home Workbench.</a></span>
                         : <span style={{ marginLeft: "2em" }}>Use <Link to={Routes.SITE_MANAGER} >Site Manager</Link> to manage which clusters will be searched.</span>}
index e75aa1f95578a091ef24f975ae4bb360c5961c5e..223e373c58187e34bd3b3f5ea8dfce7ae1c9ec88 100644 (file)
@@ -8,6 +8,7 @@ import {
     CardContent,
     CircularProgress,
     Grid,
+    IconButton,
     StyleRulesCallback,
     Table,
     TableBody,
@@ -28,6 +29,7 @@ import { addSession } from "~/store/auth/auth-action-session";
 import { SITE_MANAGER_REMOTE_HOST_VALIDATION } from "~/validators/validators";
 import { Config } from '~/common/config';
 import { ResourceCluster } from '~/views-components/data-explorer/renderers';
+import { TrashIcon } from "~/components/icon/icon";
 
 type CssRules = 'root' | 'link' | 'buttonContainer' | 'table' | 'tableRow' |
     'remoteSiteInfo' | 'buttonAdd' | 'buttonLoggedIn' | 'buttonLoggedOut' |
@@ -86,11 +88,13 @@ const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
 
 export interface SiteManagerPanelRootActionProps {
     toggleSession: (session: Session) => void;
+    removeSession: (session: Session) => void;
 }
 
 export interface SiteManagerPanelRootDataProps {
     sessions: Session[];
     remoteHostsConfig: { [key: string]: Config };
+    localClusterConfig: Config;
 }
 
 type SiteManagerPanelRootProps = SiteManagerPanelRootDataProps & SiteManagerPanelRootActionProps & WithStyles<CssRules> & InjectedFormProps;
@@ -98,7 +102,7 @@ const SITE_MANAGER_FORM_NAME = 'siteManagerForm';
 
 const submitSession = (remoteHost: string) =>
     (dispatch: Dispatch) => {
-        dispatch<any>(addSession(remoteHost)).then(() => {
+        dispatch<any>(addSession(remoteHost, undefined, true)).then(() => {
             dispatch(reset(SITE_MANAGER_FORM_NAME));
         }).catch((e: any) => {
             const errors = {
@@ -117,7 +121,7 @@ export const SiteManagerPanelRoot = compose(
         }
     }),
     withStyles(styles))
-    (({ classes, sessions, handleSubmit, toggleSession, remoteHostsConfig }: SiteManagerPanelRootProps) =>
+    (({ classes, sessions, handleSubmit, toggleSession, removeSession, localClusterConfig, remoteHostsConfig }: SiteManagerPanelRootProps) =>
         <Card className={classes.root}>
             <CardContent>
                 <Grid container direction="row">
@@ -133,9 +137,10 @@ export const SiteManagerPanelRoot = compose(
                             <TableRow className={classes.tableRow}>
                                 <TableCell>Cluster ID</TableCell>
                                 <TableCell>Host</TableCell>
-                                <TableCell>Username</TableCell>
                                 <TableCell>Email</TableCell>
+                                <TableCell>UUID</TableCell>
                                 <TableCell>Status</TableCell>
+                                <TableCell>Actions</TableCell>
                             </TableRow>
                         </TableHead>
                         <TableBody>
@@ -146,8 +151,8 @@ export const SiteManagerPanelRoot = compose(
                                         <a href={remoteHostsConfig[session.clusterId].workbench2Url} style={{ textDecoration: 'none' }}> <ResourceCluster uuid={session.clusterId} /></a>
                                         : session.clusterId}</TableCell>
                                     <TableCell>{session.remoteHost}</TableCell>
-                                    <TableCell>{validating ? <CircularProgress size={20} /> : session.username}</TableCell>
                                     <TableCell>{validating ? <CircularProgress size={20} /> : session.email}</TableCell>
+                                    <TableCell>{validating ? <CircularProgress size={20} /> : session.uuid}</TableCell>
                                     <TableCell className={classes.statusCell}>
                                         <Button fullWidth
                                             disabled={validating || session.status === SessionStatus.INVALIDATED || session.active}
@@ -156,6 +161,13 @@ export const SiteManagerPanelRoot = compose(
                                             {validating ? "Validating" : (session.loggedIn ? "Logged in" : "Logged out")}
                                         </Button>
                                     </TableCell>
+                                    <TableCell>
+                                        {session.clusterId !== localClusterConfig.uuidPrefix &&
+                                            !localClusterConfig.clusterConfig.RemoteClusters[session.clusterId] &&
+                                            <IconButton onClick={() => removeSession(session)}>
+                                                <TrashIcon />
+                                            </IconButton>}
+                                    </TableCell>
                                 </TableRow>;
                             })}
                         </TableBody>
index 0f48565d40ddd2d145cedc1351bf5214632d016e..da7ae4288b280acd1cdec275aa3741658661ab03 100644 (file)
@@ -10,19 +10,23 @@ import {
     SiteManagerPanelRootDataProps
 } from "~/views/site-manager-panel/site-manager-panel-root";
 import { Session } from "~/models/session";
-import { toggleSession } from "~/store/auth/auth-action-session";
+import { toggleSession, removeSession } from "~/store/auth/auth-action-session";
 
 const mapStateToProps = (state: RootState): SiteManagerPanelRootDataProps => {
     return {
         sessions: state.auth.sessions,
-        remoteHostsConfig: state.auth.remoteHostsConfig
+        remoteHostsConfig: state.auth.remoteHostsConfig,
+        localClusterConfig: state.auth.remoteHostsConfig[state.auth.localCluster]
     };
 };
 
 const mapDispatchToProps = (dispatch: Dispatch): SiteManagerPanelRootActionProps => ({
     toggleSession: (session: Session) => {
         dispatch<any>(toggleSession(session));
-    }
+    },
+    removeSession: (session: Session) => {
+        dispatch<any>(removeSession(session.clusterId));
+    },
 });
 
 export const SiteManagerPanel = connect(mapStateToProps, mapDispatchToProps)(SiteManagerPanelRoot);
index be543a64a5bee708cdecfd0bb65f4853008c848a..7c8b87c7f022f42db31404309bd3ad7657583272 100644 (file)
@@ -30,7 +30,6 @@ export const FedLogin = connect(mapStateToProps)(
             if (!apiToken || !user || !user.uuid.startsWith(localCluster)) {
                 return <></>;
             }
-            const [, tokenUuid, token] = apiToken.split("/");
             return <div id={"fedtoken-iframe-div"}>
                 {Object.keys(remoteHostsConfig)
                     .map((k) => {
@@ -42,7 +41,7 @@ export const FedLogin = connect(mapStateToProps)(
                             return;
                         }
                         const fedtoken = (remoteHostsConfig[k].loginCluster === localCluster)
-                            ? apiToken : getSaltedToken(k, tokenUuid, token);
+                            ? apiToken : getSaltedToken(k, apiToken);
                         return <iframe key={k} src={`${remoteHostsConfig[k].workbench2Url}/fedtoken?api_token=${fedtoken}`} style={{
                             height: 0,
                             width: 0,