17229: Webshell use localstorage if available
[arvados.git] / src / views / virtual-machine-panel / virtual-machine-user-panel.tsx
index a641ec63cd8b8c2a687e6fb68755ad7f2ecb17e2..196cba1643606c86a2e3953a6ca154664d543847 100644 (file)
@@ -2,19 +2,23 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import * as React from 'react';
+import React from 'react';
 import { connect } from 'react-redux';
 import { Grid, Typography, Button, Card, CardContent, TableBody, TableCell, TableHead, TableRow, Table, Tooltip } from '@material-ui/core';
 import { StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core/styles';
-import { ArvadosTheme } from '~/common/custom-theme';
+import { ArvadosTheme } from 'common/custom-theme';
 import { compose, Dispatch } from 'redux';
-import { saveRequestedDate, loadVirtualMachinesUserData } from '~/store/virtual-machines/virtual-machines-actions';
-import { RootState } from '~/store/store';
-import { ListResults } from '~/services/common-service/common-service';
-import { HelpIcon } from '~/components/icon/icon';
+import { saveRequestedDate, loadVirtualMachinesUserData } from 'store/virtual-machines/virtual-machines-actions';
+import { RootState } from 'store/store';
+import { ListResults } from 'services/common-service/common-service';
+import { HelpIcon } from 'components/icon/icon';
+import { SESSION_STORAGE } from "services/auth-service/auth-service";
+// import * as CopyToClipboard from 'react-copy-to-clipboard';
 
 type CssRules = 'button' | 'codeSnippet' | 'link' | 'linkIcon' | 'rightAlign' | 'cardWithoutMachines' | 'icon';
 
+const EXTRA_TOKEN = "exra";
+
 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
     button: {
         marginTop: theme.spacing.unit,
@@ -59,7 +63,10 @@ const mapStateToProps = (state: RootState) => {
         requestedDate: state.virtualMachines.date,
         userUuid: state.auth.user!.uuid,
         helpText: state.auth.config.clusterConfig.Workbench.SSHHelpPageHTML,
-        webShell: state.auth.config.clusterConfig.Services.WebShell.ExternalURL,
+        hostSuffix: state.auth.config.clusterConfig.Workbench.SSHHelpHostSuffix || "",
+        token: state.auth.extraApiToken || state.auth.apiToken || '',
+        tokenLocation: state.auth.extraApiToken ? EXTRA_TOKEN : (state.auth.apiTokenLocation || ''),
+        webshellUrl: state.auth.config.clusterConfig.Services.WebShell.ExternalURL,
         ...state.virtualMachines
     };
 };
@@ -75,7 +82,10 @@ interface VirtualMachinesPanelDataProps {
     userUuid: string;
     links: ListResults<any>;
     helpText: string;
-    webShell: string;
+    hostSuffix: string;
+    token: string;
+    tokenLocation: string,
+    webshellUrl: string;
 }
 
 interface VirtualMachinesPanelActionProps {
@@ -131,7 +141,7 @@ const CardContentWithVirtualMachines = (props: VirtualMachineProps) =>
                         {virtualMachineSendRequest(props)}
                     </div>
                     <div className={props.classes.icon}>
-                        <a href="https://doc.arvados.org/user/getting_started/vm-login-with-webshell.html" target="_blank" className={props.classes.linkIcon}>
+                        <a href="https://doc.arvados.org/user/getting_started/vm-login-with-webshell.html" target="_blank" rel="noopener noreferrer" className={props.classes.linkIcon}>
                             <Tooltip title="Access VM using webshell">
                                 <HelpIcon />
                             </Tooltip>
@@ -162,29 +172,38 @@ const virtualMachinesTable = (props: VirtualMachineProps) =>
                 <TableCell>Host name</TableCell>
                 <TableCell>Login name</TableCell>
                 <TableCell>Command line</TableCell>
-                {props.webShell !== "" && <TableCell>Web shell</TableCell>}
+                <TableCell>Web shell</TableCell>
             </TableRow>
         </TableHead>
         <TableBody>
-            {props.virtualMachines.items.map((it, index) =>
-                <TableRow key={index}>
-                    <TableCell>{it.hostname}</TableCell>
-                    <TableCell>{getUsername(props.links, props.userUuid)}</TableCell>
-                    <TableCell>ssh {getUsername(props.links, props.userUuid)}@{it.hostname}</TableCell>
-                    {props.webShell !== "" && <TableCell>
-                        <a href={`${props.webShell}${it.href}/webshell/${getUsername(props.links, props.userUuid)}`} target="_blank" className={props.classes.link}>
-                            Log in as {getUsername(props.links, props.userUuid)}
-                        </a>
-                    </TableCell>}
-                </TableRow>
-            )}
+            {props.virtualMachines.items.map(it =>
+                props.links.items.map(lk => {
+                    if (lk.tailUuid === props.userUuid) {
+                        const username = lk.properties.username;
+                        const command = `ssh ${username}@${it.hostname}${props.hostSuffix}`;
+                        let tokenParam = "";
+                        if (props.tokenLocation === SESSION_STORAGE || props.tokenLocation === EXTRA_TOKEN) {
+                          tokenParam = `&token=${encodeURIComponent(props.token)}`;
+                        }
+                        return <TableRow key={lk.uuid}>
+                            <TableCell>{it.hostname}</TableCell>
+                            <TableCell>{username}</TableCell>
+                            <TableCell>
+                                {command}
+                            </TableCell>
+                            <TableCell>
+                                <a href={`/webshell/?host=${encodeURIComponent(props.webshellUrl + '/' + it.hostname)}&login=${encodeURIComponent(username)}${tokenParam}`} target="_blank" rel="noopener noreferrer" className={props.classes.link}>
+                                    Log in as {username}
+                                </a>
+                            </TableCell>
+                        </TableRow>;
+                    }
+                    return null;
+                }
+                ))}
         </TableBody>
     </Table>;
 
-const getUsername = (links: ListResults<any>, userUuid: string) => {
-    return links.items.map(it => it.tailUuid === userUuid ? it.properties.username : '');
-};
-
 const CardSSHSection = (props: VirtualMachineProps) =>
     <Grid item xs={12}>
         <Card>