mndUser.c 8.2 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 "mndSync.h"
#include "mndTrans.h"
S
Shengliang Guan 已提交
19
#include "tkey.h"
S
Shengliang Guan 已提交
20

S
Shengliang Guan 已提交
21
#define SDB_USER_VER 1
S
Shengliang Guan 已提交
22

S
Shengliang Guan 已提交
23
static SSdbRaw *mndUserActionEncode(SUserObj *pUser) {
S
Shengliang Guan 已提交
24
  SSdbRaw *pRaw = sdbAllocRaw(SDB_USER, SDB_USER_VER, sizeof(SUserObj));
S
Shengliang Guan 已提交
25 26 27 28 29
  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 已提交
30
  SDB_SET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN)
S
Shengliang Guan 已提交
31 32 33 34
  SDB_SET_INT64(pRaw, dataPos, pUser->createdTime)
  SDB_SET_INT64(pRaw, dataPos, pUser->updateTime)
  SDB_SET_INT8(pRaw, dataPos, pUser->rootAuth)
  SDB_SET_DATALEN(pRaw, dataPos);
S
Shengliang Guan 已提交
35 36

  return pRaw;
S
Shengliang Guan 已提交
37 38
}

S
Shengliang Guan 已提交
39
static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw) {
S
Shengliang Guan 已提交
40 41
  int8_t sver = 0;
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) return NULL;
S
Shengliang Guan 已提交
42

S
Shengliang Guan 已提交
43
  if (sver != SDB_USER_VER) {
S
Shengliang Guan 已提交
44
    mError("failed to decode user since %s", terrstr());
S
Shengliang Guan 已提交
45
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
S
Shengliang Guan 已提交
46 47
    return NULL;
  }
S
Shengliang Guan 已提交
48

S
Shengliang Guan 已提交
49
  SSdbRow  *pRow = sdbAllocRow(sizeof(SUserObj));
S
Shengliang Guan 已提交
50 51
  SUserObj *pUser = sdbGetRowObj(pRow);
  if (pUser == NULL) return NULL;
S
Shengliang Guan 已提交
52

S
Shengliang Guan 已提交
53 54 55 56 57 58 59
  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)
  SDB_GET_INT8(pRaw, pRow, dataPos, &pUser->rootAuth)
S
Shengliang Guan 已提交
60

S
Shengliang Guan 已提交
61
  return pRow;
S
Shengliang Guan 已提交
62
}
S
Shengliang Guan 已提交
63

S
Shengliang Guan 已提交
64
static int32_t mndUserActionInsert(SSdb *pSdb, SUserObj *pUser) {
S
Shengliang Guan 已提交
65
  mTrace("user:%s, perform insert action", pUser->user);
S
Shengliang Guan 已提交
66 67
  pUser->prohibitDbHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
  if (pUser->prohibitDbHash == NULL) {
S
Shengliang Guan 已提交
68
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
69
    mError("user:%s, failed to perform insert action since %s", pUser->user, terrstr());
S
Shengliang Guan 已提交
70
    return -1;
S
Shengliang Guan 已提交
71 72
  }

S
Shengliang Guan 已提交
73
  pUser->pAcct = sdbAcquire(pSdb, SDB_ACCT, pUser->acct);
S
Shengliang Guan 已提交
74
  if (pUser->pAcct == NULL) {
S
Shengliang Guan 已提交
75
    terrno = TSDB_CODE_MND_ACCT_NOT_EXIST;
S
Shengliang Guan 已提交
76
    mError("user:%s, failed to perform insert action since %s", pUser->user, terrstr());
S
Shengliang Guan 已提交
77
    return -1;
S
Shengliang Guan 已提交
78 79
  }

S
Shengliang Guan 已提交
80 81
  return 0;
}
S
Shengliang Guan 已提交
82

S
Shengliang Guan 已提交
83
static int32_t mndUserActionDelete(SSdb *pSdb, SUserObj *pUser) {
S
Shengliang Guan 已提交
84
  mTrace("user:%s, perform delete action", pUser->user);
S
Shengliang Guan 已提交
85 86 87
  if (pUser->prohibitDbHash) {
    taosHashCleanup(pUser->prohibitDbHash);
    pUser->prohibitDbHash = NULL;
S
Shengliang Guan 已提交
88 89
  }

S
Shengliang Guan 已提交
90
  if (pUser->pAcct != NULL) {
S
Shengliang Guan 已提交
91
    sdbRelease(pSdb, pUser->pAcct);
S
Shengliang Guan 已提交
92
    pUser->pAcct = NULL;
S
Shengliang Guan 已提交
93 94
  }

S
Shengliang Guan 已提交
95 96 97
  return 0;
}

S
Shengliang Guan 已提交
98
static int32_t mndUserActionUpdate(SSdb *pSdb, SUserObj *pSrcUser, SUserObj *pDstUser) {
S
Shengliang Guan 已提交
99 100 101 102 103 104 105
  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;
  pSrcUser->rootAuth = pDstUser->rootAuth;
S
Shengliang Guan 已提交
106 107 108
  return 0;
}

S
Shengliang Guan 已提交
109
static int32_t mndCreateDefaultUser(SMnode *pMnode, char *acct, char *user, char *pass) {
S
Shengliang Guan 已提交
110 111 112 113 114
  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();
S
Shengliang Guan 已提交
115
  userObj.updateTime = userObj.createdTime;
S
Shengliang Guan 已提交
116 117 118

  if (strcmp(user, TSDB_DEFAULT_USER) == 0) {
    userObj.rootAuth = 1;
S
Shengliang Guan 已提交
119 120
  }

S
Shengliang Guan 已提交
121
  SSdbRaw *pRaw = mndUserActionEncode(&userObj);
S
Shengliang Guan 已提交
122
  if (pRaw == NULL) return -1;
S
Shengliang Guan 已提交
123
  sdbSetRawStatus(pRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
124

S
Shengliang Guan 已提交
125
  mTrace("user:%s, will be created while deploy sdb", userObj.user);
S
Shengliang Guan 已提交
126
  return sdbWrite(pMnode->pSdb, pRaw);
S
Shengliang Guan 已提交
127
}
S
Shengliang Guan 已提交
128

S
Shengliang Guan 已提交
129 130
static int32_t mndCreateDefaultUsers(SMnode *pMnode) {
  if (mndCreateDefaultUser(pMnode, TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS) != 0) {
S
Shengliang Guan 已提交
131 132
    return -1;
  }
S
Shengliang Guan 已提交
133

S
Shengliang Guan 已提交
134
  if (mndCreateDefaultUser(pMnode, TSDB_DEFAULT_USER, "_" TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS) != 0) {
S
Shengliang Guan 已提交
135 136
    return -1;
  }
S
Shengliang Guan 已提交
137

S
Shengliang Guan 已提交
138
  return 0;
S
Shengliang Guan 已提交
139 140
}

S
Shengliang Guan 已提交
141
static int32_t mndCreateUser(SMnode *pMnode, char *acct, char *user, char *pass, SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
142 143 144 145 146 147 148 149
  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;
  userObj.rootAuth = 0;

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

S
Shengliang Guan 已提交
153
  SSdbRaw *pRedoRaw = mndUserActionEncode(&userObj);
S
Shengliang Guan 已提交
154
  if (pRedoRaw == NULL || mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
S
Shengliang Guan 已提交
155
    mError("failed to append redo log since %s", terrstr());
S
Shengliang Guan 已提交
156
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
157
    return -1;
S
Shengliang Guan 已提交
158
  }
S
Shengliang Guan 已提交
159
  sdbSetRawStatus(pRedoRaw, SDB_STATUS_CREATING);
S
Shengliang Guan 已提交
160

S
Shengliang Guan 已提交
161
  SSdbRaw *pUndoRaw = mndUserActionEncode(&userObj);
S
Shengliang Guan 已提交
162
  if (pUndoRaw == NULL || mndTransAppendUndolog(pTrans, pUndoRaw) != 0) {
S
Shengliang Guan 已提交
163
    mError("failed to append undo log since %s", terrstr());
S
Shengliang Guan 已提交
164
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
165
    return -1;
S
Shengliang Guan 已提交
166
  }
S
Shengliang Guan 已提交
167
  sdbSetRawStatus(pUndoRaw, SDB_STATUS_DROPPED);
S
Shengliang Guan 已提交
168

S
Shengliang Guan 已提交
169
  SSdbRaw *pCommitRaw = mndUserActionEncode(&userObj);
S
Shengliang Guan 已提交
170
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
S
Shengliang Guan 已提交
171
    mError("failed to append commit log since %s", terrstr());
S
Shengliang Guan 已提交
172
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
173
    return -1;
S
Shengliang Guan 已提交
174
  }
S
Shengliang Guan 已提交
175
  sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
176

S
Shengliang Guan 已提交
177 178
  if (mndTransPrepare(pTrans, mndSyncPropose) != 0) {
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
179
    return -1;
S
Shengliang Guan 已提交
180 181
  }

S
Shengliang Guan 已提交
182
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
183
  return 0;
S
Shengliang Guan 已提交
184 185
}

S
Shengliang Guan 已提交
186
static int32_t mndProcessCreateUserMsg(SMnode *pMnode, SMnodeMsg *pMsg) {
S
Shengliang Guan 已提交
187 188 189
  SCreateUserMsg *pCreate = pMsg->rpcMsg.pCont;

  if (pCreate->user[0] == 0) {
S
Shengliang Guan 已提交
190 191 192
    terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
193 194 195
  }

  if (pCreate->pass[0] == 0) {
S
Shengliang Guan 已提交
196 197 198
    terrno = TSDB_CODE_MND_INVALID_PASS_FORMAT;
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
199 200
  }

S
Shengliang Guan 已提交
201
  SUserObj *pUser = sdbAcquire(pMnode->pSdb, SDB_USER, pCreate->user);
S
Shengliang Guan 已提交
202
  if (pUser != NULL) {
S
Shengliang Guan 已提交
203
    sdbRelease(pMnode->pSdb, pUser);
S
Shengliang Guan 已提交
204 205 206
    terrno = TSDB_CODE_MND_USER_ALREADY_EXIST;
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
207 208
  }

S
Shengliang Guan 已提交
209
  SUserObj *pOperUser = sdbAcquire(pMnode->pSdb, SDB_USER, pMsg->user);
S
Shengliang Guan 已提交
210
  if (pOperUser == NULL) {
S
Shengliang Guan 已提交
211 212 213
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
214 215
  }

S
Shengliang Guan 已提交
216 217
  int32_t code = mndCreateUser(pMnode, pOperUser->acct, pCreate->user, pCreate->pass, pMsg);
  sdbRelease(pMnode->pSdb, pOperUser);
S
Shengliang Guan 已提交
218 219

  if (code != 0) {
S
Shengliang Guan 已提交
220 221
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
    return -1;
S
Shengliang Guan 已提交
222 223 224 225 226
  }

  return TSDB_CODE_MND_ACTION_IN_PROGRESS;
}

S
Shengliang Guan 已提交
227
int32_t mndInitUser(SMnode *pMnode) {
S
Shengliang Guan 已提交
228 229
  SSdbTable table = {.sdbType = SDB_USER,
                     .keyType = SDB_KEY_BINARY,
S
Shengliang Guan 已提交
230 231 232 233 234 235
                     .deployFp = (SdbDeployFp)mndCreateDefaultUsers,
                     .encodeFp = (SdbEncodeFp)mndUserActionEncode,
                     .decodeFp = (SdbDecodeFp)mndUserActionDecode,
                     .insertFp = (SdbInsertFp)mndUserActionInsert,
                     .updateFp = (SdbUpdateFp)mndUserActionUpdate,
                     .deleteFp = (SdbDeleteFp)mndUserActionDelete};
S
Shengliang Guan 已提交
236

S
Shengliang Guan 已提交
237
  mndSetMsgHandle(pMnode, TSDB_MSG_TYPE_CREATE_USER, mndProcessCreateUserMsg);
S
Shengliang Guan 已提交
238

S
Shengliang Guan 已提交
239
  return sdbSetTable(pMnode->pSdb, table);
S
Shengliang Guan 已提交
240 241
}

S
Shengliang Guan 已提交
242 243 244 245 246 247 248 249 250 251 252 253
void mndCleanupUser(SMnode *pMnode) {}

SUserObj *mndAcquireUser(SMnode *pMnode, const char *userName) {
  SSdb *pSdb = pMnode->pSdb;
  return sdbAcquire(pSdb, SDB_USER, &userName);
}

void mndReleaseUser(SMnode *pMnode, SUserObj *pUser) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pUser);
}