Skip to content

Commit 053b485

Browse files
authored
Revise masking policy documentation for clarity
Updated the masking policy documentation to clarify how masking policies protect sensitive data and their implementation details.
1 parent d69cf09 commit 053b485

File tree

1 file changed

+57
-47
lines changed

1 file changed

+57
-47
lines changed

docs/en/guides/56-security/masking-policy.md

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,75 +6,85 @@ import EEFeature from '@site/src/components/EEFeature';
66

77
<EEFeature featureName='MASKING POLICY'/>
88

9-
A masking policy refers to rules and settings that control the display or access to sensitive data in a way that safeguards confidentiality while allowing authorized users to interact with the data. Databend enables you to define masking policies for displaying sensitive columns in a table, thus protecting confidential data while still permitting authorized roles to access specific parts of the data.
9+
Masking policies protect sensitive data by dynamically transforming column values during query execution. They enable role-based access to confidential information—authorized users see actual data, while others see masked values.
1010

11-
To illustrate, consider a scenario where you want to present email addresses in a table exclusively to managers:
11+
## How It Works
1212

13+
Masking policies apply transformation expressions to column data based on the current user's role:
14+
15+
**For managers:**
1316
```sql
1417
id | email |
1518
---|-----------------|
1619
2 | eric@example.com|
1720
1 | sue@example.com |
1821
```
1922

20-
And when non-manager users query the table, the email addresses would appear as:
21-
23+
**For other users:**
2224
```sql
23-
id|email |
24-
--+---------+
25-
2|*********|
26-
1|*********|
25+
id | email |
26+
---|----------|
27+
2 | *********|
28+
1 | *********|
2729
```
2830

29-
### Implementing Masking Policy
31+
## Key Characteristics
3032

31-
Before creating a masking policy, make sure you have properly defined or planned user roles and their corresponding access privileges, as the policy's implementation relies on these roles to ensure secure and effective data masking. To manage Databend users and roles, see [User & Role](/sql/sql-commands/ddl/user/).
33+
- **Query-time masking**: Policies transform data during SELECT operations only
34+
- **Role-based**: Access rules depend on the current user's role using `current_role()`
35+
- **Column-level**: Applied to specific table columns
36+
- **Reusable**: One policy can protect multiple columns across different tables
37+
- **Non-intrusive**: Original data remains unchanged in storage
3238

33-
Masking policies are applied to the columns of a table. To implement a masking policy for a specific column, you must first create the masking policy and then associate the policy to the intended column with the [ALTER TABLE COLUMN](/sql/sql-commands/ddl/table/alter-table-column) command. By establishing this association, the masking policy becomes tailored to the exact context where data privacy is paramount. It's important to note that a single masking policy can be associated with multiple columns, as long as they align with the same policy criteria. For commands used to manage masking policies in Databend, see [Masking Policy](/sql/sql-commands/ddl/mask-policy/).
39+
## Read vs Write Operations
3440

35-
### Usage Examples
41+
**Important**: Masking policies **only apply to read operations** (SELECT queries). Write operations (INSERT, UPDATE, DELETE) always process original, unmasked data. This ensures:
3642

37-
This example illustrates the process of setting up a masking policy to selectively reveal or mask sensitive data based on user roles.
43+
- Query results are protected based on user permissions
44+
- Applications can store and modify actual data values
45+
- Data integrity is maintained in the underlying storage
3846

39-
```sql
40-
-- Create a table and insert sample data
41-
CREATE TABLE user_info (
42-
id INT,
43-
email STRING
44-
);
47+
## Quick Start
4548

46-
INSERT INTO user_info (id, email) VALUES (1, '[email protected]');
47-
INSERT INTO user_info (id, email) VALUES (2, '[email protected]');
49+
### 1. Create a Masking Policy
4850

49-
-- Create a role
50-
CREATE ROLE 'MANAGERS';
51-
GRANT ALL ON *.* TO ROLE 'MANAGERS';
51+
```sql
52+
CREATE MASKING POLICY email_mask
53+
AS (val STRING)
54+
RETURNS STRING ->
55+
CASE
56+
WHEN current_role() IN ('MANAGERS') THEN val
57+
ELSE '*********'
58+
END;
59+
```
5260

53-
-- Create a user and grant the role to the user
54-
CREATE USER manager_user IDENTIFIED BY 'databend';
55-
GRANT ROLE 'MANAGERS' TO 'manager_user';
61+
### 2. Apply to Table Column
5662

57-
-- Create a masking policy
58-
CREATE MASKING POLICY email_mask
59-
AS
60-
(val nullable(string))
61-
RETURNS nullable(string) ->
62-
CASE
63-
WHEN current_role() IN ('MANAGERS') THEN
64-
val
65-
ELSE
66-
'*********'
67-
END
68-
COMMENT = 'hide_email';
69-
70-
-- Associate the masking policy with the 'email' column
63+
```sql
7164
ALTER TABLE user_info MODIFY COLUMN email SET MASKING POLICY email_mask;
65+
```
66+
67+
### 3. Test the Policy
68+
69+
```sql
70+
-- Create test data
71+
CREATE TABLE user_info (id INT, email STRING NOT NULL);
72+
INSERT INTO user_info VALUES (1, '[email protected]');
7273

73-
-- Query with the Root user
74+
-- Query as different roles to see masking in action
7475
SELECT * FROM user_info;
76+
```
77+
78+
## Prerequisites
79+
80+
- Define user roles and their access privileges before creating policies
81+
- Ensure users have appropriate roles assigned
82+
- See [User & Role](/sql/sql-commands/ddl/user/) for role management
83+
84+
## Policy Management
7585

76-
id|email |
77-
--+---------+
78-
2|*********|
79-
1|*********|
80-
```
86+
For detailed commands to create, modify, and manage masking policies, see:
87+
- [CREATE MASKING POLICY](/sql/sql-commands/ddl/mask-policy/create-mask-policy)
88+
- [ALTER TABLE COLUMN](/sql/sql-commands/ddl/table/alter-table-column)
89+
- [Masking Policy Commands](/sql/sql-commands/ddl/mask-policy/)
90+

0 commit comments

Comments
 (0)