-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic-data-masking.sql
87 lines (63 loc) · 3.03 KB
/
dynamic-data-masking.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
USE TestDB
GO
-- Let's create two database users
CREATE USER DataAnalyst WITHOUT LOGIN;
CREATE USER FinanceGuy WITHOUT LOGIN;
-- Dynamic Data Masking Example
CREATE TABLE dbo.SuperSensitiveCustomerData
(
ID INT IDENTITY(1,1) PRIMARY KEY
, FirstName VARCHAR(100)
, LastName VARCHAR(100) MASKED WITH(FUNCTION = 'partial(1,"xxxx", 0)')
, HomeAddress VARCHAR(250) MASKED WITH(FUNCTION = 'default()')
, SSN VARCHAR(15) MASKED WITH(FUNCTION = 'partial(6, "xxxx", 0)')
, Email VARCHAR(50) MASKED WITH(FUNCTION = 'email()')
, CreditCard VARCHAR(40) MASKED WITH(FUNCTION = 'partial(12,"xxxx", 0)')
, DateOfBirth DATE MASKED WITH(FUNCTION = 'default()')
);
-- We need to grant SELECT to both users we created
GRANT SELECT ON dbo.SuperSensitiveCustomerData TO DataAnalyst, FinanceGuy;
-- Test how both users can see the data
EXECUTE(N'SELECT * FROM dbo.SuperSensitiveCustomerData') AS USER = N'DataAnalyst';
EXECUTE(N'SELECT * FROM dbo.SuperSensitiveCustomerData') AS USER = N'FinanceGuy';
-- Let's grant DataAnalyst the UNMASK permission
GRANT UNMASK TO DataAnalyst;
-- Test again
EXECUTE(N'SELECT * FROM dbo.SuperSensitiveCustomerData') AS USER = N'DataAnalyst';
EXECUTE(N'SELECT * FROM dbo.SuperSensitiveCustomerData') AS USER = N'FinanceGuy';
-- Create a new table as a copy of the first, but with no masking functions.
CREATE TABLE dbo.MoreSuperSensitiveCustomerData
(
ID INT IDENTITY(1,1) PRIMARY KEY
, FirstName VARCHAR(100)
, LastName VARCHAR(100)
, HomeAddress VARCHAR(250)
, SSN VARCHAR(15)
, Email VARCHAR(50)
, CreditCard VARCHAR(40)
, DateOfBirth DATE
);
-- Copy the data from our first table
INSERT INTO MoreSuperSensitiveCustomerData(FirstName, LastName, HomeAddress, SSN, Email, CreditCard, DateOfBirth)
SELECT FirstName, LastName, HomeAddress, SSN, Email, CreditCard, DateOfBirth FROM SuperSensitiveCustomerData;
-- Grant select to the new table
GRANT SELECT ON dbo.MoreSuperSensitiveCustomerData TO DataAnalyst, FinanceGuy;
-- Query as the users
EXECUTE(N'SELECT * FROM dbo.MoreSuperSensitiveCustomerData') AS USER = N'DataAnalyst';
EXECUTE(N'SELECT * FROM dbo.MoreSuperSensitiveCustomerData') AS USER = N'FinanceGuy';
-- Alter the table to define masking functions
ALTER TABLE dbo.MoreSuperSensitiveCustomerData
ALTER COLUMN LastName ADD MASKED WITH(FUNCTION = 'partial(1,"xxxx", 0)');
ALTER TABLE dbo.MoreSuperSensitiveCustomerData
ALTER COLUMN HomeAddress ADD MASKED WITH(FUNCTION = 'default()');
ALTER TABLE dbo.MoreSuperSensitiveCustomerData
ALTER COLUMN SSN ADD MASKED WITH(FUNCTION = 'partial(6, "xxxx", 0)');
ALTER TABLE dbo.MoreSuperSensitiveCustomerData
ALTER COLUMN Email ADD MASKED WITH(FUNCTION = 'email()');
ALTER TABLE dbo.MoreSuperSensitiveCustomerData
ALTER COLUMN CreditCard ADD MASKED WITH(FUNCTION = 'partial(12,"xxxx", 0)');
ALTER TABLE dbo.MoreSuperSensitiveCustomerData
ALTER COLUMN DateOfBirth ADD MASKED WITH(FUNCTION = 'default()');
-- Test one more time
EXECUTE(N'SELECT * FROM dbo.MoreSuperSensitiveCustomerData') AS USER = N'DataAnalyst';
EXECUTE(N'SELECT * FROM dbo.MoreSuperSensitiveCustomerData') AS USER = N'FinanceGuy';