Data Masking

Background

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

Parameters

  1. rules:
  2. - !MASK
  3. tables:
  4. <table_name> (+): # Mask table name
  5. columns:
  6. <column_name> (+): # Mask logic column name
  7. maskAlgorithm: # Mask algorithm name
  8. # Mask algorithm configuration
  9. maskAlgorithms:
  10. <mask_algorithm_name> (+): # Mask algorithm name
  11. type: # Mask algorithm type
  12. props: # Mask algorithm properties
  13. # ...

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

Procedure

  1. Configure data masking rules in the YAML file, including data sources, mask 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 masking 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. - !MASK
  10. tables:
  11. t_user:
  12. columns:
  13. password:
  14. maskAlgorithm: md5_mask
  15. email:
  16. maskAlgorithm: mask_before_special_chars_mask
  17. telephone:
  18. maskAlgorithm: keep_first_n_last_m_mask
  19. maskAlgorithms:
  20. md5_mask:
  21. type: MD5
  22. mask_before_special_chars_mask:
  23. type: MASK_BEFORE_SPECIAL_CHARS
  24. props:
  25. special-chars: '@'
  26. replace-char: '*'
  27. keep_first_n_last_m_mask:
  28. type: KEEP_FIRST_N_LAST_M
  29. props:
  30. first-n: 3
  31. last-m: 4
  32. replace-char: '*'

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

  1. YamlShardingSphereDataSourceFactory.createDataSource(getFile());