336699github commented on a change in pull request #477: URL: https://github.com/apache/knox/pull/477#discussion_r683710398
########## 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: say the process is running as `knox` we should preconfigure(as root) `/etc/sudoers.d/knox` to say `knox <host-name>=(*:<group-name>) NOPASSWD: /usr/bin/bash ` which means allowing `knox` to run `bash` on `<host-name>` as any user in` <group-name>` without needing password. or it could indicate specific usernames like `knox <host-name>=(sam:sam's group) NOPASSWD: /usr/bin/bash` this would limit knox to ONLY run those commands as root. anything else will be reported for extra safety, i think we could also add `knox ALL=(ALL) !ALL` at the top of the file, which disallows knox to run all commands as root first, then allowing only the desired commands. here is the reference: https://www.sudo.ws/man/1.8.15/sudoers.man.html let me know what you think :D -- 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]
