Skip to content

Commit

Permalink
feature: 提供文件源管理、凭据管理相关ESB API TencentBlueKing#92
Browse files Browse the repository at this point in the history
增加新建、更新凭据API
  • Loading branch information
jsonwan committed Jul 18, 2021
1 parent a64c0b4 commit 40fffa3
Show file tree
Hide file tree
Showing 25 changed files with 779 additions and 94 deletions.
1 change: 1 addition & 0 deletions src/backend/commons/common-web/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies {
implementation project(":commons:common-spring-ext")
implementation project(":commons:esb-sdk")
implementation project(":commons:common-iam")
implementation 'io.springfox:springfox-swagger2'
implementation "org.hibernate.validator:hibernate-validator"
implementation('jakarta.validation:jakarta.validation-api')
implementation "org.springframework.cloud:spring-cloud-starter-sleuth"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
/*
* 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.common.web.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.tencent.bk.job.common.constant.ErrorCode;
import com.tencent.bk.job.common.exception.ServiceException;
import com.tencent.bk.job.common.i18n.service.MessageI18nService;
import com.tencent.bk.job.common.iam.model.AuthResult;
import com.tencent.bk.job.common.model.ValidateResult;
import com.tencent.bk.job.common.model.error.ErrorDetail;
import com.tencent.bk.job.common.model.permission.AuthResultVO;
import com.tencent.bk.job.common.util.ApplicationContextRegister;
import com.tencent.bk.job.common.util.JobContextUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

@Slf4j
@Getter
@Setter
@ToString
@NoArgsConstructor
@ApiModel("服务间调用通用返回结构")
public class InnerServiceResponse<T> {
public static final Integer SUCCESS_CODE = 0;
public static final Integer COMMON_FAIL_CODE = 1;
private static volatile MessageI18nService i18nService;

@ApiModelProperty("是否成功")
private boolean success;

@ApiModelProperty("返回码")
private Integer code;

@ApiModelProperty("错误信息")
private String errorMsg;

@ApiModelProperty("请求成功返回的数据")
private T data;

@ApiModelProperty("请求 ID")
private String requestId;

@ApiModelProperty("鉴权结果,当返回码为1238001时,该字段有值")
@JsonProperty("authResult")
private AuthResult authResult;

@ApiModelProperty("错误详情")
@JsonProperty("errorDetail")
private ErrorDetail errorDetail;

public InnerServiceResponse(Integer code, String errorMsg, T data) {
this.code = code;
this.errorMsg = errorMsg;
this.data = data;
this.requestId = JobContextUtil.getRequestId();
}

private static MessageI18nService getI18nService() {
if (i18nService == null) {
synchronized (InnerServiceResponse.class) {
if (i18nService == null) {
i18nService = ApplicationContextRegister.getBean(MessageI18nService.class);
}
}
}
return i18nService;
}

public static <T> InnerServiceResponse<T> buildSuccessResp(T data) {
InnerServiceResponse<T> resp = new InnerServiceResponse<>(SUCCESS_CODE, null, data);
resp.success = true;
return resp;
}

public static <T> InnerServiceResponse<T> buildAuthFailResp(AuthResult authResult) {
MessageI18nService i18nService = getI18nService();
String message = null;
if (i18nService != null) {
message = i18nService.getI18n(String.valueOf(ErrorCode.USER_NO_PERMISSION_COMMON));
} else {
log.warn("cannot find available i18nService");
}
InnerServiceResponse<T> resp = new InnerServiceResponse<T>(ErrorCode.USER_NO_PERMISSION_COMMON, message, null);
resp.success = false;
resp.authResult = authResult;
return resp;
}

public static <T> InnerServiceResponse<T> buildCommonFailResp(String msg) {
InnerServiceResponse<T> resp = new InnerServiceResponse<>(COMMON_FAIL_CODE, msg, null);
resp.success = false;
return resp;
}

public static <T> InnerServiceResponse<T> buildCommonFailResp(Integer errorCode, String msg) {
InnerServiceResponse<T> resp = new InnerServiceResponse<>(errorCode, msg, null);
resp.success = false;
return resp;
}

public static <T> InnerServiceResponse<T> buildCommonFailResp(Integer errorCode) {
try {
getI18nService();
} catch (Exception e) {
log.warn("cannot get i18nService from spring context");
}
String errorMsg = null;
if (i18nService != null) {
errorMsg = i18nService.getI18n(errorCode.toString());
}
InnerServiceResponse<T> resp = new InnerServiceResponse<>(errorCode, errorMsg, null);
resp.success = false;
return resp;
}

public static <T> InnerServiceResponse<T> buildCommonFailResp(Integer errorCode, MessageI18nService i18nService) {
String errorMsg = i18nService.getI18n(String.valueOf(errorCode));
if (StringUtils.isEmpty(errorMsg)) {
errorMsg = String.valueOf(errorCode);
}
InnerServiceResponse<T> resp = new InnerServiceResponse<>(errorCode, errorMsg, null);
resp.success = false;
return resp;
}

public static <T> InnerServiceResponse<T> buildCommonFailResp(Integer errorCode, Object[] params) {
getI18nService();
return buildCommonFailResp(errorCode, params, i18nService);
}

public static <T> InnerServiceResponse<T> buildCommonFailResp(Integer errorCode, Object[] params,
MessageI18nService i18nService) {
String errorMsg = "";
if (params != null && params.length > 0) {
errorMsg = i18nService.getI18nWithArgs(String.valueOf(errorCode), params);
} else {
errorMsg = i18nService.getI18n(String.valueOf(errorCode));
}
if (StringUtils.isEmpty(errorMsg)) {
errorMsg = String.valueOf(errorCode);
}
InnerServiceResponse<T> resp = new InnerServiceResponse<>(errorCode, errorMsg, null);
resp.success = false;
return resp;
}

public static <T> InnerServiceResponse<T> buildCommonFailResp(ServiceException e, MessageI18nService i18nService) {
log.info("exception: {}|{}|{}", e.getErrorCode(), e.getErrorMsg(), e.getErrorParams());
int errorCode = e.getErrorCode();
String errorMsg = e.getErrorMsg();
if (StringUtils.isEmpty(errorMsg)) {
errorMsg = i18nService.getI18nWithArgs(String.valueOf(errorCode), e.getErrorParams());
log.info("{}", errorMsg);
}
if (StringUtils.isEmpty(errorMsg)) {
errorMsg = String.valueOf(errorCode);
}
return new InnerServiceResponse<>(e.getErrorCode(), errorMsg, null);
}

public static <T> InnerServiceResponse<T> buildValidateFailResp(MessageI18nService i18nService,
ValidateResult validateResult) {
if (validateResult.getErrorParams() != null && validateResult.getErrorParams().length > 0) {
return InnerServiceResponse.buildCommonFailResp(validateResult.getErrorCode(),
i18nService.getI18nWithArgs(String.valueOf(validateResult.getErrorCode()),
validateResult.getErrorParams()));
} else {
return InnerServiceResponse.buildCommonFailResp(validateResult.getErrorCode(),
i18nService.getI18n(String.valueOf(validateResult.getErrorCode())));
}
}

public static <T> InnerServiceResponse<T> buildCommonFailResp(int errorCode, ErrorDetail errorDetail,
MessageI18nService i18nService) {
String errorMsg = i18nService.getI18n(String.valueOf(errorCode));
InnerServiceResponse<T> esbResp = new InnerServiceResponse<>(errorCode, errorMsg, null);
esbResp.setErrorDetail(errorDetail);
return esbResp;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

package com.tencent.bk.job.file_gateway.service;

import com.tencent.bk.job.ticket.model.credential.CommonCredentialDTO;
import com.tencent.bk.job.ticket.model.credential.CommonCredential;


public interface CredentialService {
Expand All @@ -34,5 +34,5 @@ public interface CredentialService {
* @param id
* @return
*/
CommonCredentialDTO getCredentialById(Long appId, String id);
CommonCredential getCredentialById(Long appId, String id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import com.tencent.bk.job.common.util.json.JsonUtils;
import com.tencent.bk.job.file_gateway.client.ServiceCredentialResourceClient;
import com.tencent.bk.job.file_gateway.service.CredentialService;
import com.tencent.bk.job.ticket.model.credential.CommonCredentialDTO;
import com.tencent.bk.job.ticket.model.credential.CommonCredential;
import com.tencent.bk.job.ticket.model.inner.resp.ServiceCredentialDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -46,19 +46,18 @@ public CredentialServiceImpl(ServiceCredentialResourceClient credentialService)
}

@Override
public CommonCredentialDTO getCredentialById(Long appId, String id) {
public CommonCredential getCredentialById(Long appId, String id) {
ServiceResponse<ServiceCredentialDTO> credentialServiceResponse = credentialService.getCredentialById(appId,
id);
ServiceCredentialDTO credentialDTO = credentialServiceResponse.getData();
if (credentialDTO == null) {
return null;
}
try {
CommonCredentialDTO commonCredentialDTO = JsonUtils.fromJson(credentialDTO.getValue(),
CommonCredentialDTO.class);
CommonCredential commonCredential = credentialDTO.getCredential();
// Type补全
commonCredentialDTO.setType(credentialDTO.getType());
return commonCredentialDTO;
commonCredential.setType(credentialDTO.getType());
return commonCredential;
} catch (Exception e) {
log.error("credential not valid:{}", credentialDTO);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import com.tencent.bk.job.file_gateway.model.dto.FileSourceDTO;
import com.tencent.bk.job.file_gateway.model.dto.FileWorkerDTO;
import com.tencent.bk.job.file_gateway.service.CredentialService;
import com.tencent.bk.job.ticket.model.credential.CommonCredentialDTO;
import com.tencent.bk.job.ticket.model.credential.CommonCredential;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
Expand All @@ -55,14 +55,14 @@ protected String fillBaseReqGetUrl(BaseReq req, Long appId, FileWorkerDTO fileWo
FileSourceDTO fileSourceDTO, String url) {
String completeUrl = getCompleteUrl(fileWorkerDTO, url);
String credentialId = fileSourceDTO.getCredentialId();
CommonCredentialDTO commonCredentialDTO = null;
CommonCredential commonCredential = null;
if (StringUtils.isNotBlank(credentialId)) {
commonCredentialDTO = credentialService.getCredentialById(fileSourceDTO.getAppId(),
commonCredential = credentialService.getCredentialById(fileSourceDTO.getAppId(),
fileSourceDTO.getCredentialId());
}
if (commonCredentialDTO != null) {
req.setCredential(commonCredentialDTO);
log.debug("Credential of id {} is {}", credentialId, commonCredentialDTO);
if (commonCredential != null) {
req.setCredential(commonCredential);
log.debug("Credential of id {} is {}", credentialId, commonCredential);
} else if (StringUtils.isNotBlank(credentialId)) {
log.warn("Cannot find credential by id {}", credentialId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

package com.tencent.bk.job.file.worker.model.req;

import com.tencent.bk.job.ticket.model.credential.CommonCredentialDTO;
import com.tencent.bk.job.ticket.model.credential.CommonCredential;
import lombok.Data;
import lombok.EqualsAndHashCode;

Expand All @@ -38,7 +38,7 @@ public class BaseReq {
String fileSourceTypeCode;

// 凭据信息
CommonCredentialDTO credential;
CommonCredential credential;

// 文件源信息Map
Map<String, Object> fileSourceInfoMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
package com.tencent.bk.job.file.worker.artifactory.service;

import com.tencent.bk.job.file.worker.model.req.BaseReq;
import com.tencent.bk.job.ticket.model.credential.CommonCredentialDTO;
import com.tencent.bk.job.ticket.model.credential.CommonCredential;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand All @@ -43,7 +43,7 @@ public ArtifactoryBaseService(MeterRegistry meterRegistry) {
}

public ArtifactoryRemoteClient getArtifactoryClientFromBaseReq(BaseReq req) {
CommonCredentialDTO credential = req.getCredential();
CommonCredential credential = req.getCredential();
Map<String, Object> fileSourceInfoMap = req.getFileSourceInfoMap();
return new ArtifactoryRemoteClient((String) fileSourceInfoMap.get("baseUrl"), credential.getUsername(),
credential.getPassword(), meterRegistry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import com.tencent.bk.job.file.worker.cos.JobTencentInnerCOSClient;
import com.tencent.bk.job.file.worker.model.req.BaseReq;
import com.tencent.bk.job.ticket.model.credential.CommonCredentialDTO;
import com.tencent.bk.job.ticket.model.credential.CommonCredential;
import org.springframework.stereotype.Service;

import java.util.Map;
Expand All @@ -37,7 +37,7 @@ public class COSBaseService {
public JobTencentInnerCOSClient getCOSClientFromBaseReq(BaseReq req) {
// endPointDomain
// appId
CommonCredentialDTO credential = req.getCredential();
CommonCredential credential = req.getCredential();
Map<String, Object> fileSourceInfoMap = req.getFileSourceInfoMap();
return new JobTencentInnerCOSClient(credential.getAccessKey(), credential.getSecretKey(),
getEndPointDomain(req), fileSourceInfoMap.get("appId").toString());
Expand Down
Loading

0 comments on commit 40fffa3

Please sign in to comment.