use of io.mycat.MycatUser in project Mycat2 by MyCATApache.
the class MycatContext method getCurrentUser.
public String getCurrentUser() {
MycatUser user = CONTEXT.get().getUser();
Authenticator authenticator = MetaClusterCurrent.wrapper(Authenticator.class);
return user.getUserName() + "@" + authenticator.getUserInfo(user.getUserName()).getIp();
}
use of io.mycat.MycatUser in project Mycat2 by MyCATApache.
the class ShowProcessListSQLHandler method onExecute.
@Override
protected Future<Void> onExecute(SQLRequest<MySqlShowProcessListStatement> request, MycatDataContext dataContext, Response response) {
Map<Thread, Process> processMap = new LinkedHashMap<>(Process.getProcessMap());
MySqlShowProcessListStatement ast = request.getAst();
boolean full = ast.isFull();
int maxCount = full ? Integer.MAX_VALUE : 100;
MycatUser currentUser = full ? null : dataContext.getUser();
ResultSetBuilder resultSetBuilder = ResultSetBuilder.create();
resultSetBuilder.addColumnInfo("Id", JDBCType.INTEGER);
resultSetBuilder.addColumnInfo("User", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("Host", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("db", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("Command", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("Time", JDBCType.BIGINT);
resultSetBuilder.addColumnInfo("State", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("Info", JDBCType.VARCHAR);
long timestamp = System.currentTimeMillis();
int currentCount = 0;
for (Map.Entry<Thread, Process> entry : processMap.entrySet()) {
Thread holdThread = entry.getKey();
Process process = entry.getValue();
if (currentUser != null && !Objects.equals(process.getUser(), currentUser.getUserName())) {
continue;
}
resultSetBuilder.addObjectRowPayload(Arrays.asList(process.getId(), process.getUser(), process.getHost(), process.getDb(), process.getCommand(), timestamp - process.getCreateTimestamp().getTime(), process.getState(), process.getInfo()));
currentCount++;
if (currentCount >= maxCount) {
break;
}
}
return response.sendResultSet(resultSetBuilder.build());
// return response.proxySelectToPrototype(request.getAst().toString());
}
use of io.mycat.MycatUser in project Mycat2 by MyCATApache.
the class MySQLClientAuthHandler method onSocketRead.
// public void setMycatSession(MycatSession mycatSession) {
// this.mycat = mycatSession;
// }
@Override
public void onSocketRead(MycatSession mycat) {
try {
if (mycat.getCurNIOHandler() != this) {
return;
}
if (!mycat.readFromChannel()) {
return;
}
mycat.setResponseFinished(ProcessState.READY);
// MycatSecurityConfig securityManager = runtime.getSecurityManager();
byte[] password = new byte[] {};
if (!isChangeAuthPlugin) {
// 密码读取与验证
this.auth = readResponseAuthPacket(mycat);
String authPluginName = auth.getAuthPluginName();
int capabilities = auth.getCapabilities();
// 切换auth_plugin
if (MySQLServerCapabilityFlags.isPluginAuth(capabilities) && !authPluginName.equals(clientAuthPluginName)) {
// 发送切换包的auth_response
isChangeAuthPlugin = true;
AuthSwitchRequestPacket authSwitchRequestPacket = new AuthSwitchRequestPacket();
clientAuthPluginName = StringUtil.isEmpty(authPluginName) ? MysqlNativePasswordPluginUtil.PROTOCOL_PLUGIN_NAME : authPluginName;
authSwitchRequestPacket.setAuthPluginName(MysqlNativePasswordPluginUtil.PROTOCOL_PLUGIN_NAME);
authSwitchRequestPacket.setStatus((byte) 0xfe);
authSwitchRequestPacket.setAuthPluginData(new String(seed));
MySQLPayloadWriter mySQLPayloadWriter = new MySQLPayloadWriter(1024);
authSwitchRequestPacket.writePayload(mySQLPayloadWriter);
mycat.writeBytes(mySQLPayloadWriter.toByteArray(), true);
return;
}
// 握手包中的加密密码
password = auth.getPassword();
} else {
MySQLPacket mySQLPacket = mycat.currentProxyPayload();
password = mySQLPacket.readEOFStringBytes();
}
int capabilities = auth.getCapabilities();
if (MySQLServerCapabilityFlags.isCanUseCompressionProtocol(capabilities)) {
String message = "Can Not Use Compression Protocol!";
failture(mycat, MySQLErrorCode.ER_UNKNOWN_ERROR, message);
mycat.lazyClose(true, message);
return;
}
String username = auth.getUsername();
int maxPacketSize = auth.getMaxPacketSize();
String database = auth.getDatabase();
int characterSet = auth.getCharacterSet();
Map<String, String> attrs = auth.getClientConnectAttrs();
MycatUser user = null;
SocketAddress remoteSocketAddress = mycat.channel().socket().getRemoteSocketAddress();
Authenticator authenticator = MetaClusterCurrent.wrapper(Authenticator.class);
String ip = SocketAddressUtil.simplySocketAddress(remoteSocketAddress);
Authenticator.AuthInfo authInfo = authenticator.getPassword(username, ip);
if (!authInfo.isOk()) {
failture(mycat, authInfo.getErrorCode(), authInfo.getException());
return;
} else {
String rightPassword = authInfo.getRightPassword();
if (rightPassword != null) {
if (!checkPassword(rightPassword, password)) {
// may be bug
String message = "Access denied for user '" + username + "'@'" + remoteSocketAddress.toString() + "' (using password: YES)";
mycat.setLastMessage(message);
mycat.setLastErrorCode(ER_ACCESS_DENIED_ERROR);
LOGGER.error("login fail: {}", message);
mycat.writeErrorEndPacketBySyncInProcessError(ER_ACCESS_DENIED_ERROR);
LOGGER.error("remoteSocketAddress:{} password is wrong", remoteSocketAddress);
return;
}
}
UserConfig userInfo = authenticator.getUserInfo(username);
user = new MycatUser(username, null, null, ip, remoteSocketAddress, userInfo);
}
mycat.getDataContext().setUser(user);
mycat.setSchema(database);
mycat.setServerCapabilities(auth.getCapabilities());
mycat.setIsolation(MySQLIsolation.REPEATED_READ);
mycat.setCharset(characterSet);
finished = true;
mycatSessionManager.initCommandDispatcher(mycat);
mycat.writeOkEndPacket();
} catch (Exception e) {
LOGGER.error("", e);
MycatMonitor.onAuthHandlerReadException(mycat, e);
onClear(mycat);
failture(mycat, e);
}
}
Aggregations