Search in sources :

Example 11 with JpomRuntimeException

use of io.jpom.system.JpomRuntimeException in project Jpom by dromara.

the class CertModel method decodeCert.

/**
 * 解析证书
 *
 * @param key  zip里面文件
 * @param file 证书文件
 * @return 处理后的json
 */
public static JSONObject decodeCert(String file, String key) {
    if (file == null) {
        return null;
    }
    if (!FileUtil.exist(file)) {
        return null;
    }
    InputStream inputStream = null;
    try {
        inputStream = ResourceUtil.getStream(key);
        PrivateKey privateKey = PemUtil.readPemPrivateKey(inputStream);
        IoUtil.close(inputStream);
        inputStream = ResourceUtil.getStream(file);
        PublicKey publicKey = PemUtil.readPemPublicKey(inputStream);
        IoUtil.close(inputStream);
        RSA rsa = new RSA(privateKey, publicKey);
        String encryptStr = rsa.encryptBase64(KEY, KeyType.PublicKey);
        String decryptStr = rsa.decryptStr(encryptStr, KeyType.PrivateKey);
        if (!KEY.equals(decryptStr)) {
            throw new JpomRuntimeException("证书和私钥证书不匹配");
        }
    } finally {
        IoUtil.close(inputStream);
    }
    try {
        inputStream = ResourceUtil.getStream(file);
        // 创建证书对象
        X509Certificate oCert = (X509Certificate) KeyUtil.readX509Certificate(inputStream);
        // 到期时间
        Date expirationTime = oCert.getNotAfter();
        // 生效日期
        Date effectiveTime = oCert.getNotBefore();
        // 域名
        String name = oCert.getSubjectDN().getName();
        int i = name.indexOf("=");
        String domain = name.substring(i + 1);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("expirationTime", expirationTime.getTime());
        jsonObject.put("effectiveTime", effectiveTime.getTime());
        jsonObject.put("domain", domain);
        jsonObject.put("pemPath", file);
        jsonObject.put("keyPath", key);
        return jsonObject;
    } catch (Exception e) {
        DefaultSystemLog.getLog().error(e.getMessage(), e);
    } finally {
        IoUtil.close(inputStream);
    }
    return null;
}
Also used : RSA(cn.hutool.crypto.asymmetric.RSA) PrivateKey(java.security.PrivateKey) JSONObject(com.alibaba.fastjson.JSONObject) InputStream(java.io.InputStream) PublicKey(java.security.PublicKey) JpomRuntimeException(io.jpom.system.JpomRuntimeException) X509Certificate(java.security.cert.X509Certificate) Date(java.util.Date) JpomRuntimeException(io.jpom.system.JpomRuntimeException)

Example 12 with JpomRuntimeException

use of io.jpom.system.JpomRuntimeException in project Jpom by dromara.

the class NodeProjectInfoModel method repairWhitelist.

private void repairWhitelist() {
    if (StrUtil.isEmpty(whitelistDirectory) && StrUtil.isEmpty(lib)) {
        throw new JpomRuntimeException("当前项目lib数据异常");
    }
    if (StrUtil.isNotEmpty(whitelistDirectory)) {
        return;
    }
    WhitelistDirectoryService whitelistDirectoryService = SpringUtil.getBean(WhitelistDirectoryService.class);
    List<String> project = whitelistDirectoryService.getWhitelist().getProject();
    for (String path : project) {
        if (lib.startsWith(path)) {
            String itemWhitelistDirectory = lib.substring(0, path.length());
            lib = lib.substring(path.length());
            setWhitelistDirectory(itemWhitelistDirectory);
            setLib(lib);
        }
    }
}
Also used : JpomRuntimeException(io.jpom.system.JpomRuntimeException) WhitelistDirectoryService(io.jpom.service.WhitelistDirectoryService)

Example 13 with JpomRuntimeException

use of io.jpom.system.JpomRuntimeException in project Jpom by dromara.

the class JsonFileUtil method readJson.

/**
 * 读取json 文件,同步
 *
 * @param path 路径
 * @return JSON
 * @throws FileNotFoundException 文件异常
 */
public static JSON readJson(String path) throws FileNotFoundException {
    File file = new File(path);
    if (!file.exists()) {
        throw new FileNotFoundException("没有找到对应配置文件:" + path);
    }
    READ_LOCK.lock();
    // 防止多线程操作文件异常
    try {
        String json = FileUtil.readString(file, CharsetUtil.CHARSET_UTF_8);
        if (StrUtil.isEmpty(json)) {
            return new JSONObject();
        }
        try {
            return (JSON) JSON.parse(json);
        } catch (Exception e) {
            throw new JpomRuntimeException("数据文件内容错误,请检查文件是否被非法修改:" + path, e);
        }
    } finally {
        READ_LOCK.unlock();
    }
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) JpomRuntimeException(io.jpom.system.JpomRuntimeException) FileNotFoundException(java.io.FileNotFoundException) JSON(com.alibaba.fastjson.JSON) File(java.io.File) JpomRuntimeException(io.jpom.system.JpomRuntimeException) FileNotFoundException(java.io.FileNotFoundException)

Example 14 with JpomRuntimeException

use of io.jpom.system.JpomRuntimeException in project Jpom by dromara.

the class ReleaseManage method diffSyncProject.

/**
 * 差异上传发布
 *
 * @param nodeModel 节点
 * @param projectId 项目ID
 * @param afterOpt  发布后的操作
 */
private void diffSyncProject(NodeModel nodeModel, String projectId, AfterOpt afterOpt, boolean clearOld) {
    File resultFile = this.resultFile;
    String resultFileParent = resultFile.isFile() ? FileUtil.getAbsolutePath(resultFile.getParent()) : FileUtil.getAbsolutePath(this.resultFile);
    // 
    List<File> files = FileUtil.loopFiles(resultFile);
    List<JSONObject> collect = files.stream().map(file -> {
        // 
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", StringUtil.delStartPath(file, resultFileParent, true));
        jsonObject.put("sha1", SecureUtil.sha1(file));
        return jsonObject;
    }).collect(Collectors.toList());
    // 
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("id", projectId);
    jsonObject.put("data", collect);
    JsonMessage<JSONObject> requestBody = NodeForward.requestBody(nodeModel, NodeUrl.MANAGE_FILE_DIFF_FILE, this.userModel, jsonObject);
    if (requestBody.getCode() != HttpStatus.HTTP_OK) {
        throw new JpomRuntimeException("对比项目文件失败:" + requestBody);
    }
    JSONObject data = requestBody.getData();
    JSONArray diff = data.getJSONArray("diff");
    JSONArray del = data.getJSONArray("del");
    int delSize = CollUtil.size(del);
    int diffSize = CollUtil.size(diff);
    if (clearOld) {
        logRecorder.info(StrUtil.format("对比文件结果,产物文件 {} 个、需要上传 {} 个、需要删除 {} 个", CollUtil.size(collect), CollUtil.size(diff), delSize));
    } else {
        logRecorder.info(StrUtil.format("对比文件结果,产物文件 {} 个、需要上传 {} 个", CollUtil.size(collect), CollUtil.size(diff)));
    }
    // 清空发布才先执行删除
    if (delSize > 0 && clearOld) {
        jsonObject.put("data", del);
        requestBody = NodeForward.requestBody(nodeModel, NodeUrl.MANAGE_FILE_BATCH_DELETE, this.userModel, jsonObject);
        if (requestBody.getCode() != HttpStatus.HTTP_OK) {
            throw new JpomRuntimeException("删除项目文件失败:" + requestBody);
        }
    }
    for (int i = 0; i < diffSize; i++) {
        boolean last = (i == diffSize - 1);
        JSONObject diffData = (JSONObject) diff.get(i);
        String name = diffData.getString("name");
        File file = FileUtil.file(resultFileParent, name);
        // 
        String startPath = StringUtil.delStartPath(file, resultFileParent, false);
        // 
        JsonMessage<String> jsonMessage = OutGivingRun.fileUpload(file, startPath, projectId, false, last ? afterOpt : AfterOpt.No, nodeModel, this.userModel, false);
        if (jsonMessage.getCode() != HttpStatus.HTTP_OK) {
            throw new JpomRuntimeException("同步项目文件失败:" + jsonMessage);
        }
        if (last) {
            // 最后一个
            logRecorder.info("发布项目包成功:" + jsonMessage);
        }
    }
}
Also used : DateUtil(cn.hutool.core.date.DateUtil) SecureUtil(cn.hutool.crypto.SecureUtil) DockerInfoService(io.jpom.service.docker.DockerInfoService) Map(java.util.Map) WorkspaceEnvVarService(io.jpom.service.system.WorkspaceEnvVarService) BetweenFormatter(cn.hutool.core.date.BetweenFormatter) Sftp(cn.hutool.extra.ssh.Sftp) NodeForward(io.jpom.common.forward.NodeForward) LineHandler(cn.hutool.core.io.LineHandler) SshService(io.jpom.service.node.ssh.SshService) NodeModel(io.jpom.model.data.NodeModel) AfterOpt(io.jpom.model.AfterOpt) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) StrUtil(cn.hutool.core.util.StrUtil) List(java.util.List) Builder(lombok.Builder) Session(com.jcraft.jsch.Session) ArrayUtil(cn.hutool.core.util.ArrayUtil) SshModel(io.jpom.model.data.SshModel) SystemClock(cn.hutool.core.date.SystemClock) CharPool(cn.hutool.core.text.CharPool) JSONObject(com.alibaba.fastjson.JSONObject) HttpStatus(cn.hutool.http.HttpStatus) IPlugin(io.jpom.plugin.IPlugin) CommandUtil(io.jpom.util.CommandUtil) ResourceUtil(cn.hutool.core.io.resource.ResourceUtil) LogRecorder(io.jpom.util.LogRecorder) BuildReleaseMethod(io.jpom.model.enums.BuildReleaseMethod) JschUtil(cn.hutool.extra.ssh.JschUtil) JpomRuntimeException(io.jpom.system.JpomRuntimeException) JsonMessage(cn.jiangzeyin.common.JsonMessage) JSONArray(com.alibaba.fastjson.JSONArray) BaseEnum(io.jpom.model.BaseEnum) NodeService(io.jpom.service.node.NodeService) OutGivingRun(io.jpom.outgiving.OutGivingRun) PluginFactory(io.jpom.plugin.PluginFactory) IoUtil(cn.hutool.core.io.IoUtil) DockerInfoModel(io.jpom.model.docker.DockerInfoModel) SpringUtil(cn.jiangzeyin.common.spring.SpringUtil) DockerSwarmInfoService(io.jpom.service.docker.DockerSwarmInfoService) File(java.io.File) NodeUrl(io.jpom.common.forward.NodeUrl) Consumer(java.util.function.Consumer) CollUtil(cn.hutool.core.collection.CollUtil) StringUtil(io.jpom.util.StringUtil) ConfigBean(io.jpom.system.ConfigBean) FileUtil(cn.hutool.core.io.FileUtil) UserModel(io.jpom.model.data.UserModel) BuildStatus(io.jpom.model.enums.BuildStatus) InputStream(java.io.InputStream) JSONObject(com.alibaba.fastjson.JSONObject) JpomRuntimeException(io.jpom.system.JpomRuntimeException) JSONArray(com.alibaba.fastjson.JSONArray) File(java.io.File)

Example 15 with JpomRuntimeException

use of io.jpom.system.JpomRuntimeException in project Jpom by dromara.

the class RepositoryController method editRepository.

/**
 * edit
 *
 * @param repositoryModelReq 仓库实体
 * @return json
 */
@PostMapping(value = "/build/repository/edit")
@Feature(method = MethodFeature.EDIT)
public Object editRepository(RepositoryModel repositoryModelReq) {
    this.checkInfo(repositoryModelReq);
    // 检查 rsa 私钥
    boolean andUpdateSshKey = this.checkAndUpdateSshKey(repositoryModelReq);
    Assert.state(andUpdateSshKey, "rsa 私钥文件不存在或者有误");
    if (repositoryModelReq.getRepoType() == RepositoryModel.RepoType.Git.getCode()) {
        RepositoryModel repositoryModel = repositoryService.getByKey(repositoryModelReq.getId(), false);
        if (repositoryModel != null) {
            repositoryModelReq.setRsaPrv(StrUtil.emptyToDefault(repositoryModelReq.getRsaPrv(), repositoryModel.getRsaPrv()));
            repositoryModelReq.setPassword(StrUtil.emptyToDefault(repositoryModelReq.getPassword(), repositoryModel.getPassword()));
        }
        // 验证 git 仓库信息
        try {
            IPlugin plugin = PluginFactory.getPlugin("git-clone");
            Map<String, Object> map = repositoryModelReq.toMap();
            Tuple branchAndTagList = (Tuple) plugin.execute("branchAndTagList", map);
        // Tuple tuple = GitUtil.getBranchAndTagList(repositoryModelReq);
        } catch (JpomRuntimeException jpomRuntimeException) {
            throw jpomRuntimeException;
        } catch (Exception e) {
            DefaultSystemLog.getLog().warn("获取仓库分支失败", e);
            return JsonMessage.toJson(500, "无法连接此仓库," + e.getMessage());
        }
    }
    if (StrUtil.isEmpty(repositoryModelReq.getId())) {
        // insert data
        repositoryService.insert(repositoryModelReq);
    } else {
        // update data
        // repositoryModelReq.setWorkspaceId(repositoryService.getCheckUserWorkspace(getRequest()));
        repositoryService.updateById(repositoryModelReq, getRequest());
    }
    return JsonMessage.toJson(200, "操作成功");
}
Also used : JpomRuntimeException(io.jpom.system.JpomRuntimeException) JSONObject(com.alibaba.fastjson.JSONObject) RepositoryModel(io.jpom.model.data.RepositoryModel) Tuple(cn.hutool.core.lang.Tuple) JpomRuntimeException(io.jpom.system.JpomRuntimeException) PostMapping(org.springframework.web.bind.annotation.PostMapping) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature)

Aggregations

JpomRuntimeException (io.jpom.system.JpomRuntimeException)16 JSONObject (com.alibaba.fastjson.JSONObject)7 File (java.io.File)6 InputStream (java.io.InputStream)4 BetweenFormatter (cn.hutool.core.date.BetweenFormatter)2 DateUtil (cn.hutool.core.date.DateUtil)2 FileUtil (cn.hutool.core.io.FileUtil)2 LineHandler (cn.hutool.core.io.LineHandler)2 Tuple (cn.hutool.core.lang.Tuple)2 SecureUtil (cn.hutool.crypto.SecureUtil)2 Db (cn.hutool.db.Db)2 JsonMessage (cn.jiangzeyin.common.JsonMessage)2 JSON (com.alibaba.fastjson.JSON)2 AfterOpt (io.jpom.model.AfterOpt)2 NodeModel (io.jpom.model.data.NodeModel)2 JarFile (java.util.jar.JarFile)2 ZipFile (java.util.zip.ZipFile)2 JdbcSQLNonTransientException (org.h2.jdbc.JdbcSQLNonTransientException)2 CollUtil (cn.hutool.core.collection.CollUtil)1 Convert (cn.hutool.core.convert.Convert)1