Fortifying Cloud Security Posture Management with Open Policy Agent (OPA)

April 5, 2023

Ensuring cloud security is a top priority for businesses that use cloud infrastructure. Open Policy Agent (OPA) is a tool that helps achieve this goal by enforcing policies throughout cloud environments. OPA is an open-source policy engine that uses a declarative language called Rego to ensure that cloud resources are configured securely.

Using OPA, organizations can define and enforce policies to govern various cloud resources, including virtual machines, storage buckets, and databases. OPA can help ensure compliance with regulatory requirements, industry standards, and best practices by enforcing policies that govern access control, configuration management, and security settings.

Furthermore, OPA’s dynamic and flexible management capabilities enable organizations to manage their cloud resources more efficiently and effectively. By leveraging OPA for CSPM, organizations can enhance their security posture and reduce the risk of data breaches and security incidents.

Here’s a concise graphical illustration showcasing OPA Policy-as-code(PaC) approach:

OPA - Solution Architecture

Outlined below are some ways in which OPA can assist with CSPM, utilizing AWS as the foundation.

OPA offers several ways to assist CSPM in relation to Amazon Web Services (AWS). For instance, it can perform automated checks to ensure that cloud resources and configurations meet security standards and compliance requirements. These checks can be defined in policies and are automatically executed using OPA to identify potential security risks and vulnerabilities.  

We will furnish you with concrete illustrations that will walk you through diverse scenarios highlighting the integration of OPA into CI/CD, utilizing policy-as-code for  secure cloud storage management, compliance management, access control management, security configuration management, and best practices validation.

Using OPA to automate checks:

Here’s an example scenario for automated checks integrated within CI/CD:

We aim to establish a CI/CD pipeline in our AWS environment by utilizing AWS CodePipeline, AWS CodeCommit, and AWS CodeBuild. Once a new code commit is made to the CodeCommit repository, the pipeline will be triggered and Conftest will automatically verify the Kubernetes manifest files against a specific set of control policies encoded in OPA Rego. In the event of any policy violation, the validation process will terminate the pipeline. The pipeline will proceed to the deployment stage only when all the control policies are satisfied by the Kubernetes manifest files.

The solution architecture is depicted in the diagram below:

OPA - Solution Architecture

Here is a sample Rego script to ensure the encryption of EBS volumes with customer-managed CMK.

package aws.ebs.encryption

violation[{"msg": msg, "details": {"volume_id": volume.volume_id}}] {
volume := input.volume
not volume.encrypted
not volume.kms_key_id

msg := "EBS volume encryption should use customer master keys with KMS"
}
.code-container { position: relative; margin-top: 20px;}.copy-btn { position: absolute; right: 8px; top: 20px; /* Adjust as needed to position above the code block */ padding: 6px 12px; cursor: pointer; background-color: #777777; /* Button background color */ color: white; /* Button text color */ border: none; border-radius: 4px; font-size: 12px;}.code-block { font-family: monospace; background-color: rgba(255, 255, 255, 1); padding: 24px; margin-top: 12px; margin-bottom: 12px; border-radius: 8px; overflow-x: auto;}

The above policy checks if a violation will be reported when a volume is not encrypted or doesn’t employ a customer-managed CMK with KMS. This policy guarantees that robust encryption mechanisms are utilized to safeguard the data stored in EBS volumes.

The Rego below ensures that all Amazon RDS instances comply with the Payment Card Industry Data Security Standard (PCI DSS).

package aws.rds.compliance
deny[msg] {
instance := input.instance
not instance.publicly_accessible
not instance.encrypted
not instance.db_parameter_group.name == "pci-dss"
msg := sprintf("RDS instance %v does not comply with PCI-DSS standards", [instance.instance_id])
}
}
.code-container { position: relative; margin-top: 20px;}.copy-btn { position: absolute; right: 8px; top: 20px; /* Adjust as needed to position above the code block */ padding: 6px 12px; cursor: pointer; background-color: #777777; /* Button background color */ color: white; /* Button text color */ border: none; border-radius: 4px; font-size: 12px;}.code-block { font-family: monospace; background-color: rgba(255, 255, 255, 1); padding: 24px; margin-top: 12px; margin-bottom: 12px; border-radius: 8px; overflow-x: auto;}

This Rego policy establishes a security control to validate whether RDS instances comply with PCI-DSS standards. The policy defines a “deny” rule that inspects incoming RDS instances to verify if they are publicly accessible, encrypted, and configured to use a database parameter group named “pci-dss.” If any of these conditions are not met, the rule generates a “deny” decision accompanied by a message that the RDS instance does not comply with PCI-DSS standards, including the instance ID in the details.

This Rego policy can help reduce the risk of data breaches and uphold the security and confidentiality of an organization’s sensitive data stored in RDS instances.

package aws.iam.access
deny {
input.request.action == "iam:PassRole"
role := input.request.parameters.roleArn
roleHasWildcard := startswith(role, "arn:aws:iam::*:role/")
permissionGranted := any_permission_granted_to(role)
not roleHasWildcard
permissionGranted
}
any_permission_granted_to(role) {
permission := input.request.parameters.policyDocument.Statement[_]
permission.Effect == "Allow"
permission.Resource == "*"
permission.Principal.AWS == role
}
.code-container { position: relative; margin-top: 20px;}.copy-btn { position: absolute; right: 8px; top: 20px; /* Adjust as needed to position above the code block */ padding: 6px 12px; cursor: pointer; background-color: #777777; /* Button background color */ color: white; /* Button text color */ border: none; border-radius: 4px; font-size: 12px;}.code-block { font-family: monospace; background-color: rgba(255, 255, 255, 1); padding: 24px; margin-top: 12px; margin-bottom: 12px; border-radius: 8px; overflow-x: auto;}

The above policy aims to prevent the IAM PassRole permission from being granted to roles with a wildcard (*) in the role’s ARN. It defines a “deny” rule that checks incoming IAM API requests to see if they are attempting to grant the PassRole permission.

If the request is attempting to grant PassRole, the rule further checks if

The role’s ARN has a wildcard Permission has already been granted to the role The policy statement allows actions on all resourcesOnce all these conditions are met, the rule generates a “deny” decision, preventing the PassRole permission from being granted to roles with a wildcard in their ARN.

The policy also defines a “any_permission_granted_to” helper rule that checks if any permission has already been granted to the role in the IAM request’s policy document. This helper rule is called by the “deny” rule to check if permission has already been granted to the role.

Network security configuration management:OPA can also assist with security configuration management by enforcing policies that ensure that cloud resources are configured with appropriate security settings. This helps to reduce the risk of security threats by ensuring that security requirements for cloud resources and configurations are met. The code snippet provided represents a Rego policy defined in a package named “aws.ec2.security.” This policy aims to ensure that only approved Virtual Private Networks (VPNs) can access an instance in Amazon Elastic Compute Cloud (EC2).

package aws.ec2.security
approved_vpns = {
"vpn-1", "vpn-2"
}
violation[{"msg": msg, "details": {"instance_id": instance.instance_id}}] {
instance := input.instance
not contains(approved_vpns, instance.network_interfaces[_].private_ip_address)
msg := "instance vpc is not approved"
}
.code-container { position: relative; margin-top: 20px;}.copy-btn { position: absolute; right: 8px; top: 20px; /* Adjust as needed to position above the code block */ padding: 6px 12px; cursor: pointer; background-color: #777777; /* Button background color */ color: white; /* Button text color */ border: none; border-radius: 4px; font-size: 12px;}.code-block { font-family: monospace; background-color: rgba(255, 255, 255, 1); padding: 24px; margin-top: 12px; margin-bottom: 12px; border-radius: 8px; overflow-x: auto;}

The policy defines a set named “approved_vpns” that contains the identifiers of approved VPNs. The rule defines a “violation” rule that checks instances and their network interfaces against the list of approved VPNs. If an instance is not associated with an approved VPN, the rule generates a violation with a message that the instance’s VPC is not approved and includes the instance ID in the details of the violation.

OPA is an extremely reliable solution for Cloud Security Posture Management (CSPM), as it offers a variety of features that simplify the process of defining and enforcing policies for cloud resources. Writing policies in Rego is simple and easy to understand, making it easier for security teams to create and modify policies as needed. OPA also provides a testing framework that enables security teams to test rules using various test cases, without deploying the rules to the production environment. This helps ensure that the rules work as expected before they are deployed, reducing the risk of errors or unintended consequences. Additionally, OPA is fast and lightweight, which enables it to be integrated with different cloud services and tools. All of these features make OPA an excellent tool for organizations seeking to enhance their CSPM capabilities and protect their cloud resources.

References used in our Research

Author:

Pruthvi T – Lead Security Researcher, Loginsoft

Get notified

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

BLOGS AND RESOURCES

Latest Articles

RansomHub Revealed: Threats, Tools, and Tactics

December 9, 2024

The Rise of INTERLOCK Ransomware

November 13, 2024

Fortifying the Cloud: A Guide to Securing Vulnerable Cloud Environments

October 23, 2024