Skip to content

Data JSON

The TAM appliance has now been enhanced to support data.json file format for data storage and retrieval. The data.json file is a JSON file that contains data that can be used by the policy engine to make decisions. The data.json file can be used to store data that is not expected to change frequently, such as configuration data, reference data, or static data.

An example usecase

For example, consider a scenario where a policy needs to evaluate the firmware version of a device to determine whether the device is eligible to receive a firmware update. The firmware version of the device is stored in the data.json file. The policy engine can use the data.json file to retrieve the firmware version of the device and evaluate it against the firmware version provided in the input.

Example

To support data.json during package registration, ensure the following folder structure is maintained. The folder name should be in the namespace format like “namespace”. It is recommended to keep the Rego files in the rego folder and the test files in a separate test folder outside of the namespace folder. Please refer to the folder structure below.

Folder structure

namespace/
├── rego/
└── policy.rego
├── data.json
test/
└── policy_test.rego

Sample rego file to access data from data.json file

package firmware.update
# Example input structure:
# {
# "device_id": "device123",
# "target_firmware_version": "1.1.0"
# }
default allow_update = false
allow_update {
device_id := input.device_id
current_version := data["namespace"].rego.devices[device_id].firmware_version
target_version := input.target_firmware_version
# Simple comparison: allow update if current version is less than target version
current_version < target_version
}

How to access data from data.json file in Rego policy

As per the above example, we can access the data from the data.json file using the data keyword followed by the relative folder location of the data.json. For example, if data.json is found inside the rego folder, we can access that data using data["namespace"].rego.devices[device_id].firmware_version. In this example, the key is a variable device_id which is used to access the firmware version of the device.

All data.json files will be bundled into a single data.json file during bundling, so the folder location is required to differentiate different data in the data.json file.

How data.json file looks like:

{
"devices": {
"device123": {
"firmware_version": "1.0.0"
}
}
}

Sample Rego Test File

Below is a sample Rego test file to demonstrate the policy testing:

package firmware.update
import future.keywords
# Test cases
test_allow_update_denied if {
not allow_update with input as {
"device_id": "device123",
"target_firmware_version": "1.1.0"
}
}
test_allow_update_allowed if {
allow_update with input as {
"device_id": "device123",
"target_firmware_version": "0.1.0"
}
}

Package registration with data.json

Terminal window
./tam package register --packageDependencies trimble.tam.account --environment Stage --consumerKey ${clientId} --consumerSecret ${secret} --packageNamespace ${package namespace} --pathToFiles "./namespace/" --pathToTestFiles ./test/

Note:

The path to files should contain the rego and test folders and not the file paths.