🔨 添加主机配置逻辑.
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright (c) 2023 - present Dromara, All rights reserved.
|
||||
*
|
||||
* https://visor.dromara.org
|
||||
* https://visor.dromara.org.cn
|
||||
* https://visor.orionsec.cn
|
||||
*
|
||||
* Members:
|
||||
* Jiahang Li - ljh1553488six@139.com - author
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.dromara.visor.common.mapstruct;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
/**
|
||||
* json 转换器
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2025/3/7 17:43
|
||||
*/
|
||||
public class JsonConversion {
|
||||
|
||||
private JsonConversion() {
|
||||
}
|
||||
|
||||
/**
|
||||
* JSONString > JSONObject
|
||||
*
|
||||
* @param json json
|
||||
* @return JSONObject
|
||||
*/
|
||||
public static JSONObject stringToJsonObject(String json) {
|
||||
return json != null ? JSON.parseObject(json) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* JSONString > JSONArray.
|
||||
*
|
||||
* @param json json
|
||||
* @return JSONArray
|
||||
*/
|
||||
public static JSONArray stringToJsonArray(String json) {
|
||||
return json != null ? JSON.parseArray(json) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* JSONObject > JSONString.
|
||||
*
|
||||
* @param jsonObject JSONObject
|
||||
* @return JSONString
|
||||
*/
|
||||
public static String jsonObjectToString(JSONObject jsonObject) {
|
||||
return jsonObject != null ? jsonObject.toJSONString() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* JSONArray > JSONString.
|
||||
*
|
||||
* @param jsonArray JSONArray
|
||||
* @return JSONString
|
||||
*/
|
||||
public static String jsonArrayToString(JSONArray jsonArray) {
|
||||
return jsonArray != null ? jsonArray.toJSONString() : null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* Copyright (c) 2023 - present Dromara, All rights reserved.
|
||||
*
|
||||
* https://visor.dromara.org
|
||||
* https://visor.dromara.org.cn
|
||||
* https://visor.orionsec.cn
|
||||
*
|
||||
* Members:
|
||||
* Jiahang Li - ljh1553488six@139.com - author
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.dromara.visor.common.mapstruct;
|
||||
|
||||
import cn.orionsec.kit.lang.utils.Strings;
|
||||
import cn.orionsec.kit.lang.utils.collect.Lists;
|
||||
import org.dromara.visor.common.constant.Const;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* string 转换器
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2025/3/7 17:35
|
||||
*/
|
||||
public class StringConversion {
|
||||
|
||||
private StringConversion() {
|
||||
}
|
||||
|
||||
/**
|
||||
* String > List<Integer>
|
||||
*
|
||||
* @param str str
|
||||
* @return list
|
||||
*/
|
||||
public static List<Integer> stringToIntegerList(String str) {
|
||||
if (Strings.isBlank(str)) {
|
||||
return Lists.newList();
|
||||
}
|
||||
return Arrays.stream(str.split(Const.COMMA))
|
||||
.map(Integer::valueOf)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* String > List<String>
|
||||
*
|
||||
* @param str str
|
||||
* @return list
|
||||
*/
|
||||
public static List<Long> stringToLongList(String str) {
|
||||
if (Strings.isBlank(str)) {
|
||||
return Lists.newList();
|
||||
}
|
||||
return Arrays.stream(str.split(Const.COMMA))
|
||||
.map(Long::valueOf)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* String > List<String>
|
||||
*
|
||||
* @param str str
|
||||
* @return list
|
||||
*/
|
||||
public static List<String> stringToStringList(String str) {
|
||||
if (Strings.isBlank(str)) {
|
||||
return Lists.newList();
|
||||
}
|
||||
return Lists.of(str.split(Const.COMMA));
|
||||
}
|
||||
|
||||
/**
|
||||
* List<Integer> String
|
||||
*
|
||||
* @param list list
|
||||
* @return str
|
||||
*/
|
||||
public static String integerListToString(List<Integer> list) {
|
||||
if (list != null) {
|
||||
return list.stream()
|
||||
.map(String::valueOf)
|
||||
.collect(Collectors.joining(Const.COMMA));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* List<Long> String
|
||||
*
|
||||
* @param list list
|
||||
* @return str
|
||||
*/
|
||||
public static String longListToString(List<Long> list) {
|
||||
if (list != null) {
|
||||
return list.stream()
|
||||
.map(String::valueOf)
|
||||
.collect(Collectors.joining(Const.COMMA));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* List<String> String
|
||||
*
|
||||
* @param list list
|
||||
* @return str
|
||||
*/
|
||||
public static String stringListToString(List<String> list) {
|
||||
return list != null ? String.join(Const.COMMA, list) : null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright (c) 2023 - present Dromara, All rights reserved.
|
||||
*
|
||||
* https://visor.dromara.org
|
||||
* https://visor.dromara.org.cn
|
||||
* https://visor.orionsec.cn
|
||||
*
|
||||
* Members:
|
||||
* Jiahang Li - ljh1553488six@139.com - author
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.dromara.visor.module.asset.controller;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.visor.framework.biz.operator.log.core.annotation.OperatorLog;
|
||||
import org.dromara.visor.framework.log.core.annotation.IgnoreLog;
|
||||
import org.dromara.visor.framework.log.core.enums.IgnoreLogMode;
|
||||
import org.dromara.visor.framework.web.core.annotation.DemoDisableApi;
|
||||
import org.dromara.visor.framework.web.core.annotation.RestWrapper;
|
||||
import org.dromara.visor.module.asset.define.operator.HostOperatorType;
|
||||
import org.dromara.visor.module.asset.entity.request.host.HostConfigQueryRequest;
|
||||
import org.dromara.visor.module.asset.entity.request.host.HostConfigUpdateRequest;
|
||||
import org.dromara.visor.module.asset.service.HostConfigService;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 主机 api
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023-9-11 14:16
|
||||
*/
|
||||
@Tag(name = "asset - 主机配置服务")
|
||||
@Slf4j
|
||||
@Validated
|
||||
@RestWrapper
|
||||
@RestController
|
||||
@RequestMapping("/asset/host-config")
|
||||
public class HostConfigController {
|
||||
|
||||
@Resource
|
||||
private HostConfigService hostConfigService;
|
||||
|
||||
@DemoDisableApi
|
||||
@OperatorLog(HostOperatorType.UPDATE_CONFIG)
|
||||
@PutMapping("/update")
|
||||
@Operation(summary = "更新主机配置")
|
||||
@PreAuthorize("@ss.hasPermission('asset:host:update-config')")
|
||||
public Integer updateHostConfig(@Validated @RequestBody HostConfigUpdateRequest request) {
|
||||
return hostConfigService.updateHostConfig(request);
|
||||
}
|
||||
|
||||
@IgnoreLog(IgnoreLogMode.RET)
|
||||
@PostMapping("/get")
|
||||
@Operation(summary = "查询主机配置")
|
||||
@PreAuthorize("@ss.hasPermission('asset:host:query')")
|
||||
public Object getHostConfig(@Validated @RequestBody HostConfigQueryRequest request) {
|
||||
return hostConfigService.getHostConfigView(request);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -35,8 +35,8 @@ import org.dromara.visor.framework.web.core.annotation.DemoDisableApi;
|
||||
import org.dromara.visor.framework.web.core.annotation.RestWrapper;
|
||||
import org.dromara.visor.module.asset.define.operator.HostOperatorType;
|
||||
import org.dromara.visor.module.asset.entity.request.host.*;
|
||||
import org.dromara.visor.module.asset.entity.vo.HostConfigVO;
|
||||
import org.dromara.visor.module.asset.entity.vo.HostVO;
|
||||
import org.dromara.visor.module.asset.service.HostConnectService;
|
||||
import org.dromara.visor.module.asset.service.HostService;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
@@ -63,6 +63,9 @@ public class HostController {
|
||||
@Resource
|
||||
private HostService hostService;
|
||||
|
||||
@Resource
|
||||
private HostConnectService hostConnectService;
|
||||
|
||||
@DemoDisableApi
|
||||
@OperatorLog(HostOperatorType.CREATE)
|
||||
@PostMapping("/create")
|
||||
@@ -90,15 +93,6 @@ public class HostController {
|
||||
return hostService.updateHostStatus(request);
|
||||
}
|
||||
|
||||
@DemoDisableApi
|
||||
@OperatorLog(HostOperatorType.UPDATE_CONFIG)
|
||||
@PutMapping("/update-config")
|
||||
@Operation(summary = "更新主机配置")
|
||||
@PreAuthorize("@ss.hasPermission('asset:host:update-config')")
|
||||
public Integer updateHostConfig(@Validated @RequestBody HostUpdateConfigRequest request) {
|
||||
return hostService.updateHostConfig(request);
|
||||
}
|
||||
|
||||
@IgnoreLog(IgnoreLogMode.RET)
|
||||
@GetMapping("/get")
|
||||
@Operation(summary = "通过 id 查询主机")
|
||||
@@ -108,15 +102,6 @@ public class HostController {
|
||||
return hostService.getHostById(id);
|
||||
}
|
||||
|
||||
@IgnoreLog(IgnoreLogMode.RET)
|
||||
@GetMapping("/get-config")
|
||||
@Operation(summary = "查询主机配置")
|
||||
@Parameter(name = "id", description = "id", required = true)
|
||||
@PreAuthorize("@ss.hasPermission('asset:host:query')")
|
||||
public HostConfigVO getHostConfig(@RequestParam("id") Long id) {
|
||||
return hostService.getHostConfig(id);
|
||||
}
|
||||
|
||||
@IgnoreLog(IgnoreLogMode.RET)
|
||||
@GetMapping("/list")
|
||||
@Operation(summary = "查询主机")
|
||||
@@ -162,5 +147,14 @@ public class HostController {
|
||||
return hostService.deleteHostByIdList(idList);
|
||||
}
|
||||
|
||||
@DemoDisableApi
|
||||
@PostMapping("/test-connect")
|
||||
@Operation(summary = "测试主机连接")
|
||||
@PreAuthorize("@ss.hasAnyPermission('asset:host:update', 'asset:host:update-config')")
|
||||
public Boolean testHostConnect(@Validated @RequestBody HostTestConnectRequest request) {
|
||||
hostConnectService.testHostConnect(request);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -29,7 +29,6 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.visor.framework.log.core.annotation.IgnoreLog;
|
||||
import org.dromara.visor.framework.log.core.enums.IgnoreLogMode;
|
||||
import org.dromara.visor.framework.web.core.annotation.RestWrapper;
|
||||
import org.dromara.visor.module.asset.entity.request.host.HostExtraQueryRequest;
|
||||
import org.dromara.visor.module.asset.entity.request.host.HostExtraUpdateRequest;
|
||||
import org.dromara.visor.module.asset.service.HostExtraService;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
@@ -61,15 +60,8 @@ public class HostExtraController {
|
||||
@Operation(summary = "获取主机拓展信息")
|
||||
@Parameter(name = "hostId", description = "hostId", required = true)
|
||||
@Parameter(name = "item", description = "item", required = true)
|
||||
public Map<String, Object> getHostExtra(@RequestParam("hostId") Long hostId, @RequestParam("item") String item) {
|
||||
return hostExtraService.getHostExtra(hostId, item);
|
||||
}
|
||||
|
||||
@IgnoreLog(IgnoreLogMode.RET)
|
||||
@PostMapping("/list")
|
||||
@Operation(summary = "获取多个主机拓展信息")
|
||||
public Map<String, Map<String, Object>> getHostExtraList(@Validated @RequestBody HostExtraQueryRequest request) {
|
||||
return hostExtraService.getHostExtraList(request);
|
||||
public Map<String, Object> getHostExtraView(@RequestParam("hostId") Long hostId, @RequestParam("item") String item) {
|
||||
return hostExtraService.getHostExtraView(hostId, item);
|
||||
}
|
||||
|
||||
@PutMapping("/update")
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
*/
|
||||
package org.dromara.visor.module.asset.convert;
|
||||
|
||||
import org.dromara.visor.common.mapstruct.StringConversion;
|
||||
import org.dromara.visor.module.asset.entity.domain.HostDO;
|
||||
import org.dromara.visor.module.asset.entity.dto.HostCacheDTO;
|
||||
import org.dromara.visor.module.asset.entity.request.host.HostCreateRequest;
|
||||
@@ -41,7 +42,7 @@ import java.util.List;
|
||||
* @version 1.0.0
|
||||
* @since 2023-9-11 14:16
|
||||
*/
|
||||
@Mapper
|
||||
@Mapper(uses = StringConversion.class)
|
||||
public interface HostConvert {
|
||||
|
||||
HostConvert MAPPER = Mappers.getMapper(HostConvert.class);
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* Copyright (c) 2023 - present Dromara, All rights reserved.
|
||||
*
|
||||
* https://visor.dromara.org
|
||||
* https://visor.dromara.org.cn
|
||||
* https://visor.orionsec.cn
|
||||
*
|
||||
* Members:
|
||||
* Jiahang Li - ljh1553488six@139.com - author
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.dromara.visor.module.asset.dao;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.dromara.visor.framework.mybatis.core.mapper.IMapper;
|
||||
import org.dromara.visor.framework.mybatis.core.query.Conditions;
|
||||
import org.dromara.visor.module.asset.entity.domain.HostConfigDO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 主机配置 Mapper 接口
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2025-3-6 10:59
|
||||
*/
|
||||
@Mapper
|
||||
public interface HostConfigDAO extends IMapper<HostConfigDO> {
|
||||
|
||||
/**
|
||||
* 通过 hostId 和 type 查询
|
||||
*
|
||||
* @param hostId hostId
|
||||
* @param type type
|
||||
* @return config
|
||||
*/
|
||||
default HostConfigDO selectByHostIdType(Long hostId, String type) {
|
||||
return this.of()
|
||||
.createWrapper()
|
||||
.eq(HostConfigDO::getHostId, hostId)
|
||||
.eq(HostConfigDO::getType, type)
|
||||
.then()
|
||||
.getOne();
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新配置状态
|
||||
*
|
||||
* @param hostId hostId
|
||||
* @param types types
|
||||
* @param status status
|
||||
*/
|
||||
default void updateConfigStatus(Long hostId, List<String> types, String status) {
|
||||
HostConfigDO update = HostConfigDO.builder()
|
||||
.status(status)
|
||||
.build();
|
||||
LambdaQueryWrapper<HostConfigDO> condition = this.wrapper()
|
||||
.eq(HostConfigDO::getHostId, hostId)
|
||||
.in(HostConfigDO::getType, types)
|
||||
.ne(HostConfigDO::getStatus, status);
|
||||
this.update(update, condition);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新配置状态
|
||||
*
|
||||
* @param hostId hostId
|
||||
* @param notTypes notTypes
|
||||
* @param status status
|
||||
*/
|
||||
default void updateConfigInvertStatus(Long hostId, List<String> notTypes, String status) {
|
||||
HostConfigDO update = HostConfigDO.builder()
|
||||
.status(status)
|
||||
.build();
|
||||
LambdaQueryWrapper<HostConfigDO> condition = this.wrapper()
|
||||
.eq(HostConfigDO::getHostId, hostId)
|
||||
.notIn(HostConfigDO::getType, notTypes)
|
||||
.ne(HostConfigDO::getStatus, status);
|
||||
this.update(update, condition);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过 hostId 删除
|
||||
*
|
||||
* @param hostIdList hostIdList
|
||||
* @return effect
|
||||
*/
|
||||
default int deleteByHostIdList(List<Long> hostIdList) {
|
||||
return this.delete(Conditions.in(HostConfigDO::getHostId, hostIdList));
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 keyId 为 NULL
|
||||
*
|
||||
* @param keyIdList keyIdList
|
||||
* @return effect
|
||||
*/
|
||||
int setKeyIdWithNull(@Param("keyIdList") List<Long> keyIdList);
|
||||
|
||||
/**
|
||||
* 设置 identityId 为 NULL
|
||||
*
|
||||
* @param identityIdList identityIdList
|
||||
* @return effect
|
||||
*/
|
||||
int setIdentityIdWithNull(@Param("identityIdList") List<Long> identityIdList);
|
||||
|
||||
}
|
||||
@@ -22,13 +22,10 @@
|
||||
*/
|
||||
package org.dromara.visor.module.asset.dao;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.dromara.visor.framework.mybatis.core.mapper.IMapper;
|
||||
import org.dromara.visor.module.asset.entity.domain.HostDO;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -41,54 +38,8 @@ import java.util.List;
|
||||
@Mapper
|
||||
public interface HostDAO extends IMapper<HostDO> {
|
||||
|
||||
List<SFunction<HostDO, ?>> BASE_COLUMN = Arrays.asList(
|
||||
HostDO::getId,
|
||||
HostDO::getType,
|
||||
HostDO::getOsType,
|
||||
HostDO::getName,
|
||||
HostDO::getCode,
|
||||
HostDO::getAddress,
|
||||
HostDO::getPort,
|
||||
HostDO::getStatus,
|
||||
HostDO::getDescription,
|
||||
HostDO::getCreateTime,
|
||||
HostDO::getUpdateTime,
|
||||
HostDO::getCreator,
|
||||
HostDO::getUpdater
|
||||
);
|
||||
|
||||
/**
|
||||
* 通过 id 查询基本信息
|
||||
*
|
||||
* @param id id
|
||||
* @return id
|
||||
*/
|
||||
default HostDO selectBaseById(Long id) {
|
||||
return this.of()
|
||||
.createWrapper()
|
||||
.select(BASE_COLUMN)
|
||||
.eq(HostDO::getId, id)
|
||||
.then()
|
||||
.getOne();
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过 id 查询基本信息
|
||||
*
|
||||
* @param idList idList
|
||||
* @return id
|
||||
*/
|
||||
default List<HostDO> selectBaseByIdList(List<Long> idList) {
|
||||
return this.of()
|
||||
.createWrapper()
|
||||
.select(BASE_COLUMN)
|
||||
.in(HostDO::getId, idList)
|
||||
.then()
|
||||
.list();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取的 hostId
|
||||
* 通过类型查询 hostId
|
||||
*
|
||||
* @param hostIdList hostIdList
|
||||
* @param type type
|
||||
@@ -100,26 +51,10 @@ public interface HostDAO extends IMapper<HostDO> {
|
||||
.createWrapper(true)
|
||||
.select(HostDO::getId)
|
||||
.in(HostDO::getId, hostIdList)
|
||||
.eq(HostDO::getType, type)
|
||||
.eq(HostDO::getStatus, status)
|
||||
.like(HostDO::getTypes, type)
|
||||
.then()
|
||||
.list(HostDO::getId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 keyId 为 NULL
|
||||
*
|
||||
* @param keyIdList keyIdList
|
||||
* @return effect
|
||||
*/
|
||||
int setKeyIdWithNull(@Param("keyIdList") List<Long> keyIdList);
|
||||
|
||||
/**
|
||||
* 设置 identityId 为 NULL
|
||||
*
|
||||
* @param identityIdList identityIdList
|
||||
* @return effect
|
||||
*/
|
||||
int setIdentityIdWithNull(@Param("identityIdList") List<Long> identityIdList);
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (c) 2023 - present Dromara, All rights reserved.
|
||||
*
|
||||
* https://visor.dromara.org
|
||||
* https://visor.dromara.org.cn
|
||||
* https://visor.orionsec.cn
|
||||
*
|
||||
* Members:
|
||||
* Jiahang Li - ljh1553488six@139.com - author
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.dromara.visor.module.asset.entity.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
import org.dromara.visor.framework.mybatis.core.domain.BaseDO;
|
||||
|
||||
/**
|
||||
* 主机配置 实体对象
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2025-3-6 10:59
|
||||
*/
|
||||
@Data
|
||||
@SuperBuilder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@TableName(value = "host_config", autoResultMap = true)
|
||||
@Schema(name = "HostConfigDO", description = "主机配置 实体对象")
|
||||
public class HostConfigDO extends BaseDO {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Schema(description = "主机id")
|
||||
@TableField("host_id")
|
||||
private Long hostId;
|
||||
|
||||
@Schema(description = "配置类型")
|
||||
@TableField("type")
|
||||
private String type;
|
||||
|
||||
@Schema(description = "配置状态")
|
||||
@TableField("status")
|
||||
private String status;
|
||||
|
||||
@Schema(description = "配置值")
|
||||
@TableField("config")
|
||||
private String config;
|
||||
|
||||
}
|
||||
@@ -50,14 +50,18 @@ public class HostDO extends BaseDO {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Schema(description = "主机类型")
|
||||
@TableField("type")
|
||||
private String type;
|
||||
@Schema(description = "类型")
|
||||
@TableField("types")
|
||||
private String types;
|
||||
|
||||
@Schema(description = "系统类型")
|
||||
@TableField("os_type")
|
||||
private String osType;
|
||||
|
||||
@Schema(description = "系统架构")
|
||||
@TableField("arch_type")
|
||||
private String archType;
|
||||
|
||||
@Schema(description = "主机名称")
|
||||
@TableField("name")
|
||||
private String name;
|
||||
@@ -70,19 +74,11 @@ public class HostDO extends BaseDO {
|
||||
@TableField("address")
|
||||
private String address;
|
||||
|
||||
@Schema(description = "主机端口")
|
||||
@TableField("port")
|
||||
private Integer port;
|
||||
|
||||
@Schema(description = "主机状态")
|
||||
@TableField("status")
|
||||
private String status;
|
||||
|
||||
@Schema(description = "主机配置")
|
||||
@TableField("config")
|
||||
private String config;
|
||||
|
||||
@Schema(description = "描述")
|
||||
@Schema(description = "主机描述")
|
||||
@TableField("description")
|
||||
private String description;
|
||||
|
||||
|
||||
@@ -49,11 +49,14 @@ public class HostCacheDTO implements LongCacheIdModel, Serializable {
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "主机类型")
|
||||
private String type;
|
||||
private String types;
|
||||
|
||||
@Schema(description = "系统类型")
|
||||
private String osType;
|
||||
|
||||
@Schema(description = "系统架构")
|
||||
private String archType;
|
||||
|
||||
@Schema(description = "主机名称")
|
||||
private String name;
|
||||
|
||||
@@ -69,7 +72,7 @@ public class HostCacheDTO implements LongCacheIdModel, Serializable {
|
||||
@Schema(description = "主机状态")
|
||||
private String status;
|
||||
|
||||
@Schema(description = "描述")
|
||||
@Schema(description = "主机描述")
|
||||
private String description;
|
||||
|
||||
}
|
||||
|
||||
@@ -69,6 +69,9 @@ public class TerminalConnectDTO {
|
||||
@Schema(description = "系统类型")
|
||||
private String osType;
|
||||
|
||||
@Schema(description = "系统架构")
|
||||
private String archType;
|
||||
|
||||
@Schema(description = "超时时间")
|
||||
private Integer timeout;
|
||||
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (c) 2023 - present Dromara, All rights reserved.
|
||||
*
|
||||
* https://visor.dromara.org
|
||||
* https://visor.dromara.org.cn
|
||||
* https://visor.orionsec.cn
|
||||
*
|
||||
* Members:
|
||||
* Jiahang Li - ljh1553488six@139.com - author
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.dromara.visor.module.asset.entity.request.host;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.*;
|
||||
import org.dromara.visor.common.entity.BaseQueryRequest;
|
||||
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
/**
|
||||
* 主机配置 查询请求对象
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2025-3-6 10:59
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Schema(name = "HostConfigQueryRequest", description = "主机配置 查询请求对象")
|
||||
public class HostConfigQueryRequest extends BaseQueryRequest {
|
||||
|
||||
@Schema(description = "主机id")
|
||||
private Long hostId;
|
||||
|
||||
@Size(max = 12)
|
||||
@Schema(description = "配置类型")
|
||||
private String type;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright (c) 2023 - present Dromara, All rights reserved.
|
||||
*
|
||||
* https://visor.dromara.org
|
||||
* https://visor.dromara.org.cn
|
||||
* https://visor.orionsec.cn
|
||||
*
|
||||
* Members:
|
||||
* Jiahang Li - ljh1553488six@139.com - author
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.dromara.visor.module.asset.entity.request.host;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 主机配置 更新请求对象
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2025-3-6 10:59
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Schema(name = "HostConfigUpdateRequest", description = "主机配置 更新请求对象")
|
||||
public class HostConfigUpdateRequest implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@NotNull
|
||||
@Schema(description = "主机id")
|
||||
private Long hostId;
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 12)
|
||||
@Schema(description = "配置类型")
|
||||
private String type;
|
||||
|
||||
@NotBlank
|
||||
@Schema(description = "配置值")
|
||||
private String config;
|
||||
|
||||
}
|
||||
@@ -28,7 +28,9 @@ import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.Size;
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
@@ -46,15 +48,20 @@ import java.util.List;
|
||||
@Schema(name = "HostCreateRequest", description = "主机 创建请求对象")
|
||||
public class HostCreateRequest implements Serializable {
|
||||
|
||||
@NotBlank
|
||||
@NotEmpty
|
||||
@Schema(description = "主机类型")
|
||||
private String type;
|
||||
private List<String> types;
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 12)
|
||||
@NotBlank
|
||||
@Schema(description = "系统类型")
|
||||
private String osType;
|
||||
|
||||
@Size(max = 12)
|
||||
@NotBlank
|
||||
@Schema(description = "系统架构")
|
||||
private String archType;
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 64)
|
||||
@Schema(description = "主机名称")
|
||||
@@ -70,11 +77,6 @@ public class HostCreateRequest implements Serializable {
|
||||
@Schema(description = "主机地址")
|
||||
private String address;
|
||||
|
||||
@Min(value = 1)
|
||||
@Max(value = 65535)
|
||||
@Schema(description = "主机端口")
|
||||
private Integer port;
|
||||
|
||||
@NotEmpty
|
||||
@Schema(description = "主机分组")
|
||||
private List<Long> groupIdList;
|
||||
|
||||
@@ -50,6 +50,10 @@ public class HostQueryRequest extends BaseQueryRequest {
|
||||
@Schema(description = "id")
|
||||
private Long id;
|
||||
|
||||
@Size(max = 8)
|
||||
@Schema(description = "主机类型")
|
||||
private String type;
|
||||
|
||||
@Size(max = 64)
|
||||
@Schema(description = "主机名称")
|
||||
private String name;
|
||||
@@ -62,14 +66,14 @@ public class HostQueryRequest extends BaseQueryRequest {
|
||||
@Schema(description = "主机地址")
|
||||
private String address;
|
||||
|
||||
@Size(max = 8)
|
||||
@Schema(description = "主机类型")
|
||||
private String type;
|
||||
|
||||
@Size(max = 12)
|
||||
@Schema(description = "系统类型")
|
||||
private String osType;
|
||||
|
||||
@Size(max = 12)
|
||||
@Schema(description = "系统架构")
|
||||
private String archType;
|
||||
|
||||
@Size(max = 8)
|
||||
@Schema(description = "主机状态")
|
||||
private String status;
|
||||
@@ -81,7 +85,13 @@ public class HostQueryRequest extends BaseQueryRequest {
|
||||
@Schema(description = "描述")
|
||||
private String description;
|
||||
|
||||
@Schema(description = "是否查询分组信息")
|
||||
private Boolean queryGroup;
|
||||
|
||||
@Schema(description = "是否查询 tag 信息")
|
||||
private Boolean queryTag;
|
||||
|
||||
@Schema(description = "是否查询规格信息")
|
||||
private Boolean querySpec;
|
||||
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.dromara.visor.module.asset.entity.vo;
|
||||
package org.dromara.visor.module.asset.entity.request.host;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
@@ -28,29 +28,29 @@ import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Map;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* 主机配置 视图响应对象
|
||||
* 主机 测试连接请求对象
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023/9/11 17:58
|
||||
* @since 2023-9-11 14:16
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Schema(name = "HostConfigVO", description = "主机配置 视图响应对象")
|
||||
public class HostConfigVO {
|
||||
@Schema(name = "HostTestConnectRequest", description = "主机 测试连接请求对象")
|
||||
public class HostTestConnectRequest {
|
||||
|
||||
@NotNull
|
||||
@Schema(description = "id")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "type")
|
||||
@NotBlank
|
||||
@Schema(description = "主机类型")
|
||||
private String type;
|
||||
|
||||
@Schema(description = "config")
|
||||
private Map<String, Object> config;
|
||||
|
||||
}
|
||||
@@ -28,7 +28,10 @@ import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
@@ -55,6 +58,11 @@ public class HostUpdateRequest implements Serializable {
|
||||
@Schema(description = "系统类型")
|
||||
private String osType;
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 12)
|
||||
@Schema(description = "系统架构")
|
||||
private String archType;
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 64)
|
||||
@Schema(description = "主机名称")
|
||||
@@ -70,11 +78,9 @@ public class HostUpdateRequest implements Serializable {
|
||||
@Schema(description = "主机地址")
|
||||
private String address;
|
||||
|
||||
@NotNull
|
||||
@Min(value = 1)
|
||||
@Max(value = 65535)
|
||||
@Schema(description = "主机端口")
|
||||
private Integer port;
|
||||
@NotEmpty
|
||||
@Schema(description = "主机类型")
|
||||
private List<String> types;
|
||||
|
||||
@NotEmpty
|
||||
@Schema(description = "主机分组")
|
||||
|
||||
@@ -29,6 +29,7 @@ import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 主机基本信息 视图响应对象
|
||||
@@ -50,7 +51,7 @@ public class HostBaseVO implements Serializable {
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "主机类型")
|
||||
private String type;
|
||||
private List<String> types;
|
||||
|
||||
@Schema(description = "主机名称")
|
||||
private String name;
|
||||
|
||||
@@ -27,6 +27,7 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.dromara.visor.module.asset.handler.host.extra.model.HostSpecExtraModel;
|
||||
import org.dromara.visor.module.infra.entity.dto.tag.TagDTO;
|
||||
|
||||
import java.io.Serializable;
|
||||
@@ -54,11 +55,14 @@ public class HostVO implements Serializable {
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "主机类型")
|
||||
private String type;
|
||||
private List<String> types;
|
||||
|
||||
@Schema(description = "系统类型")
|
||||
private String osType;
|
||||
|
||||
@Schema(description = "系统架构")
|
||||
private String archType;
|
||||
|
||||
@Schema(description = "主机名称")
|
||||
private String name;
|
||||
|
||||
@@ -104,4 +108,7 @@ public class HostVO implements Serializable {
|
||||
@Schema(description = "颜色")
|
||||
private String color;
|
||||
|
||||
@Schema(description = "规格")
|
||||
private HostSpecExtraModel spec;
|
||||
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ import org.dromara.visor.common.handler.data.GenericsStrategyDefinition;
|
||||
import org.dromara.visor.common.handler.data.model.GenericsDataModel;
|
||||
import org.dromara.visor.common.handler.data.strategy.GenericsDataStrategy;
|
||||
import org.dromara.visor.module.asset.handler.host.extra.strategy.HostLabelExtraStrategy;
|
||||
import org.dromara.visor.module.asset.handler.host.extra.strategy.HostSpecExtraStrategy;
|
||||
import org.dromara.visor.module.asset.handler.host.extra.strategy.HostSshExtraStrategy;
|
||||
|
||||
/**
|
||||
@@ -44,17 +45,24 @@ public enum HostExtraItemEnum implements GenericsStrategyDefinition {
|
||||
/**
|
||||
* SSH 额外配置
|
||||
*/
|
||||
SSH(HostSshExtraStrategy.class),
|
||||
SSH(HostSshExtraStrategy.class, true),
|
||||
|
||||
/**
|
||||
* 标签额外配置
|
||||
*/
|
||||
LABEL(HostLabelExtraStrategy.class),
|
||||
LABEL(HostLabelExtraStrategy.class, true),
|
||||
|
||||
/**
|
||||
* 规格信息配置
|
||||
*/
|
||||
SPEC(HostSpecExtraStrategy.class, false),
|
||||
|
||||
;
|
||||
|
||||
private final Class<? extends GenericsDataStrategy<? extends GenericsDataModel>> strategyClass;
|
||||
|
||||
private final boolean userExtra;
|
||||
|
||||
public static HostExtraItemEnum of(String item) {
|
||||
if (item == null) {
|
||||
return null;
|
||||
|
||||
@@ -44,6 +44,14 @@ import javax.validation.constraints.*;
|
||||
@AllArgsConstructor
|
||||
public class HostSshConfigModel implements GenericsDataModel, UpdatePasswordAction {
|
||||
|
||||
/**
|
||||
* 主机端口
|
||||
*/
|
||||
@NotNull
|
||||
@Min(value = 1)
|
||||
@Max(value = 65535)
|
||||
private Integer port;
|
||||
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
|
||||
@@ -64,6 +64,7 @@ public class HostSshConfigStrategy extends AbstractGenericsDataStrategy<HostSshC
|
||||
@Override
|
||||
public HostSshConfigModel getDefault() {
|
||||
return HostSshConfigModel.builder()
|
||||
.port(22)
|
||||
.username(USERNAME)
|
||||
.authType(HostSshAuthTypeEnum.PASSWORD.name())
|
||||
.connectTimeout(Const.MS_S_10)
|
||||
|
||||
@@ -55,7 +55,7 @@ import org.dromara.visor.module.asset.enums.ExecHostStatusEnum;
|
||||
import org.dromara.visor.module.asset.enums.HostOsTypeEnum;
|
||||
import org.dromara.visor.module.asset.handler.host.exec.log.manager.ExecLogManager;
|
||||
import org.dromara.visor.module.asset.handler.host.jsch.SessionStores;
|
||||
import org.dromara.visor.module.asset.service.TerminalService;
|
||||
import org.dromara.visor.module.asset.service.HostConnectService;
|
||||
import org.dromara.visor.module.asset.utils.ExecUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -79,7 +79,7 @@ public abstract class BaseExecCommandHandler implements IExecCommandHandler {
|
||||
|
||||
private static final ExecLogManager execLogManager = SpringHolder.getBean(ExecLogManager.class);
|
||||
|
||||
private static final TerminalService terminalService = SpringHolder.getBean(TerminalService.class);
|
||||
private static final HostConnectService hostConnectService = SpringHolder.getBean(HostConnectService.class);
|
||||
|
||||
private static final ExecHostLogDAO execHostLogDAO = SpringHolder.getBean(ExecHostLogDAO.class);
|
||||
|
||||
@@ -166,7 +166,7 @@ public abstract class BaseExecCommandHandler implements IExecCommandHandler {
|
||||
this.status = ExecHostStatusEnum.of(execHostLog.getStatus());
|
||||
Valid.eq(this.status, ExecHostStatusEnum.WAITING, ErrorMessage.TASK_ABSENT, ErrorMessage.ILLEGAL_STATUS);
|
||||
// 获取主机会话
|
||||
this.connect = terminalService.getTerminalConnectInfo(execHostLog.getHostId(), execLog.getUserId());
|
||||
this.connect = hostConnectService.getSshConnectInfo(execHostLog.getHostId(), execLog.getUserId());
|
||||
// 设置日志路径
|
||||
this.setLogPath();
|
||||
// 设置脚本路径
|
||||
@@ -411,6 +411,7 @@ public abstract class BaseExecCommandHandler implements IExecCommandHandler {
|
||||
params.put("hostUuid", uuid);
|
||||
params.put("hostUuidShort", uuid.replace("-", Strings.EMPTY));
|
||||
params.put("osType", connect.getOsType());
|
||||
params.put("archType", connect.getArchType());
|
||||
params.put("charset", connect.getCharset());
|
||||
params.put("scriptPath", execHostLog.getScriptPath());
|
||||
// 获取实际命令
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* Copyright (c) 2023 - present Dromara, All rights reserved.
|
||||
*
|
||||
* https://visor.dromara.org
|
||||
* https://visor.dromara.org.cn
|
||||
* https://visor.orionsec.cn
|
||||
*
|
||||
* Members:
|
||||
* Jiahang Li - ljh1553488six@139.com - author
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.dromara.visor.module.asset.handler.host.extra.model;
|
||||
|
||||
import lombok.*;
|
||||
import org.dromara.visor.common.handler.data.model.GenericsDataModel;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 主机拓展信息 - 规格模型
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2025/3/24 0:34
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class HostSpecExtraModel implements GenericsDataModel {
|
||||
|
||||
/**
|
||||
* 系统名称
|
||||
*/
|
||||
private String osName;
|
||||
|
||||
/**
|
||||
* cpu 核心数
|
||||
*/
|
||||
private Integer cpuCore;
|
||||
|
||||
/**
|
||||
* cpu 频率
|
||||
*/
|
||||
private Double cpuFrequency;
|
||||
|
||||
/**
|
||||
* cpu 型号
|
||||
*/
|
||||
private String cpuModel;
|
||||
|
||||
/**
|
||||
* 内存大小
|
||||
*/
|
||||
private Double memorySize;
|
||||
|
||||
/**
|
||||
* 硬盘大小
|
||||
*/
|
||||
private Double diskSize;
|
||||
|
||||
/**
|
||||
* 上行带宽
|
||||
*/
|
||||
private Integer inBandwidth;
|
||||
|
||||
/**
|
||||
* 下行带宽
|
||||
*/
|
||||
private Integer outBandwidth;
|
||||
|
||||
/**
|
||||
* 公网 ip 列表
|
||||
*/
|
||||
private List<String> publicIpAddress;
|
||||
|
||||
/**
|
||||
* 内网 ip 列表
|
||||
*/
|
||||
private List<String> privateIpAddress;
|
||||
|
||||
/**
|
||||
* 负责人
|
||||
*/
|
||||
private String chargePerson;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private Date createdTime;
|
||||
|
||||
/**
|
||||
* 到期时间
|
||||
*/
|
||||
private Date expiredTime;
|
||||
|
||||
/**
|
||||
* 扩展信息
|
||||
*/
|
||||
@Singular
|
||||
private List<HostSpecExtraItem> items;
|
||||
|
||||
/**
|
||||
* 扩展信息项
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class HostSpecExtraItem {
|
||||
|
||||
/**
|
||||
* 标签
|
||||
*/
|
||||
private String label;
|
||||
|
||||
/**
|
||||
* 值
|
||||
*/
|
||||
private String value;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (c) 2023 - present Dromara, All rights reserved.
|
||||
*
|
||||
* https://visor.dromara.org
|
||||
* https://visor.dromara.org.cn
|
||||
* https://visor.orionsec.cn
|
||||
*
|
||||
* Members:
|
||||
* Jiahang Li - ljh1553488six@139.com - author
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.dromara.visor.module.asset.handler.host.extra.strategy;
|
||||
|
||||
import org.dromara.visor.common.handler.data.strategy.AbstractGenericsDataStrategy;
|
||||
import org.dromara.visor.module.asset.handler.host.extra.model.HostSpecExtraModel;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 主机规格额外信息策略
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2025/3/24 0:21
|
||||
*/
|
||||
@Component
|
||||
public class HostSpecExtraStrategy extends AbstractGenericsDataStrategy<HostSpecExtraModel> {
|
||||
|
||||
public HostSpecExtraStrategy() {
|
||||
super(HostSpecExtraModel.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public HostSpecExtraModel getDefault() {
|
||||
return new HostSpecExtraModel();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -48,8 +48,8 @@ import org.dromara.visor.module.asset.handler.host.terminal.model.request.Termin
|
||||
import org.dromara.visor.module.asset.handler.host.terminal.model.response.TerminalCheckResponse;
|
||||
import org.dromara.visor.module.asset.handler.host.terminal.session.ITerminalSession;
|
||||
import org.dromara.visor.module.asset.handler.host.terminal.utils.TerminalUtils;
|
||||
import org.dromara.visor.module.asset.service.HostConnectService;
|
||||
import org.dromara.visor.module.asset.service.TerminalConnectLogService;
|
||||
import org.dromara.visor.module.asset.service.TerminalService;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
|
||||
@@ -71,7 +71,7 @@ public class TerminalCheckHandler extends AbstractTerminalHandler<TerminalCheckR
|
||||
private HostDAO hostDAO;
|
||||
|
||||
@Resource
|
||||
private TerminalService terminalService;
|
||||
private HostConnectService hostConnectService;
|
||||
|
||||
@Resource
|
||||
private TerminalConnectLogService terminalConnectLogService;
|
||||
@@ -102,7 +102,7 @@ public class TerminalCheckHandler extends AbstractTerminalHandler<TerminalCheckR
|
||||
Exception ex = null;
|
||||
try {
|
||||
// 获取连接信息
|
||||
connect = terminalService.getTerminalConnectInfo(host, userId);
|
||||
connect = hostConnectService.getSshConnectInfo(host, userId);
|
||||
connect.setConnectType(connectType.name());
|
||||
// 设置到缓存中
|
||||
channel.getAttributes().put(sessionId, connect);
|
||||
|
||||
@@ -40,7 +40,7 @@ import org.dromara.visor.module.asset.handler.host.transfer.session.DownloadSess
|
||||
import org.dromara.visor.module.asset.handler.host.transfer.session.ITransferSession;
|
||||
import org.dromara.visor.module.asset.handler.host.transfer.session.UploadSession;
|
||||
import org.dromara.visor.module.asset.handler.host.transfer.utils.TransferUtils;
|
||||
import org.dromara.visor.module.asset.service.TerminalService;
|
||||
import org.dromara.visor.module.asset.service.HostConnectService;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -55,7 +55,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
@Slf4j
|
||||
public class TransferHandler implements ITransferHandler {
|
||||
|
||||
private static final TerminalService terminalService = SpringHolder.getBean(TerminalService.class);
|
||||
private static final HostConnectService hostConnectService = SpringHolder.getBean(HostConnectService.class);
|
||||
|
||||
private final WebSocketSession channel;
|
||||
|
||||
@@ -117,7 +117,7 @@ public class TransferHandler implements ITransferHandler {
|
||||
if (terminalConnection == null) {
|
||||
// 获取终端连接信息
|
||||
Long userId = WebSockets.getAttr(channel, ExtraFieldConst.USER_ID);
|
||||
TerminalConnectDTO connectInfo = terminalService.getTerminalConnectInfo(hostId, userId);
|
||||
TerminalConnectDTO connectInfo = hostConnectService.getSshConnectInfo(hostId, userId);
|
||||
terminalConnection = new TerminalConnection(connectInfo, SessionStores.openSessionStore(connectInfo));
|
||||
terminalConnections.put(hostId, terminalConnection);
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ import org.dromara.visor.module.asset.enums.UploadTaskFileStatusEnum;
|
||||
import org.dromara.visor.module.asset.handler.host.jsch.SessionStores;
|
||||
import org.dromara.visor.module.asset.handler.host.upload.model.FileUploadConfigDTO;
|
||||
import org.dromara.visor.module.asset.handler.host.upload.model.FileUploadFileItemDTO;
|
||||
import org.dromara.visor.module.asset.service.TerminalService;
|
||||
import org.dromara.visor.module.asset.service.HostConnectService;
|
||||
import org.dromara.visor.module.asset.utils.SftpUtils;
|
||||
|
||||
import java.io.InputStream;
|
||||
@@ -65,7 +65,7 @@ import java.util.stream.Collectors;
|
||||
@Slf4j
|
||||
public class FileUploader implements IFileUploader {
|
||||
|
||||
private static final TerminalService terminalService = SpringHolder.getBean(TerminalService.class);
|
||||
private static final HostConnectService hostConnectService = SpringHolder.getBean(HostConnectService.class);
|
||||
|
||||
private static final UploadTaskFileDAO uploadTaskFileDAO = SpringHolder.getBean(UploadTaskFileDAO.class);
|
||||
|
||||
@@ -136,7 +136,7 @@ public class FileUploader implements IFileUploader {
|
||||
log.info("HostFileUploader.initSession start taskId: {}, hostId: {}", taskId, hostId);
|
||||
try {
|
||||
// 打开会话
|
||||
this.connectInfo = terminalService.getTerminalConnectInfo(hostId, config.getUserId());
|
||||
this.connectInfo = hostConnectService.getSshConnectInfo(hostId, config.getUserId());
|
||||
this.sessionStore = SessionStores.openSessionStore(connectInfo);
|
||||
this.executor = sessionStore.getSftpExecutor(connectInfo.getFileNameCharset());
|
||||
executor.connect();
|
||||
|
||||
@@ -26,7 +26,6 @@ import org.dromara.visor.module.asset.entity.request.asset.AssetAuthorizedDataQu
|
||||
import org.dromara.visor.module.asset.entity.vo.AuthorizedHostWrapperVO;
|
||||
import org.dromara.visor.module.asset.entity.vo.HostIdentityVO;
|
||||
import org.dromara.visor.module.asset.entity.vo.HostKeyVO;
|
||||
import org.dromara.visor.module.asset.enums.HostTypeEnum;
|
||||
import org.dromara.visor.module.infra.enums.DataPermissionTypeEnum;
|
||||
|
||||
import java.util.List;
|
||||
@@ -64,10 +63,10 @@ public interface AssetAuthorizedDataService {
|
||||
* @param type type
|
||||
* @return hostId
|
||||
*/
|
||||
List<Long> getUserAuthorizedEnabledHostId(Long userId, HostTypeEnum type);
|
||||
List<Long> getUserAuthorizedEnabledHostId(Long userId, String type);
|
||||
|
||||
/**
|
||||
* 查询用户已授权的主机
|
||||
* 查询用户已授权并且启用的主机
|
||||
*
|
||||
* @param userId userId
|
||||
* @param type type
|
||||
|
||||
@@ -23,7 +23,8 @@
|
||||
package org.dromara.visor.module.asset.service;
|
||||
|
||||
import org.dromara.visor.common.handler.data.model.GenericsDataModel;
|
||||
import org.dromara.visor.module.asset.entity.domain.HostDO;
|
||||
import org.dromara.visor.module.asset.entity.request.host.HostConfigQueryRequest;
|
||||
import org.dromara.visor.module.asset.entity.request.host.HostConfigUpdateRequest;
|
||||
|
||||
/**
|
||||
* 主机配置 服务类
|
||||
@@ -35,21 +36,29 @@ import org.dromara.visor.module.asset.entity.domain.HostDO;
|
||||
public interface HostConfigService {
|
||||
|
||||
/**
|
||||
* 获取主机配置
|
||||
* 更新主机配置
|
||||
*
|
||||
* @param id id
|
||||
* @param <T> T
|
||||
* @return host
|
||||
* @param request request
|
||||
* @return effect
|
||||
*/
|
||||
<T extends GenericsDataModel> T getHostConfig(Long id);
|
||||
Integer updateHostConfig(HostConfigUpdateRequest request);
|
||||
|
||||
/**
|
||||
* 获取主机配置
|
||||
*
|
||||
* @param host host
|
||||
* @param <T> T
|
||||
* @return host
|
||||
* @param hostId hostId
|
||||
* @param type type
|
||||
* @param <T> T
|
||||
* @return config
|
||||
*/
|
||||
<T extends GenericsDataModel> T getHostConfig(HostDO host);
|
||||
<T extends GenericsDataModel> T getHostConfig(Long hostId, String type);
|
||||
|
||||
/**
|
||||
* 查询主机配置
|
||||
*
|
||||
* @param request request
|
||||
* @return config
|
||||
*/
|
||||
<T extends GenericsDataModel> T getHostConfigView(HostConfigQueryRequest request);
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (c) 2023 - present Dromara, All rights reserved.
|
||||
*
|
||||
* https://visor.dromara.org
|
||||
* https://visor.dromara.org.cn
|
||||
* https://visor.orionsec.cn
|
||||
*
|
||||
* Members:
|
||||
* Jiahang Li - ljh1553488six@139.com - author
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.dromara.visor.module.asset.service;
|
||||
|
||||
import org.dromara.visor.module.asset.entity.domain.HostDO;
|
||||
import org.dromara.visor.module.asset.entity.dto.TerminalConnectDTO;
|
||||
import org.dromara.visor.module.asset.entity.request.host.HostTestConnectRequest;
|
||||
|
||||
/**
|
||||
* 主机连接服务
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2024/10/12 23:54
|
||||
*/
|
||||
public interface HostConnectService {
|
||||
|
||||
/**
|
||||
* 测试主机连接
|
||||
*
|
||||
* @param request request
|
||||
*/
|
||||
void testHostConnect(HostTestConnectRequest request);
|
||||
|
||||
/**
|
||||
* 获取 SSH 连接信息
|
||||
*
|
||||
* @param hostId hostId
|
||||
* @return session
|
||||
*/
|
||||
TerminalConnectDTO getSshConnectInfo(Long hostId);
|
||||
|
||||
/**
|
||||
* 使用用户配置获取 SSH 连接信息
|
||||
*
|
||||
* @param hostId hostId
|
||||
* @param userId userId
|
||||
* @return session
|
||||
*/
|
||||
TerminalConnectDTO getSshConnectInfo(Long hostId, Long userId);
|
||||
|
||||
/**
|
||||
* 使用用户配置获取 SSH 连接信息
|
||||
*
|
||||
* @param host host
|
||||
* @param userId userId
|
||||
* @return session
|
||||
*/
|
||||
TerminalConnectDTO getSshConnectInfo(HostDO host, Long userId);
|
||||
|
||||
}
|
||||
@@ -23,10 +23,11 @@
|
||||
package org.dromara.visor.module.asset.service;
|
||||
|
||||
import org.dromara.visor.common.handler.data.model.GenericsDataModel;
|
||||
import org.dromara.visor.module.asset.entity.request.host.HostExtraQueryRequest;
|
||||
import org.dromara.visor.module.asset.entity.request.host.HostExtraUpdateRequest;
|
||||
import org.dromara.visor.module.asset.enums.HostExtraItemEnum;
|
||||
import org.dromara.visor.module.asset.handler.host.extra.model.HostSpecExtraModel;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -45,7 +46,7 @@ public interface HostExtraService {
|
||||
* @param item item
|
||||
* @return extra
|
||||
*/
|
||||
Map<String, Object> getHostExtra(Long hostId, String item);
|
||||
Map<String, Object> getHostExtraView(Long hostId, String item);
|
||||
|
||||
/**
|
||||
* 获取主机额外配置
|
||||
@@ -59,12 +60,12 @@ public interface HostExtraService {
|
||||
<T extends GenericsDataModel> T getHostExtra(Long userId, Long hostId, HostExtraItemEnum item);
|
||||
|
||||
/**
|
||||
* 获取多个主机拓展信息
|
||||
* 获取主机规格信息
|
||||
*
|
||||
* @param request request
|
||||
* @return type:extra
|
||||
* @param hostIdList hostIdList
|
||||
* @return models
|
||||
*/
|
||||
Map<String, Map<String, Object>> getHostExtraList(HostExtraQueryRequest request);
|
||||
Map<Long, HostSpecExtraModel> getHostSpecMap(List<Long> hostIdList);
|
||||
|
||||
/**
|
||||
* 修改主机拓展信息
|
||||
|
||||
@@ -25,8 +25,10 @@ package org.dromara.visor.module.asset.service;
|
||||
import cn.orionsec.kit.lang.define.wrapper.DataGrid;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import org.dromara.visor.module.asset.entity.domain.HostDO;
|
||||
import org.dromara.visor.module.asset.entity.request.host.*;
|
||||
import org.dromara.visor.module.asset.entity.vo.HostConfigVO;
|
||||
import org.dromara.visor.module.asset.entity.request.host.HostCreateRequest;
|
||||
import org.dromara.visor.module.asset.entity.request.host.HostQueryRequest;
|
||||
import org.dromara.visor.module.asset.entity.request.host.HostUpdateRequest;
|
||||
import org.dromara.visor.module.asset.entity.request.host.HostUpdateStatusRequest;
|
||||
import org.dromara.visor.module.asset.entity.vo.HostVO;
|
||||
|
||||
import java.util.List;
|
||||
@@ -64,14 +66,6 @@ public interface HostService {
|
||||
*/
|
||||
Integer updateHostStatus(HostUpdateStatusRequest request);
|
||||
|
||||
/**
|
||||
* 更新主机配置
|
||||
*
|
||||
* @param request request
|
||||
* @return effect
|
||||
*/
|
||||
Integer updateHostConfig(HostUpdateConfigRequest request);
|
||||
|
||||
/**
|
||||
* 通过 id 查询主机
|
||||
*
|
||||
@@ -80,14 +74,6 @@ public interface HostService {
|
||||
*/
|
||||
HostVO getHostById(Long id);
|
||||
|
||||
/**
|
||||
* 查询主机配置
|
||||
*
|
||||
* @param id id
|
||||
* @return config
|
||||
*/
|
||||
HostConfigVO getHostConfig(Long id);
|
||||
|
||||
/**
|
||||
* 查询主机
|
||||
*
|
||||
|
||||
@@ -22,9 +22,7 @@
|
||||
*/
|
||||
package org.dromara.visor.module.asset.service;
|
||||
|
||||
import org.dromara.visor.module.asset.entity.domain.HostDO;
|
||||
import org.dromara.visor.module.asset.entity.dto.TerminalAccessDTO;
|
||||
import org.dromara.visor.module.asset.entity.dto.TerminalConnectDTO;
|
||||
import org.dromara.visor.module.asset.entity.dto.TerminalTransferDTO;
|
||||
import org.dromara.visor.module.asset.entity.vo.TerminalThemeVO;
|
||||
|
||||
@@ -76,30 +74,4 @@ public interface TerminalService {
|
||||
*/
|
||||
TerminalTransferDTO getTransferInfoByToken(String token);
|
||||
|
||||
/**
|
||||
* 获取连接信息
|
||||
*
|
||||
* @param hostId hostId
|
||||
* @return session
|
||||
*/
|
||||
TerminalConnectDTO getTerminalConnectInfo(Long hostId);
|
||||
|
||||
/**
|
||||
* 使用用户配置获取连接信息
|
||||
*
|
||||
* @param hostId hostId
|
||||
* @param userId userId
|
||||
* @return session
|
||||
*/
|
||||
TerminalConnectDTO getTerminalConnectInfo(Long hostId, Long userId);
|
||||
|
||||
/**
|
||||
* 使用用户配置获取连接信息
|
||||
*
|
||||
* @param host host
|
||||
* @param userId userId
|
||||
* @return session
|
||||
*/
|
||||
TerminalConnectDTO getTerminalConnectInfo(HostDO host, Long userId);
|
||||
|
||||
}
|
||||
|
||||
@@ -36,7 +36,6 @@ import org.dromara.visor.module.asset.entity.request.asset.AssetAuthorizedDataQu
|
||||
import org.dromara.visor.module.asset.entity.vo.*;
|
||||
import org.dromara.visor.module.asset.enums.HostExtraItemEnum;
|
||||
import org.dromara.visor.module.asset.enums.HostStatusEnum;
|
||||
import org.dromara.visor.module.asset.enums.HostTypeEnum;
|
||||
import org.dromara.visor.module.asset.handler.host.extra.model.HostLabelExtraModel;
|
||||
import org.dromara.visor.module.asset.service.AssetAuthorizedDataService;
|
||||
import org.dromara.visor.module.asset.service.HostIdentityService;
|
||||
@@ -128,14 +127,14 @@ public class AssetAuthorizedDataServiceImpl implements AssetAuthorizedDataServic
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Long> getUserAuthorizedEnabledHostId(Long userId, HostTypeEnum type) {
|
||||
public List<Long> getUserAuthorizedEnabledHostId(Long userId, String type) {
|
||||
// 获取有权限的的主机
|
||||
List<Long> hostIdList = this.getUserAuthorizedHostId(userId);
|
||||
if (hostIdList.isEmpty()) {
|
||||
return hostIdList;
|
||||
}
|
||||
// 获取启用的主机
|
||||
return hostDAO.getHostIdList(hostIdList, type.name(), HostStatusEnum.ENABLED.name());
|
||||
return hostDAO.getHostIdList(hostIdList, type, HostStatusEnum.ENABLED.name());
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
|
||||
@@ -90,7 +90,7 @@ public class ExecCommandServiceImpl implements ExecCommandService {
|
||||
Long userId = user.getId();
|
||||
List<Long> hostIdList = request.getHostIdList();
|
||||
// 检查主机权限
|
||||
List<Long> authorizedHostIdList = assetAuthorizedDataService.getUserAuthorizedEnabledHostId(userId, HostTypeEnum.SSH);
|
||||
List<Long> authorizedHostIdList = assetAuthorizedDataService.getUserAuthorizedEnabledHostId(userId, HostTypeEnum.SSH.name());
|
||||
hostIdList.removeIf(s -> !authorizedHostIdList.contains(s));
|
||||
log.info("ExecService.startExecCommand host hostList: {}", hostIdList);
|
||||
Valid.notEmpty(hostIdList, ErrorMessage.CHECK_AUTHORIZED_HOST);
|
||||
|
||||
@@ -221,7 +221,7 @@ public class ExecJobServiceImpl implements ExecJobService {
|
||||
vo.setHostIdList(hostIdList);
|
||||
// 查询主机列表
|
||||
if (!Lists.isEmpty(hostIdList)) {
|
||||
List<HostBaseVO> hosts = hostDAO.selectBaseByIdList(hostIdList)
|
||||
List<HostBaseVO> hosts = hostDAO.selectBatchIds(hostIdList)
|
||||
.stream()
|
||||
.map(HostConvert.MAPPER::toBase)
|
||||
.collect(Collectors.toList());
|
||||
@@ -440,7 +440,7 @@ public class ExecJobServiceImpl implements ExecJobService {
|
||||
*/
|
||||
private void checkHostPermission(List<Long> hostIdList) {
|
||||
// 查询有权限的主机
|
||||
List<Long> authorizedHostIdList = assetAuthorizedDataService.getUserAuthorizedEnabledHostId(SecurityUtils.getLoginUserId(), HostTypeEnum.SSH);
|
||||
List<Long> authorizedHostIdList = assetAuthorizedDataService.getUserAuthorizedEnabledHostId(SecurityUtils.getLoginUserId(), HostTypeEnum.SSH.name());
|
||||
for (Long hostId : hostIdList) {
|
||||
Valid.isTrue(authorizedHostIdList.contains(hostId), Strings.format(ErrorMessage.PLEASE_CHECK_HOST_SSH, hostId));
|
||||
}
|
||||
|
||||
@@ -135,7 +135,7 @@ public class ExecTemplateServiceImpl implements ExecTemplateService {
|
||||
return template;
|
||||
}
|
||||
// 过滤认证的主机
|
||||
List<Long> authorizedHostIdList = assetAuthorizedDataService.getUserAuthorizedEnabledHostId(SecurityUtils.getLoginUserId(), HostTypeEnum.SSH);
|
||||
List<Long> authorizedHostIdList = assetAuthorizedDataService.getUserAuthorizedEnabledHostId(SecurityUtils.getLoginUserId(), HostTypeEnum.SSH.name());
|
||||
hostIdList.removeIf(s -> !authorizedHostIdList.contains(s));
|
||||
template.setHostIdList(hostIdList);
|
||||
return template;
|
||||
|
||||
@@ -22,12 +22,20 @@
|
||||
*/
|
||||
package org.dromara.visor.module.asset.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.visor.common.constant.ErrorMessage;
|
||||
import org.dromara.visor.common.constant.ExtraFieldConst;
|
||||
import org.dromara.visor.common.enums.EnableStatus;
|
||||
import org.dromara.visor.common.handler.data.model.GenericsDataModel;
|
||||
import org.dromara.visor.common.utils.Valid;
|
||||
import org.dromara.visor.framework.biz.operator.log.core.utils.OperatorLogs;
|
||||
import org.dromara.visor.module.asset.dao.HostConfigDAO;
|
||||
import org.dromara.visor.module.asset.dao.HostDAO;
|
||||
import org.dromara.visor.module.asset.entity.domain.HostConfigDO;
|
||||
import org.dromara.visor.module.asset.entity.domain.HostDO;
|
||||
import org.dromara.visor.module.asset.entity.request.host.HostConfigQueryRequest;
|
||||
import org.dromara.visor.module.asset.entity.request.host.HostConfigUpdateRequest;
|
||||
import org.dromara.visor.module.asset.enums.HostStatusEnum;
|
||||
import org.dromara.visor.module.asset.enums.HostTypeEnum;
|
||||
import org.dromara.visor.module.asset.service.HostConfigService;
|
||||
@@ -49,24 +57,76 @@ public class HostConfigServiceImpl implements HostConfigService {
|
||||
@Resource
|
||||
private HostDAO hostDAO;
|
||||
|
||||
@Resource
|
||||
private HostConfigDAO hostConfigDAO;
|
||||
|
||||
@Override
|
||||
public <T extends GenericsDataModel> T getHostConfig(Long id) {
|
||||
public Integer updateHostConfig(HostConfigUpdateRequest request) {
|
||||
log.info("HostConfigService-updateHostConfig request: {}", request);
|
||||
Long hostId = request.getHostId();
|
||||
String type = request.getType();
|
||||
String param = OperatorLogs.toJsonString(JSON.parseObject(request.getConfig()));
|
||||
OperatorLogs.add(ExtraFieldConst.CONFIG, param);
|
||||
// 查询主机
|
||||
HostDO host = hostDAO.selectById(id);
|
||||
// 转换为配置
|
||||
return this.getHostConfig(host);
|
||||
HostDO host = hostDAO.selectById(hostId);
|
||||
Valid.notNull(host, ErrorMessage.HOST_ABSENT);
|
||||
OperatorLogs.add(OperatorLogs.NAME, host.getName());
|
||||
// 获取处理策略
|
||||
HostTypeEnum strategy = HostTypeEnum.of(type);
|
||||
GenericsDataModel newConfig = strategy.parse(request.getConfig());
|
||||
// 查询配置
|
||||
HostConfigDO record = hostConfigDAO.selectByHostIdType(hostId, type);
|
||||
if (record == null) {
|
||||
// 新增验证
|
||||
strategy.doValid(null, newConfig);
|
||||
// 新增
|
||||
HostConfigDO entity = HostConfigDO.builder()
|
||||
.hostId(hostId)
|
||||
.type(type)
|
||||
.status(EnableStatus.ENABLED.name())
|
||||
.config(newConfig.serial())
|
||||
.build();
|
||||
return hostConfigDAO.insert(entity);
|
||||
} else {
|
||||
// 修改验证
|
||||
GenericsDataModel beforeConfig = strategy.parse(record.getConfig());
|
||||
strategy.doValid(beforeConfig, newConfig);
|
||||
// 修改
|
||||
HostConfigDO entity = HostConfigDO.builder()
|
||||
.id(record.getId())
|
||||
.hostId(hostId)
|
||||
.type(type)
|
||||
.config(newConfig.serial())
|
||||
.build();
|
||||
return hostConfigDAO.updateById(entity);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends GenericsDataModel> T getHostConfig(HostDO host) {
|
||||
Valid.notNull(host, ErrorMessage.HOST_ABSENT);
|
||||
HostTypeEnum type = HostTypeEnum.of(host.getType());
|
||||
// 检查主机状态
|
||||
Valid.isTrue(HostStatusEnum.ENABLED.name().equals(host.getStatus()), ErrorMessage.HOST_NOT_ENABLED);
|
||||
// 查询主机配置
|
||||
T config = type.parse(host.getConfig());
|
||||
public <T extends GenericsDataModel> T getHostConfig(Long hostId, String type) {
|
||||
// 查询配置信息
|
||||
HostConfigDO config = hostConfigDAO.selectByHostIdType(hostId, type);
|
||||
Valid.notNull(config, ErrorMessage.CONFIG_ABSENT);
|
||||
return (T) config;
|
||||
// 检查配置状态
|
||||
Valid.isTrue(HostStatusEnum.ENABLED.name().equals(config.getStatus()), ErrorMessage.CONFIG_NOT_ENABLED);
|
||||
// 解析配置
|
||||
T model = HostTypeEnum.of(type).parse(config.getConfig());
|
||||
Valid.notNull(model, ErrorMessage.CONFIG_ABSENT);
|
||||
return model;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends GenericsDataModel> T getHostConfigView(HostConfigQueryRequest request) {
|
||||
String type = request.getType();
|
||||
HostTypeEnum strategy = HostTypeEnum.of(type);
|
||||
// 查询配置
|
||||
HostConfigDO record = hostConfigDAO.selectByHostIdType(request.getHostId(), type);
|
||||
if (record == null) {
|
||||
// 获取默认值
|
||||
return strategy.toView(strategy.getDefault().serial());
|
||||
} else {
|
||||
return strategy.toView(record.getConfig());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,241 @@
|
||||
/*
|
||||
* Copyright (c) 2023 - present Dromara, All rights reserved.
|
||||
*
|
||||
* https://visor.dromara.org
|
||||
* https://visor.dromara.org.cn
|
||||
* https://visor.orionsec.cn
|
||||
*
|
||||
* Members:
|
||||
* Jiahang Li - ljh1553488six@139.com - author
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.dromara.visor.module.asset.service.impl;
|
||||
|
||||
import cn.orionsec.kit.lang.utils.Exceptions;
|
||||
import cn.orionsec.kit.lang.utils.io.Streams;
|
||||
import cn.orionsec.kit.net.host.SessionStore;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.visor.common.constant.ErrorMessage;
|
||||
import org.dromara.visor.common.utils.Valid;
|
||||
import org.dromara.visor.module.asset.dao.HostDAO;
|
||||
import org.dromara.visor.module.asset.dao.HostIdentityDAO;
|
||||
import org.dromara.visor.module.asset.dao.HostKeyDAO;
|
||||
import org.dromara.visor.module.asset.entity.domain.HostDO;
|
||||
import org.dromara.visor.module.asset.entity.domain.HostIdentityDO;
|
||||
import org.dromara.visor.module.asset.entity.domain.HostKeyDO;
|
||||
import org.dromara.visor.module.asset.entity.dto.TerminalConnectDTO;
|
||||
import org.dromara.visor.module.asset.entity.request.host.HostTestConnectRequest;
|
||||
import org.dromara.visor.module.asset.enums.*;
|
||||
import org.dromara.visor.module.asset.handler.host.config.model.HostSshConfigModel;
|
||||
import org.dromara.visor.module.asset.handler.host.extra.model.HostSshExtraModel;
|
||||
import org.dromara.visor.module.asset.handler.host.jsch.SessionStores;
|
||||
import org.dromara.visor.module.asset.service.AssetAuthorizedDataService;
|
||||
import org.dromara.visor.module.asset.service.HostConfigService;
|
||||
import org.dromara.visor.module.asset.service.HostConnectService;
|
||||
import org.dromara.visor.module.asset.service.HostExtraService;
|
||||
import org.dromara.visor.module.infra.api.DataPermissionApi;
|
||||
import org.dromara.visor.module.infra.enums.DataPermissionTypeEnum;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 主机连接服务实现
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2024/10/12 23:58
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class HostConnectServiceImpl implements HostConnectService {
|
||||
|
||||
@Resource
|
||||
private HostDAO hostDAO;
|
||||
|
||||
@Resource
|
||||
private HostIdentityDAO hostIdentityDAO;
|
||||
|
||||
@Resource
|
||||
private HostKeyDAO hostKeyDAO;
|
||||
|
||||
@Resource
|
||||
private HostConfigService hostConfigService;
|
||||
|
||||
@Resource
|
||||
private HostExtraService hostExtraService;
|
||||
|
||||
@Resource
|
||||
private AssetAuthorizedDataService assetAuthorizedDataService;
|
||||
|
||||
@Resource
|
||||
private DataPermissionApi dataPermissionApi;
|
||||
|
||||
@Override
|
||||
public void testHostConnect(HostTestConnectRequest request) {
|
||||
Long id = request.getId();
|
||||
HostTypeEnum type = HostTypeEnum.of(request.getType());
|
||||
if (HostTypeEnum.SSH.equals(type)) {
|
||||
// SSH 连接测试
|
||||
SessionStore sessionStore = null;
|
||||
try {
|
||||
TerminalConnectDTO info = this.getSshConnectInfo(id);
|
||||
sessionStore = SessionStores.openSessionStore(info);
|
||||
} catch (Exception e) {
|
||||
throw Exceptions.app(e.getMessage(), e);
|
||||
} finally {
|
||||
Streams.close(sessionStore);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TerminalConnectDTO getSshConnectInfo(Long hostId) {
|
||||
log.info("HostConnectService.getSshConnectInfo-withHost hostId: {}", hostId);
|
||||
// 查询主机
|
||||
HostDO host = hostDAO.selectById(hostId);
|
||||
// 查询主机配置
|
||||
HostSshConfigModel config = hostConfigService.getHostConfig(hostId, HostTypeEnum.SSH.name());
|
||||
// 获取配置
|
||||
return this.getHostConnectInfo(host, config, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TerminalConnectDTO getSshConnectInfo(Long hostId, Long userId) {
|
||||
// 查询主机
|
||||
HostDO host = hostDAO.selectById(hostId);
|
||||
Valid.notNull(host, ErrorMessage.HOST_ABSENT);
|
||||
// 获取配置
|
||||
return this.getSshConnectInfo(host, userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TerminalConnectDTO getSshConnectInfo(HostDO host, Long userId) {
|
||||
Long hostId = host.getId();
|
||||
log.info("HostConnectService.getSshConnectInfo hostId: {}, userId: {}", hostId, userId);
|
||||
// 验证主机是否有权限
|
||||
List<Long> hostIdList = assetAuthorizedDataService.getUserAuthorizedHostId(userId);
|
||||
Valid.isTrue(hostIdList.contains(hostId),
|
||||
ErrorMessage.ANY_NO_PERMISSION,
|
||||
DataPermissionTypeEnum.HOST_GROUP.getPermissionName());
|
||||
// 获取主机配置
|
||||
HostSshConfigModel config = hostConfigService.getHostConfig(hostId, HostTypeEnum.SSH.name());
|
||||
Valid.notNull(config, ErrorMessage.CONFIG_ABSENT);
|
||||
// 查询主机额外配置
|
||||
HostSshExtraModel extra = hostExtraService.getHostExtra(userId, hostId, HostExtraItemEnum.SSH);
|
||||
if (extra != null) {
|
||||
HostExtraSshAuthTypeEnum extraAuthType = HostExtraSshAuthTypeEnum.of(extra.getAuthType());
|
||||
if (HostExtraSshAuthTypeEnum.CUSTOM_KEY.equals(extraAuthType)) {
|
||||
// 验证主机密钥是否有权限
|
||||
Valid.notNull(extra.getKeyId(), ErrorMessage.KEY_ABSENT);
|
||||
Valid.isTrue(dataPermissionApi.hasPermission(DataPermissionTypeEnum.HOST_KEY, userId, extra.getKeyId()),
|
||||
ErrorMessage.ANY_NO_PERMISSION,
|
||||
DataPermissionTypeEnum.HOST_KEY.getPermissionName());
|
||||
} else if (HostExtraSshAuthTypeEnum.CUSTOM_IDENTITY.equals(extraAuthType)) {
|
||||
// 验证主机身份是否有权限
|
||||
Valid.notNull(extra.getIdentityId(), ErrorMessage.IDENTITY_ABSENT);
|
||||
Valid.isTrue(dataPermissionApi.hasPermission(DataPermissionTypeEnum.HOST_IDENTITY, userId, extra.getIdentityId()),
|
||||
ErrorMessage.ANY_NO_PERMISSION,
|
||||
DataPermissionTypeEnum.HOST_IDENTITY.getPermissionName());
|
||||
}
|
||||
}
|
||||
// 获取连接配置
|
||||
return this.getHostConnectInfo(host, config, extra);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取主机 SSH 连接配置
|
||||
*
|
||||
* @param host host
|
||||
* @param config config
|
||||
* @param extra extra
|
||||
* @return session
|
||||
*/
|
||||
private TerminalConnectDTO getHostConnectInfo(HostDO host,
|
||||
HostSshConfigModel config,
|
||||
HostSshExtraModel extra) {
|
||||
// 填充认证信息
|
||||
TerminalConnectDTO conn = new TerminalConnectDTO();
|
||||
conn.setOsType(host.getOsType());
|
||||
conn.setArchType(host.getArchType());
|
||||
conn.setHostId(host.getId());
|
||||
conn.setHostName(host.getName());
|
||||
conn.setHostCode(host.getCode());
|
||||
conn.setHostAddress(host.getAddress());
|
||||
conn.setHostPort(config.getPort());
|
||||
conn.setTimeout(config.getConnectTimeout());
|
||||
conn.setCharset(config.getCharset());
|
||||
conn.setFileNameCharset(config.getFileNameCharset());
|
||||
conn.setFileContentCharset(config.getFileContentCharset());
|
||||
|
||||
// 获取自定义认证方式
|
||||
HostExtraSshAuthTypeEnum extraAuthType = Optional.ofNullable(extra)
|
||||
.map(HostSshExtraModel::getAuthType)
|
||||
.map(HostExtraSshAuthTypeEnum::of)
|
||||
.orElse(null);
|
||||
if (HostExtraSshAuthTypeEnum.CUSTOM_KEY.equals(extraAuthType)) {
|
||||
// 自定义密钥
|
||||
config.setAuthType(HostSshAuthTypeEnum.KEY.name());
|
||||
config.setKeyId(extra.getKeyId());
|
||||
if (extra.getUsername() != null) {
|
||||
config.setUsername(extra.getUsername());
|
||||
}
|
||||
} else if (HostExtraSshAuthTypeEnum.CUSTOM_IDENTITY.equals(extraAuthType)) {
|
||||
// 自定义身份
|
||||
config.setAuthType(HostSshAuthTypeEnum.IDENTITY.name());
|
||||
config.setIdentityId(extra.getIdentityId());
|
||||
}
|
||||
|
||||
// 身份认证
|
||||
HostSshAuthTypeEnum authType = HostSshAuthTypeEnum.of(config.getAuthType());
|
||||
if (HostSshAuthTypeEnum.IDENTITY.equals(authType)) {
|
||||
// 身份认证
|
||||
Valid.notNull(config.getIdentityId(), ErrorMessage.IDENTITY_ABSENT);
|
||||
HostIdentityDO identity = hostIdentityDAO.selectById(config.getIdentityId());
|
||||
Valid.notNull(identity, ErrorMessage.IDENTITY_ABSENT);
|
||||
config.setUsername(identity.getUsername());
|
||||
HostIdentityTypeEnum identityType = HostIdentityTypeEnum.of(identity.getType());
|
||||
if (HostIdentityTypeEnum.PASSWORD.equals(identityType)) {
|
||||
// 密码类型
|
||||
authType = HostSshAuthTypeEnum.PASSWORD;
|
||||
config.setPassword(identity.getPassword());
|
||||
} else if (HostIdentityTypeEnum.KEY.equals(identityType)) {
|
||||
// 密钥类型
|
||||
authType = HostSshAuthTypeEnum.KEY;
|
||||
config.setKeyId(identity.getKeyId());
|
||||
}
|
||||
}
|
||||
|
||||
// 填充认证信息
|
||||
conn.setUsername(config.getUsername());
|
||||
if (HostSshAuthTypeEnum.PASSWORD.equals(authType)) {
|
||||
// 密码认证
|
||||
conn.setPassword(config.getPassword());
|
||||
} else if (HostSshAuthTypeEnum.KEY.equals(authType)) {
|
||||
// 密钥认证
|
||||
Long keyId = config.getKeyId();
|
||||
Valid.notNull(keyId, ErrorMessage.KEY_ABSENT);
|
||||
HostKeyDO key = hostKeyDAO.selectById(keyId);
|
||||
Valid.notNull(key, ErrorMessage.KEY_ABSENT);
|
||||
conn.setKeyId(keyId);
|
||||
conn.setPublicKey(key.getPublicKey());
|
||||
conn.setPrivateKey(key.getPrivateKey());
|
||||
conn.setPrivateKeyPassword(key.getPassword());
|
||||
}
|
||||
return conn;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -22,14 +22,14 @@
|
||||
*/
|
||||
package org.dromara.visor.module.asset.service.impl;
|
||||
|
||||
import cn.orionsec.kit.lang.function.Functions;
|
||||
import cn.orionsec.kit.lang.utils.collect.Maps;
|
||||
import cn.orionsec.kit.lang.utils.Strings;
|
||||
import org.dromara.visor.common.constant.Const;
|
||||
import org.dromara.visor.common.handler.data.model.GenericsDataModel;
|
||||
import org.dromara.visor.common.utils.Valid;
|
||||
import org.dromara.visor.framework.security.core.utils.SecurityUtils;
|
||||
import org.dromara.visor.module.asset.entity.request.host.HostExtraQueryRequest;
|
||||
import org.dromara.visor.module.asset.entity.request.host.HostExtraUpdateRequest;
|
||||
import org.dromara.visor.module.asset.enums.HostExtraItemEnum;
|
||||
import org.dromara.visor.module.asset.handler.host.extra.model.HostSpecExtraModel;
|
||||
import org.dromara.visor.module.asset.service.HostExtraService;
|
||||
import org.dromara.visor.module.infra.api.DataExtraApi;
|
||||
import org.dromara.visor.module.infra.entity.dto.data.DataExtraDTO;
|
||||
@@ -57,10 +57,10 @@ public class HostExtraServiceImpl implements HostExtraService {
|
||||
private DataExtraApi dataExtraApi;
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getHostExtra(Long hostId, String item) {
|
||||
public Map<String, Object> getHostExtraView(Long hostId, String item) {
|
||||
HostExtraItemEnum extraItem = Valid.valid(HostExtraItemEnum::of, item);
|
||||
Long userId = this.getExtraUserId(extraItem);
|
||||
// 查询配置项
|
||||
Long userId = SecurityUtils.getLoginUserId();
|
||||
DataExtraQueryDTO query = DataExtraQueryDTO.builder()
|
||||
.userId(userId)
|
||||
.relId(hostId)
|
||||
@@ -83,42 +83,26 @@ public class HostExtraServiceImpl implements HostExtraService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Map<String, Object>> getHostExtraList(HostExtraQueryRequest request) {
|
||||
Long hostId = request.getHostId();
|
||||
List<String> items = request.getItems();
|
||||
List<HostExtraItemEnum> extraItems = items.stream()
|
||||
.map(s -> Valid.valid(HostExtraItemEnum::of, s))
|
||||
.collect(Collectors.toList());
|
||||
// 查询配置项
|
||||
Long userId = SecurityUtils.getLoginUserId();
|
||||
public Map<Long, HostSpecExtraModel> getHostSpecMap(List<Long> hostIdList) {
|
||||
// 查询条件
|
||||
DataExtraQueryDTO query = DataExtraQueryDTO.builder()
|
||||
.userId(userId)
|
||||
.relId(hostId)
|
||||
.items(items)
|
||||
.userId(Const.SYSTEM_USER_ID)
|
||||
.item(HostExtraItemEnum.SPEC.name())
|
||||
.relIdList(hostIdList)
|
||||
.build();
|
||||
Map<String, String> extraValues = dataExtraApi.getExtraItems(query, DataExtraTypeEnum.HOST)
|
||||
// 查询
|
||||
return dataExtraApi.getExtraItems(query, DataExtraTypeEnum.HOST)
|
||||
.stream()
|
||||
.collect(Collectors.toMap(
|
||||
DataExtraDTO::getItem,
|
||||
DataExtraDTO::getValue,
|
||||
Functions.right())
|
||||
);
|
||||
// 检查初始化
|
||||
Map<String, Map<String, Object>> result = Maps.newMap();
|
||||
for (HostExtraItemEnum extraItem : extraItems) {
|
||||
String item = extraItem.name();
|
||||
// 检查初始化并转为视图
|
||||
Map<String, Object> extraValue = this.checkItemAndToView(extraItem, extraValues.get(item), userId, hostId);
|
||||
result.put(item, extraValue);
|
||||
}
|
||||
return result;
|
||||
.filter(s -> Strings.isNotBlank(s.getValue()))
|
||||
.collect(Collectors.toMap(DataExtraDTO::getRelId,
|
||||
s -> HostExtraItemEnum.SPEC.toView(s.getValue())));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer updateHostExtra(HostExtraUpdateRequest request) {
|
||||
Long hostId = request.getHostId();
|
||||
Long userId = SecurityUtils.getLoginUserId();
|
||||
HostExtraItemEnum item = Valid.valid(HostExtraItemEnum::of, request.getItem());
|
||||
Long hostId = request.getHostId();
|
||||
Long userId = this.getExtraUserId(item);
|
||||
// 查询原始配置
|
||||
DataExtraQueryDTO query = DataExtraQueryDTO.builder()
|
||||
.userId(userId)
|
||||
@@ -178,4 +162,14 @@ public class HostExtraServiceImpl implements HostExtraService {
|
||||
return extraValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取额外配置 userId
|
||||
*
|
||||
* @param item item
|
||||
* @return userId
|
||||
*/
|
||||
private Long getExtraUserId(HostExtraItemEnum item) {
|
||||
return item.isUserExtra() ? SecurityUtils.getLoginUserId() : Const.SYSTEM_USER_ID;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ import org.dromara.visor.framework.redis.core.utils.RedisMaps;
|
||||
import org.dromara.visor.framework.redis.core.utils.RedisUtils;
|
||||
import org.dromara.visor.framework.redis.core.utils.barrier.CacheBarriers;
|
||||
import org.dromara.visor.module.asset.convert.HostIdentityConvert;
|
||||
import org.dromara.visor.module.asset.dao.HostDAO;
|
||||
import org.dromara.visor.module.asset.dao.HostConfigDAO;
|
||||
import org.dromara.visor.module.asset.dao.HostIdentityDAO;
|
||||
import org.dromara.visor.module.asset.dao.HostKeyDAO;
|
||||
import org.dromara.visor.module.asset.define.cache.HostCacheKeyDefine;
|
||||
@@ -84,7 +84,7 @@ public class HostIdentityServiceImpl implements HostIdentityService {
|
||||
private HostKeyDAO hostKeyDAO;
|
||||
|
||||
@Resource
|
||||
private HostDAO hostDAO;
|
||||
private HostConfigDAO hostConfigDAO;
|
||||
|
||||
@Resource
|
||||
private DataExtraApi dataExtraApi;
|
||||
@@ -233,7 +233,7 @@ public class HostIdentityServiceImpl implements HostIdentityService {
|
||||
// 删除数据库
|
||||
int effect = hostIdentityDAO.deleteBatchIds(idList);
|
||||
// 删除主机配置
|
||||
hostDAO.setIdentityIdWithNull(idList);
|
||||
hostConfigDAO.setIdentityIdWithNull(idList);
|
||||
// 删除主机身份额外配置
|
||||
dataExtraApi.deleteHostIdentityExtra(idList);
|
||||
// 删除数据权限
|
||||
|
||||
@@ -38,7 +38,7 @@ import org.dromara.visor.framework.redis.core.utils.RedisMaps;
|
||||
import org.dromara.visor.framework.redis.core.utils.RedisUtils;
|
||||
import org.dromara.visor.framework.redis.core.utils.barrier.CacheBarriers;
|
||||
import org.dromara.visor.module.asset.convert.HostKeyConvert;
|
||||
import org.dromara.visor.module.asset.dao.HostDAO;
|
||||
import org.dromara.visor.module.asset.dao.HostConfigDAO;
|
||||
import org.dromara.visor.module.asset.dao.HostIdentityDAO;
|
||||
import org.dromara.visor.module.asset.dao.HostKeyDAO;
|
||||
import org.dromara.visor.module.asset.define.cache.HostCacheKeyDefine;
|
||||
@@ -78,7 +78,7 @@ public class HostKeyServiceImpl implements HostKeyService {
|
||||
private HostIdentityDAO hostIdentityDAO;
|
||||
|
||||
@Resource
|
||||
private HostDAO hostDAO;
|
||||
private HostConfigDAO hostConfigDAO;
|
||||
|
||||
@Resource
|
||||
private DataExtraApi dataExtraApi;
|
||||
@@ -217,7 +217,7 @@ public class HostKeyServiceImpl implements HostKeyService {
|
||||
// 删除关联
|
||||
hostIdentityDAO.setKeyWithNull(idList);
|
||||
// 删除主机配置
|
||||
hostDAO.setKeyIdWithNull(idList);
|
||||
hostConfigDAO.setKeyIdWithNull(idList);
|
||||
// 删除主机密钥额外配置
|
||||
dataExtraApi.deleteHostKeyExtra(idList);
|
||||
// 删除数据权限
|
||||
|
||||
@@ -33,24 +33,27 @@ import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.visor.common.constant.Const;
|
||||
import org.dromara.visor.common.constant.ErrorMessage;
|
||||
import org.dromara.visor.common.constant.ExtraFieldConst;
|
||||
import org.dromara.visor.common.handler.data.model.GenericsDataModel;
|
||||
import org.dromara.visor.common.enums.EnableStatus;
|
||||
import org.dromara.visor.common.utils.Valid;
|
||||
import org.dromara.visor.framework.biz.operator.log.core.utils.OperatorLogs;
|
||||
import org.dromara.visor.framework.redis.core.utils.RedisMaps;
|
||||
import org.dromara.visor.framework.redis.core.utils.barrier.CacheBarriers;
|
||||
import org.dromara.visor.module.asset.convert.HostConvert;
|
||||
import org.dromara.visor.module.asset.dao.HostConfigDAO;
|
||||
import org.dromara.visor.module.asset.dao.HostDAO;
|
||||
import org.dromara.visor.module.asset.define.cache.HostCacheKeyDefine;
|
||||
import org.dromara.visor.module.asset.entity.domain.HostDO;
|
||||
import org.dromara.visor.module.asset.entity.dto.HostCacheDTO;
|
||||
import org.dromara.visor.module.asset.entity.request.host.*;
|
||||
import org.dromara.visor.module.asset.entity.vo.HostConfigVO;
|
||||
import org.dromara.visor.module.asset.entity.request.host.HostCreateRequest;
|
||||
import org.dromara.visor.module.asset.entity.request.host.HostQueryRequest;
|
||||
import org.dromara.visor.module.asset.entity.request.host.HostUpdateRequest;
|
||||
import org.dromara.visor.module.asset.entity.request.host.HostUpdateStatusRequest;
|
||||
import org.dromara.visor.module.asset.entity.vo.HostVO;
|
||||
import org.dromara.visor.module.asset.enums.HostStatusEnum;
|
||||
import org.dromara.visor.module.asset.enums.HostTypeEnum;
|
||||
import org.dromara.visor.module.asset.handler.host.extra.model.HostSpecExtraModel;
|
||||
import org.dromara.visor.module.asset.service.ExecJobHostService;
|
||||
import org.dromara.visor.module.asset.service.ExecTemplateHostService;
|
||||
import org.dromara.visor.module.asset.service.HostExtraService;
|
||||
import org.dromara.visor.module.asset.service.HostService;
|
||||
import org.dromara.visor.module.infra.api.DataExtraApi;
|
||||
import org.dromara.visor.module.infra.api.DataGroupRelApi;
|
||||
@@ -63,6 +66,7 @@ import org.dromara.visor.module.infra.enums.FavoriteTypeEnum;
|
||||
import org.dromara.visor.module.infra.enums.TagTypeEnum;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Comparator;
|
||||
@@ -86,6 +90,12 @@ public class HostServiceImpl implements HostService {
|
||||
@Resource
|
||||
private HostDAO hostDAO;
|
||||
|
||||
@Resource
|
||||
private HostConfigDAO hostConfigDAO;
|
||||
|
||||
@Resource
|
||||
private HostExtraService hostExtraService;
|
||||
|
||||
@Resource
|
||||
private ExecJobHostService execJobHostService;
|
||||
|
||||
@@ -105,8 +115,8 @@ public class HostServiceImpl implements HostService {
|
||||
private DataExtraApi dataExtraApi;
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Long createHost(HostCreateRequest request) {
|
||||
HostTypeEnum type = Valid.valid(HostTypeEnum::of, request.getType());
|
||||
log.info("HostService-createHost request: {}", JSON.toJSONString(request));
|
||||
// 转换
|
||||
HostDO record = HostConvert.MAPPER.to(request);
|
||||
@@ -114,8 +124,6 @@ public class HostServiceImpl implements HostService {
|
||||
// 查询数据是否冲突
|
||||
this.checkHostNamePresent(record);
|
||||
this.checkHostCodePresent(record);
|
||||
// 设置主机配置
|
||||
record.setConfig(type.getDefault().serial());
|
||||
// 插入主机
|
||||
int effect = hostDAO.insert(record);
|
||||
log.info("HostService-createHost effect: {}", effect);
|
||||
@@ -133,77 +141,57 @@ public class HostServiceImpl implements HostService {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Integer updateHostById(HostUpdateRequest request) {
|
||||
log.info("HostService-updateHostById request: {}", JSON.toJSONString(request));
|
||||
List<String> types = request.getTypes();
|
||||
// 查询
|
||||
Long id = Valid.notNull(request.getId(), ErrorMessage.ID_MISSING);
|
||||
HostDO record = hostDAO.selectBaseById(id);
|
||||
HostDO record = hostDAO.selectById(id);
|
||||
Valid.notNull(record, ErrorMessage.HOST_ABSENT);
|
||||
// 转换
|
||||
HostDO updateRecord = HostConvert.MAPPER.to(request);
|
||||
// 查询数据是否冲突
|
||||
this.checkHostNamePresent(updateRecord);
|
||||
this.checkHostCodePresent(updateRecord);
|
||||
// 更新
|
||||
// 更新主机
|
||||
int effect = hostDAO.updateById(updateRecord);
|
||||
log.info("HostService-updateHostById effect: {}", effect);
|
||||
// 引用分组
|
||||
dataGroupRelApi.updateRelGroup(DataGroupTypeEnum.HOST, request.getGroupIdList(), id);
|
||||
// 更新 tag
|
||||
tagRelApi.setTagRel(TagTypeEnum.HOST, id, request.getTags());
|
||||
// 修改 config 状态
|
||||
hostConfigDAO.updateConfigStatus(id, types, EnableStatus.ENABLED.name());
|
||||
hostConfigDAO.updateConfigInvertStatus(id, types, EnableStatus.DISABLED.name());
|
||||
// 删除缓存
|
||||
this.clearCache();
|
||||
return effect;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Integer updateHostStatus(HostUpdateStatusRequest request) {
|
||||
log.info("HostService-updateHostStatus request: {}", JSON.toJSONString(request));
|
||||
Long id = request.getId();
|
||||
HostStatusEnum status = Valid.valid(HostStatusEnum::of, request.getStatus());
|
||||
String status = Valid.valid(HostStatusEnum::of, request.getStatus()).name();
|
||||
// 查询主机
|
||||
HostDO record = hostDAO.selectBaseById(id);
|
||||
HostDO record = hostDAO.selectById(id);
|
||||
Valid.notNull(record, ErrorMessage.HOST_ABSENT);
|
||||
// 更新
|
||||
HostDO update = HostDO.builder()
|
||||
.id(id)
|
||||
.status(status.name())
|
||||
.status(status)
|
||||
.build();
|
||||
int effect = hostDAO.updateById(update);
|
||||
log.info("HostService-updateHostStatus effect: {}", effect);
|
||||
// 更新主机配置
|
||||
hostConfigDAO.updateConfigStatus(id, null, status);
|
||||
// 删除缓存
|
||||
this.clearCache();
|
||||
return effect;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer updateHostConfig(HostUpdateConfigRequest request) {
|
||||
// 设置日志参数
|
||||
String param = OperatorLogs.toJsonString(JSON.parseObject(request.getConfig()));
|
||||
OperatorLogs.add(ExtraFieldConst.CONFIG, param);
|
||||
log.info("HostService-updateHostConfig request: {}", param);
|
||||
Long id = request.getId();
|
||||
// 查询主机信息
|
||||
HostDO host = hostDAO.selectById(id);
|
||||
Valid.notNull(host, ErrorMessage.HOST_ABSENT);
|
||||
HostTypeEnum type = Valid.valid(HostTypeEnum::of, host.getType());
|
||||
GenericsDataModel beforeConfig = type.parse(host.getConfig());
|
||||
GenericsDataModel newConfig = type.parse(request.getConfig());
|
||||
// 添加日志参数
|
||||
OperatorLogs.add(OperatorLogs.ID, id);
|
||||
OperatorLogs.add(OperatorLogs.NAME, host.getName());
|
||||
// 更新前校验
|
||||
type.doValid(beforeConfig, newConfig);
|
||||
// 修改配置
|
||||
HostDO updateHost = HostDO.builder()
|
||||
.id(id)
|
||||
.config(newConfig.serial())
|
||||
.build();
|
||||
int effect = hostDAO.updateById(updateHost);
|
||||
log.info("HostService-updateHostConfig effect: {}", effect);
|
||||
return effect;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public HostVO getHostById(Long id) {
|
||||
@@ -212,7 +200,7 @@ public class HostServiceImpl implements HostService {
|
||||
// 查询分组信息
|
||||
Future<Set<Long>> groupIdFuture = dataGroupRelApi.getGroupIdByRelIdAsync(DataGroupTypeEnum.HOST, id);
|
||||
// 查询主机
|
||||
HostDO record = hostDAO.selectBaseById(id);
|
||||
HostDO record = hostDAO.selectById(id);
|
||||
Valid.notNull(record, ErrorMessage.HOST_ABSENT);
|
||||
// 转换
|
||||
HostVO vo = HostConvert.MAPPER.to(record);
|
||||
@@ -221,24 +209,6 @@ public class HostServiceImpl implements HostService {
|
||||
return vo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HostConfigVO getHostConfig(Long id) {
|
||||
// 查询主机
|
||||
HostDO host = hostDAO.selectById(id);
|
||||
Valid.notNull(host, ErrorMessage.HOST_ABSENT);
|
||||
// 获取主机类型
|
||||
String type = host.getType();
|
||||
HostTypeEnum hostType = HostTypeEnum.of(type);
|
||||
// 获取主机配置
|
||||
Map<String, Object> config = hostType.toView(host.getConfig()).toMap();
|
||||
// 返回
|
||||
return HostConfigVO.builder()
|
||||
.id(id)
|
||||
.type(type)
|
||||
.config(config)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<HostVO> getHostList(String type) {
|
||||
// 查询缓存
|
||||
@@ -253,8 +223,7 @@ public class HostServiceImpl implements HostService {
|
||||
// 查询数据库
|
||||
list = hostDAO.of()
|
||||
.createWrapper(true)
|
||||
.select(HostDAO.BASE_COLUMN)
|
||||
.eq(HostDO::getType, type)
|
||||
.like(HostDO::getTypes, type)
|
||||
.then()
|
||||
.list(HostConvert.MAPPER::toCache);
|
||||
// 设置屏障 防止穿透
|
||||
@@ -278,16 +247,12 @@ public class HostServiceImpl implements HostService {
|
||||
if (wrapper == null) {
|
||||
return DataGrid.of(Lists.empty());
|
||||
}
|
||||
// 数量条件
|
||||
LambdaQueryWrapper<HostDO> countWrapper = wrapper.clone();
|
||||
// 基础条件
|
||||
wrapper.select(HostDAO.BASE_COLUMN);
|
||||
// 查询
|
||||
DataGrid<HostVO> hosts = hostDAO.of()
|
||||
.wrapper(wrapper)
|
||||
.page(request)
|
||||
.order(request, HostDO::getId)
|
||||
.dataGrid(countWrapper, HostConvert.MAPPER::to);
|
||||
.dataGrid(HostConvert.MAPPER::to);
|
||||
// 查询拓展信息
|
||||
this.setExtraInfo(request, hosts.getRows());
|
||||
return hosts;
|
||||
@@ -328,6 +293,8 @@ public class HostServiceImpl implements HostService {
|
||||
@Async("asyncExecutor")
|
||||
public void deleteHostRelByIdListAsync(List<Long> idList) {
|
||||
log.info("HostService-deleteHostRelByIdListAsync idList: {}", idList);
|
||||
// 删除主机配置
|
||||
hostConfigDAO.deleteByHostIdList(idList);
|
||||
// 删除计划任务主机
|
||||
execJobHostService.deleteByHostIdList(idList);
|
||||
// 删除执行模板主机
|
||||
@@ -393,12 +360,13 @@ public class HostServiceImpl implements HostService {
|
||||
}
|
||||
// 基础条件
|
||||
wrapper.eq(HostDO::getId, request.getId())
|
||||
.eq(HostDO::getStatus, request.getStatus())
|
||||
.eq(HostDO::getType, request.getType())
|
||||
.eq(HostDO::getOsType, request.getOsType())
|
||||
.eq(HostDO::getArchType, request.getArchType())
|
||||
.eq(HostDO::getStatus, request.getStatus())
|
||||
.like(HostDO::getName, request.getName())
|
||||
.like(HostDO::getCode, request.getCode())
|
||||
.like(HostDO::getAddress, request.getAddress())
|
||||
.like(HostDO::getTypes, request.getType())
|
||||
.like(HostDO::getDescription, request.getDescription())
|
||||
.and(Strings.isNotEmpty(searchValue), c -> c
|
||||
.eq(HostDO::getId, searchValue).or()
|
||||
@@ -428,6 +396,20 @@ public class HostServiceImpl implements HostService {
|
||||
hosts.get(i).setTags(tagList.get(i));
|
||||
}
|
||||
}
|
||||
// 查询分组信息
|
||||
if (Booleans.isTrue(request.getQueryGroup())) {
|
||||
Map<Long, Set<Long>> groupRelList = dataGroupRelApi.getGroupRelByRelIdList(DataGroupTypeEnum.HOST, idList);
|
||||
for (HostVO host : hosts) {
|
||||
host.setGroupIdList(groupRelList.get(host.getId()));
|
||||
}
|
||||
}
|
||||
// 查询规格信息
|
||||
if (Booleans.isTrue(request.getQuerySpec())) {
|
||||
Map<Long, HostSpecExtraModel> specMap = hostExtraService.getHostSpecMap(idList);
|
||||
for (HostVO host : hosts) {
|
||||
host.setSpec(specMap.get(host.getId()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,41 +25,21 @@ package org.dromara.visor.module.asset.service.impl;
|
||||
import cn.orionsec.kit.lang.id.UUIds;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.visor.common.constant.ErrorMessage;
|
||||
import org.dromara.visor.common.constant.ExtraFieldConst;
|
||||
import org.dromara.visor.common.security.LoginUser;
|
||||
import org.dromara.visor.common.utils.Valid;
|
||||
import org.dromara.visor.framework.redis.core.utils.RedisStrings;
|
||||
import org.dromara.visor.framework.security.core.utils.SecurityUtils;
|
||||
import org.dromara.visor.module.asset.dao.HostDAO;
|
||||
import org.dromara.visor.module.asset.dao.HostIdentityDAO;
|
||||
import org.dromara.visor.module.asset.dao.HostKeyDAO;
|
||||
import org.dromara.visor.module.asset.define.cache.TerminalCacheKeyDefine;
|
||||
import org.dromara.visor.module.asset.entity.domain.HostDO;
|
||||
import org.dromara.visor.module.asset.entity.domain.HostIdentityDO;
|
||||
import org.dromara.visor.module.asset.entity.domain.HostKeyDO;
|
||||
import org.dromara.visor.module.asset.entity.dto.TerminalAccessDTO;
|
||||
import org.dromara.visor.module.asset.entity.dto.TerminalConnectDTO;
|
||||
import org.dromara.visor.module.asset.entity.dto.TerminalTransferDTO;
|
||||
import org.dromara.visor.module.asset.entity.vo.TerminalThemeVO;
|
||||
import org.dromara.visor.module.asset.enums.HostExtraItemEnum;
|
||||
import org.dromara.visor.module.asset.enums.HostExtraSshAuthTypeEnum;
|
||||
import org.dromara.visor.module.asset.enums.HostIdentityTypeEnum;
|
||||
import org.dromara.visor.module.asset.enums.HostSshAuthTypeEnum;
|
||||
import org.dromara.visor.module.asset.handler.host.config.model.HostSshConfigModel;
|
||||
import org.dromara.visor.module.asset.handler.host.extra.model.HostSshExtraModel;
|
||||
import org.dromara.visor.module.asset.service.AssetAuthorizedDataService;
|
||||
import org.dromara.visor.module.asset.service.HostConfigService;
|
||||
import org.dromara.visor.module.asset.service.HostExtraService;
|
||||
import org.dromara.visor.module.asset.service.TerminalService;
|
||||
import org.dromara.visor.module.infra.api.DataPermissionApi;
|
||||
import org.dromara.visor.module.infra.api.DictValueApi;
|
||||
import org.dromara.visor.module.infra.enums.DataPermissionTypeEnum;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -75,27 +55,6 @@ public class TerminalServiceImpl implements TerminalService {
|
||||
|
||||
private static final String THEME_DICT_KEY = "terminalTheme";
|
||||
|
||||
@Resource
|
||||
private HostConfigService hostConfigService;
|
||||
|
||||
@Resource
|
||||
private HostExtraService hostExtraService;
|
||||
|
||||
@Resource
|
||||
private AssetAuthorizedDataService assetAuthorizedDataService;
|
||||
|
||||
@Resource
|
||||
private HostDAO hostDAO;
|
||||
|
||||
@Resource
|
||||
private HostIdentityDAO hostIdentityDAO;
|
||||
|
||||
@Resource
|
||||
private HostKeyDAO hostKeyDAO;
|
||||
|
||||
@Resource
|
||||
private DataPermissionApi dataPermissionApi;
|
||||
|
||||
@Resource
|
||||
private DictValueApi dictValueApi;
|
||||
|
||||
@@ -169,139 +128,4 @@ public class TerminalServiceImpl implements TerminalService {
|
||||
return transfer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TerminalConnectDTO getTerminalConnectInfo(Long hostId) {
|
||||
log.info("HostTerminalService.getTerminalConnectInfo-withHost hostId: {}", hostId);
|
||||
// 查询主机
|
||||
HostDO host = hostDAO.selectById(hostId);
|
||||
// 查询主机配置
|
||||
HostSshConfigModel config = hostConfigService.getHostConfig(host);
|
||||
// 获取配置
|
||||
return this.getHostConnectInfo(host, config, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TerminalConnectDTO getTerminalConnectInfo(Long hostId, Long userId) {
|
||||
// 查询主机
|
||||
HostDO host = hostDAO.selectById(hostId);
|
||||
Valid.notNull(host, ErrorMessage.HOST_ABSENT);
|
||||
// 获取配置
|
||||
return this.getTerminalConnectInfo(host, userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TerminalConnectDTO getTerminalConnectInfo(HostDO host, Long userId) {
|
||||
Long hostId = host.getId();
|
||||
log.info("HostTerminalService.getTerminalConnectInfo hostId: {}, userId: {}", hostId, userId);
|
||||
// 验证主机是否有权限
|
||||
List<Long> hostIdList = assetAuthorizedDataService.getUserAuthorizedHostId(userId);
|
||||
Valid.isTrue(hostIdList.contains(hostId),
|
||||
ErrorMessage.ANY_NO_PERMISSION,
|
||||
DataPermissionTypeEnum.HOST_GROUP.getPermissionName());
|
||||
// 获取主机配置
|
||||
HostSshConfigModel config = hostConfigService.getHostConfig(host);
|
||||
Valid.notNull(config, ErrorMessage.CONFIG_ABSENT);
|
||||
// 查询主机额外配置
|
||||
HostSshExtraModel extra = hostExtraService.getHostExtra(userId, hostId, HostExtraItemEnum.SSH);
|
||||
if (extra != null) {
|
||||
HostExtraSshAuthTypeEnum extraAuthType = HostExtraSshAuthTypeEnum.of(extra.getAuthType());
|
||||
if (HostExtraSshAuthTypeEnum.CUSTOM_KEY.equals(extraAuthType)) {
|
||||
// 验证主机密钥是否有权限
|
||||
Valid.notNull(extra.getKeyId(), ErrorMessage.KEY_ABSENT);
|
||||
Valid.isTrue(dataPermissionApi.hasPermission(DataPermissionTypeEnum.HOST_KEY, userId, extra.getKeyId()),
|
||||
ErrorMessage.ANY_NO_PERMISSION,
|
||||
DataPermissionTypeEnum.HOST_KEY.getPermissionName());
|
||||
} else if (HostExtraSshAuthTypeEnum.CUSTOM_IDENTITY.equals(extraAuthType)) {
|
||||
// 验证主机身份是否有权限
|
||||
Valid.notNull(extra.getIdentityId(), ErrorMessage.IDENTITY_ABSENT);
|
||||
Valid.isTrue(dataPermissionApi.hasPermission(DataPermissionTypeEnum.HOST_IDENTITY, userId, extra.getIdentityId()),
|
||||
ErrorMessage.ANY_NO_PERMISSION,
|
||||
DataPermissionTypeEnum.HOST_IDENTITY.getPermissionName());
|
||||
}
|
||||
}
|
||||
// 获取连接配置
|
||||
return this.getHostConnectInfo(host, config, extra);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取主机会话连接配置
|
||||
*
|
||||
* @param host host
|
||||
* @param config config
|
||||
* @param extra extra
|
||||
* @return session
|
||||
*/
|
||||
private TerminalConnectDTO getHostConnectInfo(HostDO host,
|
||||
HostSshConfigModel config,
|
||||
HostSshExtraModel extra) {
|
||||
// 填充认证信息
|
||||
TerminalConnectDTO conn = new TerminalConnectDTO();
|
||||
conn.setHostId(host.getId());
|
||||
conn.setHostName(host.getName());
|
||||
conn.setHostCode(host.getCode());
|
||||
conn.setHostAddress(host.getAddress());
|
||||
conn.setHostPort(host.getPort());
|
||||
conn.setOsType(host.getOsType());
|
||||
conn.setTimeout(config.getConnectTimeout());
|
||||
conn.setCharset(config.getCharset());
|
||||
conn.setFileNameCharset(config.getFileNameCharset());
|
||||
conn.setFileContentCharset(config.getFileContentCharset());
|
||||
|
||||
// 获取自定义认证方式
|
||||
HostExtraSshAuthTypeEnum extraAuthType = Optional.ofNullable(extra)
|
||||
.map(HostSshExtraModel::getAuthType)
|
||||
.map(HostExtraSshAuthTypeEnum::of)
|
||||
.orElse(null);
|
||||
if (HostExtraSshAuthTypeEnum.CUSTOM_KEY.equals(extraAuthType)) {
|
||||
// 自定义密钥
|
||||
config.setAuthType(HostSshAuthTypeEnum.KEY.name());
|
||||
config.setKeyId(extra.getKeyId());
|
||||
if (extra.getUsername() != null) {
|
||||
config.setUsername(extra.getUsername());
|
||||
}
|
||||
} else if (HostExtraSshAuthTypeEnum.CUSTOM_IDENTITY.equals(extraAuthType)) {
|
||||
// 自定义身份
|
||||
config.setAuthType(HostSshAuthTypeEnum.IDENTITY.name());
|
||||
config.setIdentityId(extra.getIdentityId());
|
||||
}
|
||||
|
||||
// 身份认证
|
||||
HostSshAuthTypeEnum authType = HostSshAuthTypeEnum.of(config.getAuthType());
|
||||
if (HostSshAuthTypeEnum.IDENTITY.equals(authType)) {
|
||||
// 身份认证
|
||||
Valid.notNull(config.getIdentityId(), ErrorMessage.IDENTITY_ABSENT);
|
||||
HostIdentityDO identity = hostIdentityDAO.selectById(config.getIdentityId());
|
||||
Valid.notNull(identity, ErrorMessage.IDENTITY_ABSENT);
|
||||
config.setUsername(identity.getUsername());
|
||||
HostIdentityTypeEnum identityType = HostIdentityTypeEnum.of(identity.getType());
|
||||
if (HostIdentityTypeEnum.PASSWORD.equals(identityType)) {
|
||||
// 密码类型
|
||||
authType = HostSshAuthTypeEnum.PASSWORD;
|
||||
config.setPassword(identity.getPassword());
|
||||
} else if (HostIdentityTypeEnum.KEY.equals(identityType)) {
|
||||
// 密钥类型
|
||||
authType = HostSshAuthTypeEnum.KEY;
|
||||
config.setKeyId(identity.getKeyId());
|
||||
}
|
||||
}
|
||||
|
||||
// 填充认证信息
|
||||
conn.setUsername(config.getUsername());
|
||||
if (HostSshAuthTypeEnum.PASSWORD.equals(authType)) {
|
||||
// 密码认证
|
||||
conn.setPassword(config.getPassword());
|
||||
} else if (HostSshAuthTypeEnum.KEY.equals(authType)) {
|
||||
// 密钥认证
|
||||
Long keyId = config.getKeyId();
|
||||
Valid.notNull(keyId, ErrorMessage.KEY_ABSENT);
|
||||
HostKeyDO key = hostKeyDAO.selectById(keyId);
|
||||
Valid.notNull(key, ErrorMessage.KEY_ABSENT);
|
||||
conn.setKeyId(keyId);
|
||||
conn.setPublicKey(key.getPublicKey());
|
||||
conn.setPrivateKey(key.getPrivateKey());
|
||||
conn.setPrivateKeyPassword(key.getPassword());
|
||||
}
|
||||
return conn;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ import org.dromara.visor.module.asset.entity.vo.TerminalSftpLogVO;
|
||||
import org.dromara.visor.module.asset.handler.host.jsch.SessionStores;
|
||||
import org.dromara.visor.module.asset.handler.host.transfer.manager.TerminalTransferManager;
|
||||
import org.dromara.visor.module.asset.handler.host.transfer.session.DownloadSession;
|
||||
import org.dromara.visor.module.asset.service.TerminalService;
|
||||
import org.dromara.visor.module.asset.service.HostConnectService;
|
||||
import org.dromara.visor.module.asset.service.TerminalSftpService;
|
||||
import org.dromara.visor.module.infra.api.OperatorLogApi;
|
||||
import org.dromara.visor.module.infra.entity.dto.operator.OperatorLogQueryDTO;
|
||||
@@ -85,7 +85,7 @@ public class TerminalSftpServiceImpl implements TerminalSftpService {
|
||||
private OperatorLogApi operatorLogApi;
|
||||
|
||||
@Resource
|
||||
private TerminalService terminalService;
|
||||
private HostConnectService hostConnectService;
|
||||
|
||||
@Resource
|
||||
private TerminalTransferManager terminalTransferManager;
|
||||
@@ -138,7 +138,7 @@ public class TerminalSftpServiceImpl implements TerminalSftpService {
|
||||
InputStream in = null;
|
||||
try {
|
||||
// 获取终端连接信息
|
||||
TerminalConnectDTO connectInfo = terminalService.getTerminalConnectInfo(cache.getHostId(), SecurityUtils.getLoginUserId());
|
||||
TerminalConnectDTO connectInfo = hostConnectService.getSshConnectInfo(cache.getHostId(), SecurityUtils.getLoginUserId());
|
||||
sessionStore = SessionStores.openSessionStore(connectInfo);
|
||||
executor = sessionStore.getSftpExecutor(connectInfo.getFileNameCharset());
|
||||
executor.connect();
|
||||
@@ -171,7 +171,7 @@ public class TerminalSftpServiceImpl implements TerminalSftpService {
|
||||
InputStream in = null;
|
||||
try {
|
||||
// 获取终端连接信息
|
||||
TerminalConnectDTO connectInfo = terminalService.getTerminalConnectInfo(cache.getHostId(), SecurityUtils.getLoginUserId());
|
||||
TerminalConnectDTO connectInfo = hostConnectService.getSshConnectInfo(cache.getHostId(), SecurityUtils.getLoginUserId());
|
||||
sessionStore = SessionStores.openSessionStore(connectInfo);
|
||||
executor = sessionStore.getSftpExecutor(connectInfo.getFileNameCharset());
|
||||
executor.connect();
|
||||
|
||||
@@ -359,7 +359,7 @@ public class UploadTaskServiceImpl implements UploadTaskService {
|
||||
*/
|
||||
private void checkHostPermission(List<Long> hostIdList) {
|
||||
// 查询有权限的主机
|
||||
List<Long> authorizedHostIdList = assetAuthorizedDataService.getUserAuthorizedEnabledHostId(SecurityUtils.getLoginUserId(), HostTypeEnum.SSH);
|
||||
List<Long> authorizedHostIdList = assetAuthorizedDataService.getUserAuthorizedEnabledHostId(SecurityUtils.getLoginUserId(), HostTypeEnum.SSH.name());
|
||||
for (Long hostId : hostIdList) {
|
||||
Valid.isTrue(authorizedHostIdList.contains(hostId), Strings.format(ErrorMessage.PLEASE_CHECK_HOST_SSH, hostId));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="org.dromara.visor.module.asset.dao.HostConfigDAO">
|
||||
|
||||
<!-- 通用查询映射结果 -->
|
||||
<resultMap id="BaseResultMap" type="org.dromara.visor.module.asset.entity.domain.HostConfigDO">
|
||||
<id column="id" property="id"/>
|
||||
<result column="host_id" property="hostId"/>
|
||||
<result column="type" property="type"/>
|
||||
<result column="status" property="status"/>
|
||||
<result column="config" property="config"/>
|
||||
<result column="create_time" property="createTime"/>
|
||||
<result column="update_time" property="updateTime"/>
|
||||
<result column="creator" property="creator"/>
|
||||
<result column="updater" property="updater"/>
|
||||
<result column="deleted" property="deleted"/>
|
||||
</resultMap>
|
||||
|
||||
<!-- 通用查询结果列 -->
|
||||
<sql id="Base_Column_List">
|
||||
id, host_id, type, status, config, create_time, update_time, creator, updater, deleted
|
||||
</sql>
|
||||
|
||||
<update id="setKeyIdWithNull">
|
||||
UPDATE host_config
|
||||
SET config = JSON_REMOVE(config, '$.keyId')
|
||||
WHERE deleted = 0
|
||||
<foreach collection="keyIdList" item="item" separator="OR" open="AND (" close=")">
|
||||
JSON_CONTAINS(config, JSON_OBJECT('keyId', #{item}))
|
||||
</foreach>
|
||||
</update>
|
||||
|
||||
<update id="setIdentityIdWithNull">
|
||||
UPDATE host_config
|
||||
SET config = JSON_REMOVE(config, '$.identityId')
|
||||
WHERE deleted = 0
|
||||
<foreach collection="identityIdList" item="item" separator="OR" open="AND (" close=")">
|
||||
JSON_CONTAINS(config, JSON_OBJECT('identityId', #{item}))
|
||||
</foreach>
|
||||
</update>
|
||||
|
||||
</mapper>
|
||||
@@ -5,14 +5,13 @@
|
||||
<!-- 通用查询映射结果 -->
|
||||
<resultMap id="BaseResultMap" type="org.dromara.visor.module.asset.entity.domain.HostDO">
|
||||
<id column="id" property="id"/>
|
||||
<result column="type" property="type"/>
|
||||
<result column="types" property="types"/>
|
||||
<result column="os_type" property="osType"/>
|
||||
<result column="arch_type" property="archType"/>
|
||||
<result column="name" property="name"/>
|
||||
<result column="code" property="code"/>
|
||||
<result column="address" property="address"/>
|
||||
<result column="port" property="port"/>
|
||||
<result column="status" property="status"/>
|
||||
<result column="config" property="config"/>
|
||||
<result column="description" property="description"/>
|
||||
<result column="create_time" property="createTime"/>
|
||||
<result column="update_time" property="updateTime"/>
|
||||
@@ -23,25 +22,7 @@
|
||||
|
||||
<!-- 通用查询结果列 -->
|
||||
<sql id="Base_Column_List">
|
||||
id, type, os_type, name, code, address, port, status, config, description, create_time, update_time, creator, updater, deleted
|
||||
id, types, os_type, arch_type, name, code, address, status, description, create_time, update_time, creator, updater, deleted
|
||||
</sql>
|
||||
|
||||
<update id="setKeyIdWithNull">
|
||||
UPDATE host
|
||||
SET config = JSON_REMOVE(config, '$.keyId')
|
||||
WHERE deleted = 0
|
||||
<foreach collection="keyIdList" item="item" separator="OR" open="AND (" close=")">
|
||||
JSON_CONTAINS(config, JSON_OBJECT('keyId', #{item}))
|
||||
</foreach>
|
||||
</update>
|
||||
|
||||
<update id="setIdentityIdWithNull">
|
||||
UPDATE host
|
||||
SET config = JSON_REMOVE(config, '$.identityId')
|
||||
WHERE deleted = 0
|
||||
<foreach collection="identityIdList" item="item" separator="OR" open="AND (" close=")">
|
||||
JSON_CONTAINS(config, JSON_OBJECT('identityId', #{item}))
|
||||
</foreach>
|
||||
</update>
|
||||
|
||||
</mapper>
|
||||
|
||||
Reference in New Issue
Block a user