Skip to content

Policy Access Control#

This section explains how you can configure Policy Access Control in piveau-hub-search and show examples.

Setup#

1. Check your versions#

To use policy access control, use hub-repo 4.6.0 or later and hub-search 5.3.14 or later. If you’re developing locally, use piveau-indexing 1.0.37 or later in both services.

Component Version needed
hub-search 5.3.14 or later
hub-repo 4.6.0 or later
piveau-indexing 1.0.37 or later

2. Configure Keycloak#

Hub-search checks the ODRL policy against properties in the user’s access token. Make sure Keycloak includes the properties your policy uses. For example if you need to check against the country and groups:

{
  "iss": "https://auth.example.org/realms/mobility",
  "sub": "bd31b930-87a9-4ac0-8ae0-36a9f9ae4061",
  "azp": "mobility-portal",
  "typ": "Bearer",
  "iat": 1795420800,
  "exp": 1795421100,
  "preferred_username": "lena",
  "groups": ["mobility-team"],
  "country": "DE"
}

Update the hub-search configuration to turn on policy access control and map each dataset policy constraint to the Keycloak access token property it checks.

Minimal configuration#

This minimal configuration turns on policy access control. As an example, it checks the countries constraint in a dataset or custom resource’s ODRL policy against the country property in the user’s Keycloak access token. Download plain JSON.

{
  "PIVEAU_FEATURE_FLAGS": {
    // Enable Policy Access Control.
    "piveau_access_control_policy": true
  },
  "PIVEAU_ACCESS_CONTROL": {
    "policy": {
      // Match policy properties to Keycloak token properties.
      "claim_mappings": [
        {
          // Matches odrl:leftOperand "countries" in the policy.
          "constraint_name": "countries",
          // Matches the "country" property in the Keycloak access token.
          "token_path": "country"
        }
      ]
    }
  }
}

Full configuration#

This full configuration turns on policy access control for datasets and custom resources. It checks the countries constraint in the ODRL policy against the country property in the user’s Keycloak access token. It also checks the groups constraint against the token’s groups property. Download plain JSON.

{
  "PIVEAU_FEATURE_FLAGS": {
    // Enable Policy Access Control.
    "piveau_access_control_policy": true,
    // Disable RBAC Access Control.
    "piveau_access_control_v2": false
  },
  "PIVEAU_ACCESS_CONTROL": {
    "policy": {
      // For documents without a policy:
      // ALLOW grants access to everyone, including anonymous users.
      // DENY denies access to everyone.
      "default_decision": "DENY",
      // Skip policy checks for catalogues. Dataset policies still apply.
      // Use "resource" for all custom resource types or "resource_<name>" for one type.
      "exempt_types": ["catalogue"],
      // Match policy properties to Keycloak token properties.
      "claim_mappings": [
        {
          // Matches odrl:leftOperand "countries" in the policy.
          "constraint_name": "countries",
          // Matches the "country" property in the Keycloak access token.
          "token_path": "country"
        },
        {
          // Matches odrl:leftOperand "groups" in the policy.
          "constraint_name": "groups",
          // Matches the "groups" property in the Keycloak access token.
          // For a nested property, use its path, for example "piveau.user.groups".
          "token_path": "groups"
        }
      ]
    }
  }
}

4. Configure the piveau Profile#

Configure your piveau Profile to include ODRL policies. The example below shows a dataset shape with a title, a description, and an ODRL policy. Use the same profile in hub-repo and hub-search.

@prefix dcat: <http://www.w3.org/ns/dcat#> .
@prefix dct: <http://purl.org/dc/terms/> .
@prefix odrl: <http://www.w3.org/ns/odrl/2/> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix pv: <https://piveau.eu/ns/voc#> .
@prefix ex: <https://example.org/policy-profile/> .

# Map the dataset title, description, and policy
ex:DatasetShape
    a sh:NodeShape ;
    sh:targetClass dcat:Dataset ;
    sh:property [
        sh:path dct:title ;
        pv:mappingClass "SimpleMultiLangTitle" ;
        pv:mappingName "title"
    ], [
        sh:path dct:description ;
        pv:mappingClass "SimpleMultiLang" ;
        pv:mappingName "description"
    ], [
        sh:path odrl:hasPolicy ;
        pv:mappingClass "NestedType" ;
        pv:mappingName "has_policy" ;
        pv:mappingLink ex:PolicyShape
    ] .

# Map the permissions in the policy
ex:PolicyShape
    a sh:NodeShape ;
    sh:targetClass odrl:Policy ;
    sh:property [
        sh:path odrl:permission ;
        pv:mappingClass "NestedType" ;
        pv:mappingName "permissions" ;
        pv:mappingLink ex:PermissionShape
    ] .

# Map the action and constraints for each permission
ex:PermissionShape
    a sh:NodeShape ;
    sh:targetClass odrl:Permission ;
    sh:property [
        sh:path odrl:action ;
        pv:mappingClass "ShortUri" ;
        pv:mappingName "action"
    ], [
        sh:path odrl:constraint ;
        pv:mappingClass "NestedType" ;
        pv:mappingName "constraints" ;
        pv:mappingLink ex:ConstraintShape
    ] .

# Map the fields that hub-search uses to check the token
ex:ConstraintShape
    a sh:NodeShape ;
    sh:targetClass odrl:Constraint ;
    sh:property [
        sh:path odrl:leftOperand ;
        sh:maxCount 1 ;
        pv:mappingClass "StandardKeyword" ;
        pv:mappingName "leftOperand"
    ], [
        sh:path odrl:operator ;
        sh:maxCount 1 ;
        pv:mappingClass "ShortUri" ;
        pv:mappingName "operator"
    ], [
        sh:path odrl:rightOperand ;
        pv:mappingClass "StandardKeyword" ;
        pv:mappingName "rightOperand"
    ] .

Here are two example datasets that use this profile. The first allows access only to users from Germany. The second excludes external contractors.

@prefix dcat: <http://www.w3.org/ns/dcat#> .
@prefix dct: <http://purl.org/dc/terms/> .
@prefix odrl: <http://www.w3.org/ns/odrl/2/> .

# Dataset metadata
<https://example.org/datasets/regional-mobility-data> a dcat:Dataset ;
    dct:title "Regional mobility dataset"@en ;
    dct:description "Monthly passengers by station."@en ;

    # Policy for this dataset
    odrl:hasPolicy [ a odrl:Policy ;
        odrl:permission [ a odrl:Permission ;
            # This permission controls read access
            odrl:action odrl:read ;

            # Allow users whose Keycloak token has country DE
            odrl:constraint [ a odrl:Constraint ;
                odrl:leftOperand "countries" ;
                odrl:operator odrl:isAnyOf ;
                odrl:rightOperand "DE"
            ]
        ]
    ] .
@prefix dcat: <http://www.w3.org/ns/dcat#> .
@prefix dct: <http://purl.org/dc/terms/> .
@prefix odrl: <http://www.w3.org/ns/odrl/2/> .

# Dataset metadata
<https://example.org/datasets/internal-mobility-report> a dcat:Dataset ;
    dct:title "Internal mobility report"@en ;
    dct:description "Monthly mobility figures for staff."@en ;

    # Policy for this dataset
    odrl:hasPolicy [ a odrl:Policy ;
        odrl:permission [ a odrl:Permission ;
            # This permission controls read access
            odrl:action odrl:read ;

            # Deny users whose Keycloak token lists external-contractors
            odrl:constraint [ a odrl:Constraint ;
                odrl:leftOperand "groups" ;
                odrl:operator odrl:isNoneOf ;
                odrl:rightOperand "external-contractors"
            ]
        ]
    ] .

Examples#

The following simplified regional mobility dataset is used in this example. It includes an ODRL policy.

@prefix dcat: <http://www.w3.org/ns/dcat#> .
@prefix odrl: <http://www.w3.org/ns/odrl/2/> .

<https://piveau.io/set/data/regional-mobility-data>
    a dcat:Dataset ;
    odrl:hasPolicy <https://piveau.io/set/policy/1> .

<https://piveau.io/set/policy/1>
    a odrl:Policy ;
    odrl:permission [
        a odrl:Permission ;
        odrl:action odrl:read ;
        odrl:constraint [
            odrl:leftOperand "countries" ;
            odrl:operator odrl:isAnyOf ;
            odrl:rightOperand "DE"
        ]
    ] .

The profile uses the ShortUri mapping class for the action and operator. In the hub-search index, odrl:read becomes "read" and odrl:isAnyOf becomes "isAnyOf".

{
  "id": "regional-mobility-data",
  "has_policy": [
    {
      "permissions": [
        {
          "constraints": [
            {
              "rightOperand": [
                "DE"
              ],
              "operator": "isAnyOf",
              "leftOperand": "countries"
            }
          ],
          "action": [
            "read"
          ]
        }
      ]
    }
  ]
}

In this example, only users from Germany can access the regional mobility dataset. Lena is from Germany, so she can access it. Mateo is from Spain, so he cannot.

Example 1: Lena from Germany can access the regional mobility dataset#

Lena has a Keycloak access token that shows her country is Germany. She requests a dataset with an ODRL policy that allows read access only to users from Germany.

In the figure below, Lena sends her access token with the request. Hub-search checks that her country matches the policy and returns the dataset to Lena.

Lena sends her Keycloak access token with country DE. Hub-search matches it to the dataset’s ODRL policy and returns the dataset metadata with 200 OK. The decoded token and indexed policy appear below the request.

Example 2: Mateo from Spain cannot access the regional mobility dataset#

Mateo requests the same dataset. His Keycloak access token shows his country is Spain. Hub-search checks his token against the same policy and denies the request.

Mateo sends his Keycloak access token with country ES. Hub-search checks the same policy and returns 403 Forbidden. The decoded token and indexed policy appear below the request.

Common issues#

What you see What to check
You can open a dataset, but you can’t find it in a search or dataset list. In your piveau Profile, make sure the has_policy, permissions, and constraints fields each use the NestedType mapping class. In SHACL, set pv:mappingClass "NestedType" on each field.
Lena gets 403 Forbidden for the Germany-only dataset. Check that her current token contains "country": "DE". In hub-search, token_path must match country in the token and constraint_name must match countries in the policy.