Bundles

A bundle holds the policy and the data of one deployment, as OPA's bundles do. rego4k-bundles-core reads, compiles and writes bundles, and does tree shaking.

A bundle holds:

rego4k-bundles-signing verifies and signs bundles.

Read and compile a bundle

Bundles.kt
val rego = Rego.standard(Re2Engine)
// a directory or a .tar.gz file
val bundle = Bundle.read(bundlePath)
// checks the roots of the manifest too
val policy = bundle.compile(rego)
// the data files, as one document
val data = bundle.data(rego)
val allowed = policy.allowed("data.authz.allow", input, data)
Bundles.kt, lines 28 to 35, on GitHub

Bundle.read checks the paths, the manifest, the wasm entries and the modules at duplicate paths, as OPA does, with the messages of OPA.

A bundle can also have example inputs, the files examples/<name>.input.json in any directory. OPA ignores these files. The debugger offers them as inputs.

Write a bundle

write writes a .tar.gz file. The archive is deterministic: the same bundle gives the same bytes.

hash gives the SHA-256 hash of the contents, with or without the revision of the manifest. A signed decision names the policy by this hash.

Bundles.kt
// the same bundle gives the same bytes
archive.outputStream().use { bundle.write(it) }
// the hash of the contents, without the revision
val hash = bundle.hash(RevisionHashTreatment.EXCLUDED)
Bundles.kt, lines 44 to 47, on GitHub

Tree shaking

shake keeps only the rules and the data that the entrypoints can reach, and lists the builtins that the shaken bundle needs.

shakeDryRun shows the lines that tree shaking keeps, deletes and adds in each file, with no change to the bundle. The decision server shows the dry run as a page (see The decision server).

Bundles.kt
// the bundle, and the builtins that it needs
val shaken = bundle.shake(rego, "authz/allow")
// the lines that tree shaking keeps, deletes and adds
val dryRun = bundle.shakeDryRun(rego, "authz/allow")
Bundles.kt, lines 57 to 60, on GitHub

The README of rego4k-bundles-core explains which rules and data tree shaking keeps, and how it treats the policy.wasm files.

With --wasm, rego4k shake builds new wasm modules with opa build -t wasm.

Signed bundles

rego4k-bundles-signing verifies and signs bundles with the algorithms and the options of OPA:

SignedBundle.read verifies a bundle, as OPA does with a signing configuration. writeSigned signs a bundle and writes it.

Bundles.kt
val out = ByteArrayOutputStream()
bundle.writeSigned(
    out,
    SigningConfig(privateKey.readText(), algorithm = "ES256", keyId = "global_key"),
    rego,
)
val config = VerificationConfig(
    mapOf("global_key" to KeyConfig(publicKey.readText(), algorithm = "ES256")),
)
// or a BundleException
val verified =
    SignedBundle.read(ByteArrayInputStream(out.toByteArray()), config, rego)
Bundles.kt, lines 70 to 81, on GitHub

Bundle.read rejects a signed bundle, because it cannot verify it. The README of rego4k-bundles-signing lists the algorithms and the keys.