Security Patterns

11 recurring patterns, each with its problem statement, implementation steps and tradeoffs.

Shift Left Security

Move security feedback as close to the developer as possible.

  1. Developer
  2. Pre-commit checks
  3. Merge request scans
  4. Build pipeline
  5. Runtime controls

Problem

Security review happens after code is written and often after it is deployed, so defects are found when they are expensive to change and the release is already committed.

Pattern

Run automated checks in the developer's environment and in the merge request, with fast, specific, actionable output. Security becomes part of the definition of done rather than a later gate.

Implementation

  • Pre-commit hooks for secret detection and formatting.
  • SAST and dependency scanning on every merge request, scoped to changed files where possible.
  • Findings reported inline in the merge request, not in a separate portal.
  • Security review focused on design changes, informed by a lightweight threat model.

Benefits

  • Cheaper remediation while the developer still has context.
  • Fewer late release blocks.
  • Security knowledge spreads across the engineering team.

Tradeoffs

  • Noisy tooling erodes trust quickly, so tuning effort is mandatory.
  • Local checks lengthen the inner loop if they are slow.
  • Does not remove the need for runtime controls.
SemgrepGitleaksTrivyRenovate

Security Gates

Objective, documented criteria that a release must satisfy before progressing.

  1. Scan stage
  2. Policy evaluation
  3. Pass or block
  4. Exception register
  5. Promotion

Problem

Scanners produce findings but nothing decides whether a release may proceed, so results are ignored or negotiated case by case.

Pattern

Define machine-evaluated thresholds per stage. Failures block promotion, and exceptions are explicit, owned and time-bound.

Implementation

  • Standardise on machine-readable scan output.
  • Encode thresholds as policy evaluated in the pipeline.
  • Start with fixable critical findings only, then tighten.
  • Record exceptions with owner, justification and expiry.

Benefits

  • Consistent decisions.
  • Auditable release evidence.
  • Clear expectations for developers.

Tradeoffs

  • Poorly calibrated gates cause bypass culture.
  • Requires an exception process, which requires governance effort.
Open Policy AgentTrivyGitLabDefectDojo

Defense in Depth

Layer independent controls so no single failure results in compromise.

  1. Code controls
  2. Build controls
  3. Admission controls
  4. Network controls
  5. Runtime detection

Problem

Any individual control can be misconfigured, bypassed or blind to a novel technique.

Pattern

Place independent controls at each layer — code, dependency, build, artefact, admission, network, runtime — such that each assumes the previous one may have failed.

Implementation

  • Code and dependency scanning at commit.
  • Signing and provenance at build.
  • Admission verification at deploy.
  • Network segmentation and runtime detection in production.

Benefits

  • No single point of security failure.
  • Detection capability even when prevention fails.

Tradeoffs

  • More components to operate.
  • Overlapping alerts require deduplication.
KyvernoFalcoSigstore CosignCilium

Least Privilege

Grant the minimum access required, for the shortest time, at the narrowest scope.

  1. Identity provider
  2. Short-lived credential issuance
  3. Scoped role
  4. Resource
  5. Access review

Problem

Standing broad privileges mean any compromised identity yields extensive access, and permissions accumulate over time.

Pattern

Derive access from workload or human identity, issue short-lived credentials, scope to specific resources, and review against actual usage.

Implementation

  • OIDC federation for pipelines instead of stored cloud keys.
  • One service account per workload with narrow role bindings.
  • Dynamic database credentials with short leases.
  • Periodic access review driven by usage data.

Benefits

  • Smaller blast radius.
  • Simpler incident containment.
  • Direct compliance benefit.

Tradeoffs

  • More identities to manage.
  • Initial friction when scoping existing workloads.
HashiCorp VaultAWS IAMOpen Policy Agent

Zero Trust

Authenticate and authorise every request; never grant trust based on network location.

  1. Client identity
  2. Policy decision point
  3. mTLS transport
  4. Service
  5. Audit log

Problem

Perimeter-based trust means anything inside the network is implicitly trusted, so one foothold enables broad lateral movement.

Pattern

Every request carries a verifiable identity and is authorised at the point of access. Network position confers no privilege.

Implementation

  • mTLS between services with workload identity certificates.
  • Per-request authorisation using token claims.
  • Default-deny network policy with explicit allows.
  • Device and user posture checks for human access.

Benefits

  • Lateral movement is constrained.
  • Consistent policy across environments.

Tradeoffs

  • Significant architectural change.
  • Identity infrastructure becomes critical path.
IstioCiliumKeycloakOpen Policy Agent

Immutable Infrastructure

Replace running instances rather than modifying them in place.

  1. Image build
  2. Scan and sign
  3. Registry
  4. Rolling replacement
  5. Old instance terminated

Problem

In-place patching produces configuration drift, undocumented state and hosts that cannot be reproduced.

Pattern

Build a versioned image, deploy it, and never modify running instances. Changes mean a new image and a rollout.

Implementation

  • Golden images built in CI and scanned before promotion.
  • Read-only root filesystems where the workload allows it.
  • Rolling replacement instead of remote configuration changes.
  • SSH access removed or heavily restricted and audited.

Benefits

  • Reproducible hosts.
  • No drift.
  • Rollback is a redeploy of the previous image.

Tradeoffs

  • Requires fast build and deploy pipelines.
  • Stateful workloads need careful data separation.
PackerKanikoTerraformTrivy

Secure Supply Chain

Make every step from source to deployment verifiable.

  1. Source
  2. Hermetic build
  3. Sign and attest
  4. Registry
  5. Admission verification
  6. Runtime

Problem

A deployed artefact usually cannot be traced back to a specific commit and build, so tampering is undetectable.

Pattern

Verified source, hermetic build, signed artefact with provenance, and verification enforced at deployment admission.

Implementation

  • Protected branches and required review on the source repository.
  • Ephemeral, pinned build environments.
  • Signatures, SBOMs and provenance attestations per artefact.
  • Admission policy that rejects unverified images.

Benefits

  • Tamper detection.
  • Rapid impact analysis on new advisories.
  • Regulatory alignment.

Tradeoffs

  • Multiple new components to operate.
  • Verification failures can block deployments.
Sigstore CosignSyftKyvernoin-toto

Policy as Code

Express rules as versioned code enforced by an engine, not as prose in a wiki.

  1. Policy repository
  2. CI evaluation
  3. Admission enforcement
  4. Violation reports

Problem

Written standards are inconsistently applied and cannot be tested, so compliance is assessed manually and late.

Pattern

Encode each rule as a policy evaluated by an engine at multiple enforcement points, with the same rule used in CI and at admission.

Implementation

  • Store policies in git with review and unit tests.
  • Evaluate in CI against manifests and plans.
  • Enforce at admission in clusters.
  • Export violation reports as compliance evidence.

Benefits

  • Consistent enforcement.
  • Testable rules.
  • Automatic evidence generation.

Tradeoffs

  • Policy authoring is a specialist skill.
  • Enforcing too early breaks existing workloads.
Open Policy AgentKyvernoConftestCheckov

GitOps Security

Git is the single source of truth; the cluster reconciles toward it continuously.

  1. Git repository
  2. Review and merge
  3. Reconciler
  4. Cluster
  5. Drift detection

Problem

Direct cluster access for deployments means changes bypass review, and drift between intent and reality goes unnoticed.

Pattern

All desired state lives in git. A reconciler applies it and reverts manual changes. Humans lose direct write access to production.

Implementation

  • Reconciler with narrow, namespace-scoped permissions.
  • Protected branches and required review on the state repository.
  • Encrypted secrets in git or references to an external secret store.
  • Drift alerts when live state diverges from the declared state.

Benefits

  • Every change reviewed and attributable.
  • Drift is corrected automatically.
  • Rollback is a git revert.

Tradeoffs

  • Emergency changes need a documented break-glass path.
  • Secret handling requires additional tooling.
Argo CDFluxSOPSExternal Secrets Operator

Continuous Compliance

Generate control evidence continuously and automatically.

  1. Control catalogue
  2. Automated checks
  3. Evidence store
  4. Exception register
  5. Audit report

Problem

Compliance is assessed in periodic audits with manually collected evidence, which is expensive and only reflects a single point in time.

Pattern

Map each control to an automated check that runs on a schedule and stores dated, machine-readable evidence with defined retention.

Implementation

  • Control-to-check mapping maintained in version control.
  • Scheduled scans across cloud accounts, clusters and hosts.
  • Evidence stored immutably for the audit retention window.
  • Exception register with owners and expiry dates.

Benefits

  • Audit becomes a query.
  • Control failures are detected within days, not annually.

Tradeoffs

  • Not every control can be automated.
  • Evidence storage costs and retention obligations grow.
ProwlerOpenSCAPOpen Policy AgentKyverno

Automated Vulnerability Management

Aggregate findings automatically and drive them to closure with owners and SLAs.

  1. Scanners
  2. Normalisation
  3. Deduplication
  4. Prioritisation
  5. Ownership routing
  6. SLA tracking

Problem

Each scanner has its own console, findings are duplicated across tools, and nobody owns closure.

Pattern

Normalise all scanner output into one system, deduplicate by component and asset, prioritise with exploitability data, and track ageing against SLAs.

Implementation

  • Pipelines push results to a central aggregation platform.
  • Assets carry ownership metadata for routing.
  • Prioritisation combines severity, KEV/EPSS data and exposure.
  • Dashboards track ageing and closure rate rather than open counts.

Benefits

  • Single view of risk.
  • Measurable remediation throughput.
  • Less duplicated effort.

Tradeoffs

  • Integration and mapping work up front.
  • Asset ownership data must be kept current.
DefectDojoTrivyProwlerRenovate