mndUser.c 9.3 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

S
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17 18
#include "mndUser.h"
#include "mndShow.h"
S
Shengliang Guan 已提交
19 20
#include "mndSync.h"
#include "mndTrans.h"
S
Shengliang Guan 已提交
21
#include "tkey.h"
S
Shengliang Guan 已提交
22

S
Shengliang Guan 已提交
23
#define SDB_USER_VER 1
S
Shengliang Guan 已提交
24

S
Shengliang Guan 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
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;

  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;
}

S
Shengliang Guan 已提交
87
static SSdbRaw *mndUserActionEncode(SUserObj *pUser) {
S
Shengliang Guan 已提交
88
  SSdbRaw *pRaw = sdbAllocRaw(SDB_USER, SDB_USER_VER, sizeof(SUserObj));
S
Shengliang Guan 已提交
89 90 91 92 93
  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)
S
Shengliang Guan 已提交
94
  SDB_SET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN)
S
Shengliang Guan 已提交
95 96
  SDB_SET_INT64(pRaw, dataPos, pUser->createdTime)
  SDB_SET_INT64(pRaw, dataPos, pUser->updateTime)
S
Shengliang Guan 已提交
97
  SDB_SET_INT8(pRaw, dataPos, pUser->superAuth)
S
Shengliang Guan 已提交
98
  SDB_SET_DATALEN(pRaw, dataPos);
S
Shengliang Guan 已提交
99 100

  return pRaw;
S
Shengliang Guan 已提交
101 102
}

S
Shengliang Guan 已提交
103
static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw) {
S
Shengliang Guan 已提交
104 105
  int8_t sver = 0;
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) return NULL;
S
Shengliang Guan 已提交
106

S
Shengliang Guan 已提交
107
  if (sver != SDB_USER_VER) {
S
Shengliang Guan 已提交
108
    mError("failed to decode user since %s", terrstr());
S
Shengliang Guan 已提交
109
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
S
Shengliang Guan 已提交
110 111
    return NULL;
  }
S
Shengliang Guan 已提交
112

S
Shengliang Guan 已提交
113
  SSdbRow  *pRow = sdbAllocRow(sizeof(SUserObj));
S
Shengliang Guan 已提交
114 115
  SUserObj *pUser = sdbGetRowObj(pRow);
  if (pUser == NULL) return NULL;
S
Shengliang Guan 已提交
116

S
Shengliang Guan 已提交
117 118 119 120 121 122
  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)
S
Shengliang Guan 已提交
123
  SDB_GET_INT8(pRaw, pRow, dataPos, &pUser->superAuth)
S
Shengliang Guan 已提交
124

S
Shengliang Guan 已提交
125
  return pRow;
S
Shengliang Guan 已提交
126
}
S
Shengliang Guan 已提交
127

S
Shengliang Guan 已提交
128
static int32_t mndUserActionInsert(SSdb *pSdb, SUserObj *pUser) {
S
Shengliang Guan 已提交
129
  mTrace("user:%s, perform insert action", pUser->user);
S
Shengliang Guan 已提交
130 131
  pUser->prohibitDbHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
  if (pUser->prohibitDbHash == NULL) {
S
Shengliang Guan 已提交
132
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
133
    mError("user:%s, failed to perform insert action since %s", pUser->user, terrstr());
S
Shengliang Guan 已提交
134
    return -1;
S
Shengliang Guan 已提交
135 136
  }

S
Shengliang Guan 已提交
137 138
  SAcctObj *pAcct = sdbAcquire(pSdb, SDB_ACCT, pUser->acct);
  if (pAcct == NULL) {
S
Shengliang Guan 已提交
139
    terrno = TSDB_CODE_MND_ACCT_NOT_EXIST;
S
Shengliang Guan 已提交
140
    mError("user:%s, failed to perform insert action since %s", pUser->user, terrstr());
S
Shengliang Guan 已提交
141
    return -1;
S
Shengliang Guan 已提交
142
  }
S
Shengliang Guan 已提交
143 144
  pUser->acctId = pAcct->acctId;
  sdbRelease(pSdb, pAcct);
S
Shengliang Guan 已提交
145

S
Shengliang Guan 已提交
146 147
  return 0;
}
S
Shengliang Guan 已提交
148

S
Shengliang Guan 已提交
149
static int32_t mndUserActionDelete(SSdb *pSdb, SUserObj *pUser) {
S
Shengliang Guan 已提交
150
  mTrace("user:%s, perform delete action", pUser->user);
S
Shengliang Guan 已提交
151 152 153
  if (pUser->prohibitDbHash) {
    taosHashCleanup(pUser->prohibitDbHash);
    pUser->prohibitDbHash = NULL;
S
Shengliang Guan 已提交
154 155
  }

S
Shengliang Guan 已提交
156 157 158
  return 0;
}

S
Shengliang Guan 已提交
159
static int32_t mndUserActionUpdate(SSdb *pSdb, SUserObj *pSrcUser, SUserObj *pDstUser) {
S
Shengliang Guan 已提交
160 161 162 163 164 165
  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;
S
Shengliang Guan 已提交
166
  pSrcUser->superAuth = pDstUser->superAuth;
S
Shengliang Guan 已提交
167 168 169
  return 0;
}

S
Shengliang Guan 已提交
170 171 172
SUserObj *mndAcquireUser(SMnode *pMnode, const char *userName) {
  SSdb *pSdb = pMnode->pSdb;
  return sdbAcquire(pSdb, SDB_USER, &userName);
S
Shengliang Guan 已提交
173
}
S
Shengliang Guan 已提交
174

S
Shengliang Guan 已提交
175 176 177
void mndReleaseUser(SMnode *pMnode, SUserObj *pUser) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pUser);
S
Shengliang Guan 已提交
178 179
}

S
Shengliang Guan 已提交
180
static int32_t mndCreateUser(SMnode *pMnode, char *acct, char *user, char *pass, SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
181 182 183 184 185 186
  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;
S
Shengliang Guan 已提交
187
  userObj.superAuth = 0;
S
Shengliang Guan 已提交
188

S
Shengliang Guan 已提交
189
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, pMsg->rpcMsg.handle);
S
Shengliang Guan 已提交
190 191
  if (pTrans == NULL) return -1;

S
Shengliang Guan 已提交
192
  SSdbRaw *pRedoRaw = mndUserActionEncode(&userObj);
S
Shengliang Guan 已提交
193
  if (pRedoRaw == NULL || mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
S
Shengliang Guan 已提交
194
    mError("failed to append redo log since %s", terrstr());
S
Shengliang Guan 已提交
195
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
196
    return -1;
S
Shengliang Guan 已提交
197
  }
S
Shengliang Guan 已提交
198
  sdbSetRawStatus(pRedoRaw, SDB_STATUS_CREATING);
S
Shengliang Guan 已提交
199

S
Shengliang Guan 已提交
200
  SSdbRaw *pUndoRaw = mndUserActionEncode(&userObj);
S
Shengliang Guan 已提交
201
  if (pUndoRaw == NULL || mndTransAppendUndolog(pTrans, pUndoRaw) != 0) {
S
Shengliang Guan 已提交
202
    mError("failed to append undo log since %s", terrstr());
S
Shengliang Guan 已提交
203
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
204
    return -1;
S
Shengliang Guan 已提交
205
  }
S
Shengliang Guan 已提交
206
  sdbSetRawStatus(pUndoRaw, SDB_STATUS_DROPPED);
S
Shengliang Guan 已提交
207

S
Shengliang Guan 已提交
208
  SSdbRaw *pCommitRaw = mndUserActionEncode(&userObj);
S
Shengliang Guan 已提交
209
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
S
Shengliang Guan 已提交
210
    mError("failed to append commit log since %s", terrstr());
S
Shengliang Guan 已提交
211
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
212
    return -1;
S
Shengliang Guan 已提交
213
  }
S
Shengliang Guan 已提交
214
  sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
215

S
Shengliang Guan 已提交
216 217
  if (mndTransPrepare(pTrans, mndSyncPropose) != 0) {
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
218
    return -1;
S
Shengliang Guan 已提交
219 220
  }

S
Shengliang Guan 已提交
221
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
222
  return 0;
S
Shengliang Guan 已提交
223 224
}

225 226
static int32_t mndProcessCreateUserMsg(SMnodeMsg *pMsg) {
  SMnode         *pMnode = pMsg->pMnode;
S
Shengliang Guan 已提交
227 228 229
  SCreateUserMsg *pCreate = pMsg->rpcMsg.pCont;

  if (pCreate->user[0] == 0) {
S
Shengliang Guan 已提交
230 231 232
    terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
233 234 235
  }

  if (pCreate->pass[0] == 0) {
S
Shengliang Guan 已提交
236 237 238
    terrno = TSDB_CODE_MND_INVALID_PASS_FORMAT;
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
239 240
  }

S
Shengliang Guan 已提交
241
  SUserObj *pUser = sdbAcquire(pMnode->pSdb, SDB_USER, pCreate->user);
S
Shengliang Guan 已提交
242
  if (pUser != NULL) {
S
Shengliang Guan 已提交
243
    sdbRelease(pMnode->pSdb, pUser);
S
Shengliang Guan 已提交
244 245 246
    terrno = TSDB_CODE_MND_USER_ALREADY_EXIST;
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
247 248
  }

S
Shengliang Guan 已提交
249
  SUserObj *pOperUser = sdbAcquire(pMnode->pSdb, SDB_USER, pMsg->user);
S
Shengliang Guan 已提交
250
  if (pOperUser == NULL) {
S
Shengliang Guan 已提交
251 252 253
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
254 255
  }

S
Shengliang Guan 已提交
256 257
  int32_t code = mndCreateUser(pMnode, pOperUser->acct, pCreate->user, pCreate->pass, pMsg);
  sdbRelease(pMnode->pSdb, pOperUser);
S
Shengliang Guan 已提交
258 259

  if (code != 0) {
S
Shengliang Guan 已提交
260 261
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
262 263 264 265 266
  }

  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
}

S
Shengliang Guan 已提交
267 268 269 270
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;
S
Shengliang Guan 已提交
271 272
}

S
Shengliang Guan 已提交
273 274 275 276 277
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;
}