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

taosdata / TDengine / #5071

17 May 2026 01:15AM UTC coverage: 63.054% (-10.3%) from 73.326%
#5071

push

travis-ci

web-flow
feat (TDgpt): Dynamic Model Synchronization Enhancements (#35344)

* refactor: do some internal refactor.

* fix: fix multiprocess sync issue.

* feat: add dynamic anomaly detection and forecasting services

* fix: log error message for undeploying model in exception handling

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* fix: handle undeploy when model exists only on disk

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/286aafa0-c3ce-4c27-b803-2707571e9dc1

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: guard dynamic registry concurrent access

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: tighten service list locking scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: restore prophet support and update tests per review feedback

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: improve test name and move copy inside lock scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* Potential fix for pull request finding

Co-au... (continued)

238317 of 377957 relevant lines covered (63.05%)

130539817.12 hits per line

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

35.43
/source/dnode/mnode/impl/src/mndRole.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 _DEFAULT_SOURCE
17
// clang-format off
18
#ifndef TD_ASTRA
19
#include <uv.h>
20
#endif
21
#include "crypt.h"
22
#include "mndRole.h"
23
#include "mndUser.h"
24
#include "audit.h"
25
#include "mndDb.h"
26
#include "mndMnode.h"
27
#include "mndPrivilege.h"
28
#include "mndShow.h"
29
#include "mndStb.h"
30
#include "mndSync.h"
31
#include "mndTopic.h"
32
#include "mndTrans.h"
33
#include "tbase64.h"
34

35
// clang-format on
36

37
#define MND_ROLE_VER_NUMBER  1
38
#define MND_ROLE_SYSROLE_VER PRIV_INFO_TABLE_VERSION
39

40
static SRoleMgmt roleMgmt = {0};
41

42
static int32_t  mndCreateDefaultRoles(SMnode *pMnode);
43
static int32_t  mndUpgradeDefaultRoles(SMnode *pMnode, int32_t version);
44
static SSdbRow *mndRoleActionDecode(SSdbRaw *pRaw);
45
static int32_t  mndRoleActionInsert(SSdb *pSdb, SRoleObj *pRole);
46
static int32_t  mndRoleActionDelete(SSdb *pSdb, SRoleObj *pRole);
47
static int32_t  mndRoleActionUpdate(SSdb *pSdb, SRoleObj *pOld, SRoleObj *pNew);
48
static int32_t  mndCreateRole(SMnode *pMnode, char *acct, SCreateRoleReq *pCreate, SRpcMsg *pReq);
49
static int32_t  mndProcessCreateRoleReq(SRpcMsg *pReq);
50
static int32_t  mndProcessAlterRoleReq(SRpcMsg *pReq);
51
static int32_t  mndProcessDropRoleReq(SRpcMsg *pReq);
52
static int32_t  mndProcessUpgradeRoleReq(SRpcMsg *pReq);
53
static int32_t  mndProcessUpgradeRoleRsp(SRpcMsg *pReq);
54
static int32_t  mndProcessGetRoleAuthReq(SRpcMsg *pReq);
55
static int32_t  mndRetrieveRoles(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
56
static void     mndCancelGetNextRole(SMnode *pMnode, void *pIter);
57
static int32_t  mndRetrievePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
58
static void     mndCancelGetNextPrivileges(SMnode *pMnode, void *pIter);
59
static int32_t  mndRetrieveColPrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
60
static void     mndCancelGetNextColPrivileges(SMnode *pMnode, void *pIter);
61

62
int32_t mndInitRole(SMnode *pMnode) {
404,399✔
63
  // role management init
64
  roleMgmt.lastUpd = taosGetTimestampMs();
404,399✔
65
  TAOS_CHECK_RETURN(taosThreadRwlockInit(&roleMgmt.rw, NULL));
404,399✔
66

67
  SSdbTable table = {
404,399✔
68
      .sdbType = SDB_ROLE,
69
      .keyType = SDB_KEY_BINARY,
70
      .deployFp = (SdbDeployFp)mndCreateDefaultRoles,
71
      .upgradeFp = (SdbUpgradeFp)mndUpgradeDefaultRoles,
72
      .encodeFp = (SdbEncodeFp)mndRoleActionEncode,
73
      .decodeFp = (SdbDecodeFp)mndRoleActionDecode,
74
      .insertFp = (SdbInsertFp)mndRoleActionInsert,
75
      .updateFp = (SdbUpdateFp)mndRoleActionUpdate,
76
      .deleteFp = (SdbDeleteFp)mndRoleActionDelete,
77
  };
78

79
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_ROLE, mndProcessCreateRoleReq);
404,399✔
80
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_ROLE, mndProcessDropRoleReq);
404,399✔
81
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_ROLE, mndProcessAlterRoleReq);
404,399✔
82
  mndSetMsgHandle(pMnode, TDMT_MND_UPGRADE_ROLE, mndProcessUpgradeRoleReq);
404,399✔
83
  mndSetMsgHandle(pMnode, TDMT_MND_UPGRADE_ROLE_RSP, mndProcessUpgradeRoleRsp);
404,399✔
84

85
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_ROLE, mndRetrieveRoles);
404,399✔
86
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_ROLE, mndCancelGetNextRole);
404,399✔
87
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_ROLE_PRIVILEGES, mndRetrievePrivileges);
404,399✔
88
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_ROLE_PRIVILEGES, mndCancelGetNextPrivileges);
404,399✔
89
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_ROLE_COL_PRIVILEGES, mndRetrieveColPrivileges);
404,399✔
90
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_ROLE_COL_PRIVILEGES, mndCancelGetNextColPrivileges);
404,399✔
91
  return sdbSetTable(pMnode->pSdb, table);
404,399✔
92
}
93

94
void mndCleanupRole(SMnode *pMnode) { (void)taosThreadRwlockDestroy(&roleMgmt.rw); }
404,399✔
95

96
int64_t mndGetRoleLastUpd() {
×
97
  int64_t lastUpd;
98
  (void)taosThreadRwlockRdlock(&roleMgmt.rw);
×
99
  lastUpd = roleMgmt.lastUpd;
×
100
  (void)taosThreadRwlockUnlock(&roleMgmt.rw);
×
101
  return lastUpd;
×
102
}
103

104
void mndSetRoleLastUpd(int64_t updateTime) {
×
105
  (void)taosThreadRwlockWrlock(&roleMgmt.rw);
×
106
  roleMgmt.lastUpd = updateTime;
×
107
  (void)taosThreadRwlockUnlock(&roleMgmt.rw);
×
108
}
×
109

110
bool mndNeedRetrieveRole(SUserObj *pUser) {
×
111
  bool result = false;
×
112
  if (taosHashGetSize(pUser->roles) > 0) {
×
113
    (void)taosThreadRwlockRdlock(&roleMgmt.rw);
×
114
    if (pUser->lastRoleRetrieve <= roleMgmt.lastUpd) result = true;
×
115
    (void)taosThreadRwlockUnlock(&roleMgmt.rw);
×
116
  }
117
  return result;
×
118
}
119

120
static int32_t mndFillSystemRolePrivileges(SMnode *pMnode, SRoleObj *pObj, uint32_t roleType) {
1,864,674✔
121
  int32_t       code = 0, lino = 0;
1,864,674✔
122
  SPrivInfoIter iter = {0};
1,864,674✔
123
  privInfoIterInit(&iter);
1,864,674✔
124

125
  char objKey[TSDB_PRIV_MAX_KEY_LEN] = {0};
1,864,674✔
126
  char dbFName[TSDB_DB_FNAME_LEN + 1] = {0};
1,864,674✔
127

128
  SPrivInfo *pPrivInfo = NULL;
1,864,674✔
129
  while (privInfoIterNext(&iter, &pPrivInfo)) {
292,753,818✔
130
    if ((pPrivInfo->sysType & roleType) == 0) continue;
290,889,144✔
131
    if (pPrivInfo->category == PRIV_CATEGORY_SYSTEM) {  // system privileges
104,421,744✔
132
      privAddType(&pObj->sysPrivs, pPrivInfo->privType);
55,318,662✔
133
    } else if (pPrivInfo->category == PRIV_CATEGORY_OBJECT) {  // object privileges
49,103,082✔
134
      snprintf(dbFName, TSDB_DB_FNAME_LEN, "1.%s", pPrivInfo->dbName[0] == 0 ? "*" : pPrivInfo->dbName);
49,103,082✔
135
      int32_t keyLen = privObjKeyF(pPrivInfo, dbFName, "*", objKey, sizeof(objKey));
49,103,082✔
136
      if (!pObj->objPrivs && !(pObj->objPrivs = taosHashInit(1, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true,
49,103,082✔
137
                                                             HASH_ENTRY_LOCK))) {
138
        TAOS_CHECK_EXIT(terrno);
×
139
      }
140
      SPrivObjPolicies *objPolicy = taosHashGet(pObj->objPrivs, objKey, keyLen + 1);
49,103,082✔
141
      if (objPolicy) {
49,103,082✔
142
        privAddType(&objPolicy->policy, pPrivInfo->privType);
33,253,353✔
143
      } else {
144
        SPrivObjPolicies policies = {0};
15,849,729✔
145
        privAddType(&policies.policy, pPrivInfo->privType);
15,849,729✔
146
        TAOS_CHECK_EXIT(taosHashPut(pObj->objPrivs, objKey, keyLen + 1, &policies, sizeof(policies)));
15,849,729✔
147
      }
148
    }
149
  }
150
_exit:
1,864,674✔
151
  if (code != 0) {
1,864,674✔
152
    mError("role, %s failed at line %d for %s since %s", __func__, lino, pObj->name, tstrerror(code));
×
153
  }
154
  TAOS_RETURN(code);
1,864,674✔
155
}
156

157
/**
158
 * system roles: SYSDBA/SYSSEC/SYSAUDIT/SYSINFO_0/SYSINFO_1
159
 */
160
static int32_t mndCreateDefaultRole(SMnode *pMnode, char *role, uint32_t roleType) {
3,961,014✔
161
  int32_t   code = 0, lino = 0;
3,961,014✔
162
  SRoleObj *pRole = NULL, *pNew = NULL;
3,961,014✔
163
  if (mndAcquireRole(pMnode, role, &pRole) == 0) {
3,961,014✔
164
    if (pRole->version < MND_ROLE_SYSROLE_VER) {
2,096,340✔
165
      mInfo("role:%s version:%" PRId64 " upgrade to version:%d", role, pRole->version, MND_ROLE_SYSROLE_VER);
×
166
      pNew = taosMemoryCalloc(1, sizeof(SRoleObj));
×
167
      if (pNew == NULL) {
×
168
        mndReleaseRole(pMnode, pRole);
×
169
        TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
170
      }
171
      snprintf(pNew->name, TSDB_ROLE_LEN, "%s", pRole->name);
×
172
      pNew->createdTime = pRole->createdTime;
×
173
      pNew->uid = pRole->uid;
×
174
      pNew->flag = pRole->flag;
×
175
      pNew->updateTime = taosGetTimestampMs();
×
176
      pNew->version = MND_ROLE_SYSROLE_VER;
×
177
      mndReleaseRole(pMnode, pRole);
×
178
    } else {
179
      mndReleaseRole(pMnode, pRole);
2,096,340✔
180
      return 0;
2,096,340✔
181
    }
182
  } else {
183
    pNew = taosMemoryCalloc(1, sizeof(SRoleObj));
1,864,674✔
184
    if (pNew == NULL) {
1,864,674✔
185
      TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
186
    }
187
    tstrncpy(pNew->name, role, TSDB_ROLE_LEN);
1,864,674✔
188
    pNew->uid = mndGenerateUid(pNew->name, strlen(pNew->name));
1,864,674✔
189
    pNew->createdTime = taosGetTimestampMs();
1,864,674✔
190
    pNew->updateTime = pNew->createdTime;
1,864,674✔
191
    pNew->enable = 1;
1,864,674✔
192
    pNew->sys = 1;
1,864,674✔
193
    pNew->version = MND_ROLE_SYSROLE_VER;
1,864,674✔
194
  }
195

196
  TAOS_CHECK_EXIT(mndFillSystemRolePrivileges(pMnode, pNew, roleType));
1,864,674✔
197

198
  SSdbRaw *pRaw = mndRoleActionEncode(pNew);
1,864,674✔
199
  if (pRaw == NULL) goto _exit;
1,864,674✔
200
  TAOS_CHECK_EXIT(sdbSetRawStatus(pRaw, SDB_STATUS_READY));
1,864,674✔
201

202
  mInfo("role:%s, will be created when deploying, raw:%p", pNew->name, pRaw);
1,864,674✔
203

204
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL, "create-role");
1,864,674✔
205
  if (pTrans == NULL) {
1,864,674✔
206
    sdbFreeRaw(pRaw);
×
207
    mError("role:%s, failed to create since %s", pNew->name, terrstr());
×
208
    TAOS_CHECK_EXIT(terrno);
×
209
  }
210
  mInfo("trans:%d, used to create role:%s", pTrans->id, pNew->name);
1,864,674✔
211

212
  if (mndTransAppendCommitlog(pTrans, pRaw) != 0) {
1,864,674✔
213
    mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
×
214
    TAOS_CHECK_EXIT(terrno);
×
215
  }
216
  TAOS_CHECK_EXIT(sdbSetRawStatus(pRaw, SDB_STATUS_READY));
1,864,674✔
217

218
  if (mndTransPrepare(pMnode, pTrans) != 0) {
1,864,674✔
219
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
220
    TAOS_CHECK_EXIT(terrno);
×
221
  }
222

223
_exit:
1,864,674✔
224
  if (pNew) {
1,864,674✔
225
    mndRoleFreeObj(pNew);
1,864,674✔
226
    taosMemoryFree(pNew);
1,864,674✔
227
  }
228
  mndTransDrop(pTrans);
1,864,674✔
229
  TAOS_RETURN(code);
1,864,674✔
230
}
231

232
static int32_t mndCreateDefaultRoles(SMnode *pMnode) {
660,169✔
233
  int32_t code = 0, lino = 0;
660,169✔
234
  TAOS_CHECK_EXIT(mndCreateDefaultRole(pMnode, TSDB_ROLE_SYSDBA, T_ROLE_SYSDBA));
660,169✔
235
  TAOS_CHECK_EXIT(mndCreateDefaultRole(pMnode, TSDB_ROLE_SYSSEC, T_ROLE_SYSSEC));
660,169✔
236
  TAOS_CHECK_EXIT(mndCreateDefaultRole(pMnode, TSDB_ROLE_SYSAUDIT, T_ROLE_SYSAUDIT));
660,169✔
237
  TAOS_CHECK_EXIT(mndCreateDefaultRole(pMnode, TSDB_ROLE_SYSAUDIT_LOG, T_ROLE_SYSAUDIT_LOG));
660,169✔
238
  TAOS_CHECK_EXIT(mndCreateDefaultRole(pMnode, TSDB_ROLE_SYSINFO_0, T_ROLE_SYSINFO_0));
660,169✔
239
  TAOS_CHECK_EXIT(mndCreateDefaultRole(pMnode, TSDB_ROLE_SYSINFO_1, T_ROLE_SYSINFO_1));
660,169✔
240
_exit:
660,169✔
241
  TAOS_RETURN(code);
660,169✔
242
}
243

244
static int32_t tSerializeSRoleObj(void *buf, int32_t bufLen, SRoleObj *pObj) {
13,184,880✔
245
  int32_t  code = 0, lino = 0;
13,184,880✔
246
  int32_t  tlen = 0;
13,184,880✔
247
  SEncoder encoder = {0};
13,184,880✔
248
  tEncoderInit(&encoder, buf, bufLen);
13,184,880✔
249

250
  TAOS_CHECK_EXIT(tStartEncode(&encoder));
13,184,880✔
251
  TAOS_CHECK_EXIT(tEncodeCStr(&encoder, pObj->name));
26,369,760✔
252
  TAOS_CHECK_EXIT(tEncodeI64v(&encoder, pObj->createdTime));
26,369,760✔
253
  TAOS_CHECK_EXIT(tEncodeI64v(&encoder, pObj->updateTime));
26,369,760✔
254
  TAOS_CHECK_EXIT(tEncodeI64v(&encoder, pObj->uid));
26,369,760✔
255
  TAOS_CHECK_EXIT(tEncodeI64v(&encoder, pObj->version));
26,369,760✔
256
  TAOS_CHECK_EXIT(tEncodeU8(&encoder, pObj->flag));
26,369,760✔
257

258
  TAOS_CHECK_EXIT(tSerializePrivSysObjPolicies(&encoder, &pObj->sysPrivs, pObj->objPrivs));
13,184,880✔
259

260
  TAOS_CHECK_EXIT(tSerializePrivTblPolicies(&encoder, pObj->selectTbs));
13,184,880✔
261
  TAOS_CHECK_EXIT(tSerializePrivTblPolicies(&encoder, pObj->insertTbs));
13,184,880✔
262
  TAOS_CHECK_EXIT(tSerializePrivTblPolicies(&encoder, pObj->updateTbs));
13,184,880✔
263
  TAOS_CHECK_EXIT(tSerializePrivTblPolicies(&encoder, pObj->deleteTbs));
13,184,880✔
264

265
  int32_t nParentRoles = taosHashGetSize(pObj->parentRoles);
13,184,880✔
266
  TAOS_CHECK_EXIT(tEncodeI32v(&encoder, nParentRoles));
13,184,880✔
267
  if (nParentRoles > 0) {
13,184,880✔
268
    void *pIter = NULL;
×
269
    while ((pIter = taosHashIterate(pObj->parentRoles, pIter))) {
×
270
      char *roleName = taosHashGetKey(pIter, NULL);
×
271
      TAOS_CHECK_EXIT(tEncodeCStr(&encoder, roleName));
×
272
    }
273
  }
274
  int32_t nSubRoles = taosHashGetSize(pObj->subRoles);
13,184,880✔
275
  TAOS_CHECK_EXIT(tEncodeI32v(&encoder, nSubRoles));
13,184,880✔
276
  if (nSubRoles > 0) {
13,184,880✔
277
    void *pIter = NULL;
×
278
    while ((pIter = taosHashIterate(pObj->subRoles, pIter))) {
×
279
      char *roleName = taosHashGetKey(pIter, NULL);
×
280
      TAOS_CHECK_EXIT(tEncodeCStr(&encoder, roleName));
×
281
    }
282
  }
283

284
  tEndEncode(&encoder);
13,184,880✔
285
  tlen = encoder.pos;
13,184,880✔
286
_exit:
13,184,880✔
287
  tEncoderClear(&encoder);
13,184,880✔
288
  if (code < 0) {
13,184,880✔
289
    mError("role:%s, %s failed at line %d since %s", pObj->name, __func__, lino, tstrerror(code));
×
290
    TAOS_RETURN(code);
×
291
  }
292

293
  return tlen;
13,184,880✔
294
}
295

296
void tFreePrivTblPolicies(SHashObj **ppHash) {
×
297
  if (*ppHash) {
×
298
    void *pIter = NULL;
×
299
    while ((pIter = taosHashIterate(*ppHash, pIter))) {
×
300
      int32_t vlen = taosHashGetValueSize(pIter);  // value is NULL for key 1.db.* or 1.*.*
×
301
      if (vlen == 0) continue;
×
302
      privTblPoliciesFree((SPrivTblPolicies *)pIter);
303
    }
304
    taosHashCleanup(*ppHash);
×
305
    *ppHash = NULL;
×
306
  }
307
}
×
308

309
static int32_t tDeserializeSRoleObj(void *buf, int32_t bufLen, SRoleObj *pObj) {
2,426,394✔
310
  int32_t  code = 0, lino = 0;
2,426,394✔
311
  SDecoder decoder = {0};
2,426,394✔
312
  tDecoderInit(&decoder, buf, bufLen);
2,426,394✔
313

314
  TAOS_CHECK_EXIT(tStartDecode(&decoder));
2,426,394✔
315

316
  TAOS_CHECK_EXIT(tDecodeCStrTo(&decoder, pObj->name));
2,426,394✔
317
  TAOS_CHECK_EXIT(tDecodeI64v(&decoder, &pObj->createdTime));
4,852,788✔
318
  TAOS_CHECK_EXIT(tDecodeI64v(&decoder, &pObj->updateTime));
4,852,788✔
319
  TAOS_CHECK_EXIT(tDecodeI64v(&decoder, &pObj->uid));
4,852,788✔
320
  TAOS_CHECK_EXIT(tDecodeI64v(&decoder, &pObj->version));
4,852,788✔
321
  TAOS_CHECK_EXIT(tDecodeU8(&decoder, &pObj->flag));
4,852,788✔
322

323
  TAOS_CHECK_EXIT(tDeserializePrivSysObjPolicies(&decoder, &pObj->sysPrivs, &pObj->objPrivs));
2,426,394✔
324

325
  TAOS_CHECK_EXIT(tDeserializePrivTblPolicies(&decoder, &pObj->selectTbs));
2,426,394✔
326
  TAOS_CHECK_EXIT(tDeserializePrivTblPolicies(&decoder, &pObj->insertTbs));
2,426,394✔
327
  TAOS_CHECK_EXIT(tDeserializePrivTblPolicies(&decoder, &pObj->updateTbs));
2,426,394✔
328
  TAOS_CHECK_EXIT(tDeserializePrivTblPolicies(&decoder, &pObj->deleteTbs));
2,426,394✔
329

330
  char    roleName[TSDB_ROLE_LEN] = {0};
2,426,394✔
331
  int32_t nParentRoles = 0;
2,426,394✔
332
  TAOS_CHECK_EXIT(tDecodeI32v(&decoder, &nParentRoles));
2,426,394✔
333
  if (nParentRoles > 0) {
2,426,394✔
334
    if (!(pObj->parentRoles =
×
335
              taosHashInit(nParentRoles, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK))) {
×
336
      TAOS_CHECK_EXIT(terrno);
×
337
    }
338
    for (int32_t i = 0; i < nParentRoles; ++i) {
×
339
      TAOS_CHECK_EXIT(tDecodeCStrTo(&decoder, roleName));
×
340
      TAOS_CHECK_EXIT(taosHashPut(pObj->parentRoles, roleName, strlen(roleName) + 1, NULL, 0));
×
341
    }
342
  }
343
  int32_t nSubRoles = 0;
2,426,394✔
344
  TAOS_CHECK_EXIT(tDecodeI32v(&decoder, &nSubRoles));
2,426,394✔
345
  if (nSubRoles > 0) {
2,426,394✔
346
    if (!(pObj->subRoles =
×
347
              taosHashInit(nSubRoles, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK))) {
×
348
      TAOS_CHECK_EXIT(terrno);
×
349
    }
350
    for (int32_t i = 0; i < nSubRoles; ++i) {
×
351
      TAOS_CHECK_EXIT(tDecodeCStrTo(&decoder, roleName));
×
352
      TAOS_CHECK_EXIT(taosHashPut(pObj->subRoles, roleName, strlen(roleName) + 1, NULL, 0));
×
353
    }
354
  }
355

356
_exit:
2,426,394✔
357
  tEndDecode(&decoder);
2,426,394✔
358
  tDecoderClear(&decoder);
2,426,394✔
359
  if (code < 0) {
2,426,394✔
360
    mError("role, %s failed at line %d since %s, row:%p", __func__, lino, tstrerror(code), pObj);
×
361
  }
362
  TAOS_RETURN(code);
2,426,394✔
363
}
364

365
SSdbRaw *mndRoleActionEncode(SRoleObj *pObj) {
6,592,440✔
366
  int32_t  code = 0, lino = 0;
6,592,440✔
367
  void    *buf = NULL;
6,592,440✔
368
  SSdbRaw *pRaw = NULL;
6,592,440✔
369
  int32_t  tlen = tSerializeSRoleObj(NULL, 0, pObj);
6,592,440✔
370
  if (tlen < 0) {
6,592,440✔
371
    TAOS_CHECK_EXIT(tlen);
×
372
  }
373
  int32_t size = sizeof(int32_t) + tlen;
6,592,440✔
374
  if (!(pRaw = sdbAllocRaw(SDB_ROLE, MND_ROLE_VER_NUMBER, size))) {
6,592,440✔
375
    TAOS_CHECK_EXIT(terrno);
×
376
  }
377
  if (!(buf = taosMemoryMalloc(tlen))) {
6,592,440✔
378
    TAOS_CHECK_EXIT(terrno);
×
379
  }
380
  if ((tlen = tSerializeSRoleObj(buf, tlen, pObj)) < 0) {
6,592,440✔
381
    TAOS_CHECK_EXIT(tlen);
×
382
  }
383

384
  int32_t dataPos = 0;
6,592,440✔
385
  SDB_SET_INT32(pRaw, dataPos, tlen, _exit);
6,592,440✔
386
  SDB_SET_BINARY(pRaw, dataPos, buf, tlen, _exit);
6,592,440✔
387
  SDB_SET_DATALEN(pRaw, dataPos, _exit);
6,592,440✔
388
_exit:
6,592,440✔
389
  taosMemoryFreeClear(buf);
6,592,440✔
390
  if (code != TSDB_CODE_SUCCESS) {
6,592,440✔
391
    terrno = code;
×
392
    mError("role, failed at line %d to encode to raw:%p since %s", lino, pRaw, tstrerror(code));
×
393
    sdbFreeRaw(pRaw);
×
394
    return NULL;
×
395
  }
396
  mTrace("role, encode to raw:%p, row:%p", pRaw, pObj);
6,592,440✔
397
  return pRaw;
6,592,440✔
398
}
399

400
static SSdbRow *mndRoleActionDecode(SSdbRaw *pRaw) {
2,426,394✔
401
  int32_t   code = 0, lino = 0;
2,426,394✔
402
  SSdbRow  *pRow = NULL;
2,426,394✔
403
  SRoleObj *pObj = NULL;
2,426,394✔
404
  void     *buf = NULL;
2,426,394✔
405

406
  int8_t sver = 0;
2,426,394✔
407
  TAOS_CHECK_EXIT(sdbGetRawSoftVer(pRaw, &sver));
2,426,394✔
408

409
  if (sver != MND_ROLE_VER_NUMBER) {
2,426,394✔
410
    mError("role, read invalid ver, data ver: %d, curr ver: %d", sver, MND_ROLE_VER_NUMBER);
×
411
    TAOS_CHECK_EXIT(TSDB_CODE_SDB_INVALID_DATA_VER);
×
412
  }
413

414
  if (!(pRow = sdbAllocRow(sizeof(SRoleObj)))) {
2,426,394✔
415
    TAOS_CHECK_EXIT(terrno);
×
416
  }
417

418
  if (!(pObj = sdbGetRowObj(pRow))) {
2,426,394✔
419
    TAOS_CHECK_EXIT(terrno);
×
420
  }
421

422
  int32_t tlen;
2,421,546✔
423
  int32_t dataPos = 0;
2,426,394✔
424
  SDB_GET_INT32(pRaw, dataPos, &tlen, _exit);
2,426,394✔
425
  if (!(buf = taosMemoryMalloc(tlen + 1))) {
2,426,394✔
426
    TAOS_CHECK_EXIT(terrno);
×
427
  }
428
  SDB_GET_BINARY(pRaw, dataPos, buf, tlen, _exit);
2,426,394✔
429
  TAOS_CHECK_EXIT(tDeserializeSRoleObj(buf, tlen, pObj));
2,426,394✔
430
  if (pObj->version < MND_ROLE_SYSROLE_VER) {
2,426,394✔
431
  }
432
  taosInitRWLatch(&pObj->lock);
2,426,394✔
433
_exit:
2,426,394✔
434
  taosMemoryFreeClear(buf);
2,426,394✔
435
  if (code != TSDB_CODE_SUCCESS) {
2,426,394✔
436
    terrno = code;
×
437
    mError("role, failed at line %d to decode from raw:%p since %s", lino, pRaw, tstrerror(code));
×
438
    mndRoleFreeObj(pObj);
×
439
    taosMemoryFreeClear(pRow);
×
440
    return NULL;
×
441
  }
442
  mTrace("role, decode from raw:%p, row:%p", pRaw, pObj);
2,426,394✔
443
  return pRow;
2,426,394✔
444
}
445

446
void mndRoleFreeObj(SRoleObj *pObj) {
5,404,991✔
447
  if (pObj) {
5,404,991✔
448
    taosHashCleanup(pObj->objPrivs);
5,404,991✔
449
    taosHashCleanup(pObj->selectTbs);
5,404,991✔
450
    taosHashCleanup(pObj->insertTbs);
5,404,991✔
451
    taosHashCleanup(pObj->updateTbs);
5,404,991✔
452
    taosHashCleanup(pObj->deleteTbs);
5,404,991✔
453
    taosHashCleanup(pObj->parentRoles);
5,404,991✔
454
    taosHashCleanup(pObj->subRoles);
5,404,991✔
455
    pObj->objPrivs = NULL;
5,404,991✔
456
    pObj->selectTbs = NULL;
5,404,991✔
457
    pObj->insertTbs = NULL;
5,404,991✔
458
    pObj->updateTbs = NULL;
5,404,991✔
459
    pObj->deleteTbs = NULL;
5,404,991✔
460
    pObj->parentRoles = NULL;
5,404,991✔
461
    pObj->subRoles = NULL;
5,404,991✔
462
  }
463
}
5,404,991✔
464

465
static int32_t mndRoleActionInsert(SSdb *pSdb, SRoleObj *pObj) {
2,426,394✔
466
  mTrace("role:%s, perform insert action, row:%p", pObj->name, pObj);
2,426,394✔
467
  return 0;
2,426,394✔
468
}
469

470
static int32_t mndRoleActionDelete(SSdb *pSdb, SRoleObj *pObj) {
2,426,394✔
471
  mTrace("role:%s, perform delete action, row:%p", pObj->name, pObj);
2,426,394✔
472
  mndRoleFreeObj(pObj);
2,426,394✔
473
  return 0;
2,426,394✔
474
}
475

476
static int32_t mndRoleActionUpdate(SSdb *pSdb, SRoleObj *pOld, SRoleObj *pNew) {
×
477
  mTrace("role:%s, perform update action, old row:%p new row:%p", pOld->name, pOld, pNew);
×
478
  taosWLockLatch(&pOld->lock);
×
479
  pOld->updateTime = pNew->updateTime;
×
480
  pOld->sysPrivs = pNew->sysPrivs;
×
481
  pOld->version = pNew->version;
×
482
  pOld->flag = pNew->flag;
×
483
  TSWAP(pOld->objPrivs, pNew->objPrivs);
×
484
  TSWAP(pOld->selectTbs, pNew->selectTbs);
×
485
  TSWAP(pOld->insertTbs, pNew->insertTbs);
×
486
  TSWAP(pOld->updateTbs, pNew->updateTbs);
×
487
  TSWAP(pOld->deleteTbs, pNew->deleteTbs);
×
488
  TSWAP(pOld->parentRoles, pNew->parentRoles);
×
489
  TSWAP(pOld->subRoles, pNew->subRoles);
×
490
  taosWUnLockLatch(&pOld->lock);
×
491
  return 0;
×
492
}
493

494
int32_t mndAcquireRole(SMnode *pMnode, const char *roleName, SRoleObj **ppRole) {
77,732,741✔
495
  int32_t code = 0;
77,732,741✔
496
  SSdb   *pSdb = pMnode->pSdb;
77,732,741✔
497

498
  *ppRole = sdbAcquire(pSdb, SDB_ROLE, roleName);
77,733,861✔
499
  if (*ppRole == NULL) {
77,733,861✔
500
    if (terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
3,005,380✔
501
      code = TSDB_CODE_MND_ROLE_NOT_EXIST;
3,005,380✔
502
    } else {
503
      code = TSDB_CODE_MND_ROLE_NOT_AVAILABLE;
×
504
    }
505
  }
506
  TAOS_RETURN(code);
77,733,861✔
507
}
508

509
void mndReleaseRole(SMnode *pMnode, SRoleObj *pRole) {
75,869,187✔
510
  SSdb *pSdb = pMnode->pSdb;
75,869,187✔
511
  sdbRelease(pSdb, pRole);
75,869,187✔
512
}
75,869,187✔
513

514
static int32_t mndCreateRole(SMnode *pMnode, char *acct, SCreateRoleReq *pCreate, SRpcMsg *pReq) {
×
515
  int32_t  code = 0, lino = 0;
×
516
  SRoleObj obj = {0};
×
517

518
  tstrncpy(obj.name, pCreate->name, TSDB_ROLE_LEN);
×
519
  obj.createdTime = taosGetTimestampMs();
×
520
  obj.updateTime = obj.createdTime;
×
521
  obj.uid = mndGenerateUid(obj.name, strlen(obj.name));
×
522
  obj.version = 1;
×
523
  obj.enable = 1;
×
524
  obj.sys = 0;
×
525
  // TODO: assign default privileges
526

527
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_ROLE, pReq, "create-role");
×
528
  if (pTrans == NULL) {
×
529
    TAOS_CHECK_EXIT(terrno);
×
530
  }
531
  mInfo("trans:%d, used to create role:%s", pTrans->id, pCreate->name);
×
532

533
  SSdbRaw *pCommitRaw = mndRoleActionEncode(&obj);
×
534
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
×
535
    mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
×
536
    TAOS_CHECK_EXIT(terrno);
×
537
  }
538
  TAOS_CHECK_EXIT(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY));
×
539
  TAOS_CHECK_EXIT(mndTransPrepare(pMnode, pTrans));
×
540
_exit:
×
541
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
542
    mError("role:%s, failed at line %d to create role, since %s", obj.name, lino, tstrerror(code));
×
543
  }
544
  mndRoleFreeObj(&obj);
×
545
  mndTransDrop(pTrans);
×
546
  TAOS_RETURN(code);
×
547
}
548

549
static int32_t mndProcessCreateRoleReq(SRpcMsg *pReq) {
×
550
  SMnode        *pMnode = pReq->info.node;
×
551
  int32_t        code = 0, lino = 0;
×
552
  SRoleObj      *pRole = NULL;
×
553
  SUserObj      *pOperUser = NULL;
×
554
  SUserObj      *pUser = NULL;
×
555
  SCreateRoleReq createReq = {0};
×
556
  int64_t        tss = taosGetTimestampMs();
×
557

558
  if (tDeserializeSCreateRoleReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
×
559
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_MSG);
×
560
  }
561
  if ((code = mndAcquireRole(pMnode, createReq.name, &pRole)) == 0) {
×
562
    if (createReq.ignoreExists) {
×
563
      mInfo("role:%s, already exist, ignore exist is set", createReq.name);
×
564
      goto _exit;
×
565
    } else {
566
      TAOS_CHECK_EXIT(TSDB_CODE_MND_ROLE_ALREADY_EXIST);
×
567
    }
568
  } else {
569
    if ((code = terrno) == TSDB_CODE_MND_ROLE_NOT_EXIST) {
×
570
      // continue
571
    } else {
572
      goto _exit;
×
573
    }
574
  }
575
  code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser);
×
576
  if (pOperUser == NULL) {
×
577
    TAOS_CHECK_EXIT(TSDB_CODE_MND_NO_USER_FROM_CONN);
×
578
  }
579

580
  mInfo("role:%s, start to create by %s", createReq.name, pOperUser->user);
×
581

582
  // TAOS_CHECK_EXIT(mndCheckOperPrivilege(pMnode, RPC_MSG_USER(pReq), MND_OPER_CREATE_ROLE));
583
  TAOS_CHECK_EXIT(mndCheckSysObjPrivilege(pMnode, pOperUser, RPC_MSG_TOKEN(pReq), PRIV_ROLE_CREATE, 0, 0, NULL, NULL));
×
584

585
  if (createReq.name[0] == 0) {
×
586
    TAOS_CHECK_EXIT(TSDB_CODE_MND_ROLE_INVALID_FORMAT);
×
587
  }
588
  code = mndAcquireUser(pMnode, createReq.name, &pUser);
×
589
  if (pUser != NULL) {
×
590
    TAOS_CHECK_EXIT(TSDB_CODE_MND_USER_ALREADY_EXIST);
×
591
  }
592
  if (sdbGetSize(pMnode->pSdb, SDB_ROLE) >= TSDB_MAX_ROLES) {
×
593
    mError("role:%s, failed to create since reach max role limit %d", createReq.name, TSDB_MAX_ROLES);
×
594
    TAOS_CHECK_EXIT(TSDB_CODE_MND_TOO_MANY_ROLES);
×
595
  }
596
  TAOS_CHECK_EXIT(mndCreateRole(pMnode, pOperUser->acct, &createReq, pReq));
×
597
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
×
598

599
  if (tsAuditLevel >= AUDIT_LEVEL_CLUSTER) {
×
600
    char    detail[128] = {0};
×
601
    int32_t len = snprintf(detail, sizeof(detail), "operUser:%s", pOperUser->user);
×
602

603
    int64_t tse = taosGetTimestampMs();
×
604
    double  duration = (double)(tse - tss);
×
605
    duration = duration / 1000;
×
606
    auditRecord(pReq, pMnode->clusterId, "createRole", "", createReq.name, detail, strlen(detail), duration, 0);
×
607
  }
608
_exit:
×
609
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
610
    mError("role:%s, failed to create at line %d since %s", createReq.name, lino, tstrerror(code));
×
611
  }
612
  mndReleaseUser(pMnode, pUser);
×
613
  mndReleaseUser(pMnode, pOperUser);
×
614
  mndReleaseRole(pMnode, pRole);
×
615
  tFreeSCreateRoleReq(&createReq);
×
616
  TAOS_RETURN(code);
×
617
}
618

619
static int32_t mndDropParentRole(SMnode *pMnode, STrans *pTrans, SRoleObj *pObj) {  // TODO
×
620
  return 0;
×
621
}
622

623
static int32_t mndDropRole(SMnode *pMnode, SRpcMsg *pReq, SRoleObj *pObj) {
×
624
  int32_t code = 0, lino = 0;
×
625
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_ROLE, pReq, "drop-role");
×
626
  if (pTrans == NULL) {
×
627
    mError("role:%s, failed to drop since %s", pObj->name, terrstr());
×
628
    TAOS_CHECK_EXIT(terrno);
×
629
  }
630
  mInfo("trans:%d, used to drop role:%s", pTrans->id, pObj->name);
×
631

632
  SSdbRaw *pCommitRaw = mndRoleActionEncode(pObj);
×
633
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
×
634
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
×
635
    TAOS_CHECK_EXIT(terrno);
×
636
  }
637
  TAOS_CHECK_EXIT(sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED));
×
638
  TAOS_CHECK_EXIT(mndDropParentRole(pMnode, pTrans, pObj));
×
639
  TAOS_CHECK_EXIT(mndUserDropRole(pMnode, pTrans, pObj));
×
640
  TAOS_CHECK_EXIT(mndTransPrepare(pMnode, pTrans));
×
641
  mndSetRoleLastUpd(taosGetTimestampMs());
×
642
_exit:
×
643
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
644
    mError("role:%s, failed to drop at line:%d since %s", pObj->name, lino, tstrerror(code));
×
645
  }
646
  mndTransDrop(pTrans);
×
647
  TAOS_RETURN(0);
×
648
}
649

650
static int32_t mndProcessDropRoleReq(SRpcMsg *pReq) {
×
651
  SMnode      *pMnode = pReq->info.node;
×
652
  int32_t      code = 0, lino = 0;
×
653
  SRoleObj    *pObj = NULL;
×
654
  SUserObj    *pOperUser = NULL;
×
655
  SDropRoleReq dropReq = {0};
×
656
  int64_t      tss = taosGetTimestampMs();
×
657

658
  TAOS_CHECK_EXIT(tDeserializeSDropRoleReq(pReq->pCont, pReq->contLen, &dropReq));
×
659

660
  if (dropReq.name[0] == 0) {
×
661
    TAOS_CHECK_EXIT(TSDB_CODE_MND_ROLE_INVALID_FORMAT);
×
662
  }
663

664
  code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser);
×
665
  if (pOperUser == NULL) {
×
666
    TAOS_CHECK_EXIT(TSDB_CODE_MND_NO_USER_FROM_CONN);
×
667
  }
668
  mInfo("role:%s, start to drop", dropReq.name);
×
669

670
  // TAOS_CHECK_EXIT(mndCheckOperPrivilege(pMnode, RPC_MSG_USER(pReq), MND_OPER_DROP_ROLE));
671
  TAOS_CHECK_EXIT(mndCheckSysObjPrivilege(pMnode, pOperUser, RPC_MSG_TOKEN(pReq), PRIV_ROLE_DROP, 0, 0, NULL, NULL));
×
672

673
  if ((code = mndAcquireRole(pMnode, dropReq.name, &pObj))) {
×
674
    if (dropReq.ignoreNotExists) {
×
675
      code = 0;
×
676
      goto _exit;
×
677
    }
678
    TAOS_CHECK_EXIT(code);
×
679
  }
680

681
  TAOS_CHECK_EXIT(mndDropRole(pMnode, pReq, pObj));
×
682
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
×
683

684
  if (tsAuditLevel >= AUDIT_LEVEL_CLUSTER) {
×
685
    int64_t tse = taosGetTimestampMs();
×
686
    double  duration = (double)(tse - tss);
×
687
    duration = duration / 1000;
×
688
    auditRecord(pReq, pMnode->clusterId, "dropRole", "", dropReq.name, dropReq.sql, dropReq.sqlLen, duration, 0);
×
689
  }
690
_exit:
×
691
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
692
    mError("role:%s, failed to drop at line %d since %s", dropReq.name, lino, tstrerror(code));
×
693
  }
694
  mndReleaseRole(pMnode, pObj);
×
695
  mndReleaseUser(pMnode, pOperUser);
×
696
  tFreeSDropRoleReq(&dropReq);
×
697
  TAOS_RETURN(code);
×
698
}
699

700
static int32_t mndAlterRole(SMnode *pMnode, SRpcMsg *pReq, SRoleObj *pObj) {
×
701
  int32_t code = 0, lino = 0;
×
702
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_ROLE, pReq, "alter-role");
×
703
  if (pTrans == NULL) {
×
704
    mError("role:%s, failed to alter since %s", pObj->name, terrstr());
×
705
    TAOS_CHECK_EXIT(terrno);
×
706
  }
707
  mInfo("trans:%d, used to alter role:%s", pTrans->id, pObj->name);
×
708

709
  SSdbRaw *pCommitRaw = mndRoleActionEncode(pObj);
×
710
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
×
711
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
×
712
    TAOS_CHECK_EXIT(terrno);
×
713
  }
714
  TAOS_CHECK_EXIT(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY));
×
715
  TAOS_CHECK_EXIT(mndTransPrepare(pMnode, pTrans));
×
716
_exit:
×
717
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
718
    mError("role:%s, failed to alter at line:%d since %s", pObj->name, lino, tstrerror(code));
×
719
  }
720
  mndTransDrop(pTrans);
×
721
  TAOS_RETURN(0);
×
722
}
723

724
int32_t mndRoleDupObj(SRoleObj *pOld, SRoleObj *pNew) {
×
725
  int32_t code = 0, lino = 0;
×
726
  snprintf(pNew->name, TSDB_ROLE_LEN, "%s", pOld->name);
×
727
  pNew->createdTime = pOld->createdTime;
×
728
  pNew->uid = pOld->uid;
×
729
  pNew->version = pOld->version + 1;
×
730
  pNew->flag = pOld->flag;
×
731
  pNew->updateTime = taosGetTimestampMs();
×
732
  pNew->sysPrivs = pOld->sysPrivs;
×
733

734
  taosRLockLatch(&pOld->lock);
×
735
  TAOS_CHECK_EXIT(mndDupPrivObjHash(pOld->objPrivs, &pNew->objPrivs));
×
736
  TAOS_CHECK_EXIT(mndDupPrivTblHash(pOld->selectTbs, &pNew->selectTbs, false));
×
737
  TAOS_CHECK_EXIT(mndDupPrivTblHash(pOld->insertTbs, &pNew->insertTbs, false));
×
738
  TAOS_CHECK_EXIT(mndDupPrivTblHash(pOld->updateTbs, &pNew->updateTbs, false));
×
739
  TAOS_CHECK_EXIT(mndDupPrivTblHash(pOld->deleteTbs, &pNew->deleteTbs, false));
×
740
  // TODO: alterTbs?
741
  TAOS_CHECK_EXIT(mndDupRoleHash(pOld->parentRoles, &pNew->parentRoles));
×
742
  TAOS_CHECK_EXIT(mndDupRoleHash(pOld->subRoles, &pNew->subRoles));
×
743
_exit:
×
744
  taosRUnLockLatch(&pOld->lock);
×
745
  if (code < 0) {
×
746
    mError("role:%s, failed at line %d to dup obj since %s", pOld->name, lino, tstrerror(code));
×
747
  }
748
  TAOS_RETURN(code);
×
749
}
750

751
static bool mndIsRoleChanged(SRoleObj *pOld, SAlterRoleReq *pAlterReq) {
×
752
  switch (pAlterReq->alterType) {
×
753
    case TSDB_ALTER_ROLE_LOCK: {
×
754
      if ((pAlterReq->lock && !pOld->enable) || (!pAlterReq->lock && pOld->enable)) {
×
755
        return false;
×
756
      }
757
      break;
×
758
    }
759
    default:
×
760
      break;
×
761
  }
762
  return true;
×
763
}
764

765
#ifdef TD_ENTERPRISE
766
extern int32_t mndAlterRoleInfo(SMnode *pMnode, SUserObj *pOperUser, const char *token, SRoleObj *pOld, SRoleObj *pNew,
767
                                SAlterRoleReq *pAlterReq);
768
#endif
769

770
static int32_t mndProcessAlterRoleReq(SRpcMsg *pReq) {
1,113,923✔
771
  int32_t       code = 0, lino = 0;
1,113,923✔
772
  SMnode       *pMnode = pReq->info.node;
1,113,923✔
773
  SRoleObj     *pObj = NULL;
1,113,923✔
774
  SRoleObj      newObj = {0};
1,113,923✔
775
  SUserObj     *pOperUser = NULL;
1,113,923✔
776
  SAlterRoleReq alterReq = {0};
1,113,923✔
777
  bool          alterUser = false;
1,113,923✔
778
  int64_t       tss = taosGetTimestampMs();
1,113,923✔
779

780
  TAOS_CHECK_EXIT(tDeserializeSAlterRoleReq(pReq->pCont, pReq->contLen, &alterReq));
1,113,923✔
781

782
  mInfo("role:%s, start to alter, flag:%u", alterReq.principal, alterReq.flag);
1,113,923✔
783
  TAOS_CHECK_EXIT(mndCheckOperPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_ALTER_ROLE));
1,113,923✔
784

785
  TAOS_CHECK_EXIT(mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser));
1,113,923✔
786

787
  if (alterReq.principal[0] == 0) {
1,113,923✔
788
    TAOS_CHECK_EXIT(TSDB_CODE_MND_ROLE_INVALID_FORMAT);
×
789
  }
790

791
  if (mndAcquireRole(pMnode, alterReq.principal, &pObj) == 0) {
1,113,923✔
792
    if (alterReq.alterType == TSDB_ALTER_ROLE_ROLE) {
×
793
      TAOS_CHECK_EXIT(TSDB_CODE_OPS_NOT_SUPPORT);  // not support grant role to role yet
×
794
    }
795
  } else {
796
    if (alterReq.alterType == TSDB_ALTER_ROLE_LOCK) {
1,113,923✔
797
      TAOS_CHECK_EXIT(terrno);
×
798
    }
799
    alterUser = true;
1,113,923✔
800
  }
801

802
  if (alterUser) {
1,113,923✔
803
    mInfo("role:%s, not exist, will alter user instead", alterReq.principal);
1,113,923✔
804
    TAOS_CHECK_EXIT(mndAlterUserFromRole(pReq, pOperUser, &alterReq));
1,113,923✔
805
  } else if (mndIsRoleChanged(pObj, &alterReq)) {
×
806
    TAOS_CHECK_EXIT(mndRoleDupObj(pObj, &newObj));
×
807
#ifdef TD_ENTERPRISE
808
    TAOS_CHECK_EXIT(mndAlterRoleInfo(pMnode, pOperUser, RPC_MSG_TOKEN(pReq), pObj, &newObj, &alterReq));
×
809
#endif
810
    TAOS_CHECK_EXIT(mndAlterRole(pMnode, pReq, &newObj));
×
811
    if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
×
812
    mndSetRoleLastUpd(taosGetTimestampMs());
×
813
  }
814

815
  if (tsAuditLevel >= AUDIT_LEVEL_CLUSTER) {
×
816
    int64_t tse = taosGetTimestampMs();
×
817
    double  duration = (double)(tse - tss);
×
818
    duration = duration / 1000;
×
819
    auditRecord(pReq, pMnode->clusterId, "alterRole", "", alterReq.principal, alterReq.sql, alterReq.sqlLen, duration,
×
820
                0);
821
  }
822
_exit:
1,113,923✔
823
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1,113,923✔
824
    mError("role:%s, failed to alter at line %d since %s", alterReq.principal, lino, tstrerror(code));
2,552✔
825
  }
826
  mndReleaseUser(pMnode, pOperUser);
1,113,923✔
827
  mndReleaseRole(pMnode, pObj);
1,113,923✔
828
  mndRoleFreeObj(&newObj);
1,113,923✔
829
  tFreeSAlterRoleReq(&alterReq);
1,113,923✔
830
  TAOS_RETURN(code);
1,113,923✔
831
}
832

833
static int32_t mndUpgradeDefaultRoles(SMnode *pMnode, int32_t version) {
349,392✔
834
  int32_t code = 0, lino = 0;
349,392✔
835
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPGRADE_ROLE, .info.ahandle = 0, .info.notFreeAhandle = 1};
349,392✔
836
  SEpSet  epSet = {0};
349,392✔
837
  mndGetMnodeEpSet(pMnode, &epSet);
349,392✔
838
  TAOS_CHECK_EXIT(tmsgSendReq(&epSet, &rpcMsg));
349,392✔
839
_exit:
349,392✔
840
  if (code < 0) {
349,392✔
841
    mError("failed at line %d to upgrade roles since %s", lino, tstrerror(code));
×
842
  }
843
  TAOS_RETURN(code);
349,392✔
844
}
845

846
static int32_t mndProcessUpgradeRoleReq(SRpcMsg *pReq) {
349,390✔
847
  return mndCreateDefaultRoles(pReq->info.node);
349,390✔
848
}
849

850
static int32_t mndProcessUpgradeRoleRsp(SRpcMsg *pReq) { return 0; }
349,392✔
851

852
static int32_t mndProcessGetRoleAuthReq(SRpcMsg *pReq) { TAOS_RETURN(0); }
×
853

854
static int32_t mndRetrieveRoles(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
×
855
  SMnode   *pMnode = pReq->info.node;
×
856
  SSdb     *pSdb = pMnode->pSdb;
×
857
  int32_t   code = 0, lino = 0;
×
858
  int32_t   numOfRows = 0;
×
859
  SRoleObj *pObj = NULL;
×
860
  int32_t   cols = 0;
×
861
  int32_t   bufSize = TSDB_MAX_SUBROLE * TSDB_ROLE_LEN + VARSTR_HEADER_SIZE;
×
862
  char      tBuf[TSDB_MAX_SUBROLE * TSDB_ROLE_LEN + VARSTR_HEADER_SIZE] = {0};
×
863

864
  while (numOfRows < rows) {
×
865
    pShow->pIter = sdbFetch(pSdb, SDB_ROLE, pShow->pIter, (void **)&pObj);
×
866
    if (pShow->pIter == NULL) break;
×
867

868
    cols = 0;
×
869
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
870
    char             name[TSDB_ROLE_LEN + VARSTR_HEADER_SIZE] = {0};
×
871
    STR_WITH_MAXSIZE_TO_VARSTR(name, pObj->name, pShow->pMeta->pSchemas[cols].bytes);
×
872
    COL_DATA_SET_VAL_GOTO((const char *)name, false, pObj, pShow->pIter, _exit);
×
873

874
    if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
×
875
      int8_t enable = pObj->enable ? 1 : 0;
×
876
      COL_DATA_SET_VAL_GOTO((const char *)&enable, false, pObj, pShow->pIter, _exit);
×
877
    }
878

879
    if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
×
880
      COL_DATA_SET_VAL_GOTO((const char *)&pObj->createdTime, false, pObj, pShow->pIter, _exit);
×
881
    }
882
    if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
×
883
      COL_DATA_SET_VAL_GOTO((const char *)&pObj->updateTime, false, pObj, pShow->pIter, _exit);
×
884
    }
885
    if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
×
886
      char *roleType = pObj->sys ? "SYSTEM" : "USER";
×
887
      STR_WITH_MAXSIZE_TO_VARSTR(tBuf, roleType, pShow->pMeta->pSchemas[cols].bytes);
×
888
      COL_DATA_SET_VAL_GOTO((const char *)tBuf, false, pObj, pShow->pIter, _exit);
×
889
    }
890

891
    if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
×
892
      void  *pIter = NULL;
×
893
      size_t klen = 0, tlen = 0;
×
894
      char  *pBuf = POINTER_SHIFT(tBuf, VARSTR_HEADER_SIZE);
×
895
      while ((pIter = taosHashIterate(pObj->subRoles, pIter))) {
×
896
        char *roleName = taosHashGetKey(pIter, &klen);
×
897
        tlen += snprintf(pBuf + tlen, bufSize - tlen, "%s,", roleName);
×
898
      }
899
      if (tlen > 0) {
×
900
        pBuf[--tlen] = 0;  // remove last ','
×
901
      } else {
902
        pBuf[0] = 0;
×
903
      }
904
      varDataSetLen(tBuf, tlen);
×
905
      COL_DATA_SET_VAL_GOTO((const char *)tBuf, false, pObj, pShow->pIter, _exit);
×
906
    }
907
    numOfRows++;
×
908
    sdbRelease(pSdb, pObj);
×
909
  }
910
  pShow->numOfRows += numOfRows;
×
911
_exit:
×
912
  if (code < 0) {
×
913
    mError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
914
    TAOS_RETURN(code);
×
915
  }
916
  return numOfRows;
×
917
}
918

919
static void mndCancelGetNextRole(SMnode *pMnode, void *pIter) {
×
920
  SSdb *pSdb = pMnode->pSdb;
×
921
  sdbCancelFetchByType(pSdb, pIter, SDB_ROLE);
×
922
}
×
923

924
static int32_t mndRetrievePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
×
925
  int32_t   code = 0, lino = 0;
×
926
  SMnode   *pMnode = pReq->info.node;
×
927
  SSdb     *pSdb = pMnode->pSdb;
×
928
  int32_t   numOfRows = 0;
×
929
  int32_t   cols = 0;
×
930
  SRoleObj *pObj = NULL;
×
931
  char     *pBuf = NULL, *qBuf = NULL;
×
932
  char     *sql = NULL;
×
933
  char      roleName[TSDB_ROLE_LEN + VARSTR_HEADER_SIZE] = {0};
×
934
  int32_t   bufSize = TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE;
×
935

936
  bool fetchNextInstance = pShow->restore ? false : true;
×
937
  pShow->restore = false;
×
938

939
  while (numOfRows < rows) {
×
940
    if (fetchNextInstance) {
×
941
      pShow->pIter = sdbFetch(pSdb, SDB_ROLE, pShow->pIter, (void **)&pObj);
×
942
      if (pShow->pIter == NULL) break;
×
943
    } else {
944
      fetchNextInstance = true;
×
945
      void *pKey = taosHashGetKey(pShow->pIter, NULL);
×
946
      if (!(pObj = sdbAcquire(pSdb, SDB_ROLE, pKey))) {
×
947
        continue;
×
948
      }
949
    }
950

951
    // count total privileges for current role
952
    int32_t nSysPrivileges = privPopCnt(&pObj->sysPrivs);
×
953
    int32_t nObjPrivileges = 0;
×
954
    void   *pIter = NULL;
×
955
    while ((pIter = taosHashIterate(pObj->objPrivs, pIter))) {
×
956
      SPrivObjPolicies *pPolices = (SPrivObjPolicies *)pIter;
×
957
      nObjPrivileges += privPopCnt(&pPolices->policy);
×
958
    }
959
    int32_t nTblPrivileges = privTblPrivCnt(pObj->selectTbs);
×
960
    nTblPrivileges += privTblPrivCnt(pObj->insertTbs);
×
961
    nTblPrivileges += privTblPrivCnt(pObj->updateTbs);
×
962
    nTblPrivileges += privTblPrivCnt(pObj->deleteTbs);
×
963

964
    int32_t totalPrivileges = nSysPrivileges + nObjPrivileges + nTblPrivileges;
×
965

966
    if (numOfRows + totalPrivileges >= rows) {
×
967
      if (totalPrivileges >= SHOW_PRIVILEGES_STEP_SIZE) {
×
968
        mError("role:%s, has too many privileges:%d to show", pObj->name, totalPrivileges);
×
969
        sdbRelease(pSdb, pObj);
×
970
        TAOS_CHECK_EXIT(TSDB_CODE_MND_TOO_MANY_PRIVS);
×
971
      }
972
      pShow->restore = true;
×
973
      sdbRelease(pSdb, pObj);
×
974
      break;
×
975
    }
976

977
    if (!pBuf && !(pBuf = taosMemoryMalloc(bufSize))) {
×
978
      sdbRelease(pSdb, pObj);
×
979
      TAOS_CHECK_EXIT(terrno);
×
980
    }
981

982
    cols = 0;
×
983
    STR_WITH_MAXSIZE_TO_VARSTR(roleName, pObj->name, pShow->pMeta->pSchemas[cols].bytes);
×
984

985
    // system privileges
986
    SPrivIter privIter = {0};
×
987
    privIterInit(&privIter, &pObj->sysPrivs);
×
988
    SPrivInfo *pPrivInfo = NULL;
×
989
    while (privIterNext(&privIter, &pPrivInfo)) {
×
990
      cols = 0;
×
991
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
992
      COL_DATA_SET_VAL_GOTO((const char *)roleName, false, pObj, pShow->pIter, _exit);
×
993

994
      if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
×
995
        STR_WITH_MAXSIZE_TO_VARSTR(pBuf, pPrivInfo->name, pShow->pMeta->pSchemas[cols].bytes);
×
996
        COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
×
997
      }
998
      if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
×
999
        STR_WITH_MAXSIZE_TO_VARSTR(pBuf, privObjGetName(PRIV_OBJ_CLUSTER), pShow->pMeta->pSchemas[cols].bytes);
×
1000
        COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
×
1001
      }
1002
      // skip db, table, condition, notes, columns, update_time
1003
      COL_DATA_SET_EMPTY_VARCHAR(pBuf, 6);
×
1004

1005
      numOfRows++;
×
1006
    }
1007

1008
    // object privileges
1009
    pIter = NULL;
×
1010
    while ((pIter = taosHashIterate(pObj->objPrivs, pIter))) {
×
1011
      SPrivObjPolicies *pPolices = (SPrivObjPolicies *)pIter;
×
1012

1013
      char   *key = taosHashGetKey(pPolices, NULL);
×
1014
      int32_t objType = PRIV_OBJ_UNKNOWN;
×
1015
      char    dbName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
1016
      char    tblName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
1017

1018
      if ((code = privObjKeyParse(key, &objType, dbName, sizeof(dbName), tblName, sizeof(tblName), false))) {
×
1019
        sdbRelease(pSdb, pObj);
×
1020
        TAOS_CHECK_EXIT(code);
×
1021
      }
1022

1023
      SPrivIter privIter = {0};
×
1024
      privIterInit(&privIter, &pPolices->policy);
×
1025
      SPrivInfo *pPrivInfo = NULL;
×
1026
      while (privIterNext(&privIter, &pPrivInfo)) {
×
1027
        cols = 0;
×
1028
        SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
×
1029
        COL_DATA_SET_VAL_GOTO((const char *)roleName, false, pObj, pShow->pIter, _exit);
×
1030

1031
        if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
×
1032
          STR_WITH_MAXSIZE_TO_VARSTR(pBuf, pPrivInfo->name, pShow->pMeta->pSchemas[cols].bytes);
×
1033
          COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
×
1034
        }
1035

1036
        if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
×
1037
          STR_WITH_MAXSIZE_TO_VARSTR(pBuf, privObjGetName(objType), pShow->pMeta->pSchemas[cols].bytes);
×
1038
          COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
×
1039
        }
1040

1041
        if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
×
1042
          STR_WITH_MAXSIZE_TO_VARSTR(pBuf, dbName, pShow->pMeta->pSchemas[cols].bytes);
×
1043
          COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
×
1044
        }
1045

1046
        if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
×
1047
          STR_WITH_MAXSIZE_TO_VARSTR(pBuf, tblName, pShow->pMeta->pSchemas[cols].bytes);
×
1048
          COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
×
1049
        }
1050

1051
        // skip condition, notes, columns, update_time
1052
        COL_DATA_SET_EMPTY_VARCHAR(pBuf, 4);
×
1053

1054
        numOfRows++;
×
1055
      }
1056
    }
1057
    // table level privileges
1058
    TAOS_CHECK_EXIT(mndShowTablePrivileges(pReq, pShow, pBlock, rows - numOfRows, pObj, pObj->name, pObj->selectTbs,
×
1059
                                           PRIV_TBL_SELECT, pBuf, bufSize, &numOfRows));
1060
    TAOS_CHECK_EXIT(mndShowTablePrivileges(pReq, pShow, pBlock, rows - numOfRows, pObj, pObj->name, pObj->insertTbs,
×
1061
                                           PRIV_TBL_INSERT, pBuf, bufSize, &numOfRows));
1062
    TAOS_CHECK_EXIT(mndShowTablePrivileges(pReq, pShow, pBlock, rows - numOfRows, pObj, pObj->name, pObj->updateTbs,
×
1063
                                           PRIV_TBL_UPDATE, pBuf, bufSize, &numOfRows));
1064
    TAOS_CHECK_EXIT(mndShowTablePrivileges(pReq, pShow, pBlock, rows - numOfRows, pObj, pObj->name, pObj->deleteTbs,
×
1065
                                           PRIV_TBL_DELETE, pBuf, bufSize, &numOfRows));
1066
    sdbRelease(pSdb, pObj);
×
1067
  }
1068

1069
  pShow->numOfRows += numOfRows;
×
1070
_exit:
×
1071
  taosMemoryFreeClear(pBuf);
×
1072
  taosMemoryFreeClear(sql);
×
1073
  if (code < 0) {
×
1074
    mError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1075
    TAOS_RETURN(code);
×
1076
  }
1077
  return numOfRows;
×
1078
}
1079

1080
static void mndCancelGetNextPrivileges(SMnode *pMnode, void *pIter) {
×
1081
  SSdb *pSdb = pMnode->pSdb;
×
1082
  sdbCancelFetchByType(pSdb, pIter, SDB_ROLE);
×
1083
}
×
1084

1085
static int32_t mndRetrieveColPrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
×
1086
  int32_t   code = 0, lino = 0;
×
1087
  SMnode   *pMnode = pReq->info.node;
×
1088
  SSdb     *pSdb = pMnode->pSdb;
×
1089
  int32_t   numOfRows = 0;
×
1090
#if 0
1091
  int32_t   cols = 0;
1092
  SRoleObj *pObj = NULL;
1093
  char     *pBuf = NULL, *qBuf = NULL;
1094
  char     *sql = NULL;
1095
  char      roleName[TSDB_ROLE_LEN + VARSTR_HEADER_SIZE] = {0};
1096
  int32_t   bufSize = TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE;
1097

1098
  bool fetchNextInstance = pShow->restore ? false : true;
1099
  pShow->restore = false;
1100

1101
  while (numOfRows < rows) {
1102
    if (fetchNextInstance) {
1103
      pShow->pIter = sdbFetch(pSdb, SDB_ROLE, pShow->pIter, (void **)&pObj);
1104
      if (pShow->pIter == NULL) break;
1105
    } else {
1106
      fetchNextInstance = true;
1107
      void *pKey = taosHashGetKey(pShow->pIter, NULL);
1108
      if (!(pObj = sdbAcquire(pSdb, SDB_ROLE, pKey))) {
1109
        continue;
1110
      }
1111
    }
1112

1113
    int32_t nSysPrivileges = 0, nObjPrivileges = 0;
1114
    if (nSysPrivileges + nObjPrivileges >= rows) {
1115
      pShow->restore = true;
1116
      sdbRelease(pSdb, pObj);
1117
      break;
1118
    }
1119

1120
    if (!pBuf && !(pBuf = taosMemoryMalloc(bufSize))) {
1121
      sdbRelease(pSdb, pObj);
1122
      TAOS_CHECK_EXIT(terrno);
1123
    }
1124

1125
    cols = 0;
1126
    STR_WITH_MAXSIZE_TO_VARSTR(roleName, pObj->name, pShow->pMeta->pSchemas[cols].bytes);
1127

1128
    SPrivIter privIter = {0};
1129
    privIterInit(&privIter, &pObj->sysPrivs);
1130
    SPrivInfo *pPrivInfo = NULL;
1131
    while (privIterNext(&privIter, &pPrivInfo)) {
1132
      cols = 0;
1133
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1134
      COL_DATA_SET_VAL_GOTO((const char *)roleName, false, pObj, pShow->pIter, _exit);
1135

1136
      if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
1137
        STR_WITH_MAXSIZE_TO_VARSTR(pBuf, pPrivInfo->name, pShow->pMeta->pSchemas[cols].bytes);
1138
        COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
1139
      }
1140

1141
      // for (int32_t i = 0; i < 6; i++) {
1142
      //   if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
1143
      //     STR_WITH_MAXSIZE_TO_VARSTR(pBuf, "", 2);
1144
      //     COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
1145
      //   }
1146
      // }
1147
      // skip db, table, condition, notes, columns, update_time
1148
      COL_DATA_SET_EMPTY_VARCHAR(pBuf, 7);
1149
      numOfRows++;
1150
    }
1151

1152
    sdbRelease(pSdb, pObj);
1153
  }
1154

1155
  pShow->numOfRows += numOfRows;
1156
_exit:
1157
  taosMemoryFreeClear(pBuf);
1158
  taosMemoryFreeClear(sql);
1159
  if (code < 0) {
1160
    mError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
1161
    TAOS_RETURN(code);
1162
  }
1163
#endif
1164
  return numOfRows;
×
1165
}
1166

1167
static void mndCancelGetNextColPrivileges(SMnode *pMnode, void *pIter) {
×
1168
  SSdb *pSdb = pMnode->pSdb;
×
1169
  sdbCancelFetchByType(pSdb, pIter, SDB_ROLE);
×
1170
}
×
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