Advanced Calculated Policies

This guide covers techniques for building calculated policies that go beyond simple single-query templates. If you're new to calculated policies, start with Create a Calculated Policy first.

Exploring CMDB Data

Calculated policies rely on three things: the information contained in the CMDB for the target resource, the GraphQL query to access that information, and the Nunjucks template to evaluate it.

CMDB: Definitions for a resource's CMDB entry can be found in the Hub documentation. For example, for a CloudWatch Alarm you would start with the Alarm resource summary, then go to the Alarm resource definition. The easiest way to explore is to create an instance of the resource you want to work on, then look at the data provided in the resource detail page.

GraphQL: Use the GraphQL Explorer in your Guardrails workspace (under Developers in the sidebar) to explore the resource query. Under the hood, the calculated policy dialog is making a similar query. Provided you have an example resource already, find it with Search, open its Developers tab, and copy the RESOURCE ID. Include that ID in the id field of the resource query in the GraphQL Explorer.

A simple starter query in the GraphQL Explorer might look like:

query SimpleResourceQuery {
  resource(id: "196925434872486") {
    data
    turbot {
      id
      akas
    }
  }
}

The equivalent calculated policy query is:

{
  resource {
    data
    turbot {
      id
      akas
    }
  }
}

Note the implicit inclusion of the resource ID -- the calculated policy takes care of this for you, running the query in the context of each resource being evaluated.

Nunjucks: The Nunjucks docs describe the full capabilities of the template language. Developers familiar with string manipulation, if-else, for, and range should be able to orient quickly.

Familiarity with the CMDB data for the target resource, GraphQL, and Nunjucks is essential to making progress quickly on calculated policies. Be aware that sometimes a deep understanding of how the cloud provider represents a resource is required. Data that appears in intuitive places in the provider's UI may not be intuitively placed in the underlying data structure.

When working on calculated policies, make sure to select a target resource in the calculated policy editor. The examples below won't produce meaningful results if you don't pick a resource.

Multi-Query Calculated Policies

Some calculated policies benefit from or require multiple stages of refinement of input data. For example, a control objective on a particular network resource type depends on the presence of other resources in the same virtual network. A single-query approach would grab all resources of that resource type for the entire workspace, then filter that huge list in the Nunjucks template -- essentially asking "Return all resources of this resource type everywhere." In large environments, this can result in thousands or tens of thousands of items. Calculated policy GraphQL results are hard limited to 300 results with no paging possible.

The multi-query approach instead starts with the question "What virtual network is this resource in?" then asks "In this virtual network specifically, what network resources are available?" Now, instead of thousands of items, the calculated policy only has to deal with a few relevant resources. The benefit of multi-query is that policy developers are assured they get exactly the data they need, with the cost of some added complexity in their policies.

Multi-Query Syntax

Multi-query calculated policies use a YAML array of GraphQL + Nunjucks template pairs. The below Terraform excerpt shows the format:

- |
  {
    account {
      turbot {
        id
      }
    }
  }
- |
  {
    account {
      turbot {
        id
      }
    }
    trails: resources(filter: "resourceId:{{ $.account.turbot.id }} resourceTypeId:'tmod:@turbot/aws-cloudtrail#/resource/types/trail' $.Status.IsLogging:true $.IsMultiRegionTrail:true limit:300") {
      items {
        trailName: get(path: "Name")
        cloudWatchLogsLogGroupArn: get(path: "CloudWatchLogsLogGroupArn")
        eventSelectors: get(path: "EventSelectors")
      }
    }
  }

Key points:

Next Steps