@xdev-asia/xdev-knowledge-mcp 1.0.38 → 1.0.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/content/series/architecture/xay-dung-he-thong-y-te-microservices/chapters/01-phan-1-kien-truc-nen-tang/lessons/04-bai-4-threat-modeling-stride-dread.md +79 -114
- package/content/series/architecture/xay-dung-he-thong-y-te-microservices/chapters/02-phan-2-iam-keycloak/lessons/01-bai-5-setup-keycloak-realm-benh-vien.md +33 -84
- package/content/series/architecture/xay-dung-he-thong-y-te-microservices/chapters/02-phan-2-iam-keycloak/lessons/02-bai-6-phan-quyen-rbac-abac.md +6 -23
- package/content/series/architecture/xay-dung-he-thong-y-te-microservices/chapters/02-phan-2-iam-keycloak/lessons/03-bai-7-smart-on-fhir-oauth2-oidc.md +25 -36
- package/content/series/architecture/xay-dung-he-thong-y-te-microservices/chapters/02-phan-2-iam-keycloak/lessons/04-bai-8-mfa-passkeys-emergency-access.md +7 -23
- package/content/series/architecture/xay-dung-he-thong-y-te-microservices/chapters/03-phan-3-data-layer-postgresql/lessons/01-bai-9-postgresql-security-hardening.md +23 -69
- package/content/series/architecture/xay-dung-he-thong-y-te-microservices/chapters/03-phan-3-data-layer-postgresql/lessons/02-bai-10-ma-hoa-du-lieu-postgresql.md +25 -80
- package/content/series/architecture/xay-dung-he-thong-y-te-microservices/chapters/03-phan-3-data-layer-postgresql/lessons/03-bai-11-row-level-security-column-encryption.md +26 -55
- package/content/series/architecture/xay-dung-he-thong-y-te-microservices/chapters/03-phan-3-data-layer-postgresql/lessons/04-bai-12-audit-logging-cdc-pgaudit.md +51 -87
- package/content/series/architecture/xay-dung-he-thong-y-te-microservices/chapters/04-phan-4-microservices-quarkus/lessons/03-bai-15-ma-hoa-end-to-end-microservices.md +18 -63
- package/content/series/architecture/xay-dung-he-thong-y-te-microservices/chapters/04-phan-4-microservices-quarkus/lessons/04-bai-16-mtls-service-mesh.md +26 -88
- package/content/series/architecture/xay-dung-he-thong-y-te-microservices/chapters/05-phan-5-compliance-audit/lessons/01-bai-17-hipaa-technical-safeguards.md +50 -61
- package/content/series/architecture/xay-dung-he-thong-y-te-microservices/chapters/05-phan-5-compliance-audit/lessons/02-bai-18-audit-trail-opentelemetry-elk.md +11 -34
- package/content/series/architecture/xay-dung-he-thong-y-te-microservices/chapters/05-phan-5-compliance-audit/lessons/03-bai-19-data-masking-anonymization.md +113 -223
- package/content/series/architecture/xay-dung-he-thong-y-te-microservices/chapters/05-phan-5-compliance-audit/lessons/04-bai-20-backup-disaster-recovery.md +92 -149
- package/content/series/architecture/xay-dung-he-thong-y-te-microservices/chapters/06-phan-6-production-van-hanh/lessons/01-bai-21-zero-trust-architecture.md +126 -271
- package/content/series/architecture/xay-dung-he-thong-y-te-microservices/chapters/06-phan-6-production-van-hanh/lessons/02-bai-22-container-kubernetes-security.md +10 -52
- package/content/series/architecture/xay-dung-he-thong-y-te-microservices/chapters/06-phan-6-production-van-hanh/lessons/03-bai-23-penetration-testing.md +51 -90
- package/content/series/architecture/xay-dung-he-thong-y-te-microservices/chapters/06-phan-6-production-van-hanh/lessons/04-bai-24-capstone-deploy-production.md +137 -232
- package/package.json +1 -1
|
@@ -26,30 +26,17 @@ PostgreSQL là lựa chọn phổ biến cho hệ thống y tế nhờ tính lin
|
|
|
26
26
|
|
|
27
27
|
### 1.1. Các lớp bảo mật PostgreSQL
|
|
28
28
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
│ Authorization Layer │
|
|
41
|
-
│ Roles + Privileges + Row-Level Security │
|
|
42
|
-
├─────────────────────────────────────────────────────┤
|
|
43
|
-
│ Encryption Layer │
|
|
44
|
-
│ TDE + Column Encryption + Backup Encryption │
|
|
45
|
-
├─────────────────────────────────────────────────────┤
|
|
46
|
-
│ Audit Layer │
|
|
47
|
-
│ pgAudit + Custom Triggers + Log Shipping │
|
|
48
|
-
├─────────────────────────────────────────────────────┤
|
|
49
|
-
│ Operating System Layer │
|
|
50
|
-
│ File Permissions + SELinux/AppArmor │
|
|
51
|
-
└─────────────────────────────────────────────────────┘
|
|
52
|
-
```
|
|
29
|
+

|
|
30
|
+
|
|
31
|
+
| Layer | Tên | Thành phần |
|
|
32
|
+
|-------|-----|------------|
|
|
33
|
+
| 7 | Application | Quarkus / Spring Boot + Connection Pool |
|
|
34
|
+
| 6 | Network | Firewall Rules + VPN/Private Network |
|
|
35
|
+
| 5 | Authentication | pg_hba.conf + SSL/TLS + Client Certificates |
|
|
36
|
+
| 4 | Authorization | Roles + Privileges + Row-Level Security |
|
|
37
|
+
| 3 | Encryption | TDE + Column Encryption + Backup Encryption |
|
|
38
|
+
| 2 | Audit | pgAudit + Custom Triggers + Log Shipping |
|
|
39
|
+
| 1 | Operating System | File Permissions + SELinux/AppArmor |
|
|
53
40
|
|
|
54
41
|
### 1.2. Checklist bảo mật cần hoàn thành
|
|
55
42
|
|
|
@@ -145,7 +132,8 @@ hostssl all all 0.0.0.0/0 reject
|
|
|
145
132
|
sudo -u postgres psql
|
|
146
133
|
```
|
|
147
134
|
|
|
148
|
-
**Rule 4 - `hostssl` + `scram-sha-256`**:
|
|
135
|
+
**Rule 4 - `hostssl` + `scram-sha-256`**:
|
|
136
|
+
|
|
149
137
|
- `hostssl` = bắt buộc SSL/TLS
|
|
150
138
|
- `scram-sha-256` = password hashing mạnh nhất PostgreSQL hỗ trợ
|
|
151
139
|
|
|
@@ -273,28 +261,13 @@ WHERE usename IS NOT NULL;
|
|
|
273
261
|
|
|
274
262
|
### 4.1. Nguyên tắc Least Privilege cho Healthcare
|
|
275
263
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
│ │ └── Quản lý schema, backup, monitoring │
|
|
284
|
-
│ │ │
|
|
285
|
-
│ ├── app_roles (LOGIN, limited privileges) │
|
|
286
|
-
│ │ ├── app_patient_svc │
|
|
287
|
-
│ │ ├── app_lab_svc │
|
|
288
|
-
│ │ ├── app_pharmacy_svc │
|
|
289
|
-
│ │ └── app_gateway_svc │
|
|
290
|
-
│ │ │
|
|
291
|
-
│ ├── readonly_role (SELECT only) │
|
|
292
|
-
│ │ ├── readonly_analytics │
|
|
293
|
-
│ │ └── readonly_reporting │
|
|
294
|
-
│ │ │
|
|
295
|
-
│ └── monitoring_user (pg_monitor) │
|
|
296
|
-
└──────────────────────────────────────────────────────┘
|
|
297
|
-
```
|
|
264
|
+

|
|
265
|
+
|
|
266
|
+
- **postgres** (superuser) ← CHỈ dùng cho maintenance
|
|
267
|
+
- **dba_admin** (CREATEDB, CREATEROLE) — Quản lý schema, backup, monitoring
|
|
268
|
+
- **app_roles** (LOGIN, limited privileges) — app_patient_svc, app_lab_svc, app_pharmacy_svc, app_gateway_svc
|
|
269
|
+
- **readonly_role** (SELECT only) — readonly_analytics, readonly_reporting
|
|
270
|
+
- **monitoring_user** (pg_monitor)
|
|
298
271
|
|
|
299
272
|
### 4.2. Tạo Role Hierarchy
|
|
300
273
|
|
|
@@ -570,33 +543,13 @@ WHERE rolcanlogin = true
|
|
|
570
543
|
### 6.1. Tại sao cần PgBouncer
|
|
571
544
|
|
|
572
545
|
Trong hệ thống microservices, mỗi service instance tạo connection pool riêng. PgBouncer giúp:
|
|
546
|
+
|
|
573
547
|
- Giảm tổng số connections tới PostgreSQL
|
|
574
548
|
- Thêm một lớp authentication
|
|
575
549
|
- Rate limiting
|
|
576
550
|
- Connection routing
|
|
577
551
|
|
|
578
|
-
|
|
579
|
-
┌─────────────────────────────────────────────────────┐
|
|
580
|
-
│ Microservices (mỗi cái 20 connections) │
|
|
581
|
-
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
|
|
582
|
-
│ │ Patient │ │ Lab │ │ Pharmacy │ │
|
|
583
|
-
│ │ Service │ │ Service │ │ Service │ │
|
|
584
|
-
│ │ x3 pods │ │ x2 pods │ │ x2 pods │ │
|
|
585
|
-
│ └────┬─────┘ └────┬─────┘ └────┬─────┘ │
|
|
586
|
-
│ │ 60 conn │ 40 conn │ 40 conn │
|
|
587
|
-
│ └─────────────┼────────────┘ │
|
|
588
|
-
│ │ 140 total │
|
|
589
|
-
│ ┌──────┴───────┐ │
|
|
590
|
-
│ │ PgBouncer │ │
|
|
591
|
-
│ │ Pool: 50 │ ← Giảm từ 140 → 50 │
|
|
592
|
-
│ └──────┬───────┘ │
|
|
593
|
-
│ │ 50 conn │
|
|
594
|
-
│ ┌──────┴───────┐ │
|
|
595
|
-
│ │ PostgreSQL │ │
|
|
596
|
-
│ │ max: 200 │ │
|
|
597
|
-
│ └──────────────┘ │
|
|
598
|
-
└─────────────────────────────────────────────────────┘
|
|
599
|
-
```
|
|
552
|
+

|
|
600
553
|
|
|
601
554
|
### 6.2. PgBouncer Configuration
|
|
602
555
|
|
|
@@ -1011,6 +964,7 @@ Trong bài học này, chúng ta đã triển khai **PostgreSQL Security Hardeni
|
|
|
1011
964
|
8. **OS-Level** - File permissions và systemd hardening
|
|
1012
965
|
|
|
1013
966
|
Các nguyên tắc cốt lõi:
|
|
967
|
+
|
|
1014
968
|
- **Defense in Depth**: Nhiều lớp bảo mật chồng chéo
|
|
1015
969
|
- **Least Privilege**: Mỗi role chỉ có quyền tối thiểu cần thiết
|
|
1016
970
|
- **Encrypt Everything**: SSL/TLS cho mọi connection
|
|
@@ -23,37 +23,18 @@ course:
|
|
|
23
23
|
|
|
24
24
|

|
|
25
25
|
|
|
26
|
-
|
|
27
26
|
Dữ liệu y tế (PHI) yêu cầu mã hóa ở **hai trạng thái**: at-rest (khi lưu trữ) và in-transit (khi truyền). HIPAA Security Rule §164.312(a)(2)(iv) và §164.312(e)(2)(ii) quy định cụ thể về encryption requirements.
|
|
28
27
|
|
|
29
28
|
### 1.1. Encryption Layers
|
|
30
29
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
│ │ At-Rest Encryption │ │
|
|
40
|
-
│ │ │ │
|
|
41
|
-
│ │ Level 1: Full Disk Encryption (LUKS/dm-crypt) │ │
|
|
42
|
-
│ │ └── Bảo vệ khi disk bị đánh cắp │ │
|
|
43
|
-
│ │ │ │
|
|
44
|
-
│ │ Level 2: Transparent Data Encryption (TDE) │ │
|
|
45
|
-
│ │ └── Encrypt data files, WAL, temp files │ │
|
|
46
|
-
│ │ │ │
|
|
47
|
-
│ │ Level 3: Column-Level Encryption (pgcrypto) │ │
|
|
48
|
-
│ │ └── Encrypt từng field PHI riêng biệt │ │
|
|
49
|
-
│ │ │ │
|
|
50
|
-
│ │ Level 4: Application-Level Encryption │ │
|
|
51
|
-
│ │ └── Encrypt trước khi gửi tới database │ │
|
|
52
|
-
│ └─────────────────────────────────────────────────┘ │
|
|
53
|
-
│ │
|
|
54
|
-
│ Backup ──── Encrypted Backup ────► S3 (SSE-KMS) │
|
|
55
|
-
└─────────────────────────────────────────────────────────┘
|
|
56
|
-
```
|
|
30
|
+

|
|
31
|
+
|
|
32
|
+
- **In-Transit**: TLS 1.3 giữa Application và Database
|
|
33
|
+
- **At-Rest Level 1**: Full Disk Encryption (LUKS/dm-crypt) — bảo vệ khi disk bị đánh cắp
|
|
34
|
+
- **At-Rest Level 2**: Transparent Data Encryption (TDE) — encrypt data files, WAL, temp files
|
|
35
|
+
- **At-Rest Level 3**: Column-Level Encryption (pgcrypto) — encrypt từng field PHI riêng biệt
|
|
36
|
+
- **At-Rest Level 4**: Application-Level Encryption — encrypt trước khi gửi tới database
|
|
37
|
+
- **Backup**: Encrypted Backup → S3 (SSE-KMS)
|
|
57
38
|
|
|
58
39
|
### 1.2. So sánh các phương pháp mã hóa
|
|
59
40
|
|
|
@@ -490,35 +471,15 @@ public class SslVerificationService {
|
|
|
490
471
|
|
|
491
472
|
### 5.1. Vault Transit Secrets Engine
|
|
492
473
|
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
│ 1. Generate DEK │ │ │
|
|
503
|
-
│ (Data Encryption Key) │ │ │
|
|
504
|
-
│ │ │ │ │
|
|
505
|
-
│ 2. Encrypt data │ │ │
|
|
506
|
-
│ with DEK │ │ │
|
|
507
|
-
│ │ │ │ │
|
|
508
|
-
│ 3. ─── Encrypt DEK ──►│ │ │
|
|
509
|
-
│ │ │ 4. Wrap DEK │ │
|
|
510
|
-
│ │ │ with KEK ────►│ │
|
|
511
|
-
│ │ ◄── Wrapped ───│ │ │
|
|
512
|
-
│ │ DEK │ │ │
|
|
513
|
-
│ │ │ │ │
|
|
514
|
-
│ 5. Store encrypted │ │ │
|
|
515
|
-
│ data + wrapped DEK │ │ │
|
|
516
|
-
│ in PostgreSQL │ │ │
|
|
517
|
-
│ │
|
|
518
|
-
│ DEK = Data Encryption Key (per-record hoặc per-table) │
|
|
519
|
-
│ KEK = Key Encryption Key (master key trong Vault) │
|
|
520
|
-
└─────────────────────────────────────────────────────────┘
|
|
521
|
-
```
|
|
474
|
+

|
|
475
|
+
|
|
476
|
+
**Quy trình Envelope Encryption:**
|
|
477
|
+
|
|
478
|
+
1. Generate DEK (Data Encryption Key) — per-record hoặc per-table
|
|
479
|
+
2. Encrypt data with DEK
|
|
480
|
+
3. Gửi DEK tới Vault Transit để wrap
|
|
481
|
+
4. Vault wrap DEK với KEK (Key Encryption Key) qua HSM/KMS
|
|
482
|
+
5. Lưu encrypted data + wrapped DEK trong PostgreSQL
|
|
522
483
|
|
|
523
484
|
### 5.2. Vault Configuration
|
|
524
485
|
|
|
@@ -895,30 +856,13 @@ echo "Restore completed from: ${BACKUP_FILE}"
|
|
|
895
856
|
|
|
896
857
|
### 8.1. Quy trình Key Rotation
|
|
897
858
|
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
│ │ (active) │ (active) │ (active) │
|
|
906
|
-
│ │ │ │ │
|
|
907
|
-
│ │ Encrypt │ RE-encrypt │ RE-encrypt │
|
|
908
|
-
│ │ new data │ old data │ old data │
|
|
909
|
-
│ │ with v1 │ with v2 │ with v3 │
|
|
910
|
-
│ │ │ │ │
|
|
911
|
-
│ │ │ v1 still │ v1 retired │
|
|
912
|
-
│ │ │ can decrypt │ v2 can decrypt │
|
|
913
|
-
│ └──────────────┴──────────────┴─────────────────────│
|
|
914
|
-
│ │
|
|
915
|
-
│ Vault tự động: │
|
|
916
|
-
│ - Rotate KEK mỗi 90 ngày │
|
|
917
|
-
│ - OLD versions vẫn decrypt được │
|
|
918
|
-
│ - NEW data encrypt bằng latest version │
|
|
919
|
-
│ - Rewrap: re-encrypt DEK với new KEK version │
|
|
920
|
-
└─────────────────────────────────────────────────────┘
|
|
921
|
-
```
|
|
859
|
+

|
|
860
|
+
|
|
861
|
+
- **Day 0–90**: Key v1 active — encrypt new data
|
|
862
|
+
- **Day 90–180**: Key v2 active — re-encrypt old data, v1 vẫn decrypt được
|
|
863
|
+
- **Day 180+**: Key v3 active — v1 retired, v2 vẫn decrypt được
|
|
864
|
+
- Vault tự động rotate KEK, OLD versions vẫn decrypt, NEW data encrypt bằng latest
|
|
865
|
+
- **Rewrap**: re-encrypt DEK với new KEK version
|
|
922
866
|
|
|
923
867
|
### 8.2. Batch Re-encryption Script
|
|
924
868
|
|
|
@@ -1004,6 +948,7 @@ Trong bài học này, chúng ta đã triển khai **mã hóa toàn diện** cho
|
|
|
1004
948
|
9. **Key Rotation**: Automated DEK rewrapping khi KEK rotate
|
|
1005
949
|
|
|
1006
950
|
Nguyên tắc quan trọng:
|
|
951
|
+
|
|
1007
952
|
- **Never store encryption keys cùng với encrypted data**
|
|
1008
953
|
- **Always use authenticated encryption** (AES-GCM)
|
|
1009
954
|
- **Implement key rotation** với zero-downtime
|
|
@@ -23,7 +23,6 @@ course:
|
|
|
23
23
|
|
|
24
24
|

|
|
25
25
|
|
|
26
|
-
|
|
27
26
|
Row-Level Security (RLS) cho phép PostgreSQL kiểm soát **hàng nào** trong bảng mà user có thể nhìn thấy hoặc thao tác. Đây là tính năng critical cho healthcare vì:
|
|
28
27
|
|
|
29
28
|
- **Bác sĩ A** chỉ thấy bệnh nhân của mình
|
|
@@ -33,63 +32,35 @@ Row-Level Security (RLS) cho phép PostgreSQL kiểm soát **hàng nào** trong
|
|
|
33
32
|
|
|
34
33
|
### 1.1. RLS vs Application-Level Filtering
|
|
35
34
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
│ │
|
|
54
|
-
│ Ưu điểm: │
|
|
55
|
-
│ - Database enforce, không bypass được │
|
|
56
|
-
│ - Transparent cho application │
|
|
57
|
-
│ - Works với mọi tool (psql, reporting, BI) │
|
|
58
|
-
│ - Defense in depth layer │
|
|
59
|
-
└─────────────────────────────────────────────────────────┘
|
|
60
|
-
```
|
|
35
|
+

|
|
36
|
+
|
|
37
|
+
**Application-Level Filtering (KHÔNG AN TOÀN):**
|
|
38
|
+
|
|
39
|
+
- `SELECT * FROM patients WHERE doctor_id = :currentDoctor`
|
|
40
|
+
- Developer quên WHERE clause → data leak
|
|
41
|
+
- SQL injection bypass WHERE clause
|
|
42
|
+
- Direct DB access bypass application logic
|
|
43
|
+
- Reporting tools bypass application
|
|
44
|
+
|
|
45
|
+
**Row-Level Security (AN TOÀN):**
|
|
46
|
+
|
|
47
|
+
- `SELECT * FROM patients;` — Trả về CHỈ rows allowed
|
|
48
|
+
- Database enforce, không bypass được
|
|
49
|
+
- Transparent cho application
|
|
50
|
+
- Works với mọi tool (psql, reporting, BI)
|
|
51
|
+
- Defense in depth layer
|
|
61
52
|
|
|
62
53
|
### 1.2. RLS Architecture cho Healthcare
|
|
63
54
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
│ │ 2. SET LOCAL session variables │
|
|
74
|
-
│ │ SET LOCAL app.current_user_id = 'uuid'; │
|
|
75
|
-
│ │ SET LOCAL app.current_role = 'doctor'; │
|
|
76
|
-
│ │ SET LOCAL app.current_dept = 'cardiology'; │
|
|
77
|
-
│ │ SET LOCAL app.hospital_id = 'uuid'; │
|
|
78
|
-
│ │ │
|
|
79
|
-
│ │ 3. Execute query │
|
|
80
|
-
│ │ SELECT * FROM patients; │
|
|
81
|
-
│ ▼ │
|
|
82
|
-
│ PostgreSQL │
|
|
83
|
-
│ │ │
|
|
84
|
-
│ │ 4. RLS Policy evaluates │
|
|
85
|
-
│ │ current_setting('app.current_role') │
|
|
86
|
-
│ │ current_setting('app.current_dept') │
|
|
87
|
-
│ │ │
|
|
88
|
-
│ │ 5. Return ONLY allowed rows │
|
|
89
|
-
│ ▼ │
|
|
90
|
-
│ Results (filtered by RLS) │
|
|
91
|
-
└─────────────────────────────────────────────────────────┘
|
|
92
|
-
```
|
|
55
|
+

|
|
56
|
+
|
|
57
|
+
**Request Flow:**
|
|
58
|
+
|
|
59
|
+
1. **Quarkus App** — Extract JWT claims (user_id, role, department, hospital_id)
|
|
60
|
+
2. **SET LOCAL** session variables (`app.current_user_id`, `app.current_role`, `app.current_dept`, `app.hospital_id`)
|
|
61
|
+
3. **Execute query** — `SELECT * FROM patients;`
|
|
62
|
+
4. **PostgreSQL RLS Policy** evaluates `current_setting('app.current_role')`, `current_setting('app.current_dept')`
|
|
63
|
+
5. **Return ONLY allowed rows** — Results filtered by RLS
|
|
93
64
|
|
|
94
65
|
## 2. Thiết lập RLS cơ bản
|
|
95
66
|
|
|
@@ -23,61 +23,43 @@ course:
|
|
|
23
23
|
|
|
24
24
|

|
|
25
25
|
|
|
26
|
-
|
|
27
26
|
HIPAA Security Rule §164.312(b) yêu cầu **hardware, software, and/or procedural mechanisms that record and examine activity** trong hệ thống chứa ePHI. Audit logging không chỉ là compliance requirement mà còn là công cụ **phát hiện intrusion, forensics, và accountability**.
|
|
28
27
|
|
|
29
28
|
### 1.1. Audit Requirements cho Y Tế
|
|
30
29
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
│ - 6-year retention (HIPAA minimum) │
|
|
49
|
-
└─────────────────────────────────────────────────────────┘
|
|
50
|
-
```
|
|
30
|
+
| Yêu cầu | Mô tả |
|
|
31
|
+
|---------|--------|
|
|
32
|
+
| **WHO?** | User identity (Keycloak subject) |
|
|
33
|
+
| **WHAT?** | Action performed (SELECT, INSERT, UPDATE) |
|
|
34
|
+
| **WHEN?** | Timestamp (with timezone) |
|
|
35
|
+
| **WHERE?** | Source IP, application, service |
|
|
36
|
+
| **WHICH?** | Object accessed (table, column, row) |
|
|
37
|
+
| **HOW?** | Query text, parameters |
|
|
38
|
+
| **RESULT?** | Success/failure, rows affected |
|
|
39
|
+
|
|
40
|
+
**Yêu cầu bổ sung cho Healthcare:**
|
|
41
|
+
|
|
42
|
+
- PHI access tracking
|
|
43
|
+
- Break-the-glass audit
|
|
44
|
+
- Before/After values cho data changes
|
|
45
|
+
- Immutable audit trail (no tampering)
|
|
46
|
+
- 6-year retention (HIPAA minimum)
|
|
51
47
|
|
|
52
48
|
### 1.2. Audit Architecture
|
|
53
49
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
│ ┌────▼─────┐ │
|
|
68
|
-
│ │ Fluent │ │
|
|
69
|
-
│ │ Bit │ │
|
|
70
|
-
│ └────┬─────┘ │
|
|
71
|
-
│ │ │
|
|
72
|
-
│ ┌─────────┼─────────┐ │
|
|
73
|
-
│ │ │ │ │
|
|
74
|
-
│ ┌────▼───┐ ┌───▼────┐ ┌──▼──────┐ │
|
|
75
|
-
│ │OpenSear│ │ S3 │ │PostgreSQ│ │
|
|
76
|
-
│ │ch/ELK │ │Archive │ │L Audit │ │
|
|
77
|
-
│ │(Query) │ │(7 yrs) │ │DB │ │
|
|
78
|
-
│ └────────┘ └────────┘ └─────────┘ │
|
|
79
|
-
└─────────────────────────────────────────────────────────┘
|
|
80
|
-
```
|
|
50
|
+

|
|
51
|
+
|
|
52
|
+
**Audit Sources:**
|
|
53
|
+
|
|
54
|
+
- **Quarkus App Logs** → JSON logs
|
|
55
|
+
- **PostgreSQL pgAudit** → CSV/syslog
|
|
56
|
+
- **Debezium CDC** → Kafka events
|
|
57
|
+
|
|
58
|
+
**Pipeline:** Tất cả → **FluentBit** → phân phối tới:
|
|
59
|
+
|
|
60
|
+
- **OpenSearch/ELK** — Query & visualization
|
|
61
|
+
- **S3 Archive** — Lưu trữ 7 năm
|
|
62
|
+
- **PostgreSQL Audit DB** — Structured query
|
|
81
63
|
|
|
82
64
|
## 2. pgAudit - Installation & Configuration
|
|
83
65
|
|
|
@@ -179,6 +161,7 @@ SELECT * FROM patient_schema.patients;
|
|
|
179
161
|
```
|
|
180
162
|
|
|
181
163
|
Audit log output (CSV format):
|
|
164
|
+
|
|
182
165
|
```
|
|
183
166
|
2025-01-15 10:30:45.123 ICT [12345] user=app_patient_svc,db=healthcare_db,app=patient-service,client=10.0.1.10
|
|
184
167
|
AUDIT: SESSION,1,1,WRITE,INSERT,,patient_schema.patients,
|
|
@@ -240,6 +223,7 @@ ORDER BY relname;
|
|
|
240
223
|
## 4. Custom Audit Trigger Functions
|
|
241
224
|
|
|
242
225
|
pgAudit ghi lại **SQL statements**, nhưng không capture:
|
|
226
|
+
|
|
243
227
|
- **Before/After values** (old vs new data)
|
|
244
228
|
- **Application-level context** (JWT user, IP)
|
|
245
229
|
- **Business-level events** (who viewed which patient record)
|
|
@@ -524,38 +508,20 @@ $$ LANGUAGE plpgsql SECURITY DEFINER;
|
|
|
524
508
|
|
|
525
509
|
### 5.1. CDC Architecture
|
|
526
510
|
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
│ └────┬─────┘ │
|
|
542
|
-
│ │ JSON change events │
|
|
543
|
-
│ ▼ │
|
|
544
|
-
│ ┌──────────────────────────────────────────┐ │
|
|
545
|
-
│ │ Apache Kafka │ │
|
|
546
|
-
│ │ Topics: │ │
|
|
547
|
-
│ │ ├── healthcare.patient_schema.patients │ │
|
|
548
|
-
│ │ ├── healthcare.patient_schema.records │ │
|
|
549
|
-
│ │ └── healthcare.audit_schema.changes │ │
|
|
550
|
-
│ └────┬─────────────┬───────────┬───────────┘ │
|
|
551
|
-
│ │ │ │ │
|
|
552
|
-
│ ┌────▼───┐ ┌─────▼──┐ ┌────▼────┐ │
|
|
553
|
-
│ │OpenSear│ │ Audit │ │ Alert │ │
|
|
554
|
-
│ │ch Sink │ │ S3 │ │ Engine │ │
|
|
555
|
-
│ │Connect │ │Archive │ │(KSQL) │ │
|
|
556
|
-
│ └────────┘ └────────┘ └─────────┘ │
|
|
557
|
-
└─────────────────────────────────────────────────────────┘
|
|
558
|
-
```
|
|
511
|
+

|
|
512
|
+
|
|
513
|
+
**CDC Flow:**
|
|
514
|
+
|
|
515
|
+
1. **PostgreSQL WAL** — Write-Ahead Log (logical replication)
|
|
516
|
+
2. **Logical Decoding** → **Debezium Connector** (Kafka Connect Source)
|
|
517
|
+
3. **JSON change events** → **Apache Kafka** Topics:
|
|
518
|
+
- `healthcare.patient_schema.patients`
|
|
519
|
+
- `healthcare.patient_schema.records`
|
|
520
|
+
- `healthcare.audit_schema.changes`
|
|
521
|
+
4. **Consumers:**
|
|
522
|
+
- **OpenSearch Sink Connect** — Full-text search
|
|
523
|
+
- **Audit S3 Archive** — Long-term retention
|
|
524
|
+
- **Alert Engine (KSQL)** — Real-time anomaly detection
|
|
559
525
|
|
|
560
526
|
### 5.2. PostgreSQL Configuration cho CDC
|
|
561
527
|
|
|
@@ -1200,13 +1166,11 @@ Trong bài học này, chúng ta đã xây dựng **hệ thống Audit Logging t
|
|
|
1200
1166
|
9. **Monitoring**: Prometheus alerting cho replication lag, trigger health, access anomalies
|
|
1201
1167
|
|
|
1202
1168
|
Architecture tổng thể:
|
|
1203
|
-
|
|
1204
|
-
App Audit (Quarkus)
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
CDC (Debezium) ──► Kafka ──► KSQL (anomaly detection)
|
|
1209
|
-
```
|
|
1169
|
+
|
|
1170
|
+
- **App Audit (Quarkus)** → Kafka → OpenSearch (real-time query) + S3 (archive 7+ years)
|
|
1171
|
+
- **DB Audit (pgAudit)** → CSV → Fluent Bit → OpenSearch
|
|
1172
|
+
- **Custom Triggers** → `audit_schema` tables → Compliance Reports
|
|
1173
|
+
- **CDC (Debezium)** → Kafka → KSQL (anomaly detection)
|
|
1210
1174
|
|
|
1211
1175
|
## Bài tập
|
|
1212
1176
|
|