ai
Better Uptime การใช้งาน 2026 — ระบบ Monitoring

SASE Security กับ Clean Architecture — วิธีออกแบบระบบ Security ด้วย SASE และ Clean Architecture | SiamCafe Blog
เรียนรู้การออกแบบระบบ Security ด้วย SASE Framework ร่วมกับ Clean Architecture Principles ตั้งแต่ Layer Design, Security Policies, Use Cases ไปจนถึง Implementation พร้อม Code จริง
FAQ_Q:Clean Architecture คืออะไร
FAQ_A:Clean Architecture เป็นหลักการออกแบบซอฟต์แวร์ของ Robert C. Martin (Uncle Bob) แบ่งระบบเป็น Layers ที่แยก Concerns ชัดเจน Entities (Business Rules), Use Cases (Application Logic), Interface Adapters (Controllers, Presenters), Frameworks (UI, Database) Dependencies ชี้เข้าด้านในเสมอ
FAQ_Q:SASE กับ Clean Architecture เกี่ยวกันอย่างไร
FAQ_A:Clean Architecture ช่วยออกแบบระบบ SASE ให้ Maintainable และ Testable แยก Security Logic (Use Cases) ออกจาก Infrastructure (Frameworks) เปลี่ยน SASE Provider ได้โดยไม่กระทบ Business Logic ทดสอบ Security Policies ได้โดยไม่ต้องมี Infrastructure จริง
FAQ_Q:Dependency Rule คืออะไร
FAQ_A:Dependency Rule เป็นกฎสำคัญของ Clean Architecture กำหนดว่า Dependencies ต้องชี้จาก Layer นอกเข้า Layer ใน เช่น Frameworks ขึ้นกับ Interface Adapters, Interface Adapters ขึ้นกับ Use Cases, Use Cases ขึ้นกับ Entities Layer ในไม่รู้จัก Layer นอก
FAQ_Q: วิธีทดสอบ Security Policies ทำอย่างไร
FAQ_A: ใช้ Clean Architecture แยก Security Logic เป็น Use Cases ทดสอบด้วย Unit Tests โดยไม่ต้องมี Infrastructure จริง Mock Repository Interfaces ทดสอบ Policy Evaluation, Access Control, Threat Detection แยกจาก Network, Database, External APIs
BODY_START


SASE Security และ Clean Architecture

SASE (Secure Access Service Edge) เป็น Framework ที่รวม Network และ Security Services เมื่อออกแบบด้วย Clean Architecture ได้ระบบที่ Maintainable, Testable และเปลี่ยน Provider ได้ง่าย แยก Business Logic ออกจาก Infrastructure ชัดเจน
Clean Architecture แบ่งระบบเป็น 4 Layers หลัก โดย Dependencies ชี้เข้าด้านในเสมอ (Dependency Rule) ทำให้ทดสอบ Security Policies ได้โดยไม่ต้องมี Infrastructure จริง

Adapters และ Infrastructure
# === Layer 3 & 4: Adapters + Infrastructure ===
# --- Cloudflare Gateway Adapter ---
class CloudflareAccessGateway(AccessGateway):
"""Adapter สำหรับ Cloudflare Zero Trust"""
def __init__(self, api_token, account_id):
self.api_token = api_token
self.account_id = account_id
self.base_url = f"https://api.cloudflare.com/client/v4/accounts/{account_id}"
def enforce(self, request, decision):
"""Enforce ผ่าน Cloudflare Access"""
import requests
headers = {"Authorization": f"Bearer {self.api_token}"}
if decision == AccessDecision.DENY:
# Block IP
payload = {
"mode": "block",
"configuration": {
"target": "ip",
"value": request.source_ip,
},
"notes": f"Blocked by SASE policy for {request.user.email}",
}
resp = requests.post(
f"{self.base_url}/firewall/access_rules/rules",
headers=headers, json=payload,
)
return resp.status_code == 200
return True
# --- PostgreSQL Policy Repository ---
class PostgresPolicyRepository(PolicyRepository):
"""Adapter สำหรับ PostgreSQL"""
def __init__(self, connection_string):
import psycopg2
self.conn = psycopg2.connect(connection_string)
def get_policies(self, destination):
cur = self.conn.cursor()
cur.execute(
"SELECT * FROM security_policies WHERE destination = %s AND enabled = true ORDER BY priority",
(destination,)
)
rows = cur.fetchall()
return [self._row_to_policy(r) for r in rows]
def save_policy(self, policy):
cur = self.conn.cursor()
cur.execute(
"INSERT INTO security_policies (policy_id, name, conditions, action, priority, enabled) VALUES (%s, %s, %s, %s, %s, %s)",
(policy.policy_id, policy.name, str(policy.conditions), policy.action.value, policy.priority, policy.enabled)
)
self.conn.commit()
return True
def _row_to_policy(self, row):
return SecurityPolicy(
policy_id=row[0], name=row[1],
conditions=eval(row[2]), action=AccessDecision(row[3]),
priority=row[4], enabled=row[5],
)
# --- FastAPI Controller ---
# from fastapi import FastAPI, HTTPException
# app = FastAPI()
#
# @app.post("/evaluate")
# async def evaluate_access(request_data: dict):
# # Build AccessRequest from request_data
# # Call EvaluateAccessUseCase
# # Return decision
# pass
#
# @app.get("/policies")
# async def list_policies(destination: str):
# policies = policy_repo.get_policies(destination)
# return {"policies": [p.__dict__ for p in policies]}
print("\nClean Architecture Layers:")
print(" Layer 1 (Entities): User, Device, Policy, Threat")
print(" Layer 2 (Use Cases): EvaluateAccess, DetectThreat")
print(" Layer 3 (Adapters): Cloudflare, Zscaler, PostgreSQL")
print(" Layer 4 (Frameworks): FastAPI, PostgreSQL Driver")
Best Practices
- Dependency Rule: Dependencies ชี้เข้าด้านในเสมอ Layer ในไม่รู้จัก Layer นอก
- Interfaces: ใช้ Abstract Classes (Interfaces) เชื่อม Layers ทำให้เปลี่ยน Implementation ได้
- Unit Testing: ทดสอบ Use Cases ด้วย Mock Repositories ไม่ต้องมี Infrastructure จริง
- Provider Agnostic: เปลี่ยน SASE Provider (Cloudflare → Zscaler) โดยแก้แค่ Adapter Layer
- Domain Events: ใช้ Events สื่อสารระหว่าง Use Cases แทน Direct Coupling
- Separation of Concerns: แยก Security Logic ออกจาก Framework Code ชัดเจน
Clean Architecture คืออะไร
หลักการออกแบบซอฟต์แวร์ของ Uncle Bob แบ่งเป็น Layers แยก Concerns Entities Use Cases Interface Adapters Frameworks Dependencies ชี้เข้าด้านในเสมอ





