Skip to content

Commit

Permalink
Merge pull request #200 from yg3630536/feature/optimization-verify
Browse files Browse the repository at this point in the history
* Optimize error code and PropertiesService
  • Loading branch information
junqizhang-dev committed May 20, 2020
2 parents 82e5006 + 3322239 commit 257f73d
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 38 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/webank/weid/constant/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ public enum ErrorCode {
CREDENTIAL_NOT_SUPPORT_SELECTIVE_DISCLOSURE(100440,
"the credential does not support selective disclosure."),

/**
* credential verify fail.
*/
CREDENTIAL_VERIFY_FAIL(100441, "credential verify fail."),

/**
* Authorization WeIDs: from and to must be different.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ private static ErrorCode verifySingleSignedCredential(
return ErrorCode.CREDENTIAL_SIGNATURE_BROKEN;
}
if (!result) {
return ErrorCode.CREDENTIAL_SIGNATURE_BROKEN;
return ErrorCode.CREDENTIAL_VERIFY_FAIL;
}
return ErrorCode.SUCCESS;
}
Expand Down Expand Up @@ -730,7 +730,7 @@ private static ResponseData<Boolean> verifyZkpCredential(CredentialPojo credenti
return new ResponseData<Boolean>(true, ErrorCode.SUCCESS);
}

return new ResponseData<Boolean>(false, ErrorCode.CREDENTIAL_ERROR);
return new ResponseData<Boolean>(false, ErrorCode.CREDENTIAL_VERIFY_FAIL);
}

private static Boolean isZkpCredential(CredentialPojo credential) {
Expand Down Expand Up @@ -933,7 +933,7 @@ private static ResponseData<Boolean> verifyLiteCredential(
return new ResponseData<Boolean>(false, ErrorCode.CREDENTIAL_SIGNATURE_BROKEN);
}
if (!result) {
return new ResponseData<Boolean>(false, ErrorCode.CREDENTIAL_SIGNATURE_BROKEN);
return new ResponseData<Boolean>(false, ErrorCode.CREDENTIAL_VERIFY_FAIL);
}
return new ResponseData<Boolean>(true, ErrorCode.SUCCESS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ private ResponseData<Boolean> verifySignature(
DataToolUtils
.verifySignature(rawData, signatureData, new BigInteger(publicKey));
if (!result) {
return new ResponseData<>(false, ErrorCode.CREDENTIAL_SIGNATURE_BROKEN);
return new ResponseData<>(false, ErrorCode.CREDENTIAL_VERIFY_FAIL);
}
return new ResponseData<>(true, ErrorCode.SUCCESS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.webank.weid.constant.DataDriverConstant;
import com.webank.weid.constant.ErrorCode;
Expand All @@ -22,7 +25,9 @@
*
*/
public class PropertiesService extends InnerService {


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

private static PropertiesService instance;

// 存放内置配置得对象
Expand Down Expand Up @@ -70,7 +75,7 @@ public void run() {
}
};
timer = new Timer();
timer.scheduleAtFixedRate(task, 0, intevalPeriod);
timer.scheduleAtFixedRate(task, intevalPeriod, intevalPeriod);
}

private static void reStartMonitor() {
Expand Down Expand Up @@ -124,27 +129,83 @@ public Map<String, String> getAllProperty() {
* 更新properties缓存数据,后续可以用于定时更新.
*/
private void load() {
logger.info("[load] begin load properties.");
synchronized (properties) {
Map<String, String> query = query();
properties.clear();
properties.putAll(query);
if (instance != null) {
Long intevalPeriod = getIntevalPeriod();
if (intevalPeriod != null
&& intevalPeriod.longValue() != prevIntevalPeriod.longValue()) {
reStartMonitor();
try {
Map<String, String> query = query();
processUpdate(query);
if (instance != null) {
Long intevalPeriod = getIntevalPeriod();
if (intevalPeriod != null
&& intevalPeriod.longValue() != prevIntevalPeriod.longValue()) {
reStartMonitor();
}
}
} catch (Throwable e) {
logger.error("[load] the properties load error.", e);
}
}
}

/*
* properties 中存放的是现内存中的所有配置数据
* queryMap 中为数据库中查询出来最新的配置数据
* 此处更新内存中的数据,并日志输出,需要删除的key, 新增的key,更新的key
*
* 更新过程: 先把更新数据库中的key全量放入内存中,然后处理需要删除的key
*/
private void processUpdate(Map<String, String> queryMap) {
// 判断两个map是否一致,如果一致则不用后续处理
if (queryMap.equals(properties)) {
logger.info("[processUpdate] configuration not changed.");
return;
}
Set<String> delKey = getDeleteKey(queryMap);
if (delKey.size() > 0) {
logger.info("[processUpdate] del key = {}.", delKey);
}
Set<String> updateKey = getUpdateKey(queryMap);
if (updateKey.size() > 0) {
logger.info("[processUpdate] update key = {}.", updateKey);
print(queryMap, updateKey);
}
properties.putAll(queryMap);
delKey.stream().forEach(key -> properties.remove(key));
logger.info("[processUpdate] configuration change complete.");
}

private void print(Map<String, String> queryMap, Set<String> keys) {
// 日志输出变化项, 此处未来需要对隐秘数据进行过滤
keys.stream().forEach(key -> logger.info(
"[processUpdate] key: {}, value: {} ---> {}",
key,
properties.get(key),
queryMap.get(key)
));
}

private Set<String> getDeleteKey(Map<String, String> queryMap) {
return properties.entrySet()
.stream()
.filter(entry -> !queryMap.containsKey(entry.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)).keySet();
}

private Set<String> getUpdateKey(Map<String, String> queryMap) {
return queryMap.entrySet()
.stream()
.filter(entry -> (properties.containsKey(entry.getKey())
&& !StringUtils.equals(entry.getValue(), properties.get(entry.getKey()))))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)).keySet();
}

/**
* 向数据库中添加配置集合.
*
* @param data 存入的配置数据
* @return 返回添加结果
*/
public boolean addProperties(Map<String, String> data) {
public boolean saveProperties(Map<String, String> data) {
synchronized (properties) {
Map<String, String> query = this.query();
query.putAll(data);
Expand All @@ -168,9 +229,7 @@ public synchronized boolean removeProperties(Set<String> data) {
// 查询数据库中得配置
Map<String, String> query = this.query();
// 删除对应的key
for (String string : data) {
query.remove(string);
}
data.stream().forEach(key -> query.remove(key));
// 更新到数据库中
if (this.save(query)) {
// 同步配置到内存
Expand All @@ -187,6 +246,14 @@ private Map<String, String> query() {
if (StringUtils.isNotBlank(responseData.getResult())) {
map = DataToolUtils.deserialize(responseData.getResult(), Map.class);
}
// 处理value为null的转换成空字符串, 因为ConcurrentHashMap中不允许null值存在
map.entrySet().stream().forEach(
entry -> {
if (entry.getValue() == null) {
entry.setValue(StringUtils.EMPTY);
}
}
);
return map;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/webank/weid/util/DataToolUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ public static ErrorCode verifySecp256k1SignatureFromWeId(
}
}
if (!result) {
return ErrorCode.CREDENTIAL_ISSUER_MISMATCH;
return ErrorCode.CREDENTIAL_VERIFY_FAIL;
}
} catch (Exception e) {
logger.error("some exceptions occurred in signature verification", e);
Expand Down Expand Up @@ -943,7 +943,7 @@ public static ErrorCode verifySignatureFromWeId(
}
}
if (!result) {
return ErrorCode.CREDENTIAL_ISSUER_MISMATCH;
return ErrorCode.CREDENTIAL_VERIFY_FAIL;
}
} catch (SignatureException e) {
logger.error("some exceptions occurred in signature verification", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ public void testCreateCredential_priKeyNotMatch() {
Assert.assertNotNull(response.getResult());

ResponseData<Boolean> verify = credentialService.verify(response.getResult());
Assert.assertEquals(ErrorCode.CREDENTIAL_ISSUER_MISMATCH.getCode(),
Assert.assertEquals(ErrorCode.CREDENTIAL_VERIFY_FAIL.getCode(),
verify.getErrorCode().intValue());
}

Expand All @@ -417,7 +417,7 @@ public void testCreateCredential_sdkPriKey() {
Assert.assertEquals(ErrorCode.SUCCESS.getCode(), response.getErrorCode().intValue());

ResponseData<Boolean> verify = credentialService.verify(response.getResult());
Assert.assertEquals(ErrorCode.CREDENTIAL_ISSUER_MISMATCH.getCode(),
Assert.assertEquals(ErrorCode.CREDENTIAL_VERIFY_FAIL.getCode(),
verify.getErrorCode().intValue());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void testVerifyCredential_contentIsOtherString() {
LogUtil.info(logger, "verifyCredential", response);

credential.setContext(context);
Assert.assertEquals(ErrorCode.CREDENTIAL_ISSUER_MISMATCH.getCode(),
Assert.assertEquals(ErrorCode.CREDENTIAL_VERIFY_FAIL.getCode(),
response.getErrorCode().intValue());
Assert.assertEquals(false, response.getResult());
}
Expand Down Expand Up @@ -151,7 +151,7 @@ public void testVerifyCredential_cptIdAndClaimDifferent() {
LogUtil.info(logger, "verifyCredential", response);

credential.setCptId(cptId);
Assert.assertEquals(ErrorCode.CREDENTIAL_ISSUER_MISMATCH.getCode(),
Assert.assertEquals(ErrorCode.CREDENTIAL_VERIFY_FAIL.getCode(),
response.getErrorCode().intValue());
Assert.assertEquals(false, response.getResult());
}
Expand Down Expand Up @@ -258,7 +258,7 @@ public void testVerifyCredential_otherIssuer() {
LogUtil.info(logger, "verifyCredential", response);

credential.setIssuer(issuer);
Assert.assertEquals(ErrorCode.CREDENTIAL_ISSUER_MISMATCH.getCode(),
Assert.assertEquals(ErrorCode.CREDENTIAL_VERIFY_FAIL.getCode(),
response.getErrorCode().intValue());
Assert.assertEquals(false, response.getResult());
}
Expand Down Expand Up @@ -476,7 +476,7 @@ public void testVerifyCredentialCase24() {
ResponseData<Boolean> response = super.verifyCredential(credentialWrapper.getCredential());
LogUtil.info(logger, "verifyCredential", response);

Assert.assertEquals(ErrorCode.CREDENTIAL_ISSUER_MISMATCH.getCode(),
Assert.assertEquals(ErrorCode.CREDENTIAL_VERIFY_FAIL.getCode(),
response.getErrorCode().intValue());
Assert.assertEquals(false, response.getResult());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void testVerifyCredentialWithSpecifiedPubKeyCase2() {
credentialService.verifyCredentialWithSpecifiedPubKey(credentialWrapper, weIdPublicKey);
LogUtil.info(logger, "verifyCredentialWithSpecifiedPubKey", response);

Assert.assertEquals(ErrorCode.CREDENTIAL_SIGNATURE_BROKEN.getCode(),
Assert.assertEquals(ErrorCode.CREDENTIAL_VERIFY_FAIL.getCode(),
response.getErrorCode().intValue());
Assert.assertEquals(false, response.getResult());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void testVerifyCredential_contentError() {
ResponseData<Boolean> response = super.verifyCredentialPojo(copyCredentialPojo);
LogUtil.info(logger, "verifyCredential", response);

Assert.assertEquals(ErrorCode.CREDENTIAL_ISSUER_MISMATCH.getCode(),
Assert.assertEquals(ErrorCode.CREDENTIAL_VERIFY_FAIL.getCode(),
response.getErrorCode().intValue());
Assert.assertEquals(false, response.getResult());
}
Expand All @@ -153,7 +153,7 @@ public void testVerifyCredential_otherCptId() {
ResponseData<Boolean> response = super.verifyCredentialPojo(copyCredentialPojo);
LogUtil.info(logger, "verifyCredential", response);

Assert.assertEquals(ErrorCode.CREDENTIAL_ISSUER_MISMATCH.getCode(),
Assert.assertEquals(ErrorCode.CREDENTIAL_VERIFY_FAIL.getCode(),
response.getErrorCode().intValue());
Assert.assertEquals(false, response.getResult());
}
Expand Down Expand Up @@ -202,7 +202,7 @@ public void testVerifyCredential_OtherId() {
ResponseData<Boolean> response = super.verifyCredentialPojo(copyCredentialPojo);
LogUtil.info(logger, "verifyCredential", response);

Assert.assertEquals(ErrorCode.CREDENTIAL_ISSUER_MISMATCH.getCode(),
Assert.assertEquals(ErrorCode.CREDENTIAL_VERIFY_FAIL.getCode(),
response.getErrorCode().intValue());
Assert.assertEquals(false, response.getResult());
}
Expand Down Expand Up @@ -253,7 +253,7 @@ public void testVerifyCredential_credentialPojoIssuerOther() {
ResponseData<Boolean> response = super.verifyCredentialPojo(copyCredentialPojo);
LogUtil.info(logger, "verifyCredential", response);

Assert.assertEquals(ErrorCode.CREDENTIAL_ISSUER_MISMATCH.getCode(),
Assert.assertEquals(ErrorCode.CREDENTIAL_VERIFY_FAIL.getCode(),
response.getErrorCode().intValue());
Assert.assertEquals(false, response.getResult());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void testVerifyCredential_contentError() {
copyCredentialPojo);
LogUtil.info(logger, "testVerifyCredentialByPubKey", response);

Assert.assertEquals(ErrorCode.CREDENTIAL_SIGNATURE_BROKEN.getCode(),
Assert.assertEquals(ErrorCode.CREDENTIAL_VERIFY_FAIL.getCode(),
response.getErrorCode().intValue());
Assert.assertEquals(false, response.getResult());
}
Expand All @@ -163,7 +163,7 @@ public void testVerifyCredential_otherCptId() {
copyCredentialPojo);
LogUtil.info(logger, "testVerifyCredentialByPubKey", response);

Assert.assertEquals(ErrorCode.CREDENTIAL_SIGNATURE_BROKEN.getCode(),
Assert.assertEquals(ErrorCode.CREDENTIAL_VERIFY_FAIL.getCode(),
response.getErrorCode().intValue());
Assert.assertEquals(false, response.getResult());
}
Expand Down Expand Up @@ -215,7 +215,7 @@ public void testVerifyCredential_OtherId() {
copyCredentialPojo);
LogUtil.info(logger, "testVerifyCredentialByPubKey", response);

Assert.assertEquals(ErrorCode.CREDENTIAL_SIGNATURE_BROKEN.getCode(),
Assert.assertEquals(ErrorCode.CREDENTIAL_VERIFY_FAIL.getCode(),
response.getErrorCode().intValue());
Assert.assertEquals(false, response.getResult());
}
Expand Down Expand Up @@ -269,7 +269,7 @@ public void testVerifyCredential_credentialPojoIssuerOther() {
copyCredentialPojo);
LogUtil.info(logger, "verifyCredential", response);

Assert.assertEquals(ErrorCode.CREDENTIAL_SIGNATURE_BROKEN.getCode(),
Assert.assertEquals(ErrorCode.CREDENTIAL_VERIFY_FAIL.getCode(),
response.getErrorCode().intValue());
Assert.assertEquals(false, response.getResult());
}
Expand All @@ -286,7 +286,7 @@ public void testVerifyCredential_otherIssuer() {
credentialPojo);
LogUtil.info(logger, "verifyCredential", response);

Assert.assertEquals(ErrorCode.CREDENTIAL_SIGNATURE_BROKEN.getCode(),
Assert.assertEquals(ErrorCode.CREDENTIAL_VERIFY_FAIL.getCode(),
response.getErrorCode().intValue());
Assert.assertEquals(false, response.getResult());
}
Expand Down Expand Up @@ -619,7 +619,7 @@ public void testVerifyCredential_invalidIssuer() {
copyCredentialPojo);
LogUtil.info(logger, "verifyCredential", response);

Assert.assertEquals(ErrorCode.CREDENTIAL_SIGNATURE_BROKEN.getCode(),
Assert.assertEquals(ErrorCode.CREDENTIAL_VERIFY_FAIL.getCode(),
response.getErrorCode().intValue());
Assert.assertEquals(false, response.getResult());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void testVerifyCredentialWithSpecifiedPubKeyCase2() {
credentialService.verifyCredentialWithSpecifiedPubKey(credentialWrapper, weIdPublicKey);
LogUtil.info(logger, "verifyCredentialWithSpecifiedPubKey", response);

Assert.assertEquals(ErrorCode.CREDENTIAL_SIGNATURE_BROKEN.getCode(),
Assert.assertEquals(ErrorCode.CREDENTIAL_VERIFY_FAIL.getCode(),
response.getErrorCode().intValue());
Assert.assertEquals(false, response.getResult());
}
Expand Down

0 comments on commit 257f73d

Please sign in to comment.