functions

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package functions contains the implementation of the functions that are exposed to the CEL environment.

Index

Constants

This section is empty.

Variables

View Source
var Addition = func(_, _ ref.Val) ref.Val {
	return &elements.NodeList{
		NodeList: &sbom.NodeList{},
	}
}
View Source
var AdditionOp = func(...ref.Val) ref.Val {
	return &elements.NodeList{
		NodeList: &sbom.NodeList{},
	}
}
View Source
var Files = func(lhs ref.Val) ref.Val {
	nodeList, err := getTypedNodes(lhs, sbom.Node_FILE)
	if err != nil {
		return types.NewErr("searching for files: %w", err)
	}
	return &nodeList
}

Files returns all the Nodes marked as type file from an element. The function supports documents, nodelists, and nodes. If the node is a file, it will return a NodeList with it as the single node or empty if it is a package.

If the passed type is not supported, the return value will be an error.

View Source
var GetAuthors = func(lhs ref.Val) ref.Val {
	var metadata *sbom.Metadata
	switch v := lhs.Value().(type) {
	case *sbom.Document:
		metadata = v.GetMetadata()
	case *elements.Document:
		metadata = v.GetMetadata()
	case *sbom.Metadata:
		metadata = v
	default:
		return types.NewErr("method unsupported on type %T", lhs.Value())
	}

	reg, err := types.NewRegistry()
	if err != nil {
		return types.NewErrFromString(err.Error())
	}

	if metadata == nil {
		return reg.NativeToValue([]*sbom.Person{})
	}

	if metadata.GetAuthors() == nil {
		return reg.NativeToValue([]*sbom.Person{})
	}

	return reg.NativeToValue(metadata.GetAuthors())
}

GetAuthors returns the document authors in a generic struct

View Source
var GetEdges = func(lhs ref.Val) ref.Val {
	switch v := lhs.Value().(type) {
	case *sbom.NodeList:
		l := []ref.Val{}
		for _, e := range v.Edges {
			l = append(l, &elements.Edge{
				Edge: e,
			})
		}
		return types.NewRefValList(adapter.ProtobomTypeAdapter{}, l)
	default:
		return types.NewErr("argument to GetEdges only applies to NodeList")
	}
}

GetNodes returns the list of nodes of the nodelist

View Source
var GetMetadata = func(lhs ref.Val) ref.Val {
	switch v := lhs.Value().(type) {
	case *sbom.Document:
		return &elements.Metadata{
			Metadata: v.Metadata,
		}
	default:
		return types.NewErr("invalid binding for get_metadata")
	}
}
View Source
var GetNodeList = func(lhs ref.Val) ref.Val {
	switch v := lhs.Value().(type) {
	case *sbom.Document:
		return &elements.NodeList{
			NodeList: v.NodeList,
		}
	default:
		return types.NewErr("invalid binding for get_node_list")
	}
}
View Source
var GetNodes = func(lhs ref.Val) ref.Val {
	switch v := lhs.Value().(type) {
	case *sbom.NodeList:
		l := []ref.Val{}
		for _, n := range v.Nodes {
			l = append(l, &elements.Node{
				Node: n,
			})
		}
		return types.NewRefValList(adapter.ProtobomTypeAdapter{}, l)
	default:
		return types.NewErr("argument to RootNodes only applies to NodeList")
	}
}

GetNodes returns the list of nodes of the nodelist

View Source
var LoadSBOM = func(_, pathVal ref.Val) ref.Val {
	path, ok := pathVal.Value().(string)
	if !ok {
		return types.NewErr("argument to element by id has to be a string")
	}

	f, err := os.Open(path)
	if err != nil {
		return types.NewErr("opening SBOM file: %w", err)
	}

	r := reader.New()
	doc, err := r.ParseStream(f)
	if err != nil {
		return types.NewErr("parsing SBOM: %w", err)
	}

	return &elements.Document{
		Document: doc,
	}
}
View Source
var NodeByID = func(lhs, rawID ref.Val) ref.Val {
	queryID, ok := rawID.Value().(string)
	if !ok {
		return types.NewErr("argument to element by id has to be a string")
	}
	var node *sbom.Node
	switch v := lhs.Value().(type) {
	case *sbom.Document:
		node = v.NodeList.GetNodeByID(queryID)
	case *sbom.NodeList:
		node = v.GetNodeByID(queryID)
	case *sbom.Node:
		if v.Id == queryID {
			node = v
		}
	default:
		return types.NewErr("method unsupported on type %T", lhs.Value())
	}

	if node == nil {
		return nil
	}

	return &elements.Node{
		Node: node,
	}
}

NodeByID returns a Node matching the specified ID

View Source
var NodeGetOriginators = func(lhs ref.Val) ref.Val {
	switch v := lhs.Value().(type) {
	case *sbom.Node:
		reg, err := types.NewRegistry()
		if err != nil {
			return types.NewErrFromString(err.Error())
		}

		if v.GetOriginators() == nil {
			return reg.NativeToValue([]*sbom.Person{})
		}

		return reg.NativeToValue(v.GetOriginators())
	default:
		return types.NewErr("GetOriginators only applies to Node")
	}
}

NodeGetSuppliers returns the list of nodes of the nodelist

View Source
var NodeGetSuppliers = func(lhs ref.Val) ref.Val {
	switch v := lhs.Value().(type) {
	case *sbom.Node:
		reg, err := types.NewRegistry()
		if err != nil {
			return types.NewErrFromString(err.Error())
		}

		if v.GetSuppliers() == nil {
			return reg.NativeToValue([]*sbom.Person{})
		}

		return reg.NativeToValue(v.GetSuppliers())
	default:
		return types.NewErr("GetSuppliers only applies to Node")
	}
}

NodeGetSuppliers returns the list of nodes of the nodelist

View Source
var NodesByPurlType = func(lhs, rhs ref.Val) ref.Val {
	purlType, ok := rhs.Value().(string)
	if !ok {
		return types.NewErr("argument to GetNodesByPurlType must be a string")
	}

	var nl *sbom.NodeList
	switch v := lhs.Value().(type) {
	case *sbom.Document:
		nl = v.NodeList.GetNodesByPurlType(purlType)
	case *sbom.NodeList:
		nl = v.GetNodesByPurlType(purlType)
	default:
		return types.NewErr("method unsupported on type %T", lhs.Value())
	}

	return &elements.NodeList{
		NodeList: nl,
	}
}
View Source
var Packages = func(lhs ref.Val) ref.Val {
	nodeList, err := getTypedNodes(lhs, sbom.Node_PACKAGE)
	if err != nil {
		return types.NewErr("searching for packages: %w", err)
	}
	return &nodeList
}

Packages returns a NodeList with any packages in the lhs element. It supports Documents, NodeLists and Nodes. If a node is provided it will return a NodeList with the single node it is a package, otherwise it will be empty.

If lhs is an unsupprted type, Packages will return an error.

View Source
var RelateNodeListAtID = func(vals ...ref.Val) ref.Val {
	if len(vals) != 4 {
		return types.NewErr("invalid number of arguments for RealteAtNodeListAtID")
	}
	id, ok := vals[2].Value().(string)
	if !ok {
		return types.NewErr("node id has to be a string")
	}

	_, ok = vals[3].Value().(string)
	if !ok {
		return types.NewErr("relationship type has has to be a string")
	}

	nodelist, ok := vals[1].(*elements.NodeList)
	if !ok {
		return types.NewErr("could not cast nodelist")
	}

	switch v := vals[0].Value().(type) {
	case *sbom.Document:

		if err := v.NodeList.RelateNodeListAtID(nodelist.NodeList, id, sbom.Edge_dependsOn); err != nil {
			return types.NewErr("relating nodelist: %w", err)
		}
		return &elements.Document{
			Document: v,
		}
	case *sbom.NodeList:
		if err := v.RelateNodeListAtID(nodelist.NodeList, id, sbom.Edge_dependsOn); err != nil {
			return types.NewErr("relating nodelist: %w", err)
		}
		return &elements.NodeList{
			NodeList: v,
		}
	default:
		return types.NewErr("method unsupported on type %T", vals[0].Value())
	}
}

RelateNodeListAtID relates a nodelist at the specified ID

View Source
var RootNodes = func(lhs ref.Val) ref.Val {
	switch v := lhs.Value().(type) {
	case *sbom.Document:
		l := []ref.Val{}
		for _, n := range v.GetRootNodes() {
			l = append(l, &elements.Node{
				Node: n,
			})
		}
		return types.NewRefValList(adapter.ProtobomTypeAdapter{}, l)
	case *sbom.NodeList:
		l := []ref.Val{}
		for _, n := range v.GetRootNodes() {
			l = append(l, &elements.Node{
				Node: n,
			})
		}
		return types.NewRefValList(adapter.ProtobomTypeAdapter{}, l)
	default:
		return types.NewErr("argument to RootNodes only applies to Document and NodeList")
	}
}
View Source
var ToDocument = func(lhs ref.Val) ref.Val {
	var nodelist *elements.NodeList
	switch v := lhs.Value().(type) {
	case *sbom.NodeList:
		nodelist = &elements.NodeList{NodeList: v}
	case *elements.NodeList:
		nodelist = v
	case *elements.Node:
		nodelist = v.ToNodeList()
	case *sbom.Node:
		nodelist = (&elements.Node{Node: v}).ToNodeList()
	default:
		return types.NewErr("unable to convert element to document")
	}

	reconnectOrphanNodes(nodelist)

	doc := &elements.Document{
		Document: &sbom.Document{
			Metadata: &sbom.Metadata{
				Id:      "",
				Version: "1",
				Name:    "Protobom/CEL generated document",
				Date:    timestamppb.Now(),
				Tools: []*sbom.Tool{
					{
						Name:    "Protobom/CEL",
						Version: version.GetVersionInfo().GitVersion,
						Vendor:  "Protobom",
					},
				},
				Authors: []*sbom.Person{},
				Comment: "This document was generated by Protobom/CEL",
			},
			NodeList: nodelist.NodeList,
		},
	}

	return doc
}

ToDocument converts an element into a full document. This is useful when you need to convert an evaluation result to a document to output them as a native SBOM.

View Source
var ToNodeList = func(lhs ref.Val) ref.Val {
	switch v := lhs.Value().(type) {
	case *sbom.Document:
		return &elements.NodeList{
			NodeList: v.NodeList,
		}
	case *elements.Document:
		return &elements.NodeList{
			NodeList: v.NodeList,
		}
	case *sbom.NodeList:
		return &elements.NodeList{
			NodeList: v,
		}
	case *elements.NodeList:
		return v
	case *elements.Node:
		nl := v.ToNodeList()
		return nl
	case *sbom.Node:
		nl := (&elements.Node{Node: v}).ToNodeList()
		return nl
	default:
		return types.NewErr("type %T does not support conversion to NodeList", v)
	}
}

ToNodeList takes a node and returns a new NodeList with that nodelist with the node as the only member.

Functions

func GetNodesByName

func GetNodesByName(lhs, rhs ref.Val) ref.Val

GetNodesByName takes a name and returns a list of nodes matching it

func NodeDescendants

func NodeDescendants(vals ...ref.Val) ref.Val

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL