Skip to content

Commit

Permalink
Merge pull request #122 from yg3630536/feature/check-config
Browse files Browse the repository at this point in the history
* check the config
  • Loading branch information
chenhaozx committed Feb 24, 2020
2 parents d373607 + 19e88a5 commit eaf7316
Show file tree
Hide file tree
Showing 19 changed files with 170 additions and 41 deletions.
12 changes: 8 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ List caffeine = [
"com.github.ben-manes.caffeine:caffeine:2.8.0"
]

List oval = [
"net.sf.oval:oval:1.90"
]

configurations {
localDeps
}
Expand All @@ -127,12 +131,12 @@ dependencies {
localDeps 'org.projectlombok:lombok:1.18.10'
if (gradleVer.startsWith("4")) {
if (!gradle.startParameter.isOffline()) {
compile logger, lombok, apache_commons, json, mysql_driver, zxing, rpc, pdfbox, protobuf, caffeine
compile logger, lombok, apache_commons, json, mysql_driver, zxing, rpc, pdfbox, protobuf, caffeine, oval
compile("com.webank:weid-contract-java:1.2.14") {
exclude group: "org.slf4j", module: "slf4j-log4j12"
}
compile files("lib/WeDPR-Java-SDK.jar")
testCompile logger, lombok, apache_commons, json, junit, jmockit, rpc, pdfbox, protobuf, caffeine
testCompile logger, lombok, apache_commons, json, junit, jmockit, rpc, pdfbox, protobuf, caffeine, oval
} else {
compile fileTree(dir: 'dist/lib', include: '*.jar')
}
Expand All @@ -143,12 +147,12 @@ dependencies {
annotationProcessor 'org.projectlombok:lombok:1.18.10'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.10'
testCompileOnly 'org.projectlombok:lombok:1.18.10'
compile logger, apache_commons, json, mysql_driver, zxing, rpc, pdfbox, protobuf, caffeine
compile logger, apache_commons, json, mysql_driver, zxing, rpc, pdfbox, protobuf, caffeine, oval
compile("com.webank:weid-contract-java:1.2.14") {
exclude group: "org.slf4j", module: "slf4j-log4j12"
}
compile files("lib/WeDPR-Java-SDK.jar")
testCompile logger, apache_commons, json, junit, jmockit, rpc, pdfbox, protobuf, caffeine
testCompile logger, apache_commons, json, junit, jmockit, rpc, pdfbox, protobuf, caffeine, oval
} else {
compileOnly files('dist/lib/lombok-1.18.10.jar')
annotationProcessor files('dist/lib/lombok-1.18.10.jar')
Expand Down
105 changes: 104 additions & 1 deletion src/main/java/com/webank/weid/config/FiscoConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,20 @@

package com.webank.weid.config;

import java.util.ArrayList;
import java.util.List;

import lombok.Data;
import net.sf.oval.ConstraintViolation;
import net.sf.oval.Validator;
import net.sf.oval.constraint.MatchPattern;
import net.sf.oval.constraint.Min;
import net.sf.oval.constraint.NotEmpty;
import net.sf.oval.constraint.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.webank.weid.exception.WeIdBaseException;
import com.webank.weid.util.PropertyUtils;

/**
Expand All @@ -35,31 +45,109 @@
public class FiscoConfig {

private static final Logger logger = LoggerFactory.getLogger(FiscoConfig.class);

private static final Validator validator = new Validator();
// Note that all keys are appended with a colon ":" to support regex auto-loading

@NotNull(message = "the bcos.version is undefined")
@NotEmpty(message = "the value of bcos.version is null")
@MatchPattern(pattern = "[0-9]+(\\.\\w*)?", message = "the value of bcos.version is invalid")
private String version;

@NotNull(message = "the nodes is undefined")
@NotEmpty(message = "the value of nodes is null")
private String nodes;

@NotNull(message = "the weId.contractaddress is undefined")
@NotEmpty(message = "the value of weId.contractaddress is null")
private String weIdAddress;

@NotNull(message = "the cpt.contractaddress is undefined")
@NotEmpty(message = "the value of cpt.contractaddress is null")
private String cptAddress;

@NotNull(message = "the issuer.contractaddress is undefined")
@NotEmpty(message = "the value of issuer.contractaddress is null")
private String issuerAddress;

@NotNull(message = "the evidence.contractaddress is undefined")
@NotEmpty(message = "the value of evidence.contractaddress is null")
private String evidenceAddress;

@NotNull(message = "the specificissuer.contractaddress is undefined")
@NotEmpty(message = "the value of specificissuer.contractaddress is null")
private String specificIssuerAddress;

@NotNull(message = "the chain.id is undefined")
@NotEmpty(message = "the value of chain.id is null")
@MatchPattern(pattern = "\\d+", message = "the value of chain.id is invalid")
private String chainId;

@NotNull(message = "the web3sdk.timeout is undefined")
@NotEmpty(message = "the value of web3sdk.timeout is null")
@MatchPattern(pattern = "\\d+", message = "the value of web3sdk.timeout is invalid")
@Min(value = 5, message = "the the minimum value of web3sdk.timeout is 5")
private String web3sdkTimeout;

@NotNull(message = "the web3sdk.core-pool-size is undefined")
@NotEmpty(message = "the value of web3sdk.core-pool-size is null")
@MatchPattern(pattern = "\\d+", message = "the value of web3sdk.core-pool-size is invalid")
private String web3sdkCorePoolSize;

@NotNull(message = "the web3sdk.max-pool-size is undefined")
@NotEmpty(message = "the value of web3sdk.max-pool-size is null")
@MatchPattern(pattern = "\\d+", message = "the value of web3sdk.max-pool-size is invalid")
private String web3sdkMaxPoolSize;

@NotNull(message = "the web3sdk.queue-capacity is undefined")
@NotEmpty(message = "the value of web3sdk.queue-capacity is null")
@MatchPattern(pattern = "\\d+", message = "the value of web3sdk.queue-capacity is invalid")
private String web3sdkQueueSize;

@NotNull(message = "the web3sdk.keep-alive-seconds is undefined")
@NotEmpty(message = "the value of web3sdk.keep-alive-seconds is null")
@MatchPattern(pattern = "\\d+", message = "the value of web3sdk.keep-alive-seconds is invalid")
private String web3sdkKeepAliveSeconds;

@NotNull(message = "the group.id is undefined")
@NotEmpty(message = "the value of group.id is null")
@MatchPattern(pattern = "\\d+", message = "the value of group.id is invalid")
private String groupId;

@NotNull(message = "the encrypt.type is undefined")
@NotEmpty(message = "the value of encrypt.type is null")
@MatchPattern(pattern = "[0,1]", message = "the value of encrypt.type should be in [0,1]")
private String encryptType;

@NotNull(message = "the v1.ca-crt-path is undefined")
@NotEmpty(message = "the value of v1.ca-crt-path is null")
private String v1CaCrtPath;

@NotNull(message = "the v1.client-crt-password is undefined")
@NotEmpty(message = "the value of v1.client-crt-password is null")
private String v1ClientCrtPassword;

@NotNull(message = "the v1.client-key-store-path is undefined")
@NotEmpty(message = "the value of v1.client-key-store-path is null")
private String v1ClientKeyStorePath;

@NotNull(message = "the v1.key-store-password is undefined")
@NotEmpty(message = "the value of v1.key-store-password is null")
private String v1KeyStorePassword;

@NotNull(message = "the v2.ca-crt-path is undefined")
@NotEmpty(message = "the value of v2.ca-crt-path is null")
private String v2CaCrtPath;

@NotNull(message = "the v2.node-crt-path is undefined")
@NotEmpty(message = "the value of v2.node-crt-path is null")
private String v2NodeCrtPath;

@NotNull(message = "the v2.node-key-path is undefined")
@NotEmpty(message = "the value of v2.node-key-path is null")
private String v2NodeKeyPath;

@NotNull(message = "the blockchain.orgid is undefined")
@NotEmpty(message = "the value of blockchain.orgid is null")
private String currentOrgId;

/**
Expand Down Expand Up @@ -100,4 +188,19 @@ public boolean load() {
return false;
}
}

/**
* check the attribute value for FiscoConfig.
*/
public void check() {
List<ConstraintViolation> constraintViolationSet = validator.validate(this);
List<String> messageList = new ArrayList<String>();
constraintViolationSet.forEach(violationInfo -> {
messageList.add(violationInfo.getMessage());
});
if (messageList.size() > 0) {
logger.error("[FiscoConfig.check] message: {}", messageList.get(0));
throw new WeIdBaseException(messageList.get(0));
}
}
}
4 changes: 1 addition & 3 deletions src/main/java/com/webank/weid/service/BaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ public abstract class BaseService {
if (!fiscoConfig.load()) {
logger.error("[BaseService] Failed to load Fisco-BCOS blockchain node information.");
}
if (StringUtils.isEmpty(fiscoConfig.getCurrentOrgId())) {
logger.error("[BaseService] the blockchain orgId is blank.");
}
fiscoConfig.check();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public ResponseData<Boolean> registerAuthorityIssuer(RegisterAuthorityIssuerArgs
try {
return engine.addAuthorityIssuer(args);
} catch (Exception e) {
logger.error("register has error, Error Message:{}", e);
return new ResponseData<>(false, ErrorCode.AUTHORITY_ISSUER_ERROR);
}
}
Expand Down Expand Up @@ -133,7 +134,6 @@ public ResponseData<AuthorityIssuer> queryAuthorityIssuerInfo(String weId) {
}
try {
return engine.getAuthorityIssuerInfoNonAccValue(weId);

} catch (Exception e) {
logger.error("query authority issuer failed.", e);
return new ResponseData<>(null, ErrorCode.AUTHORITY_ISSUER_ERROR);
Expand Down Expand Up @@ -430,6 +430,7 @@ private ErrorCode checkAuthorityIssuerArgsValidity(AuthorityIssuer args) {
return ErrorCode.AUTHORITY_ISSUER_ACCVALUE_ILLEAGAL;
}
} catch (Exception e) {
logger.error("accValue is invalid.", e);
return ErrorCode.AUTHORITY_ISSUER_ACCVALUE_ILLEAGAL;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ private static ErrorCode verifyContentInner(CredentialPojo credential, String pu
innerCredentialList = (ArrayList) credential.getClaim().get("credentialList");
}
} catch (Exception e) {
logger.error("the credential claim data illegal.", e);
return ErrorCode.CREDENTIAL_CLAIM_DATA_ILLEGAL;
}
for (Object innerCredentialObject : innerCredentialList) {
Expand All @@ -527,7 +528,7 @@ private static ErrorCode verifyContentInner(CredentialPojo credential, String pu
return errorCode;
}
} catch (Exception e) {
logger.error("Failed to convert credentialPojo to object: " + e.getMessage());
logger.error("Failed to convert credentialPojo to object.", e);
return ErrorCode.ILLEGAL_INPUT;
}
}
Expand Down Expand Up @@ -563,7 +564,7 @@ private static ErrorCode verifySingleSignedCredential(
}
}
} catch (Exception e) {
logger.error("Failed to convert credentialPojo: " + e.getMessage());
logger.error("Failed to convert credentialPojo: " + e.getMessage(), e);
return ErrorCode.CREDENTIAL_CLAIM_DATA_ILLEGAL;
}
rawData = CredentialPojoUtils.getEmbeddedCredentialThumbprintWithoutSig(credentialList);
Expand Down Expand Up @@ -648,7 +649,7 @@ private static ErrorCode verifyCptFormat(Integer cptId, Map<String, Object> clai
return ErrorCode.SUCCESS;
} catch (Exception e) {
logger.error(
"Generic error occurred during verify cpt format when verifyCredential: " + e);
"Generic error occurred during verify cpt format when verifyCredential: ", e);
return ErrorCode.CREDENTIAL_ERROR;
}
}
Expand Down Expand Up @@ -1029,7 +1030,7 @@ private List<Map> trimCredentialList(List<CredentialPojo> credentialList) {
try {
trimmedCredentialMapList.add(DataToolUtils.objToMap(credAlive));
} catch (Exception e) {
logger.error("Failed to convert Credential to map structure.");
logger.error("Failed to convert Credential to map structure.", e);
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private String convertFileToString(File file) {
try {
return Files.asByteSource(file).asCharSource(Charsets.UTF_8).read();
} catch (Exception e) {
logger.error("Failed to load file as String.");
logger.error("Failed to load file as String.", e);
return StringUtils.EMPTY;
}
}
Expand All @@ -178,7 +178,7 @@ private ResponseData<String> getHashValue(Hashable object) {
}
return new ResponseData<>(hashValue, ErrorCode.SUCCESS);
} catch (Exception e) {
logger.error("Input Object type unsupported: " + object.getClass().getName());
logger.error("Input Object type unsupported: {}", object.getClass().getName(), e);
return new ResponseData<>(StringUtils.EMPTY, ErrorCode.ILLEGAL_INPUT);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ public ResponseData<String> getWeIdDocumentJson(String weId) {
try {
weIdDocument = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result);
} catch (Exception e) {
logger.error("write object to String fail.", e);
return new ResponseData<>(
StringUtils.EMPTY,
ErrorCode.getTypeByErrorCode(responseData.getErrorCode())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected static <T> T reloadContract(
} catch (Exception e) {
logger.error("load contract :{} failed. Error message is :{}",
cls.getSimpleName(), e.getMessage(), e);
throw new LoadContractException();
throw new LoadContractException(e);
}

if (contract == null) {
Expand Down Expand Up @@ -117,11 +117,11 @@ protected static <T> T getContractService(String contractAddress, Class<T> cls)
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
logger.error("load contract :{} failed. Error message is :{}",
cls.getSimpleName(), e.getMessage(), e);
throw new LoadContractException();
throw new LoadContractException(e);
} catch (Exception e) {
logger.error("load contract Exception:{} failed. Error message is :{}",
cls.getSimpleName(), e.getMessage(), e);
throw new LoadContractException();
throw new LoadContractException(e);
}

if (contract == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ public ResponseData<WeIdDocument> getWeIdDocument(String weId) {
logger.error("Set weId service failed. Error message :{}", e);
return new ResponseData<>(null, ErrorCode.TRANSACTION_EXECUTE_ERROR);
} catch (TimeoutException e) {
logger.error("Set weId service timeout. Error message :{}", e);
return new ResponseData<>(null, ErrorCode.TRANSACTION_TIMEOUT);
} catch (ResolveAttributeException e) {
logger.error("[getWeIdDocument]: resolveTransaction failed. "
Expand Down Expand Up @@ -498,6 +499,7 @@ public ResponseData<Boolean> createWeId(
logger.error("Set public key failed. Error message :{}", e);
return new ResponseData<Boolean>(false, ErrorCode.TRANSACTION_EXECUTE_ERROR);
} catch (TimeoutException e) {
logger.error("Set public key timeout. Error message :{}", e);
return new ResponseData<Boolean>(false, ErrorCode.TRANSACTION_TIMEOUT);
}
}
Expand Down Expand Up @@ -542,6 +544,7 @@ public ResponseData<Boolean> setAttribute(
logger.error("Set public key failed. Error message :{}", e);
return new ResponseData<Boolean>(false, ErrorCode.TRANSACTION_EXECUTE_ERROR);
} catch (TimeoutException e) {
logger.error("Set public key timeout. Error message :{}", e);
return new ResponseData<Boolean>(false, ErrorCode.TRANSACTION_TIMEOUT);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public ResponseData<CptBaseInfo> updateCpt(int cptId, String address, String cpt
}
return response;
} catch (Exception e) {
logger.error("[updateCpt] cptId limited max value. cptId:{}", cptId);
logger.error("[updateCpt] cptId limited max value. cptId:{}", cptId, e);
return new ResponseData<>(null, ErrorCode.UNKNOW_ERROR);
}
}
Expand Down Expand Up @@ -421,8 +421,9 @@ public ResponseData<CredentialTemplateEntity> queryCredentialTemplate(Integer cp
.intValue();
} catch (Exception e1) {
logger.error(
"[queryCredentialTemplate] get block number for cpt : {} failed.",
cptId);
"[queryCredentialTemplate] get block number for cpt : {} failed. Error message:{}",
cptId,
e1);
return new ResponseData<CredentialTemplateEntity>(null, ErrorCode.UNKNOW_ERROR);
}
if (blockNum == 0) {
Expand All @@ -437,7 +438,7 @@ public ResponseData<CredentialTemplateEntity> queryCredentialTemplate(Integer cp
.getBlockByNumber(new DefaultBlockParameterNumber(blockNum), true).send();
} catch (IOException e) {
logger.error(
"[queryCredentialTemplate]get block by number :{} failed. Error message:{}",
"[queryCredentialTemplate] get block by number :{} failed. Error message:{}",
blockNum,
e);
}
Expand Down Expand Up @@ -493,6 +494,7 @@ public ResponseData<CredentialTemplateEntity> queryCredentialTemplate(Integer cp

credentialTemplateStorage.setCredentialSchema(attributes);
} catch (Exception e) {
logger.error("[queryCredentialTemplate] query credential template has error.", e);
return new ResponseData<CredentialTemplateEntity>(null, ErrorCode.UNKNOW_ERROR);
}

Expand Down

0 comments on commit eaf7316

Please sign in to comment.