KBAC policy, ContX IQ policy and Knowledge Query
Authorization in IndyKite is configuration: a KBAC policy answers "may this subject perform this action on this resource?" through the AuthZEN endpoints, and a ContX IQ policy declares which graph data a Knowledge Query may return and for whom.
This Terraform configuration creates the three objects together:
1. A KBAC policy (policy_version 2.0-kbac) allowing a Person to drive a Car they own.
2. A ContX IQ policy (policy_version 1.0-ciq) with the Cypher pattern, the filter and the allowed_reads of a read query.
3. A Knowledge Query that returns a license number for an input parameter, authorized by the ContX IQ policy through policy_id.
Both resources take the policy or query as JSON, written here with jsonencode so Terraform validates the structure.
Use case
Scenario: a team keeps its authorization rules in version control next to the rest of the project.
- The KBAC policy is evaluated by POST /access/v1/evaluation and the search endpoints; changing the Cypher condition and applying is a reviewed, versioned change.
- The ContX IQ policy and Knowledge Query pair is executed by POST /contx-iq/v1/execute with the query's GID and input parameters ($person_email, $ln_number); the policy's allowed_reads caps what the query can return.
- status lets a policy be staged: draft or inactive policies exist but are not applied; set active when ready.
Requirements
- ServiceAccount credentials created in the IndyKite Hub for your organization.
- IndyKite Terraform provider 0.49.0 or later, the release the example pins in required_providers; the Terraform guide lists the version each feature first appeared in.
- A project GID for project_id.
- For the ContX IQ query to return data: Person, Contract, Vehicle and LicenseNumber nodes with the relationships the Cypher names, captured through the Capture API.
Steps
1. Put your project GID in project_id on the three resources.
2. Apply. The two policies and the query are created; the query references the ContX IQ policy's GID.
3. Evaluate the KBAC policy with POST /access/v1/evaluation (subject Person, action CAN_DRIVE, resource Car).
4. Execute the Knowledge Query with POST /contx-iq/v1/execute, passing its GID and the input parameters.
5. To stage a change, set status to draft on the policy, apply, review, then set it back to active.
A KBAC policy, a ContX IQ policy and the Knowledge Query it authorizes, in one file.
terraform {
required_providers {
indykite = {
source = "indykite/indykite"
version = ">= 0.49.0" # or a later release
}
}
}
# indykite provider integrates IndyKite platform with Terraform scripting.
# Provider for now does not support any parameters and all is set within service account credential file.
provider "indykite" {}
# A KBAC policy: may a Person drive a Car they own? Answered by POST /access/v1/evaluation.
# policy_version 2.0-kbac is the structured form; 3.0-kbac takes raw Cypher instead.
resource "indykite_authorization_policy" "policy_drive_car" {
name = "terraform-policy-drive-car"
display_name = "Terraform policy drive car"
description = "A Person may drive a Car they own"
project_id = "gid:AAAAAmluZHlraURlgAABDwAAAAA" # your project GID
status = "active" # active, draft or inactive
json = jsonencode({
meta = {
policy_version = "2.0-kbac"
},
subject = {
type = "Person"
},
actions = ["CAN_DRIVE"],
resource = {
type = "Car"
},
condition = {
cypher = "MATCH (subject:Person)-[:OWNS]->(resource:Car)"
}
})
}
# A ContX IQ policy: which graph data a Knowledge Query may read, and for whom.
# policy_version 1.0-ciq; allowed_reads lists the properties a query may return.
resource "indykite_authorization_policy" "policy_for_ciq" {
name = "terraform-policy-for-ciq"
display_name = "Terraform policy for ContX IQ"
description = "Lets an application read the license numbers of the vehicles a person's contracts cover"
project_id = "gid:AAAAAmluZHlraURlgAABDwAAAAA" # your project GID
status = "active"
json = jsonencode({
meta = {
policy_version = "1.0-ciq"
},
subject = {
type = "_Application"
},
condition = {
cypher = "MATCH (subject:_Application) MATCH (person:Person)-[r1:ACCEPTED]->(contract:Contract)-[r2:COVERS]->(vehicle:Vehicle)-[r3:HAS]->(ln:LicenseNumber)",
filter = [
{
operator = "AND",
operands = [
{
attribute = "person.property.email",
operator = "=",
value = "$person_email"
},
{
attribute = "subject.external_id",
operator = "=",
value = "$_appId"
}
]
}
]
},
allowed_reads = {
nodes = ["ln.property.number", "ln.property.transferrable"],
relationships = []
}
})
}
# The Knowledge Query executed through POST /contx-iq/v1/execute; it must be
# authorized by a ContX IQ policy, referenced by policy_id.
resource "indykite_knowledge_query" "license_number" {
name = "terraform-knowledge-query"
display_name = "Terraform knowledge query"
description = "Returns the license number matching an input parameter"
project_id = "gid:AAAAAmluZHlraURlgAABDwAAAAA" # your project GID
status = "active"
policy_id = indykite_authorization_policy.policy_for_ciq.id
query = jsonencode({
nodes = ["ln.property.number"],
relationships = [],
filter = {
attribute = "ln.property.number",
operator = "=",
value = "$ln_number"
}
})
}