Search in sources :

Example 6 with UserConfig

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);
}
Also used : RowDataPacket(com.actiontech.dble.net.mysql.RowDataPacket) EOFPacket(com.actiontech.dble.net.mysql.EOFPacket) UserConfig(com.actiontech.dble.config.model.UserConfig) ByteBuffer(java.nio.ByteBuffer) List(java.util.List) FieldPacket(com.actiontech.dble.net.mysql.FieldPacket) Map(java.util.Map)

Example 7 with UserConfig

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();
}
Also used : HashMap(java.util.HashMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ConfigException(com.actiontech.dble.config.util.ConfigException) WallConfig(com.alibaba.druid.wall.WallConfig) FirewallConfig(com.actiontech.dble.config.model.FirewallConfig) UserConfig(com.actiontech.dble.config.model.UserConfig) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with UserConfig

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);
    }
}
Also used : ServerConfig(com.actiontech.dble.config.ServerConfig) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) UserConfig(com.actiontech.dble.config.model.UserConfig)

Example 9 with UserConfig

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);
    }
}
Also used : ServerConfig(com.actiontech.dble.config.ServerConfig) TableConfig(com.actiontech.dble.config.model.TableConfig) UserConfig(com.actiontech.dble.config.model.UserConfig) SQLNonTransientException(java.sql.SQLNonTransientException) SQLException(java.sql.SQLException) IOException(java.io.IOException) RouteResultset(com.actiontech.dble.route.RouteResultset)

Example 10 with UserConfig

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");
    }
}
Also used : PhysicalDBNode(com.actiontech.dble.backend.datasource.PhysicalDBNode) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) Set(java.util.Set) PhysicalDBPool(com.actiontech.dble.backend.datasource.PhysicalDBPool) ERTable(com.actiontech.dble.config.model.ERTable) UserConfig(com.actiontech.dble.config.model.UserConfig) FirewallConfig(com.actiontech.dble.config.model.FirewallConfig) ServerConfig(com.actiontech.dble.config.ServerConfig) Map(java.util.Map)

Aggregations

UserConfig (com.actiontech.dble.config.model.UserConfig)16 ServerConfig (com.actiontech.dble.config.ServerConfig)5 FirewallConfig (com.actiontech.dble.config.model.FirewallConfig)5 SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)5 PhysicalDBNode (com.actiontech.dble.backend.datasource.PhysicalDBNode)3 PhysicalDBPool (com.actiontech.dble.backend.datasource.PhysicalDBPool)3 ERTable (com.actiontech.dble.config.model.ERTable)3 List (java.util.List)3 Set (java.util.Set)3 ConfigInitializer (com.actiontech.dble.config.ConfigInitializer)2 ConfigException (com.actiontech.dble.config.util.ConfigException)2 EOFPacket (com.actiontech.dble.net.mysql.EOFPacket)2 FieldPacket (com.actiontech.dble.net.mysql.FieldPacket)2 RowDataPacket (com.actiontech.dble.net.mysql.RowDataPacket)2 ByteBuffer (java.nio.ByteBuffer)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Element (org.w3c.dom.Element)2 Node (org.w3c.dom.Node)2