Encryption

Background

The YAML configuration approach to data encryption is highly readable, with the YAML format enabling a quick understanding of dependencies between encryption rules. Based on the YAML configuration, ShardingSphere automatically completes the creation of ShardingSphereDataSource objects, reducing unnecessary coding efforts for users.

Parameters

  1. rules:
  2. - !ENCRYPT
  3. tables:
  4. <table_name> (+): # Encrypt table name
  5. columns:
  6. <column_name> (+): # Encrypt logic column name
  7. cipher:
  8. name: # Cipher column name
  9. encryptorName: # Cipher encrypt algorithm name
  10. assistedQuery (?):
  11. name: # Assisted query column name
  12. encryptorName: # Assisted query encrypt algorithm name
  13. likeQuery (?):
  14. name: # Like query column name
  15. encryptorName: # Like query encrypt algorithm name
  16. # Encrypt algorithm configuration
  17. encryptors:
  18. <encrypt_algorithm_name> (+): # Encrypt algorithm name
  19. type: # Encrypt algorithm type
  20. props: # Encrypt algorithm properties
  21. # ...

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

Procedure

  1. Configure data encryption rules in the YAML file, including data sources, encryption rules, global attributes, and other configuration items.
  2. Using the createDataSource of calling the YamlShardingSphereDataSourceFactory object to create ShardingSphereDataSource based on the configuration information in the YAML file.

Sample

The data encryption YAML configurations are as follows:

  1. dataSources:
  2. unique_ds:
  3. dataSourceClassName: com.zaxxer.hikari.HikariDataSource
  4. driverClassName: com.mysql.jdbc.Driver
  5. jdbcUrl: jdbc:mysql://localhost:3306/demo_ds?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
  6. username: root
  7. password:
  8. rules:
  9. - !ENCRYPT
  10. tables:
  11. t_user:
  12. columns:
  13. username:
  14. cipher:
  15. name: username
  16. encryptorName: aes_encryptor
  17. assistedQuery:
  18. name: assisted_query_username
  19. encryptorName: assisted_encryptor
  20. likeQuery:
  21. name: like_query_username
  22. encryptorName: like_encryptor
  23. pwd:
  24. cipher:
  25. name: pwd
  26. encryptorName: aes_encryptor
  27. assistedQuery:
  28. name: assisted_query_pwd
  29. encryptorName: assisted_encryptor
  30. encryptors:
  31. aes_encryptor:
  32. type: AES
  33. props:
  34. aes-key-value: 123456abc
  35. assisted_encryptor:
  36. type: MD5
  37. like_encryptor:
  38. type: CHAR_DIGEST_LIKE

Read the YAML configuration to create a data source according to the createDataSource method of YamlShardingSphereDataSourceFactory.

  1. YamlShardingSphereDataSourceFactory.createDataSource(getFile());

In order to keep compatibility with earlier YAML configuration, ShardingSphere provides following compatible configuration through ‘COMPATIBLE_ENCRYPT’, which will be removed in future versions, and it is recommended to upgrade latest YAML configuration.

  1. dataSources:
  2. unique_ds:
  3. dataSourceClassName: com.zaxxer.hikari.HikariDataSource
  4. driverClassName: com.mysql.jdbc.Driver
  5. jdbcUrl: jdbc:mysql://localhost:3306/demo_ds?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
  6. username: root
  7. password:
  8. rules:
  9. - !COMPATIBLE_ENCRYPT
  10. tables:
  11. t_user:
  12. columns:
  13. username:
  14. cipherColumn: username
  15. encryptorName: aes_encryptor
  16. assistedQueryColumn: assisted_query_username
  17. assistedQueryEncryptorName: assisted_encryptor
  18. likeQueryColumn: like_query_username
  19. likeQueryEncryptorName: like_encryptor
  20. pwd:
  21. cipherColumn: pwd
  22. encryptorName: aes_encryptor
  23. assistedQueryColumn: assisted_query_pwd
  24. assistedQueryEncryptorName: assisted_encryptor
  25. encryptors:
  26. aes_encryptor:
  27. type: AES
  28. props:
  29. aes-key-value: 123456abc
  30. assisted_encryptor:
  31. type: MD5
  32. like_encryptor:
  33. type: CHAR_DIGEST_LIKE