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

taosdata / TDengine / #4950

06 Feb 2026 07:29AM UTC coverage: 66.849% (-0.1%) from 66.973%
#4950

push

travis-ci

web-flow
merge: from main to 3.0 #34521

759 of 1081 new or added lines in 28 files covered. (70.21%)

1144 existing lines in 130 files now uncovered.

205692 of 307696 relevant lines covered (66.85%)

127112954.87 hits per line

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

70.9
/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) {
411,850✔
63
  // role management init
64
  roleMgmt.lastUpd = taosGetTimestampMs();
411,850✔
65
  TAOS_CHECK_RETURN(taosThreadRwlockInit(&roleMgmt.rw, NULL));
411,850✔
66

67
  SSdbTable table = {
411,850✔
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);
411,850✔
80
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_ROLE, mndProcessDropRoleReq);
411,850✔
81
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_ROLE, mndProcessAlterRoleReq);
411,850✔
82
  mndSetMsgHandle(pMnode, TDMT_MND_UPGRADE_ROLE, mndProcessUpgradeRoleReq);
411,850✔
83
  mndSetMsgHandle(pMnode, TDMT_MND_UPGRADE_ROLE_RSP, mndProcessUpgradeRoleRsp);
411,850✔
84

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

94
void mndCleanupRole(SMnode *pMnode) { (void)taosThreadRwlockDestroy(&roleMgmt.rw); }
411,793✔
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) {
1,085✔
105
  (void)taosThreadRwlockWrlock(&roleMgmt.rw);
1,085✔
106
  roleMgmt.lastUpd = updateTime;
1,085✔
107
  (void)taosThreadRwlockUnlock(&roleMgmt.rw);
1,085✔
108
}
1,085✔
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,760,568✔
121
  int32_t       code = 0, lino = 0;
1,760,568✔
122
  SPrivInfoIter iter = {0};
1,760,568✔
123
  privInfoIterInit(&iter);
1,760,568✔
124

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

128
  SPrivInfo *pPrivInfo = NULL;
1,760,568✔
129
  while (privInfoIterNext(&iter, &pPrivInfo)) {
258,803,496✔
130
    if ((pPrivInfo->sysType & roleType) == 0) continue;
257,042,928✔
131
    if (pPrivInfo->category == PRIV_CATEGORY_SYSTEM) {  // system privileges
92,723,248✔
132
      privAddType(&pObj->sysPrivs, pPrivInfo->privType);
48,415,620✔
133
    } else if (pPrivInfo->category == PRIV_CATEGORY_OBJECT) {  // object privileges
44,307,628✔
134
      snprintf(dbFName, TSDB_DB_FNAME_LEN, "1.%s", pPrivInfo->dbName[0] == 0 ? "*" : pPrivInfo->dbName);
44,307,628✔
135
      int32_t keyLen = privObjKeyF(pPrivInfo, dbFName, "*", objKey, sizeof(objKey));
44,307,628✔
136
      if (!pObj->objPrivs && !(pObj->objPrivs = taosHashInit(1, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true,
44,307,628✔
137
                                                             HASH_ENTRY_LOCK))) {
138
        TAOS_CHECK_EXIT(terrno);
×
139
      }
140
      SPrivObjPolicies *objPolicy = taosHashGet(pObj->objPrivs, objKey, keyLen + 1);
44,307,628✔
141
      if (objPolicy) {
44,307,628✔
142
        privAddType(&objPolicy->policy, pPrivInfo->privType);
30,809,940✔
143
      } else {
144
        SPrivObjPolicies policies = {0};
13,497,688✔
145
        privAddType(&policies.policy, pPrivInfo->privType);
13,497,688✔
146
        TAOS_CHECK_EXIT(taosHashPut(pObj->objPrivs, objKey, keyLen + 1, &policies, sizeof(policies)));
13,497,688✔
147
      }
148
    }
149
  }
150
_exit:
1,760,568✔
151
  if (code != 0) {
1,760,568✔
152
    mError("role, %s failed at line %d for %s since %s", __func__, lino, pObj->name, tstrerror(code));
×
153
  }
154
  TAOS_RETURN(code);
1,760,568✔
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,989,790✔
161
  int32_t   code = 0, lino = 0;
3,989,790✔
162
  SRoleObj *pRole = NULL, *pNew = NULL;
3,989,790✔
163
  if (mndAcquireRole(pMnode, role, &pRole) == 0) {
3,989,790✔
164
    if (pRole->version < MND_ROLE_SYSROLE_VER) {
2,229,222✔
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,229,222✔
180
      return 0;
2,229,222✔
181
    }
182
  } else {
183
    pNew = taosMemoryCalloc(1, sizeof(SRoleObj));
1,760,568✔
184
    if (pNew == NULL) {
1,760,568✔
185
      TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
186
    }
187
    tstrncpy(pNew->name, role, TSDB_ROLE_LEN);
1,760,568✔
188
    pNew->uid = mndGenerateUid(pNew->name, strlen(pNew->name));
1,760,568✔
189
    pNew->createdTime = taosGetTimestampMs();
1,760,568✔
190
    pNew->updateTime = pNew->createdTime;
1,760,568✔
191
    pNew->enable = 1;
1,760,568✔
192
    pNew->sys = 1;
1,760,568✔
193
    pNew->version = MND_ROLE_SYSROLE_VER;
1,760,568✔
194
  }
195

196
  TAOS_CHECK_EXIT(mndFillSystemRolePrivileges(pMnode, pNew, roleType));
1,760,568✔
197

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

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

204
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL, "create-role");
1,760,568✔
205
  if (pTrans == NULL) {
1,760,568✔
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,760,568✔
211

212
  if (mndTransAppendCommitlog(pTrans, pRaw) != 0) {
1,760,568✔
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,760,568✔
217

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

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

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

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

250
  TAOS_CHECK_EXIT(tStartEncode(&encoder));
13,011,282✔
251
  TAOS_CHECK_EXIT(tEncodeCStr(&encoder, pObj->name));
26,022,564✔
252
  TAOS_CHECK_EXIT(tEncodeI64v(&encoder, pObj->createdTime));
26,022,564✔
253
  TAOS_CHECK_EXIT(tEncodeI64v(&encoder, pObj->updateTime));
26,022,564✔
254
  TAOS_CHECK_EXIT(tEncodeI64v(&encoder, pObj->uid));
26,022,564✔
255
  TAOS_CHECK_EXIT(tEncodeI64v(&encoder, pObj->version));
26,022,564✔
256
  TAOS_CHECK_EXIT(tEncodeU8(&encoder, pObj->flag));
26,022,564✔
257

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

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

265
  int32_t nParentRoles = taosHashGetSize(pObj->parentRoles);
13,011,282✔
266
  TAOS_CHECK_EXIT(tEncodeI32v(&encoder, nParentRoles));
13,011,282✔
267
  if (nParentRoles > 0) {
13,011,282✔
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,011,282✔
275
  TAOS_CHECK_EXIT(tEncodeI32v(&encoder, nSubRoles));
13,011,282✔
276
  if (nSubRoles > 0) {
13,011,282✔
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,011,282✔
285
  tlen = encoder.pos;
13,011,282✔
286
_exit:
13,011,282✔
287
  tEncoderClear(&encoder);
13,011,282✔
288
  if (code < 0) {
13,011,282✔
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,011,282✔
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,472,250✔
310
  int32_t  code = 0, lino = 0;
2,472,250✔
311
  SDecoder decoder = {0};
2,472,250✔
312
  tDecoderInit(&decoder, buf, bufLen);
2,472,250✔
313

314
  TAOS_CHECK_EXIT(tStartDecode(&decoder));
2,472,250✔
315

316
  TAOS_CHECK_EXIT(tDecodeCStrTo(&decoder, pObj->name));
2,472,250✔
317
  TAOS_CHECK_EXIT(tDecodeI64v(&decoder, &pObj->createdTime));
4,944,500✔
318
  TAOS_CHECK_EXIT(tDecodeI64v(&decoder, &pObj->updateTime));
4,944,500✔
319
  TAOS_CHECK_EXIT(tDecodeI64v(&decoder, &pObj->uid));
4,944,500✔
320
  TAOS_CHECK_EXIT(tDecodeI64v(&decoder, &pObj->version));
4,944,500✔
321
  TAOS_CHECK_EXIT(tDecodeU8(&decoder, &pObj->flag));
4,944,500✔
322

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

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

330
  char    roleName[TSDB_ROLE_LEN] = {0};
2,472,250✔
331
  int32_t nParentRoles = 0;
2,472,250✔
332
  TAOS_CHECK_EXIT(tDecodeI32v(&decoder, &nParentRoles));
2,472,250✔
333
  if (nParentRoles > 0) {
2,472,250✔
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,472,250✔
344
  TAOS_CHECK_EXIT(tDecodeI32v(&decoder, &nSubRoles));
2,472,250✔
345
  if (nSubRoles > 0) {
2,472,250✔
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,472,250✔
357
  tEndDecode(&decoder);
2,472,250✔
358
  tDecoderClear(&decoder);
2,472,250✔
359
  if (code < 0) {
2,472,250✔
360
    mError("role, %s failed at line %d since %s, row:%p", __func__, lino, tstrerror(code), pObj);
×
361
  }
362
  TAOS_RETURN(code);
2,472,250✔
363
}
364

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

384
  int32_t dataPos = 0;
6,505,641✔
385
  SDB_SET_INT32(pRaw, dataPos, tlen, _exit);
6,505,641✔
386
  SDB_SET_BINARY(pRaw, dataPos, buf, tlen, _exit);
6,505,641✔
387
  SDB_SET_DATALEN(pRaw, dataPos, _exit);
6,505,641✔
388
_exit:
6,505,641✔
389
  taosMemoryFreeClear(buf);
6,505,641✔
390
  if (code != TSDB_CODE_SUCCESS) {
6,505,641✔
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,505,641✔
397
  return pRaw;
6,505,641✔
398
}
399

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

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

409
  if (sver != MND_ROLE_VER_NUMBER) {
2,472,250✔
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,472,250✔
415
    TAOS_CHECK_EXIT(terrno);
×
416
  }
417

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

422
  int32_t tlen;
2,471,284✔
423
  int32_t dataPos = 0;
2,472,250✔
424
  SDB_GET_INT32(pRaw, dataPos, &tlen, _exit);
2,472,250✔
425
  if (!(buf = taosMemoryMalloc(tlen + 1))) {
2,472,250✔
426
    TAOS_CHECK_EXIT(terrno);
×
427
  }
428
  SDB_GET_BINARY(pRaw, dataPos, buf, tlen, _exit);
2,472,250✔
429
  TAOS_CHECK_EXIT(tDeserializeSRoleObj(buf, tlen, pObj));
2,472,250✔
430
  if (pObj->version < MND_ROLE_SYSROLE_VER) {
2,472,250✔
431
  }
432
  taosInitRWLatch(&pObj->lock);
2,472,250✔
433
_exit:
2,472,250✔
434
  taosMemoryFreeClear(buf);
2,472,250✔
435
  if (code != TSDB_CODE_SUCCESS) {
2,472,250✔
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,472,250✔
443
  return pRow;
2,472,250✔
444
}
445

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

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

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

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

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

498
  *ppRole = sdbAcquire(pSdb, SDB_ROLE, roleName);
86,630,604✔
499
  if (*ppRole == NULL) {
86,630,604✔
500
    if (terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
3,048,696✔
501
      code = TSDB_CODE_MND_ROLE_NOT_EXIST;
3,048,696✔
502
    } else {
503
      code = TSDB_CODE_MND_ROLE_NOT_AVAILABLE;
×
504
    }
505
  }
506
  TAOS_RETURN(code);
86,630,604✔
507
}
508

509
void mndReleaseRole(SMnode *pMnode, SRoleObj *pRole) {
84,870,112✔
510
  SSdb *pSdb = pMnode->pSdb;
84,870,112✔
511
  sdbRelease(pSdb, pRole);
84,870,112✔
512
}
84,871,828✔
513

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

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

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

533
  SSdbRaw *pCommitRaw = mndRoleActionEncode(&obj);
155✔
534
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
155✔
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));
155✔
539
  TAOS_CHECK_EXIT(mndTransPrepare(pMnode, pTrans));
155✔
540
_exit:
155✔
541
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
155✔
542
    mError("role:%s, failed at line %d to create role, since %s", obj.name, lino, tstrerror(code));
×
543
  }
544
  mndRoleFreeObj(&obj);
155✔
545
  mndTransDrop(pTrans);
155✔
546
  TAOS_RETURN(code);
155✔
547
}
548

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

558
  if (tDeserializeSCreateRoleReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
155✔
559
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_MSG);
×
560
  }
561
  if ((code = mndAcquireRole(pMnode, createReq.name, &pRole)) == 0) {
155✔
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) {
155✔
570
      // continue
571
    } else {
572
      goto _exit;
×
573
    }
574
  }
575
  code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser);
155✔
576
  if (pOperUser == NULL) {
155✔
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);
155✔
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));
155✔
584

585
  if (createReq.name[0] == 0) {
155✔
586
    TAOS_CHECK_EXIT(TSDB_CODE_MND_ROLE_INVALID_FORMAT);
×
587
  }
588
  code = mndAcquireUser(pMnode, createReq.name, &pUser);
155✔
589
  if (pUser != NULL) {
155✔
590
    TAOS_CHECK_EXIT(TSDB_CODE_MND_USER_ALREADY_EXIST);
×
591
  }
592
  if (sdbGetSize(pMnode->pSdb, SDB_ROLE) >= TSDB_MAX_ROLES) {
155✔
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));
155✔
597
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
155✔
598

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

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

NEW
660
  if (dropReq.name[0] == 0) {
×
NEW
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));
NEW
671
  TAOS_CHECK_EXIT(mndCheckSysObjPrivilege(pMnode, pOperUser, RPC_MSG_TOKEN(pReq), PRIV_ROLE_DROP, 0, 0, NULL, NULL));
×
672

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

681
  TAOS_CHECK_EXIT(mndDropRole(pMnode, pReq, pObj));
×
UNCOV
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) {
1,085✔
701
  int32_t code = 0, lino = 0;
1,085✔
702
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_ROLE, pReq, "alter-role");
1,085✔
703
  if (pTrans == NULL) {
1,085✔
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);
1,085✔
708

709
  SSdbRaw *pCommitRaw = mndRoleActionEncode(pObj);
1,085✔
710
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
1,085✔
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));
1,085✔
715
  TAOS_CHECK_EXIT(mndTransPrepare(pMnode, pTrans));
1,085✔
716
_exit:
1,085✔
717
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1,085✔
718
    mError("role:%s, failed to alter at line:%d since %s", pObj->name, lino, tstrerror(code));
×
719
  }
720
  mndTransDrop(pTrans);
1,085✔
721
  TAOS_RETURN(0);
1,085✔
722
}
723

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

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

751
static bool mndIsRoleChanged(SRoleObj *pOld, SAlterRoleReq *pAlterReq) {
1,240✔
752
  switch (pAlterReq->alterType) {
1,240✔
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:
1,240✔
760
      break;
1,240✔
761
  }
762
  return true;
1,240✔
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,219,909✔
771
  int32_t       code = 0, lino = 0;
1,219,909✔
772
  SMnode       *pMnode = pReq->info.node;
1,219,909✔
773
  SRoleObj     *pObj = NULL;
1,219,909✔
774
  SRoleObj      newObj = {0};
1,219,909✔
775
  SUserObj     *pOperUser = NULL;
1,219,909✔
776
  SAlterRoleReq alterReq = {0};
1,219,909✔
777
  bool          alterUser = false;
1,219,909✔
778
  int64_t       tss = taosGetTimestampMs();
1,219,909✔
779

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

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

785
  TAOS_CHECK_EXIT(mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser));
1,219,909✔
786

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

791
  if (mndAcquireRole(pMnode, alterReq.principal, &pObj) == 0) {
1,219,909✔
792
    if (alterReq.alterType == TSDB_ALTER_ROLE_ROLE) {
1,240✔
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,218,669✔
797
      TAOS_CHECK_EXIT(terrno);
×
798
    }
799
    alterUser = true;
1,218,669✔
800
  }
801

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

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

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

847
static int32_t mndProcessUpgradeRoleReq(SRpcMsg *pReq) {
371,537✔
848
  return mndCreateDefaultRoles(pReq->info.node);
371,537✔
849
}
850

851
static int32_t mndProcessUpgradeRoleRsp(SRpcMsg *pReq) { return 0; }
371,537✔
852

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

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

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

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

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

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

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

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

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

937
  bool fetchNextInstance = pShow->restore ? false : true;
775✔
938
  pShow->restore = false;
775✔
939

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

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

965
    int32_t totalPrivileges = nSysPrivileges + nObjPrivileges + nTblPrivileges;
5,425✔
966

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

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

983
    cols = 0;
5,425✔
984
    STR_WITH_MAXSIZE_TO_VARSTR(roleName, pObj->name, pShow->pMeta->pSchemas[cols].bytes);
5,425✔
985

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

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

1006
      numOfRows++;
127,875✔
1007
    }
1008

1009
    // object privileges
1010
    pIter = NULL;
5,425✔
1011
    while ((pIter = taosHashIterate(pObj->objPrivs, pIter))) {
41,540✔
1012
      SPrivObjPolicies *pPolices = (SPrivObjPolicies *)pIter;
36,115✔
1013

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

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

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

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

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

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

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

1052
        // skip condition, notes, columns, update_time
1053
        COL_DATA_SET_EMPTY_VARCHAR(pBuf, 4);
588,225✔
1054

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

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

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

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

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

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

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

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

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

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

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

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

1153
    sdbRelease(pSdb, pObj);
1154
  }
1155

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

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