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

taosdata / TDengine / #4887

16 Dec 2025 08:27AM UTC coverage: 65.289% (-0.003%) from 65.292%
#4887

push

travis-ci

web-flow
feat[TS-7233]: audit (#33850)

377 of 536 new or added lines in 28 files covered. (70.34%)

1025 existing lines in 111 files now uncovered.

178977 of 274129 relevant lines covered (65.29%)

102580217.43 hits per line

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

51.35
/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) {}
3,095,916✔
26

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

34
  TAOS_CHECK_EXIT(tStartEncode(&encoder));
11,357,112✔
35
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pObj->id));
22,714,224✔
36
  TAOS_CHECK_EXIT(tEncodeCStr(&encoder, pObj->algorithm_id));
22,714,224✔
37
  TAOS_CHECK_EXIT(tEncodeCStr(&encoder, pObj->name));
22,714,224✔
38
  TAOS_CHECK_EXIT(tEncodeCStr(&encoder, pObj->desc));
22,714,224✔
39
  TAOS_CHECK_EXIT(tEncodeI16(&encoder, pObj->type));
22,714,224✔
40
  TAOS_CHECK_EXIT(tEncodeI8(&encoder, pObj->source));
22,714,224✔
41
  TAOS_CHECK_EXIT(tEncodeCStr(&encoder, pObj->ossl_algr_name));
22,714,224✔
42

43
  tEndEncode(&encoder);
11,357,112✔
44

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

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

61
  TAOS_CHECK_EXIT(tStartDecode(&decoder));
3,095,916✔
62
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pObj->id));
6,191,832✔
63
  TAOS_CHECK_EXIT(tDecodeCStrTo(&decoder, pObj->algorithm_id));
3,095,916✔
64
  TAOS_CHECK_EXIT(tDecodeCStrTo(&decoder, pObj->name));
3,095,916✔
65
  TAOS_CHECK_EXIT(tDecodeCStrTo(&decoder, pObj->desc));
3,095,916✔
66
  TAOS_CHECK_EXIT(tDecodeI16(&decoder, &pObj->type));
6,191,832✔
67
  TAOS_CHECK_EXIT(tDecodeI8(&decoder, &pObj->source));
6,191,832✔
68
  TAOS_CHECK_EXIT(tDecodeCStrTo(&decoder, pObj->ossl_algr_name));
3,095,916✔
69

70
  tEndDecode(&decoder);
3,095,916✔
71

72
_exit:
3,095,916✔
73
  tDecoderClear(&decoder);
3,095,916✔
74
  return code;
3,095,916✔
75
}
76

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

82
  void    *buf = NULL;
5,678,556✔
83
  SSdbRaw *pRaw = NULL;
5,678,556✔
84

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

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

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

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

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

115
OVER:
5,678,556✔
116
  taosMemoryFreeClear(buf);
5,678,556✔
117
  if (terrno != TSDB_CODE_SUCCESS) {
5,678,556✔
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,678,556✔
124
  return pRaw;
5,678,556✔
125
}
126

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

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

140
  if (sver != MND_ENCRYPT_ALGR_VER_NUMBER) {
3,095,916✔
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));
3,095,916✔
147
  if (pRow == NULL) {
3,095,916✔
148
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
149
    goto OVER;
×
150
  }
151

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

158
  int32_t tlen;
3,094,764✔
159
  int32_t dataPos = 0;
3,095,916✔
160
  SDB_GET_INT32(pRaw, dataPos, &tlen, OVER);
3,095,916✔
161
  buf = taosMemoryMalloc(tlen + 1);
3,095,916✔
162
  if (buf == NULL) {
3,095,916✔
163
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
164
    goto OVER;
×
165
  }
166
  SDB_GET_BINARY(pRaw, dataPos, buf, tlen, OVER);
3,095,916✔
167

168
  if ((terrno = tDeserializeSEncryptAlgrObj(buf, tlen, pEncryptAlgr)) < 0) {
3,095,916✔
169
    goto OVER;
×
170
  }
171

172
OVER:
3,095,916✔
173
  taosMemoryFreeClear(buf);
3,095,916✔
174
  if (terrno != TSDB_CODE_SUCCESS) {
3,095,916✔
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);
3,095,916✔
181
  return pRow;
3,095,916✔
182
}
183

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

189
int32_t mndEncryptAlgrActionDelete(SSdb *pSdb, SEncryptAlgrObj *pEncryptAlgr) {
3,095,916✔
190
  mTrace("encrypt_algr:%" PRId32 ", perform delete action", pEncryptAlgr->id);
3,095,916✔
191
  tFreeEncryptAlgrObj(pEncryptAlgr);
3,095,916✔
192
  return 0;
3,095,916✔
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){
334,009✔
205
  Obj->id = 1;
334,009✔
206
  strncpy(Obj->algorithm_id, "SM4-CBC", TSDB_ENCRYPT_ALGR_NAME_LEN);
334,009✔
207
  strncpy(Obj->name, "SM4", TSDB_ENCRYPT_ALGR_NAME_LEN);
334,009✔
208
  strncpy(Obj->desc, "SM4 symmetric encryption", TSDB_ENCRYPT_ALGR_DESC_LEN);
334,009✔
209
  Obj->type = ENCRYPT_ALGR_TYPE__SYMMETRIC_CIPHERS;
334,009✔
210
  Obj->source = ENCRYPT_ALGR_SOURCE_BUILTIN;
334,009✔
211
  strncpy(Obj->ossl_algr_name, "SM4-CBC:SM4", TSDB_ENCRYPT_ALGR_NAME_LEN);
334,009✔
212
}
334,009✔
213

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

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

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

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

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

262
static SSdbRaw * mndCreateEncryptAlgrRaw(STrans *pTrans, SEncryptAlgrObj *Obj) {
2,004,054✔
263
  int32_t code = 0;
2,004,054✔
264

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

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

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

285
  SRpcMsg rpcMsg = {.pCont = NULL,
334,009✔
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};
334,009✔
294

295
  mndGetMnodeEpSet(pMnode, &epSet);
334,009✔
296

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

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

308
  mndSetEncryptAlgrFp setFpArr[] = {mndSetSM4EncryptAlgr
334,009✔
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);
334,009✔
320

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

329
  for (int32_t i = 0; i < algrNum; i++) {
2,338,063✔
330
    SEncryptAlgrObj *Obj = taosMemoryMalloc(sizeof(SEncryptAlgrObj));
2,004,054✔
331
    if (Obj == NULL) {
2,004,054✔
332
      goto _OVER;
×
333
    }
334
    memset(Obj, 0, sizeof(SEncryptAlgrObj));
2,004,054✔
335
    setFpArr[i](Obj);
2,004,054✔
336
    SSdbRaw *pRaw = mndCreateEncryptAlgrRaw(pTrans, Obj);
2,004,054✔
337
    taosMemoryFree(Obj);
2,004,054✔
338
    if (pRaw == NULL) {
2,004,054✔
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) {
334,009✔
345
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
346
    goto _OVER;
×
347
  }
348

349
  code = 0;
334,009✔
350

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

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

362
  return mndCreateBuiltinEncryptAlgr(pMnode);
334,009✔
363
}
364

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

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

373
  return mndSendCreateBuiltinReq(pMnode);
334,009✔
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"
381
static int32_t mndRetrieveEncryptAlgr(SRpcMsg *pMsg, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows){
×
382
  SMnode      *pMnode = pMsg->info.node;
×
383
  SSdb        *pSdb = pMnode->pSdb;
×
384
  int32_t      code = 0;
×
385
  int32_t      lino = 0;
×
386
  int32_t      numOfRows = 0;
×
387
  int32_t      cols = 0;
×
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;
×
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)));
×
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);
×
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);
×
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){
×
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);
×
430
    } else if (pObj->source == ENCRYPT_ALGR_SOURCE_CUSTOMIZED) {
×
431
      tstrncpy(varDataVal(tmpBuf), CUSTOMIZED, strlen(CUSTOMIZED) + 1);
×
432
    }
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:
×
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
  }
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;
×
459
  SCreateEncryptAlgrReq createReq = {0};
×
460
  STrans               *pTrans = NULL;
×
461

NEW
462
  int64_t tss = taosGetTimestampMs();
×
463

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

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

470
  // TAOS_CHECK_GOTO(grantCheck(TSDB_GRANT_USER), &lino, _OVER);
471

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

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

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

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

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

NEW
516
  if (tsAuditLevel >= AUDIT_LEVEL_CLUSTER) {
×
NEW
517
    int64_t tse = taosGetTimestampMs();
×
NEW
518
    double  duration = (double)(tse - tss);
×
NEW
519
    duration = duration / 1000;
×
NEW
520
    auditRecord(pReq, pMnode->clusterId, "createEncryptAlgr", "", createReq.algorithmId, createReq.sql,
×
NEW
521
                strlen(createReq.sql), duration, 0);
×
522
  }
523

524
  return code;
×
525
_OVER:
×
526
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
527
    mError("algr:%s, failed to create at line %d since %s", createReq.algorithmId, lino, tstrerror(code));
×
528
  }
529

530
  tFreeSCreateEncryptAlgrReq(&createReq);
×
531
  mndTransDrop(pTrans);
×
532
  TAOS_RETURN(code);
×
533
}
534

535
static int32_t mndDropEncryptAlgr(SMnode *pMnode, SRpcMsg *pReq, SEncryptAlgrObj *pAlgr) {
×
536
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "drop-encrypt-algr");
×
537
  if (pTrans == NULL) {
×
538
    mError("algr:%s, failed to drop since %s", pAlgr->algorithm_id, terrstr());
×
539
    TAOS_RETURN(terrno);
×
540
  }
541
  mInfo("trans:%d, used to drop algr:%s", pTrans->id, pAlgr->algorithm_id);
×
542

543
  SSdbRaw *pCommitRaw = mndEncryptAlgrActionEncode(pAlgr);
×
544
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
×
545
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
×
546
    mndTransDrop(pTrans);
×
547
    TAOS_RETURN(terrno);
×
548
  }
549
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED) < 0) {
×
550
    mndTransDrop(pTrans);
×
551
    TAOS_RETURN(terrno);
×
552
  }
553

554
  if (mndTransPrepare(pMnode, pTrans) != 0) {
×
555
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
556
    mndTransDrop(pTrans);
×
557
    TAOS_RETURN(terrno);
×
558
  }
559

560
  mndTransDrop(pTrans);
×
561
  TAOS_RETURN(0);
×
562
}
563

564
static int32_t mndProcessDropEncryptAlgrReq(SRpcMsg *pReq) {
×
565
  SMnode             *pMnode = pReq->info.node;
×
566
  int32_t             code = 0;
×
567
  int32_t             lino = 0;
×
568
  SEncryptAlgrObj    *pObj = NULL;
×
569
  SDropEncryptAlgrReq dropReq = {0};
×
570

NEW
571
  int64_t tss = taosGetTimestampMs();
×
572

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

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

578
  if (dropReq.algorithmId[0] == 0) {
×
579
    TAOS_CHECK_GOTO(TSDB_CODE_MND_INVALID_ENCRYPT_ALGR_FORMAT, &lino, _OVER);
×
580
  }
581

582
  pObj = mndAcquireEncryptAlgrByAId(pMnode, dropReq.algorithmId);
×
583
  if (pObj == NULL) {
×
584
    TAOS_CHECK_GOTO(TSDB_CODE_MNODE_ENCRYPT_ALGR_NOT_EXIST, &lino, _OVER);
×
585
  }
586

587
  bool    exist = false;
×
588
  void   *pIter = NULL;
×
589
  SDbObj *pDb = NULL;
×
590
  while (1) {
591
    pIter = sdbFetch(pMnode->pSdb, SDB_DB, pIter, (void **)&pDb);
×
592
    if (pIter == NULL) break;
×
593

594
    if (pDb->cfg.encryptAlgorithm == pObj->id) {
×
595
      exist = true;
×
596
      sdbRelease(pMnode->pSdb, pDb);
×
597
      sdbCancelFetch(pMnode->pSdb, pIter);
×
598
      break;
×
599
    }
600

601
    sdbRelease(pMnode->pSdb, pDb);
×
602
  }
603

604
  if (exist) {
×
605
    TAOS_CHECK_GOTO(TSDB_CODE_MNODE_ENCRYPT_ALGR_IN_USE, &lino, _OVER);
×
606
  }
607

608
  TAOS_CHECK_GOTO(mndDropEncryptAlgr(pMnode, pReq, pObj), &lino, _OVER);
×
609
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
×
610

NEW
611
  if (tsAuditLevel >= AUDIT_LEVEL_CLUSTER) {
×
NEW
612
    int64_t tse = taosGetTimestampMs();
×
NEW
613
    double  duration = (double)(tse - tss);
×
NEW
614
    duration = duration / 1000;
×
NEW
615
    auditRecord(pReq, pMnode->clusterId, "dropEncryptAlgr", "", dropReq.algorithmId, dropReq.sql, dropReq.sqlLen,
×
616
                duration, 0);
617
  }
618

619
_OVER:
×
620
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
621
    mError("algr:%s, failed to drop at line %d since %s", dropReq.algorithmId, lino, tstrerror(code));
×
622
  }
623

624
  mndReleaseEncryptAlgr(pMnode, pObj);
×
625
  tFreeSDropEncryptAlgrReq(&dropReq);
×
626
  TAOS_RETURN(code);
×
627
}
628

629
int32_t mndInitEncryptAlgr(SMnode *pMnode) {
516,103✔
630
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_ENCRYPT_ALGR, mndProcessCreateEncryptAlgrReq);
516,103✔
631
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_ENCRYPT_ALGORITHMS, mndRetrieveEncryptAlgr);
516,103✔
632
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_ENCRYPT_ALGR, mndProcessDropEncryptAlgrReq);
516,103✔
633
  mndSetMsgHandle(pMnode, TDMT_MND_BUILTIN_ENCRYPT_ALGR, mndProcessBuiltinReq);
516,103✔
634
  mndSetMsgHandle(pMnode, TDMT_MND_BUILTIN_ENCRYPT_ALGR_RSP, mndProcessBuiltinRsp);
516,103✔
635

636
  SSdbTable table = {
516,103✔
637
      .sdbType = SDB_ENCRYPT_ALGORITHMS,
638
      .keyType = SDB_KEY_INT32,
639
      .upgradeFp = (SdbUpgradeFp)mndUpgradeBuiltinEncryptAlgr,
640
      .encodeFp = (SdbEncodeFp)mndEncryptAlgrActionEncode,
641
      .decodeFp = (SdbDecodeFp)mndEncryptAlgrActionDecode,
642
      .insertFp = (SdbInsertFp)mndEncryptAlgrActionInsert,
643
      .updateFp = (SdbUpdateFp)mndEncryptAlgrActionUpdate,
644
      .deleteFp = (SdbDeleteFp)mndEncryptAlgrActionDelete,
645
  };
646

647
  return sdbSetTable(pMnode->pSdb, table);
516,103✔
648
}
649

650
void mndCleanupEncryptAlgr(SMnode *pMnode) { mDebug("mnd encrypt algorithms cleanup"); }
515,986✔
651

652
SEncryptAlgrObj *mndAcquireEncryptAlgrById(SMnode *pMnode, int64_t id) {
1,500,940✔
653
  SSdb        *pSdb = pMnode->pSdb;
1,500,940✔
654
  SEncryptAlgrObj *pAlgr = sdbAcquire(pSdb, SDB_ENCRYPT_ALGORITHMS, &id);
1,500,733✔
655
  if (pAlgr == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
1,500,940✔
656
    terrno = TSDB_CODE_SUCCESS;
1,498,146✔
657
  }
658
  return pAlgr;
1,500,940✔
659
}
660

661
void mndGetEncryptOsslAlgrNameById(SMnode *pMnode, int64_t id, char* out){
1,500,940✔
662
  SEncryptAlgrObj *obj = mndAcquireEncryptAlgrById(pMnode, id);
1,500,940✔
663
  if(obj != NULL){
1,500,940✔
664
    tstrncpy(out, obj->ossl_algr_name, TSDB_ENCRYPT_ALGR_NAME_LEN);
2,794✔
665
    mndReleaseEncryptAlgr(pMnode, obj);
2,794✔
666
  }
667
}
1,500,940✔
668

669
SEncryptAlgrObj *mndAcquireEncryptAlgrByAId(SMnode *pMnode, char* algorithm_id) {
4,211✔
670
  SSdb        *pSdb = pMnode->pSdb;
4,211✔
671
  SEncryptAlgrObj *pObj = NULL;
4,211✔
672
  void *pIter = NULL;
4,211✔
673
  while (1) {
674
    pIter = sdbFetch(pSdb, SDB_ENCRYPT_ALGORITHMS, pIter, (void **)&pObj);
13,727✔
675
    if (pIter == NULL) break;
13,727✔
676

677
    if (strncasecmp(pObj->algorithm_id, algorithm_id, TSDB_ENCRYPT_ALGR_NAME_LEN) == 0) {
12,141✔
678
      sdbCancelFetch(pSdb, pIter);
2,625✔
679
      break;
2,625✔
680
    }
681

682
    sdbRelease(pSdb, pObj);
9,516✔
683
  }
684
  return pObj;
4,211✔
685
}
686

687
void mndReleaseEncryptAlgr(SMnode *pMnode, SEncryptAlgrObj *pObj) {
2,794✔
688
  SSdb *pSdb = pMnode->pSdb;
2,794✔
689
  sdbRelease(pSdb, pObj);
2,794✔
690
  pObj = NULL;
2,794✔
691
}
2,794✔
692

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