Search in sources :

Example 6 with DataSourceTypeEnum

use of com.dtstack.taier.common.enums.DataSourceTypeEnum 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)

Example 7 with DataSourceTypeEnum

use of com.dtstack.taier.common.enums.DataSourceTypeEnum in project Taier by DTStack.

the class DatasourceService method addOrUpdateSourceWithKerberos.

/**
 * 上传Kerberos添加和修改数据源
 * @param dataSourceVO
 * @param resource
 * @param userId
 * @param tenantId
 * @return
 */
@Transactional(rollbackFor = Exception.class)
public Long addOrUpdateSourceWithKerberos(DataSourceVO dataSourceVO, Pair<String, String> resource, Long userId, Long tenantId) {
    Map<String, Object> confMap;
    JSONObject dataJson = DataSourceUtils.getDataSourceJson(dataSourceVO.getDataJsonString());
    dataSourceVO.setDataJson(dataJson);
    List<Integer> list = JSON.parseObject(dataSourceVO.getAppTypeListString(), List.class);
    dataSourceVO.setAppTypeList(list);
    String localKerberosConf;
    DataSourceTypeEnum typeEnum = DataSourceTypeEnum.typeVersionOf(dataSourceVO.getDataType(), dataSourceVO.getDataVersion());
    if (Objects.nonNull(resource)) {
        // resource不为空表示本地上传文件
        localKerberosConf = kerberosService.getTempLocalKerberosConf(userId);
        try {
            confMap = ClientCache.getKerberos(typeEnum.getVal()).parseKerberosFromUpload(resource.getRight(), localKerberosConf);
        } catch (IOException e) {
            LOGGER.error("添加数据源, kerberos config 解析异常,{}", e.getMessage(), e);
            throw new PubSvcDefineException(String.format("kerberos config 解析异常,Caused by: %s", e.getMessage()), e);
        }
        // 设置openKerberos变量表示开启kerberos认证
        DataSourceUtils.setOpenKerberos(dataJson, true);
        DataSourceUtils.setKerberosFile(dataJson, resource.getRight());
    } else {
        DsInfo originSource = dsInfoService.getOneById(dataSourceVO.getId());
        DataSourceUtils.getOriginKerberosConfig(dataJson, originSource.getDataJson());
        localKerberosConf = kerberosService.getLocalKerberosPath(dataSourceVO.getId());
        confMap = fillKerberosConfig(dataSourceVO.getId());
    }
    // 设置前台传入的principals
    setPrincipals(dataJson, confMap);
    // 检查链接
    Boolean connValue = checkConnectionWithConf(dataSourceVO, confMap, localKerberosConf);
    if (!connValue) {
        throw new PubSvcDefineException("不能添加连接失败的数据源", ErrorCode.CONFIG_ERROR);
    }
    Long dataSourceId = dataSourceVO.getId();
    // 有认证文件上传 进行上传至sftp操作
    Long id = null;
    try {
        if (Objects.nonNull(resource)) {
            if (dataSourceVO.getId() == 0L) {
                // 没有保存过的数据源需要先保存, 获取自增id
                dataSourceVO.setKerberosConfig(confMap);
                dataSourceVO.setDataJson((JSONObject) dataJson.clone());
                dataSourceVO.setLocalKerberosConf(localKerberosConf);
                dataSourceId = addOrUpdate(dataSourceVO, userId);
            }
            Map<String, String> sftpMap = kerberosService.getSftpMap(tenantId);
            // 目录转换 - 将临时目录根据数据源ID转移到新的kerberos文件目录
            File localKerberosConfDir = new File(localKerberosConf);
            File newConfDir = new File(kerberosService.getLocalKerberosPath(dataSourceId));
            // 如果原来的目录存在 删除原来的文件
            try {
                FileUtils.deleteDirectory(newConfDir);
            } catch (IOException e) {
                LOGGER.error("删除历史的kerberos文件失败", e);
            }
            // 目录转换, temp路径转换成新路径
            localKerberosConfDir.renameTo(newConfDir);
            // 上传配置文件到sftp
            String dataSourceKey = kerberosService.getSourceKey(dataSourceId, null);
            KerberosService.uploadDirFinal(sftpMap, newConfDir.getPath(), dataSourceKey);
            confMap.put("kerberosDir", dataSourceKey);
        }
        dataSourceVO.setKerberosConfig(confMap);
        dataSourceVO.setLocalKerberosConf(localKerberosConf);
        dataSourceVO.setDataJson(dataJson);
        dataSourceVO.setId(dataSourceId);
        id = addOrUpdate(dataSourceVO, userId);
    } catch (Exception e) {
        LOGGER.error("addOrUpdateSourceWithKerberos error", e);
        throw new PubSvcDefineException(e.getMessage());
    }
    return id;
}
Also used : DataSourceTypeEnum(com.dtstack.taier.common.enums.DataSourceTypeEnum) PubSvcDefineException(com.dtstack.taier.common.exception.PubSvcDefineException) 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) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject) DsInfo(com.dtstack.taier.dao.domain.DsInfo) File(java.io.File) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with DataSourceTypeEnum

use of com.dtstack.taier.common.enums.DataSourceTypeEnum in project Taier by DTStack.

the class DatasourceService method addOrUpdate.

/**
 * 添加和修改数据源信息
 * @param dataSourceVO
 * @param userId
 * @return
 */
@Transactional(rollbackFor = Exception.class)
public Long addOrUpdate(DataSourceVO dataSourceVO, Long userId) {
    DataSourceTypeEnum typeEnum = DataSourceTypeEnum.typeVersionOf(dataSourceVO.getDataType(), dataSourceVO.getDataVersion());
    JSONObject json = dataSourceVO.getDataJson();
    // 字段转换
    colMap(json, typeEnum.getVal(), dataSourceVO.getKerberosConfig());
    dataSourceVO.setDataJson(json);
    dataSourceVO.setModifyUserId(userId);
    // 构造数据源元数据
    DsInfo dsInfo = buildDsInfo(dataSourceVO);
    if (dataSourceVO.getId() > 0) {
        // edit 不存在授权操作
        dsInfoService.getOneById(dataSourceVO.getId());
        dsInfo.setId(dataSourceVO.getId());
        dsInfo.setModifyUserId(dataSourceVO.getUserId());
        if (dsInfoService.checkDataNameDup(dsInfo)) {
            throw new PubSvcDefineException(ErrorCode.DATASOURCE_DUP_NAME);
        }
        dsInfoService.updateById(dsInfo);
    } else {
        // add 存在授权产品操作
        dsInfo.setCreateUserId(dataSourceVO.getUserId());
        dsInfo.setModifyUserId(dataSourceVO.getUserId());
        if (dsInfoService.checkDataNameDup(dsInfo)) {
            throw new PubSvcDefineException(ErrorCode.DATASOURCE_DUP_NAME);
        }
        dsInfoService.save(dsInfo);
        // 保存数据源类型权重
        typeService.plusDataTypeWeight(dsInfo.getDataType(), 1);
    }
    return dsInfo.getId();
}
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) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with DataSourceTypeEnum

use of com.dtstack.taier.common.enums.DataSourceTypeEnum in project Taier by DTStack.

the class KerberosService method prepareKerberosConfig.

/**
 * 预处理Kerberos配置
 */
public void prepareKerberosConfig(DsInfoBO dsInfoBO) {
    if (dsInfoBO.getKerberosConfig() == null) {
        return;
    }
    try {
        // 获取kerberos本地路径
        String localKerberosConf = getLocalKerberosPath(dsInfoBO.getId());
        downloadKerberosFromSftp(dsInfoBO.getIsMeta(), dsInfoBO.getId(), DataSourceUtils.getDataSourceJson(dsInfoBO.getDataJson()), localKerberosConf, dsInfoBO.getTenantId());
    } catch (SftpException e) {
        throw new DtCenterDefException(String.format("获取kerberos认证文件失败,Caused by: %s", e.getMessage()), e);
    }
    String localKerberosPath = getLocalKerberosPath(dsInfoBO.getId());
    JSONObject dataJson = dsInfoBO.getData();
    // principal 键
    String principal = dataJson.getString(FormNames.PRINCIPAL);
    Map<String, Object> kerberosConfig = dsInfoBO.getKerberosConfig();
    if (Strings.isNotBlank(principal)) {
        kerberosConfig.put(HadoopConfTool.PRINCIPAL, principal);
    }
    // Hbase master kerberos Principal
    String hbaseMasterPrincipal = dataJson.getString(FormNames.HBASE_MASTER_PRINCIPAL);
    if (Strings.isNotBlank(hbaseMasterPrincipal)) {
        kerberosConfig.put(HadoopConfTool.HBASE_MASTER_PRINCIPAL, hbaseMasterPrincipal);
    }
    // Hbase region kerberos Principal
    String hbasePrincipal = dataJson.getString(FormNames.HBASE_REGION_PRINCIPAL);
    if (Strings.isNotBlank(hbasePrincipal)) {
        kerberosConfig.put(HadoopConfTool.HBASE_REGION_PRINCIPAL, hbasePrincipal);
    }
    DataSourceTypeEnum typeEnum = DataSourceTypeEnum.typeVersionOf(dsInfoBO.getDataType(), dsInfoBO.getDataVersion());
    IKerberos kerberos = ClientCache.getKerberos(typeEnum.getVal());
    kerberos.prepareKerberosForConnect(kerberosConfig, localKerberosPath);
    dsInfoBO.setKerberosConfig(kerberosConfig);
}
Also used : DataSourceTypeEnum(com.dtstack.taier.common.enums.DataSourceTypeEnum) JSONObject(com.alibaba.fastjson.JSONObject) SftpException(com.jcraft.jsch.SftpException) DtCenterDefException(com.dtstack.taier.common.exception.DtCenterDefException) JSONObject(com.alibaba.fastjson.JSONObject) IKerberos(com.dtstack.dtcenter.loader.client.IKerberos)

Aggregations

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