Skip to content

Commit

Permalink
Merge branch 'master' into fix-socket-auth
Browse files Browse the repository at this point in the history
  • Loading branch information
morgo authored Nov 29, 2021
2 parents 909e64e + b1ca2eb commit c037f7d
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 31 deletions.
20 changes: 20 additions & 0 deletions cmd/explaintest/r/new_character_set_builtin.result
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,23 @@ select concat(a, 0xb6fe) from t;
Error 3854: Cannot convert string 'B6FE' from binary to utf8mb4
select concat(b, 0xe4ba8c) from t;
Error 3854: Cannot convert string 'E4BA8C' from binary to gbk
drop table if exists t;
create table t (a char(20) charset utf8mb4, b char(20) charset gbk, c binary(20));
insert into t values ('一二三', '一二三', '一二三');
set @@block_encryption_mode='aes-128-ecb';
select hex(aes_encrypt(a, '123')), hex(aes_encrypt(b, '123')), hex(aes_encrypt(c, '123')) from t;
hex(aes_encrypt(a, '123')) hex(aes_encrypt(b, '123')) hex(aes_encrypt(c, '123'))
C54279F381B0710E145E94106F03C94C 7A747EC6F1906276D036B1F3CE27BAAB A0E5E01289017B8A3691CCFBDE81A59ED4A9D5BF50A298D41287E395CDDCAD56
set @@block_encryption_mode='aes-128-ofb';
select hex(aes_encrypt(a, '123', '1234567890123456')), hex(aes_encrypt(b, '123', '1234567890123456')), hex(aes_encrypt(c, '123', '1234567890123456')) from t;
hex(aes_encrypt(a, '123', '1234567890123456')) hex(aes_encrypt(b, '123', '1234567890123456')) hex(aes_encrypt(c, '123', '1234567890123456'))
65473346EE2BF64CD2 5344055C9C5A 65473346EE2BF64CD2BA02F128114B0E061E095B
set @@tidb_enable_vectorized_expression = true;
select hex(aes_encrypt(a, '123', '1234567890123456')), hex(aes_encrypt(b, '123', '1234567890123456')), hex(aes_encrypt(c, '123', '1234567890123456')) from t;
hex(aes_encrypt(a, '123', '1234567890123456')) hex(aes_encrypt(b, '123', '1234567890123456')) hex(aes_encrypt(c, '123', '1234567890123456'))
65473346EE2BF64CD2 5344055C9C5A 65473346EE2BF64CD2BA02F128114B0E061E095B
set @@block_encryption_mode='aes-128-ecb';
select hex(aes_encrypt(a, '123')), hex(aes_encrypt(b, '123')), hex(aes_encrypt(c, '123')) from t;
hex(aes_encrypt(a, '123')) hex(aes_encrypt(b, '123')) hex(aes_encrypt(c, '123'))
C54279F381B0710E145E94106F03C94C 7A747EC6F1906276D036B1F3CE27BAAB A0E5E01289017B8A3691CCFBDE81A59ED4A9D5BF50A298D41287E395CDDCAD56
set @@tidb_enable_vectorized_expression = false;
14 changes: 14 additions & 0 deletions cmd/explaintest/t/new_character_set_builtin.test
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,17 @@ select b = 0xe4ba8c from t;
select concat(a, 0xb6fe) from t;
--error ER_CANNOT_CONVERT_STRING
select concat(b, 0xe4ba8c) from t;

-- test for builtin function aes_encrypt()
drop table if exists t;
create table t (a char(20) charset utf8mb4, b char(20) charset gbk, c binary(20));
insert into t values ('一二三', '一二三', '一二三');
set @@block_encryption_mode='aes-128-ecb';
select hex(aes_encrypt(a, '123')), hex(aes_encrypt(b, '123')), hex(aes_encrypt(c, '123')) from t;
set @@block_encryption_mode='aes-128-ofb';
select hex(aes_encrypt(a, '123', '1234567890123456')), hex(aes_encrypt(b, '123', '1234567890123456')), hex(aes_encrypt(c, '123', '1234567890123456')) from t;
set @@tidb_enable_vectorized_expression = true;
select hex(aes_encrypt(a, '123', '1234567890123456')), hex(aes_encrypt(b, '123', '1234567890123456')), hex(aes_encrypt(c, '123', '1234567890123456')) from t;
set @@block_encryption_mode='aes-128-ecb';
select hex(aes_encrypt(a, '123')), hex(aes_encrypt(b, '123')), hex(aes_encrypt(c, '123')) from t;
set @@tidb_enable_vectorized_expression = false;
55 changes: 25 additions & 30 deletions docs/design/2020-06-24-placement-rules-in-sql.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Defining placement rules in SQL

- Author(s): [djshow832](https://github.com/djshow832) (Ming Zhang), [morgo](https://github.com/morgo) (Morgan Tocker)
- Last updated: 2021-09-13
- Last updated: 2021-11-28
- Discussion PR: https://github.com/pingcap/tidb/pull/26221
- Tracking Issue: https://github.com/pingcap/tidb/issues/18030
- Original Document (Chinese): https://docs.google.com/document/d/18Kdhi90dv33muF9k_VAIccNLeGf-DdQyUc8JlWF9Gok
Expand Down Expand Up @@ -175,25 +175,25 @@ A new system table `information_schema.placement_rules` is added to view all exp
The table definition is as follows:

```sql
+----------------------+--------------+------+------+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+--------------+------+------+---------+-------+
| POLICY_ID | bigint(64) | NO | | NULL | |
| CATALOG_NAME | varchar(512) | NO | | NULL | |
| POLICY_NAME | varchar(5) | YES | | NULL | |
| SCHEMA_NAME | varchar(5) | YES | | NULL | |
| TABLE_NAME | varchar(5) | YES | | NULL | |
| PARTITION_NAME | varchar(5) | YES | | NULL | |
| PRIMARY_REGION | varchar(5) | NO | | NULL | |
| REGIONS | varchar(5) | NO | | NULL | |
| CONSTRAINTS | varchar(5) | NO | | NULL | |
| LEADER_CONSTRAINTS | varchar(5) | NO | | NULL | |
| FOLLOWER_CONSTRAINTS | varchar(5) | NO | | NULL | |
| LEARNER_CONSTRAINTS | varchar(5) | NO | | NULL | |
| SCHEDULE | varchar(20) | NO | | NULL | |
| FOLLOWERS | bigint(64) | NO | | NULL | |
| LEARNERS | bigint(64) | NO | | NULL | |
+----------------------+--------------+------+------+---------+-------+
+----------------------+---------------+------+------+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+---------------+------+------+---------+-------+
| POLICY_ID | bigint(64) | NO | | NULL | |
| CATALOG_NAME | varchar(512) | NO | | NULL | |
| POLICY_NAME | varchar(64) | YES | | NULL | |
| SCHEMA_NAME | varchar(64) | YES | | NULL | |
| TABLE_NAME | varchar(64) | YES | | NULL | |
| PARTITION_NAME | varchar(64) | YES | | NULL | |
| PRIMARY_REGION | varchar(1024) | NO | | NULL | |
| REGIONS | varchar(1024) | NO | | NULL | |
| CONSTRAINTS | varchar(1024) | NO | | NULL | |
| LEADER_CONSTRAINTS | varchar(1024) | NO | | NULL | |
| FOLLOWER_CONSTRAINTS | varchar(1024) | NO | | NULL | |
| LEARNER_CONSTRAINTS | varchar(1024) | NO | | NULL | |
| SCHEDULE | varchar(20) | NO | | NULL | |
| FOLLOWERS | bigint(64) | NO | | NULL | |
| LEARNERS | bigint(64) | NO | | NULL | |
+----------------------+---------------+------+------+---------+-------+
15 rows in set (0.00 sec)
```

Expand All @@ -203,7 +203,7 @@ The information_schema tables for `tables` and `partitions` should be modified t

```golang
{name: "TIDB_PLACEMENT_POLICY_NAME", tp: mysql.TypeVarchar, size: 64},
{name: "TIDB_DIRECT_PLACEMENT", tp: mysql.TypeVarchar, size: types.UnspecifiedLength}
{name: "TIDB_DIRECT_PLACEMENT", tp: mysql.TypeVarchar, size: 1024}
```

This helps make the information match what is available in `SHOW CREATE TABLE`, but in a structured format.
Expand Down Expand Up @@ -418,15 +418,6 @@ Explanation:

`+any` changes an earlier proposal where the `FOLLOWERS` count could also be specified. This has been removed to reduce the risk of discrepancies and misconfiguration. See also "Policy Validation" below.

#### Built-in Placement Policies

By default every system will have two placement policies, which can be modified via `ALTER PLACEMENT POLICY` but never dropped:

* `default`: This policy is used only in the event that a policy has not been specified.
* `system`: This policy is used for internal TiDB system tables.

Some common applications might be to increase the replica count on system or default tables. It is not typically recommended to add constraints to these policies as it will lead to cluster inbalance, but it is possible.

#### Schedule Property

When using either the syntactic sugar or list format for placement rules, PD is free to schedule followers/leaders wherever it decides. For example:
Expand Down Expand Up @@ -1040,6 +1031,10 @@ This specific semantic will be the hardest to implement because of the other dep

## Changelog

* 2021-11-29:
- Updated limits on object length.
- Removed built-in placement policies (not supported for now, need additional discussion due to `DEFAULT` conflicts.)

* 2021-10-29:
- Add more description to 'scheduling_state'.

Expand Down
2 changes: 1 addition & 1 deletion expression/builtin_convert_charset.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func HandleBinaryLiteral(ctx sessionctx.Context, expr Expression, ec *ExprCollat
ft.Charset, ft.Collate = ec.Charset, ec.Collation
return BuildFromBinaryFunction(ctx, expr, ft)
}
case ast.Hex, ast.Length, ast.OctetLength, ast.ASCII, ast.ToBase64, ast.AesDecrypt, ast.Decode, ast.Encode,
case ast.Hex, ast.Length, ast.OctetLength, ast.ASCII, ast.ToBase64, ast.AesEncrypt, ast.AesDecrypt, ast.Decode, ast.Encode,
ast.PasswordFunc, ast.MD5, ast.SHA, ast.SHA1, ast.SHA2, ast.Compress:
if _, err := charset.GetDefaultCollationLegacy(expr.GetType().Charset); err != nil {
return BuildToBinaryFunction(ctx, expr)
Expand Down
41 changes: 41 additions & 0 deletions expression/builtin_encryption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,47 @@ func TestAESEncrypt(t *testing.T) {
require.NoError(t, err)
testNullInput(t, ctx, ast.AesEncrypt)
testAmbiguousInput(t, ctx, ast.AesEncrypt)

// Test GBK String
gbkStr, _ := charset.NewEncoding("gbk").EncodeString("你好")
gbkTests := []struct {
mode string
chs string
origin interface{}
params []interface{}
crypt string
}{
// test for ecb
{"aes-128-ecb", "utf8mb4", "你好", []interface{}{"123"}, "CEBD80EEC6423BEAFA1BB30FD7625CBC"},
{"aes-128-ecb", "gbk", gbkStr, []interface{}{"123"}, "6AFA9D7BA2C1AED1603E804F75BB0127"},
{"aes-128-ecb", "utf8mb4", "123", []interface{}{"你好"}, "E03F6D9C1C86B82F5620EE0AA9BD2F6A"},
{"aes-128-ecb", "gbk", "123", []interface{}{"你好"}, "31A2D26529F0E6A38D406379ABD26FA5"},
{"aes-128-ecb", "utf8mb4", "你好", []interface{}{"你好"}, "3E2D8211DAE17143F22C2C5969A35263"},
{"aes-128-ecb", "gbk", gbkStr, []interface{}{"你好"}, "84982910338160D037615D283AD413DE"},
// test for cbc
{"aes-128-cbc", "utf8mb4", "你好", []interface{}{"123", "1234567890123456"}, "B95509A516ACED59C3DF4EC41C538D83"},
{"aes-128-cbc", "gbk", gbkStr, []interface{}{"123", "1234567890123456"}, "D4322D091B5DDE0DEB35B1749DA2483C"},
{"aes-128-cbc", "utf8mb4", "123", []interface{}{"你好", "1234567890123456"}, "E19E86A9E78E523267AFF36261AD117D"},
{"aes-128-cbc", "gbk", "123", []interface{}{"你好", "1234567890123456"}, "5A2F8F2C1841CC4E1D1640F1EA2A1A23"},
{"aes-128-cbc", "utf8mb4", "你好", []interface{}{"你好", "1234567890123456"}, "B73637C73302C909EA63274C07883E71"},
{"aes-128-cbc", "gbk", gbkStr, []interface{}{"你好", "1234567890123456"}, "61E13E9B00F2E757F4E925D3268227A0"},
}

for _, tt := range gbkTests {
err := ctx.GetSessionVars().SetSystemVar(variable.CharacterSetConnection, tt.chs)
require.NoError(t, err)
err = variable.SetSessionSystemVar(ctx.GetSessionVars(), variable.BlockEncryptionMode, tt.mode)
require.NoError(t, err)

args := datumsToConstants([]types.Datum{types.NewDatum(tt.origin)})
args = append(args, primitiveValsToConstants(ctx, tt.params)...)
f, err := fc.getFunction(ctx, args)

require.NoError(t, err)
crypt, err := evalBuiltinFunc(f, chunk.Row{})
require.NoError(t, err)
require.Equal(t, types.NewDatum(tt.crypt), toHex(crypt))
}
}

func TestAESDecrypt(t *testing.T) {
Expand Down

0 comments on commit c037f7d

Please sign in to comment.