This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new c5d2a55eab7 Throw DecryptFailedException when a decryption error
occurs. (#30900)
c5d2a55eab7 is described below
commit c5d2a55eab7342c4ba12b2d59ffb0b8c29bd58fc
Author: Cong Hu <[email protected]>
AuthorDate: Wed Apr 17 11:21:52 2024 +0800
Throw DecryptFailedException when a decryption error occurs. (#30900)
* Throw DecryptFailedException when a decryption error occurs.
* Modify the code according to the suggestions from the code review.
* Add the caught exception to the cause of DecryptFailedException.
* Remove useless annotation.
---
.../user-manual/error-code/sql-error-code.cn.md | 1 +
.../user-manual/error-code/sql-error-code.en.md | 2 ++
.../encrypt/exception/EncryptSQLException.java | 4 ++++
.../DecryptFailedException.java} | 19 +++++++++----------
.../encrypt/merge/dql/EncryptMergedResult.java | 10 +++++++++-
.../core/external/sql/ShardingSphereSQLException.java | 8 ++++++++
.../core/external/sql/sqlstate/XOpenSQLState.java | 2 ++
.../sql/type/feature/FeatureSQLException.java | 4 ++++
8 files changed, 39 insertions(+), 11 deletions(-)
diff --git a/docs/document/content/user-manual/error-code/sql-error-code.cn.md
b/docs/document/content/user-manual/error-code/sql-error-code.cn.md
index 0af74654c8d..8a1f6354f96 100644
--- a/docs/document/content/user-manual/error-code/sql-error-code.cn.md
+++ b/docs/document/content/user-manual/error-code/sql-error-code.cn.md
@@ -224,6 +224,7 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
| 21004 | HY000 | '%s' column's encrypt algorithm '%s' should
support %s in database '%s'. |
| 21010 | 44000 | Altered column '%s' must use same encrypt
algorithm with previous column '%s' in table '%s'. |
| 21020 | 0A000 | The SQL clause '%s' is unsupported in encrypt
feature. |
+| 21030 | 22000 | Failed to decrypt the ciphertext '%s' in the
column '%s' of table '%s'. |
### 影子库
diff --git a/docs/document/content/user-manual/error-code/sql-error-code.en.md
b/docs/document/content/user-manual/error-code/sql-error-code.en.md
index 20d85a7fbb9..8a15a3c76f4 100644
--- a/docs/document/content/user-manual/error-code/sql-error-code.en.md
+++ b/docs/document/content/user-manual/error-code/sql-error-code.en.md
@@ -224,6 +224,8 @@ SQL error codes provide by standard `SQL State`, `Vendor
Code` and `Reason`, whi
| 21004 | HY000 | '%s' column's encrypt algorithm '%s' should
support %s in database '%s'. |
| 21010 | 44000 | Altered column '%s' must use same encrypt
algorithm with previous column '%s' in table '%s'. |
| 21020 | 0A000 | The SQL clause '%s' is unsupported in encrypt
feature. |
+| 21030 | 22000 | Failed to decrypt the ciphertext '%s' in the
column '%s' of table '%s'. |
+
### Shadow Database
diff --git
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/exception/EncryptSQLException.java
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/exception/EncryptSQLException.java
index 39518bed1c7..3ab086f2ea3 100644
---
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/exception/EncryptSQLException.java
+++
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/exception/EncryptSQLException.java
@@ -32,4 +32,8 @@ public abstract class EncryptSQLException extends
FeatureSQLException {
protected EncryptSQLException(final SQLState sqlState, final int
errorCode, final String reason, final Object... messageArgs) {
super(sqlState, FEATURE_CODE, errorCode, reason, messageArgs);
}
+
+ protected EncryptSQLException(final SQLState sqlState, final int
errorCode, final Exception cause, final String reason, final Object...
messageArgs) {
+ super(sqlState, FEATURE_CODE, errorCode, cause, reason, messageArgs);
+ }
}
diff --git
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/exception/EncryptSQLException.java
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/exception/data/DecryptFailedException.java
similarity index 60%
copy from
features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/exception/EncryptSQLException.java
copy to
features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/exception/data/DecryptFailedException.java
index 39518bed1c7..1de68fc2871 100644
---
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/exception/EncryptSQLException.java
+++
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/exception/data/DecryptFailedException.java
@@ -15,21 +15,20 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.encrypt.exception;
+package org.apache.shardingsphere.encrypt.exception.data;
-import
org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.SQLState;
-import
org.apache.shardingsphere.infra.exception.core.external.sql.type.feature.FeatureSQLException;
+import org.apache.shardingsphere.encrypt.exception.EncryptSQLException;
+import
org.apache.shardingsphere.infra.exception.core.external.sql.identifier.SQLExceptionIdentifier;
+import
org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.XOpenSQLState;
/**
- * Encrypt SQL exception.
+ * Decrypt failed exception.
*/
-public abstract class EncryptSQLException extends FeatureSQLException {
+public final class DecryptFailedException extends EncryptSQLException {
- private static final long serialVersionUID = -8616403083235617682L;
+ private static final long serialVersionUID = 1122241238288845667L;
- private static final int FEATURE_CODE = 10;
-
- protected EncryptSQLException(final SQLState sqlState, final int
errorCode, final String reason, final Object... messageArgs) {
- super(sqlState, FEATURE_CODE, errorCode, reason, messageArgs);
+ public DecryptFailedException(final String ciphertext, final
SQLExceptionIdentifier sqlExceptionIdentifier, final Exception cause) {
+ super(XOpenSQLState.DATA_EXCEPTION, 30, cause, "Failed to decrypt the
ciphertext '%s' in '%s'.", ciphertext, sqlExceptionIdentifier);
}
}
diff --git
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptMergedResult.java
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptMergedResult.java
index 32724b912c6..a7b7141385e 100644
---
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptMergedResult.java
+++
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptMergedResult.java
@@ -18,11 +18,13 @@
package org.apache.shardingsphere.encrypt.merge.dql;
import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.encrypt.exception.data.DecryptFailedException;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.encrypt.rule.column.EncryptColumn;
import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.impl.ColumnProjection;
import
org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
+import
org.apache.shardingsphere.infra.exception.core.external.sql.identifier.SQLExceptionIdentifier;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
@@ -66,7 +68,13 @@ public final class EncryptMergedResult implements
MergedResult {
EncryptColumn encryptColumn =
encryptRule.getEncryptTable(originalTableName).getEncryptColumn(originalColumnName);
String schemaName =
selectStatementContext.getTablesContext().getSchemaName().orElseGet(() -> new
DatabaseTypeRegistry(selectStatementContext.getDatabaseType()).getDefaultSchemaName(database.getName()));
- return encryptColumn.getCipher().decrypt(database.getName(),
schemaName, originalTableName, originalColumnName, cipherValue);
+ try {
+ return encryptColumn.getCipher().decrypt(database.getName(),
schemaName, originalTableName, originalColumnName, cipherValue);
+ // CHECKSTYLE:OFF
+ } catch (final Exception ex) {
+ // CHECKSTYLE:ON
+ throw new DecryptFailedException(String.valueOf(cipherValue), new
SQLExceptionIdentifier(database.getName(), originalTableName,
originalColumnName), ex);
+ }
}
@Override
diff --git
a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/ShardingSphereSQLException.java
b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/ShardingSphereSQLException.java
index 7eb06f71d4a..4e790a63add 100644
---
a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/ShardingSphereSQLException.java
+++
b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/ShardingSphereSQLException.java
@@ -46,10 +46,18 @@ public abstract class ShardingSphereSQLException extends
ShardingSphereExternalE
this(sqlState.getValue(), typeOffset, errorCode, reason, messageArgs);
}
+ protected ShardingSphereSQLException(final SQLState sqlState, final int
typeOffset, final int errorCode, final Exception cause, final String reason,
final Object... messageArgs) {
+ this(sqlState.getValue(), typeOffset, errorCode, cause, reason,
messageArgs);
+ }
+
protected ShardingSphereSQLException(final String sqlState, final int
typeOffset, final int errorCode, final String reason, final Object...
messageArgs) {
this(sqlState, typeOffset, errorCode, null == reason ? null :
String.format(reason, formatMessageArguments(messageArgs)), (Exception) null);
}
+ protected ShardingSphereSQLException(final String sqlState, final int
typeOffset, final int errorCode, final Exception cause, final String reason,
final Object... messageArgs) {
+ this(sqlState, typeOffset, errorCode, null == reason ? null :
String.format(reason, formatMessageArguments(messageArgs)), cause);
+ }
+
protected ShardingSphereSQLException(final String sqlState, final int
typeOffset, final int errorCode, final String reason, final Exception cause) {
super(reason, cause);
this.sqlState = sqlState;
diff --git
a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/sqlstate/XOpenSQLState.java
b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/sqlstate/XOpenSQLState.java
index e1b80647525..f23d1c3c2f6 100644
---
a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/sqlstate/XOpenSQLState.java
+++
b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/sqlstate/XOpenSQLState.java
@@ -43,6 +43,8 @@ public enum XOpenSQLState implements SQLState {
MISMATCH_INSERT_VALUES_AND_COLUMNS("21S01"),
+ DATA_EXCEPTION("22000"),
+
INVALID_PARAMETER_VALUE("22023"),
INVALID_TRANSACTION_STATE("25000"),
diff --git
a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/feature/FeatureSQLException.java
b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/feature/FeatureSQLException.java
index 340003b3ed3..53c43984a5b 100644
---
a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/feature/FeatureSQLException.java
+++
b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/feature/FeatureSQLException.java
@@ -34,6 +34,10 @@ public abstract class FeatureSQLException extends
ShardingSphereSQLException {
super(sqlState, TYPE_OFFSET, getErrorCode(featureCode, errorCode),
reason, messageArgs);
}
+ protected FeatureSQLException(final SQLState sqlState, final int
featureCode, final int errorCode, final Exception cause, final String reason,
final Object... messageArgs) {
+ super(sqlState, TYPE_OFFSET, getErrorCode(featureCode, errorCode),
cause, reason, messageArgs);
+ }
+
private static int getErrorCode(final int featureCode, final int
errorCode) {
Preconditions.checkArgument(featureCode >= 0 && featureCode < 100,
"The value range of feature code should be [0, 100).");
Preconditions.checkArgument(errorCode >= 0 && errorCode < 100, "The
value range of error code should be [0, 100).");