Skip to content

feat(policies): Chainloop discover custom builtin#2558

Merged
migmartri merged 1 commit intochainloop-dev:mainfrom
jiparis:PFM-781-discover
Nov 17, 2025
Merged

feat(policies): Chainloop discover custom builtin#2558
migmartri merged 1 commit intochainloop-dev:mainfrom
jiparis:PFM-781-discover

Conversation

@jiparis
Copy link
Member

@jiparis jiparis commented Nov 17, 2025

This PR extends the CLI to support access to the Discover endpoint as a custom Rego builtin function. This is an example of extending the engine using the extension mechanism provided in #2552

To use it in policies:

apiVersion: workflowcontract.chainloop.dev/v1
kind: Policy
metadata:
  name: policy-builtins
spec:
  policies:
    - kind: CONTAINER_IMAGE
      embedded: |
        package main
        import rego.v1
        
        result := {"violations": violations}
        
        violations contains msg if {
          digest := sprintf("sha256:%s",[input.chainloop_metadata.digest.sha256])
          discovered := chainloop.discover(digest, "")
          
          some ref in discovered.references
          ref.kind == "ATTESTATION"
          ref.metadata.hasPolicyViolations == "true"
        
          msg:= sprintf("attestation with digest %s contains policy violations [name: %s, project: %s, org: %s]", [ref.digest, ref.metadata.name, ref.metadata.project, ref.metadata.organization])
        }
> go run app/cli/main.go policy devel eval --material ghcr.io/chainloop-dev/chainloop/control-plane:v1.57.0-amd64 --policy test/policy-builtins.yaml
{
   "result": {
      "violations": [
         "attestation with digest sha256:5d2ae3589cd46277fb83b1e9a3eb4c58dfb0c1e042729520119ed0533ef7cd6d contains policy violations [name: release, project: chainloop, org: read-only-demo]"
      ],
      "skip_reasons": [],
      "skipped": false
   }
}

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

@migmartri migmartri left a comment

Choose a reason for hiding this comment

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

Very cool!

// }
//
// ```
func RegisterDiscoverBuiltin(conn *grpc.ClientConn) error {
Copy link
Member

Choose a reason for hiding this comment

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

one thing it would be nice to do (in the future) is to generate documentation of these built-in. We do smth for integrations already, like creating a markdown that then we can sync somewhere.

// }
//
// ```
func RegisterDiscoverBuiltin(conn *grpc.ClientConn) error {
Copy link
Member

Choose a reason for hiding this comment

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

I think that in the future the input should evolve to a struct, so we can have controlplaneConnection, CAS, and platform connections if needed.

}, getDiscoverImpl(conn))
}

func getDiscoverImpl(conn *grpc.ClientConn) topdown.BuiltinFunc {
Copy link
Member

Choose a reason for hiding this comment

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

it would be nice to have logger support, so when running devel with --debug we can see some info

@migmartri migmartri merged commit 2bb154d into chainloop-dev:main Nov 17, 2025
13 checks passed
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