moresandeep commented on a change in pull request #477:
URL: https://github.com/apache/knox/pull/477#discussion_r683551460



##########
File path: 
gateway-applications/src/main/resources/applications/webshell/app/js/webshell.js
##########
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+function WebShellClient() {
+};
+
+
+WebShellClient.prototype.connect = function (callbackFuncs) {
+    var endpoint = 'wss://localhost:8443/gateway/homepage/webshell/webshellws';

Review comment:
       hardcoded host? will the host always be local? 
   There could be cases where localhost might not be configured in hosts config 
file, perhaps 127.0.0.1

##########
File path: 
gateway-server/src/main/java/org/apache/knox/gateway/webshell/ConnectInfo.java
##########
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.knox.gateway.webshell;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+
+public abstract class ConnectInfo {

Review comment:
       Perhaps rename the class to ConnectionInfo?

##########
File path: 
gateway-server/src/main/java/org/apache/knox/gateway/webshell/WebshellWebSocketAdapter.java
##########
@@ -0,0 +1,162 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.knox.gateway.webshell;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.Locale;
+import java.util.concurrent.ExecutorService;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.eclipse.jetty.io.RuntimeIOException;
+import org.eclipse.jetty.websocket.api.Session;
+import org.eclipse.jetty.websocket.api.WebSocketAdapter;
+import org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class WebshellWebSocketAdapter extends WebSocketAdapter {
+    private static final Logger LOG = 
LoggerFactory.getLogger(WebshellWebSocketAdapter.class);
+
+    private Session session;
+
+    private final ExecutorService pool;
+
+    private ConnectInfo connectInfo;
+
+    public static final String OPERATION_CONNECT = "connect";
+    public static final String OPERATION_COMMAND = "command";
+
+    public WebshellWebSocketAdapter(ServletUpgradeRequest req, ExecutorService 
pool) {
+        super();
+        this.pool = pool;
+        //todo: validate hadoop-jwt cookie
+    }
+
+    @Override
+    public void onWebSocketConnect(final Session session) {
+        this.session = session;
+        //todo: process based works on mac but not on Centos 7, use JSch to 
connect for now
+        //this.connectInfo = new ProcessConnectInfo();
+        this.connectInfo = new JSchConnectInfo();
+        LOG.info("websocket connected.");
+    }
+
+
+    @SuppressWarnings("PMD.DoNotUseThreads")
+    @Override
+    public void onWebSocketText(final String message) {
+        LOG.info("received message "+ message);
+
+        ObjectMapper objectMapper = new ObjectMapper();
+        final WebShellData webShellData;
+        try {
+            webShellData = objectMapper.readValue(message, WebShellData.class);
+        } catch (JsonProcessingException e) {
+            LOG.error("error parsing string to WebShellData");
+            throw new RuntimeException(e);
+        }
+
+        if (OPERATION_CONNECT.equals(webShellData.getOperation())) {
+            connectInfo.connect(webShellData.getUsername());
+            pool.execute(new Runnable() {
+                @Override
+                public void run(){
+                    blockingReadFromHost();
+                }
+            });
+        } else if (OPERATION_COMMAND.equals(webShellData.getOperation())) {
+            transToHost(webShellData.getCommand());
+        } else {
+            String err = String.format(Locale.ROOT, "Operation %s not 
supported",webShellData.getOperation());
+            LOG.error(err);
+            throw new UnsupportedOperationException(err);
+        }
+    }
+

Review comment:
       We should also have a method of binary transport.

##########
File path: 
gateway-server/src/main/java/org/apache/knox/gateway/webshell/JSchConnectInfo.java
##########
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.knox.gateway.webshell;
+
+import com.jcraft.jsch.Channel;
+import com.jcraft.jsch.JSch;
+import com.jcraft.jsch.JSchException;
+import com.jcraft.jsch.Session;
+import org.eclipse.jetty.io.RuntimeIOException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+import java.util.Properties;
+
+/**
+ * data structure to store a connection session
+ */
+public class JSchConnectInfo extends ConnectInfo {
+
+    private JSch jsch;
+    private Channel channel;
+    private Session jschSession;
+    private static final Logger LOG = 
LoggerFactory.getLogger(JSchConnectInfo.class);
+    public JSchConnectInfo(){
+        try {
+            jsch = new JSch();
+            Properties config = new Properties();
+            config.put("StrictHostKeyChecking", "no");
+            jschSession = jsch.getSession("knoxui", "localhost", 22);
+            jschSession.setConfig(config);
+            jschSession.setPassword("knoxui");
+        }catch (JSchException e){
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Override
+    public void connect(String username){
+        try {
+            jschSession.connect(30000);
+            channel = jschSession.openChannel("shell");
+            inputStream = channel.getInputStream();
+            outputStream = channel.getOutputStream();
+            channel.connect(30000);
+            String sudoCmd = "exec sudo -u "+ username + " -H bash -i\ncd 
$HOME\nwhoami\n";
+            outputStream.write(sudoCmd.getBytes(StandardCharsets.UTF_8));
+            outputStream.flush();
+            checkConnection(username);
+        }  catch (JSchException|IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    private void checkConnection(String username) throws IOException{

Review comment:
       I am not sure what this method is doing, is this validating connection 
based on just username?

##########
File path: gateway-server/pom.xml
##########
@@ -409,6 +409,19 @@
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
         </dependency>
+       <dependency>
+            <groupId>de.thetaphi</groupId>
+            <artifactId>forbiddenapis</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.jcraft</groupId>
+            <artifactId>jsch</artifactId>
+            <version>0.1.54</version>
+       </dependency>
+       <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>

Review comment:
       Why do we need mysql-connector-java dependency?

##########
File path: 
gateway-server/src/main/java/org/apache/knox/gateway/webshell/JSchConnectInfo.java
##########
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.knox.gateway.webshell;
+
+import com.jcraft.jsch.Channel;
+import com.jcraft.jsch.JSch;
+import com.jcraft.jsch.JSchException;
+import com.jcraft.jsch.Session;
+import org.eclipse.jetty.io.RuntimeIOException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+import java.util.Properties;
+
+/**
+ * data structure to store a connection session
+ */
+public class JSchConnectInfo extends ConnectInfo {
+
+    private JSch jsch;
+    private Channel channel;
+    private Session jschSession;
+    private static final Logger LOG = 
LoggerFactory.getLogger(JSchConnectInfo.class);
+    public JSchConnectInfo(){
+        try {
+            jsch = new JSch();
+            Properties config = new Properties();

Review comment:
       We cannot have hardcoded properties especially usernames and passwords 
:) 

##########
File path: 
gateway-server/src/main/java/org/apache/knox/gateway/webshell/ProcessConnectInfo.java
##########
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.knox.gateway.webshell;
+
+import org.eclipse.jetty.io.RuntimeIOException;
+import de.thetaphi.forbiddenapis.SuppressForbidden;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+
+/**
+* data structure to store a connection session
+*/
+public class ProcessConnectInfo extends ConnectInfo {
+
+
+    private Process process;
+    private static final Logger LOG = 
LoggerFactory.getLogger(ProcessConnectInfo.class);
+
+    @SuppressForbidden
+    @Override
+    public void connect(String username){
+        try {
+            ProcessBuilder builder = new 
ProcessBuilder("sudo","-u",username,"-H","bash","-i");

Review comment:
       Not sure if we can use sudo when knox is running as non root.

##########
File path: 
gateway-server/src/main/java/org/apache/knox/gateway/webshell/JSchConnectInfo.java
##########
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.knox.gateway.webshell;
+
+import com.jcraft.jsch.Channel;
+import com.jcraft.jsch.JSch;
+import com.jcraft.jsch.JSchException;
+import com.jcraft.jsch.Session;
+import org.eclipse.jetty.io.RuntimeIOException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+import java.util.Properties;
+
+/**
+ * data structure to store a connection session
+ */
+public class JSchConnectInfo extends ConnectInfo {
+
+    private JSch jsch;
+    private Channel channel;
+    private Session jschSession;
+    private static final Logger LOG = 
LoggerFactory.getLogger(JSchConnectInfo.class);
+    public JSchConnectInfo(){
+        try {
+            jsch = new JSch();
+            Properties config = new Properties();
+            config.put("StrictHostKeyChecking", "no");
+            jschSession = jsch.getSession("knoxui", "localhost", 22);
+            jschSession.setConfig(config);
+            jschSession.setPassword("knoxui");
+        }catch (JSchException e){
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Override
+    public void connect(String username){
+        try {
+            jschSession.connect(30000);
+            channel = jschSession.openChannel("shell");
+            inputStream = channel.getInputStream();
+            outputStream = channel.getOutputStream();
+            channel.connect(30000);
+            String sudoCmd = "exec sudo -u "+ username + " -H bash -i\ncd 
$HOME\nwhoami\n";

Review comment:
       What happens if the process is running as non-root?  Knox process cannot 
be run as root this cause issues here.

##########
File path: 
gateway-server/src/main/java/org/apache/knox/gateway/webshell/JSchConnectInfo.java
##########
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.knox.gateway.webshell;
+
+import com.jcraft.jsch.Channel;
+import com.jcraft.jsch.JSch;
+import com.jcraft.jsch.JSchException;
+import com.jcraft.jsch.Session;
+import org.eclipse.jetty.io.RuntimeIOException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+import java.util.Properties;
+
+/**
+ * data structure to store a connection session
+ */
+public class JSchConnectInfo extends ConnectInfo {
+
+    private JSch jsch;
+    private Channel channel;
+    private Session jschSession;
+    private static final Logger LOG = 
LoggerFactory.getLogger(JSchConnectInfo.class);
+    public JSchConnectInfo(){
+        try {
+            jsch = new JSch();
+            Properties config = new Properties();
+            config.put("StrictHostKeyChecking", "no");
+            jschSession = jsch.getSession("knoxui", "localhost", 22);
+            jschSession.setConfig(config);
+            jschSession.setPassword("knoxui");
+        }catch (JSchException e){
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Override
+    public void connect(String username){
+        try {
+            jschSession.connect(30000);

Review comment:
       All the properties should be configurable :) 

##########
File path: 
gateway-server/src/main/java/org/apache/knox/gateway/webshell/ProcessConnectInfo.java
##########
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.knox.gateway.webshell;
+
+import org.eclipse.jetty.io.RuntimeIOException;
+import de.thetaphi.forbiddenapis.SuppressForbidden;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+
+/**
+* data structure to store a connection session
+*/
+public class ProcessConnectInfo extends ConnectInfo {
+
+
+    private Process process;
+    private static final Logger LOG = 
LoggerFactory.getLogger(ProcessConnectInfo.class);
+
+    @SuppressForbidden
+    @Override
+    public void connect(String username){
+        try {
+            ProcessBuilder builder = new 
ProcessBuilder("sudo","-u",username,"-H","bash","-i");
+            builder.redirectErrorStream(true); // combine stderr with stdout
+            process = builder.start();
+            inputStream = process.getInputStream();
+            outputStream = process.getOutputStream();

Review comment:
       I feel we shold actively save references to all the processes created in 
events like 
   1. We need to terminate them safely
   2. Make sure we don't spwan too many of them (throttle may be)
   3. For future use display them
   4. 4. Control them from api/ui




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to