You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@knox.apache.org by GitBox <gi...@apache.org> on 2021/11/11 03:49:28 UTC

[GitHub] [knox] luliu8 commented on a change in pull request #477: [WIP] KNOX-2631

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



##########
File path: gateway-server/src/main/java/org/apache/knox/gateway/webshell/WebshellWebSocketAdapter.java
##########
@@ -0,0 +1,161 @@
+/*
+ * 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.concurrent.ExecutorService;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.knox.gateway.config.GatewayConfig;
+import org.apache.knox.gateway.services.GatewayServices;
+import org.apache.knox.gateway.services.security.token.UnknownTokenException;
+import org.apache.knox.gateway.websockets.ProxyWebSocketAdapter;
+import org.eclipse.jetty.websocket.api.Session;
+import org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest;
+
+public class WebshellWebSocketAdapter extends ProxyWebSocketAdapter  {
+    private Session session;
+    private ConnectionInfo connectionInfo;
+    private String username;
+
+    public WebshellWebSocketAdapter(ServletUpgradeRequest req,  ExecutorService pool, GatewayConfig config, GatewayServices services) {
+        super(null, pool, null, config);
+        // todo: may not need to get username from request URI, username is contained in JWT token
+        if (config.isWebShellEnabled()) {
+            username = req.getRequestURI().getRawQuery();
+            try {
+                WebShellTokenUtils.validateJWT(req, services, config);
+            } catch (final UnknownTokenException e) {
+                LOG.onError("no valid token found");
+                // todo: how to handle validation error?
+                throw new RuntimeException(e);
+            }
+            this.connectionInfo = new ProcessConnectionInfo(username);
+        }
+        else {
+            throw new RuntimeException("webshell not enabled");
+        }
+    }
+
+    @Override
+    public void onWebSocketConnect(final Session session) {
+        this.session = session;
+        LOG.debugLog("websocket connected.");
+        connectionInfo.connect();
+        pool.execute(new Runnable() {
+            @Override
+            public void run() {
+                blockingReadFromHost();
+            }
+        });
+    }
+
+    // this function will block, should be run in an asynchronous thread
+    private void blockingReadFromHost(){
+        LOG.debugLog("start listening to bash process");
+        byte[] buffer = new byte[1024];
+        int bytesRead;
+        try {
+            // todo: maybe change to non-blocking read using java.nio
+            while ((bytesRead = connectionInfo.getInputStream().read(buffer)) != -1) {
+                transToClient(new String(buffer, 0, bytesRead, StandardCharsets.UTF_8));
+            }
+        } catch (IOException e){
+            transToClient("bash process terminated unexpectedly");
+        } finally {
+            cleanupOnError("bash process terminated unexpectedly");
+        }
+    }
+
+    @SuppressWarnings("PMD.DoNotUseThreads")
+    @Override
+    public void onWebSocketText(final String message) {
+        LOG.debugLog("received message "+ message);
+

Review comment:
       > Let's check for token expiry here.
   does each message carry the JWT with it?
   
   
   




-- 
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: dev-unsubscribe@knox.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org