• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #4880

11 Dec 2025 02:43AM UTC coverage: 64.544%. Remained the same
#4880

push

travis-ci

guanshengliang
feat(TS-7270): internal dependence

307 of 617 new or added lines in 24 files covered. (49.76%)

3380 existing lines in 25 files now uncovered.

163565 of 253417 relevant lines covered (64.54%)

105600506.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

52.53
/source/dnode/mnode/impl/src/mndEncryptAlgr.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#define MND_ENCRYPT_ALGR_VER_NUMBER 1
17

18
#include "mndEncryptAlgr.h"
19
#include "audit.h"
20
#include "mndMnode.h"
21
#include "mndShow.h"
22
#include "mndSync.h"
23
#include "mndTrans.h"
24

25
static void tFreeEncryptAlgrObj(SEncryptAlgrObj *pEncryptAlgr) {}
2,966,148✔
26

27
static int32_t tSerializeSEncryptAlgrObj(void *buf, int32_t bufLen, const SEncryptAlgrObj *pObj) {
10,895,088✔
28
  SEncoder encoder = {0};
10,895,088✔
29
  int32_t  code = 0;
10,895,088✔
30
  int32_t  lino;
31
  int32_t  tlen;
32
  tEncoderInit(&encoder, buf, bufLen);
10,895,088✔
33

34
  TAOS_CHECK_EXIT(tStartEncode(&encoder));
10,895,088✔
35
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pObj->id));
21,790,176✔
36
  TAOS_CHECK_EXIT(tEncodeCStr(&encoder, pObj->algorithm_id));
21,790,176✔
37
  TAOS_CHECK_EXIT(tEncodeCStr(&encoder, pObj->name));
21,790,176✔
38
  TAOS_CHECK_EXIT(tEncodeCStr(&encoder, pObj->desc));
21,790,176✔
39
  TAOS_CHECK_EXIT(tEncodeI16(&encoder, pObj->type));
21,790,176✔
40
  TAOS_CHECK_EXIT(tEncodeI8(&encoder, pObj->source));
21,790,176✔
41
  TAOS_CHECK_EXIT(tEncodeCStr(&encoder, pObj->ossl_algr_name));
21,790,176✔
42

43
  tEndEncode(&encoder);
10,895,088✔
44

45
_exit:
10,895,088✔
46
  if (code) {
10,895,088✔
47
    tlen = code;
×
48
  } else {
49
    tlen = encoder.pos;
10,895,088✔
50
  }
51
  tEncoderClear(&encoder);
10,895,088✔
52
  return tlen;
10,895,088✔
53
}
54

55
static int32_t tDeserializeSEncryptAlgrObj(void *buf, int32_t bufLen, SEncryptAlgrObj *pObj) {
2,966,148✔
56
  int32_t  code = 0;
2,966,148✔
57
  int32_t  lino;
58
  SDecoder decoder = {0};
2,966,148✔
59
  tDecoderInit(&decoder, buf, bufLen);
2,966,148✔
60

61
  TAOS_CHECK_EXIT(tStartDecode(&decoder));
2,966,148✔
62
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pObj->id));
5,932,296✔
63
  TAOS_CHECK_EXIT(tDecodeCStrTo(&decoder, pObj->algorithm_id));
2,966,148✔
64
  TAOS_CHECK_EXIT(tDecodeCStrTo(&decoder, pObj->name));
2,966,148✔
65
  TAOS_CHECK_EXIT(tDecodeCStrTo(&decoder, pObj->desc));
2,966,148✔
66
  TAOS_CHECK_EXIT(tDecodeI16(&decoder, &pObj->type));
5,932,296✔
67
  TAOS_CHECK_EXIT(tDecodeI8(&decoder, &pObj->source));
5,932,296✔
68
  TAOS_CHECK_EXIT(tDecodeCStrTo(&decoder, pObj->ossl_algr_name));
2,966,148✔
69

70
  tEndDecode(&decoder);
2,966,148✔
71

72
_exit:
2,966,148✔
73
  tDecoderClear(&decoder);
2,966,148✔
74
  return code;
2,966,148✔
75
}
76

77
static SSdbRaw *mndEncryptAlgrActionEncode(SEncryptAlgrObj *pEncryptAlgr) {
5,447,544✔
78
  int32_t code = 0;
5,447,544✔
79
  int32_t lino = 0;
5,447,544✔
80
  terrno = TSDB_CODE_SUCCESS;
5,447,544✔
81

82
  void    *buf = NULL;
5,447,544✔
83
  SSdbRaw *pRaw = NULL;
5,447,544✔
84

85
  int32_t tlen = tSerializeSEncryptAlgrObj(NULL, 0, pEncryptAlgr);
5,447,544✔
86
  if (tlen < 0) {
5,447,544✔
87
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
88
    goto OVER;
×
89
  }
90

91
  int32_t size = sizeof(int32_t) + tlen;
5,447,544✔
92
  pRaw = sdbAllocRaw(SDB_ENCRYPT_ALGORITHMS, MND_ENCRYPT_ALGR_VER_NUMBER, size);
5,447,544✔
93
  if (pRaw == NULL) {
5,447,544✔
94
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
95
    goto OVER;
×
96
  }
97

98
  buf = taosMemoryMalloc(tlen);
5,447,544✔
99
  if (buf == NULL) {
5,447,544✔
100
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
101
    goto OVER;
×
102
  }
103

104
  tlen = tSerializeSEncryptAlgrObj(buf, tlen, pEncryptAlgr);
5,447,544✔
105
  if (tlen < 0) {
5,447,544✔
106
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
107
    goto OVER;
×
108
  }
109

110
  int32_t dataPos = 0;
5,447,544✔
111
  SDB_SET_INT32(pRaw, dataPos, tlen, OVER);
5,447,544✔
112
  SDB_SET_BINARY(pRaw, dataPos, buf, tlen, OVER);
5,447,544✔
113
  SDB_SET_DATALEN(pRaw, dataPos, OVER);
5,447,544✔
114

115
OVER:
5,447,544✔
116
  taosMemoryFreeClear(buf);
5,447,544✔
117
  if (terrno != TSDB_CODE_SUCCESS) {
5,447,544✔
118
    mError("encrypt_algr:%" PRId32 ", failed to encode to raw:%p since %s", pEncryptAlgr->id, pRaw, terrstr());
×
119
    sdbFreeRaw(pRaw);
×
120
    return NULL;
×
121
  }
122

123
  mTrace("encrypt_algr:%" PRId32 ", encode to raw:%p, row:%p", pEncryptAlgr->id, pRaw, pEncryptAlgr);
5,447,544✔
124
  return pRaw;
5,447,544✔
125
}
126

127
SSdbRow *mndEncryptAlgrActionDecode(SSdbRaw *pRaw) {
2,966,148✔
128
  int32_t      code = 0;
2,966,148✔
129
  int32_t      lino = 0;
2,966,148✔
130
  SSdbRow     *pRow = NULL;
2,966,148✔
131
  SEncryptAlgrObj *pEncryptAlgr = NULL;
2,966,148✔
132
  void        *buf = NULL;
2,966,148✔
133
  terrno = TSDB_CODE_SUCCESS;
2,966,148✔
134

135
  int8_t sver = 0;
2,966,148✔
136
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) {
2,966,148✔
137
    goto OVER;
×
138
  }
139

140
  if (sver != MND_ENCRYPT_ALGR_VER_NUMBER) {
2,966,148✔
141
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
142
    mError("encrypt_algr read invalid ver, data ver: %d, curr ver: %d", sver, MND_ENCRYPT_ALGR_VER_NUMBER);
×
143
    goto OVER;
×
144
  }
145

146
  pRow = sdbAllocRow(sizeof(SEncryptAlgrObj));
2,966,148✔
147
  if (pRow == NULL) {
2,966,148✔
148
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
149
    goto OVER;
×
150
  }
151

152
  pEncryptAlgr = sdbGetRowObj(pRow);
2,966,148✔
153
  if (pEncryptAlgr == NULL) {
2,966,148✔
154
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
155
    goto OVER;
×
156
  }
157

158
  int32_t tlen;
2,950,338✔
159
  int32_t dataPos = 0;
2,966,148✔
160
  SDB_GET_INT32(pRaw, dataPos, &tlen, OVER);
2,966,148✔
161
  buf = taosMemoryMalloc(tlen + 1);
2,966,148✔
162
  if (buf == NULL) {
2,966,148✔
163
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
164
    goto OVER;
×
165
  }
166
  SDB_GET_BINARY(pRaw, dataPos, buf, tlen, OVER);
2,966,148✔
167

168
  if ((terrno = tDeserializeSEncryptAlgrObj(buf, tlen, pEncryptAlgr)) < 0) {
2,966,148✔
169
    goto OVER;
×
170
  }
171

172
OVER:
2,966,148✔
173
  taosMemoryFreeClear(buf);
2,966,148✔
174
  if (terrno != TSDB_CODE_SUCCESS) {
2,966,148✔
175
    mError("encrypt_algr:%" PRId32 ", failed to decode from raw:%p since %s", pEncryptAlgr->id, pRaw, terrstr());
×
176
    taosMemoryFreeClear(pRow);
×
177
    return NULL;
×
178
  }
179

180
  mTrace("encrypt_algr:%" PRId32 ", decode from raw:%p, row:%p", pEncryptAlgr->id, pRaw, pEncryptAlgr);
2,966,148✔
181
  return pRow;
2,966,148✔
182
}
183

184
int32_t mndEncryptAlgrActionInsert(SSdb *pSdb, SEncryptAlgrObj *pEncryptAlgr) {
2,966,148✔
185
  mTrace("encrypt_algr:%" PRId32 ", perform insert action", pEncryptAlgr->id);
2,966,148✔
186
  return 0;
2,966,148✔
187
}
188

189
int32_t mndEncryptAlgrActionDelete(SSdb *pSdb, SEncryptAlgrObj *pEncryptAlgr) {
2,966,148✔
190
  mTrace("encrypt_algr:%" PRId32 ", perform delete action", pEncryptAlgr->id);
2,966,148✔
191
  tFreeEncryptAlgrObj(pEncryptAlgr);
2,966,148✔
192
  return 0;
2,966,148✔
193
}
194

195
int32_t mndEncryptAlgrActionUpdate(SSdb *pSdb, SEncryptAlgrObj *pOldEncryptAlgr, SEncryptAlgrObj *pNewEncryptAlgr) {
×
196
  mTrace("encrypt_algr:%" PRId32 ", perform update action, old row:%p new row:%p", pOldEncryptAlgr->id, pOldEncryptAlgr,
×
197
         pNewEncryptAlgr);
198

199
  return 0;
×
200
}
201

202
typedef void (*mndSetEncryptAlgrFp)(SEncryptAlgrObj *Obj);
203

204
static void mndSetSM4EncryptAlgr(SEncryptAlgrObj *Obj){
322,662✔
205
  Obj->id = 1;
322,662✔
206
  strncpy(Obj->algorithm_id, "SM4-CBC", TSDB_ENCRYPT_ALGR_NAME_LEN);
322,662✔
207
  strncpy(Obj->name, "SM4", TSDB_ENCRYPT_ALGR_NAME_LEN);
322,662✔
208
  strncpy(Obj->desc, "SM4 symmetric encryption", TSDB_ENCRYPT_ALGR_DESC_LEN);
322,662✔
209
  Obj->type = ENCRYPT_ALGR_TYPE__SYMMETRIC_CIPHERS;
322,662✔
210
  Obj->source = ENCRYPT_ALGR_SOURCE_BUILTIN;
322,662✔
211
  strncpy(Obj->ossl_algr_name, "SM4-CBC:SM4", TSDB_ENCRYPT_ALGR_NAME_LEN);
322,662✔
212
}
322,662✔
213

214
static void mndSetAESEncryptAlgr(SEncryptAlgrObj *Obj){
322,662✔
215
  Obj->id = 2;
322,662✔
216
  strncpy(Obj->algorithm_id, "AES-128-CBC", TSDB_ENCRYPT_ALGR_NAME_LEN);
322,662✔
217
  strncpy(Obj->name, "AES", TSDB_ENCRYPT_ALGR_NAME_LEN);
322,662✔
218
  strncpy(Obj->desc, "AES symmetric encryption", TSDB_ENCRYPT_ALGR_DESC_LEN);
322,662✔
219
  Obj->type = ENCRYPT_ALGR_TYPE__SYMMETRIC_CIPHERS;
322,662✔
220
  Obj->source = ENCRYPT_ALGR_SOURCE_BUILTIN;
322,662✔
221
  strncpy(Obj->ossl_algr_name, "AES-128-CBC", TSDB_ENCRYPT_ALGR_NAME_LEN);
322,662✔
222
}
322,662✔
223

224
static void mndSetSM3EncryptAlgr(SEncryptAlgrObj *Obj) {
322,662✔
225
  Obj->id = 3;
322,662✔
226
  strncpy(Obj->algorithm_id, "SM3", TSDB_ENCRYPT_ALGR_NAME_LEN);
322,662✔
227
  strncpy(Obj->name, "SM3", TSDB_ENCRYPT_ALGR_NAME_LEN);
322,662✔
228
  strncpy(Obj->desc, "SM3 digests", TSDB_ENCRYPT_ALGR_DESC_LEN);
322,662✔
229
  Obj->type = ENCRYPT_ALGR_TYPE__DIGEST;
322,662✔
230
  Obj->source = ENCRYPT_ALGR_SOURCE_BUILTIN;
322,662✔
231
  strncpy(Obj->ossl_algr_name, "SM3", TSDB_ENCRYPT_ALGR_NAME_LEN);
322,662✔
232
}
322,662✔
233

234
static void mndSetSHAEncryptAlgr(SEncryptAlgrObj *Obj) {
322,662✔
235
  Obj->id = 4;
322,662✔
236
  strncpy(Obj->algorithm_id, "SHA-256", TSDB_ENCRYPT_ALGR_NAME_LEN);
322,662✔
237
  strncpy(Obj->name, "SHA-256", TSDB_ENCRYPT_ALGR_NAME_LEN);
322,662✔
238
  strncpy(Obj->desc, "SHA2 digests", TSDB_ENCRYPT_ALGR_DESC_LEN);
322,662✔
239
  Obj->type = ENCRYPT_ALGR_TYPE__DIGEST;
322,662✔
240
  Obj->source = ENCRYPT_ALGR_SOURCE_BUILTIN;
322,662✔
241
  strncpy(Obj->ossl_algr_name, "SHA-256", TSDB_ENCRYPT_ALGR_NAME_LEN);
322,662✔
242
}
322,662✔
243

244
static void mndSetSM2EncryptAlgr(SEncryptAlgrObj *Obj) {
322,662✔
245
  Obj->id = 5;
322,662✔
246
  strncpy(Obj->algorithm_id, "SM2", TSDB_ENCRYPT_ALGR_NAME_LEN);
322,662✔
247
  strncpy(Obj->name, "SM2", TSDB_ENCRYPT_ALGR_NAME_LEN);
322,662✔
248
  strncpy(Obj->desc, "SM2 Asymmetric Cipher", TSDB_ENCRYPT_ALGR_DESC_LEN);
322,662✔
249
  Obj->type = ENCRYPT_ALGR_TYPE__ASYMMETRIC_CIPHERS;
322,662✔
250
  Obj->source = ENCRYPT_ALGR_SOURCE_BUILTIN;
322,662✔
251
}
322,662✔
252

253
static void mndSetRSAEncryptAlgr(SEncryptAlgrObj *Obj) {
322,662✔
254
  Obj->id = 6;
322,662✔
255
  strncpy(Obj->algorithm_id, "RSA", TSDB_ENCRYPT_ALGR_NAME_LEN);
322,662✔
256
  strncpy(Obj->name, "RSA", TSDB_ENCRYPT_ALGR_NAME_LEN);
322,662✔
257
  strncpy(Obj->desc, "RSA Asymmetric Cipher", TSDB_ENCRYPT_ALGR_DESC_LEN);
322,662✔
258
  Obj->type = ENCRYPT_ALGR_TYPE__ASYMMETRIC_CIPHERS;
322,662✔
259
  Obj->source = ENCRYPT_ALGR_SOURCE_BUILTIN;
322,662✔
260
}
322,662✔
261

262
static SSdbRaw * mndCreateEncryptAlgrRaw(STrans *pTrans, SEncryptAlgrObj *Obj) {
1,935,972✔
263
  int32_t code = 0;
1,935,972✔
264

265
  SSdbRaw *pRaw = mndEncryptAlgrActionEncode(Obj);
1,935,972✔
266
  if (pRaw == NULL) {
1,935,972✔
267
    return NULL;
×
268
  }
269
  code = sdbSetRawStatus(pRaw, SDB_STATUS_READY);
1,935,972✔
270
  if (code != 0) {
1,935,972✔
271
    terrno = code;
×
272
    return NULL;
×
273
  }
274

275
  if ((code = mndTransAppendCommitlog(pTrans, pRaw)) != 0) {
1,935,972✔
276
    terrno = code;
×
277
    return NULL;
×
278
  }
279
  return pRaw;
1,935,972✔
280
}
281

282
int32_t mndSendCreateBuiltinReq(SMnode *pMnode) {
322,662✔
283
  int32_t code = 0;
322,662✔
284

285
  SRpcMsg rpcMsg = {.pCont = NULL,
322,662✔
286
                    .contLen = 0,
287
                    .msgType = TDMT_MND_BUILTIN_ENCRYPT_ALGR,
288
                    .info.ahandle = 0,
289
                    .info.notFreeAhandle = 1,
290
                    .info.refId = 0,
291
                    .info.noResp = 0,
292
                    .info.handle = 0};
293
  SEpSet  epSet = {0};
322,662✔
294

295
  mndGetMnodeEpSet(pMnode, &epSet);
322,662✔
296

297
  code = tmsgSendReq(&epSet, &rpcMsg);
322,662✔
298
  if (code != 0) {
322,662✔
299
    mError("failed to send builtin encrypt algr req, since %s", tstrerror(code));
×
300
  }
301
  return code;
322,662✔
302
}
303

304
static int32_t mndCreateBuiltinEncryptAlgr(SMnode *pMnode) {
322,662✔
305
  int32_t code = 0;
322,662✔
306
  STrans *pTrans = NULL;
322,662✔
307

308
  mndSetEncryptAlgrFp setFpArr[] = {mndSetSM4EncryptAlgr
322,662✔
309
#if defined(TD_ENTERPRISE) && defined(LINUX)
310
                                    ,
311
                                    mndSetAESEncryptAlgr,
312
                                    mndSetSM3EncryptAlgr,
313
                                    mndSetSHAEncryptAlgr,
314
                                    mndSetSM2EncryptAlgr,
315
                                    mndSetRSAEncryptAlgr
316
#endif
317
  };
318

319
  int32_t algrNum = sizeof(setFpArr) / sizeof(mndSetEncryptAlgrFp);
322,662✔
320

321
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL, "create-enc-algr");
322,662✔
322
  if (pTrans == NULL) {
322,662✔
UNCOV
323
    mError("failed to create since %s",terrstr());
×
UNCOV
324
    code = terrno;
×
UNCOV
325
    goto _OVER;
×
326
  }
327
  mInfo("trans:%d, used to create default encrypt_algr", pTrans->id);
322,662✔
328

329
  for (int32_t i = 0; i < algrNum; i++) {
2,258,634✔
330
    SEncryptAlgrObj *Obj = taosMemoryMalloc(sizeof(SEncryptAlgrObj));
1,935,972✔
331
    if (Obj == NULL) {
1,935,972✔
332
      goto _OVER;
×
333
    }
334
    memset(Obj, 0, sizeof(SEncryptAlgrObj));
1,935,972✔
335
    setFpArr[i](Obj);
1,935,972✔
336
    SSdbRaw *pRaw = mndCreateEncryptAlgrRaw(pTrans, Obj);
1,935,972✔
337
    taosMemoryFree(Obj);
1,935,972✔
338
    if (pRaw == NULL) {
1,935,972✔
UNCOV
339
      mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
×
340
      goto _OVER;
×
341
    }
342
  }
343

344
  if ((code = mndTransPrepare(pMnode, pTrans)) != 0) {
322,662✔
UNCOV
345
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
UNCOV
346
    goto _OVER;
×
347
  }
348

349
  code = 0;
322,662✔
350

351
_OVER:
322,662✔
352
  mndTransDrop(pTrans);
322,662✔
353
  return code;
322,662✔
354
}
355

356
static int32_t mndProcessBuiltinReq(SRpcMsg *pReq) {
322,662✔
357
  SMnode *pMnode = pReq->info.node;
322,662✔
358
  if (!mndIsLeader(pMnode)) {
322,662✔
UNCOV
359
    return TSDB_CODE_SUCCESS;
×
360
  }
361

362
  return mndCreateBuiltinEncryptAlgr(pMnode);
322,662✔
363
}
364

365
static int32_t mndProcessBuiltinRsp(SRpcMsg *pRsp) {
322,662✔
366
  mInfo("builtin rsp");
322,662✔
367
  return 0;
322,662✔
368
}
369

370
static int32_t mndUpgradeBuiltinEncryptAlgr(SMnode *pMnode, int32_t version) {
406,119✔
371
  if (version >= TSDB_MNODE_BUILTIN_DATA_VERSION) return 0;
406,119✔
372

373
  return mndSendCreateBuiltinReq(pMnode);
322,662✔
374
}
375

376
#define SYMCBC "Symmetric Ciphers CBC mode"
377
#define ASYM       "Asymmetric Ciphers"
378
#define DIGEST     "Digests"
379
#define BUILTIN "build-in"
380
#define CUSTOMIZED "customized"
UNCOV
381
static int32_t mndRetrieveEncryptAlgr(SRpcMsg *pMsg, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows){
×
UNCOV
382
  SMnode      *pMnode = pMsg->info.node;
×
UNCOV
383
  SSdb        *pSdb = pMnode->pSdb;
×
UNCOV
384
  int32_t      code = 0;
×
UNCOV
385
  int32_t      lino = 0;
×
UNCOV
386
  int32_t      numOfRows = 0;
×
UNCOV
387
  int32_t      cols = 0;
×
UNCOV
388
  SEncryptAlgrObj *pObj = NULL;
×
389

390
  char tmpBuf[1000] = {0};
×
391

392
  while (numOfRows < rows) {
×
393
    pShow->pIter = sdbFetch(pSdb, SDB_ENCRYPT_ALGORITHMS, pShow->pIter, (void **)&pObj);
×
394
    if (pShow->pIter == NULL) break;
×
395

396
    cols = 0;
×
UNCOV
397
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
398
    COL_DATA_SET_VAL_GOTO((const char *)&pObj->id, false, pObj, pShow->pIter, _OVER);
×
399

400
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
401
    tstrncpy(varDataVal(tmpBuf), pObj->algorithm_id, TSDB_ENCRYPT_ALGR_NAME_LEN);
×
402
    varDataSetLen(tmpBuf, strlen(varDataVal(tmpBuf)));
×
UNCOV
403
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)tmpBuf, false), pObj, &lino, _OVER);
×
404

405
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
406
    tstrncpy(varDataVal(tmpBuf), pObj->name, TSDB_ENCRYPT_ALGR_NAME_LEN);
×
UNCOV
407
    varDataSetLen(tmpBuf, strlen(varDataVal(tmpBuf)));
×
408
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)tmpBuf, false), pObj, &lino, _OVER);
×
409

410
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
411
    tstrncpy(varDataVal(tmpBuf), pObj->desc, TSDB_ENCRYPT_ALGR_DESC_LEN);
×
UNCOV
412
    varDataSetLen(tmpBuf, strlen(varDataVal(tmpBuf)));
×
413
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)tmpBuf, false), pObj, &lino, _OVER);
×
414

415
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
416
    if(pObj->type == ENCRYPT_ALGR_TYPE__SYMMETRIC_CIPHERS){
×
UNCOV
417
      tstrncpy(varDataVal(tmpBuf), SYMCBC, strlen(SYMCBC) + 1);
×
418
    } else if (pObj->type == ENCRYPT_ALGR_TYPE__DIGEST) {
×
419
      tstrncpy(varDataVal(tmpBuf), DIGEST, strlen(DIGEST) + 1);
×
420
    } else if (pObj->type == ENCRYPT_ALGR_TYPE__ASYMMETRIC_CIPHERS) {
×
421
      tstrncpy(varDataVal(tmpBuf), ASYM, strlen(ASYM) + 1);
×
422
    }
423
    varDataSetLen(tmpBuf, strlen(varDataVal(tmpBuf)));
×
424
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)tmpBuf, false), pObj, &lino, _OVER);
×
425

426
    memset(tmpBuf, 0, 1000);
×
427
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
428
    if(pObj->source == ENCRYPT_ALGR_SOURCE_BUILTIN){
×
429
      tstrncpy(varDataVal(tmpBuf), BUILTIN, strlen(BUILTIN) + 1);
×
UNCOV
430
    } else if (pObj->source == ENCRYPT_ALGR_SOURCE_CUSTOMIZED) {
×
431
      tstrncpy(varDataVal(tmpBuf), CUSTOMIZED, strlen(CUSTOMIZED) + 1);
×
432
    }
UNCOV
433
    varDataSetLen(tmpBuf, strlen(varDataVal(tmpBuf)));
×
434
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)tmpBuf, false), pObj, &lino, _OVER);
×
435

436
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
437
    tstrncpy(varDataVal(tmpBuf), pObj->ossl_algr_name, TSDB_ENCRYPT_ALGR_NAME_LEN);
×
438
    varDataSetLen(tmpBuf, strlen(varDataVal(tmpBuf)));
×
439
    RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)tmpBuf, false), pObj, &lino, _OVER);
×
440

441
    sdbRelease(pSdb, pObj);
×
442
    numOfRows++;
×
443
  }
444

445
  pShow->numOfRows += numOfRows;
×
446

447
_OVER:
×
UNCOV
448
  if (code != 0) {
×
449
    mError("failed to retrieve encrypt_algr info at line %d since %s", lino, tstrerror(code));
×
450
    TAOS_RETURN(code);
×
451
  }
UNCOV
452
  return numOfRows;
×
453
}
454

455
static int32_t mndProcessCreateEncryptAlgrReq(SRpcMsg *pReq) {
×
456
  SMnode               *pMnode = pReq->info.node;
×
457
  int32_t               code = 0;
×
458
  int32_t               lino = 0;
×
UNCOV
459
  SCreateEncryptAlgrReq createReq = {0};
×
460
  STrans               *pTrans = NULL;
×
461

UNCOV
462
  if (tDeserializeSCreateEncryptAlgrReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
×
463
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_MSG, &lino, _OVER);
×
464
  }
465

466
  mInfo("algr:%s, start to create, ossl_name:%s", createReq.algorithmId, createReq.osslAlgrName);
×
467

468
  // TAOS_CHECK_GOTO(grantCheck(TSDB_GRANT_USER), &lino, _OVER);
469

470
  SEncryptAlgrObj *exist = mndAcquireEncryptAlgrByAId(pMnode, createReq.algorithmId);
×
471
  if (exist != NULL) {
×
UNCOV
472
    mndReleaseEncryptAlgr(pMnode, exist);
×
UNCOV
473
    TAOS_CHECK_GOTO(TSDB_CODE_MNODE_ALGR_EXIST, &lino, _OVER);
×
474
  }
475

UNCOV
476
  SEncryptAlgrObj Obj = {0};
×
UNCOV
477
  int32_t         id = sdbGetMaxId(pMnode->pSdb, SDB_ENCRYPT_ALGORITHMS);
×
478
  if (id < 100) id = 101;
×
479
  Obj.id = id;
×
480
  strncpy(Obj.algorithm_id, createReq.algorithmId, TSDB_ENCRYPT_ALGR_NAME_LEN);
×
481
  strncpy(Obj.name, createReq.name, TSDB_ENCRYPT_ALGR_NAME_LEN);
×
UNCOV
482
  strncpy(Obj.desc, createReq.desc, TSDB_ENCRYPT_ALGR_DESC_LEN);
×
UNCOV
483
  if (strncmp(createReq.type, "Symmetric_Ciphers_CBC_mode", TSDB_ENCRYPT_ALGR_TYPE_LEN) == 0) {
×
484
    Obj.type = ENCRYPT_ALGR_TYPE__SYMMETRIC_CIPHERS;
×
485
  } else if (strncmp(createReq.type, "Digests", TSDB_ENCRYPT_ALGR_TYPE_LEN) == 0) {
×
486
    Obj.type = ENCRYPT_ALGR_TYPE__DIGEST;
×
487
  } else if (strncmp(createReq.type, "Asymmetric_Ciphers", TSDB_ENCRYPT_ALGR_TYPE_LEN) == 0) {
×
488
    Obj.type = ENCRYPT_ALGR_TYPE__ASYMMETRIC_CIPHERS;
×
489
  } else {
490
    TAOS_CHECK_GOTO(TSDB_CODE_MNODE_INVALID_ENCRYPT_ALGR_TYPE, &lino, _OVER);
×
491
  }
492
  Obj.source = ENCRYPT_ALGR_SOURCE_CUSTOMIZED;
×
493
  strncpy(Obj.ossl_algr_name, createReq.osslAlgrName, TSDB_ENCRYPT_ALGR_NAME_LEN);
×
494

495
  pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL, "create-enc-algr");
×
496
  if (pTrans == NULL) {
×
UNCOV
497
    mError("failed to create since %s", terrstr());
×
498
    code = terrno;
×
UNCOV
499
    goto _OVER;
×
500
  }
501
  mInfo("trans:%d, used to create encrypt_algr", pTrans->id);
×
502

503
  SSdbRaw *pRaw = mndCreateEncryptAlgrRaw(pTrans, &Obj);
×
504
  if (pRaw == NULL) {
×
505
    mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
×
506
    goto _OVER;
×
507
  }
508

509
  if ((code = mndTransPrepare(pMnode, pTrans)) != 0) {
×
UNCOV
510
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
511
    goto _OVER;
×
512
  }
513

514
  auditRecord(pReq, pMnode->clusterId, "createEncryptAlgr", "", createReq.algorithmId, createReq.sql,
×
UNCOV
515
              strlen(createReq.sql));
×
516

517
  return code;
×
518
_OVER:
×
519
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
UNCOV
520
    mError("algr:%s, failed to create at line %d since %s", createReq.algorithmId, lino, tstrerror(code));
×
521
  }
522

523
  tFreeSCreateEncryptAlgrReq(&createReq);
×
UNCOV
524
  mndTransDrop(pTrans);
×
525
  TAOS_RETURN(code);
×
526
}
527

528
static int32_t mndDropEncryptAlgr(SMnode *pMnode, SRpcMsg *pReq, SEncryptAlgrObj *pAlgr) {
×
UNCOV
529
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "drop-encrypt-algr");
×
UNCOV
530
  if (pTrans == NULL) {
×
531
    mError("algr:%s, failed to drop since %s", pAlgr->algorithm_id, terrstr());
×
532
    TAOS_RETURN(terrno);
×
533
  }
UNCOV
534
  mInfo("trans:%d, used to drop algr:%s", pTrans->id, pAlgr->algorithm_id);
×
535

536
  SSdbRaw *pCommitRaw = mndEncryptAlgrActionEncode(pAlgr);
×
537
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
×
538
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
×
539
    mndTransDrop(pTrans);
×
540
    TAOS_RETURN(terrno);
×
541
  }
542
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED) < 0) {
×
UNCOV
543
    mndTransDrop(pTrans);
×
544
    TAOS_RETURN(terrno);
×
545
  }
546

547
  if (mndTransPrepare(pMnode, pTrans) != 0) {
×
548
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
UNCOV
549
    mndTransDrop(pTrans);
×
550
    TAOS_RETURN(terrno);
×
551
  }
552

UNCOV
553
  mndTransDrop(pTrans);
×
UNCOV
554
  TAOS_RETURN(0);
×
555
}
556

557
static int32_t mndProcessDropEncryptAlgrReq(SRpcMsg *pReq) {
×
558
  SMnode             *pMnode = pReq->info.node;
×
UNCOV
559
  int32_t             code = 0;
×
UNCOV
560
  int32_t             lino = 0;
×
561
  SEncryptAlgrObj    *pObj = NULL;
×
562
  SDropEncryptAlgrReq dropReq = {0};
×
563

UNCOV
564
  TAOS_CHECK_GOTO(tDeserializeSDropEncryptAlgrReq(pReq->pCont, pReq->contLen, &dropReq), &lino, _OVER);
×
565

566
  mInfo("algr:%s, start to drop", dropReq.algorithmId);
×
567
  // TAOS_CHECK_GOTO(mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_DROP_USER), &lino, _OVER);
568

569
  if (dropReq.algorithmId[0] == 0) {
×
570
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_ENCRYPT_ALGR_FORMAT, &lino, _OVER);
×
571
  }
572

UNCOV
573
  pObj = mndAcquireEncryptAlgrByAId(pMnode, dropReq.algorithmId);
×
574
  if (pObj == NULL) {
×
UNCOV
575
    TAOS_CHECK_GOTO(TSDB_CODE_MNODE_ENCRYPT_ALGR_NOT_EXIST, &lino, _OVER);
×
576
  }
577

578
  bool    exist = false;
×
UNCOV
579
  void   *pIter = NULL;
×
UNCOV
580
  SDbObj *pDb = NULL;
×
581
  while (1) {
582
    pIter = sdbFetch(pMnode->pSdb, SDB_DB, pIter, (void **)&pDb);
×
583
    if (pIter == NULL) break;
×
584

UNCOV
585
    if (pDb->cfg.encryptAlgorithm == pObj->id) {
×
586
      exist = true;
×
587
      sdbRelease(pMnode->pSdb, pDb);
×
588
      sdbCancelFetch(pMnode->pSdb, pIter);
×
UNCOV
589
      break;
×
590
    }
591

UNCOV
592
    sdbRelease(pMnode->pSdb, pDb);
×
593
  }
594

595
  if (exist) {
×
596
    TAOS_CHECK_GOTO(TSDB_CODE_MNODE_ENCRYPT_ALGR_IN_USE, &lino, _OVER);
×
597
  }
598

UNCOV
599
  TAOS_CHECK_GOTO(mndDropEncryptAlgr(pMnode, pReq, pObj), &lino, _OVER);
×
600
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
×
601

UNCOV
602
  auditRecord(pReq, pMnode->clusterId, "dropEncryptAlgr", "", dropReq.algorithmId, dropReq.sql, dropReq.sqlLen);
×
603

604
_OVER:
×
UNCOV
605
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
UNCOV
606
    mError("algr:%s, failed to drop at line %d since %s", dropReq.algorithmId, lino, tstrerror(code));
×
607
  }
608

UNCOV
609
  mndReleaseEncryptAlgr(pMnode, pObj);
×
610
  tFreeSDropEncryptAlgrReq(&dropReq);
×
UNCOV
611
  TAOS_RETURN(code);
×
612
}
613

614
int32_t mndInitEncryptAlgr(SMnode *pMnode) {
495,142✔
615
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_ENCRYPT_ALGR, mndProcessCreateEncryptAlgrReq);
495,142✔
616
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_ENCRYPT_ALGORITHMS, mndRetrieveEncryptAlgr);
495,142✔
617
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_ENCRYPT_ALGR, mndProcessDropEncryptAlgrReq);
495,142✔
618
  mndSetMsgHandle(pMnode, TDMT_MND_BUILTIN_ENCRYPT_ALGR, mndProcessBuiltinReq);
495,142✔
619
  mndSetMsgHandle(pMnode, TDMT_MND_BUILTIN_ENCRYPT_ALGR_RSP, mndProcessBuiltinRsp);
495,142✔
620

621
  SSdbTable table = {
495,142✔
622
      .sdbType = SDB_ENCRYPT_ALGORITHMS,
623
      .keyType = SDB_KEY_INT32,
624
      .upgradeFp = (SdbUpgradeFp)mndUpgradeBuiltinEncryptAlgr,
625
      .encodeFp = (SdbEncodeFp)mndEncryptAlgrActionEncode,
626
      .decodeFp = (SdbDecodeFp)mndEncryptAlgrActionDecode,
627
      .insertFp = (SdbInsertFp)mndEncryptAlgrActionInsert,
628
      .updateFp = (SdbUpdateFp)mndEncryptAlgrActionUpdate,
629
      .deleteFp = (SdbDeleteFp)mndEncryptAlgrActionDelete,
630
  };
631

632
  return sdbSetTable(pMnode->pSdb, table);
495,142✔
633
}
634

635
void mndCleanupEncryptAlgr(SMnode *pMnode) { mDebug("mnd encrypt algorithms cleanup"); }
494,358✔
636

637
SEncryptAlgrObj *mndAcquireEncryptAlgrById(SMnode *pMnode, int64_t id) {
1,518,298✔
638
  SSdb        *pSdb = pMnode->pSdb;
1,518,298✔
639
  SEncryptAlgrObj *pAlgr = sdbAcquire(pSdb, SDB_ENCRYPT_ALGORITHMS, &id);
1,518,298✔
640
  if (pAlgr == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
1,518,298✔
641
    terrno = TSDB_CODE_SUCCESS;
1,515,877✔
642
  }
643
  return pAlgr;
1,518,298✔
644
}
645

646
void mndGetEncryptOsslAlgrNameById(SMnode *pMnode, int64_t id, char* out){
1,518,298✔
647
  SEncryptAlgrObj *obj = mndAcquireEncryptAlgrById(pMnode, id);
1,518,298✔
648
  if(obj != NULL){
1,518,298✔
649
    tstrncpy(out, obj->ossl_algr_name, TSDB_ENCRYPT_ALGR_NAME_LEN);
2,421✔
650
    mndReleaseEncryptAlgr(pMnode, obj);
2,421✔
651
  }
652
}
1,518,298✔
653

654
SEncryptAlgrObj *mndAcquireEncryptAlgrByAId(SMnode *pMnode, char* algorithm_id) {
5,565✔
655
  SSdb        *pSdb = pMnode->pSdb;
5,565✔
656
  SEncryptAlgrObj *pObj = NULL;
5,565✔
657
  void *pIter = NULL;
5,565✔
658
  while (1) {
659
    pIter = sdbFetch(pSdb, SDB_ENCRYPT_ALGORITHMS, pIter, (void **)&pObj);
19,665✔
660
    if (pIter == NULL) break;
19,665✔
661

662
    if (strncasecmp(pObj->algorithm_id, algorithm_id, TSDB_ENCRYPT_ALGR_NAME_LEN) == 0) {
17,315✔
663
      sdbCancelFetch(pSdb, pIter);
3,215✔
664
      break;
3,215✔
665
    }
666

667
    sdbRelease(pSdb, pObj);
14,100✔
668
  }
669
  return pObj;
5,565✔
670
}
671

672
void mndReleaseEncryptAlgr(SMnode *pMnode, SEncryptAlgrObj *pObj) {
2,421✔
673
  SSdb *pSdb = pMnode->pSdb;
2,421✔
674
  sdbRelease(pSdb, pObj);
2,421✔
675
  pObj = NULL;
2,421✔
676
}
2,421✔
677

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc