use of com.actiontech.dble.config.model.UserConfig in project dble by actiontech.
the class ShowWhiteHost method execute.
public static void execute(ManagerConnection c) {
ByteBuffer buffer = c.allocate();
// write header
buffer = HEADER.write(buffer, c, true);
// write fields
for (FieldPacket field : FIELDS) {
buffer = field.write(buffer, c, true);
}
// write eof
buffer = EOF.write(buffer, c, true);
// write rows
byte packetId = EOF.getPacketId();
Map<String, List<UserConfig>> map = DbleServer.getInstance().getConfig().getFirewall().getWhitehost();
for (Map.Entry<String, List<UserConfig>> entry : map.entrySet()) {
List<UserConfig> userConfigs = entry.getValue();
StringBuilder users = new StringBuilder();
for (int i = 0; i < userConfigs.size(); i++) {
if (i > 0) {
users.append(",");
}
users.append(userConfigs.get(i).getName());
}
RowDataPacket row = getRow(entry.getKey(), users.toString(), c.getCharset().getResults());
row.setPacketId(++packetId);
buffer = row.write(buffer, c, true);
}
// write last eof
EOFPacket lastEof = new EOFPacket();
lastEof.setPacketId(++packetId);
buffer = lastEof.write(buffer, c, true);
// write buffer
c.write(buffer);
}
use of com.actiontech.dble.config.model.UserConfig in project dble by actiontech.
the class FirewallConfigLoader method load.
public void load(Element root, XMLServerLoader xsl, boolean isLowerCaseTableNames) throws IllegalAccessException, InvocationTargetException {
FirewallConfig firewall = xsl.getFirewall();
Map<String, UserConfig> users = xsl.getUsers();
NodeList list = root.getElementsByTagName("host");
Map<String, List<UserConfig>> whitehost = new HashMap<>();
for (int i = 0, n = list.getLength(); i < n; i++) {
Node node = list.item(i);
if (node instanceof Element) {
Element e = (Element) node;
String host = e.getAttribute("host").trim();
String userStr = e.getAttribute("user").trim();
if (firewall.existsHost(host)) {
throw new ConfigException("host duplicated : " + host);
}
String[] arrayUsers = userStr.split(",");
List<UserConfig> userConfigs = new ArrayList<>();
for (String user : arrayUsers) {
UserConfig uc = users.get(user);
if (null == uc) {
throw new ConfigException("[user: " + user + "] doesn't exist in [host: " + host + "]");
}
if (!uc.isManager() && (uc.getSchemas() == null || uc.getSchemas().size() == 0)) {
throw new ConfigException("[host: " + host + "] contains one root privileges user: " + user);
}
userConfigs.add(uc);
}
whitehost.put(host, userConfigs);
}
}
firewall.setWhitehost(whitehost);
WallConfig wallConfig = new WallConfig();
NodeList blacklist = root.getElementsByTagName("blacklist");
for (int i = 0, n = blacklist.getLength(); i < n; i++) {
Node node = blacklist.item(i);
if (node instanceof Element) {
Element e = (Element) node;
String check = e.getAttribute("check");
if (null != check) {
firewall.setBlackListCheck(Boolean.parseBoolean(check));
}
Map<String, Object> props = ConfigUtil.loadElements((Element) node);
ParameterMapping.mapping(wallConfig, props);
}
}
firewall.setWallConfig(wallConfig);
firewall.init();
}
use of com.actiontech.dble.config.model.UserConfig in project dble by actiontech.
the class ShowTables method response.
public static void response(ServerConnection c, String stmt) {
ShowCreateStmtInfo info;
try {
info = new ShowCreateStmtInfo(stmt);
} catch (Exception e) {
c.writeErrMessage(ErrorCode.ER_PARSE_ERROR, e.toString());
return;
}
String showSchema = info.getSchema();
if (showSchema != null && DbleServer.getInstance().getSystemVariables().isLowerCaseTableNames()) {
showSchema = showSchema.toLowerCase();
}
String cSchema = showSchema == null ? c.getSchema() : showSchema;
if (cSchema == null) {
c.writeErrMessage("3D000", "No database selected", ErrorCode.ER_NO_DB_ERROR);
return;
}
SchemaConfig schema = DbleServer.getInstance().getConfig().getSchemas().get(cSchema);
if (schema == null) {
c.writeErrMessage("42000", "Unknown database '" + cSchema + "'", ErrorCode.ER_BAD_DB_ERROR);
return;
}
ServerConfig conf = DbleServer.getInstance().getConfig();
UserConfig user = conf.getUsers().get(c.getUser());
if (user == null || !user.getSchemas().contains(cSchema)) {
c.writeErrMessage("42000", "Access denied for user '" + c.getUser() + "' to database '" + cSchema + "'", ErrorCode.ER_DBACCESS_DENIED_ERROR);
return;
}
// if schema has default node ,show tables will send to backend
String node = schema.getDataNode();
if (!Strings.isNullOrEmpty(node)) {
try {
parserAndExecuteShowTables(c, stmt, node, info);
} catch (Exception e) {
c.writeErrMessage(ErrorCode.ER_PARSE_ERROR, e.toString());
}
} else {
responseDirect(c, cSchema, info);
}
}
use of com.actiontech.dble.config.model.UserConfig in project dble by actiontech.
the class ServerConnection method routeSystemInfoAndExecuteSQL.
public void routeSystemInfoAndExecuteSQL(String stmt, SchemaUtil.SchemaInfo schemaInfo, int sqlType) {
ServerConfig conf = DbleServer.getInstance().getConfig();
UserConfig user = conf.getUsers().get(this.getUser());
if (user == null || !user.getSchemas().contains(schemaInfo.getSchema())) {
writeErrMessage("42000", "Access denied for user '" + this.getUser() + "' to database '" + schemaInfo.getSchema() + "'", ErrorCode.ER_DBACCESS_DENIED_ERROR);
return;
}
RouteResultset rrs = new RouteResultset(stmt, sqlType);
try {
if (RouterUtil.isNoSharding(schemaInfo.getSchemaConfig(), schemaInfo.getTable())) {
RouterUtil.routeToSingleNode(rrs, schemaInfo.getSchemaConfig().getDataNode());
} else {
TableConfig tc = schemaInfo.getSchemaConfig().getTables().get(schemaInfo.getTable());
if (tc == null) {
String msg = "Table '" + schemaInfo.getSchema() + "." + schemaInfo.getTable() + "' doesn't exist";
writeErrMessage("42S02", msg, ErrorCode.ER_NO_SUCH_TABLE);
return;
}
RouterUtil.routeToRandomNode(rrs, schemaInfo.getSchemaConfig(), schemaInfo.getTable());
}
session.execute(rrs);
} catch (Exception e) {
executeException(e, stmt);
}
}
use of com.actiontech.dble.config.model.UserConfig in project dble by actiontech.
the class RollbackConfig method rollback.
public static void rollback() throws Exception {
ServerConfig conf = DbleServer.getInstance().getConfig();
Map<String, PhysicalDBPool> dataHosts = conf.getBackupDataHosts();
Map<String, UserConfig> users = conf.getBackupUsers();
Map<String, SchemaConfig> schemas = conf.getBackupSchemas();
Map<String, PhysicalDBNode> dataNodes = conf.getBackupDataNodes();
FirewallConfig firewall = conf.getBackupFirewall();
Map<ERTable, Set<ERTable>> erRelations = conf.getBackupErRelations();
boolean backDataHostWithoutWR = conf.backDataHostWithoutWR();
if (conf.canRollback()) {
conf.rollback(users, schemas, dataNodes, dataHosts, erRelations, firewall, backDataHostWithoutWR);
} else if (conf.canRollbackAll()) {
boolean rollbackStatus = true;
String errorMsg = null;
for (PhysicalDBPool dn : dataHosts.values()) {
dn.init(dn.getActiveIndex());
if (!dn.isInitSuccess()) {
rollbackStatus = false;
errorMsg = "dataHost[" + dn.getHostName() + "] inited failure";
break;
}
}
// INIT FAILED
if (!rollbackStatus) {
for (PhysicalDBPool dn : dataHosts.values()) {
dn.clearDataSources("rollbackup config");
dn.stopHeartbeat();
}
throw new Exception(errorMsg);
}
final Map<String, PhysicalDBPool> cNodes = conf.getDataHosts();
// apply
conf.rollback(users, schemas, dataNodes, dataHosts, erRelations, firewall, backDataHostWithoutWR);
// stop old resource heartbeat
for (PhysicalDBPool dn : cNodes.values()) {
dn.clearDataSources("clear old config ");
dn.stopHeartbeat();
}
AlarmAppender.rollbackConfig();
} else {
throw new Exception("there is no old version");
}
}
Aggregations