Skip to content

Commit

Permalink
Optimize error code for encrypt. (apache#23335)
Browse files Browse the repository at this point in the history
* Optimize error code for encrypt.

* Optimize error code for encrypt.
  • Loading branch information
tuichenchuxin authored Jan 5, 2023
1 parent 372c93a commit 12bcb2f
Show file tree
Hide file tree
Showing 14 changed files with 293 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@

package org.apache.shardingsphere.encrypt.checker;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnRuleConfiguration;
import org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
import org.apache.shardingsphere.encrypt.constant.EncryptOrder;
import org.apache.shardingsphere.encrypt.exception.metadata.EncryptAssistedQueryColumnNotFoundException;
import org.apache.shardingsphere.encrypt.exception.metadata.EncryptAssistedQueryEncryptorNotFoundException;
import org.apache.shardingsphere.encrypt.exception.metadata.EncryptCipherColumnNotFoundException;
import org.apache.shardingsphere.encrypt.exception.metadata.EncryptEncryptorNotFoundException;
import org.apache.shardingsphere.encrypt.exception.metadata.EncryptLikeQueryColumnNotFoundException;
import org.apache.shardingsphere.encrypt.exception.metadata.EncryptLikeQueryEncryptorNotFoundException;
import org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationChecker;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;

import javax.sql.DataSource;
import java.util.Collection;
Expand All @@ -51,36 +57,33 @@ private void checkTableConfiguration(final String databaseName, final Collection
}

private void checkCipherColumnConfiguration(final String databaseName, final Collection<String> encryptors, final EncryptColumnRuleConfiguration column) {
Preconditions.checkState(!Strings.isNullOrEmpty(column.getCipherColumn()),
"Cipher column of `%s` can not be null in database `%s`.", column.getLogicColumn(), databaseName);
Preconditions.checkState(!Strings.isNullOrEmpty(column.getEncryptorName()),
"Encryptor name of `%s` can not be null in database `%s`.", column.getLogicColumn(), databaseName);
Preconditions.checkState(encryptors.contains(column.getEncryptorName()),
"Can not find encryptor `%s` in database `%s`.", column.getEncryptorName(), databaseName);
ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(column.getCipherColumn()), () -> new EncryptCipherColumnNotFoundException(column.getLogicColumn(), databaseName));
ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(column.getEncryptorName()),
() -> new EncryptEncryptorNotFoundException(String.format("Encryptor name of `%s` can not be null in database `%s`.", column.getLogicColumn(), databaseName)));
ShardingSpherePreconditions.checkState(encryptors.contains(column.getEncryptorName()),
() -> new EncryptEncryptorNotFoundException(String.format("Can not find encryptor `%s` in database `%s`.", column.getEncryptorName(), databaseName)));
}

private void checkAssistColumnConfiguration(final String databaseName, final Collection<String> encryptors, final EncryptColumnRuleConfiguration column) {
if (Strings.isNullOrEmpty(column.getAssistedQueryColumn()) && Strings.isNullOrEmpty(column.getAssistedQueryEncryptorName())) {
return;
}
Preconditions.checkState(!Strings.isNullOrEmpty(column.getAssistedQueryColumn()),
"Assisted query column of `%s` can not be null in database `%s`.", column.getLogicColumn(), databaseName);
Preconditions.checkState(!Strings.isNullOrEmpty(column.getAssistedQueryEncryptorName()),
"Assisted query encryptor name of `%s` can not be null in database `%s`.", column.getLogicColumn(), databaseName);
Preconditions.checkState(encryptors.contains(column.getAssistedQueryEncryptorName()),
"Can not find assisted query encryptor `%s` in database `%s`.", column.getAssistedQueryEncryptorName(), databaseName);
ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(column.getAssistedQueryColumn()), () -> new EncryptAssistedQueryColumnNotFoundException(column.getLogicColumn(), databaseName));
ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(column.getAssistedQueryEncryptorName()), () -> new EncryptAssistedQueryEncryptorNotFoundException(
String.format("Assisted query encryptor name of `%s` can not be null in database `%s`.", column.getLogicColumn(), databaseName)));
ShardingSpherePreconditions.checkState(encryptors.contains(column.getAssistedQueryEncryptorName()), () -> new EncryptAssistedQueryEncryptorNotFoundException(
String.format("Can not find assisted query encryptor `%s` in database `%s`.", column.getAssistedQueryEncryptorName(), databaseName)));
}

private void checkLikeColumnConfiguration(final String databaseName, final Collection<String> encryptors, final EncryptColumnRuleConfiguration column) {
if (Strings.isNullOrEmpty(column.getLikeQueryColumn()) && Strings.isNullOrEmpty(column.getLikeQueryEncryptorName())) {
return;
}
Preconditions.checkState(!Strings.isNullOrEmpty(column.getLikeQueryColumn()),
"Like query column of `%s` can not be null in database `%s`.", column.getLogicColumn(), databaseName);
Preconditions.checkState(!Strings.isNullOrEmpty(column.getLikeQueryEncryptorName()),
"Like query encryptor name of `%s` can not be null in database `%s`.", column.getLogicColumn(), databaseName);
Preconditions.checkState(encryptors.contains(column.getLikeQueryEncryptorName()),
"Can not find like query encryptor `%s` in database `%s`.", column.getLikeQueryEncryptorName(), databaseName);
ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(column.getLikeQueryColumn()), () -> new EncryptLikeQueryColumnNotFoundException(column.getLogicColumn(), databaseName));
ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(column.getLikeQueryEncryptorName()),
() -> new EncryptLikeQueryEncryptorNotFoundException(String.format("Like query encryptor name of `%s` can not be null in database `%s`.", column.getLogicColumn(), databaseName)));
ShardingSpherePreconditions.checkState(encryptors.contains(column.getLikeQueryEncryptorName()),
() -> new EncryptLikeQueryEncryptorNotFoundException(String.format("Can not find like query encryptor `%s` in database `%s`.", column.getLikeQueryEncryptorName(), databaseName)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.
*/

package org.apache.shardingsphere.encrypt.exception.metadata;

import org.apache.shardingsphere.encrypt.exception.EncryptSQLException;
import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;

/**
* Encrypt assisted query column not found exception.
*/
public final class EncryptAssistedQueryColumnNotFoundException extends EncryptSQLException {

private static final long serialVersionUID = -4958403725374225598L;

public EncryptAssistedQueryColumnNotFoundException(final String logicColumnName, final String databaseName) {
super(XOpenSQLState.CHECK_OPTION_VIOLATION, 5, "Assisted query column of `%s` can not be null in database `%s`.", logicColumnName, databaseName);
}

public EncryptAssistedQueryColumnNotFoundException() {
super(XOpenSQLState.CHECK_OPTION_VIOLATION, 5, "Can not find assisted query column Name.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.
*/

package org.apache.shardingsphere.encrypt.exception.metadata;

import org.apache.shardingsphere.encrypt.exception.EncryptSQLException;
import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;

/**
* Encrypt assisted query encryptor not found exception.
*/
public final class EncryptAssistedQueryEncryptorNotFoundException extends EncryptSQLException {

private static final long serialVersionUID = -8700683634804933320L;

public EncryptAssistedQueryEncryptorNotFoundException(final String reason) {
super(XOpenSQLState.CHECK_OPTION_VIOLATION, 6, reason);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.
*/

package org.apache.shardingsphere.encrypt.exception.metadata;

import org.apache.shardingsphere.encrypt.exception.EncryptSQLException;
import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;

/**
* Encrypt cipher column not found exception.
*/
public final class EncryptCipherColumnNotFoundException extends EncryptSQLException {

private static final long serialVersionUID = -6765795304282762539L;

public EncryptCipherColumnNotFoundException(final String logicColumnName, final String databaseName) {
super(XOpenSQLState.CHECK_OPTION_VIOLATION, 3, "Cipher column of `%s` can not be null in database `%s`.", logicColumnName, databaseName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.
*/

package org.apache.shardingsphere.encrypt.exception.metadata;

import org.apache.shardingsphere.encrypt.exception.EncryptSQLException;
import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;

/**
* Encrypt encryptor not found exception.
*/
public final class EncryptEncryptorNotFoundException extends EncryptSQLException {

private static final long serialVersionUID = -4847495252826650747L;

public EncryptEncryptorNotFoundException(final String reason) {
super(XOpenSQLState.CHECK_OPTION_VIOLATION, 4, reason);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.
*/

package org.apache.shardingsphere.encrypt.exception.metadata;

import org.apache.shardingsphere.encrypt.exception.EncryptSQLException;
import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;

/**
* Encrypt like query column not found exception.
*/
public final class EncryptLikeQueryColumnNotFoundException extends EncryptSQLException {

private static final long serialVersionUID = -1628043793867162797L;

public EncryptLikeQueryColumnNotFoundException(final String logicColumnName, final String databaseName) {
super(XOpenSQLState.CHECK_OPTION_VIOLATION, 7, "Like query column of `%s` can not be null in database `%s`.", logicColumnName, databaseName);
}

public EncryptLikeQueryColumnNotFoundException() {
super(XOpenSQLState.CHECK_OPTION_VIOLATION, 7, "Can not find like query column Name.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.
*/

package org.apache.shardingsphere.encrypt.exception.metadata;

import org.apache.shardingsphere.encrypt.exception.EncryptSQLException;
import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;

/**
* Encrypt like query encryptor not found exception.
*/
public final class EncryptLikeQueryEncryptorNotFoundException extends EncryptSQLException {

private static final long serialVersionUID = 1620586484949188815L;

public EncryptLikeQueryEncryptorNotFoundException(final String reason) {
super(XOpenSQLState.CHECK_OPTION_VIOLATION, 8, reason);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.
*/

package org.apache.shardingsphere.encrypt.exception.metadata;

import org.apache.shardingsphere.encrypt.exception.EncryptSQLException;
import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;

/**
* Encrypt table not found exception.
*/
public final class EncryptTableNotFoundException extends EncryptSQLException {

private static final long serialVersionUID = 8909641495852822938L;

public EncryptTableNotFoundException(final String tableName) {
super(XOpenSQLState.CHECK_OPTION_VIOLATION, 9, "Can not find encrypt table: %s", tableName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@

package org.apache.shardingsphere.encrypt.merge.dal.show;

import com.google.common.base.Preconditions;
import org.apache.shardingsphere.encrypt.exception.syntax.UnsupportedEncryptSQLException;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.encrypt.rule.EncryptTable;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.type.TableAvailable;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;

import java.io.InputStream;
import java.sql.SQLException;
Expand All @@ -42,7 +43,8 @@ public abstract class EncryptShowColumnsMergedResult implements MergedResult {
private final EncryptRule encryptRule;

protected EncryptShowColumnsMergedResult(final SQLStatementContext<?> sqlStatementContext, final EncryptRule encryptRule) {
Preconditions.checkState(sqlStatementContext instanceof TableAvailable && 1 == ((TableAvailable) sqlStatementContext).getAllTables().size());
ShardingSpherePreconditions.checkState(sqlStatementContext instanceof TableAvailable && 1 == ((TableAvailable) sqlStatementContext).getAllTables().size(),
() -> new UnsupportedEncryptSQLException("SHOW COLUMNS FOR MULTI TABLE"));
tableName = ((TableAvailable) sqlStatementContext).getAllTables().iterator().next().getTableName().getIdentifier().getValue();
this.encryptRule = encryptRule;
}
Expand Down
Loading

0 comments on commit 12bcb2f

Please sign in to comment.