Abhay Bhargav
June 6, 2023

Enhancing Security with Policy-as-Code

Table of content:

  1. Introduction
  2. What is Policy as Code (PaC)?
  3. How to enhance security with Policy-as-Code (PaC) across stack?
  4. Decoupling security controls
  5. Unit testing and testability
  6. Essential PaC tools and frameworks
  7. Conclusion


As the digital landscape evolves, ensuring robust security measures becomes increasingly vital. One approach gaining traction is Policy-as-Code (PaC). 

Implementing PaC allows organizations to decouple security controls into purpose-built capabilities, promoting greater flexibility and scalability. Not to mention, PaC frameworks offer enhanced testability through the ability to unit-test security policies. 

In this article, I will walk you through the benefits of PaC and highlight some essential tools and frameworks that can help your organization enforce security across the stack.

What is Policy-as-Code (PaC)?

Policy as code can help you automate security and compliance procedures, reducing human error and time-to-market constraints. It significantly eliminates the need to check for compliance manually. 

PaC is a method of programmatically enforcing security and risk controls within a CI/CD pipeline. It codifies policy evaluation, response, and notification rules in an application security testing context, allowing security teams to automate testing workflows. 

Benefits of PaC are:

  • It speeds up security testing by automating the process and removing manual intervention.
  • It increases efficiency.
  • It helps with version control for seamless updates.
  • It reduces errors and enables validation.

How to Enhance Security with Policy-as-Code (PaC) Across the Stack?

Now that we understand what PaC is and its importance, let us do a deep dive into how to enhance security with the help of PaC.

Decoupling Security Controls

Implementing Policy-as-Code (PaC) allows organizations to decouple their security controls into smaller, purpose-built components. 

Traditionally, security controls were tightly coupled and intertwined, making it challenging to address specific security requirements efficiently. By adopting PaC, organizations can break down these complex controls into modular pieces, each designed to handle a specific security aspect. 

For example, different PaC frameworks can enforce policies at the operating system level, API gateways, or within Kubernetes environments. 

This modularity enhances the flexibility and scalability of security controls and enables organizations to tailor their policies to the unique needs of each component within the technology stack. 

By decoupling security controls, organizations can achieve greater precision, agility, and ease of management, ensuring their security measures align with their specific requirements.

Unit Testing and Testability

An essential aspect of implementing PaC is the inherent testability of using dedicated frameworks and tools. These frameworks often provide built-in features for unit testing and integration testing of security policies.

Treating security policies as code allows organizations to leverage established testing methodologies and tools. Through unit testing, organizations can verify that individual security policies function as intended, ensuring that the desired security outcomes are achieved. 

Integration testing allows organizations to test the interoperability and compatibility of different security controls within the technology stack. This comprehensive testing approach significantly reduces the likelihood of misconfigurations and vulnerabilities arising from improperly enforced security policies. 

By incorporating testing practices into the development and deployment of security controls, your organization can enhance its security protocol's accuracy, reliability, and effectiveness.

Essential PaC Tools and Frameworks

Before delving into the details of essential Policy-as-Code (PaC) tools and frameworks, it is worth highlighting the importance of selecting the right solutions that align with your organization's specific security needs. 

PaC offers a versatile approach to security, allowing you to enforce policies across various layers of your technology stack. 

I recommend the following tools and frameworks for implementing PaC and enhancing security control:

OpenPolicyAgent (OPA)

OpenPolicyAgent, aka OPA, is the pioneer and most versatile Policy-as-Code framework. It provides organizations with a powerful tool to define and enforce policies across various environments, ranging from operating systems to API Gateways. 

The key advantage of OPA is its use of a Domain Specific Language (DSL) called Rego. Rego allows organizations to express policies declaratively and enables the integration of external data sources for policy enforcement. 

This flexibility and extensibility make OPA an excellent choice for enforcing policies throughout the technology stack, providing granular control over security controls.


Although not strictly a Policy-as-Code framework, eBPF (extended Berkeley Packet Filter) offers a compelling approach to codify and enforce security controls at the kernel level. 

With eBPF, organizations can deploy lightweight, kernel-level applications that can monitor and filter system events and enforce security policies. 

This approach allows for fine-grained control over security controls for Nix machines or containers. 

By leveraging eBPF, organizations can implement security measures with minimal performance impact, making it an efficient and effective solution for enforcing policies at the kernel level.


Kyverno is a policy-management tool designed explicitly for Kubernetes environments. What sets Kyverno apart is its ability to define policies using Kubernetes manifests. This means that organizations can express policies in a familiar and intuitive manner, leveraging the existing infrastructure-as-code approach. 

With Kyverno, organizations can seamlessly integrate security policies into their Kubernetes clusters. It ensures that deployments adhere to predefined policies, enhancing the security and reliability of applications running on Kubernetes. Kyverno simplifies policy management and enables consistent and secure deployments throughout the Kubernetes environment.

Casbin / Oso

Role-Based Access Control (RBAC) is a critical security aspect, but its implementation can be complex and vulnerable. Casbin and Oso are powerful tools that simplify the implementation of dynamic RBAC by leveraging Authorization-as-Code approaches. These tools provide a framework for defining and managing access control policies in a flexible and scalable manner. 

Organizations can streamline RBAC management by adopting Casbin or Oso and mitigate potential security risks associated with RBAC misconfigurations. These tools offer an easier way to enforce fine-grained access control, reducing the likelihood of vulnerabilities such as Business Object Level Authorization (BOLA) and Business Function Level Authorization (BFLA).

HashiCorp Terraform Sentinel

HashiCorp Terraform Sentinel is a closed-source product that is a valuable tool for enforcing Infrastructure-as-Code (IaC) policies. Sentinel enables organizations to define and implement policies to govern infrastructure deployments made with HashiCorp Terraform. These policies ensure that infrastructure configurations adhere to predefined security and compliance requirements. 

Sentinel integrates into the Terraform workflow to enforce policies during the provisioning and deployment process. While Sentinel is a popular choice, similar functionality can be achieved in Continuous Integration (CI) pipelines using open-source tools like Semgrep or Checkov. 

These tools provide automated scanning and policy enforcement capabilities for IaC templates, ensuring that infrastructure deployments follow best practices and security guidelines.

By leveraging these essential Policy-as-Code tools and frameworks, your organization can strengthen its security posture and enforce policies across the entire technology stack. 

These solutions offer flexibility, ease of use, and testability, allowing organizations to establish robust security controls while maintaining operational efficiency.


In an ever-evolving digital landscape, ensuring robust security measures is crucial for organizations. Policy-as-Code (PaC) offers an advanced approach to enhance security across the technology stack. 

By decoupling and enforcing security controls using PaC frameworks and tools, your organization can achieve greater flexibility, scalability, and testability.

At we45, we understand the significance of robust security practices across the technology stack. Our team of experts can guide you in selecting and implementing the most suitable security frameworks and tools based on your specific requirements.

Secure your systems, applications, and infrastructure with We45's industry-leading expertise. 

Contact us today to learn how we can help you achieve a robust and resilient security foundation.

If you want to hear my views on PaC and DevSecOps, I would speak about Policy-as-Code on SecAppDev 2023 consortium to be held on June 12-16th in Leuven, Belgium.