Implementing Security Patterns

1. Service-to-Service Authentication Pattern

MechanismDetail
mTLSBoth sides present certs; mutual auth
JWT (with kid)Signed token; verifiable offline
SPIFFE / SPIREWorkload identity standard
API KeysSimpler; rotate often; not for high security

2. Token-Based Authentication Pattern

Token TypeDetail
JWTSelf-contained; signed; can carry claims
Opaque (reference)Random ID; introspect against issuer
PASETOModern alternative to JWT (no algorithm confusion)
Refresh TokenLong-lived; exchanges for new access token

Example: JWT Bearer

curl -H "Authorization: Bearer eyJhbGciOiJSUzI1NiIs..." https://api.shop.com/orders

3. OAuth2 Pattern

Grant TypeUse
Authorization Code + PKCEWeb/mobile/SPA — recommended for users
Client CredentialsService-to-service
Device CodeTVs / CLI tools
Refresh TokenRenew access without re-login
Resource Owner PasswordDEPRECATED
ImplicitDEPRECATED

4. API Key Pattern

PracticeDetail
TransportHeader (X-API-Key) over HTTPS only
StorageHash at rest (bcrypt/argon2)
RotationSupport N active keys for zero-downtime rotation
ScopingPer-environment, per-permission set
RevocationImmediate; cache TTL ≤ minute

5. Mutual TLS Pattern

AspectDetail
MechanismClient + server present X.509 certs
TrustInternal CA issues short-lived certs
RotationAuto via cert-manager / Istio
IdentitySubject/SAN = service identity

6. Zero Trust Security Pattern

PrincipleDetail
Never Trust, Always VerifyNo implicit trust by network location
Verify ExplicitlyAuthn/z on every call
Least PrivilegeMinimum permissions per identity
Assume BreachSegment, monitor, encrypt everywhere
Continuous ValidationRisk-based re-auth, device posture

7. Defense in Depth Pattern

LayerControl
EdgeWAF, DDoS, rate limit, TLS
NetworkVPC, security groups, NetworkPolicy, mTLS
ServiceAuthn, authz, input validation
DataEncryption at rest, field-level encryption, KMS
RuntimeContainer hardening, RASP
MonitoringSIEM, anomaly detection, audit logs

8. Role-Based Access Control Pattern

ElementDetail
SubjectUser / service
RoleNamed bundle of permissions (admin, viewer)
PermissionAction on resource (orders:read)
ProsSimple, manageable for small permission sets
ConsRole explosion at scale (RBAC sprawl)

9. Attribute-Based Access Control Pattern

ElementDetail
PolicyRules over attributes (subject, resource, action, context)
EngineOPA/Rego, Cedar, AWS IAM conditions
ProsFine-grained; expressive; scales
ConsHarder to reason; performance considerations

Example: OPA Rego Policy

package authz
default allow = false
allow {
  input.action == "read"
  input.user.tenantId == input.resource.tenantId
  input.user.role in {"viewer", "editor", "admin"}
}

10. Secrets Management Pattern

PracticeDetail
Centralized VaultHashiCorp Vault, AWS Secrets Manager, Azure Key Vault
Dynamic SecretsIssue per-request, short-lived (DB credentials)
Auto-RotationPeriodic; automated
Encryption in Transit/RestAlways
AuditWho accessed what, when
NeverHardcode in code, env vars in YAML, git history
Warning: Treat any secret committed to git as compromised — rotate immediately, even after deletion.