Search in sources :

Example 1 with PubSvcDefineException

use of com.dtstack.taier.common.exception.PubSvcDefineException in project Taier by DTStack.

the class DatasourceService method kerberosConnectPrepare.

/**
 * kerberos认证前预处理 :对kerberos参数替换相对路径为绝对路径等操作
 * @param sourceId
 * @return
 */
public Map<String, Object> kerberosConnectPrepare(Long sourceId) {
    DsInfo dataSource = dsInfoService.getOneById(sourceId);
    DataSourceTypeEnum typeEnum = DataSourceTypeEnum.typeVersionOf(dataSource.getDataType(), dataSource.getDataVersion());
    if (Objects.isNull(typeEnum)) {
        throw new PubSvcDefineException(ErrorCode.CAN_NOT_FITABLE_SOURCE_TYPE);
    }
    Map<String, Object> kerberosConfig = fillKerberosConfig(dataSource.getId());
    HashMap<String, Object> tmpKerberosConfig = new HashMap<>(kerberosConfig);
    // kerberos获取表操作预处理
    if (MapUtils.isNotEmpty(kerberosConfig)) {
        String localKerberosPath = kerberosService.getLocalKerberosPath(sourceId);
        IKerberos kerberos = ClientCache.getKerberos(typeEnum.getVal());
        try {
            kerberos.prepareKerberosForConnect(tmpKerberosConfig, localKerberosPath);
        } catch (Exception e) {
            LOGGER.error("kerberos连接预处理失败!{}", e.getMessage(), e);
            throw new DtCenterDefException(String.format("kerberos连接预处理失败,Caused by: %s", e.getMessage()), e);
        }
    }
    return tmpKerberosConfig;
}
Also used : DataSourceTypeEnum(com.dtstack.taier.common.enums.DataSourceTypeEnum) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) PubSvcDefineException(com.dtstack.taier.common.exception.PubSvcDefineException) DtCenterDefException(com.dtstack.taier.common.exception.DtCenterDefException) JSONObject(com.alibaba.fastjson.JSONObject) IKerberos(com.dtstack.dtcenter.loader.client.IKerberos) DsInfo(com.dtstack.taier.dao.domain.DsInfo) SftpException(com.jcraft.jsch.SftpException) RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) IOException(java.io.IOException) PubSvcDefineException(com.dtstack.taier.common.exception.PubSvcDefineException) DtCenterDefException(com.dtstack.taier.common.exception.DtCenterDefException)

Example 2 with PubSvcDefineException

use of com.dtstack.taier.common.exception.PubSvcDefineException in project Taier by DTStack.

the class DatasourceService method getPrincipalsWithConf.

/**
 * 解析kerberos文件获取principal列表
 * @param source
 * @param resource
 * @param userId
 * @return
 */
public List<String> getPrincipalsWithConf(DataSourceVO source, Pair<String, String> resource, Long userId) {
    String localKerberosPath;
    Map<String, Object> kerberosConfig;
    // 获取数据源类型,这里要做type version的改造
    DataSourceTypeEnum typeEnum = DataSourceTypeEnum.typeVersionOf(source.getDataType(), source.getDataVersion());
    IKerberos kerberos = ClientCache.getKerberos(typeEnum.getVal());
    if (Objects.nonNull(resource)) {
        localKerberosPath = kerberosService.getTempLocalKerberosConf(userId);
        try {
            // 解析Zip文件获取配置对象
            kerberosConfig = kerberos.parseKerberosFromUpload(resource.getRight(), localKerberosPath);
        } catch (IOException e) {
            LOGGER.error("解析principals, kerberos config 解析异常,{}", e.getMessage(), e);
            throw new PubSvcDefineException(String.format("kerberos config 解析异常,Caused by: %s", e.getMessage()), e);
        }
        // 连接 Kerberos 前的准备工作
        kerberos.prepareKerberosForConnect(kerberosConfig, localKerberosPath);
    } else {
        kerberosConfig = kerberosConnectPrepare(source.getId());
    }
    return kerberos.getPrincipals(kerberosConfig);
}
Also used : DataSourceTypeEnum(com.dtstack.taier.common.enums.DataSourceTypeEnum) PubSvcDefineException(com.dtstack.taier.common.exception.PubSvcDefineException) JSONObject(com.alibaba.fastjson.JSONObject) IKerberos(com.dtstack.dtcenter.loader.client.IKerberos) IOException(java.io.IOException)

Example 3 with PubSvcDefineException

use of com.dtstack.taier.common.exception.PubSvcDefineException in project Taier by DTStack.

the class DatasourceService method colMap.

/**
 * 解析字段
 * @param json
 * @param type
 * @param kerberosConfig
 * @return
 */
private void colMap(JSONObject json, Integer type, Map<String, Object> kerberosConfig) {
    if (DataSourceType.getKafkaS().contains(type)) {
        ISourceDTO sourceDTO = SourceDTOType.getSourceDTO(json, type, Maps.newHashMap(), Maps.newHashMap());
        String brokersAddress = null;
        try {
            brokersAddress = ClientCache.getKafka(type).getAllBrokersAddress(sourceDTO);
        } catch (Exception e) {
            LOGGER.error("获取kafka brokersAddress 异常!", e);
            throw new PubSvcDefineException("获取kafka brokersAddress 异常!", e);
        }
        json.put("bootstrapServers", brokersAddress);
    }
    if (kerberosConfig != null) {
        json.put(FormNames.KERBEROS_CONFIG, kerberosConfig);
    }
}
Also used : PubSvcDefineException(com.dtstack.taier.common.exception.PubSvcDefineException) ISourceDTO(com.dtstack.dtcenter.loader.dto.source.ISourceDTO) SftpException(com.jcraft.jsch.SftpException) RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) IOException(java.io.IOException) PubSvcDefineException(com.dtstack.taier.common.exception.PubSvcDefineException) DtCenterDefException(com.dtstack.taier.common.exception.DtCenterDefException)

Example 4 with PubSvcDefineException

use of com.dtstack.taier.common.exception.PubSvcDefineException in project Taier by DTStack.

the class DatasourceService method buildDsInfo.

/**
 * 构建数据源元数据对象
 * @param dataSourceVO
 * @return
 */
private DsInfo buildDsInfo(DataSourceVO dataSourceVO) {
    DsInfo dsInfo = new DsInfo();
    dsInfo.setDataType(dataSourceVO.getDataType());
    dsInfo.setDataVersion(dataSourceVO.getDataVersion());
    dsInfo.setDataName(dataSourceVO.getDataName());
    dsInfo.setDataDesc(dataSourceVO.getDataDesc());
    dsInfo.setStatus(1);
    dsInfo.setIsMeta(dataSourceVO.getIsMeta());
    dsInfo.setTenantId(dataSourceVO.getTenantId());
    dsInfo.setTenantId(dataSourceVO.getTenantId());
    dsInfo.setSchemaName(dataSourceVO.getSchemaName());
    DataSourceTypeEnum typeEnum = DataSourceTypeEnum.typeVersionOf(dataSourceVO.getDataType(), dataSourceVO.getDataVersion());
    dsInfo.setDataTypeCode(typeEnum.getVal());
    // dataJson
    if (Objects.nonNull(dataSourceVO.getDataJson())) {
        JSONObject dataJson = dataSourceVO.getDataJson();
        if (dataSourceVO.getDataType().equals(DataSourceTypeEnum.HBASE2.getDataType())) {
            // Hbase需要特殊处理
            JSONObject jsonObject = new JSONObject();
            jsonObject.put(FormNames.HBASE_ZK_QUORUM, dataJson.get(FormNames.HBASE_QUORUM));
            jsonObject.put(FormNames.HBASE_ZK_PARENT, dataJson.get(FormNames.HBASE_PARENT));
            dataJson.put("hbaseConfig", jsonObject);
        }
        JSONObject kerberos;
        if ((kerberos = dataJson.getJSONObject(DataSourceUtils.KERBEROS_CONFIG)) != null) {
            dataJson.put(DataSourceUtils.KERBEROS_FILE_TIMESTAMP, kerberos.getOrDefault(DataSourceUtils.KERBEROS_FILE_TIMESTAMP, System.currentTimeMillis()));
        }
        dsInfo.setDataJson(DataSourceUtils.getEncodeDataSource(dataJson, true));
        String linkInfo = getDataSourceLinkInfo(dataSourceVO.getDataType(), dataSourceVO.getDataVersion(), dataSourceVO.getDataJson());
        dsInfo.setLinkJson(DataSourceUtils.getEncodeDataSource(linkInfo, true));
    } else if (Strings.isNotBlank(dataSourceVO.getDataJsonString())) {
        JSONObject dataSourceJson = DataSourceUtils.getDataSourceJson(dataSourceVO.getDataJsonString());
        if (dataSourceVO.getDataType().equals(DataSourceTypeEnum.HBASE2.getDataType())) {
            // Hbase需要特殊处理
            JSONObject jsonObject = new JSONObject();
            jsonObject.put(FormNames.HBASE_QUORUM, dataSourceJson.get(FormNames.HBASE_QUORUM));
            jsonObject.put(FormNames.HBASE_ZK_PARENT, dataSourceJson.get(FormNames.HBASE_PARENT));
            dataSourceJson.put("hbaseConfig", jsonObject);
        }
        JSONObject kerberos;
        if ((kerberos = dataSourceJson.getJSONObject(DataSourceUtils.KERBEROS_CONFIG)) != null) {
            dataSourceJson.put(DataSourceUtils.KERBEROS_FILE_TIMESTAMP, kerberos.getOrDefault(DataSourceUtils.KERBEROS_FILE_TIMESTAMP, System.currentTimeMillis()));
        }
        dsInfo.setDataJson(DataSourceUtils.getEncodeDataSource(dataSourceJson, true));
        // 获取连接信息
        String linkInfo = getDataSourceLinkInfo(dataSourceVO.getDataType(), dataSourceVO.getDataVersion(), dataSourceJson);
        dsInfo.setLinkJson(DataSourceUtils.getEncodeDataSource(linkInfo, true));
    } else {
        throw new PubSvcDefineException(ErrorCode.DATASOURCE_CONF_ERROR);
    }
    return dsInfo;
}
Also used : DataSourceTypeEnum(com.dtstack.taier.common.enums.DataSourceTypeEnum) JSONObject(com.alibaba.fastjson.JSONObject) PubSvcDefineException(com.dtstack.taier.common.exception.PubSvcDefineException) DsInfo(com.dtstack.taier.dao.domain.DsInfo)

Example 5 with PubSvcDefineException

use of com.dtstack.taier.common.exception.PubSvcDefineException in project Taier by DTStack.

the class DatasourceService method checkConnectionWithKerberos.

/**
 * 检测kerberos认证的数据源连通性
 * @param source
 * @param resource
 * @param projectId
 * @param userId
 * @return
 */
public Boolean checkConnectionWithKerberos(DataSourceVO source, Pair<String, String> resource, Long projectId, Long userId) {
    Map<String, Object> kerberosConfig;
    String localKerberosPath;
    DataSourceTypeEnum typeEnum = DataSourceTypeEnum.typeVersionOf(source.getDataType(), source.getDataVersion());
    IKerberos kerberos = ClientCache.getKerberos(typeEnum.getVal());
    if (Objects.nonNull(resource)) {
        localKerberosPath = kerberosService.getTempLocalKerberosConf(userId);
        try {
            kerberosConfig = kerberos.parseKerberosFromUpload(resource.getRight(), localKerberosPath);
        } catch (IOException e) {
            LOGGER.error("检测连通性, kerberos config 解析异常,{}", e.getMessage(), e);
            throw new PubSvcDefineException(String.format("kerberos config 解析异常,Caused by: %s", e.getMessage()), e);
        }
    } else {
        localKerberosPath = kerberosService.getLocalKerberosPath(source.getId());
        kerberosConfig = fillKerberosConfig(source.getId());
    }
    try {
        source.setDataJson(DataSourceUtils.getDataSourceJson(source.getDataJsonString()));
    } catch (Exception e) {
        LOGGER.error("检查数据源连接,DataJsonString 转化异常", e);
        throw new PubSvcDefineException("JSONObject 转化异常", e);
    }
    // 设置前台传入的principals
    setPrincipals(source.getDataJson(), kerberosConfig);
    return checkConnectionWithConf(source, kerberosConfig, localKerberosPath);
}
Also used : DataSourceTypeEnum(com.dtstack.taier.common.enums.DataSourceTypeEnum) PubSvcDefineException(com.dtstack.taier.common.exception.PubSvcDefineException) JSONObject(com.alibaba.fastjson.JSONObject) IKerberos(com.dtstack.dtcenter.loader.client.IKerberos) IOException(java.io.IOException) SftpException(com.jcraft.jsch.SftpException) RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) IOException(java.io.IOException) PubSvcDefineException(com.dtstack.taier.common.exception.PubSvcDefineException) DtCenterDefException(com.dtstack.taier.common.exception.DtCenterDefException)

Aggregations

PubSvcDefineException (com.dtstack.taier.common.exception.PubSvcDefineException)9 JSONObject (com.alibaba.fastjson.JSONObject)8 DataSourceTypeEnum (com.dtstack.taier.common.enums.DataSourceTypeEnum)6 DsInfo (com.dtstack.taier.dao.domain.DsInfo)5 IOException (java.io.IOException)5 DtCenterDefException (com.dtstack.taier.common.exception.DtCenterDefException)4 RdosDefineException (com.dtstack.taier.common.exception.RdosDefineException)4 SftpException (com.jcraft.jsch.SftpException)4 IKerberos (com.dtstack.dtcenter.loader.client.IKerberos)3 HashMap (java.util.HashMap)2 Transactional (org.springframework.transaction.annotation.Transactional)2 ISourceDTO (com.dtstack.dtcenter.loader.dto.source.ISourceDTO)1 File (java.io.File)1 LinkedHashMap (java.util.LinkedHashMap)1