Documentation
¶
Overview ¶
Example ¶
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/protobom/protobom/pkg/reader"
"google.golang.org/protobuf/encoding/protojson"
"github.com/protobom/storage/backends/ent"
)
func main() {
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
dbFile := filepath.Join(cwd, "example.db")
// Remove example.db if it already exists.
if err := os.Remove(dbFile); err != nil && !errors.Is(err, os.ErrNotExist) {
panic(err)
}
rdr := reader.New()
backend := ent.NewBackend().WithDatabaseFile(dbFile)
if err := backend.InitClient(); err != nil {
panic(err)
}
defer backend.CloseClient()
sbom, err := rdr.ParseFile(filepath.Join(cwd, "testdata", "sbom.cdx.json"))
if err != nil {
panic(err)
}
if err := backend.Store(sbom, nil); err != nil {
panic(err)
}
retrieved, err := backend.Retrieve(sbom.GetMetadata().GetId(), nil)
if err != nil {
panic(err)
}
// Remove source data URI to allow comparison.
retrieved.GetMetadata().GetSourceData().Uri = nil
// Produces a different output than the standard [encoding/json] package,
// which does not operate correctly on protocol buffer messages.
output, err := protojson.MarshalOptions{UseProtoNames: true}.Marshal(retrieved)
if err != nil {
panic(err)
}
// The output produced by the protojson package is intentionally non-deterministic.
// Format with standard encoding/json package for consistent whitespace.
formatted := bytes.NewBuffer([]byte{})
if err := json.Indent(formatted, output, "", " "); err != nil {
panic(err)
}
fmt.Println(formatted.String())
}
Output: { "metadata": { "id": "urn:uuid:3e671687-395b-41f5-a30f-a58921a69b79", "version": "1", "date": "2020-04-13T20:20:39Z", "source_data": { "format": "application/vnd.cyclonedx+json;version=1.5", "hashes": { "2": "1ecc17c081f9a0b452b1d8a0d846901bcc40508f", "3": "71a3948e45c0bcd83a617ed94674079778d10a0578932e6e536533339b1bbea5", "5": "2cc9ac5ab13a8074463e85996e91aa96916a08d33fc3aff9129dd44b24b850884f6176898a21d48dabd9f3824a2dd6bcc1f350e8f13d4be1c564211d1108e43c" }, "size": "5263" } }, "node_list": { "nodes": [ { "id": "protobom-auto--000000001", "name": "Acme Application", "version": "9.1.1", "primary_purpose": [ "APPLICATION" ] }, { "id": "pkg:npm/acme/component@1.0.0", "name": "tomcat-catalina", "version": "9.0.14", "licenses": [ "Apache-2.0" ], "license_concluded": "Apache-2.0", "identifiers": { "1": "pkg:npm/acme/component@1.0.0" }, "hashes": { "1": "3942447fac867ae5cdb3229b658f4d48", "2": "e6b1000b94e835ffd37f4c6dcbdad43f4b48a02a", "3": "f498a8ff2dd007e29c2074f5e4b01a9a01775c3ff3aeaf6906ea503bc5791b7b", "5": "e8f33e424f3f4ed6db76a482fde1a5298970e442c531729119e37991884bdffab4f9426b7ee11fccd074eeda0634d71697d6f88a460dce0ac8d627a29f7d1282" }, "primary_purpose": [ "LIBRARY" ] }, { "id": "protobom-auto--000000003", "name": "mylibrary", "version": "1.0.0", "primary_purpose": [ "LIBRARY" ] } ], "edges": [ { "type": "contains", "from": "protobom-auto--000000001", "to": [ "pkg:npm/acme/component@1.0.0", "protobom-auto--000000003" ] }, { "type": "dependsOn", "from": "pkg:npm/acme/component@1.0.0", "to": [ "pkg:npm/acme/component@1.0.0" ] } ], "root_elements": [ "protobom-auto--000000001" ] } }
Index ¶
- Variables
- func GenerateUUID(msg proto.Message) (uuid.UUID, error)
- type Annotation
- type Annotations
- type Backend
- func (backend *Backend) AddAnnotationToDocuments(name, value string, documentIDs ...string) error
- func (backend *Backend) AddAnnotationToNodes(name, value string, nodeIDs ...string) error
- func (backend *Backend) AddDocumentAnnotations(documentID, name string, values ...string) error
- func (backend *Backend) AddNodeAnnotations(nodeID, name string, values ...string) error
- func (backend *Backend) ClearDocumentAnnotations(documentIDs ...string) error
- func (backend *Backend) ClearNodeAnnotations(nodeIDs ...string) error
- func (backend *Backend) CloseClient()
- func (backend *Backend) Debug() *Backend
- func (backend *Backend) Ent() *ent.Client
- func (backend *Backend) GetDocumentAnnotations(documentID string, names ...string) (ent.Annotations, error)
- func (backend *Backend) GetDocumentUniqueAnnotation(documentID, name string) (string, error)
- func (backend *Backend) GetDocumentsByAnnotation(name string, values ...string) ([]*sbom.Document, error)
- func (backend *Backend) GetDocumentsByID(ids ...string) ([]*sbom.Document, error)
- func (backend *Backend) GetExternalReferencesByDocumentID(id string, types ...string) ([]*sbom.ExternalReference, error)
- func (backend *Backend) GetNodeAnnotations(nodeID string, names ...string) (ent.Annotations, error)
- func (backend *Backend) GetNodeUniqueAnnotation(nodeID, name string) (string, error)
- func (backend *Backend) GetNodesByAnnotation(name string, values ...string) ([]*sbom.Node, error)
- func (backend *Backend) GetNodesByID(ids ...string) ([]*sbom.Node, error)
- func (backend *Backend) InitClient() error
- func (backend *Backend) RemoveDocumentAnnotations(documentID, name string, values ...string) error
- func (backend *Backend) RemoveNodeAnnotations(nodeID, name string, values ...string) error
- func (backend *Backend) Retrieve(id string, _ *storage.RetrieveOptions) (doc *sbom.Document, err error)
- func (backend *Backend) SetDocumentAnnotations(documentID, name string, values ...string) error
- func (backend *Backend) SetDocumentUniqueAnnotation(documentID, name, value string) error
- func (backend *Backend) SetNodeAnnotations(nodeID, name string, values ...string) error
- func (backend *Backend) SetNodeUniqueAnnotation(nodeID, name, value string) error
- func (backend *Backend) Store(doc *sbom.Document, opts *storage.StoreOptions) error
- func (backend *Backend) WithAnnotation(name, value string, unique bool) *Backend
- func (backend *Backend) WithAnnotations(annotations Annotations) *Backend
- func (backend *Backend) WithBackendOptions(opts *BackendOptions) *Backend
- func (backend *Backend) WithDatabaseFile(file string) *Backend
- type BackendOptions
- type Option
- type TxFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrMultipleDocuments = errors.New("multiple documents matching ID") ErrMissingDocument = errors.New("no documents matching IDs") )
Functions ¶
Types ¶
type Annotation ¶ added in v0.1.5
type Annotation = ent.Annotation
Annotation is the model entity for the Annotation schema.
type Annotations ¶ added in v0.1.5
type Annotations = ent.Annotations
Annotations is a parsable slice of Annotation.
type Backend ¶
type Backend struct {
// Options is the set of options common to all ent Backends.
Options *BackendOptions
// contains filtered or unexported fields
}
Backend implements the protobom.pkg.storage.Backend interface.
func NewBackend ¶
func (*Backend) AddAnnotationToDocuments ¶ added in v0.1.4
AddAnnotationToDocuments applies a single named annotation value to multiple documents.
func (*Backend) AddAnnotationToNodes ¶ added in v0.2.0
AddAnnotationToNodes applies a single named annotation value to multiple nodes.
func (*Backend) AddDocumentAnnotations ¶ added in v0.2.0
AddDocumentAnnotations applies multiple named annotation values to a single document.
func (*Backend) AddNodeAnnotations ¶ added in v0.2.0
AddNodeAnnotations applies multiple named annotation values to a single node.
func (*Backend) ClearDocumentAnnotations ¶ added in v0.2.0
ClearDocumentAnnotations removes all annotations from the specified documents.
func (*Backend) ClearNodeAnnotations ¶ added in v0.2.0
ClearNodeAnnotations removes all annotations from the specified nodes.
func (*Backend) CloseClient ¶
func (backend *Backend) CloseClient()
func (*Backend) GetDocumentAnnotations ¶ added in v0.1.4
func (backend *Backend) GetDocumentAnnotations(documentID string, names ...string) (ent.Annotations, error)
GetDocumentAnnotations gets all annotations for the specified document, limited to a set of annotation names if specified.
func (*Backend) GetDocumentUniqueAnnotation ¶ added in v0.1.5
GetDocumentUniqueAnnotation gets the value for a unique annotation.
func (*Backend) GetDocumentsByAnnotation ¶ added in v0.1.4
func (backend *Backend) GetDocumentsByAnnotation(name string, values ...string) ([]*sbom.Document, error)
GetDocumentsByAnnotation gets all documents having the specified named annotation, limited to a set of annotation values if specified.
func (*Backend) GetDocumentsByID ¶ added in v0.1.1
func (*Backend) GetExternalReferencesByDocumentID ¶ added in v0.1.1
func (*Backend) GetNodeAnnotations ¶ added in v0.2.0
GetNodeAnnotations gets all annotations for the specified node, limited to a set of annotation names if specified.
func (*Backend) GetNodeUniqueAnnotation ¶ added in v0.2.0
GetNodeUniqueAnnotation gets the value for a unique annotation.
func (*Backend) GetNodesByAnnotation ¶ added in v0.2.0
GetNodesByAnnotation gets all nodes having the specified named annotation, limited to a set of annotation values if specified.
func (*Backend) GetNodesByID ¶ added in v0.2.0
func (*Backend) InitClient ¶
func (*Backend) RemoveDocumentAnnotations ¶ added in v0.2.0
RemoveDocumentAnnotations removes all annotations with the specified name from the document, limited to a set of annotation values if specified.
func (*Backend) RemoveNodeAnnotations ¶ added in v0.2.0
RemoveNodeAnnotations removes all annotations with the specified name from the node, limited to a set of annotation values if specified.
func (*Backend) Retrieve ¶
func (backend *Backend) Retrieve(id string, _ *storage.RetrieveOptions) (doc *sbom.Document, err error)
Retrieve implements the storage.Retriever interface.
func (*Backend) SetDocumentAnnotations ¶ added in v0.2.0
SetDocumentAnnotations explicitly sets the named annotations for the specified document.
func (*Backend) SetDocumentUniqueAnnotation ¶ added in v0.2.0
SetDocumentUniqueAnnotation sets a named annotation value that is unique to the specified document.
func (*Backend) SetNodeAnnotations ¶ added in v0.2.0
SetNodeAnnotations explicitly sets the named annotations for the specified node.
func (*Backend) SetNodeUniqueAnnotation ¶ added in v0.2.0
SetNodeUniqueAnnotation sets a named annotation value that is unique to the specified node.
func (*Backend) WithAnnotation ¶ added in v0.1.5
func (*Backend) WithAnnotations ¶ added in v0.1.5
func (backend *Backend) WithAnnotations(annotations Annotations) *Backend
func (*Backend) WithBackendOptions ¶
func (backend *Backend) WithBackendOptions(opts *BackendOptions) *Backend
func (*Backend) WithDatabaseFile ¶
type BackendOptions ¶
type BackendOptions struct {
// DatabaseFile is the file path of the SQLite database to be created.
DatabaseFile string
// Annotations is a slice of annotations to apply to stored document.
Annotations
// Debug configures the ent client to output all SQL statements during execution.
Debug bool
}
BackendOptions contains options specific to the protobom ent backend.
func NewBackendOptions ¶
func NewBackendOptions() *BackendOptions
NewBackendOptions creates a new BackendOptions for the backend.
type Option ¶
type Option func(*Backend)
Option represents a single configuration option for the ent backend.
func WithBackendOptions ¶
func WithBackendOptions(opts *BackendOptions) Option