TDengine/source/dnode/mnode/impl/src/mndUser.c

287 lines
9.7 KiB
C
Raw Normal View History

2021-09-22 08:15:20 +00:00
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2021-10-16 07:16:05 +00:00
#define _DEFAULT_SOURCE
2021-12-03 12:52:44 +00:00
#include "mndUser.h"
#include "mndShow.h"
2021-11-27 14:56:18 +00:00
#include "mndSync.h"
#include "mndTrans.h"
2021-12-01 09:27:31 +00:00
#include "tkey.h"
2021-10-17 03:42:05 +00:00
2021-11-12 07:06:58 +00:00
#define SDB_USER_VER 1
2021-10-18 06:00:35 +00:00
2021-12-03 12:52:44 +00:00
static int32_t mndCreateDefaultUsers(SMnode *pMnode);
static SSdbRaw *mndUserActionEncode(SUserObj *pUser);
static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw);
static int32_t mndUserActionInsert(SSdb *pSdb, SUserObj *pUser);
static int32_t mndUserActionDelete(SSdb *pSdb, SUserObj *pUser);
static int32_t mndUserActionUpdate(SSdb *pSdb, SUserObj *pSrcUser, SUserObj *pDstUser);
static int32_t mndCreateUser(SMnode *pMnode, char *acct, char *user, char *pass, SMnodeMsg *pMsg);
static int32_t mndProcessCreateUserMsg(SMnodeMsg *pMsg);
static int32_t mndProcessAlterUserMsg(SMnodeMsg *pMsg);
static int32_t mndProcessDropUserMsg(SMnodeMsg *pMsg);
int32_t mndInitUser(SMnode *pMnode) {
SSdbTable table = {.sdbType = SDB_USER,
.keyType = SDB_KEY_BINARY,
.deployFp = (SdbDeployFp)mndCreateDefaultUsers,
.encodeFp = (SdbEncodeFp)mndUserActionEncode,
.decodeFp = (SdbDecodeFp)mndUserActionDecode,
.insertFp = (SdbInsertFp)mndUserActionInsert,
.updateFp = (SdbUpdateFp)mndUserActionUpdate,
.deleteFp = (SdbDeleteFp)mndUserActionDelete};
mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_CREATE_USER, mndProcessCreateUserMsg);
mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_ALTER_USER, mndProcessAlterUserMsg);
mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_DROP_USER, mndProcessDropUserMsg);
return sdbSetTable(pMnode->pSdb, table);
}
void mndCleanupUser(SMnode *pMnode) {}
static int32_t mndCreateDefaultUser(SMnode *pMnode, char *acct, char *user, char *pass) {
SUserObj userObj = {0};
tstrncpy(userObj.user, user, TSDB_USER_LEN);
tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
taosEncryptPass((uint8_t *)pass, strlen(pass), userObj.pass);
userObj.createdTime = taosGetTimestampMs();
userObj.updateTime = userObj.createdTime;
2021-12-05 10:37:54 +00:00
userObj.readAuth = 1;
userObj.writeAuth = 1;
2021-12-03 12:52:44 +00:00
if (strcmp(user, TSDB_DEFAULT_USER) == 0) {
userObj.superAuth = 1;
}
SSdbRaw *pRaw = mndUserActionEncode(&userObj);
if (pRaw == NULL) return -1;
sdbSetRawStatus(pRaw, SDB_STATUS_READY);
mTrace("user:%s, will be created while deploy sdb", userObj.user);
return sdbWrite(pMnode->pSdb, pRaw);
}
static int32_t mndCreateDefaultUsers(SMnode *pMnode) {
if (mndCreateDefaultUser(pMnode, TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS) != 0) {
return -1;
}
if (mndCreateDefaultUser(pMnode, TSDB_DEFAULT_USER, "_" TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS) != 0) {
return -1;
}
return 0;
}
2021-11-29 05:19:00 +00:00
static SSdbRaw *mndUserActionEncode(SUserObj *pUser) {
2021-11-30 07:28:51 +00:00
SSdbRaw *pRaw = sdbAllocRaw(SDB_USER, SDB_USER_VER, sizeof(SUserObj));
2021-11-12 07:06:58 +00:00
if (pRaw == NULL) return NULL;
int32_t dataPos = 0;
SDB_SET_BINARY(pRaw, dataPos, pUser->user, TSDB_USER_LEN)
SDB_SET_BINARY(pRaw, dataPos, pUser->pass, TSDB_KEY_LEN)
2021-11-12 13:35:29 +00:00
SDB_SET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN)
2021-11-12 07:06:58 +00:00
SDB_SET_INT64(pRaw, dataPos, pUser->createdTime)
SDB_SET_INT64(pRaw, dataPos, pUser->updateTime)
2021-12-03 06:36:41 +00:00
SDB_SET_INT8(pRaw, dataPos, pUser->superAuth)
2021-12-05 10:37:54 +00:00
SDB_SET_INT8(pRaw, dataPos, pUser->readAuth)
SDB_SET_INT8(pRaw, dataPos, pUser->writeAuth)
2021-11-12 07:06:58 +00:00
SDB_SET_DATALEN(pRaw, dataPos);
2021-11-09 03:37:58 +00:00
return pRaw;
2021-10-18 06:00:35 +00:00
}
2021-11-29 05:19:00 +00:00
static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw) {
2021-11-12 07:06:58 +00:00
int8_t sver = 0;
if (sdbGetRawSoftVer(pRaw, &sver) != 0) return NULL;
2021-10-18 06:00:35 +00:00
2021-11-12 07:06:58 +00:00
if (sver != SDB_USER_VER) {
2021-12-01 09:27:31 +00:00
mError("failed to decode user since %s", terrstr());
2021-11-12 07:06:58 +00:00
terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
2021-11-09 03:37:58 +00:00
return NULL;
}
2021-10-18 06:00:35 +00:00
2021-11-12 13:35:29 +00:00
SSdbRow *pRow = sdbAllocRow(sizeof(SUserObj));
2021-11-12 07:06:58 +00:00
SUserObj *pUser = sdbGetRowObj(pRow);
if (pUser == NULL) return NULL;
2021-10-18 06:00:35 +00:00
2021-11-12 07:06:58 +00:00
int32_t dataPos = 0;
SDB_GET_BINARY(pRaw, pRow, dataPos, pUser->user, TSDB_USER_LEN)
SDB_GET_BINARY(pRaw, pRow, dataPos, pUser->pass, TSDB_KEY_LEN)
SDB_GET_BINARY(pRaw, pRow, dataPos, pUser->acct, TSDB_USER_LEN)
SDB_GET_INT64(pRaw, pRow, dataPos, &pUser->createdTime)
SDB_GET_INT64(pRaw, pRow, dataPos, &pUser->updateTime)
2021-12-03 06:36:41 +00:00
SDB_GET_INT8(pRaw, pRow, dataPos, &pUser->superAuth)
2021-12-05 10:37:54 +00:00
SDB_GET_INT8(pRaw, pRow, dataPos, &pUser->readAuth)
SDB_GET_INT8(pRaw, pRow, dataPos, &pUser->writeAuth)
2021-10-18 06:00:35 +00:00
2021-11-12 07:06:58 +00:00
return pRow;
2021-11-09 03:37:58 +00:00
}
2021-10-18 06:00:35 +00:00
2021-11-29 07:53:02 +00:00
static int32_t mndUserActionInsert(SSdb *pSdb, SUserObj *pUser) {
2021-12-01 09:27:31 +00:00
mTrace("user:%s, perform insert action", pUser->user);
2021-11-09 03:37:58 +00:00
pUser->prohibitDbHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
if (pUser->prohibitDbHash == NULL) {
2021-11-11 09:31:52 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY;
2021-12-01 09:27:31 +00:00
mError("user:%s, failed to perform insert action since %s", pUser->user, terrstr());
2021-11-11 03:11:14 +00:00
return -1;
2021-10-18 06:00:35 +00:00
}
2021-12-03 06:36:41 +00:00
SAcctObj *pAcct = sdbAcquire(pSdb, SDB_ACCT, pUser->acct);
if (pAcct == NULL) {
2021-11-11 03:11:14 +00:00
terrno = TSDB_CODE_MND_ACCT_NOT_EXIST;
2021-12-01 09:27:31 +00:00
mError("user:%s, failed to perform insert action since %s", pUser->user, terrstr());
2021-11-11 03:11:14 +00:00
return -1;
2021-10-18 06:00:35 +00:00
}
2021-12-03 06:36:41 +00:00
pUser->acctId = pAcct->acctId;
sdbRelease(pSdb, pAcct);
2021-10-18 06:00:35 +00:00
2021-11-09 03:37:58 +00:00
return 0;
}
2021-10-18 06:00:35 +00:00
2021-11-29 07:53:02 +00:00
static int32_t mndUserActionDelete(SSdb *pSdb, SUserObj *pUser) {
2021-12-01 09:27:31 +00:00
mTrace("user:%s, perform delete action", pUser->user);
2021-11-09 03:37:58 +00:00
if (pUser->prohibitDbHash) {
taosHashCleanup(pUser->prohibitDbHash);
pUser->prohibitDbHash = NULL;
2021-10-18 06:00:35 +00:00
}
2021-11-09 03:37:58 +00:00
return 0;
}
2021-11-29 07:53:02 +00:00
static int32_t mndUserActionUpdate(SSdb *pSdb, SUserObj *pSrcUser, SUserObj *pDstUser) {
2021-12-01 09:27:31 +00:00
mTrace("user:%s, perform update action", pSrcUser->user);
memcpy(pSrcUser->user, pDstUser->user, TSDB_USER_LEN);
memcpy(pSrcUser->pass, pDstUser->pass, TSDB_KEY_LEN);
memcpy(pSrcUser->acct, pDstUser->acct, TSDB_USER_LEN);
pSrcUser->createdTime = pDstUser->createdTime;
pSrcUser->updateTime = pDstUser->updateTime;
2021-12-03 06:36:41 +00:00
pSrcUser->superAuth = pDstUser->superAuth;
2021-12-05 10:37:54 +00:00
pSrcUser->readAuth = pDstUser->readAuth;
pSrcUser->writeAuth = pDstUser->writeAuth;
2021-11-09 03:37:58 +00:00
return 0;
}
2021-12-04 02:23:39 +00:00
SUserObj *mndAcquireUser(SMnode *pMnode, char *userName) {
2021-12-03 12:52:44 +00:00
SSdb *pSdb = pMnode->pSdb;
2021-12-04 02:23:39 +00:00
return sdbAcquire(pSdb, SDB_USER, userName);
2021-11-09 03:37:58 +00:00
}
2021-10-18 06:00:35 +00:00
2021-12-03 12:52:44 +00:00
void mndReleaseUser(SMnode *pMnode, SUserObj *pUser) {
SSdb *pSdb = pMnode->pSdb;
sdbRelease(pSdb, pUser);
2021-10-18 06:00:35 +00:00
}
2021-11-29 07:53:02 +00:00
static int32_t mndCreateUser(SMnode *pMnode, char *acct, char *user, char *pass, SMnodeMsg *pMsg) {
2021-11-09 06:27:17 +00:00
SUserObj userObj = {0};
tstrncpy(userObj.user, user, TSDB_USER_LEN);
tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
taosEncryptPass((uint8_t *)pass, strlen(pass), userObj.pass);
userObj.createdTime = taosGetTimestampMs();
userObj.updateTime = userObj.createdTime;
2021-12-03 06:36:41 +00:00
userObj.superAuth = 0;
2021-12-05 10:37:54 +00:00
userObj.readAuth = 1;
userObj.writeAuth = 1;
2021-11-09 06:27:17 +00:00
2021-11-29 08:02:37 +00:00
STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, pMsg->rpcMsg.handle);
2021-11-11 03:11:14 +00:00
if (pTrans == NULL) return -1;
2021-11-29 05:19:00 +00:00
SSdbRaw *pRedoRaw = mndUserActionEncode(&userObj);
2021-11-29 08:02:37 +00:00
if (pRedoRaw == NULL || mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
2021-11-19 06:46:06 +00:00
mError("failed to append redo log since %s", terrstr());
2021-11-29 08:02:37 +00:00
mndTransDrop(pTrans);
2021-11-11 03:11:14 +00:00
return -1;
2021-11-09 06:27:17 +00:00
}
2021-11-12 07:06:58 +00:00
sdbSetRawStatus(pRedoRaw, SDB_STATUS_CREATING);
2021-11-09 06:27:17 +00:00
2021-11-29 05:19:00 +00:00
SSdbRaw *pUndoRaw = mndUserActionEncode(&userObj);
2021-11-29 08:02:37 +00:00
if (pUndoRaw == NULL || mndTransAppendUndolog(pTrans, pUndoRaw) != 0) {
2021-11-19 06:46:06 +00:00
mError("failed to append undo log since %s", terrstr());
2021-11-29 08:02:37 +00:00
mndTransDrop(pTrans);
2021-11-11 03:11:14 +00:00
return -1;
2021-11-09 06:27:17 +00:00
}
2021-11-12 11:11:35 +00:00
sdbSetRawStatus(pUndoRaw, SDB_STATUS_DROPPED);
2021-11-09 06:27:17 +00:00
2021-11-29 05:19:00 +00:00
SSdbRaw *pCommitRaw = mndUserActionEncode(&userObj);
2021-11-29 08:02:37 +00:00
if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
2021-11-19 06:46:06 +00:00
mError("failed to append commit log since %s", terrstr());
2021-11-29 08:02:37 +00:00
mndTransDrop(pTrans);
2021-11-11 03:11:14 +00:00
return -1;
2021-11-09 06:27:17 +00:00
}
2021-11-12 07:06:58 +00:00
sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
2021-11-09 06:27:17 +00:00
2021-11-29 08:02:37 +00:00
if (mndTransPrepare(pTrans, mndSyncPropose) != 0) {
mndTransDrop(pTrans);
2021-11-11 03:11:14 +00:00
return -1;
2021-11-09 06:27:17 +00:00
}
2021-11-29 08:02:37 +00:00
mndTransDrop(pTrans);
2021-11-11 03:11:14 +00:00
return 0;
2021-11-09 06:27:17 +00:00
}
2021-12-03 07:01:26 +00:00
static int32_t mndProcessCreateUserMsg(SMnodeMsg *pMsg) {
SMnode *pMnode = pMsg->pMnode;
2021-11-09 06:27:17 +00:00
SCreateUserMsg *pCreate = pMsg->rpcMsg.pCont;
if (pCreate->user[0] == 0) {
2021-11-11 03:11:14 +00:00
terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
mError("user:%s, failed to create since %s", pCreate->user, terrstr());
return -1;
2021-11-09 06:27:17 +00:00
}
if (pCreate->pass[0] == 0) {
2021-11-11 03:11:14 +00:00
terrno = TSDB_CODE_MND_INVALID_PASS_FORMAT;
mError("user:%s, failed to create since %s", pCreate->user, terrstr());
return -1;
2021-11-09 06:27:17 +00:00
}
2021-11-29 07:53:02 +00:00
SUserObj *pUser = sdbAcquire(pMnode->pSdb, SDB_USER, pCreate->user);
2021-11-09 06:27:17 +00:00
if (pUser != NULL) {
2021-11-29 07:53:02 +00:00
sdbRelease(pMnode->pSdb, pUser);
2021-11-11 03:11:14 +00:00
terrno = TSDB_CODE_MND_USER_ALREADY_EXIST;
mError("user:%s, failed to create since %s", pCreate->user, terrstr());
return -1;
2021-11-09 06:27:17 +00:00
}
2021-12-01 09:27:31 +00:00
SUserObj *pOperUser = sdbAcquire(pMnode->pSdb, SDB_USER, pMsg->user);
2021-11-09 06:27:17 +00:00
if (pOperUser == NULL) {
2021-11-11 03:11:14 +00:00
terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
mError("user:%s, failed to create since %s", pCreate->user, terrstr());
return -1;
2021-11-09 06:27:17 +00:00
}
2021-11-29 07:53:02 +00:00
int32_t code = mndCreateUser(pMnode, pOperUser->acct, pCreate->user, pCreate->pass, pMsg);
sdbRelease(pMnode->pSdb, pOperUser);
2021-11-09 06:27:17 +00:00
if (code != 0) {
2021-11-11 03:11:14 +00:00
mError("user:%s, failed to create since %s", pCreate->user, terrstr());
return -1;
2021-11-09 06:27:17 +00:00
}
return TSDB_CODE_MND_ACTION_IN_PROGRESS;
}
2021-12-03 12:52:44 +00:00
static int32_t mndProcessAlterUserMsg(SMnodeMsg *pMsg) {
terrno = TSDB_CODE_MND_MSG_NOT_PROCESSED;
mError("failed to process alter user msg since %s", terrstr());
return -1;
2021-12-01 09:27:31 +00:00
}
2021-12-03 12:52:44 +00:00
static int32_t mndProcessDropUserMsg(SMnodeMsg *pMsg) {
terrno = TSDB_CODE_MND_MSG_NOT_PROCESSED;
mError("failed to process drop user msg since %s", terrstr());
return -1;
}