Skip to content

Commit

Permalink
* table extensions for default storage
Browse files Browse the repository at this point in the history
* optimization the domain configure
* support the timeout for data
  • Loading branch information
yanggang-JV committed Aug 30, 2019
1 parent 6027805 commit 4000fd8
Show file tree
Hide file tree
Showing 21 changed files with 544 additions and 905 deletions.
565 changes: 0 additions & 565 deletions docs/zh_CN/docs/weidentity-java-sdk-doc.rst

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,14 @@ public final class DataDriverConstant {
public static final String POOL_NUM_TEST_PER_ER_DEFAULT_VALUE = "5";
public static final String POOL_MIN_EITM_DEFAULT_VALUE = "1800000";

/**
* 系统默认的domain.
*/
public static final String DOMAIN_DEFAULT = "domain.default";
public static final String DOMAIN_DEFAULT_TIMEOUT = "domain.default.timeout";

/**
* 系统domain之私钥存储domainKey.
*/
public static final String DEFAULT_DOMAIN = "default.domain";
public static final String DOMAIN_ENCRYPTKEY = "domain.encryptKey";
}
19 changes: 11 additions & 8 deletions src/main/java/com/webank/weid/constant/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -364,13 +364,6 @@ public enum ErrorCode {
*/
ENCRYPT_KEY_INVALID(100704, "the key is invalid."),


/**
* the key is expire.
*/
ENCRYPT_KEY_EXPIRE(100705, "the key is expire."),


/**
* transportation base error.
*/
Expand Down Expand Up @@ -641,7 +634,17 @@ public enum ErrorCode {
160013,
"can not get the connection from pool, please check the error log."
),


/**
* the orgid is null.
*/
ORG_ID_IS_NULL(160014, "the orgid is null."),

/**
* the data is expire.
*/
SQL_DATA_EXPIRE(160015, "the data is expire."),

/**
* other uncatched exceptions or error.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public WeIdBaseException(String msg) {
* @param errorCode the errorCode
*/
public WeIdBaseException(ErrorCode errorCode) {
this(errorCode.getCode() + " - " + errorCode.getCodeDesc());
this.errorCode = errorCode;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,35 @@
import com.webank.weid.suite.api.persistence.Persistence;
import com.webank.weid.suite.persistence.sql.driver.MysqlDriver;
import com.webank.weid.util.DataToolUtils;
import com.webank.weid.util.PropertyUtils;

public class KeyManagerCallback extends AmopCallback {

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

private Persistence dataDriver = new MysqlDriver();
private Persistence dataDriver;

private WeIdService weidService;

private static final String TRANSENCRYPTIONDOMAIN =
PropertyUtils.getProperty(DataDriverConstant.DEFAULT_DOMAIN);

private WeIdService getWeIdService() {
if (weidService == null) {
weidService = new WeIdServiceImpl();
}
return weidService;
}

private Persistence getDataDriver() {
if (dataDriver == null) {
dataDriver = new MysqlDriver();
}
return dataDriver;
}

@Override
public GetEncryptKeyResponse onPush(GetEncryptKeyArgs arg) {
logger.info("[KeyManagerCallback.onPush] begin query key param:{}", arg);
GetEncryptKeyResponse encryptResponse = new GetEncryptKeyResponse();
ResponseData<String> keyResponse = dataDriver.get(TRANSENCRYPTIONDOMAIN, arg.getKeyId());
ResponseData<String> keyResponse = this.getDataDriver().get(
DataDriverConstant.DOMAIN_ENCRYPTKEY, arg.getKeyId());
if (keyResponse.getErrorCode().intValue() == ErrorCode.SUCCESS.getCode()
&& StringUtils.isBlank(keyResponse.getResult())) {
logger.info("[KeyManagerCallback.onPush] the encrypt key is not exists.");
Expand All @@ -90,10 +94,6 @@ public GetEncryptKeyResponse onPush(GetEncryptKeyArgs arg) {
encryptResponse.setErrorCode(ErrorCode.ENCRYPT_KEY_NO_PERMISSION.getCode());
encryptResponse.setErrorMessage(
ErrorCode.ENCRYPT_KEY_NO_PERMISSION.getCodeDesc());
} else if (isExpire(keyMap)) { //检查是否过期
logger.info("[KeyManagerCallback.onPush] the key is expire.");
encryptResponse.setErrorCode(ErrorCode.ENCRYPT_KEY_EXPIRE.getCode());
encryptResponse.setErrorMessage(ErrorCode.ENCRYPT_KEY_EXPIRE.getCodeDesc());
} else {
encryptResponse.setEncryptKey((String)keyMap.get(ParamKeyConstant.KEY_DATA));
encryptResponse.setErrorCode(ErrorCode.SUCCESS.getCode());
Expand Down Expand Up @@ -153,10 +153,4 @@ private boolean checkAuthority(GetEncryptKeyArgs arg, Map<String, Object> keyMap
}
return true;
}

private boolean isExpire(Map<String, Object> keyMap) {
//获取过期时间
long expire = (long)keyMap.get(ParamKeyConstant.KEY_EXPIRE);
return System.currentTimeMillis() > expire;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,13 @@ public class ProtocolProperty {
* 协议编解码类型.
*/
private EncodeType encodeType;

/**
* 秘钥过期时间配置,单位秒,默认为24小时.
*/
private int keyExpireTime = 86400;

public EncodeType getEncodeType() {
return encodeType;
}

public int getKeyExpireTime() {
return keyExpireTime;
}

public ProtocolProperty(EncodeType encodeType) {
this.encodeType = encodeType;
}

public ProtocolProperty(EncodeType encodeType, int keyExpireTime) {
this(encodeType);
this.keyExpireTime = keyExpireTime;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import com.webank.weid.suite.entity.EncodeData;
import com.webank.weid.suite.persistence.sql.driver.MysqlDriver;
import com.webank.weid.util.DataToolUtils;
import com.webank.weid.util.PropertyUtils;

/**
* 密文编解码处理器.
Expand All @@ -59,12 +58,16 @@ public class CipherEncodeProcessor extends BaseService implements EncodeProcesso

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

private Persistence dataDriver = new MysqlDriver();
private Persistence dataDriver;

protected AmopService amopService = new AmopServiceImpl();

private static final String ENCRYPTIONDOMAIN =
PropertyUtils.getProperty(DataDriverConstant.DEFAULT_DOMAIN);
private Persistence getDataDriver() {
if (dataDriver == null) {
dataDriver = new MysqlDriver();
}
return dataDriver;
}

/**
* 密文编码处理:先进行压缩,然后进行AES加密.
Expand All @@ -77,9 +80,6 @@ public String encode(EncodeData encodeData) throws EncodeSuiteException {
Map<String, Object> keyMap = new HashMap<String, Object>();
keyMap.put(ParamKeyConstant.KEY_DATA, key);
keyMap.put(ParamKeyConstant.KEY_VERIFIERS, encodeData.getVerifiers());
//当前时间 + 过期时间
long expireTime = System.currentTimeMillis() + encodeData.getExpireTime() * 1000;
keyMap.put(ParamKeyConstant.KEY_EXPIRE, expireTime);
String saveData = DataToolUtils.serialize(keyMap);

//将数据进行AES加密处理
Expand All @@ -89,8 +89,8 @@ public String encode(EncodeData encodeData) throws EncodeSuiteException {
.encrypt(encodeData.getData(), key);

//保存秘钥
ResponseData<Integer> response =
this.dataDriver.save(ENCRYPTIONDOMAIN, encodeData.getId(), saveData);
ResponseData<Integer> response = this.getDataDriver().save(
DataDriverConstant.DOMAIN_ENCRYPTKEY, encodeData.getId(), saveData);
if (response.getErrorCode().intValue() != ErrorCode.SUCCESS.getCode()) {
throw new EncodeSuiteException(
ErrorCode.getTypeByErrorCode(response.getErrorCode().intValue())
Expand Down Expand Up @@ -143,7 +143,7 @@ private String getEntryptKey(EncodeData encodeData) {
if (fiscoConfig.getCurrentOrgId().equals(encodeData.getOrgId())) {
//保存秘钥
ResponseData<String> response =
this.dataDriver.get(ENCRYPTIONDOMAIN, encodeData.getId());
this.getDataDriver().get(DataDriverConstant.DOMAIN_ENCRYPTKEY, encodeData.getId());
if (response.getErrorCode().intValue() != ErrorCode.SUCCESS.getCode()) {
throw new EncodeSuiteException(
ErrorCode.getTypeByErrorCode(response.getErrorCode().intValue())
Expand Down Expand Up @@ -182,10 +182,6 @@ private String getEncryptKey(EncodeData encodeData, String value) {
logger.error("[getEncryptKey] no access to get the data, this weid is {}.", weId);
throw new EncodeSuiteException(ErrorCode.ENCRYPT_KEY_NO_PERMISSION);
}
if (isExpire(keyMap)) {
logger.error("[getEncryptKey] the encrypt key is expire, this weid is {}.", weId);
throw new EncodeSuiteException(ErrorCode.ENCRYPT_KEY_EXPIRE);
}
return (String)keyMap.get(ParamKeyConstant.KEY_DATA);
} catch (DataTypeCastException e) {
logger.error("[getEncryptKey] deserialize the data error, you should upgrade SDK.", e);
Expand Down Expand Up @@ -241,10 +237,4 @@ private String requestEncryptKeyByAmop(EncodeData encodeData) {
}
return keyResponse.getEncryptKey();
}

private boolean isExpire(Map<String, Object> keyMap) {
//获取过期时间
long expire = (long)keyMap.get(ParamKeyConstant.KEY_EXPIRE);
return System.currentTimeMillis() > expire;
}
}
10 changes: 1 addition & 9 deletions src/main/java/com/webank/weid/suite/entity/EncodeData.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ public class EncodeData {
* 解码者身份信息.
*/
private WeIdAuthentication weIdAuthentication;

/**
* 密文方式的过期时间.
*/
private int expireTime;

private EncodeData(String id, String orgId, String data) {
this.id = id;
Expand All @@ -75,18 +70,15 @@ private EncodeData(String id, String orgId, String data) {
* @param id 数据编号
* @param data 需要编解码数据
* @param verifiers 协议数据指定用户
* @param expireTime 协议加密时秘钥的有效时间,单位(秒)
*/
public EncodeData(
String id,
String orgId,
String data,
List<String> verifiers,
int expireTime
List<String> verifiers
) {
this(id, orgId, data);
this.verifiers = verifiers;
this.expireTime = expireTime;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright© (2018) WeBank Co., Ltd.
*
* This file is part of weid-java-sdk.
*
* weid-java-sdk is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* weid-java-sdk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with weid-java-sdk. If not, see <https://www.gnu.org/licenses/>.
*/

package com.webank.weid.suite.persistence.sql;

import java.util.Date;

import lombok.Data;

@Data
public class DefaultTable {

/**
* 主键.
*/
private String id;

/**
* blob主体数据.
*/
private String data;

/**
* 创建时间.
*/
private Date created;
/**
* 更新时间.
*/
private Date updated;

/**
* 编码格式.
*/
private String protocol;

/**
* 超时时间.
*/
private Date expire;

/**
* 数据所属版本.
*/
private String version;

/**
* 扩展字段1.
*/
private int ext1;

/**
* 扩展字段2.
*/
private int ext2;

/**
* 扩展字段3.
*/
private String ext3;

/**
* 扩展字段4.
*/
private String ext4;
}
Loading

0 comments on commit 4000fd8

Please sign in to comment.