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. plainColumn (?): # Plain column name
  8. cipherColumn: # Cipher column name
  9. encryptorName: # Cipher encrypt algorithm name
  10. assistedQueryColumn (?): # Assisted query column name
  11. assistedQueryEncryptorName: # Assisted query encrypt algorithm name
  12. likeQueryColumn (?): # Like query column name
  13. likeQueryEncryptorName: # Like query encrypt algorithm name
  14. queryWithCipherColumn(?): # The current table whether query with cipher column for data encrypt.
  15. # Encrypt algorithm configuration
  16. encryptors:
  17. <encrypt_algorithm_name> (+): # Encrypt algorithm name
  18. type: # Encrypt algorithm type
  19. props: # Encrypt algorithm properties
  20. # ...
  21. queryWithCipherColumn: # Whether query with cipher column for data encrypt. User you can use plaintext to query if have

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. plainColumn: username_plain
  15. cipherColumn: username
  16. encryptorName: name_encryptor
  17. assistedQueryColumn: assisted_query_username
  18. assistedQueryEncryptorName: assisted_encryptor
  19. likeQueryColumn: like_query_username
  20. likeQueryEncryptorName: like_encryptor
  21. pwd:
  22. cipherColumn: pwd
  23. encryptorName: pwd_encryptor
  24. assistedQueryColumn: assisted_query_pwd
  25. assistedQueryEncryptorName: assisted_encryptor
  26. queryWithCipherColumn: true
  27. encryptors:
  28. name_encryptor:
  29. type: AES
  30. props:
  31. aes-key-value: 123456abc
  32. assisted_encryptor:
  33. type: AES
  34. props:
  35. aes-key-value: 123456abc
  36. like_encryptor:
  37. type: CHAR_DIGEST_LIKE
  38. pwd_encryptor:
  39. type: MD5

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

  1. YamlShardingSphereDataSourceFactory.createDataSource(getFile());