Skip to content

Commit

Permalink
Merge pull request #128 from jsonwan/github_feature/whiteip_scope_query
Browse files Browse the repository at this point in the history
feature: 白名单IP添加时增加对“执行方式”的判断,如仅开文件分发则在脚本执行中不可选中 #115
  • Loading branch information
wangyu096 authored Jul 28, 2021
2 parents b271b3f + 5529873 commit b6f6e1b
Show file tree
Hide file tree
Showing 12 changed files with 262 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.tencent.bk.job.common.model.vo.TargetNodeVO;
import com.tencent.bk.job.manage.model.web.request.AgentStatisticsReq;
import com.tencent.bk.job.manage.model.web.request.FavorAppReq;
import com.tencent.bk.job.manage.model.web.request.IpCheckReq;
import com.tencent.bk.job.manage.model.web.request.ipchooser.AppTopologyTreeNode;
import com.tencent.bk.job.manage.model.web.request.ipchooser.ListHostByBizTopologyNodesReq;
import com.tencent.bk.job.manage.model.web.vo.AppVO;
Expand Down Expand Up @@ -190,7 +191,7 @@ ServiceResponse<List<DynamicGroupInfoVO>> listAppDynamicGroupWithoutHosts(
ServiceResponse<List<HostInfoVO>> listHostByIp(
@ApiParam(value = "用户名,网关自动传入", required = true) @RequestHeader("username") String username,
@ApiParam(value = "业务 ID", required = true) @PathVariable("appId") Long appId,
@ApiParam(value = "用户输入的 IP 列表", required = true) @RequestBody List<String> checkIpList);
@ApiParam(value = "用户输入的 IP 列表", required = true) @RequestBody IpCheckReq req);

@ApiOperation(value = "查询主机统计信息")
@PostMapping("/{appId}/host/statistics")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available.
*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-JOB蓝鲸智云作业平台 is licensed under the MIT License.
*
* License for BK-JOB蓝鲸智云作业平台:
* --------------------------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

package com.tencent.bk.job.manage.model.web.request;

import com.tencent.bk.job.manage.common.consts.whiteip.ActionScopeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;

@Data
@ApiModel("IP检查请求报文")
public class IpCheckReq {

@ApiModelProperty(value = "应用场景:脚本执行/文件分发", required = false)
ActionScopeEnum actionScope;

@ApiModelProperty(value = "IP列表,单个IP格式:cloudAreaId:ip", required = true)
List<String> ipList;

}


Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public ServiceResponse<List<ServiceHostStatusDTO>> getHostStatusByDynamicGroup(
public ServiceResponse<List<ServiceHostStatusDTO>> getHostStatusByIp(Long appId, String username,
ServiceGetHostStatusByIpReq req) {
List<String> ipList = req.getIpList();
List<HostInfoVO> hostInfoVOList = applicationService.getHostsByIp(username, appId, ipList);
List<HostInfoVO> hostInfoVOList = applicationService.getHostsByIp(username, appId, null, ipList);
List<ServiceHostStatusDTO> hostStatusDTOList = new ArrayList<>();
hostInfoVOList.forEach(hostInfoVO -> {
ServiceHostStatusDTO serviceHostStatusDTO = new ServiceHostStatusDTO();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,14 @@
import com.tencent.bk.job.manage.model.dto.ApplicationFavorDTO;
import com.tencent.bk.job.manage.model.web.request.AgentStatisticsReq;
import com.tencent.bk.job.manage.model.web.request.FavorAppReq;
import com.tencent.bk.job.manage.model.web.request.IpCheckReq;
import com.tencent.bk.job.manage.model.web.request.ipchooser.AppTopologyTreeNode;
import com.tencent.bk.job.manage.model.web.request.ipchooser.ListHostByBizTopologyNodesReq;
import com.tencent.bk.job.manage.model.web.vo.*;
import com.tencent.bk.job.manage.model.web.vo.AppVO;
import com.tencent.bk.job.manage.model.web.vo.CcTopologyNodeVO;
import com.tencent.bk.job.manage.model.web.vo.DynamicGroupInfoVO;
import com.tencent.bk.job.manage.model.web.vo.NodeInfoVO;
import com.tencent.bk.job.manage.model.web.vo.PageDataWithAvailableIdList;
import com.tencent.bk.job.manage.model.web.vo.index.AgentStatistics;
import com.tencent.bk.job.manage.service.ApplicationService;
import com.tencent.bk.job.manage.service.impl.ApplicationFavorService;
Expand Down Expand Up @@ -272,12 +277,12 @@ public ServiceResponse<List<AppTopologyTreeNode>> getNodeDetail(String username,
JobContextUtil.setAppId(appId);
List<AppTopologyTreeNode> treeNodeList = applicationService.getAppTopologyTreeNodeDetail(username, appId,
targetNodeVOList.stream().map(it -> new AppTopologyTreeNode(
it.getType(),
"",
it.getId(),
"",
null
)).collect(Collectors.toList()));
it.getType(),
"",
it.getId(),
"",
null
)).collect(Collectors.toList()));
return ServiceResponse.buildSuccessResp(treeNodeList);
}

Expand All @@ -286,11 +291,11 @@ public ServiceResponse<List<List<CcTopologyNodeVO>>> queryNodePaths(String usern
List<TargetNodeVO> targetNodeVOList) {
List<List<InstanceTopologyDTO>> pathList = applicationService.queryNodePaths(username, appId,
targetNodeVOList.stream().map(it -> {
InstanceTopologyDTO instanceTopologyDTO = new InstanceTopologyDTO();
instanceTopologyDTO.setObjectId(it.getType());
instanceTopologyDTO.setInstanceId(it.getId());
return instanceTopologyDTO;
}).collect(Collectors.toList()));
InstanceTopologyDTO instanceTopologyDTO = new InstanceTopologyDTO();
instanceTopologyDTO.setObjectId(it.getType());
instanceTopologyDTO.setInstanceId(it.getId());
return instanceTopologyDTO;
}).collect(Collectors.toList()));
List<List<CcTopologyNodeVO>> resultList = new ArrayList<>();
for (List<InstanceTopologyDTO> instanceTopologyDTOS : pathList) {
if (instanceTopologyDTOS == null) {
Expand All @@ -315,12 +320,12 @@ public ServiceResponse<List<NodeInfoVO>> listHostByNode(String username, Long ap
JobContextUtil.setAppId(appId);
List<NodeInfoVO> moduleHostInfoList = applicationService.getHostsByNode(username, appId,
targetNodeVOList.stream().map(it -> new AppTopologyTreeNode(
it.getType(),
"",
it.getId(),
"",
null
)).collect(Collectors.toList()));
it.getType(),
"",
it.getId(),
"",
null
)).collect(Collectors.toList()));
return ServiceResponse.buildSuccessResp(moduleHostInfoList);
}

Expand Down Expand Up @@ -364,8 +369,13 @@ public ServiceResponse<List<DynamicGroupInfoVO>> listAppDynamicGroupWithoutHosts
}

@Override
public ServiceResponse<List<HostInfoVO>> listHostByIp(String username, Long appId, List<String> checkIpList) {
return ServiceResponse.buildSuccessResp(applicationService.getHostsByIp(username, appId, checkIpList));
public ServiceResponse<List<HostInfoVO>> listHostByIp(String username, Long appId, IpCheckReq req) {
return ServiceResponse.buildSuccessResp(applicationService.getHostsByIp(
username,
appId,
req.getActionScope(),
req.getIpList())
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ public interface ActionScopeDAO {

int deleteActionScopeById(DSLContext dslContext, Long id);

ActionScopeDTO getActionScopeById(DSLContext dslContext, Long id);
ActionScopeDTO getActionScopeById(Long id);

ActionScopeVO getActionScopeVOById(DSLContext dslContext, Long id);
ActionScopeDTO getActionScopeByCode(String code);

List<ActionScopeDTO> listActionScopeDTO(DSLContext dslContext);
ActionScopeVO getActionScopeVOById(Long id);

List<ActionScopeDTO> listActionScopeDTO();

int updateActionScopeById(DSLContext dslContext, ActionScopeDTO actionScopeDTO);
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ Long countWhiteIPRecord(DSLContext dslContext, String partIP, List<Long> appIdLi

List<String> getWhiteIPActionScopes(DSLContext dslContext, Collection<Long> appIds, String ip, Long cloudAreaId);

List<CloudIPDTO> listWhiteIPByAppIds(DSLContext dslContext, Collection<Long> appIds);
List<CloudIPDTO> listWhiteIPByAppIds(DSLContext dslContext, Collection<Long> appIds, Long actionScopeId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.generated.tables.ActionScope;
import org.jooq.generated.tables.records.ActionScopeRecord;
import org.jooq.types.ULong;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
Expand All @@ -43,11 +44,13 @@

@Repository
public class ActionScopeDAOImpl implements ActionScopeDAO {
private DSLContext defaultDslContext;
private static final ActionScope T_ACTION_SCOPE = ActionScope.ACTION_SCOPE;
private final MessageI18nService i18nService;

@Autowired
public ActionScopeDAOImpl(MessageI18nService i18nService) {
public ActionScopeDAOImpl(DSLContext dslContext, MessageI18nService i18nService) {
this.defaultDslContext = dslContext;
this.i18nService = i18nService;
}

Expand Down Expand Up @@ -80,29 +83,24 @@ public int deleteActionScopeById(DSLContext dslContext, Long id) {
}

@Override
public ActionScopeDTO getActionScopeById(DSLContext dslContext, Long id) {
val record = dslContext.selectFrom(T_ACTION_SCOPE).where(
public ActionScopeDTO getActionScopeById(Long id) {
ActionScopeRecord record = defaultDslContext.selectFrom(T_ACTION_SCOPE).where(
T_ACTION_SCOPE.ID.eq(id)
).fetchOne();
if (record == null) {
return null;
} else {
return new ActionScopeDTO(
record.getId(),
record.getCode(),
record.getName(),
record.getDescription(),
record.getCreator(),
record.getCreateTime().longValue(),
record.getLastModifyUser(),
record.getLastModifyTime().longValue()
);
}
return convert(record);
}

@Override
public ActionScopeVO getActionScopeVOById(DSLContext dslContext, Long id) {
val record = dslContext.selectFrom(T_ACTION_SCOPE).where(
public ActionScopeDTO getActionScopeByCode(String code) {
ActionScopeRecord record = defaultDslContext.selectFrom(T_ACTION_SCOPE).where(
T_ACTION_SCOPE.CODE.eq(code)
).fetchOne();
return convert(record);
}

@Override
public ActionScopeVO getActionScopeVOById(Long id) {
val record = defaultDslContext.selectFrom(T_ACTION_SCOPE).where(
T_ACTION_SCOPE.ID.eq(id)
).fetchOne();
if (record == null) {
Expand All @@ -120,8 +118,8 @@ val record = dslContext.selectFrom(T_ACTION_SCOPE).where(
}

@Override
public List<ActionScopeDTO> listActionScopeDTO(DSLContext dslContext) {
val records = dslContext.selectFrom(T_ACTION_SCOPE).fetch();
public List<ActionScopeDTO> listActionScopeDTO() {
val records = defaultDslContext.selectFrom(T_ACTION_SCOPE).fetch();
if (records == null) {
return new ArrayList<>();
}
Expand Down Expand Up @@ -152,4 +150,17 @@ public int updateActionScopeById(DSLContext dslContext, ActionScopeDTO actionSco
.execute();
}

private ActionScopeDTO convert(ActionScopeRecord record) {
if (record == null) return null;
return new ActionScopeDTO(
record.getId(),
record.getCode(),
record.getName(),
record.getDescription(),
record.getCreator(),
record.getCreateTime().longValue(),
record.getLastModifyUser(),
record.getLastModifyTime().longValue()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@
import com.tencent.bk.job.common.model.dto.ApplicationInfoDTO;
import com.tencent.bk.job.common.util.CustomCollectionUtils;
import com.tencent.bk.job.manage.dao.ApplicationInfoDAO;
import com.tencent.bk.job.manage.dao.whiteip.*;
import com.tencent.bk.job.manage.dao.whiteip.ActionScopeDAO;
import com.tencent.bk.job.manage.dao.whiteip.WhiteIPActionScopeDAO;
import com.tencent.bk.job.manage.dao.whiteip.WhiteIPAppRelDAO;
import com.tencent.bk.job.manage.dao.whiteip.WhiteIPIPDAO;
import com.tencent.bk.job.manage.dao.whiteip.WhiteIPRecordDAO;
import com.tencent.bk.job.manage.model.dto.whiteip.CloudIPDTO;
import com.tencent.bk.job.manage.model.dto.whiteip.WhiteIPActionScopeDTO;
import com.tencent.bk.job.manage.model.dto.whiteip.WhiteIPIPDTO;
Expand All @@ -39,9 +43,20 @@
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.lang.StringUtils;
import org.jooq.*;
import org.jooq.Condition;
import org.jooq.DSLContext;
import org.jooq.OrderField;
import org.jooq.Record;
import org.jooq.Record1;
import org.jooq.Result;
import org.jooq.TableField;
import org.jooq.conf.ParamType;
import org.jooq.generated.tables.*;
import org.jooq.generated.tables.ActionScope;
import org.jooq.generated.tables.Application;
import org.jooq.generated.tables.WhiteIpActionScope;
import org.jooq.generated.tables.WhiteIpAppRel;
import org.jooq.generated.tables.WhiteIpIp;
import org.jooq.generated.tables.WhiteIpRecord;
import org.jooq.impl.DSL;
import org.jooq.types.ULong;
import org.slf4j.Logger;
Expand All @@ -56,7 +71,19 @@
import java.util.List;
import java.util.stream.Collectors;

import static com.tencent.bk.job.manage.common.consts.whiteip.Keys.*;
import static com.tencent.bk.job.manage.common.consts.whiteip.Keys.KEY_ACTION_SCOPE_ID_LIST;
import static com.tencent.bk.job.manage.common.consts.whiteip.Keys.KEY_APP_ID_LIST;
import static com.tencent.bk.job.manage.common.consts.whiteip.Keys.KEY_APP_NAME;
import static com.tencent.bk.job.manage.common.consts.whiteip.Keys.KEY_APP_TYPE;
import static com.tencent.bk.job.manage.common.consts.whiteip.Keys.KEY_CLOUD_AREA_ID;
import static com.tencent.bk.job.manage.common.consts.whiteip.Keys.KEY_CREATE_TIME;
import static com.tencent.bk.job.manage.common.consts.whiteip.Keys.KEY_CREATOR;
import static com.tencent.bk.job.manage.common.consts.whiteip.Keys.KEY_ID;
import static com.tencent.bk.job.manage.common.consts.whiteip.Keys.KEY_IP;
import static com.tencent.bk.job.manage.common.consts.whiteip.Keys.KEY_IP_LIST;
import static com.tencent.bk.job.manage.common.consts.whiteip.Keys.KEY_LAST_MODIFY_TIME;
import static com.tencent.bk.job.manage.common.consts.whiteip.Keys.KEY_LAST_MODIFY_USER;
import static com.tencent.bk.job.manage.common.consts.whiteip.Keys.KEY_REMARK;

@Slf4j
@Repository
Expand Down Expand Up @@ -333,7 +360,7 @@ private List<WhiteIPRecordVO> listWhiteIPRecordByConditions(DSLContext dslContex
val actionScopeIdListStr = (String) record.get(KEY_ACTION_SCOPE_ID_LIST);
List<String> actionScopeIdList = CustomCollectionUtils.getNoDuplicateList(actionScopeIdListStr, ",");
List<ActionScopeVO> actionScopeVOList = actionScopeIdList.stream().map(actionScopeId ->
actionScopeDAO.getActionScopeVOById(dslContext, Long.parseLong(actionScopeId))
actionScopeDAO.getActionScopeVOById(Long.parseLong(actionScopeId))
).collect(Collectors.toList());
val appIdListStr = (String) record.get(KEY_APP_ID_LIST);
List<String> appIdList = CustomCollectionUtils.getNoDuplicateList(appIdListStr, ",");
Expand Down Expand Up @@ -488,18 +515,27 @@ private List<String> getWhiteIPActionScopesByConditions(DSLContext dslContext, L
}

@Override
public List<CloudIPDTO> listWhiteIPByAppIds(DSLContext dslContext, Collection<Long> appIds) {
public List<CloudIPDTO> listWhiteIPByAppIds(DSLContext dslContext, Collection<Long> appIds, Long actionScopeId) {
val tWhiteIPIP = WhiteIpIp.WHITE_IP_IP.as("tWhiteIPIP");
val tWhiteIPAppRel = WhiteIpAppRel.WHITE_IP_APP_REL.as("tWhiteIPAppRel");
val tWhiteIPActionScope = WhiteIpActionScope.WHITE_IP_ACTION_SCOPE.as("tWhiteIPActionScope");
Collection<Condition> conditions = new ArrayList<>();
if (appIds != null) {
conditions.add(tWhiteIPAppRel.APP_ID.in(appIds));
}
if (actionScopeId != null) {
conditions.add(tWhiteIPActionScope.ACTION_SCOPE_ID.eq(actionScopeId));
}
val query = dslContext.select(
tWhiteIPIP.CLOUD_AREA_ID.as(KEY_CLOUD_AREA_ID),
tWhiteIPIP.IP.as(KEY_IP)
).from(tWhiteIPAppRel)
.join(tWhiteIPIP).on(tWhiteIPAppRel.RECORD_ID.eq(tWhiteIPIP.RECORD_ID))
.where(tWhiteIPAppRel.APP_ID.in(appIds));
.join(tWhiteIPActionScope).on(tWhiteIPAppRel.RECORD_ID.eq(tWhiteIPActionScope.RECORD_ID))
.where(conditions);
try {
val records = query.fetch();
if (records != null && records.size() > 0) {
if (records.size() > 0) {
return records.map(record -> {
val cloudId = (Long) record.get(KEY_CLOUD_AREA_ID);
val ip = (String) record.get(KEY_IP);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.tencent.bk.job.common.model.dto.ApplicationInfoDTO;
import com.tencent.bk.job.common.model.dto.DynamicGroupInfoDTO;
import com.tencent.bk.job.common.model.vo.HostInfoVO;
import com.tencent.bk.job.manage.common.consts.whiteip.ActionScopeEnum;
import com.tencent.bk.job.manage.model.web.request.AgentStatisticsReq;
import com.tencent.bk.job.manage.model.web.request.ipchooser.AppTopologyTreeNode;
import com.tencent.bk.job.manage.model.web.request.ipchooser.ListHostByBizTopologyNodesReq;
Expand Down Expand Up @@ -130,7 +131,7 @@ List<AppTopologyTreeNode> getAppTopologyTreeNodeDetail(String username, Long app
* @param checkIpList 待查询的 IP 列表
* @return 主机信息列表
*/
List<HostInfoVO> getHostsByIp(String username, Long appId, List<String> checkIpList);
List<HostInfoVO> getHostsByIp(String username, Long appId, ActionScopeEnum actionScope, List<String> checkIpList);

List<ApplicationInfoDTO> listAllAppsFromLocalDB();

Expand Down
Loading

0 comments on commit b6f6e1b

Please sign in to comment.