disable viewing history commands without auth #2620

This commit is contained in:
hengyunabc 2023-08-24 19:51:12 +08:00
parent 693fe71af6
commit db0b3bb13d
2 changed files with 77 additions and 5 deletions

View File

@ -0,0 +1,53 @@
package com.taobao.arthas.core.shell.term.impl;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import com.taobao.arthas.common.ArthasConstants;
import com.taobao.arthas.core.shell.session.Session;
import io.termd.core.readline.Function;
import io.termd.core.readline.Readline;
import io.termd.core.readline.Readline.Interaction;
/**
* 拦截指定的 Function apply 函数
*
* @author hengyunabc 2023-08-24
*
*/
public class FunctionInvocationHandler implements InvocationHandler {
private TermImpl termImpl;
private Function target;
public FunctionInvocationHandler(TermImpl termImpl, Function target) {
this.termImpl = termImpl;
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String name = method.getName();
if (name.equals("apply")) {
Session session = termImpl.getSession();
if (session != null) {
boolean authenticated = session.get(ArthasConstants.SUBJECT_KEY) != null;
if (authenticated) {
return method.invoke(target, args);
} else {
Readline.Interaction interaction = (Interaction) args[0];
// 必要
interaction.resume();
return null;
}
}
}
return method.invoke(target, args);
}
}

View File

@ -1,10 +1,15 @@
package com.taobao.arthas.core.shell.term.impl;
import java.io.File;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
import com.taobao.arthas.core.shell.cli.Completion;
import com.taobao.arthas.core.shell.handlers.Handler;
import com.taobao.arthas.core.shell.handlers.term.CloseHandlerWrapper;
import com.taobao.arthas.core.shell.handlers.term.DefaultTermStdinHandler;
import com.taobao.arthas.core.shell.handlers.term.EventHandler;
import com.taobao.arthas.core.shell.handlers.Handler;
import com.taobao.arthas.core.shell.handlers.term.RequestHandler;
import com.taobao.arthas.core.shell.handlers.term.SizeHandlerWrapper;
import com.taobao.arthas.core.shell.handlers.term.StdinHandlerWrapper;
@ -13,17 +18,15 @@ import com.taobao.arthas.core.shell.term.SignalHandler;
import com.taobao.arthas.core.shell.term.Term;
import com.taobao.arthas.core.util.Constants;
import com.taobao.arthas.core.util.FileUtils;
import io.termd.core.function.Consumer;
import io.termd.core.readline.Function;
import io.termd.core.readline.Keymap;
import io.termd.core.readline.Readline;
import io.termd.core.readline.functions.HistorySearchForward;
import io.termd.core.tty.TtyConnection;
import io.termd.core.util.Helper;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
*/
@ -50,6 +53,18 @@ public class TermImpl implements Term {
readline = new Readline(keymap);
readline.setHistory(FileUtils.loadCommandHistory(new File(Constants.CMD_HISTORY_FILE)));
for (Function function : readlineFunctions) {
/**
* 防止没有鉴权时查看历史命令
*
* @see io.termd.core.readline.functions.HistorySearchForward
*/
if (function.name().contains("history")) {
FunctionInvocationHandler funcHandler = new FunctionInvocationHandler(this, function);
function = (Function) Proxy.newProxyInstance(this.getClass().getClassLoader(),
HistorySearchForward.class.getInterfaces(), funcHandler);
}
readline.addFunction(function);
}
@ -64,6 +79,10 @@ public class TermImpl implements Term {
return this;
}
public Session getSession() {
return session;
}
@Override
public void readline(String prompt, Handler<String> lineHandler) {
if (conn.getStdinHandler() != echoHandler) {