Java执行Linux命令(记dolphinscheduler测试数据源跳转登录页面排查弯路)

Java执行Linux命令(记dolphinscheduler测试数据源跳转登录页面排查弯路),第1张

Java执行Linux命令(记dolphinscheduler测试数据源跳转登录页面排查弯路)

Java执行Liunx命令
    • 背景
    • 具体代码
      • 新增工具类
        • 参数格式
      • 改动代码
    • 其它
      • 测试网络端口是否ping通
        • telnet
        • curl

背景

dolphinscheduler(V1.3.6)测试数据源,IP不通时会超时,然后跳转到登录页面,若不想跳转可以增大超时时间,但是这样会等待很长时间,实际上超过20秒连接不上就可以判定为网络不通

具体代码 新增工具类

通过curl命令判断网络是否通,curl 199.199.199.8:3306

package org.apache.dolphinscheduler.api.utils;

import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;



public class DataSourceUtils {
	 private static final Logger logger = LoggerFactory.getLogger(DataSourceUtils.class);
    
    public static boolean jdbcUrlChecks(String dataParam) {  
        try {
        	String commd = getUrlByJson(dataParam);
        	if(commandExist("curl")) {
        		 String[] execCommd = { "/bin/sh", "-c","curl " + commd};  
                 logger.info("execCommd="+Arrays.toString(execCommd));
                 Process process = Runtime.getRuntime().exec(execCommd); 
                 long startTime = System.currentTimeMillis()/1000;
                 long pingTime =0L;
                 boolean stop = false;//结束标识
                 while(process.isAlive() && (!stop)) {
                	 pingTime =(System.currentTimeMillis()/1000)-startTime;
                	 if(pingTime >20) {
                		 stop = true;
                	 }
                 }
                 if(pingTime>20) {//20秒不通,视为网络不通
                	 return false;
                 }
                 //process.waitFor();//等待命令执行结束
                 //logger.info("ping time:"+((System.currentTimeMillis()/1000)-startTime)+" s");
                 if(0 == process.exitValue()) {//0-执行成功
                 	return true;
                 }else {
                 	return false;
                 }
        		
        	} else {
        		return true;//telnet、curl不存在 默认返回true
        	}
           
        } catch (Exception e) {  
        	logger.error("jdbcUrlChecks() fail:",e);
        	return false;
        }  
        
    } 
    
    
    public static boolean commandExist(String commd) {
    	try {  
    		//命令
            String[] execCommd = { "/bin/sh", "-c", "which "+commd };  
            Process process = Runtime.getRuntime().exec(execCommd);
            //等待命令执行结束
            process.waitFor();
            //判断退出码 0-正常退出(执行成功)
            if(0 == process.exitValue()) {
            	return true;
            }else {
            	return false;
            }
        } catch (Exception e) {  
        	logger.error("commandExist() fail:",e);
        	return false;
        } 
    }
    
//    
//    public static boolean commondRst() {
//    	try {  
//            //命令执行结果
//            String[] execCommdRst = { "/bin/sh", "-c", " echo $?" };  
//            Process processRst = Runtime.getRuntime().exec(execCommdRst);  
//            
//            LineNumberReader br = new LineNumberReader(new InputStreamReader(processRst.getInputStream())); 
//            if(br != null && br.readLine() !=null && "0".equals(br.readLine())) {
//            	return true; 
//            } else {
//            	return false; 
//            }
//        } catch (Exception e) {  
//        	logger.error("commondRst() fail:",e);
//        	return false; 
//        }  
//    }
    
    
    private static String getUrlByJson(String dataParam) {
    	JSonObject json = JSONObject.parseObject(dataParam);
    	String urlParam = json.getString("address");
		return urlParam.substring(urlParam.lastIndexOf("/")+1);
    	
    }
    
    public static void main(String [] args) {
    	String dataParam ="{"address":"jdbc:mysql://199.199.199.8:3306","database":"test_db","jdbcUrl":"jdbc:mysql://199.199.199.8:3306/test_db","user":"test","password":"Test@2021"}"; 
		JSonObject json = JSONObject.parseObject(dataParam);
    	String urlParam = json.getString("address");
		System.out.print(urlParam.substring(urlParam.lastIndexOf("/")+1));
    }
}

参数格式
{"address":"jdbc:mysql://199.199.199.8:3306","database":"test_db","jdbcUrl":"jdbc:mysql://199.199.199.8:3306/test_db","user":"test","password":"Copote@2021"}


改动代码

DataSourceService涉及两处

package org.apache.dolphinscheduler.api.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.exceptions.ServiceException;
import org.apache.dolphinscheduler.api.utils.DataSourceUtils;
import org.apache.dolphinscheduler.api.utils.PageInfo;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.enums.DbConnectType;
import org.apache.dolphinscheduler.common.enums.DbType;
import org.apache.dolphinscheduler.common.utils.CommonUtils;
import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.dolphinscheduler.common.utils.StringUtils;
import org.apache.dolphinscheduler.dao.datasource.*;
import org.apache.dolphinscheduler.dao.entity.DataSource;
import org.apache.dolphinscheduler.dao.entity.Resource;
import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.dao.mapper.DataSourceMapper;
import org.apache.dolphinscheduler.dao.mapper.DataSourceUserMapper;

import org.apache.commons.collections.MapUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.*;
import java.util.regex.Pattern;

import static org.apache.dolphinscheduler.common.utils.PropertyUtils.getString;


@Service
public class DataSourceService extends baseService{

    private static final Logger logger = LoggerFactory.getLogger(DataSourceService.class);

    public static final String NAME = "name";
    public static final String NOTE = "note";
    public static final String TYPE = "type";
    public static final String HOST = "host";
    public static final String PORT = "port";
    public static final String PRINCIPAL = "principal";
    public static final String DATAbase = "database";
    public static final String USER_NAME = "userName";
    public static final String PASSWORD = Constants.PASSWORD;
    public static final String OTHER = "other";

    private static final Pattern IPV4_PATTERN = Pattern.compile("^[a-zA-Z0-9\_\-\.]+$");

    private static final Pattern IPV6_PATTERN = Pattern.compile("^[a-zA-Z0-9\_\-\.\:\[\]]+$");

    private static final Pattern DATAbase_PATTER = Pattern.compile("^[a-zA-Z0-9\_\-\.]+$");

    private static final Pattern PARAMS_PATTER = Pattern.compile("^[a-zA-Z0-9]+$");


    @Autowired
    private DataSourceMapper dataSourceMapper;


    @Autowired
    private DataSourceUserMapper datasourceUserMapper;

    
    public Map createDataSource(User loginUser, String name, String desc, DbType type, String parameter) {

        Map result = new HashMap<>(5);
        // check name can use or not
        if (checkName(name)) {
            putMsg(result, Status.DATASOURCE_EXIST);
            return result;
        }
        
//        Boolean pingConnection = DataSourceUtils.jdbcUrlChecks(parameter);
//        if (!pingConnection) {
//            logger.info("connect failed, type:{}, parameter:{}", type, parameter);
//            putMsg(result, Status.DATASOURCE_CONNECT_FAILED);
//            return result;
//        }
        
        Boolean isConnection = checkConnection(type, parameter);
        if (!isConnection) {
        	logger.info("connect failed, type:{}, parameter:{}", type, parameter);
        	putMsg(result, Status.DATASOURCE_CONNECT_FAILED);
        	return result;
        }

        baseDataSource datasource = DataSourceFactory.getDatasource(type, parameter);
        if (datasource == null) {
            putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, parameter);
            return result;
        }

        // build datasource
        DataSource dataSource = new DataSource();
        Date now = new Date();

        dataSource.setName(name.trim());
        dataSource.setNote(desc);
        dataSource.setUserId(loginUser.getId());
        dataSource.setUserName(loginUser.getUserName());
        dataSource.setType(type);
        dataSource.setConnectionParams(parameter);
        dataSource.setCreateTime(now);
        dataSource.setUpdateTime(now);
        dataSourceMapper.insert(dataSource);

        putMsg(result, Status.SUCCESS);

        return result;
    }


    
    public Map updateDataSource(int id, User loginUser, String name, String desc, DbType type, String parameter) {

        Map result = new HashMap<>();
        // determine whether the data source exists
        DataSource dataSource = dataSourceMapper.selectById(id);
        if (dataSource == null) {
            putMsg(result, Status.RESOURCE_NOT_EXIST);
            return result;
        }

        if(!hasPerm(loginUser, dataSource.getUserId())){
            putMsg(result, Status.USER_NO_OPERATION_PERM);
            return result;
        }

        //check name can use or not
        if(!name.trim().equals(dataSource.getName()) && checkName(name)){
            putMsg(result, Status.DATASOURCE_EXIST);
            return result;
        }

        Boolean isConnection = checkConnection(type, parameter);
        if (!isConnection) {
            logger.info("connect failed, type:{}, parameter:{}", type, parameter);
            putMsg(result, Status.DATASOURCE_CONNECT_FAILED);
            return result;
        }
        Date now = new Date();

        dataSource.setName(name.trim());
        dataSource.setNote(desc);
        dataSource.setUserName(loginUser.getUserName());
        dataSource.setType(type);
        dataSource.setConnectionParams(parameter);
        dataSource.setUpdateTime(now);
        dataSourceMapper.updateById(dataSource);
        putMsg(result, Status.SUCCESS);
        return result;
    }

    private boolean checkName(String name) {
        List queryDataSource = dataSourceMapper.queryDataSourceByName(name.trim());
        if (queryDataSource != null && queryDataSource.size() > 0) {
            return true;
        }
        return false;
    }


    
    public Map queryDataSource(int id) {

        Map result = new HashMap(5);
        DataSource dataSource = dataSourceMapper.selectById(id);
        if (dataSource == null) {
            putMsg(result, Status.RESOURCE_NOT_EXIST);
            return result;
        }
        // type
        String dataSourceType = dataSource.getType().toString();
        // name
        String dataSourceName = dataSource.getName();
        // desc
        String desc = dataSource.getNote();
        // parameter
        String parameter = dataSource.getConnectionParams();

        baseDataSource datasourceForm = DataSourceFactory.getDatasource(dataSource.getType(), parameter);
        DbConnectType  connectType = null;
        String hostSeperator = Constants.DOUBLE_SLASH;
        if(DbType.ORACLE.equals(dataSource.getType())){
            connectType = ((OracleDataSource) datasourceForm).getConnectType();
            if(DbConnectType.ORACLE_SID.equals(connectType)){
                hostSeperator = Constants.AT_SIGN;
            }
        }
        String database = datasourceForm.getDatabase();
        // jdbc connection params
        String other = datasourceForm.getOther();
        String address = datasourceForm.getAddress();

        String[] hostsPorts = getHostsAndPort(address,hostSeperator);
        // ip host
        String host = hostsPorts[0];
        // prot
        String port = hostsPorts[1];
        String separator = "";

        switch (dataSource.getType()) {
            case HIVE:
            case SQLSERVER:
                separator = ";";
                break;
            case MYSQL:
            case POSTGRESQL:
            case CLICKHOUSE:
            case ORACLE:
                separator = "&";
                break;
            default:
                separator = "&";
                break;
        }

        Map otherMap = new linkedHashMap();
        if (other != null) {
            String[] configs = other.split(separator);
            for (String config : configs) {
                otherMap.put(config.split("=")[0], config.split("=")[1]);
            }

        }

        Map map = new HashMap<>(10);
        map.put(NAME, dataSourceName);
        map.put(NOTE, desc);
        map.put(TYPE, dataSourceType);
        if (connectType != null) {
            map.put(Constants.ORACLE_DB_CONNECT_TYPE, connectType);
        }

        map.put(HOST, host);
        map.put(PORT, port);
        map.put(PRINCIPAL, datasourceForm.getPrincipal());
        map.put(DATAbase, database);
        map.put(USER_NAME, datasourceForm.getUser());
        map.put(PASSWORD, datasourceForm.getPassword());
        map.put(OTHER, otherMap);
        result.put(Constants.DATA_LIST, map);
        putMsg(result, Status.SUCCESS);
        return result;
    }


    
    public Map queryDataSourceListPaging(User loginUser, String searchVal, Integer pageNo, Integer pageSize) {
        Map result = new HashMap<>();
        IPage dataSourceList = null;
        Page dataSourcePage = new Page(pageNo, pageSize);

        if (isAdmin(loginUser)) {
            dataSourceList = dataSourceMapper.selectPaging(dataSourcePage, 0, searchVal);
        }else{
            dataSourceList = dataSourceMapper.selectPaging(dataSourcePage, loginUser.getId(), searchVal);
        }

        List dataSources = dataSourceList.getRecords();
        handlePasswd(dataSources);
        PageInfo pageInfo = new PageInfo(pageNo, pageSize);
        pageInfo.setTotalCount((int)(dataSourceList.getTotal()));
        pageInfo.setLists(dataSources);
        result.put(Constants.DATA_LIST, pageInfo);
        putMsg(result, Status.SUCCESS);

        return result;
    }

    
    private void handlePasswd(List dataSourceList) {

        for (DataSource dataSource : dataSourceList) {

            String connectionParams  = dataSource.getConnectionParams();
            JSonObject  object = JSON.parseObject(connectionParams);
            object.put(Constants.PASSWORD, Constants.XXXXXX);
            dataSource.setConnectionParams(JSONUtils.toJson(object));

        }
    }

    
    public Map queryDataSourceList(User loginUser, Integer type) {
        Map result = new HashMap<>(5);

        List datasourceList;

        if (isAdmin(loginUser)) {
            datasourceList = dataSourceMapper.listAllDataSourceByType(type);
        }else{
            datasourceList = dataSourceMapper.queryDataSourceByType(loginUser.getId(), type);
        }

        result.put(Constants.DATA_LIST, datasourceList);
        putMsg(result, Status.SUCCESS);

        return result;
    }

    
    public Result verifyDataSourceName(User loginUser, String name) {
        Result result = new Result();
        List dataSourceList = dataSourceMapper.queryDataSourceByName(name);
        if (dataSourceList != null && dataSourceList.size() > 0) {
            logger.error("datasource name:{} has exist, can't create again.", name);
            putMsg(result, Status.DATASOURCE_EXIST);
        } else {
            putMsg(result, Status.SUCCESS);
        }

        return result;
    }

    
    private Connection getConnection(DbType dbType, String parameter) {
        Connection connection = null;
        baseDataSource datasource = null;
        try {
            switch (dbType) {
                case POSTGRESQL:
                    datasource = JSON.parseObject(parameter, PostgreDataSource.class);
                    Class.forName(Constants.ORG_POSTGRESQL_DRIVER);
                    break;
                case MYSQL:
                    datasource = JSON.parseObject(parameter, MySQLDataSource.class);
                    Class.forName(Constants.COM_MYSQL_JDBC_DRIVER);
                    break;
                case HIVE:
                case SPARK:
                    if (CommonUtils.getKerberosStartupState())  {
                            System.setProperty(org.apache.dolphinscheduler.common.Constants.JAVA_SECURITY_KRB5_CONF,
                                    getString(org.apache.dolphinscheduler.common.Constants.JAVA_SECURITY_KRB5_CONF_PATH));
                            Configuration configuration = new Configuration();
                            configuration.set(org.apache.dolphinscheduler.common.Constants.HADOOP_SECURITY_AUTHENTICATION, "kerberos");
                            UserGroupInformation.setConfiguration(configuration);
                            UserGroupInformation.loginUserFromKeytab(getString(org.apache.dolphinscheduler.common.Constants.LOGIN_USER_KEY_TAB_USERNAME),
                                    getString(org.apache.dolphinscheduler.common.Constants.LOGIN_USER_KEY_TAB_PATH));
                    }
                    if (dbType == DbType.HIVE){
                        datasource = JSON.parseObject(parameter, HiveDataSource.class);
                    }else if (dbType == DbType.SPARK){
                        datasource = JSON.parseObject(parameter, SparkDataSource.class);
                    }
                    Class.forName(Constants.ORG_APACHE_HIVE_JDBC_HIVE_DRIVER);
                    break;
                case CLICKHOUSE:
                    datasource = JSON.parseObject(parameter, ClickHouseDataSource.class);
                    Class.forName(Constants.COM_CLICKHOUSE_JDBC_DRIVER);
                    break;
                case ORACLE:
                    datasource = JSON.parseObject(parameter, OracleDataSource.class);
                    Class.forName(Constants.COM_ORACLE_JDBC_DRIVER);
                    break;
                case SQLSERVER:
                    datasource = JSON.parseObject(parameter, SQLServerDataSource.class);
                    Class.forName(Constants.COM_SQLSERVER_JDBC_DRIVER);
                    break;
                case DB2:
                    datasource = JSON.parseObject(parameter, DB2ServerDataSource.class);
                    Class.forName(Constants.COM_DB2_JDBC_DRIVER);
                    break;
                default:
                    break;
            }

            if(datasource != null){
            	if(DataSourceUtils.jdbcUrlChecks(parameter)) {
            		connection = DriverManager.getConnection(datasource.getJdbcUrl(), datasource.getUser(), datasource.getPassword());
            	}
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return connection;
    }


    
    public boolean checkConnection(DbType type, String parameter) {
        Boolean isConnection = false;
        if(DataSourceUtils.jdbcUrlChecks(parameter)) {//
        	Connection con = getConnection(type, parameter);
        	if (con != null) {
        		isConnection = true;
        		try {
        			con.close();
        		} catch (SQLException e) {
        			logger.error("close connection fail at DataSourceService::checkConnection()", e);
        		}
        	}
        }
        return isConnection;
    }


    
    public boolean connectionTest(User loginUser, int id) {
        DataSource dataSource = dataSourceMapper.selectById(id);
        return checkConnection(dataSource.getType(), dataSource.getConnectionParams());
    }

    
    public String buildParameter(String name, String desc, DbType type, String host,
                                 String port, String database, String principal, String userName,
                                 String password, DbConnectType connectType, String other) {
        checkParams(type, port, host, database, other);
        String address = buildAddress(type, host, port, connectType);
        Map parameterMap = new linkedHashMap(6);
        String jdbcUrl = address + "/" + database;
        if (Constants.ORACLE.equals(type.name())) {
            parameterMap.put(Constants.ORACLE_DB_CONNECT_TYPE, connectType);
        }

        if (CommonUtils.getKerberosStartupState() &&
                (type == DbType.HIVE || type == DbType.SPARK)){
            jdbcUrl += ";principal=" + principal;
        }

        String separator = "";
        if (Constants.MYSQL.equals(type.name())
                || Constants.POSTGRESQL.equals(type.name())
                || Constants.CLICKHOUSE.equals(type.name())
                || Constants.ORACLE.equals(type.name())) {
            separator = "&";
        } else if (Constants.HIVE.equals(type.name())
                || Constants.SPARK.equals(type.name())
                || Constants.DB2.equals(type.name())
                || Constants.SQLSERVER.equals(type.name())) {
            separator = ";";
        }

        parameterMap.put(Constants.ADDRESS, address);
        parameterMap.put(Constants.DATAbase, database);
        parameterMap.put(Constants.JDBC_URL, jdbcUrl);
        parameterMap.put(Constants.USER, userName);
        parameterMap.put(Constants.PASSWORD, password);
        if (CommonUtils.getKerberosStartupState() &&
                (type == DbType.HIVE || type == DbType.SPARK)){
            parameterMap.put(Constants.PRINCIPAL,principal);
        }
        if (other != null && !"".equals(other)) {
            linkedHashMap map = JSON.parseObject(other, new TypeReference>() {
            });
            if (type == DbType.MYSQL) {
                map = (linkedHashMap) MySQLDataSource.buildOtherParams(map);
            }
            if (MapUtils.isNotEmpty(map)) {
                StringBuilder otherSb = new StringBuilder();
                for (Map.Entry entry: map.entrySet()) {
                    otherSb.append(String.format("%s=%s%s", entry.getKey(), entry.getValue(), separator));
                }
                if (!Constants.DB2.equals(type.name())) {
                    otherSb.deleteCharAt(otherSb.length() - 1);
                }
                parameterMap.put(Constants.OTHER, otherSb);
            }

        }

        if(logger.isDebugEnabled()){
            logger.info("parameters map-----" + JSON.toJSonString(parameterMap));
        }
        return JSON.toJSonString(parameterMap);


    }

    private String buildAddress(DbType type, String host, String port, DbConnectType connectType) {
        StringBuilder sb = new StringBuilder();
        if (Constants.MYSQL.equals(type.name())) {
            sb.append(Constants.JDBC_MYSQL);
            sb.append(host).append(":").append(port);
        } else if (Constants.POSTGRESQL.equals(type.name())) {
            sb.append(Constants.JDBC_POSTGRESQL);
            sb.append(host).append(":").append(port);
        } else if (Constants.HIVE.equals(type.name()) || Constants.SPARK.equals(type.name())) {
            sb.append(Constants.JDBC_HIVE_2);
            String[] hostArray = host.split(",");
            if (hostArray.length > 0) {
                for (String zkHost : hostArray) {
                    sb.append(String.format("%s:%s,", zkHost, port));
                }
                sb.deleteCharAt(sb.length() - 1);
            }
        } else if (Constants.CLICKHOUSE.equals(type.name())) {
            sb.append(Constants.JDBC_CLICKHOUSE);
            sb.append(host).append(":").append(port);
        } else if (Constants.ORACLE.equals(type.name())) {
            if (connectType == DbConnectType.ORACLE_SID) {
                sb.append(Constants.JDBC_ORACLE_SID);
            } else {
                sb.append(Constants.JDBC_ORACLE_SERVICE_NAME);
            }
            sb.append(host).append(":").append(port);
        } else if (Constants.SQLSERVER.equals(type.name())) {
            sb.append(Constants.JDBC_SQLSERVER);
            sb.append(host).append(":").append(port);
        }else if (Constants.DB2.equals(type.name())) {
            sb.append(Constants.JDBC_DB2);
            sb.append(host).append(":").append(port);
        }

        return sb.toString();
    }

    
    @Transactional(rollbackFor = Exception.class)
    public Result delete(User loginUser, int datasourceId) {
        Result result = new Result();
        try {
            //query datasource by id
            DataSource dataSource = dataSourceMapper.selectById(datasourceId);
            if(dataSource == null){
                logger.error("resource id {} not exist", datasourceId);
                putMsg(result, Status.RESOURCE_NOT_EXIST);
                return result;
            }
            if(!hasPerm(loginUser, dataSource.getUserId())){
                putMsg(result, Status.USER_NO_OPERATION_PERM);
                return result;
            }
            dataSourceMapper.deleteById(datasourceId);
            datasourceUserMapper.deleteByDatasourceId(datasourceId);
            putMsg(result, Status.SUCCESS);
        } catch (Exception e) {
            logger.error("delete datasource error",e);
            throw new RuntimeException("delete datasource error");
        }
        return result;
    }

    
    public Map unauthDatasource(User loginUser, Integer userId) {

        Map result = new HashMap<>();
        //only admin operate
        if (!isAdmin(loginUser)) {
            putMsg(result, Status.USER_NO_OPERATION_PERM);
            return result;
        }

        
        List resultList = new ArrayList<>();
        List datasourceList = dataSourceMapper.queryDatasourceExceptUserId(userId);
        Set datasourceSet = null;
        if (datasourceList != null && datasourceList.size() > 0) {
            datasourceSet = new HashSet<>(datasourceList);

            List authedDataSourceList = dataSourceMapper.queryAuthedDatasource(userId);

            Set authedDataSourceSet = null;
            if (authedDataSourceList != null && authedDataSourceList.size() > 0) {
                authedDataSourceSet = new HashSet<>(authedDataSourceList);
                datasourceSet.removeAll(authedDataSourceSet);

            }
            resultList = new ArrayList<>(datasourceSet);
        }
        result.put(Constants.DATA_LIST, resultList);
        putMsg(result, Status.SUCCESS);
        return result;
    }


    
    public Map authedDatasource(User loginUser, Integer userId) {
        Map result = new HashMap<>(5);

        if (!isAdmin(loginUser)) {
            putMsg(result, Status.USER_NO_OPERATION_PERM);
            return result;
        }

        List authedDatasourceList = dataSourceMapper.queryAuthedDatasource(userId);
        result.put(Constants.DATA_LIST, authedDatasourceList);
        putMsg(result, Status.SUCCESS);
        return result;
    }


    
    private String[] getHostsAndPort(String address) {
        return getHostsAndPort(address,Constants.DOUBLE_SLASH);
    }

    
    private String[] getHostsAndPort(String address,String separator) {
        String[] result = new String[2];
        String[] tmpArray = address.split(separator);
        String hostsAndPorts = tmpArray[tmpArray.length - 1];
        StringBuilder hosts = new StringBuilder();
        String[] hostPortArray = hostsAndPorts.split(Constants.COMMA);
        String port = hostPortArray[0].split(Constants.colon)[1];
        for (String hostPort : hostPortArray) {
            hosts.append(hostPort.split(Constants.colon)[0]).append(Constants.COMMA);
        }
        hosts.deleteCharAt(hosts.length() - 1);
        result[0] = hosts.toString();
        result[1] = port;
        return result;
    }


    private void checkParams(DbType type, String port, String host, String database, String other) {
        if (null == DbType.of(type.getCode())) {
            throw new ServiceException(Status.DATASOURCE_DB_TYPE_ILLEGAL);
        }
        if (!isNumeric(port)) {
            throw new ServiceException(Status.DATASOURCE_PORT_ILLEGAL);
        }
        if (!IPV4_PATTERN.matcher(host).matches() || !IPV6_PATTERN.matcher(host).matches()) {
            throw new ServiceException(Status.DATASOURCE_HOST_ILLEGAL);
        }
        if (!DATAbase_PATTER.matcher(database).matches()) {
            throw new ServiceException(Status.DATASOURCE_NAME_ILLEGAL);
        }
        if (StringUtils.isBlank(other)) {
            return;
        }
        Map map = JSONUtils.toMap(other);
        if (MapUtils.isEmpty(map)) {
            return;
        }
        boolean paramsCheck = map.entrySet().stream().allMatch(p -> PARAMS_PATTER.matcher(p.getValue()).matches());
        if (!paramsCheck) {
            throw new ServiceException(Status.DATASOURCE_OTHER_PARAMS_ILLEGAL);
        }
    }

    private static boolean isNumeric(String str) {
        for (int i = str.length(); --i >= 0; ) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }
}

其它 测试网络端口是否ping通 telnet

telnet ip port
失败

[dolphinscheduler@host1 ~]$ telnet 199.199.199.8 3306
Trying 199.199.199.8...
telnet: connect to address 199.199.199.8: Connection timed out
[dolphinscheduler@host1 ~]$ echo $?
1
[dolphinscheduler@host1 ~]$ 
[dolphinscheduler@host1 ~]$ telnet 192.168.56.10 8080
Trying 192.168.56.10...
telnet: connect to address 192.168.56.10: Connection refused
[dolphinscheduler@host1 ~]$ echo $?
1

非正常退出
网络是通的,但是需要安全认证,代码会判定为执行失败,因此上面的代码最终只保留了curl,本来的逻辑是判读telnet curl 命令是否存在,存在则使用该命令执行

[dolphinscheduler@host1 ~]$ telnet 192.168.56.10 3306
Trying 192.168.56.10...
Connected to 192.168.56.10.
Escape character is '^]'.
J
#caching_sha2_password2#08S01Got timeout reading communication packetsConnection closed by foreign host.
[dolphinscheduler@host1 ~]$ echo $?
1
curl

curl ip:port
失败

[dolphinscheduler@host1 ~]$ curl 199.199.199.8:3306
curl: (7) Failed connect to 199.199.199.8:3306; 连接超时
[dolphinscheduler@host1 ~]$ echo $?
7
[dolphinscheduler@host1 ~]$ 

成功

[dolphinscheduler@host1 ~]$ curl 192.168.56.10:3306
8.0.26QE7-ÿÿ!ÿ͕%S'o/rH%Mcaching_sha2_password!ÿ#08S01Got packets out of order[dolphinscheduler@host1 ~]$ echo $?
0
[dolphinscheduler@host1 ~]$ 

测试

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5681432.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存