Skip to content

Commit

Permalink
feat: Implements EncryptionRule DistSQL Parser Visitor with AST (#301)
Browse files Browse the repository at this point in the history
* feat: implements encrypt rule distsql visitor interface with ast

Signed-off-by: wangbo <wangbo@sphere-ex.com>

* feat: add TODO

Signed-off-by: wangbo <wangbo@sphere-ex.com>

* chore: fix import package error

Signed-off-by: wangbo <wangbo@sphere-ex.com>

* chore: fix ci error

Signed-off-by: wangbo <wangbo@sphere-ex.com>

* chore: fix lint

Signed-off-by: wangbo <wangbo@sphere-ex.com>

* feat: add encrypt distsql ast

Signed-off-by: wangbo <wangbo@sphere-ex.com>

---------

Signed-off-by: wangbo <wangbo@sphere-ex.com>
Co-authored-by: wangbo <wangbo@sphere-ex.com>
  • Loading branch information
wbtlb and wangbo authored Apr 8, 2023
1 parent fdc99fd commit 0d3b1e9
Show file tree
Hide file tree
Showing 15 changed files with 6,695 additions and 0 deletions.
1 change: 1 addition & 0 deletions shardingsphere-operator/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/apache/shardingsphere-on-cloud/shardingsphere-operator
go 1.19

require (
github.com/antlr/antlr4 v0.0.0-20181218183524-be58ebffde8e
github.com/go-logr/logr v1.2.3
github.com/onsi/ginkgo/v2 v2.8.0
github.com/onsi/gomega v1.26.0
Expand Down
2 changes: 2 additions & 0 deletions shardingsphere-operator/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRF
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/antlr/antlr4 v0.0.0-20181218183524-be58ebffde8e h1:yxMh4HIdsSh2EqxUESWvzszYMNzOugRyYCeohfwNULM=
github.com/antlr/antlr4 v0.0.0-20181218183524-be58ebffde8e/go.mod h1:T7PbCXFs94rrTttyxjbyT5+/1V8T2TYDejxUfHJjw1Y=
github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20210826220005-b48c857c3a0e/go.mod h1:F7bn7fEU90QkQ3tnmaTx3LTKLEDqnwWODIYppRQ5hnY=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
Expand Down
49 changes: 49 additions & 0 deletions shardingsphere-operator/pkg/distsql/antlr4/encrypt/Alphabet.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


lexer grammar Alphabet;

FOR_GENERATOR: 'DO NOT MATCH ANY THING, JUST FOR GENERATOR';

fragment A: [Aa];
fragment B: [Bb];
fragment C: [Cc];
fragment D: [Dd];
fragment E: [Ee];
fragment F: [Ff];
fragment G: [Gg];
fragment H: [Hh];
fragment I: [Ii];
fragment J: [Jj];
fragment K: [Kk];
fragment L: [Ll];
fragment M: [Mm];
fragment N: [Nn];
fragment O: [Oo];
fragment P: [Pp];
fragment Q: [Qq];
fragment R: [Rr];
fragment S: [Ss];
fragment T: [Tt];
fragment U: [Uu];
fragment V: [Vv];
fragment W: [Ww];
fragment X: [Xx];
fragment Y: [Yy];
fragment Z: [Zz];
fragment UL_: '_';
57 changes: 57 additions & 0 deletions shardingsphere-operator/pkg/distsql/antlr4/encrypt/BaseRule.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

grammar BaseRule;

import Symbol, Keyword, Literals;

literal
: STRING_ | (MINUS_)? INT_ | TRUE | FALSE
;

algorithmDefinition
: TYPE LP_ NAME EQ_ algorithmTypeName (COMMA_ propertiesDefinition)? RP_
;

algorithmTypeName
: buildinAlgorithmTypeName | STRING_
;

buildinAlgorithmTypeName
: MD5
| AES
| RC4
| SM3
| SM4
| CHAR_DIGEST_LIKE
;

propertiesDefinition
: PROPERTIES LP_ properties? RP_
;

properties
: property (COMMA_ property)*
;

property
: key=STRING_ EQ_ value=literal
;

tableName
: IDENTIFIER_
;
184 changes: 184 additions & 0 deletions shardingsphere-operator/pkg/distsql/antlr4/encrypt/Keyword.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

lexer grammar Keyword;

import Alphabet;

WS
: [ \t\r\n] + ->skip
;

CREATE
: C R E A T E
;

ALTER
: A L T E R
;

DROP
: D R O P
;

SHOW
: S H O W
;

RESOURCE
: R E S O U R C E
;

RULE
: R U L E
;

FROM
: F R O M
;

ENCRYPT
: E N C R Y P T
;

TYPE
: T Y P E
;

ENCRYPT_ALGORITHM
: E N C R Y P T UL_ A L G O R I T H M
;

ASSISTED_QUERY_ALGORITHM
: A S S I S T E D UL_ Q U E R Y UL_ A L G O R I T H M
;

LIKE_QUERY_ALGORITHM
: L I K E UL_ Q U E R Y UL_ A L G O R I T H M
;

NAME
: N A M E
;

PROPERTIES
: P R O P E R T I E S
;

COLUMN
: C O L U M N
;

RULES
: R U L E S
;

TABLE
: T A B L E
;

COLUMNS
: C O L U M N S
;

CIPHER
: C I P H E R
;

PLAIN
: P L A I N
;

ASSISTED_QUERY_COLUMN
: A S S I S T E D UL_ Q U E R Y UL_ C O L U M N
;

LIKE_QUERY_COLUMN
: L I K E UL_ Q U E R Y UL_ C O L U M N
;

QUERY_WITH_CIPHER_COLUMN
: Q U E R Y UL_ W I T H UL_ C I P H E R UL_ C O L U M N
;

TRUE
: T R U E
;

FALSE
: F A L S E
;

DATA_TYPE
: D A T A UL_ T Y P E
;

PLAIN_DATA_TYPE
: P L A I N UL_ D A T A UL_ T Y P E
;

CIPHER_DATA_TYPE
: C I P H E R UL_ D A T A UL_ T Y P E
;

ASSISTED_QUERY_DATA_TYPE
: A S S I S T E D UL_ Q U E R Y UL_ D A T A UL_ T Y P E
;

LIKE_QUERY_DATA_TYPE
: L I K E UL_ Q U E R Y UL_ D A T A UL_ T Y P E
;

IF
: I F
;

EXISTS
: E X I S T S
;

COUNT
: C O U N T
;

MD5
: M D [5]
;

AES
: A E S
;

RC4
: R C [4]
;

SM3
: S M [3]
;

SM4
: S M [4]
;

CHAR_DIGEST_LIKE
: C H A R UL_ D I G E S T UL_ L I K E
;

NOT
: N O T
;
50 changes: 50 additions & 0 deletions shardingsphere-operator/pkg/distsql/antlr4/encrypt/Literals.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

lexer grammar Literals;

import Alphabet, Symbol;

IDENTIFIER_
: [A-Za-z_$0-9]*?[A-Za-z_$]+?[A-Za-z_$0-9]*
| BQ_ ~'`'+ BQ_
;

STRING_
: (DQ_ ('\\'. | '""' | ~('"' | '\\'))* DQ_)
| (SQ_ ('\\'. | '\'\'' | ~('\'' | '\\'))* SQ_)
;

INT_
: [0-9]+
;

HEX_
: [0-9a-fA-F]
;

NUMBER_
: INT_? DOT_? INT_ (E (PLUS_ | MINUS_)? INT_)?
;

HEXDIGIT_
: '0x' HEX_+ | 'X' SQ_ HEX_+ SQ_
;

BITNUM_
: '0b' ('0' | '1')+ | B SQ_ ('0' | '1')+ SQ_
;
Loading

0 comments on commit 0d3b1e9

Please sign in to comment.