-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path23_Security_2.sql
More file actions
281 lines (240 loc) · 8.4 KB
/
23_Security_2.sql
File metadata and controls
281 lines (240 loc) · 8.4 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**************************************************************
* MySQL 8.0 Security Tutorial – Part 2
* This script demonstrates advanced security features
* in MySQL 8.0, including:
* - InnoDB tablespace-level encryption (data at rest).
* - Encrypted connections with TLS/SSL.
* - Dynamic data masking with MySQL Enterprise or
* masking function alternatives.
* - Row-level access patterns (VIEW + DEFINER).
* - Transparent Data Encryption via keyring components.
* - User attribute metadata and proxy users.
* - Privilege escalation prevention.
**************************************************************/
-------------------------------------------------
-- Region: 0. Initialization
-------------------------------------------------
/*
Run as root or an account with SYSTEM_USER and SUPER privileges.
*/
USE mysql_course;
-------------------------------------------------
-- Region: 1. InnoDB Data-at-Rest Encryption
-------------------------------------------------
/*
1.1 Enable the keyring component (runs once at server startup via
early-plugin-load or component installation).
The community keyring_file component stores keys in a local file.
*/
-- INSTALL COMPONENT 'file://component_keyring_file';
/*
1.2 Verify the active keyring.
*/
SELECT PLUGIN_NAME, PLUGIN_STATUS
FROM INFORMATION_SCHEMA.PLUGINS
WHERE PLUGIN_NAME LIKE 'keyring%';
/*
1.3 Create an encrypted InnoDB table.
ENCRYPTION = 'Y' encrypts the tablespace using the master key.
*/
DROP TABLE IF EXISTS sensitive_data;
CREATE TABLE sensitive_data
(
record_id INT PRIMARY KEY AUTO_INCREMENT,
customer_id INT NOT NULL,
credit_card CHAR(16) NOT NULL,
notes VARCHAR(255)
) ENGINE = InnoDB ENCRYPTION = 'Y';
INSERT INTO sensitive_data (customer_id, credit_card, notes)
VALUES
(101, '4111111111111111', 'Test card – Visa'),
(102, '5500005555555559', 'Test card – MasterCard');
/*
1.4 Verify encryption status via INFORMATION_SCHEMA.
*/
SELECT
TABLE_SCHEMA,
TABLE_NAME,
CREATE_OPTIONS
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'mysql_course'
AND TABLE_NAME = 'sensitive_data';
/*
1.5 Rotate the master encryption key.
This re-encrypts all tablespace keys without touching the data.
*/
-- ALTER INSTANCE ROTATE INNODB MASTER KEY;
-------------------------------------------------
-- Region: 2. Encrypted Connections (TLS/SSL)
-------------------------------------------------
/*
2.1 Check whether the server has TLS enabled.
*/
SHOW VARIABLES LIKE 'have_ssl';
SHOW VARIABLES LIKE 'ssl_%';
/*
2.2 Check the TLS status of the current session.
*/
SHOW STATUS LIKE 'Ssl_cipher';
/*
2.3 Require TLS for a specific user account.
*/
DROP USER IF EXISTS 'secure_user'@'%';
CREATE USER 'secure_user'@'%'
IDENTIFIED WITH caching_sha2_password BY 'SecurePass!99'
REQUIRE SSL;
/*
2.4 Require a specific TLS cipher or X.509 certificate.
*/
DROP USER IF EXISTS 'cert_user'@'%';
CREATE USER 'cert_user'@'%'
IDENTIFIED WITH caching_sha2_password BY 'CertPass!99'
REQUIRE X509;
/*
2.5 Verify connection requirements per user.
*/
SELECT User, Host, ssl_type, ssl_cipher, x509_issuer
FROM mysql.user
WHERE User IN ('secure_user', 'cert_user');
-------------------------------------------------
-- Region: 3. Data Masking Alternatives
-------------------------------------------------
/*
3.1 MySQL Enterprise Edition provides the data_masking plugin.
The community equivalent is to use views or generated columns
that apply masking expressions.
3.2 Create a masking view over the employees table from lesson 22.
Sensitive columns (ssn, salary) are either hidden or masked.
*/
DROP VIEW IF EXISTS v_employees_masked;
CREATE VIEW v_employees_masked AS
SELECT
employee_id,
first_name,
last_name,
email,
-- Show only last 4 digits of SSN
CONCAT('***-**-', RIGHT(ssn, 4)) AS ssn_masked,
-- Mask salary with a salary band instead of actual value
CASE
WHEN salary < 50000 THEN 'Band A'
WHEN salary < 100000 THEN 'Band B'
ELSE 'Band C'
END AS salary_band
FROM employees;
SELECT * FROM v_employees_masked;
/*
3.3 Enterprise masking functions reference (available with
MySQL Enterprise / MySQL HeatWave).
These functions are listed here for documentation; run them
only if the plugin is installed.
*/
-- SELECT mask_pan('4111111111111111'); -- mask a payment card number
-- SELECT mask_ssn('123-45-6789'); -- mask a US SSN
-- SELECT gen_rnd_email(); -- generate a random email
-- SELECT gen_rnd_us_phone(); -- generate a random phone
-------------------------------------------------
-- Region: 4. Row-Level Access Patterns (DEFINER Views)
-------------------------------------------------
/*
4.1 MySQL does not have native row-level security policies, but the
same effect can be achieved with SQL SECURITY DEFINER views that
filter rows based on the CLIENT information or a user mapping table.
4.2 Create a user-to-department mapping table.
*/
DROP TABLE IF EXISTS user_department_map;
CREATE TABLE user_department_map
(
mysql_user VARCHAR(100) NOT NULL,
department VARCHAR(100) NOT NULL,
PRIMARY KEY (mysql_user, department)
) ENGINE = InnoDB;
INSERT INTO user_department_map (mysql_user, department)
VALUES
('app_user', 'IT'),
('app_user', 'HR');
DROP TABLE IF EXISTS emp_data;
CREATE TABLE emp_data
(
emp_id INT PRIMARY KEY AUTO_INCREMENT,
emp_name VARCHAR(100) NOT NULL,
department VARCHAR(100) NOT NULL,
salary DECIMAL(10,2)
) ENGINE = InnoDB;
INSERT INTO emp_data (emp_name, department, salary)
VALUES
('Alice', 'IT', 90000),
('Bob', 'Finance', 80000),
('Carol', 'HR', 75000),
('Dave', 'IT', 95000);
/*
4.3 Create a DEFINER view that restricts rows to the calling user's
allowed departments. SQL SECURITY DEFINER executes as the view
owner, so the underlying table does not need to be directly
accessible to the calling user.
*/
DROP VIEW IF EXISTS v_my_employees;
CREATE DEFINER = CURRENT_USER
SQL SECURITY DEFINER
VIEW v_my_employees AS
SELECT e.*
FROM emp_data e
JOIN user_department_map m
ON m.department = e.department
AND m.mysql_user = CURRENT_USER();
/*
4.4 Test the view from the current session.
*/
SELECT * FROM v_my_employees;
-------------------------------------------------
-- Region: 5. Privilege Escalation Prevention
-------------------------------------------------
/*
5.1 Grant only required privileges — avoid GRANT OPTION unless necessary.
*/
DROP USER IF EXISTS 'restricted_user'@'localhost';
CREATE USER 'restricted_user'@'localhost'
IDENTIFIED WITH caching_sha2_password BY 'RestrictedP@ss1';
GRANT SELECT, INSERT ON mysql_course.emp_data TO 'restricted_user'@'localhost';
-- No UPDATE, DELETE, or GRANT OPTION intentionally.
/*
5.2 Show effective grants.
*/
SHOW GRANTS FOR 'restricted_user'@'localhost';
/*
5.3 Restrict the user to operate only within mysql_course.
Granting global (*.*) privileges is intentionally omitted.
*/
-------------------------------------------------
-- Region: 6. Proxy Users
-------------------------------------------------
/*
6.1 A proxy user forwards authentication to a base user.
Useful for mapping OS accounts to database roles without
storing individual passwords in MySQL.
*/
DROP USER IF EXISTS 'base_account'@'%';
DROP USER IF EXISTS ''@'%';
CREATE USER 'base_account'@'%'
IDENTIFIED WITH caching_sha2_password BY 'BaseAccP@ss1';
GRANT ALL ON mysql_course.* TO 'base_account'@'%';
/*
6.2 Create an anonymous proxy user that maps to base_account.
Requires the PROXY privilege.
*/
-- CREATE USER ''@'%' IDENTIFIED WITH auth_socket AS 'base_account';
-- GRANT PROXY ON 'base_account'@'%' TO ''@'%';
-------------------------------------------------
-- Region: 7. Cleanup
-------------------------------------------------
DROP USER IF EXISTS 'secure_user'@'%';
DROP USER IF EXISTS 'cert_user'@'%';
DROP USER IF EXISTS 'restricted_user'@'localhost';
DROP TABLE IF EXISTS sensitive_data;
DROP TABLE IF EXISTS emp_data;
DROP TABLE IF EXISTS user_department_map;
DROP VIEW IF EXISTS v_employees_masked;
DROP VIEW IF EXISTS v_my_employees;
-------------------------------------------------
-- Region: End of Script
-------------------------------------------------