{
  "id": "authz-8",
  "title": "KBAC 3.0: Data Residency - How do I route AuthZEN decisions by location?",
  "description": "Route authorization decisions to a specific location of a composite IKG at request time. A 3.0-kbac policy declares USE graph.byName($region); each AuthZEN call supplies a logical location (an alias_mapping key) in context.input_params, and IndyKite translates it to the physical constituent database.",
  "category": "KBAC",
  "tags": [
    "KBAC Policy",
    "3.0-kbac",
    "Data Residency",
    "Location Parameter",
    "Composite Database",
    "authZEN",
    "Input Params"
  ],
  "api": "KBAC",
  "language": "Json",
  "lastUpdated": "2026-07-14",
  "openApiEndpoints": [
    "/configs/v1/authorization-policies",
    "/access/v1/evaluation",
    "/access/v1/search/resource"
  ],
  "relatedGuides": [
    "/guides/guide-data-residency",
    "/guides/guide-authzen",
    "/guides/guide-dynamic-authz"
  ],
  "commonErrors": [
    {
      "code": "422",
      "message": "unknown location \"...\" for parameter \"$region\"",
      "solution": "The location must be a key of the project's alias_mapping (global, east, west in this example). Send the logical location, never the Neo4j database name"
    },
    {
      "code": "422",
      "message": "location parameter \"$region\" requires a composite database, but the app space has none configured",
      "solution": "The policy declares graph.byName($param) but the project has no composite_db_name configured. Configure the composite connection (see residency-1) or use a policy without location routing"
    },
    {
      "code": "422",
      "message": "location parameter \"$region\" must be a non-empty string",
      "solution": "Every location parameter declared by the policy must arrive in context.input_params as a non-empty string"
    },
    {
      "code": "403",
      "message": "bearer token subject differs from requested subject",
      "solution": "On bearer-token calls to a 3.0-kbac policy, the token's subject must match the subject in the request. Send the decision request for the user the token belongs to"
    }
  ],
  "summary": "This example is 3.0-kbac ONLY - it makes AuthZEN decisions location-aware on a composite IKG.\n\nHow it works:\n1. The policy condition starts with USE graph.byName($region). Because $region routes graph.byName(), it becomes a LOCATION PARAMETER.\n2. Each AuthZEN request supplies a logical location in context.input_params (e.g. {\"region\": \"east\"}). Logical locations are the KEYS of the project's alias_mapping (global=db1&east=db2&west=db3) - callers never see or send physical database names.\n3. Just before execution, IndyKite translates the logical location to the physical constituent (e.g. ikcomposite.db2) and runs the raw condition there.\n\nThe same request works on every AuthZEN endpoint: /access/v1/evaluation, /access/v1/evaluations (put the location in the default or per-evaluation context), and the three search endpoints.\n\nLocation parameter rules:\n- A location parameter must not be referenced anywhere else in the Cypher (its value is rewritten to the physical alias at request time).\n- It must arrive as a non-empty string, and must be a key of alias_mapping - otherwise the call fails with 422 Unprocessable Entity.\n- Policies without any graph.byName($param) simply have no location parameters - nothing changes for them.\n\nResidency is opt-in per policy: 2.0-kbac policies keep running against the default database, untouched. 3.0-kbac itself does not require a composite database either - only USE routing does; a 3.0-kbac policy without USE evaluates against the default database as plain raw Cypher.",
  "usecase": "Scenario: A car-sharing platform stores each jurisdiction's personal data in its own constituent database (east, west), as set up in resource residency-1. The mobile app knows which jurisdiction the signed-in user belongs to and asks for decisions in that location:\n\n- \"Can alice drive KITT?\" with region=east -> true: alice's full data and her OWNS relationship live in the east constituent.\n- The same question with region=west -> false: the west constituent has no such data.\n- region=north -> 422 Unprocessable Entity: north is not a key of the project's alias_mapping.\n- \"Which cars can alice drive in east?\" (search/resource with region=east) -> [car-kitt].\n\nOne policy serves every location; the caller picks the location per request.",
  "requirements": "Prerequisites:\n- A customer-hosted composite-database project with alias_mapping, e.g. global=db1&east=db2&west=db3 (see resource residency-1 for the full setup)\n- Nodes ingested with locations: Person(person-alice) and Car(car-kitt) with an OWNS relationship in the east location\n- ServiceAccount credentials: For creating policies (Bearer token)\n- AppAgent credentials: For authorization queries (X-IK-ClientKey)\n\nRequired API access:\n- POST /configs/v1/authorization-policies (create policy)\n- POST /access/v1/evaluation (single check)\n- POST /access/v1/search/resource (which resources, in this location)\n\nVersion requirement: meta.policy_version must be \"3.0-kbac\" - location parameters do not exist on 2.0-kbac.",
  "steps": "Step 1: Create the location-routed 3.0-kbac policy\n- Authentication: ServiceAccount credential (Bearer token)\n- Action: POST the policy whose condition starts with USE graph.byName($region)\n- Result: $region is registered as a location parameter of the policy\n\nStep 2: Evaluate in the east location\n- Authentication: AppAgent credential (X-IK-ClientKey header)\n- Action: POST to /access/v1/evaluation with context.input_params.region = \"east\"\n- Result: {decision: true} - evaluated inside the constituent mapped to east\n\nStep 3: Evaluate the same question in the west location\n- Action: Same request with region = \"west\"\n- Result: {decision: false} - the west constituent holds no matching data\n\nStep 4: See what happens with an unmapped location\n- Action: Same request with region = \"north\" (not a key of alias_mapping)\n- Result: 422 Unprocessable Entity - unknown location \"north\" for parameter \"$region\"\n\nStep 5: Search resources within a location\n- Action: POST to /access/v1/search/resource with region = \"east\"\n- Result: The list of cars alice can drive according to the east constituent\n\nStep 6: Cleanup\n- Action: DELETE the policy configuration",
  "graph": {
    "caption": "The graph the example runs on: 9 nodes (4 Person, 2 Car, 1 Bus, 1 Ticket, 1 Laptop) and 8 relationships (DRIVES, HAS, FOR, OWNS). Hover a node for its properties, click it to highlight its neighbourhood.",
    "nodes": [
      {
        "external_id": "alice",
        "type": "Person",
        "is_identity": true,
        "properties": [
          {
            "type": "email",
            "value": "alice@email.com"
          },
          {
            "type": "given_name",
            "value": "Alice"
          },
          {
            "type": "last_name",
            "value": "Smith"
          }
        ]
      },
      {
        "external_id": "knightrider",
        "type": "Person",
        "is_identity": true,
        "properties": [
          {
            "type": "email",
            "value": "knightrider@demo.com"
          },
          {
            "type": "name",
            "value": "Michael Knight"
          }
        ]
      },
      {
        "external_id": "satchmo",
        "type": "Person",
        "is_identity": true,
        "properties": [
          {
            "type": "email",
            "value": "satchmo@demo.com"
          },
          {
            "type": "name",
            "value": "Louis Armstrong"
          }
        ]
      },
      {
        "external_id": "karel",
        "type": "Person",
        "is_identity": true,
        "properties": [
          {
            "type": "email",
            "value": "karel@demo.com"
          },
          {
            "type": "name",
            "value": "Karel Plihal"
          }
        ]
      },
      {
        "external_id": "kitt",
        "type": "Car",
        "is_identity": false,
        "properties": [
          {
            "type": "manufacturer",
            "value": "pontiac"
          },
          {
            "type": "model",
            "value": "Firebird"
          }
        ]
      },
      {
        "external_id": "cadillacv16",
        "type": "Car",
        "is_identity": false,
        "properties": [
          {
            "type": "manufacturer",
            "value": "Cadillac"
          },
          {
            "type": "model",
            "value": "V-16"
          }
        ]
      },
      {
        "external_id": "harmonika",
        "type": "Bus",
        "is_identity": false,
        "properties": [
          {
            "type": "manufacturer",
            "value": "Ikarus"
          },
          {
            "type": "model",
            "value": "280"
          }
        ]
      },
      {
        "external_id": "listek",
        "type": "Ticket",
        "is_identity": false,
        "properties": []
      },
      {
        "external_id": "airbook-xyz",
        "type": "Laptop",
        "is_identity": false,
        "properties": []
      }
    ],
    "relationships": [
      {
        "source": "knightrider",
        "target": "kitt",
        "type": "DRIVES",
        "properties": []
      },
      {
        "source": "satchmo",
        "target": "cadillacv16",
        "type": "DRIVES",
        "properties": []
      },
      {
        "source": "karel",
        "target": "listek",
        "type": "HAS",
        "properties": []
      },
      {
        "source": "listek",
        "target": "harmonika",
        "type": "FOR",
        "properties": []
      },
      {
        "source": "karel",
        "target": "airbook-xyz",
        "type": "OWNS",
        "properties": []
      },
      {
        "source": "alice",
        "target": "airbook-xyz",
        "type": "OWNS",
        "properties": []
      },
      {
        "source": "knightrider",
        "target": "kitt",
        "type": "OWNS",
        "properties": []
      },
      {
        "source": "alice",
        "target": "cadillacv16",
        "type": "DRIVES",
        "properties": []
      }
    ]
  },
  "code": [
    {
      "step": "Step 1",
      "description": "3.0-kbac policy with dynamic routing. $region routes graph.byName() and must not be referenced anywhere else in the Cypher. Platform-bound parameters ($subject_external_id, $subject_type, $resource_external_id, $resource_type) cannot be used as routing parameters, and $subject_id must not appear at all.",
      "filename": "policy_location_routed.json",
      "language": "Json",
      "ingest": {
        "meta": {
          "policy_version": "3.0-kbac"
        },
        "subject": {
          "type": "Person"
        },
        "actions": [
          "CAN_DRIVE"
        ],
        "resource": {
          "type": "Car"
        },
        "condition": {
          "cypher": "USE graph.byName($region) MATCH (subject:Person)-[:OWNS]->(resource:Car)"
        }
      }
    },
    {
      "description": "Request to create the location-routed policy using REST.",
      "filename": "POST https://eu.api.indykite.com/configs/v1/authorization-policies",
      "language": "Json",
      "ingest": {
        "project_id": "your_project_gid",
        "description": "3.0-kbac data-residency policy. $region is a location parameter: it routes graph.byName() and must not be referenced anywhere else in the Cypher. Callers supply a logical location (an alias_mapping key such as east or west) in context.input_params.region; IndyKite translates it to the physical constituent database just before execution.",
        "display_name": "policy - can drive (location-routed)",
        "name": "policy-can-drive-location-routed",
        "policy": "{\"meta\":{\"policy_version\":\"3.0-kbac\"},\"subject\":{\"type\":\"Person\"},\"actions\":[\"CAN_DRIVE\"],\"resource\":{\"type\":\"Car\"},\"condition\":{\"cypher\":\"USE graph.byName($region) MATCH (subject:Person)-[:OWNS]->(resource:Car)\"}}",
        "status": "ACTIVE",
        "tags": []
      }
    },
    {
      "step": "Step 2",
      "description": "Evaluation routed to the east location. \"east\" is a logical location - a key of the project's alias_mapping - not a Neo4j database name. IndyKite resolves it to the physical constituent just before running the condition.",
      "filename": "POST https://eu.api.indykite.com/access/v1/evaluation",
      "language": "Json",
      "ingest": {
        "subject": {
          "type": "Person",
          "id": "person-alice"
        },
        "resource": {
          "type": "Car",
          "id": "car-kitt"
        },
        "action": {
          "name": "CAN_DRIVE"
        },
        "context": {
          "input_params": {
            "region": "east"
          }
        }
      }
    },
    {
      "description": "Response - allowed: the OWNS relationship exists in the east constituent.",
      "filename": "Response 200",
      "language": "Json",
      "ingest": {
        "decision": true
      }
    },
    {
      "description": "The same east-routed evaluation in Python.",
      "filename": "evaluation_east.py",
      "language": "Python",
      "code": "\nimport http.client\nimport json\n\nconn = http.client.HTTPSConnection(\"eu.api.indykite.com\")\n\npayload = json.dumps({\n  \"subject\": {\"type\": \"Person\", \"id\": \"person-alice\"},\n  \"resource\": {\"type\": \"Car\", \"id\": \"car-kitt\"},\n  \"action\": {\"name\": \"CAN_DRIVE\"},\n  # \"east\" is a logical location (an alias_mapping key), never a Neo4j database name\n  \"context\": {\"input_params\": {\"region\": \"east\"}}\n})\n\nheaders = {\n  'Content-Type': \"application/json\",\n  'X-IK-ClientKey': \"\"\n}\n\nconn.request(\"POST\", \"/access/v1/evaluation\", payload, headers)\n\nres = conn.getresponse()\ndata = res.read()\n"
    },
    {
      "step": "Step 3",
      "description": "The identical question routed to the west location.",
      "filename": "POST https://eu.api.indykite.com/access/v1/evaluation",
      "language": "Json",
      "ingest": {
        "subject": {
          "type": "Person",
          "id": "person-alice"
        },
        "resource": {
          "type": "Car",
          "id": "car-kitt"
        },
        "action": {
          "name": "CAN_DRIVE"
        },
        "context": {
          "input_params": {
            "region": "west"
          }
        }
      }
    },
    {
      "description": "Response - denied: no matching data in the west constituent. Same subject, same resource, same policy; only the location changed.",
      "filename": "Response 200",
      "language": "Json",
      "ingest": {
        "decision": false
      }
    },
    {
      "step": "Step 4",
      "description": "Request with a location that is not a key of alias_mapping.",
      "filename": "POST https://eu.api.indykite.com/access/v1/evaluation",
      "language": "Json",
      "ingest": {
        "subject": {
          "type": "Person",
          "id": "person-alice"
        },
        "resource": {
          "type": "Car",
          "id": "car-kitt"
        },
        "action": {
          "name": "CAN_DRIVE"
        },
        "context": {
          "input_params": {
            "region": "north"
          }
        }
      }
    },
    {
      "description": "Response - the call fails before evaluation. The error names the logical location and the parameter; the physical database alias is never exposed.",
      "filename": "Response 422",
      "language": "Json",
      "ingest": {
        "message": "Unprocessable Entity",
        "errors": [
          "unknown location \"north\" for parameter \"$region\""
        ]
      }
    },
    {
      "step": "Step 5",
      "description": "Resource search scoped to the east location - which cars can alice drive there? Search endpoints take the location parameter exactly like evaluation.",
      "filename": "POST https://eu.api.indykite.com/access/v1/search/resource",
      "language": "Json",
      "ingest": {
        "subject": {
          "type": "Person",
          "id": "person-alice"
        },
        "action": {
          "name": "CAN_DRIVE"
        },
        "resource": {
          "type": "Car"
        },
        "context": {
          "input_params": {
            "region": "east"
          }
        }
      }
    },
    {
      "description": "Response - resource references from the east constituent.",
      "filename": "Response 200",
      "language": "Json",
      "ingest": {
        "results": [
          {
            "type": "Car",
            "id": "car-kitt"
          }
        ]
      }
    },
    {
      "step": "Step 6",
      "description": "Delete the policy when done.",
      "filename": "DELETE https://eu.api.indykite.com/configs/v1/authorization-policies/{id}",
      "language": "Json",
      "ingest": {
        "id": "your_policy_configuration_gid"
      }
    }
  ],
  "links": {
    "html": "/resources/authz-8",
    "markdown": "/resources/authz-8.md",
    "openApi": "https://openapi.indykite.com/"
  }
}