diff --git a/src/client/src/tscSystem.c b/src/client/src/tscSystem.c index 68b11ce416c8224a375e3d4d2a09216e1b95f0ae..6713f84f9922ed81763562ff1f48ad5b0a74c551 100644 --- a/src/client/src/tscSystem.c +++ b/src/client/src/tscSystem.c @@ -42,6 +42,7 @@ void * tscTmr; void * tscQhandle; void * tscCheckDiskUsageTmr; int tsInsertHeadSize; +char tsLastUser[TSDB_USER_LEN + 1]; int tscNumOfThreads; @@ -81,6 +82,13 @@ int32_t tscInitRpc(const char *user, const char *secret) { } } + // not stop service, switch users + if (strcmp(tsLastUser, user) != 0 && pTscMgmtConn != NULL) { + tscTrace("switch user from %s to %s", user, tsLastUser); + rpcClose(pTscMgmtConn); + pTscMgmtConn = NULL; + } + if (pTscMgmtConn == NULL) { memset(&rpcInit, 0, sizeof(rpcInit)); rpcInit.localIp = tsLocalIp; @@ -94,6 +102,7 @@ int32_t tscInitRpc(const char *user, const char *secret) { rpcInit.user = (char*)user; rpcInit.ckey = "key"; rpcInit.secret = secretEncrypt; + strcpy(tsLastUser, user); pTscMgmtConn = rpcOpen(&rpcInit); if (pTscMgmtConn == NULL) { diff --git a/src/inc/taoserror.h b/src/inc/taoserror.h index 5cd38f6d6bb4d07383140286772c35533c98bef4..7b060b1c65d77f9d500a234adec3ab400dd1c890 100644 --- a/src/inc/taoserror.h +++ b/src/inc/taoserror.h @@ -75,14 +75,19 @@ TAOS_DEFINE_ERROR(TSDB_CODE_NOT_CONFIGURED, 0, 27, "not configured") TAOS_DEFINE_ERROR(TSDB_CODE_NODE_OFFLINE, 0, 28, "node offline") TAOS_DEFINE_ERROR(TSDB_CODE_NETWORK_UNAVAIL, 0, 29, "network unavailable") -// db & user +// db TAOS_DEFINE_ERROR(TSDB_CODE_DB_NOT_SELECTED, 0, 100, "db not selected") TAOS_DEFINE_ERROR(TSDB_CODE_DB_ALREADY_EXIST, 0, 101, "database aleady exist") TAOS_DEFINE_ERROR(TSDB_CODE_INVALID_DB, 0, 102, "invalid database") TAOS_DEFINE_ERROR(TSDB_CODE_MONITOR_DB_FORBIDDEN, 0, 103, "monitor db forbidden") -TAOS_DEFINE_ERROR(TSDB_CODE_USER_ALREADY_EXIST, 0, 104, "user already exist") -TAOS_DEFINE_ERROR(TSDB_CODE_INVALID_USER, 0, 105, "invalid user") -TAOS_DEFINE_ERROR(TSDB_CODE_INVALID_PASS, 0, 106, "invalid password") + +// user +TAOS_DEFINE_ERROR(TSDB_CODE_USER_ALREADY_EXIST, 0, 150, "user already exist") +TAOS_DEFINE_ERROR(TSDB_CODE_INVALID_USER, 0, 151, "invalid user") +TAOS_DEFINE_ERROR(TSDB_CODE_INVALID_PASS, 0, 152, "invalid password") +TAOS_DEFINE_ERROR(TSDB_CODE_INVALID_USER_FORMAT, 0, 153, "invalid user format") +TAOS_DEFINE_ERROR(TSDB_CODE_INVALID_PASS_FORMAT, 0, 154, "invalid password format") +TAOS_DEFINE_ERROR(TSDB_CODE_NO_USER_FROM_CONN, 0, 155, "can not get user from conn") // table TAOS_DEFINE_ERROR(TSDB_CODE_TABLE_ALREADY_EXIST, 0, 200, "table already exist") diff --git a/src/mnode/inc/mgmtUser.h b/src/mnode/inc/mgmtUser.h index 8f1cf5d4503db3cca8d396545c9f1aa06f810690..d0fd03de779e2e4e50fb31bd6633fe0c4824f9ac 100644 --- a/src/mnode/inc/mgmtUser.h +++ b/src/mnode/inc/mgmtUser.h @@ -24,6 +24,7 @@ extern "C" { int32_t mgmtInitUsers(); void mgmtCleanUpUsers(); SUserObj *mgmtGetUser(char *name); +void * mgmtGetNextUser(void *pNode, SUserObj **pUser); void mgmtIncUserRef(SUserObj *pUser); void mgmtDecUserRef(SUserObj *pUser); SUserObj *mgmtGetUserFromConn(void *pConn, bool *usePublicIp); diff --git a/src/mnode/src/mgmtDnode.c b/src/mnode/src/mgmtDnode.c index ef3f93ddade33565dd9c295e6c0640d297db7f04..23a1d157a5d845767deb7122636de6d237d07250 100644 --- a/src/mnode/src/mgmtDnode.c +++ b/src/mnode/src/mgmtDnode.c @@ -336,7 +336,7 @@ void mgmtProcessDnodeStatusMsg(SRpcMsg *rpcMsg) { if (pStatus->dnodeId == 0) { mTrace("dnode:%d, first access, privateIp:%s, name:%s", pDnode->dnodeId, taosIpStr(pDnode->privateIp), pDnode->dnodeName); } else { - mTrace("dnode:%d, status received, access times %d", pDnode->dnodeId, pDnode->lastAccess); + //mTrace("dnode:%d, status received, access times %d", pDnode->dnodeId, pDnode->lastAccess); } int32_t openVnodes = htons(pStatus->openVnodes); diff --git a/src/mnode/src/mgmtUser.c b/src/mnode/src/mgmtUser.c index 240704f9ecc149b5df44e3a0073b9f8c6411b493..77c48985f628842551adc486db9617963e28de4a 100644 --- a/src/mnode/src/mgmtUser.c +++ b/src/mnode/src/mgmtUser.c @@ -19,23 +19,23 @@ #include "ttime.h" #include "tutil.h" #include "tglobal.h" +#include "tgrant.h" #include "dnode.h" #include "mgmtDef.h" #include "mgmtLog.h" #include "mgmtAcct.h" -#include "tgrant.h" #include "mgmtMnode.h" #include "mgmtSdb.h" #include "mgmtShell.h" #include "mgmtUser.h" -void * tsUserSdb = NULL; +static void * tsUserSdb = NULL; static int32_t tsUserUpdateSize = 0; static int32_t mgmtGetUserMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn); static int32_t mgmtRetrieveUsers(SShowObj *pShow, char *data, int32_t rows, void *pConn); -static void mgmtProcessCreateUserMsg(SQueuedMsg *pMsg); -static void mgmtProcessAlterUserMsg(SQueuedMsg *pMsg); -static void mgmtProcessDropUserMsg(SQueuedMsg *pMsg); +static void mgmtProcessCreateUserMsg(SQueuedMsg *pMsg); +static void mgmtProcessAlterUserMsg(SQueuedMsg *pMsg); +static void mgmtProcessDropUserMsg(SQueuedMsg *pMsg); static int32_t mgmtUserActionDestroy(SSdbOper *pOper) { tfree(pOper->pObj); @@ -48,8 +48,8 @@ static int32_t mgmtUserActionInsert(SSdbOper *pOper) { if (pAcct != NULL) { mgmtAddUserToAcct(pAcct, pUser); - } - else { + mgmtDecAcctRef(pAcct); + } else { mError("user:%s, acct:%s info not exist in sdb", pUser->user, pUser->acct); return TSDB_CODE_INVALID_ACCT; } @@ -86,7 +86,7 @@ static int32_t mgmtUserActionEncode(SSdbOper *pOper) { } static int32_t mgmtUserActionDecode(SSdbOper *pOper) { - SUserObj *pUser = (SUserObj *) calloc(1, sizeof(SUserObj)); + SUserObj *pUser = (SUserObj *)calloc(1, sizeof(SUserObj)); if (pUser == NULL) return TSDB_CODE_SERV_OUT_OF_MEMORY; memcpy(pUser, pOper->rowData, tsUserUpdateSize); @@ -103,7 +103,7 @@ static int32_t mgmtUserActionRestored() { mgmtDecAcctRef(pAcct); } - return 0; + return TSDB_CODE_SUCCESS; } int32_t mgmtInitUsers() { @@ -128,7 +128,7 @@ int32_t mgmtInitUsers() { tsUserSdb = sdbOpenTable(&tableDesc); if (tsUserSdb == NULL) { - mError("failed to init user data"); + mError("table:%s, failed to create hash", tableDesc.tableName); return -1; } @@ -138,7 +138,7 @@ int32_t mgmtInitUsers() { mgmtAddShellShowMetaHandle(TSDB_MGMT_TABLE_USER, mgmtGetUserMeta); mgmtAddShellShowRetrieveHandle(TSDB_MGMT_TABLE_USER, mgmtRetrieveUsers); - mTrace("table:users table is created"); + mTrace("table:%s, hash is created", tableDesc.tableName); return 0; } @@ -150,6 +150,10 @@ SUserObj *mgmtGetUser(char *name) { return (SUserObj *)sdbGetRow(tsUserSdb, name); } +void *mgmtGetNextUser(void *pNode, SUserObj **pUser) { + return sdbFetchRow(tsUserSdb, pNode, (void **)pUser); +} + void mgmtIncUserRef(SUserObj *pUser) { return sdbIncRef(tsUserSdb, pUser); } @@ -176,17 +180,21 @@ static int32_t mgmtUpdateUser(SUserObj *pUser) { int32_t mgmtCreateUser(SAcctObj *pAcct, char *name, char *pass) { int32_t code = acctCheck(pAcct, ACCT_GRANT_USER); - if (code != 0) { + if (code != TSDB_CODE_SUCCESS) { return code; } - if (name[0] == 0 || pass[0] == 0) { - return TSDB_CODE_INVALID_MSG_CONTENT; + if (name[0] == 0) { + return TSDB_CODE_INVALID_USER_FORMAT; + } + + if (pass[0] == 0) { + return TSDB_CODE_INVALID_PASS_FORMAT; } SUserObj *pUser = mgmtGetUser(name); if (pUser != NULL) { - mTrace("user:%s is already there", name); + mTrace("user:%s, is already there", name); mgmtDecUserRef(pUser); return TSDB_CODE_USER_ALREADY_EXIST; } @@ -241,10 +249,10 @@ static int32_t mgmtDropUser(SUserObj *pUser) { static int32_t mgmtGetUserMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) { SUserObj *pUser = mgmtGetUserFromConn(pConn, NULL); if (pUser == NULL) { - return TSDB_CODE_INVALID_USER; + return TSDB_CODE_NO_USER_FROM_CONN; } - int32_t cols = 0; + int32_t cols = 0; SSchema *pSchema = pMeta->schema; pShow->bytes[cols] = TSDB_USER_LEN; @@ -314,6 +322,7 @@ static int32_t mgmtRetrieveUsers(SShowObj *pShow, char *data, int32_t rows, void numOfRows++; mgmtDecUserRef(pUser); } + pShow->numOfReads += numOfRows; return numOfRows; } @@ -325,20 +334,21 @@ SUserObj *mgmtGetUserFromConn(void *pConn, bool *usePublicIp) { *usePublicIp = (connInfo.serverIp == tsPublicIpInt); } return mgmtGetUser(connInfo.user); + } else { + mError("can not get user from conn:%p", pConn); + return NULL; } - - return NULL; } static void mgmtProcessCreateUserMsg(SQueuedMsg *pMsg) { int32_t code; - SUserObj *pUser = pMsg->pUser; + SUserObj *pOperUser = pMsg->pUser; - if (pUser->superAuth) { + if (pOperUser->superAuth) { SCMCreateUserMsg *pCreate = pMsg->pCont; - code = mgmtCreateUser(pUser->pAcct, pCreate->user, pCreate->pass); + code = mgmtCreateUser(pOperUser->pAcct, pCreate->user, pCreate->pass); if (code == TSDB_CODE_SUCCESS) { - mLPrint("user:%s is created by %s", pCreate->user, pUser->user); + mLPrint("user:%s, is created by %s", pCreate->user, pOperUser->user); } } else { code = TSDB_CODE_NO_RIGHTS; @@ -384,7 +394,7 @@ static void mgmtProcessAlterUserMsg(SQueuedMsg *pMsg) { memset(pUser->pass, 0, sizeof(pUser->pass)); taosEncryptPass((uint8_t*)pAlter->pass, strlen(pAlter->pass), pUser->pass); code = mgmtUpdateUser(pUser); - mLPrint("user:%s password is altered by %s, result:%d", pUser->user, pOperUser->user, tstrerror(code)); + mLPrint("user:%s, password is altered by %s, result:%s", pUser->user, pOperUser->user, tstrerror(code)); } else { code = TSDB_CODE_NO_RIGHTS; } @@ -426,7 +436,7 @@ static void mgmtProcessAlterUserMsg(SQueuedMsg *pMsg) { } code = mgmtUpdateUser(pUser); - mLPrint("user:%s privilege is altered by %s, result:%d", pUser->user, pOperUser->user, tstrerror(code)); + mLPrint("user:%s, privilege is altered by %s, result:%s", pUser->user, pOperUser->user, tstrerror(code)); } else { code = TSDB_CODE_NO_RIGHTS; } @@ -447,7 +457,7 @@ static void mgmtProcessDropUserMsg(SQueuedMsg *pMsg) { SUserObj *pUser = mgmtGetUser(pDrop->user); if (pUser == NULL) { mgmtSendSimpleResp(pMsg->thandle, TSDB_CODE_INVALID_USER); - return ; + return; } if (strcmp(pUser->user, "monitor") == 0 || strcmp(pUser->user, pUser->acct) == 0 || @@ -475,7 +485,7 @@ static void mgmtProcessDropUserMsg(SQueuedMsg *pMsg) { if (hasRight) { code = mgmtDropUser(pUser); if (code == TSDB_CODE_SUCCESS) { - mLPrint("user:%s is dropped by %s, result:%s", pUser->user, pOperUser->user, tstrerror(code)); + mLPrint("user:%s, is dropped by %s, result:%s", pUser->user, pOperUser->user, tstrerror(code)); } } else { code = TSDB_CODE_NO_RIGHTS; diff --git a/tests/script/basicSuite.sim b/tests/script/basicSuite.sim index 22c92b50c7d40b8077a0b0b6f24de8a900ad8d4c..aad021e3b526fb1ef46ad3b6b5918f035e63fd91 100644 --- a/tests/script/basicSuite.sim +++ b/tests/script/basicSuite.sim @@ -3,6 +3,6 @@ run general/db/testSuite.sim run general/insert/testSuite.sim run general/table/testSuite.sim -run general/user/testSuite.sim +run general/user/basicSuite.sim ################################## diff --git a/tests/script/general/user/basic1.sim b/tests/script/general/user/basic1.sim index 1b8c4c84d8682d020718c20d20ade67b307d2189..f02a4d0e2722efaa7ca5de8627de6edaeff51d93 100644 --- a/tests/script/general/user/basic1.sim +++ b/tests/script/general/user/basic1.sim @@ -66,6 +66,4 @@ print $data10 $data11 $data22 print $data20 $data11 $data22 print $data30 $data31 $data32 - - - +system sh/exec.sh -n dnode1 -s stop \ No newline at end of file diff --git a/tests/script/general/user/basicSuite.sim b/tests/script/general/user/basicSuite.sim new file mode 100644 index 0000000000000000000000000000000000000000..199c8f39a154e6abd07f891b325f648c1f466529 --- /dev/null +++ b/tests/script/general/user/basicSuite.sim @@ -0,0 +1 @@ +run general/user/basic1.sim \ No newline at end of file diff --git a/tests/script/general/account/monitor.sim b/tests/script/general/user/monitor.sim similarity index 100% rename from tests/script/general/account/monitor.sim rename to tests/script/general/user/monitor.sim diff --git a/tests/script/general/account/pass_alter.sim b/tests/script/general/user/pass_alter.sim similarity index 100% rename from tests/script/general/account/pass_alter.sim rename to tests/script/general/user/pass_alter.sim diff --git a/tests/script/general/account/pass_len.sim b/tests/script/general/user/pass_len.sim similarity index 100% rename from tests/script/general/account/pass_len.sim rename to tests/script/general/user/pass_len.sim diff --git a/tests/script/general/user/testSuite.sim b/tests/script/general/user/testSuite.sim index 199c8f39a154e6abd07f891b325f648c1f466529..242e8ca27aa49ac5acfa0c2ac94f2f7078176c13 100644 --- a/tests/script/general/user/testSuite.sim +++ b/tests/script/general/user/testSuite.sim @@ -1 +1,6 @@ -run general/user/basic1.sim \ No newline at end of file +run general/user/basic1.sim +run general/user/pass_alter.sim +run general/user/pass_len.sim +run general/user/user_create.sim +run general/user/user_len.sim +#run general/user/monitor.sim \ No newline at end of file diff --git a/tests/script/general/account/user_create.sim b/tests/script/general/user/user_create.sim similarity index 99% rename from tests/script/general/account/user_create.sim rename to tests/script/general/user/user_create.sim index 802b703118e78865d045942c14a73131245a79e1..e63246cbd649000c1924056eed30de4b7b8fbef2 100644 --- a/tests/script/general/account/user_create.sim +++ b/tests/script/general/user/user_create.sim @@ -4,8 +4,6 @@ system sh/ip.sh -i 1 -s up system sh/deploy.sh -n dnode1 -m 192.168.0.1 -i 192.168.0.1 system sh/cfg.sh -n dnode1 -c clog -v 0 system sh/exec.sh -n dnode1 -s start - -sleep 3000 sql connect print =============== step1 diff --git a/tests/script/general/account/user_len.sim b/tests/script/general/user/user_len.sim similarity index 100% rename from tests/script/general/account/user_len.sim rename to tests/script/general/user/user_len.sim