Skip to content

Commit 56bea7d

Browse files
authored
feat: support create/drop totp_secret statement (#34162)
1 parent 25c6c14 commit 56bea7d

File tree

28 files changed

+729
-29
lines changed

28 files changed

+729
-29
lines changed

docs/en/14-reference/03-taos-sql/60-user.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ CREATE USER user_name PASS 'password'
1717
[CONNECT_TIME {value | DEFAULT | UNLIMITED}]
1818
[CONNECT_IDLE_TIME {value | DEFAULT | UNLIMITED}]
1919
[CALL_PER_SESSION {value | DEFAULT | UNLIMITED}]
20+
[VNODE_PER_CALL {value | DEFAULT | UNLIMITED}]
2021
[FAILED_LOGIN_ATTEMPTS {value | DEFAULT | UNLIMITED}]
2122
[PASSWORD_LOCK_TIME {value | DEFAULT | UNLIMITED}]
2223
[PASSWORD_LIFE_TIME {value | DEFAULT | UNLIMITED}]
@@ -47,6 +48,7 @@ alter all dnodes 'EnableStrongPassword' '0'
4748
- `CONNECT_TIME` The maximum allowed duration for a single session in minutes. The default value is `480`, with a minimum of `1`, set to `UNLIMITED` disables the restriction. Support in Enterprise Edition v3.4.0.0 and above.
4849
- `CONNECT_IDLE_TIME` The maximum allowed idle duration for a single session in minutes. The default value is `30`, with a minimum of `1`, set to `UNLIMITED` disables the restriction. Support in Enterprise Edition v3.4.0.0 and above.
4950
- `CALL_PER_SESSION` The maximum allowed number of sub-calls per session. The default value is `10`, with a minimum of `1`, set to `UNLIMITED` disables the restriction. Support in Enterprise Edition v3.4.0.0 and above.
51+
- `VNODE_PER_CALL` The maximum number of vnodes that a single call can involve. The default value is `-1`, which means unlimited. Support in Enterprise Edition v3.4.0.0 and above.
5052
- `FAILED_LOGIN_ATTEMPTS` The number of allowed consecutive failed login attempts; the user will be locked after exceeding this limit. The default value is `3`, with a minimum of `1`, set to `UNLIMITED` disables the restriction. Support in Enterprise Edition v3.4.0.0 and above.
5153
- `PASSWORD_LOCK_TIME` The unlock waiting time for the user when locked due to failed login attempts, in minutes. The default value is `1440`, with a minimum of `1`, set to `UNLIMITED` means the user is locked for ever. Support in Enterprise Edition v3.4.0.0 and above.
5254
- `PASSWORD_LIFE_TIME` Password validity period, in days. The default value is `90`, with a minimum of `1`, set to `UNLIMITED` means never expire. Support in Enterprise Edition v3.4.0.0 and above.
@@ -116,6 +118,7 @@ alter_user_clause: {
116118
[CONNECT_TIME {value | DEFAULT | UNLIMITED}]
117119
[CONNECT_IDLE_TIME {value | DEFAULT | UNLIMITED}]
118120
[CALL_PER_SESSION {value | DEFAULT | UNLIMITED}]
121+
[VNODE_PER_CALL {value | DEFAULT | UNLIMITED}]
119122
[FAILED_LOGIN_ATTEMPTS {value | DEFAULT | UNLIMITED}]
120123
[PASSWORD_LOCK_TIME {value | DEFAULT | UNLIMITED}]
121124
[PASSWORD_LIFE_TIME {value | DEFAULT | UNLIMITED}]
@@ -142,6 +145,43 @@ taos> alter user test enable 0;
142145
Query OK, 0 of 0 rows affected (0.001160s)
143146
```
144147

148+
## TOTP Two-Factor Authentication
149+
150+
TOTP Two-Factor Authentication is a feature of TDengine TSDB Enterprise Edition, support in version v3.4.0.1 and above.
151+
152+
### Create/Update TOTP secret
153+
154+
```sql
155+
CREATE TOTP_SECRET FOR USER user_name
156+
```
157+
158+
If the user has not yet created a TOTP secret, this command will create a TOTP secret for the user. If the user has already created a TOTP secret, this command will update the secret for the user. In either case, this command will return the newly created secret, which will only be displayed once, please save it promptly. The system will automatically enable TOTP two-factor authentication for users who have a TOTP secret.
159+
160+
For example, we can use the following command to create a TOTP secret for user test.
161+
162+
```sql
163+
taos> create totp_secret for user test;
164+
totp_secret |
165+
=======================================================
166+
ERIRPLZL4ZBFTPT5BNXMVFPR4Z3PTHUWTBTCNZPOHYPYQGTD25XA |
167+
Query OK, 1 row(s) in set (0.002314s)
168+
```
169+
170+
### Drop TOTP Secret
171+
172+
```sql
173+
DROP TOTP_SECRET FROM USER user_name
174+
```
175+
176+
This command drops the TOTP secret from the user. After the secret is dropped, the user's TOTP two‑factor authentication will be disabled.
177+
178+
For example, we can use the following command to drop the TOTP key from user test.
179+
180+
```sql
181+
taos> drop totp_secret from user test;
182+
Drop OK, 0 row(s) affected (0.002295s)
183+
```
184+
145185
## Token Management
146186

147187
Token management is a feature of TDengine TSDB Enterprise Edition, support in version v3.4.0.0 and above.

docs/zh/14-reference/03-taos-sql/60-user.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ CREATE USER user_name PASS 'password'
1818
[CONNECT_TIME {value | DEFAULT | UNLIMITED}]
1919
[CONNECT_IDLE_TIME {value | DEFAULT | UNLIMITED}]
2020
[CALL_PER_SESSION {value | DEFAULT | UNLIMITED}]
21+
[VNODE_PER_CALL {value | DEFAULT | UNLIMITED}]
2122
[FAILED_LOGIN_ATTEMPTS {value | DEFAULT | UNLIMITED}]
2223
[PASSWORD_LOCK_TIME {value | DEFAULT | UNLIMITED}]
2324
[PASSWORD_LIFE_TIME {value | DEFAULT | UNLIMITED}]
@@ -48,6 +49,7 @@ alter all dnodes 'EnableStrongPassword' '0'
4849
- `CONNECT_TIME` 限制单次会话最大持续时间,单位为分钟,默认 480,最小 1,设置为 UNLIMITED 则不限制。从企业版 v3.4.0.0 开始支持。
4950
- `CONNECT_IDLE_TIME` 允许的会话最大空闲时间,单位为分钟,默认 30,最小 1,设置为 UNLIMITED 则不限制。从企业版 v3.4.0.0 开始支持。
5051
- `CALL_PER_SESSION` 单会话最大并发子调用数量,默认 10,最小 1,设置为 UNLIMITED 则不限制。从企业版 v3.4.0.0 开始支持。
52+
- `VNODE_PER_CALL` 单调用可以涉及的最大 vnode 数量。默认 -1,代表无限制。从企业版 v3.4.0.0 开始支持。
5153
- `FAILED_LOGIN_ATTEMPTS` 允许的连续失败登录次数,超过次数后账户将被锁定,默认 3,最小 1,设置为 UNLIMITED 则不限制。从企业版 v3.4.0.0 开始支持。
5254
- `PASSWORD_LOCK_TIME` 账户因登录失败被锁定后的解锁等待时间,单位分钟,默认 1440,最小 1,设置为 UNLIMITED 则永久锁定。从企业版 v3.4.0.0 开始支持。
5355
- `PASSWORD_LIFE_TIME` 密码有效期,单位天,默认 90,最小 1,设置为 UNLIMITED 则永不过期。从企业版 v3.4.0.0 开始支持。
@@ -117,6 +119,7 @@ alter_user_clause: {
117119
[CONNECT_TIME {value | DEFAULT | UNLIMITED}]
118120
[CONNECT_IDLE_TIME {value | DEFAULT | UNLIMITED}]
119121
[CALL_PER_SESSION {value | DEFAULT | UNLIMITED}]
122+
[VNODE_PER_CALL {value | DEFAULT | UNLIMITED}]
120123
[FAILED_LOGIN_ATTEMPTS {value | DEFAULT | UNLIMITED}]
121124
[PASSWORD_LOCK_TIME {value | DEFAULT | UNLIMITED}]
122125
[PASSWORD_LIFE_TIME {value | DEFAULT | UNLIMITED}]
@@ -143,6 +146,43 @@ taos> alter user test enable 0;
143146
Query OK, 0 of 0 rows affected (0.001160s)
144147
```
145148

149+
## TOTP 双因认证
150+
151+
TOTP 双因认证是 TDengine TSDB 企业版功能,从企业版 v3.4.0.1 开始支持。
152+
153+
### 创建/更新 TOTP 密钥
154+
155+
```sql
156+
CREATE TOTP_SECRET FOR USER user_name
157+
```
158+
159+
如果用户还未创建 TOTP 密钥,此命令将为该用户创建 TOTP 密钥。如果用户已经创建了 TOTP 密钥,此命令为用户更新该密钥。不论哪种情况,此命令会返回新创建的密钥,此密钥仅展示一次,请及时保存。系统会为创建了 TOTP 密钥的用户自动启用 TOTP 双因认证。
160+
161+
例如,可以使用下面的命令为用户 test 创建 TOTP 密钥。
162+
163+
```sql
164+
taos> create totp_secret for user test;
165+
totp_secret |
166+
=======================================================
167+
ERIRPLZL4ZBFTPT5BNXMVFPR4Z3PTHUWTBTCNZPOHYPYQGTD25XA |
168+
Query OK, 1 row(s) in set (0.002314s)
169+
```
170+
171+
### 删除 TOTP 密钥
172+
173+
```sql
174+
DROP TOTP_SECRET FROM USER user_name
175+
```
176+
177+
此命令删除用户的 TOTP 密钥,密钥删除后,用户的 TOTP 双因认证功能将被禁用。
178+
179+
例如,可以使用下面的命令删除用户 test 的 TOTP 密钥。
180+
181+
```sql
182+
taos> drop totp_secret from user test;
183+
Drop OK, 0 row(s) affected (0.002295s)
184+
```
185+
146186
## 令牌管理
147187

148188
令牌管理是 TDengine TSDB 企业版功能,从企业版 v3.4.0.0 开始支持。

include/common/tmsg.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,8 @@ typedef enum ENodeType {
435435
QUERY_NODE_CREATE_ROLE_STMT,
436436
QUERY_NODE_DROP_ROLE_STMT,
437437
QUERY_NODE_ALTER_ROLE_STMT,
438+
QUERY_NODE_CREATE_TOTP_SECRET_STMT,
439+
QUERY_NODE_DROP_TOTP_SECRET_STMT,
438440

439441
// placeholder for [155, 180]
440442
QUERY_NODE_SHOW_CREATE_VIEW_STMT = 181,
@@ -1769,6 +1771,29 @@ int32_t tSerializeSDropTokenReq(void* buf, int32_t bufLen, SDropTokenReq* pReq);
17691771
int32_t tDeserializeSDropTokenReq(void* buf, int32_t bufLen, SDropTokenReq* pReq);
17701772
void tFreeSDropTokenReq(SDropTokenReq* pReq);
17711773

1774+
typedef struct {
1775+
char user[TSDB_USER_LEN];
1776+
int32_t sqlLen;
1777+
char* sql;
1778+
} SCreateTotpSecretReq;
1779+
1780+
int32_t tSerializeSCreateTotpSecretReq(void* buf, int32_t bufLen, SCreateTotpSecretReq* pReq);
1781+
int32_t tDeserializeSCreateTotpSecretReq(void* buf, int32_t bufLen, SCreateTotpSecretReq* pReq);
1782+
void tFreeSCreateTotpSecretReq(SCreateTotpSecretReq* pReq);
1783+
1784+
typedef struct {
1785+
char user[TSDB_USER_LEN];
1786+
char totpSecret[(TSDB_TOTP_SECRET_LEN * 8 + 4) / 5 + 1]; // base32 encoded totp secret + null terminator
1787+
} SCreateTotpSecretRsp;
1788+
1789+
int32_t tSerializeSCreateTotpSecretRsp(void* buf, int32_t bufLen, SCreateTotpSecretRsp* pRsp);
1790+
int32_t tDeserializeSCreateTotpSecretRsp(void* buf, int32_t bufLen, SCreateTotpSecretRsp* pRsp);
1791+
1792+
typedef SCreateTotpSecretReq SDropTotpSecretReq;
1793+
#define tSerializeSDropTotpSecretReq tSerializeSCreateTotpSecretReq
1794+
#define tDeserializeSDropTotpSecretReq tDeserializeSCreateTotpSecretReq
1795+
#define tFreeSDropTotpSecretReq tFreeSCreateTotpSecretReq
1796+
17721797
typedef struct {
17731798
char user[TSDB_USER_LEN];
17741799
} SGetUserAuthReq;

include/common/tmsgdef.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@
159159
TD_DEF_MSG_TYPE(TDMT_MND_DROP_TOKEN, "drop-token", NULL, NULL)
160160
TD_DEF_MSG_TYPE(TDMT_MND_KEY_SYNC, "key-sync", NULL, NULL)
161161
TD_DEF_MSG_TYPE(TDMT_MND_ALTER_ENCRYPT_KEY, "alter-encrypt-key", NULL, NULL)
162+
TD_DEF_MSG_TYPE(TDMT_MND_CREATE_TOTP_SECRET, "create-totp-secret", NULL, NULL)
163+
TD_DEF_MSG_TYPE(TDMT_MND_DROP_TOTP_SECRET, "drop-totp-secret", NULL, NULL)
162164
TD_CLOSE_MSG_SEG(TDMT_DND_MSG)
163165

164166
TD_NEW_MSG_SEG(TDMT_MND_MSG) // 1<<8

include/libs/nodes/cmdnodes.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,11 @@ extern "C" {
6868

6969
#define SHOW_ALIVE_RESULT_COLS 1
7070

71-
#define CREATE_USER_TOKEN_RESULT_COLS 1
72-
#define CREATE_USER_TOKEN_RESULT_FIELD1_LEN (TSDB_TOKEN_LEN + VARSTR_HEADER_SIZE)
71+
#define CREATE_TOKEN_RESULT_COLS 1
72+
#define CREATE_TOKEN_RESULT_FIELD1_LEN (TSDB_TOKEN_LEN + VARSTR_HEADER_SIZE)
73+
74+
#define CREATE_TOTP_SECRET_RESULT_COLS 1
75+
#define CREATE_TOTP_SECRET_RESULT_FIELD1_LEN ((TSDB_TOTP_SECRET_LEN * 8 + 4) / 5 + 1 + VARSTR_HEADER_SIZE)
7376

7477
#define BIT_FLAG_MASK(n) (1 << n)
7578
#define BIT_FLAG_SET_MASK(val, mask) ((val) |= (mask))
@@ -600,6 +603,17 @@ typedef struct SDropTokenStmt {
600603
bool ignoreNotExists;
601604
} SDropTokenStmt;
602605

606+
typedef struct SCreateTotpSecretStmt {
607+
ENodeType type;
608+
609+
char user[TSDB_USER_LEN];
610+
} SCreateTotpSecretStmt;
611+
612+
typedef struct SDropTotpSecretStmt {
613+
ENodeType type;
614+
615+
char user[TSDB_USER_LEN];
616+
} SDropTotpSecretStmt;
603617

604618
typedef struct SDropEncryptAlgrStmt {
605619
ENodeType type;

include/os/osRand.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ void taosRandStr(char* str, int32_t size);
3535
void taosRandStr2(char* str, int32_t size);
3636

3737
uint32_t taosSafeRand(void);
38+
void taosSafeRandBytes(uint8_t* pBuf, int32_t size);
3839

3940
#ifdef __cplusplus
4041
}

0 commit comments

Comments
 (0)