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

taosdata / TDengine / #4943

30 Jan 2026 06:19AM UTC coverage: 66.718% (-0.07%) from 66.788%
#4943

push

travis-ci

web-flow
merge: from main to 3.0 #34453

1122 of 2018 new or added lines in 72 files covered. (55.6%)

823 existing lines in 156 files now uncovered.

204811 of 306978 relevant lines covered (66.72%)

123993567.34 hits per line

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

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

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

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

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

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

128
  SPrivInfo *pPrivInfo = NULL;
1,715,922✔
129
  while (privInfoIterNext(&iter, &pPrivInfo)) {
250,524,612✔
130
    if ((pPrivInfo->sysType & roleType) == 0) continue;
248,808,690✔
131
    if (pPrivInfo->category == PRIV_CATEGORY_SYSTEM) {  // system privileges
89,513,931✔
132
      privAddType(&pObj->sysPrivs, pPrivInfo->privType);
46,329,894✔
133
    } else if (pPrivInfo->category == PRIV_CATEGORY_OBJECT) {  // object privileges
43,184,037✔
134
      snprintf(dbFName, TSDB_DB_FNAME_LEN, "1.%s", pPrivInfo->dbName[0] == 0 ? "*" : pPrivInfo->dbName);
43,184,037✔
135
      int32_t keyLen = privObjKeyF(pPrivInfo, dbFName, "*", objKey, sizeof(objKey));
43,184,037✔
136
      if (!pObj->objPrivs && !(pObj->objPrivs = taosHashInit(1, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true,
43,184,037✔
137
                                                             HASH_ENTRY_LOCK))) {
NEW
138
        TAOS_CHECK_EXIT(terrno);
×
139
      }
140
      SPrivObjPolicies *objPolicy = taosHashGet(pObj->objPrivs, objKey, keyLen + 1);
43,184,037✔
141
      if (objPolicy) {
43,184,037✔
142
        privAddType(&objPolicy->policy, pPrivInfo->privType);
30,028,635✔
143
      } else {
144
        SPrivObjPolicies policies = {0};
13,155,402✔
145
        privAddType(&policies.policy, pPrivInfo->privType);
13,155,402✔
146
        TAOS_CHECK_EXIT(taosHashPut(pObj->objPrivs, objKey, keyLen + 1, &policies, sizeof(policies)));
13,155,402✔
147
      }
148
    }
149
  }
150
_exit:
1,715,922✔
151
  if (code != 0) {
1,715,922✔
152
    mError("role, %s failed at line %d for %s since %s", __func__, lino, pObj->name, tstrerror(code));
×
153
  }
154
  TAOS_RETURN(code);
1,715,922✔
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,883,908✔
161
  int32_t   code = 0, lino = 0;
3,883,908✔
162
  SRoleObj *pRole = NULL, *pNew = NULL;
3,883,908✔
163
  if (mndAcquireRole(pMnode, role, &pRole) == 0) {
3,883,908✔
164
    if (pRole->version < MND_ROLE_SYSROLE_VER) {
2,167,986✔
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,167,986✔
180
      return 0;
2,167,986✔
181
    }
182
  } else {
183
    pNew = taosMemoryCalloc(1, sizeof(SRoleObj));
1,715,922✔
184
    if (pNew == NULL) {
1,715,922✔
185
      TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
186
    }
187
    tstrncpy(pNew->name, role, TSDB_ROLE_LEN);
1,715,922✔
188
    pNew->uid = mndGenerateUid(pNew->name, strlen(pNew->name));
1,715,922✔
189
    pNew->createdTime = taosGetTimestampMs();
1,715,922✔
190
    pNew->updateTime = pNew->createdTime;
1,715,922✔
191
    pNew->enable = 1;
1,715,922✔
192
    pNew->sys = 1;
1,715,922✔
193
    pNew->version = MND_ROLE_SYSROLE_VER;
1,715,922✔
194
  }
195

196
  TAOS_CHECK_EXIT(mndFillSystemRolePrivileges(pMnode, pNew, roleType));
1,715,922✔
197

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

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

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

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

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

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

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

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

250
  TAOS_CHECK_EXIT(tStartEncode(&encoder));
12,686,082✔
251
  TAOS_CHECK_EXIT(tEncodeCStr(&encoder, pObj->name));
25,372,164✔
252
  TAOS_CHECK_EXIT(tEncodeI64v(&encoder, pObj->createdTime));
25,372,164✔
253
  TAOS_CHECK_EXIT(tEncodeI64v(&encoder, pObj->updateTime));
25,372,164✔
254
  TAOS_CHECK_EXIT(tEncodeI64v(&encoder, pObj->uid));
25,372,164✔
255
  TAOS_CHECK_EXIT(tEncodeI64v(&encoder, pObj->version));
25,372,164✔
256
  TAOS_CHECK_EXIT(tEncodeU8(&encoder, pObj->flag));
25,372,164✔
257

258
  TAOS_CHECK_EXIT(tSerializePrivSysObjPolicies(&encoder, &pObj->sysPrivs, pObj->objPrivs));
12,686,082✔
259

260
  TAOS_CHECK_EXIT(tSerializePrivTblPolicies(&encoder, pObj->selectTbs));
12,686,082✔
261
  TAOS_CHECK_EXIT(tSerializePrivTblPolicies(&encoder, pObj->insertTbs));
12,686,082✔
262
  TAOS_CHECK_EXIT(tSerializePrivTblPolicies(&encoder, pObj->updateTbs));
12,686,082✔
263
  TAOS_CHECK_EXIT(tSerializePrivTblPolicies(&encoder, pObj->deleteTbs));
12,686,082✔
264

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

314
  TAOS_CHECK_EXIT(tStartDecode(&decoder));
2,411,996✔
315

316
  TAOS_CHECK_EXIT(tDecodeCStrTo(&decoder, pObj->name));
2,411,996✔
317
  TAOS_CHECK_EXIT(tDecodeI64v(&decoder, &pObj->createdTime));
4,823,992✔
318
  TAOS_CHECK_EXIT(tDecodeI64v(&decoder, &pObj->updateTime));
4,823,992✔
319
  TAOS_CHECK_EXIT(tDecodeI64v(&decoder, &pObj->uid));
4,823,992✔
320
  TAOS_CHECK_EXIT(tDecodeI64v(&decoder, &pObj->version));
4,823,992✔
321
  TAOS_CHECK_EXIT(tDecodeU8(&decoder, &pObj->flag));
4,823,992✔
322

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

498
  *ppRole = sdbAcquire(pSdb, SDB_ROLE, roleName);
99,777,755✔
499
  if (*ppRole == NULL) {
99,777,034✔
500
    if (terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
2,787,425✔
501
      code = TSDB_CODE_MND_ROLE_NOT_EXIST;
2,787,425✔
502
    } else {
503
      code = TSDB_CODE_MND_ROLE_NOT_AVAILABLE;
×
504
    }
505
  }
506
  TAOS_RETURN(code);
99,777,034✔
507
}
508

509
void mndReleaseRole(SMnode *pMnode, SRoleObj *pRole) {
98,062,882✔
510
  SSdb *pSdb = pMnode->pSdb;
98,062,882✔
511
  sdbRelease(pSdb, pRole);
98,062,882✔
512
}
98,063,540✔
513

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

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

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

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

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

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

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

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

603
    int64_t tse = taosGetTimestampMs();
145✔
604
    double  duration = (double)(tse - tss);
145✔
605
    duration = duration / 1000;
145✔
606
    auditRecord(pReq, pMnode->clusterId, "createRole", "", createReq.name, detail, strlen(detail), duration, 0);
145✔
607
  }
608
_exit:
145✔
609
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
145✔
610
    mError("role:%s, failed to create at line %d since %s", createReq.name, lino, tstrerror(code));
×
611
  }
612
  mndReleaseUser(pMnode, pUser);
145✔
613
  mndReleaseUser(pMnode, pOperUser);
145✔
614
  mndReleaseRole(pMnode, pRole);
145✔
615
  tFreeSCreateRoleReq(&createReq);
145✔
616
  TAOS_RETURN(code);
145✔
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
  code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser);
×
660
  if (pOperUser == NULL) {
×
661
    TAOS_CHECK_EXIT(TSDB_CODE_MND_NO_USER_FROM_CONN);
×
662
  }
663
  mInfo("role:%s, start to drop", dropReq.name);
×
664
  // TAOS_CHECK_EXIT(mndCheckOperPrivilege(pMnode, RPC_MSG_USER(pReq), MND_OPER_DROP_ROLE));
NEW
665
  TAOS_CHECK_EXIT(mndCheckSysObjPrivilege(pMnode, pOperUser, RPC_MSG_TOKEN(pReq), PRIV_ROLE_CREATE, 0, 0, NULL, NULL));
×
666

667
  if (dropReq.name[0] == 0) {
×
668
    TAOS_CHECK_EXIT(TSDB_CODE_MND_ROLE_INVALID_FORMAT);
×
669
  }
670

671
  TAOS_CHECK_EXIT(mndAcquireRole(pMnode, dropReq.name, &pObj));
×
672

673
  TAOS_CHECK_EXIT(mndDropRole(pMnode, pReq, pObj));
×
674
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
×
675

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

692
static int32_t mndAlterRole(SMnode *pMnode, SRpcMsg *pReq, SRoleObj *pObj) {
1,015✔
693
  int32_t code = 0, lino = 0;
1,015✔
694
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_ROLE, pReq, "alter-role");
1,015✔
695
  if (pTrans == NULL) {
1,015✔
696
    mError("role:%s, failed to alter since %s", pObj->name, terrstr());
×
697
    TAOS_CHECK_EXIT(terrno);
×
698
  }
699
  mInfo("trans:%d, used to alter role:%s", pTrans->id, pObj->name);
1,015✔
700

701
  SSdbRaw *pCommitRaw = mndRoleActionEncode(pObj);
1,015✔
702
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
1,015✔
703
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
×
704
    TAOS_CHECK_EXIT(terrno);
×
705
  }
706
  TAOS_CHECK_EXIT(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY));
1,015✔
707
  TAOS_CHECK_EXIT(mndTransPrepare(pMnode, pTrans));
1,015✔
708
_exit:
1,015✔
709
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1,015✔
710
    mError("role:%s, failed to alter at line:%d since %s", pObj->name, lino, tstrerror(code));
×
711
  }
712
  mndTransDrop(pTrans);
1,015✔
713
  TAOS_RETURN(0);
1,015✔
714
}
715

716
int32_t mndRoleDupObj(SRoleObj *pOld, SRoleObj *pNew) {
1,160✔
717
  int32_t code = 0, lino = 0;
1,160✔
718
  snprintf(pNew->name, TSDB_ROLE_LEN, "%s", pOld->name);
1,160✔
719
  pNew->createdTime = pOld->createdTime;
1,160✔
720
  pNew->uid = pOld->uid;
1,160✔
721
  pNew->version = pOld->version + 1;
1,160✔
722
  pNew->flag = pOld->flag;
1,160✔
723
  pNew->updateTime = taosGetTimestampMs();
1,160✔
724
  pNew->sysPrivs = pOld->sysPrivs;
1,160✔
725

726
  taosRLockLatch(&pOld->lock);
1,160✔
727
  TAOS_CHECK_EXIT(mndDupPrivObjHash(pOld->objPrivs, &pNew->objPrivs));
1,160✔
728
  TAOS_CHECK_EXIT(mndDupPrivTblHash(pOld->selectTbs, &pNew->selectTbs, false));
1,160✔
729
  TAOS_CHECK_EXIT(mndDupPrivTblHash(pOld->insertTbs, &pNew->insertTbs, false));
1,160✔
730
  TAOS_CHECK_EXIT(mndDupPrivTblHash(pOld->updateTbs, &pNew->updateTbs, false));
1,160✔
731
  TAOS_CHECK_EXIT(mndDupPrivTblHash(pOld->deleteTbs, &pNew->deleteTbs, false));
1,160✔
732
  // TODO: alterTbs?
733
  TAOS_CHECK_EXIT(mndDupRoleHash(pOld->parentRoles, &pNew->parentRoles));
1,160✔
734
  TAOS_CHECK_EXIT(mndDupRoleHash(pOld->subRoles, &pNew->subRoles));
1,160✔
735
_exit:
1,160✔
736
  taosRUnLockLatch(&pOld->lock);
1,160✔
737
  if (code < 0) {
1,160✔
738
    mError("role:%s, failed at line %d to dup obj since %s", pOld->name, lino, tstrerror(code));
×
739
  }
740
  TAOS_RETURN(code);
1,160✔
741
}
742

743
static bool mndIsRoleChanged(SRoleObj *pOld, SAlterRoleReq *pAlterReq) {
1,160✔
744
  switch (pAlterReq->alterType) {
1,160✔
745
    case TSDB_ALTER_ROLE_LOCK: {
×
746
      if ((pAlterReq->lock && !pOld->enable) || (!pAlterReq->lock && pOld->enable)) {
×
747
        return false;
×
748
      }
749
      break;
×
750
    }
751
    default:
1,160✔
752
      break;
1,160✔
753
  }
754
  return true;
1,160✔
755
}
756

757
#ifdef TD_ENTERPRISE
758
extern int32_t mndAlterRoleInfo(SMnode *pMnode, SUserObj *pOperUser, const char *token, SRoleObj *pOld, SRoleObj *pNew,
759
                                SAlterRoleReq *pAlterReq);
760
#endif
761

762
static int32_t mndProcessAlterRoleReq(SRpcMsg *pReq) {
1,005,906✔
763
  int32_t       code = 0, lino = 0;
1,005,906✔
764
  SMnode       *pMnode = pReq->info.node;
1,005,906✔
765
  SRoleObj     *pObj = NULL;
1,005,906✔
766
  SRoleObj      newObj = {0};
1,005,906✔
767
  SUserObj     *pOperUser = NULL;
1,005,906✔
768
  SAlterRoleReq alterReq = {0};
1,005,906✔
769
  bool          alterUser = false;
1,005,906✔
770
  int64_t       tss = taosGetTimestampMs();
1,005,906✔
771

772
  TAOS_CHECK_EXIT(tDeserializeSAlterRoleReq(pReq->pCont, pReq->contLen, &alterReq));
1,005,906✔
773

774
  mInfo("role:%s, start to alter, flag:%u", alterReq.principal, alterReq.flag);
1,005,906✔
775
  TAOS_CHECK_EXIT(mndCheckOperPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_ALTER_ROLE));
1,005,906✔
776

777
  TAOS_CHECK_EXIT(mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser));
1,005,906✔
778

779
  if (alterReq.principal[0] == 0) {
1,005,906✔
780
    TAOS_CHECK_EXIT(TSDB_CODE_MND_ROLE_INVALID_FORMAT);
×
781
  }
782

783
  if (mndAcquireRole(pMnode, alterReq.principal, &pObj) == 0) {
1,005,906✔
784
    if (alterReq.alterType == TSDB_ALTER_ROLE_ROLE) {
1,160✔
785
      TAOS_CHECK_EXIT(TSDB_CODE_OPS_NOT_SUPPORT);  // not support grant role to role yet
×
786
    }
787
  } else {
788
    if (alterReq.alterType == TSDB_ALTER_ROLE_LOCK) {
1,004,746✔
789
      TAOS_CHECK_EXIT(terrno);
×
790
    }
791
    alterUser = true;
1,004,746✔
792
  }
793

794
  if (alterUser) {
1,005,906✔
795
    mInfo("role:%s, not exist, will alter user instead", alterReq.principal);
1,004,746✔
796
    TAOS_CHECK_EXIT(mndAlterUserFromRole(pReq, pOperUser, &alterReq));
1,004,746✔
797
  } else if (mndIsRoleChanged(pObj, &alterReq)) {
1,160✔
798
    TAOS_CHECK_EXIT(mndRoleDupObj(pObj, &newObj));
1,160✔
799
#ifdef TD_ENTERPRISE
800
    TAOS_CHECK_EXIT(mndAlterRoleInfo(pMnode, pOperUser, RPC_MSG_TOKEN(pReq), pObj, &newObj, &alterReq));
1,160✔
801
#endif
802
    TAOS_CHECK_EXIT(mndAlterRole(pMnode, pReq, &newObj));
1,015✔
803
    if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
1,015✔
804
    mndSetRoleLastUpd(taosGetTimestampMs());
1,015✔
805
  }
806

807
  if (tsAuditLevel >= AUDIT_LEVEL_CLUSTER) {
1,160✔
808
    int64_t tse = taosGetTimestampMs();
1,160✔
809
    double  duration = (double)(tse - tss);
1,160✔
810
    duration = duration / 1000;
1,160✔
811
    auditRecord(pReq, pMnode->clusterId, "alterRole", "", alterReq.principal, alterReq.sql, alterReq.sqlLen, duration,
1,160✔
812
                0);
813
  }
814
_exit:
1,005,906✔
815
  if (code < 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
1,005,906✔
816
    mError("role:%s, failed to alter at line %d since %s", alterReq.principal, lino, tstrerror(code));
1,548✔
817
  }
818
  mndReleaseUser(pMnode, pOperUser);
1,005,906✔
819
  mndReleaseRole(pMnode, pObj);
1,005,906✔
820
  mndRoleFreeObj(&newObj);
1,005,906✔
821
  tFreeSAlterRoleReq(&alterReq);
1,005,906✔
822
  TAOS_RETURN(code);
1,005,906✔
823
}
824

825
static int32_t mndUpgradeDefaultRoles(SMnode *pMnode, int32_t version) {
361,331✔
826
  int32_t code = 0, lino = 0;
361,331✔
827
  if (!mndIsLeader(pMnode)) return code;
361,331✔
828
  SRpcMsg rpcMsg = {.msgType = TDMT_MND_UPGRADE_ROLE, .info.ahandle = 0, .info.notFreeAhandle = 1};
361,331✔
829
  SEpSet  epSet = {0};
361,331✔
830
  mndGetMnodeEpSet(pMnode, &epSet);
361,331✔
831
  TAOS_CHECK_EXIT(tmsgSendReq(&epSet, &rpcMsg));
361,331✔
832
_exit:
361,331✔
833
  if (code < 0) {
361,331✔
NEW
834
    mError("failed at line %d to upgrade roles since %s", lino, tstrerror(code));
×
835
  }
836
  TAOS_RETURN(code);
361,331✔
837
}
838

839
static int32_t mndProcessUpgradeRoleReq(SRpcMsg *pReq) {
361,331✔
840
  return mndCreateDefaultRoles(pReq->info.node);
361,331✔
841
}
842

843
static int32_t mndProcessUpgradeRoleRsp(SRpcMsg *pReq) { return 0; }
361,331✔
844

UNCOV
845
static int32_t mndProcessGetRoleAuthReq(SRpcMsg *pReq) { TAOS_RETURN(0); }
×
846

847
static int32_t mndRetrieveRoles(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
145✔
848
  SMnode   *pMnode = pReq->info.node;
145✔
849
  SSdb     *pSdb = pMnode->pSdb;
145✔
850
  int32_t   code = 0, lino = 0;
145✔
851
  int32_t   numOfRows = 0;
145✔
852
  SRoleObj *pObj = NULL;
145✔
853
  int32_t   cols = 0;
145✔
854
  int32_t   bufSize = TSDB_MAX_SUBROLE * TSDB_ROLE_LEN + VARSTR_HEADER_SIZE;
145✔
855
  char      tBuf[TSDB_MAX_SUBROLE * TSDB_ROLE_LEN + VARSTR_HEADER_SIZE] = {0};
145✔
856

857
  while (numOfRows < rows) {
1,160✔
858
    pShow->pIter = sdbFetch(pSdb, SDB_ROLE, pShow->pIter, (void **)&pObj);
1,160✔
859
    if (pShow->pIter == NULL) break;
1,160✔
860

861
    cols = 0;
1,015✔
862
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1,015✔
863
    char             name[TSDB_ROLE_LEN + VARSTR_HEADER_SIZE] = {0};
1,015✔
864
    STR_WITH_MAXSIZE_TO_VARSTR(name, pObj->name, pShow->pMeta->pSchemas[cols].bytes);
1,015✔
865
    COL_DATA_SET_VAL_GOTO((const char *)name, false, pObj, pShow->pIter, _exit);
1,015✔
866

867
    if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
1,015✔
868
      int8_t enable = pObj->enable ? 1 : 0;
1,015✔
869
      COL_DATA_SET_VAL_GOTO((const char *)&enable, false, pObj, pShow->pIter, _exit);
1,015✔
870
    }
871

872
    if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
1,015✔
873
      COL_DATA_SET_VAL_GOTO((const char *)&pObj->createdTime, false, pObj, pShow->pIter, _exit);
1,015✔
874
    }
875
    if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
1,015✔
876
      COL_DATA_SET_VAL_GOTO((const char *)&pObj->updateTime, false, pObj, pShow->pIter, _exit);
1,015✔
877
    }
878
    if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
1,015✔
879
      char *roleType = pObj->sys ? "SYSTEM" : "USER";
1,015✔
880
      STR_WITH_MAXSIZE_TO_VARSTR(tBuf, roleType, pShow->pMeta->pSchemas[cols].bytes);
1,015✔
881
      COL_DATA_SET_VAL_GOTO((const char *)tBuf, false, pObj, pShow->pIter, _exit);
1,015✔
882
    }
883

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

912
static void mndCancelGetNextRole(SMnode *pMnode, void *pIter) {
×
913
  SSdb *pSdb = pMnode->pSdb;
×
914
  sdbCancelFetchByType(pSdb, pIter, SDB_ROLE);
×
915
}
×
916

917
static int32_t mndRetrievePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
725✔
918
  int32_t   code = 0, lino = 0;
725✔
919
  SMnode   *pMnode = pReq->info.node;
725✔
920
  SSdb     *pSdb = pMnode->pSdb;
725✔
921
  int32_t   numOfRows = 0;
725✔
922
  int32_t   cols = 0;
725✔
923
  SRoleObj *pObj = NULL;
725✔
924
  char     *pBuf = NULL, *qBuf = NULL;
725✔
925
  char     *sql = NULL;
725✔
926
  char      roleName[TSDB_ROLE_LEN + VARSTR_HEADER_SIZE] = {0};
725✔
927
  int32_t   bufSize = TSDB_PRIVILEDGE_CONDITION_LEN + VARSTR_HEADER_SIZE;
725✔
928

929
  bool fetchNextInstance = pShow->restore ? false : true;
725✔
930
  pShow->restore = false;
725✔
931

932
  while (numOfRows < rows) {
5,800✔
933
    if (fetchNextInstance) {
5,800✔
934
      pShow->pIter = sdbFetch(pSdb, SDB_ROLE, pShow->pIter, (void **)&pObj);
5,800✔
935
      if (pShow->pIter == NULL) break;
5,800✔
936
    } else {
937
      fetchNextInstance = true;
×
938
      void *pKey = taosHashGetKey(pShow->pIter, NULL);
×
939
      if (!(pObj = sdbAcquire(pSdb, SDB_ROLE, pKey))) {
×
940
        continue;
×
941
      }
942
    }
943

944
    // count total privileges for current role
945
    int32_t nSysPrivileges = privPopCnt(&pObj->sysPrivs);
5,075✔
946
    int32_t nObjPrivileges = 0;
5,075✔
947
    void   *pIter = NULL;
5,075✔
948
    while ((pIter = taosHashIterate(pObj->objPrivs, pIter))) {
38,860✔
949
      SPrivObjPolicies *pPolices = (SPrivObjPolicies *)pIter;
33,785✔
950
      nObjPrivileges += privPopCnt(&pPolices->policy);
67,570✔
951
    }
952
    int32_t nTblPrivileges = privTblPrivCnt(pObj->selectTbs);
5,075✔
953
    nTblPrivileges += privTblPrivCnt(pObj->insertTbs);
5,075✔
954
    nTblPrivileges += privTblPrivCnt(pObj->updateTbs);
5,075✔
955
    nTblPrivileges += privTblPrivCnt(pObj->deleteTbs);
5,075✔
956

957
    int32_t totalPrivileges = nSysPrivileges + nObjPrivileges + nTblPrivileges;
5,075✔
958

959
    if (numOfRows + totalPrivileges >= rows) {
5,075✔
NEW
960
      if (totalPrivileges >= SHOW_PRIVILEGES_STEP_SIZE) {
×
NEW
961
        mError("role:%s, has too many privileges:%d to show", pObj->name, totalPrivileges);
×
NEW
962
        sdbRelease(pSdb, pObj);
×
NEW
963
        TAOS_CHECK_EXIT(TSDB_CODE_MND_TOO_MANY_PRIVS);
×
964
      }
965
      pShow->restore = true;
×
966
      sdbRelease(pSdb, pObj);
×
967
      break;
×
968
    }
969

970
    if (!pBuf && !(pBuf = taosMemoryMalloc(bufSize))) {
5,075✔
971
      sdbRelease(pSdb, pObj);
×
972
      TAOS_CHECK_EXIT(terrno);
×
973
    }
974

975
    cols = 0;
5,075✔
976
    STR_WITH_MAXSIZE_TO_VARSTR(roleName, pObj->name, pShow->pMeta->pSchemas[cols].bytes);
5,075✔
977

978
    // system privileges
979
    SPrivIter privIter = {0};
5,075✔
980
    privIterInit(&privIter, &pObj->sysPrivs);
5,075✔
981
    SPrivInfo *pPrivInfo = NULL;
5,075✔
982
    while (privIterNext(&privIter, &pPrivInfo)) {
122,525✔
983
      cols = 0;
117,450✔
984
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
117,450✔
985
      COL_DATA_SET_VAL_GOTO((const char *)roleName, false, pObj, pShow->pIter, _exit);
117,450✔
986

987
      if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
117,450✔
988
        STR_WITH_MAXSIZE_TO_VARSTR(pBuf, pPrivInfo->name, pShow->pMeta->pSchemas[cols].bytes);
117,450✔
989
        COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
117,450✔
990
      }
991
      if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
117,450✔
992
        STR_WITH_MAXSIZE_TO_VARSTR(pBuf, privObjGetName(PRIV_OBJ_CLUSTER), pShow->pMeta->pSchemas[cols].bytes);
117,450✔
993
        COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
117,450✔
994
      }
995
      // skip db, table, condition, notes, columns, update_time
996
      COL_DATA_SET_EMPTY_VARCHAR(pBuf, 6);
822,150✔
997

998
      numOfRows++;
117,450✔
999
    }
1000

1001
    // object privileges
1002
    pIter = NULL;
5,075✔
1003
    while ((pIter = taosHashIterate(pObj->objPrivs, pIter))) {
38,860✔
1004
      SPrivObjPolicies *pPolices = (SPrivObjPolicies *)pIter;
33,785✔
1005

1006
      char   *key = taosHashGetKey(pPolices, NULL);
33,785✔
1007
      int32_t objType = PRIV_OBJ_UNKNOWN;
33,785✔
1008
      char    dbName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
33,785✔
1009
      char    tblName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
33,785✔
1010

1011
      if ((code = privObjKeyParse(key, &objType, dbName, sizeof(dbName), tblName, sizeof(tblName), false))) {
33,785✔
1012
        sdbRelease(pSdb, pObj);
×
1013
        TAOS_CHECK_EXIT(code);
×
1014
      }
1015

1016
      SPrivIter privIter = {0};
33,785✔
1017
      privIterInit(&privIter, &pPolices->policy);
33,785✔
1018
      SPrivInfo *pPrivInfo = NULL;
33,785✔
1019
      while (privIterNext(&privIter, &pPrivInfo)) {
143,840✔
1020
        cols = 0;
110,055✔
1021
        SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
110,055✔
1022
        COL_DATA_SET_VAL_GOTO((const char *)roleName, false, pObj, pShow->pIter, _exit);
110,055✔
1023

1024
        if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
110,055✔
1025
          STR_WITH_MAXSIZE_TO_VARSTR(pBuf, pPrivInfo->name, pShow->pMeta->pSchemas[cols].bytes);
110,055✔
1026
          COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
110,055✔
1027
        }
1028

1029
        if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
110,055✔
1030
          STR_WITH_MAXSIZE_TO_VARSTR(pBuf, privObjGetName(objType), pShow->pMeta->pSchemas[cols].bytes);
110,055✔
1031
          COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
110,055✔
1032
        }
1033

1034
        if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
110,055✔
1035
          STR_WITH_MAXSIZE_TO_VARSTR(pBuf, dbName, pShow->pMeta->pSchemas[cols].bytes);
110,055✔
1036
          COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
110,055✔
1037
        }
1038

1039
        if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
110,055✔
1040
          STR_WITH_MAXSIZE_TO_VARSTR(pBuf, tblName, pShow->pMeta->pSchemas[cols].bytes);
110,055✔
1041
          COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
110,055✔
1042
        }
1043

1044
        // skip condition, notes, columns, update_time
1045
        COL_DATA_SET_EMPTY_VARCHAR(pBuf, 4);
550,275✔
1046

1047
        numOfRows++;
110,055✔
1048
      }
1049
    }
1050
    // table level privileges
1051
    TAOS_CHECK_EXIT(mndShowTablePrivileges(pReq, pShow, pBlock, rows - numOfRows, pObj, pObj->name, pObj->selectTbs,
5,075✔
1052
                                           PRIV_TBL_SELECT, pBuf, bufSize, &numOfRows));
1053
    TAOS_CHECK_EXIT(mndShowTablePrivileges(pReq, pShow, pBlock, rows - numOfRows, pObj, pObj->name, pObj->insertTbs,
5,075✔
1054
                                           PRIV_TBL_INSERT, pBuf, bufSize, &numOfRows));
1055
    TAOS_CHECK_EXIT(mndShowTablePrivileges(pReq, pShow, pBlock, rows - numOfRows, pObj, pObj->name, pObj->updateTbs,
5,075✔
1056
                                           PRIV_TBL_UPDATE, pBuf, bufSize, &numOfRows));
1057
    TAOS_CHECK_EXIT(mndShowTablePrivileges(pReq, pShow, pBlock, rows - numOfRows, pObj, pObj->name, pObj->deleteTbs,
5,075✔
1058
                                           PRIV_TBL_DELETE, pBuf, bufSize, &numOfRows));
1059
    sdbRelease(pSdb, pObj);
5,075✔
1060
  }
1061

1062
  pShow->numOfRows += numOfRows;
725✔
1063
_exit:
725✔
1064
  taosMemoryFreeClear(pBuf);
725✔
1065
  taosMemoryFreeClear(sql);
725✔
1066
  if (code < 0) {
725✔
NEW
1067
    mError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1068
    TAOS_RETURN(code);
×
1069
  }
1070
  return numOfRows;
725✔
1071
}
1072

1073
static void mndCancelGetNextPrivileges(SMnode *pMnode, void *pIter) {
×
1074
  SSdb *pSdb = pMnode->pSdb;
×
1075
  sdbCancelFetchByType(pSdb, pIter, SDB_ROLE);
×
1076
}
×
1077

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

1091
  bool fetchNextInstance = pShow->restore ? false : true;
1092
  pShow->restore = false;
1093

1094
  while (numOfRows < rows) {
1095
    if (fetchNextInstance) {
1096
      pShow->pIter = sdbFetch(pSdb, SDB_ROLE, pShow->pIter, (void **)&pObj);
1097
      if (pShow->pIter == NULL) break;
1098
    } else {
1099
      fetchNextInstance = true;
1100
      void *pKey = taosHashGetKey(pShow->pIter, NULL);
1101
      if (!(pObj = sdbAcquire(pSdb, SDB_ROLE, pKey))) {
1102
        continue;
1103
      }
1104
    }
1105

1106
    int32_t nSysPrivileges = 0, nObjPrivileges = 0;
1107
    if (nSysPrivileges + nObjPrivileges >= rows) {
1108
      pShow->restore = true;
1109
      sdbRelease(pSdb, pObj);
1110
      break;
1111
    }
1112

1113
    if (!pBuf && !(pBuf = taosMemoryMalloc(bufSize))) {
1114
      sdbRelease(pSdb, pObj);
1115
      TAOS_CHECK_EXIT(terrno);
1116
    }
1117

1118
    cols = 0;
1119
    STR_WITH_MAXSIZE_TO_VARSTR(roleName, pObj->name, pShow->pMeta->pSchemas[cols].bytes);
1120

1121
    SPrivIter privIter = {0};
1122
    privIterInit(&privIter, &pObj->sysPrivs);
1123
    SPrivInfo *pPrivInfo = NULL;
1124
    while (privIterNext(&privIter, &pPrivInfo)) {
1125
      cols = 0;
1126
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
1127
      COL_DATA_SET_VAL_GOTO((const char *)roleName, false, pObj, pShow->pIter, _exit);
1128

1129
      if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
1130
        STR_WITH_MAXSIZE_TO_VARSTR(pBuf, pPrivInfo->name, pShow->pMeta->pSchemas[cols].bytes);
1131
        COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
1132
      }
1133

1134
      // for (int32_t i = 0; i < 6; i++) {
1135
      //   if ((pColInfo = taosArrayGet(pBlock->pDataBlock, ++cols))) {
1136
      //     STR_WITH_MAXSIZE_TO_VARSTR(pBuf, "", 2);
1137
      //     COL_DATA_SET_VAL_GOTO((const char *)pBuf, false, pObj, pShow->pIter, _exit);
1138
      //   }
1139
      // }
1140
      // skip db, table, condition, notes, columns, update_time
1141
      COL_DATA_SET_EMPTY_VARCHAR(pBuf, 7);
1142
      numOfRows++;
1143
    }
1144

1145
    sdbRelease(pSdb, pObj);
1146
  }
1147

1148
  pShow->numOfRows += numOfRows;
1149
_exit:
1150
  taosMemoryFreeClear(pBuf);
1151
  taosMemoryFreeClear(sql);
1152
  if (code < 0) {
1153
    mError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
1154
    TAOS_RETURN(code);
1155
  }
1156
#endif
UNCOV
1157
  return numOfRows;
×
1158
}
1159

1160
static void mndCancelGetNextColPrivileges(SMnode *pMnode, void *pIter) {
×
1161
  SSdb *pSdb = pMnode->pSdb;
×
1162
  sdbCancelFetchByType(pSdb, pIter, SDB_ROLE);
×
1163
}
×
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