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/04 15:39:13 UTC

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

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



##########
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();

Review comment:
       How is this a username?

##########
File path: gateway-server/src/main/java/org/apache/knox/gateway/websockets/GatewayWebsocketHandler.java
##########
@@ -111,16 +119,23 @@ public void configure(final WebSocketServletFactory factory) {
 
   @Override
   public Object createWebSocket(ServletUpgradeRequest req,
-      ServletUpgradeResponse resp) {
+                                ServletUpgradeResponse resp) {
 
     try {
       final URI requestURI = req.getRequestURI();
 
-      /* URL used to connect to websocket backend */
+      // Handle webshell websocket request
+      if (StringUtils.endsWith(requestURI.getRawPath(), "/webshell/webshellws")){
+        // rawPath = /gateway/homepage/webshell/webshellws
+        LOG.debugLog("received websocket request for webshhell: "+ requestURI.getRawPath()+" query:"+requestURI.getRawQuery());
+        return new WebshellWebSocketAdapter(req, pool, config, services);

Review comment:
       instead of passing req, we should add jwt-hadoop token to clientConfig and pass it here.
   Adding jwt-hadoop token to clientConfig  will be beneficial for non webshell requests too

##########
File path: gateway-server/src/main/java/org/apache/knox/gateway/webshell/WebShellTokenUtils.java
##########
@@ -0,0 +1,247 @@
+package org.apache.knox.gateway.webshell;
+
+import com.nimbusds.jose.JWSHeader;
+import org.apache.knox.gateway.config.GatewayConfig;
+import org.apache.knox.gateway.i18n.messages.MessagesFactory;
+//todo: add gateway-provider-security-jwt dependency?
+import org.apache.knox.gateway.provider.federation.jwt.JWTMessages;
+import org.apache.knox.gateway.provider.federation.jwt.filter.SignatureVerificationCache;
+import org.apache.knox.gateway.services.GatewayServices;
+import org.apache.knox.gateway.services.ServiceType;
+import org.apache.knox.gateway.services.security.token.JWTokenAuthority;
+import org.apache.knox.gateway.services.security.token.TokenMetadata;
+import org.apache.knox.gateway.services.security.token.TokenServiceException;
+import org.apache.knox.gateway.services.security.token.TokenStateService;
+import org.apache.knox.gateway.services.security.token.TokenUtils;
+import org.apache.knox.gateway.services.security.token.UnknownTokenException;
+import org.apache.knox.gateway.services.security.token.impl.JWT;
+import org.apache.knox.gateway.services.security.token.impl.JWTToken;
+import org.apache.knox.gateway.services.topology.TopologyService;
+import org.apache.knox.gateway.topology.Service;
+import org.apache.knox.gateway.topology.Topology;
+import org.apache.knox.gateway.util.CertificateUtils;
+import org.apache.knox.gateway.util.Tokens;
+import org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest;
+import java.net.HttpCookie;
+import java.security.interfaces.RSAPublicKey;
+import java.text.ParseException;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+
+public class WebShellTokenUtils {
+    private static final String KNOXSSO_COOKIE_NAME = "knoxsso.cookie.name";
+    private static final String DEFAULT_SSO_COOKIE_NAME = "hadoop-jwt";
+    private static final String JWT_DEFAULT_ISSUER = "KNOXSSO";
+    private static final String JWT_EXPECTED_ISSUER = "jwt.expected.issuer";
+    private static final String JWT_DEFAULT_SIGALG = "RS256";
+    private static final String JWT_EXPECTED_SIGALG = "jwt.expected.sigalg";
+    private static final String SSO_VERIFICATION_PEM = "sso.token.verification.pem";
+
+    private static JWTMessages log = MessagesFactory.get(JWTMessages.class);
+    private static String cookieName;
+    private static String expectedIssuer;
+    private static String expectedSigAlg;
+    private static TokenStateService tokenStateService;
+    private static JWTokenAuthority authorityService;
+    private static RSAPublicKey publicKey;
+    // todo: how to initialize these two attributes.
+    private static SignatureVerificationCache signatureVerificationCache;
+    private static String expectedJWKSUrl;
+
+    public static void validateJWT(ServletUpgradeRequest req, GatewayServices services, GatewayConfig gatewayConfig) throws UnknownTokenException {
+        configureParameters(services, gatewayConfig);
+        List<HttpCookie> ssoCookies = req.getCookies();
+        for (HttpCookie ssoCookie : ssoCookies) {
+            if (cookieName.equals(ssoCookie.getName())) {
+                try {
+                    JWT token = new JWTToken(ssoCookie.getValue());
+                    if (validateToken(token)) {
+                        // we found a valid cookie we don't need to keep checking anymore
+                        return;
+                    }
+                } catch (ParseException | UnknownTokenException ignore) {
+                    // Ignore the error since cookie was invalid
+                    // Fall through to keep checking if there are more cookies
+                }
+            }
+        }
+        // no cookie contain a valid JWT
+        throw new UnknownTokenException("No valid JWT found for webshell");
+    }
+
+    private static void configureParameters(GatewayServices services, GatewayConfig gatewayConfig) {

Review comment:
       We should check if parameters are already configured, if they are then no need to call this again. One thing to note is that we need to check if topology has reloaded, we can check that using `topology.getTimestamp()` method

##########
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);

Review comment:
       This should be a boolean.

##########
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");

Review comment:
       The error should be something meaningful, we also need a LOG.error entry

##########
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.

##########
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;

Review comment:
       Need to check for valid JWT here.

##########
File path: gateway-server/src/main/java/org/apache/knox/gateway/webshell/WebShellTokenUtils.java
##########
@@ -0,0 +1,247 @@
+package org.apache.knox.gateway.webshell;
+
+import com.nimbusds.jose.JWSHeader;
+import org.apache.knox.gateway.config.GatewayConfig;
+import org.apache.knox.gateway.i18n.messages.MessagesFactory;
+//todo: add gateway-provider-security-jwt dependency?
+import org.apache.knox.gateway.provider.federation.jwt.JWTMessages;
+import org.apache.knox.gateway.provider.federation.jwt.filter.SignatureVerificationCache;
+import org.apache.knox.gateway.services.GatewayServices;
+import org.apache.knox.gateway.services.ServiceType;
+import org.apache.knox.gateway.services.security.token.JWTokenAuthority;
+import org.apache.knox.gateway.services.security.token.TokenMetadata;
+import org.apache.knox.gateway.services.security.token.TokenServiceException;
+import org.apache.knox.gateway.services.security.token.TokenStateService;
+import org.apache.knox.gateway.services.security.token.TokenUtils;
+import org.apache.knox.gateway.services.security.token.UnknownTokenException;
+import org.apache.knox.gateway.services.security.token.impl.JWT;
+import org.apache.knox.gateway.services.security.token.impl.JWTToken;
+import org.apache.knox.gateway.services.topology.TopologyService;
+import org.apache.knox.gateway.topology.Service;
+import org.apache.knox.gateway.topology.Topology;
+import org.apache.knox.gateway.util.CertificateUtils;
+import org.apache.knox.gateway.util.Tokens;
+import org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest;
+import java.net.HttpCookie;
+import java.security.interfaces.RSAPublicKey;
+import java.text.ParseException;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+
+public class WebShellTokenUtils {

Review comment:
       We can make this simple by just relying on tokenstateservice. The assumption here is that Knox issues the token and put it in the tokenstateservice. All we need to do is check the token timestamp and expiry. We need to run this by @pzampino and @smolnar82 

##########
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);

Review comment:
       Let's just save hadoop-jwt token here for future use and implement the logic in onConnect() method.
   By moving the logic we ensure that ongoing webshell instances check for valid jwt tokens and the token expiry is honored. 




-- 
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