|
| 1 | +// |
| 2 | +// Copyright 2025 The Chainloop Authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package builtins |
| 17 | + |
| 18 | +import ( |
| 19 | + "errors" |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "github.com/open-policy-agent/opa/v1/ast" |
| 23 | + "github.com/open-policy-agent/opa/v1/topdown" |
| 24 | + "github.com/open-policy-agent/opa/v1/types" |
| 25 | + "google.golang.org/grpc" |
| 26 | +) |
| 27 | + |
| 28 | +const discoverBuiltinName = "chainloop.discover" |
| 29 | + |
| 30 | +// RegisterDiscoverBuiltin registers chainloop's Discover endpoint as a builtin Rego function with signature: |
| 31 | +// |
| 32 | +// chainloop.discover(digest, kind) |
| 33 | +// |
| 34 | +// For instance, to get the references for an CONTAINER_IMAGE material, and fail if any of them is an attestation with policy violations: |
| 35 | +// ``` |
| 36 | +// |
| 37 | +// violations contains msg if { |
| 38 | +// digest := sprintf("sha256:%s",[input.chainloop_metadata.digest.sha256]) |
| 39 | +// discovered := chainloop.discover(digest, "") |
| 40 | +// |
| 41 | +// some ref in discovered.references |
| 42 | +// ref.kind == "ATTESTATION" |
| 43 | +// ref.metadata.hasPolicyViolations == "true" |
| 44 | +// |
| 45 | +// 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]) |
| 46 | +// } |
| 47 | +// |
| 48 | +// ``` |
| 49 | +func RegisterDiscoverBuiltin(conn *grpc.ClientConn) error { |
| 50 | + return register(&ast.Builtin{ |
| 51 | + Name: discoverBuiltinName, |
| 52 | + Description: "Discovers artifact graph data by calling the Referrer chainloop service", |
| 53 | + Decl: types.NewFunction( |
| 54 | + types.Args( |
| 55 | + types.Named("digest", types.S).Description("digest of the artifact to discover"), |
| 56 | + types.Named("kind", types.S).Description("optional filter by kind to disambiguate"), |
| 57 | + ), |
| 58 | + types.Named("response", types.A).Description("response object as in the `chainloop discover` CLI output"), |
| 59 | + ), |
| 60 | + Nondeterministic: true, |
| 61 | + }, getDiscoverRegoImpl(conn)) |
| 62 | +} |
| 63 | + |
| 64 | +func getDiscoverRegoImpl(conn *grpc.ClientConn) topdown.BuiltinFunc { |
| 65 | + discoverSvc := NewDiscoverService(conn) |
| 66 | + |
| 67 | + return func(bctx topdown.BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error { |
| 68 | + if len(operands) < 1 { |
| 69 | + return errors.New("need at least one operand") |
| 70 | + } |
| 71 | + |
| 72 | + var digest, kind ast.String |
| 73 | + var ok bool |
| 74 | + |
| 75 | + // Extract digest |
| 76 | + digest, ok = operands[0].Value.(ast.String) |
| 77 | + if !ok { |
| 78 | + return errors.New("digest must be a string") |
| 79 | + } |
| 80 | + |
| 81 | + if len(operands) > 1 { |
| 82 | + // Extract kind |
| 83 | + kind, ok = operands[1].Value.(ast.String) |
| 84 | + if !ok { |
| 85 | + return errors.New("kind must be a string") |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // Call the shared service |
| 90 | + resp, err := discoverSvc.Discover(bctx.Context, string(digest), string(kind)) |
| 91 | + if err != nil { |
| 92 | + return fmt.Errorf("failed to call discover endpoint: %w", err) |
| 93 | + } |
| 94 | + |
| 95 | + // call the iterator with the output value |
| 96 | + return iter(ast.NewTerm(ast.MustInterfaceToValue(resp.Result))) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// register is a wrapper around topdown.RegisterBuiltinFunc to handle registration |
| 101 | +func register(builtin *ast.Builtin, impl topdown.BuiltinFunc) error { |
| 102 | + topdown.RegisterBuiltinFunc(builtin.Name, impl) |
| 103 | + ast.RegisterBuiltin(builtin) |
| 104 | + return nil |
| 105 | +} |
0 commit comments