Skip to content

feat(policies): allow custom builtin functions in Rego policies#2552

Merged
jiparis merged 12 commits into
chainloop-dev:mainfrom
jiparis:PFM-781
Nov 16, 2025
Merged

feat(policies): allow custom builtin functions in Rego policies#2552
jiparis merged 12 commits into
chainloop-dev:mainfrom
jiparis:PFM-781

Conversation

@jiparis

@jiparis jiparis commented Nov 15, 2025

Copy link
Copy Markdown
Member

This PR adds extension capabilities to the Rego engine by using the builtins mechanism from OPA. Registering a new function is done through a call to builtins.Register with the builtin definition:

func RegisterHelloBuiltin() error {
	return Register(&ast.Builtin{
		Name: helloBuiltinName,
		Decl: types.NewFunction(
			types.Args(
				types.Named("name", types.S), // Digest to fetch
			),
			types.Named("response", types.A), // Response as object
		),
	}, getHelloImpl)
}

Implementation signature must be a topdown.BuiltinFunc. Check provided example and tests.

This call must be done before using the Rego engine, so ideally it should be done during package initialization (init()) or app initialization before instantiating Rego.

Signed-off-by: Jose I. Paris <jiparis@chainloop.dev>
Signed-off-by: Jose I. Paris <jiparis@chainloop.dev>
Signed-off-by: Jose I. Paris <jiparis@chainloop.dev>
Signed-off-by: Jose I. Paris <jiparis@chainloop.dev>
Signed-off-by: Jose I. Paris <jiparis@chainloop.dev>
}

func craftMaterial(materialPath, materialKind string, logger *zerolog.Logger) (*v12.Attestation_Material, error) {
if fileNotExists(materialPath) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be able to devel CONTAINER_IMAGE policies.

Signed-off-by: Jose I. Paris <jiparis@chainloop.dev>
Signed-off-by: Jose I. Paris <jiparis@chainloop.dev>
Signed-off-by: Jose I. Paris <jiparis@chainloop.dev>

@migmartri migmartri left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, see my comments, thanks!

Comment thread CLAUDE.md Outdated
Comment thread pkg/policies/engine/rego/builtins/errors.go Outdated
Comment thread pkg/policies/engine/rego/builtins/registry.go Outdated
Comment thread pkg/policies/engine/rego/builtins/registry.go Outdated
Comment thread pkg/policies/engine/rego/rego.go Outdated
Signed-off-by: Jose I. Paris <jiparis@chainloop.dev>
Signed-off-by: Jose I. Paris <jiparis@chainloop.dev>
Signed-off-by: Jose I. Paris <jiparis@chainloop.dev>
Signed-off-by: Jose I. Paris <jiparis@chainloop.dev>
@jiparis jiparis merged commit d4cc444 into chainloop-dev:main Nov 16, 2025
13 checks passed
@jiparis jiparis deleted the PFM-781 branch November 16, 2025 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants