From 689e7f0e3dc1a414671cd9c899411a322f1e9635 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Fri, 11 Feb 2022 15:20:27 +0800 Subject: [PATCH] process get user auth request --- include/common/tmsg.h | 19 +++++- include/common/tmsgdef.h | 1 + source/common/src/tmsg.c | 70 +++++++++++++++++++++- source/dnode/mgmt/impl/src/dndTransport.c | 1 + source/dnode/mnode/impl/src/mndUser.c | 64 +++++++++++++++++++- source/dnode/mnode/impl/test/user/user.cpp | 28 +++++++++ 6 files changed, 180 insertions(+), 3 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index e3c8f1e993..a090f6925c 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -371,15 +371,32 @@ void* tDeserializeSCreateUserReq(void* buf, SCreateUserReq* pReq); typedef struct { int8_t alterType; + int8_t superUser; char user[TSDB_USER_LEN]; char pass[TSDB_PASSWORD_LEN]; char dbname[TSDB_DB_FNAME_LEN]; - int8_t superUser; } SAlterUserReq; int32_t tSerializeSAlterUserReq(void** buf, SAlterUserReq* pReq); void* tDeserializeSAlterUserReq(void* buf, SAlterUserReq* pReq); +typedef struct { + char user[TSDB_USER_LEN]; +} SGetUserAuthReq; + +int32_t tSerializeSGetUserAuthReq(void** buf, SGetUserAuthReq* pReq); +void* tDeserializeSGetUserAuthReq(void* buf, SGetUserAuthReq* pReq); + +typedef struct { + char user[TSDB_USER_LEN]; + int8_t superAuth; + SHashObj* readDbs; + SHashObj* writeDbs; +} SGetUserAuthRsp; + +int32_t tSerializeSGetUserAuthRsp(void** buf, SGetUserAuthRsp* pReq); +void* tDeserializeSGetUserAuthRsp(void* buf, SGetUserAuthRsp* pReq); + typedef struct { int16_t colId; // column id int16_t colIndex; // column index in colList if it is a normal column or index in tagColList if a tag diff --git a/include/common/tmsgdef.h b/include/common/tmsgdef.h index f95fe8dd08..98d2b2c519 100644 --- a/include/common/tmsgdef.h +++ b/include/common/tmsgdef.h @@ -97,6 +97,7 @@ enum { TD_DEF_MSG_TYPE(TDMT_MND_CREATE_USER, "mnode-create-user", NULL, NULL) TD_DEF_MSG_TYPE(TDMT_MND_ALTER_USER, "mnode-alter-user", NULL, NULL) TD_DEF_MSG_TYPE(TDMT_MND_DROP_USER, "mnode-drop-user", NULL, NULL) + TD_DEF_MSG_TYPE(TDMT_MND_GET_USER_AUTH, "mnode-get-user-auth", NULL, NULL) TD_DEF_MSG_TYPE(TDMT_MND_CREATE_DNODE, "mnode-create-dnode", NULL, NULL) TD_DEF_MSG_TYPE(TDMT_MND_CONFIG_DNODE, "mnode-config-dnode", NULL, NULL) TD_DEF_MSG_TYPE(TDMT_MND_ALTER_DNODE, "mnode-alter-dnode", NULL, NULL) diff --git a/source/common/src/tmsg.c b/source/common/src/tmsg.c index 17120a6a5e..33add3eb6b 100644 --- a/source/common/src/tmsg.c +++ b/source/common/src/tmsg.c @@ -670,4 +670,72 @@ void *tDeserializeSAlterUserReq(void *buf, SAlterUserReq *pReq) { buf = taosDecodeStringTo(buf, pReq->dbname); buf = taosDecodeFixedI8(buf, &pReq->superUser); return buf; -} \ No newline at end of file +} + +int32_t tSerializeSGetUserAuthReq(void **buf, SGetUserAuthReq *pReq) { + int32_t tlen = 0; + tlen += taosEncodeString(buf, pReq->user); + return tlen; +} + +void *tDeserializeSGetUserAuthReq(void *buf, SGetUserAuthReq *pReq) { + buf = taosDecodeStringTo(buf, pReq->user); + return buf; +} + +int32_t tSerializeSGetUserAuthRsp(void **buf, SGetUserAuthRsp *pReq) { + int32_t tlen = 0; + tlen += taosEncodeString(buf, pReq->user); + tlen += taosEncodeFixedI8(buf, pReq->superAuth); + + int32_t numOfReadDbs = taosHashGetSize(pReq->readDbs); + int32_t numOfWriteDbs = taosHashGetSize(pReq->writeDbs); + tlen += taosEncodeFixedI32(buf, numOfReadDbs); + tlen += taosEncodeFixedI32(buf, numOfWriteDbs); + + char *db = taosHashIterate(pReq->readDbs, NULL); + while (db != NULL) { + tlen += taosEncodeString(buf, db); + db = taosHashIterate(pReq->readDbs, db); + } + + db = taosHashIterate(pReq->writeDbs, NULL); + while (db != NULL) { + tlen += taosEncodeString(buf, db); + db = taosHashIterate(pReq->writeDbs, db); + } + + return tlen; +} + +void *tDeserializeSGetUserAuthRsp(void *buf, SGetUserAuthRsp *pReq) { + buf = taosDecodeStringTo(buf, pReq->user); + buf = taosDecodeFixedI8(buf, &pReq->superAuth); + + pReq->readDbs = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, false); + pReq->writeDbs = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, false); + if (pReq->readDbs == NULL || pReq->writeDbs == NULL) { + return NULL; + } + + int32_t numOfReadDbs = 0; + int32_t numOfWriteDbs = 0; + buf = taosDecodeFixedI32(buf, &numOfReadDbs); + buf = taosDecodeFixedI32(buf, &numOfWriteDbs); + + for (int32_t i = 0; i < numOfReadDbs; ++i) { + char db[TSDB_DB_FNAME_LEN] = {0}; + buf = taosDecodeStringTo(buf, db); + int32_t len = strlen(db) + 1; + taosHashPut(pReq->readDbs, db, len, db, len); + } + + for (int32_t i = 0; i < numOfWriteDbs; ++i) { + char db[TSDB_DB_FNAME_LEN] = {0}; + buf = taosDecodeStringTo(buf, db); + int32_t len = strlen(db) + 1; + taosHashPut(pReq->writeDbs, db, len, db, len); + } + + return buf; +} diff --git a/source/dnode/mgmt/impl/src/dndTransport.c b/source/dnode/mgmt/impl/src/dndTransport.c index 257c72bff5..9b3ab40226 100644 --- a/source/dnode/mgmt/impl/src/dndTransport.c +++ b/source/dnode/mgmt/impl/src/dndTransport.c @@ -73,6 +73,7 @@ static void dndInitMsgFp(STransMgmt *pMgmt) { pMgmt->msgFp[TMSG_INDEX(TDMT_MND_CREATE_USER)] = dndProcessMnodeWriteMsg; pMgmt->msgFp[TMSG_INDEX(TDMT_MND_ALTER_USER)] = dndProcessMnodeWriteMsg; pMgmt->msgFp[TMSG_INDEX(TDMT_MND_DROP_USER)] = dndProcessMnodeWriteMsg; + pMgmt->msgFp[TMSG_INDEX(TDMT_MND_GET_USER_AUTH)] = dndProcessMnodeReadMsg; pMgmt->msgFp[TMSG_INDEX(TDMT_MND_CREATE_DNODE)] = dndProcessMnodeWriteMsg; pMgmt->msgFp[TMSG_INDEX(TDMT_MND_CONFIG_DNODE)] = dndProcessMnodeWriteMsg; pMgmt->msgFp[TMSG_INDEX(TDMT_MND_DROP_DNODE)] = dndProcessMnodeWriteMsg; diff --git a/source/dnode/mnode/impl/src/mndUser.c b/source/dnode/mnode/impl/src/mndUser.c index 5db89aa0cf..b54200f114 100644 --- a/source/dnode/mnode/impl/src/mndUser.c +++ b/source/dnode/mnode/impl/src/mndUser.c @@ -34,6 +34,7 @@ static int32_t mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreat static int32_t mndProcessCreateUserReq(SMnodeMsg *pReq); static int32_t mndProcessAlterUserReq(SMnodeMsg *pReq); static int32_t mndProcessDropUserReq(SMnodeMsg *pReq); +static int32_t mndProcessGetUserAuthReq(SMnodeMsg *pReq); static int32_t mndGetUserMeta(SMnodeMsg *pReq, SShowObj *pShow, STableMetaRsp *pMeta); static int32_t mndRetrieveUsers(SMnodeMsg *pReq, SShowObj *pShow, char *data, int32_t rows); static void mndCancelGetNextUser(SMnode *pMnode, void *pIter); @@ -51,6 +52,7 @@ int32_t mndInitUser(SMnode *pMnode) { mndSetMsgHandle(pMnode, TDMT_MND_CREATE_USER, mndProcessCreateUserReq); mndSetMsgHandle(pMnode, TDMT_MND_ALTER_USER, mndProcessAlterUserReq); mndSetMsgHandle(pMnode, TDMT_MND_DROP_USER, mndProcessDropUserReq); + mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_AUTH, mndProcessGetUserAuthReq); mndAddShowMetaHandle(pMnode, TSDB_MGMT_TABLE_USER, mndGetUserMeta); mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_USER, mndRetrieveUsers); @@ -574,6 +576,66 @@ DROP_USER_OVER: return code; } +static int32_t mndProcessGetUserAuthReq(SMnodeMsg *pReq) { + SMnode *pMnode = pReq->pMnode; + int32_t code = -1; + SUserObj *pUser = NULL; + SGetUserAuthReq authReq = {0}; + SGetUserAuthRsp authRsp = {0}; + + if (tDeserializeSGetUserAuthReq(pReq->rpcMsg.pCont, &authReq) == NULL) goto GET_AUTH_OVER; + + mTrace("user:%s, start to get auth", authReq.user); + + pUser = mndAcquireUser(pMnode, authReq.user); + if (pUser == NULL) { + terrno = TSDB_CODE_MND_USER_NOT_EXIST; + goto GET_AUTH_OVER; + } + + memcpy(authRsp.user, pUser->user, TSDB_USER_LEN); + authRsp.superAuth = pUser->superUser; + authRsp.readDbs = mndDupDbHash(pUser->readDbs); + authRsp.writeDbs = mndDupDbHash(pUser->writeDbs); + + SSdb *pSdb = pMnode->pSdb; + void *pIter = NULL; + while (1) { + SDbObj *pDb = NULL; + pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb); + if (pIter == NULL) break; + + if (strcmp(pDb->createUser, pUser->user) == 0) { + int32_t len = strlen(pDb->name) + 1; + taosHashPut(authRsp.readDbs, pDb->name, len, pDb->name, len); + taosHashPut(authRsp.writeDbs, pDb->name, len, pDb->name, len); + } + + sdbRelease(pSdb, pDb); + } + + int32_t contLen = tSerializeSGetUserAuthRsp(NULL, &authRsp); + void *pRsp = rpcMallocCont(contLen); + if (pRsp == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + goto GET_AUTH_OVER; + } + + void *pBuf = pRsp; + tSerializeSGetUserAuthRsp(&pBuf, &authRsp); + + pReq->pCont = pRsp; + pReq->contLen = contLen; + code = 0; + +GET_AUTH_OVER: + mndReleaseUser(pMnode, pUser); + taosHashCleanup(authRsp.readDbs); + taosHashCleanup(authRsp.writeDbs); + + return code; +} + static int32_t mndGetUserMeta(SMnodeMsg *pReq, SShowObj *pShow, STableMetaRsp *pMeta) { SMnode *pMnode = pReq->pMnode; SSdb *pSdb = pMnode->pSdb; @@ -668,4 +730,4 @@ static int32_t mndRetrieveUsers(SMnodeMsg *pReq, SShowObj *pShow, char *data, in static void mndCancelGetNextUser(SMnode *pMnode, void *pIter) { SSdb *pSdb = pMnode->pSdb; sdbCancelFetch(pSdb, pIter); -} \ No newline at end of file +} diff --git a/source/dnode/mnode/impl/test/user/user.cpp b/source/dnode/mnode/impl/test/user/user.cpp index d02f2095f2..5d8c958f15 100644 --- a/source/dnode/mnode/impl/test/user/user.cpp +++ b/source/dnode/mnode/impl/test/user/user.cpp @@ -398,6 +398,34 @@ TEST_F(MndTestUser, 03_Alter_User) { ASSERT_EQ(pRsp->code, 0); } + { + SGetUserAuthReq authReq = {0}; + strcpy(authReq.user, "u3"); + int32_t contLen = tSerializeSGetUserAuthReq(NULL, &authReq); + void* pReq = rpcMallocCont(contLen); + void* pBuf = pReq; + tSerializeSGetUserAuthReq(&pBuf, &authReq); + + SRpcMsg* pRsp = test.SendReq(TDMT_MND_GET_USER_AUTH, pReq, contLen); + ASSERT_NE(pRsp, nullptr); + ASSERT_EQ(pRsp->code, 0); + + SGetUserAuthRsp authRsp = {0}; + tDeserializeSGetUserAuthRsp(pRsp->pCont, &authRsp); + EXPECT_STREQ(authRsp.user, "u3"); + EXPECT_EQ(authRsp.superAuth, 1); + int32_t numOfReadDbs = taosHashGetSize(authRsp.readDbs); + int32_t numOfWriteDbs = taosHashGetSize(authRsp.writeDbs); + EXPECT_EQ(numOfReadDbs, 1); + EXPECT_EQ(numOfWriteDbs, 0); + + char* dbname = (char*)taosHashGet(authRsp.readDbs, "1.d2", 5); + EXPECT_STREQ(dbname, "1.d2"); + + taosHashCleanup(authRsp.readDbs); + taosHashCleanup(authRsp.writeDbs); + } + { SAlterUserReq alterReq = {0}; alterReq.alterType = TSDB_ALTER_USER_REMOVE_READ_DB; -- GitLab