Encryption

Background

The data encryption Java API rule configuration allows users to directly create ShardingSphereDataSource objects by writing java code. The Java API configuration method is very flexible and can integrate various types of business systems without relying on additional jar packages.

Parameters

Root Configuration

Class name: org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration

Attributes:

NameDataTypeDescriptionDefault Value
tables (+)Collection<EncryptTableRuleConfiguration>Encrypt table rule configurations
encryptors (+)Map<String, AlgorithmConfiguration>Encrypt algorithm name and configurations

Encrypt Table Rule Configuration

Class name: org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration

Attributes:

NameDataTypeDescription
nameStringTable name
columns (+)Collection<EncryptColumnRuleConfiguration>Encrypt column rule configurations

Encrypt Column Rule Configuration

Class name: org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnRuleConfiguration

Attributes:

NameDataTypeDescription
nameStringLogic column name
cipherEncryptColumnItemRuleConfigurationCipher column config
assistedQuery (?)EncryptColumnItemRuleConfigurationAssisted query column config
likeQuery (?)EncryptColumnItemRuleConfigurationLike query column config

Encrypt Column Item Rule Configuration

Class name: org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnItemRuleConfiguration

Attributes:

NameDataTypeDescription
nameStringencrypt column item name
encryptorNameStringencryptor name

Encrypt Algorithm Configuration

Class name: org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration

Attributes:

NameDataTypeDescription
nameStringEncrypt algorithm name
typeStringEncrypt algorithm type
propertiesPropertiesEncrypt algorithm properties

Please refer to Built-in Encrypt Algorithm List for more details about type of algorithm.

Procedure

  1. Create a real data source mapping relationship, where key is the logical name of the data source and value is the datasource object.
  2. Create the encryption rule object EncryptRuleConfiguration, and initialize the encryption table object EncryptTableRuleConfiguration, encryption algorithm and other parameters in the object.
  3. Call createDataSource of ShardingSphereDataSourceFactory to create ShardingSphereDataSource.

Sample

  1. public final class EncryptDatabasesConfiguration {
  2. public DataSource getDataSource() throws SQLException {
  3. Properties props = new Properties();
  4. props.setProperty("aes-key-value", "123456");
  5. EncryptColumnRuleConfiguration columnConfigAes = new EncryptColumnRuleConfiguration("username", new EncryptColumnItemRuleConfiguration("username", "name_encryptor"));
  6. EncryptColumnRuleConfiguration columnConfigTest = new EncryptColumnRuleConfiguration("pwd", new EncryptColumnItemRuleConfiguration("pwd", "pwd_encryptor"));
  7. columnConfigTest.setAssistedQuery(new EncryptColumnItemRuleConfiguration("assisted_query_pwd", "pwd_encryptor"));
  8. columnConfigTest.setLikeQuery(new EncryptColumnItemRuleConfiguration("like_pwd", "like_encryptor"));
  9. EncryptTableRuleConfiguration encryptTableRuleConfig = new EncryptTableRuleConfiguration("t_user", Arrays.asList(columnConfigAes, columnConfigTest));
  10. Map<String, AlgorithmConfiguration> encryptAlgorithmConfigs = new HashMap<>();
  11. encryptAlgorithmConfigs.put("name_encryptor", new AlgorithmConfiguration("AES", props));
  12. encryptAlgorithmConfigs.put("pwd_encryptor", new AlgorithmConfiguration("assistedTest", props));
  13. encryptAlgorithmConfigs.put("like_encryptor", new AlgorithmConfiguration("CHAR_DIGEST_LIKE", new Properties()));
  14. EncryptRuleConfiguration encryptRuleConfig = new EncryptRuleConfiguration(Collections.singleton(encryptTableRuleConfig), encryptAlgorithmConfigs);
  15. return ShardingSphereDataSourceFactory.createDataSource(DataSourceUtil.createDataSource("demo_ds"), Collections.singleton(encryptRuleConfig), props);
  16. }
  17. }