Skip to content

Commit 8324e65

Browse files
authored
Fix privilege check in CreateDatabaseBackendHandler (#32)
1 parent 1e156c9 commit 8324e65

File tree

17 files changed

+311
-13
lines changed

17 files changed

+311
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.sphereex.dbplusengine.authority.obj.domain;
19+
20+
import com.sphereex.dbplusengine.authority.model.obj.ACLObject;
21+
22+
/**
23+
* RAL ACL object.
24+
*/
25+
public final class RALACLObject implements ACLObject {
26+
}

kernel/authority/core/src/main/java/com/sphereex/dbplusengine/authority/obj/extractor/type/dal/dialect/MySQLACLObjectExtractor.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818
package com.sphereex.dbplusengine.authority.obj.extractor.type.dal.dialect;
1919

2020
import com.sphereex.dbplusengine.authority.model.obj.ACLObject;
21+
import com.sphereex.dbplusengine.authority.obj.domain.TableACLObject;
2122
import lombok.AccessLevel;
2223
import lombok.NoArgsConstructor;
2324
import org.apache.shardingsphere.sql.parser.statement.mysql.MySQLStatement;
25+
import org.apache.shardingsphere.sql.parser.statement.mysql.dal.MySQLShowCreateTableStatement;
26+
import org.apache.shardingsphere.sql.parser.statement.mysql.ddl.MySQLDropIndexStatement;
2427

2528
import java.util.Collection;
2629
import java.util.Collections;
@@ -39,7 +42,27 @@ public final class MySQLACLObjectExtractor {
3942
* @return extracted ACL objects
4043
*/
4144
public static Collection<ACLObject> extract(final String currentDatabase, final MySQLStatement sqlStatement) {
42-
// TODO
45+
if (sqlStatement instanceof MySQLShowCreateTableStatement) {
46+
return extractSowCreateTableStatement(currentDatabase, (MySQLShowCreateTableStatement) sqlStatement);
47+
}
48+
if (sqlStatement instanceof MySQLDropIndexStatement) {
49+
return extractDropIndexStatement(currentDatabase, (MySQLDropIndexStatement) sqlStatement);
50+
}
4351
return Collections.emptyList();
4452
}
53+
54+
private static Collection<ACLObject> extractSowCreateTableStatement(final String currentDatabase, final MySQLShowCreateTableStatement sqlStatement) {
55+
String database = sqlStatement.getTable().getOwner().map(optional -> optional.getIdentifier().getValue()).orElse(currentDatabase);
56+
String table = sqlStatement.getTable().getTableName().getIdentifier().getValue();
57+
return Collections.singleton(new TableACLObject(database, table));
58+
}
59+
60+
private static Collection<ACLObject> extractDropIndexStatement(final String currentDatabase, final MySQLDropIndexStatement sqlStatement) {
61+
if (!sqlStatement.getSimpleTable().isPresent()) {
62+
return Collections.emptyList();
63+
}
64+
String database = sqlStatement.getSimpleTable().get().getOwner().map(optional -> optional.getIdentifier().getValue()).orElse(currentDatabase);
65+
String table = sqlStatement.getSimpleTable().get().getTableName().getIdentifier().getValue();
66+
return Collections.singleton(new TableACLObject(database, table));
67+
}
4568
}

kernel/authority/core/src/main/java/com/sphereex/dbplusengine/authority/obj/extractor/type/ddl/DDLACLObjectExtractor.java

+15
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,22 @@
1919

2020
import com.sphereex.dbplusengine.authority.model.obj.ACLObject;
2121
import com.sphereex.dbplusengine.authority.obj.extractor.type.ddl.type.AlterTableACLObjectExtractor;
22+
import com.sphereex.dbplusengine.authority.obj.extractor.type.ddl.type.CreateDatabaseACLObjectExtractor;
23+
import com.sphereex.dbplusengine.authority.obj.extractor.type.ddl.type.CreateIndexACLObjectExtractor;
2224
import com.sphereex.dbplusengine.authority.obj.extractor.type.ddl.type.CreateTableACLObjectExtractor;
2325
import com.sphereex.dbplusengine.authority.obj.extractor.type.ddl.type.DropDatabaseACLObjectExtractor;
26+
import com.sphereex.dbplusengine.authority.obj.extractor.type.ddl.type.DropIndexACLObjectExtractor;
2427
import com.sphereex.dbplusengine.authority.obj.extractor.type.ddl.type.DropTableACLObjectExtractor;
2528
import com.sphereex.dbplusengine.authority.obj.extractor.type.ddl.type.TruncateTableACLObjectExtractor;
2629
import lombok.AccessLevel;
2730
import lombok.NoArgsConstructor;
2831
import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
2932
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterTableStatement;
33+
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateDatabaseStatement;
34+
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateIndexStatement;
3035
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateTableStatement;
3136
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropDatabaseStatement;
37+
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropIndexStatement;
3238
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropTableStatement;
3339
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.TruncateStatement;
3440

@@ -58,9 +64,18 @@ public static Collection<ACLObject> extract(final String currentDatabase, final
5864
if (sqlStatement instanceof DropTableStatement) {
5965
return DropTableACLObjectExtractor.extract(currentDatabase, (DropTableStatement) sqlStatement);
6066
}
67+
if (sqlStatement instanceof CreateIndexStatement) {
68+
return CreateIndexACLObjectExtractor.extract(currentDatabase, (CreateIndexStatement) sqlStatement);
69+
}
70+
if (sqlStatement instanceof DropIndexStatement) {
71+
return DropIndexACLObjectExtractor.extract(currentDatabase, (DropIndexStatement) sqlStatement);
72+
}
6173
if (sqlStatement instanceof TruncateStatement) {
6274
return TruncateTableACLObjectExtractor.extract(currentDatabase, (TruncateStatement) sqlStatement);
6375
}
76+
if (sqlStatement instanceof CreateDatabaseStatement) {
77+
return CreateDatabaseACLObjectExtractor.extract((CreateDatabaseStatement) sqlStatement);
78+
}
6479
if (sqlStatement instanceof DropDatabaseStatement) {
6580
return DropDatabaseACLObjectExtractor.extract((DropDatabaseStatement) sqlStatement);
6681
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.sphereex.dbplusengine.authority.obj.extractor.type.ddl.type;
19+
20+
import com.sphereex.dbplusengine.authority.model.obj.ACLObject;
21+
import com.sphereex.dbplusengine.authority.obj.domain.TableACLObject;
22+
import lombok.AccessLevel;
23+
import lombok.NoArgsConstructor;
24+
import org.apache.shardingsphere.authority.constant.AuthorityConstants;
25+
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateDatabaseStatement;
26+
27+
import java.util.Collection;
28+
import java.util.Collections;
29+
30+
/**
31+
* Create database ACL object extractor.
32+
*/
33+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
34+
public final class CreateDatabaseACLObjectExtractor {
35+
36+
/**
37+
* Extract ACL objects.
38+
*
39+
* @param sqlStatement create database statement
40+
* @return extracted ACL objects
41+
*/
42+
public static Collection<ACLObject> extract(final CreateDatabaseStatement sqlStatement) {
43+
return Collections.singleton(new TableACLObject(sqlStatement.getDatabaseName(), AuthorityConstants.PRIVILEGE_WILDCARD));
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.sphereex.dbplusengine.authority.obj.extractor.type.ddl.type;
19+
20+
import com.sphereex.dbplusengine.authority.model.obj.ACLObject;
21+
import com.sphereex.dbplusengine.authority.obj.domain.TableACLObject;
22+
import lombok.AccessLevel;
23+
import lombok.NoArgsConstructor;
24+
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateIndexStatement;
25+
26+
import java.util.Collection;
27+
import java.util.Collections;
28+
29+
/**
30+
* Create index ACL object extractor.
31+
*/
32+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
33+
public final class CreateIndexACLObjectExtractor {
34+
35+
/**
36+
* Extract ACL objects.
37+
*
38+
* @param currentDatabase current database name
39+
* @param sqlStatement create index statement
40+
* @return extracted ACL objects
41+
*/
42+
public static Collection<ACLObject> extract(final String currentDatabase, final CreateIndexStatement sqlStatement) {
43+
if (null == sqlStatement.getTable()) {
44+
return Collections.emptyList();
45+
}
46+
String database = sqlStatement.getTable().getOwner().map(optional -> optional.getIdentifier().getValue()).orElse(currentDatabase);
47+
String table = sqlStatement.getTable().getTableName().getIdentifier().getValue();
48+
return Collections.singleton(new TableACLObject(database, table));
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.sphereex.dbplusengine.authority.obj.extractor.type.ddl.type;
19+
20+
import com.sphereex.dbplusengine.authority.model.obj.ACLObject;
21+
import com.sphereex.dbplusengine.authority.obj.extractor.type.dal.dialect.MySQLACLObjectExtractor;
22+
import lombok.AccessLevel;
23+
import lombok.NoArgsConstructor;
24+
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropIndexStatement;
25+
import org.apache.shardingsphere.sql.parser.statement.mysql.MySQLStatement;
26+
27+
import java.util.Collection;
28+
import java.util.Collections;
29+
30+
/**
31+
* Drop index ACL object extractor.
32+
*/
33+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
34+
public final class DropIndexACLObjectExtractor {
35+
36+
/**
37+
* Extract ACL objects.
38+
*
39+
* @param currentDatabase current database name
40+
* @param sqlStatement drop index statement
41+
* @return extracted ACL objects
42+
*/
43+
public static Collection<ACLObject> extract(final String currentDatabase, final DropIndexStatement sqlStatement) {
44+
return sqlStatement instanceof MySQLStatement ? MySQLACLObjectExtractor.extract(currentDatabase, (MySQLStatement) sqlStatement) : Collections.emptyList();
45+
}
46+
}

kernel/authority/core/src/main/java/com/sphereex/dbplusengine/authority/obj/extractor/type/distsql/DistSQLACLObjectExtractor.java

+7
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@
2020
import com.sphereex.dbplusengine.authority.model.obj.ACLObject;
2121
import com.sphereex.dbplusengine.authority.obj.domain.DCLACLObject;
2222
import com.sphereex.dbplusengine.authority.obj.domain.DistSQLACLObject;
23+
import com.sphereex.dbplusengine.authority.obj.domain.RALACLObject;
2324
import com.sphereex.dbplusengine.distsql.acl.DistSQLACLObjectUtils;
2425
import com.sphereex.dbplusengine.distsql.extractor.DistSQLResourceIdentifierExtractor;
2526
import lombok.AccessLevel;
2627
import lombok.NoArgsConstructor;
2728
import org.apache.shardingsphere.authority.constant.AuthorityConstants;
2829
import org.apache.shardingsphere.distsql.statement.DistSQLStatement;
30+
import org.apache.shardingsphere.distsql.statement.ral.RALStatement;
31+
import org.apache.shardingsphere.distsql.statement.rdl.rule.global.GlobalRuleDefinitionStatement;
32+
import org.apache.shardingsphere.distsql.statement.rql.rule.global.ShowGlobalRulesStatement;
2933
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
3034
import org.apache.shardingsphere.sql.parser.statement.core.statement.dcl.DCLStatement;
3135

@@ -51,6 +55,9 @@ public static Collection<ACLObject> extract(final String currentDatabase, final
5155
if (sqlStatement instanceof DCLStatement) {
5256
return Collections.singleton(new DCLACLObject());
5357
}
58+
if (sqlStatement instanceof RALStatement || sqlStatement instanceof ShowGlobalRulesStatement || sqlStatement instanceof GlobalRuleDefinitionStatement) {
59+
return Collections.singleton(new RALACLObject());
60+
}
5461
Collection<String> aclObjectNames = TypedSPILoader.findService(DistSQLResourceIdentifierExtractor.class, sqlStatement.getClass())
5562
.map(optional -> optional.extract(sqlStatement)).orElse(Collections.emptyList());
5663
String aclObjectTypeName = DistSQLACLObjectUtils.getACLObject(sqlStatement.getClass());

kernel/authority/core/src/main/java/com/sphereex/dbplusengine/authority/operation/ACLOperationExtractor.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,31 @@
2222
import lombok.NoArgsConstructor;
2323
import org.apache.shardingsphere.distsql.statement.DistSQLStatement;
2424
import org.apache.shardingsphere.distsql.statement.ral.RALStatement;
25+
import org.apache.shardingsphere.distsql.statement.rdl.resource.unit.type.AlterStorageUnitStatement;
2526
import org.apache.shardingsphere.distsql.statement.rdl.resource.unit.type.RegisterStorageUnitStatement;
2627
import org.apache.shardingsphere.distsql.statement.rdl.resource.unit.type.UnregisterStorageUnitStatement;
2728
import org.apache.shardingsphere.distsql.statement.rdl.rule.database.type.AlterRuleStatement;
2829
import org.apache.shardingsphere.distsql.statement.rdl.rule.database.type.CreateRuleStatement;
2930
import org.apache.shardingsphere.distsql.statement.rdl.rule.database.type.DropRuleStatement;
31+
import org.apache.shardingsphere.distsql.statement.rdl.rule.global.GlobalRuleDefinitionStatement;
3032
import org.apache.shardingsphere.distsql.statement.rql.resource.ShowStorageUnitsStatement;
3133
import org.apache.shardingsphere.distsql.statement.rql.rule.database.ShowDatabaseRulesStatement;
34+
import org.apache.shardingsphere.distsql.statement.rql.rule.global.ShowGlobalRulesStatement;
3235
import org.apache.shardingsphere.distsql.statement.rul.sql.FormatStatement;
3336
import org.apache.shardingsphere.distsql.statement.rul.sql.ParseStatement;
3437
import org.apache.shardingsphere.distsql.statement.rul.sql.PreviewStatement;
3538
import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
3639
import org.apache.shardingsphere.sql.parser.statement.core.statement.dcl.DCLStatement;
3740
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterDatabaseStatement;
41+
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterIndexStatement;
3842
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterTableStatement;
3943
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateDatabaseStatement;
4044
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateFunctionStatement;
45+
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateIndexStatement;
4146
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateTableStatement;
4247
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DDLStatement;
4348
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropDatabaseStatement;
49+
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropIndexStatement;
4450
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropTableStatement;
4551
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.TruncateStatement;
4652
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.DMLStatement;
@@ -102,7 +108,7 @@ private static ACLOperation extractDDL(final DDLStatement sqlStatement) {
102108
if (sqlStatement instanceof AlterDatabaseStatement) {
103109
return ACLOperation.ALTER_ANY_DATABASE;
104110
}
105-
if (sqlStatement instanceof AlterTableStatement) {
111+
if (sqlStatement instanceof AlterTableStatement || isIndexDDLStatement(sqlStatement)) {
106112
return ACLOperation.ALTER;
107113
}
108114
if (sqlStatement instanceof CreateFunctionStatement) {
@@ -114,6 +120,10 @@ private static ACLOperation extractDDL(final DDLStatement sqlStatement) {
114120
return ACLOperation.UNKNOWN;
115121
}
116122

123+
private static boolean isIndexDDLStatement(final SQLStatement sqlStatement) {
124+
return sqlStatement instanceof CreateIndexStatement || sqlStatement instanceof AlterIndexStatement || sqlStatement instanceof DropIndexStatement;
125+
}
126+
117127
private static ACLOperation extractDistSQL(final DistSQLStatement sqlStatement) {
118128
if (sqlStatement instanceof CreateRuleStatement) {
119129
return ACLOperation.CREATE_RULE;
@@ -127,7 +137,7 @@ private static ACLOperation extractDistSQL(final DistSQLStatement sqlStatement)
127137
if (sqlStatement instanceof ShowDatabaseRulesStatement || sqlStatement instanceof ShowStorageUnitsStatement) {
128138
return ACLOperation.SHOW_RULES;
129139
}
130-
if (sqlStatement instanceof RegisterStorageUnitStatement) {
140+
if (sqlStatement instanceof RegisterStorageUnitStatement || sqlStatement instanceof AlterStorageUnitStatement) {
131141
return ACLOperation.REGISTER;
132142
}
133143
if (sqlStatement instanceof UnregisterStorageUnitStatement) {
@@ -145,7 +155,7 @@ private static ACLOperation extractDistSQL(final DistSQLStatement sqlStatement)
145155
if (sqlStatement instanceof ParseStatement) {
146156
return ACLOperation.PARSE;
147157
}
148-
if (sqlStatement instanceof RALStatement) {
158+
if (sqlStatement instanceof RALStatement || sqlStatement instanceof ShowGlobalRulesStatement || sqlStatement instanceof GlobalRuleDefinitionStatement) {
149159
return ACLOperation.RAL_OPERATE;
150160
}
151161
return ACLOperation.UNKNOWN;

kernel/authority/distsql/handler/src/test/resources/cases/show-dist-users-current-config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ rules:
2121
users:
2222
- user: root@%
2323
password: 123456
24+
admin: true
2425
#SPEX ADDED: END

kernel/authority/distsql/handler/src/test/resources/cases/show-dist-users.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<distsql-rule-query-executor-test-cases>
2020
<test-case dist-sql="SHOW DIST USERS" current-rule-config-yaml-file="cases/show-dist-users-current-config.yaml">
2121
<expected-query-result-rows>
22-
<expected-query-result-row>%|root</expected-query-result-row>
22+
<expected-query-result-row>%|root|Y</expected-query-result-row>
2323
</expected-query-result-rows>
2424
</test-case>
2525
</distsql-rule-query-executor-test-cases>

0 commit comments

Comments
 (0)