{
  "id": "terraform-9",
  "title": "KBAC policy, ContX IQ policy and Knowledge Query",
  "description": "Declare a KBAC authorization policy, a ContX IQ policy and the Knowledge Query it authorizes, with the indykite_authorization_policy and indykite_knowledge_query resources.",
  "category": "KBAC",
  "tags": [
    "KBAC",
    "AuthZEN",
    "ContX IQ",
    "Knowledge Query",
    "Authorization Policy"
  ],
  "lastUpdated": "2026-10-12",
  "terraformProviderDocs": [
    "https://registry.terraform.io/providers/indykite/indykite/latest/docs/resources/authorization_policy",
    "https://registry.terraform.io/providers/indykite/indykite/latest/docs/resources/knowledge_query"
  ],
  "relatedGuides": [
    "/guides/guide-authzen",
    "/guides/guide-contx-iq",
    "/guides/guide-terraform"
  ],
  "commonErrors": [
    {
      "code": "INVALID_ARGUMENT",
      "message": "Policy is not valid JSON",
      "solution": "The policy document must be valid JSON and a valid policy: check meta.policy_version (2.0-kbac or 3.0-kbac for KBAC, 1.0-ciq for ContX IQ), the subject and resource types, and that the Cypher condition parses. The AuthZEN and ContX IQ guides document every field."
    },
    {
      "code": "INVALID_ARGUMENT",
      "message": "authorization policy not found or is not CIQ policy",
      "solution": "policy_id must be the GID of a ContX IQ policy in the same project; reference the resource attribute rather than copying an ID."
    },
    {
      "code": "ALREADY_EXISTS",
      "message": "Conflict",
      "solution": "name is unique within the project and immutable. Choose another name, or import the existing policy or query into Terraform state."
    }
  ],
  "summary": "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.\nThis Terraform configuration creates the three objects together:\n1. A KBAC policy (policy_version 2.0-kbac) allowing a Person to drive a Car they own.\n2. A ContX IQ policy (policy_version 1.0-ciq) with the Cypher pattern, the filter and the allowed_reads of a read query.\n3. A Knowledge Query that returns a license number for an input parameter, authorized by the ContX IQ policy through policy_id.\nBoth resources take the policy or query as JSON, written here with jsonencode so Terraform validates the structure.",
  "usecase": "Scenario: a team keeps its authorization rules in version control next to the rest of the project.\n- 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.\n- 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.\n- 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.\n- 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.\n- A project GID for project_id.\n- 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.\n2. Apply. The two policies and the query are created; the query references the ContX IQ policy's GID.\n3. Evaluate the KBAC policy with POST /access/v1/evaluation (subject Person, action CAN_DRIVE, resource Car).\n4. Execute the Knowledge Query with POST /contx-iq/v1/execute, passing its GID and the input parameters.\n5. To stage a change, set status to draft on the policy, apply, review, then set it back to active.",
  "code": [
    {
      "description": "A KBAC policy, a ContX IQ policy and the Knowledge Query it authorizes, in one file.",
      "filename": "main.tf",
      "code": "\nterraform {\n  required_providers {\n    indykite = {\n      source  = \"indykite/indykite\"\n      version = \">= 0.49.0\" # or a later release\n    }\n  }\n}\n\n# indykite provider integrates IndyKite platform with Terraform scripting.\n# Provider for now does not support any parameters and all is set within service account credential file.\nprovider \"indykite\" {}\n\n# A KBAC policy: may a Person drive a Car they own? Answered by POST /access/v1/evaluation.\n# policy_version 2.0-kbac is the structured form; 3.0-kbac takes raw Cypher instead.\nresource \"indykite_authorization_policy\" \"policy_drive_car\" {\n  name         = \"terraform-policy-drive-car\"\n  display_name = \"Terraform policy drive car\"\n  description  = \"A Person may drive a Car they own\"\n  project_id   = \"gid:AAAAAmluZHlraURlgAABDwAAAAA\" # your project GID\n  status       = \"active\"                          # active, draft or inactive\n  json = jsonencode({\n    meta = {\n      policy_version = \"2.0-kbac\"\n    },\n    subject = {\n      type = \"Person\"\n    },\n    actions = [\"CAN_DRIVE\"],\n    resource = {\n      type = \"Car\"\n    },\n    condition = {\n      cypher = \"MATCH (subject:Person)-[:OWNS]->(resource:Car)\"\n    }\n  })\n}\n\n# A ContX IQ policy: which graph data a Knowledge Query may read, and for whom.\n# policy_version 1.0-ciq; allowed_reads lists the properties a query may return.\nresource \"indykite_authorization_policy\" \"policy_for_ciq\" {\n  name         = \"terraform-policy-for-ciq\"\n  display_name = \"Terraform policy for ContX IQ\"\n  description  = \"Lets an application read the license numbers of the vehicles a person's contracts cover\"\n  project_id   = \"gid:AAAAAmluZHlraURlgAABDwAAAAA\" # your project GID\n  status       = \"active\"\n  json = jsonencode({\n    meta = {\n      policy_version = \"1.0-ciq\"\n    },\n    subject = {\n      type = \"_Application\"\n    },\n    condition = {\n      cypher = \"MATCH (subject:_Application) MATCH (person:Person)-[r1:ACCEPTED]->(contract:Contract)-[r2:COVERS]->(vehicle:Vehicle)-[r3:HAS]->(ln:LicenseNumber)\",\n      filter = [\n        {\n          operator = \"AND\",\n          operands = [\n            {\n              attribute = \"person.property.email\",\n              operator  = \"=\",\n              value     = \"$person_email\"\n            },\n            {\n              attribute = \"subject.external_id\",\n              operator  = \"=\",\n              value     = \"$_appId\"\n            }\n          ]\n        }\n      ]\n    },\n    allowed_reads = {\n      nodes         = [\"ln.property.number\", \"ln.property.transferrable\"],\n      relationships = []\n    }\n  })\n}\n\n# The Knowledge Query executed through POST /contx-iq/v1/execute; it must be\n# authorized by a ContX IQ policy, referenced by policy_id.\nresource \"indykite_knowledge_query\" \"license_number\" {\n  name         = \"terraform-knowledge-query\"\n  display_name = \"Terraform knowledge query\"\n  description  = \"Returns the license number matching an input parameter\"\n  project_id   = \"gid:AAAAAmluZHlraURlgAABDwAAAAA\" # your project GID\n  status       = \"active\"\n  policy_id    = indykite_authorization_policy.policy_for_ciq.id\n  query = jsonencode({\n    nodes         = [\"ln.property.number\"],\n    relationships = [],\n    filter = {\n      attribute = \"ln.property.number\",\n      operator  = \"=\",\n      value     = \"$ln_number\"\n    }\n  })\n}\n"
    }
  ],
  "links": {
    "html": "/terraform/terraform-9",
    "markdown": "/terraform/terraform-9.md",
    "terraformRegistry": "https://registry.terraform.io/providers/indykite/indykite/latest/docs"
  }
}