Calculated Policies FAQ
- What is a calculated policy?
- How do calculated policies work?
- Which policy can I turn into a calculated policy?
- Are there any limitations to calculated policies?
- How do I set a calculated policy?
- How can I reference variables with special characters in a calculated policy?
- How do I add Terraform to calculated policy templates?
What is a calculated policy?
Customers often need to take remediation action based on specific business rules. For example, a static S3 bucket must be attached to a CloudFront distribution. Calculated policies encode that business logic in an executable form.
How do calculated policies work?
Calculated policies are composed of a GraphQL query to get information about a resource and a Nunjucks template to specify the business logic. The resource data feeds into the template, which outputs strings that match policy values. Calculated policies are always run from the context of the resource itself. The Guardrails Samples Repo has examples of what can be done.
On resource discovery or an event, Guardrails will evaluate a calculated policy in the same way as a normal policy.
Which policy can I turn into a calculated policy?
All policies can be made into calculated policies.
The most common policies used for implementing custom business logic are the policy pairs of Active and Active > Age, then Approved and Approved > Usage. The Active and Approved policies specify what action to take -- whether to raise an alarm (Check) or delete the offending resource (Enforce). The business logic for deciding resource age or approved status is encoded in the Active > Age and Approved > Usage policies.
Active and Approved controls are standard for all resources.
Are there any limitations to calculated policies?
Calculated policies can only take the actions encoded in existing controls. Active and Approved controls are great for removing unapproved resources or raising alarms for an administrator. Guardrails has implemented many common remediations, such as encryption at rest, tagging, and resource access policies. If the desired remediation action isn't currently available, reach out to Guardrails support with your use case.
GraphQL query results are limited to 300 items with no paging. For scenarios that exceed this limit, use multi-query calculated policies.
How do I set a calculated policy?
See Create a Calculated Policy for a step-by-step walkthrough in the Guardrails console.
Below is an example of the logic involved. We'll investigate how to set a VPC Endpoint approved policy to Not Approved if the VPC Endpoint AWS policy has a principal value set to * (you will need an existing VPC endpoint under Guardrails management to test this).
-
In your AWS account, locate an existing VPC endpoint and make note of its ID (e.g.,
vpce-05b1912865c21251f). -
Navigate to the account where the policy will be set, then open the Policies tab and click New Policy Setting.
-
Search for
AWS > VPC > Endpoint > Approved > Usageand select it. -
Set the scope to the level at which the policy should apply (e.g., a region in your AWS account).
-
Switch to calculated policy mode by clicking the Calculated toggle.
-
In the Resource field, search for the VPC Endpoint ID you noted earlier. This selects the test resource that the editor will use to preview the query results.
-
Our calculated policy evaluates the policy attached to a VPC Endpoint, so we query to return only the policy data. Use the following GraphQL query:
{ resource { stmts: get(path: "PolicyDocument.Statement") } } -
Validate that the query result shows a valid result for your VPC endpoint. A default endpoint policy will have a result set that looks like this:
{ "resource": { "stmts": [ { "Action": "*", "Effect": "Allow", "Resource": "*", "Principal": "*" } ] } }Note the square bracket in the response -- this indicates that the returned data is an array, which affects the structure of the policy template.
-
Calculated policies use Nunjucks template format to evaluate custom logic and return one of the expected values for this policy. In this case the allowed values are:
Not approved,Approved,Approved if AWS > VPC > Enabled. The following template checks to see if any overly broad settings exist in the attached policy:{% set starValue = "False" %} {# Initialize a value to False #} {% for statement in $.resource.stmts %} {# Iterate through all statements in the policy #} {% if statement.Principal == "*" %} {% set starValue = "True" %} {% endif %} {% endfor %} {% if starValue == "True" %} {# If true, the policy is in violation #} "Not approved" {% else %} "Approved" {% endif %} -
Verify that the query and template evaluate correctly -- the preview below the template field will show the evaluated value for the example VPC endpoint you selected. Each VPC endpoint will be individually evaluated by Guardrails at runtime.
-
Click Save to create your calculated policy. It will immediately take effect and evaluate whether the current VPC endpoints are configured correctly.
How can I reference variables with special characters in a calculated policy?
Most variables referenced in calculated policies are straightforward, such as stage, data, or name. However, some cloud resources include attributes with special characters -- hyphens (-), slashes, or other symbols like */*.
Let's assume we're doing the following query and template for the policy AWS > API Gateway > Stage > Approved > Usage:
{
stage {
methodSettings
}
}
{
"stage": {
"methodSettings": {
"*/*": {
"loggingLevel": "INFO",
"cachingEnabled": false,
"metricsEnabled": true,
"dataTraceEnabled": false,
"cacheTtlInSeconds": 300,
"cacheDataEncrypted": false,
"throttlingRateLimit": 10000,
"throttlingBurstLimit": 5000,
"requireAuthorizationForCacheControl": true,
"unauthorizedCacheControlHeaderStrategy": "SUCCEED_WITH_RESPONSE_HEADER"
}
}
}
}
The goal is to make an evaluation based on the loggingLevel attribute. To access this attribute, use bracket notation in the Nunjucks template:
$.stage.methodSettings['*/*'].loggingLevel
You can do the same for attributes that contain hyphens:
$.bucket.turbot.tags['this-tag-has-hyphens']
How do I add Terraform to calculated policy templates?
Using a policy such as AWS > Backup > Source > Stack, it is possible to create a calculated policy template that outputs Terraform. For example:
resource "aws_iam_role" "{{ $.region.metadata.aws.accountId }}_ec2_backup_role" {
name = "turbot_ec2_backup_role"
assume_role_policy = ""
}
While the calculated policy will be in the OK state (assuming the query is valid), the control AWS > Backup > Source will return Invalid:
Failed to convert the stack source from HCL to JSON
Command failed: PATH=$PATH:. hcl2json
Failed to convert file: :1,104-122: Invalid single-argument block definition;
A single-line block definition must end with a closing brace immediately after its single argument definition.
To remedy this, add the pipe character (|) at the start of the Terraform resource definition in the calculated policy template:
|
resource "aws_iam_role" "{{ $.region.metadata.aws.regionName }}_ec2_backup_role" {
name = "turbot_ec2_backup_role"
assume_role_policy = ""
}
Additional Resources
- Create a Calculated Policy -- Step-by-step guide
- Advanced Calculated Policies -- Multi-query patterns and CMDB exploration
- Calculated Policy Builder -- Test and debug calculated policies
- Guardrails Samples Repo -- Working examples of policy packs
- Nunjucks Best Practices -- Template tips and patterns