From a1b7444a4868885ea99286b39e5d5e061278f296 Mon Sep 17 00:00:00 2001 From: dmchen Date: Mon, 1 Apr 2024 09:39:29 +0000 Subject: [PATCH] refactor --- source/dnode/mgmt/node_util/src/dmFile.c | 19 ++++++++----------- source/dnode/vnode/inc/vnode.h | 3 +-- source/libs/wal/src/walMeta.c | 6 ++---- source/libs/wal/src/walWrite.c | 9 +++------ 4 files changed, 14 insertions(+), 23 deletions(-) diff --git a/source/dnode/mgmt/node_util/src/dmFile.c b/source/dnode/mgmt/node_util/src/dmFile.c index e765bdec88..ba6151220b 100644 --- a/source/dnode/mgmt/node_util/src/dmFile.c +++ b/source/dnode/mgmt/node_util/src/dmFile.c @@ -337,7 +337,6 @@ int32_t updateEncryptKey(char *key) { } } - //TODO: dmchen parse key from code if (!(machineId = tGetMachineId())) { terrno = TSDB_CODE_OUT_OF_MEMORY; goto _OVER; @@ -436,33 +435,31 @@ int32_t getEncryptKey(){ } char *encryptKey = NULL; - //TODO: dmchen parse key from code if(checkAndGetCryptKey(content, machineId, &encryptKey) != 0){ goto _OVER; } - strncpy(tsEncryptKey, encryptKey, ENCRYPT_KEY_LEN); - - taosMemoryFreeClear(encryptKey); - - //TODO: dmchen checksum - tsEncryptionKeyChksum = taosCalcChecksum(0, tsEncryptKey, ENCRYPT_KEY_LEN); - tsEncryptionKeyStat = ENCRYPT_KEY_STAT_LOADED; taosMemoryFreeClear(content); - if(tsEncryptKey[0] == '\0'){ + if(encryptKey[0] == '\0'){ terrno = TSDB_CODE_DNODE_INVALID_ENCRYPTKEY; dError("failed to read key since %s", terrstr()); goto _OVER; } - if(compareCheckCode(checkFile, tsEncryptKey) != 0){ + if(compareCheckCode(checkFile, encryptKey) != 0){ goto _OVER; } + strncpy(tsEncryptKey, encryptKey, ENCRYPT_KEY_LEN); + taosMemoryFreeClear(encryptKey); + tsEncryptionKeyChksum = taosCalcChecksum(0, tsEncryptKey, ENCRYPT_KEY_LEN); + tsEncryptionKeyStat = ENCRYPT_KEY_STAT_LOADED; + code = 0; _OVER: if (content != NULL) taosMemoryFree(content); + if (encryptKey != NULL) taosMemoryFree(encryptKey); if (code != 0) { if (terrno == 0) terrno = TAOS_SYSTEM_ERROR(errno); dError("failed to get encrypt key since %s", terrstr()); diff --git a/source/dnode/vnode/inc/vnode.h b/source/dnode/vnode/inc/vnode.h index be3a212820..6dd4955353 100644 --- a/source/dnode/vnode/inc/vnode.h +++ b/source/dnode/vnode/inc/vnode.h @@ -273,8 +273,7 @@ struct STsdbCfg { int32_t keepTimeOffset; // just for save config, use STsdbKeepCfg in STsdb instead SRetention retentions[TSDB_RETENTION_MAX]; int32_t encryptAlgorithm; - //TODO dmchen len - char encryptKey[17]; + char encryptKey[ENCRYPT_KEY_LEN + 1]; }; typedef struct { diff --git a/source/libs/wal/src/walMeta.c b/source/libs/wal/src/walMeta.c index bbca87ff9d..08162ef158 100644 --- a/source/libs/wal/src/walMeta.c +++ b/source/libs/wal/src/walMeta.c @@ -154,8 +154,7 @@ static FORCE_INLINE int64_t walScanLogGetLastVer(SWal* pWal, int32_t fileIdx) { // validate body int32_t cryptedBodyLen = logContent->head.bodyLen; - //TODO: dmchen enum - if(pWal->cfg.encryptAlgorithm == 1){ + if(pWal->cfg.encryptAlgorithm == DND_CA_SM4){ cryptedBodyLen = ENCRYPTED_LEN(cryptedBodyLen); } recordLen = walCkHeadSz + cryptedBodyLen; @@ -627,8 +626,7 @@ int walCheckAndRepairIdxFile(SWal* pWal, int32_t fileIdx) { int32_t plainBodyLen = ckHead.head.bodyLen; int32_t cryptedBodyLen = plainBodyLen; - //TODO: dmchen enum - if(pWal->cfg.encryptAlgorithm == 1){ + if(pWal->cfg.encryptAlgorithm == DND_CA_SM4){ cryptedBodyLen = ENCRYPTED_LEN(cryptedBodyLen); } idxEntry.offset += sizeof(SWalCkHead) + cryptedBodyLen; diff --git a/source/libs/wal/src/walWrite.c b/source/libs/wal/src/walWrite.c index 102e035425..3aa971d43a 100644 --- a/source/libs/wal/src/walWrite.c +++ b/source/libs/wal/src/walWrite.c @@ -528,8 +528,7 @@ static FORCE_INLINE int32_t walWriteImpl(SWal *pWal, int64_t index, tmsg_t msgTy char* newBody = NULL; char* newBodyEncrypted = NULL; - //TODO: dmchen enum - if(pWal->cfg.encryptAlgorithm == 1){ + if(pWal->cfg.encryptAlgorithm == DND_CA_SM4){ cyptedBodyLen = ENCRYPTED_LEN(cyptedBodyLen); char* newBody = taosMemoryMalloc(cyptedBodyLen); if(newBody == NULL){ @@ -571,16 +570,14 @@ static FORCE_INLINE int32_t walWriteImpl(SWal *pWal, int64_t index, tmsg_t msgTy wError("vgId:%d, file:%" PRId64 ".log, failed to write since %s", pWal->cfg.vgId, walGetLastFileFirstVer(pWal), strerror(errno)); code = -1; - //TODO: dmchen enum - if(pWal->cfg.encryptAlgorithm == 1){ + if(pWal->cfg.encryptAlgorithm == DND_CA_SM4){ taosMemoryFree(newBody); taosMemoryFree(newBodyEncrypted); } goto END; } - //TODO: dmchen enum - if(pWal->cfg.encryptAlgorithm == 1){ + if(pWal->cfg.encryptAlgorithm == DND_CA_SM4){ taosMemoryFree(newBody); taosMemoryFree(newBodyEncrypted); wInfo("vgId:%d, free newBody newBodyEncrypted %s",